emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] fill paragraph: break after sentence.


From: Aaron Ecay
Subject: Re: [O] fill paragraph: break after sentence.
Date: Tue, 06 Sep 2016 11:38:56 +0100
User-agent: Notmuch/0.22+21~g7e6e23c (https://notmuchmail.org) Emacs/25.1.50.2 (x86_64-unknown-linux-gnu)

Hi Uwe,

The following code is what I use.  It uses filladapt mode, but doesn’t
work with auto-fill (I manually refill paragraphs with M-q as I’m
writing).  I wrote the code a long time ago, it works for me, YMMV,
etc.  Hope it is helpful.

#+BEGIN_SRC emacs-lisp
  (defun awe-org-fill-paragraph-function (&rest ignore)
    (let ((bounds (cons (save-excursion (backward-paragraph) (point))
                        (save-excursion (forward-paragraph) (point))))
          beg end end-marker)
      (save-excursion
        (goto-char (cdr bounds))
        (skip-chars-backward "\n")
        (setq end-marker (point-marker))
        (setq end (make-marker))
        (goto-char (car bounds))
        (skip-chars-forward "\n")
        (catch 'exit
          (while t
            (setq beg (point))
            (forward-sentence)
            (move-marker end (point))
            (save-excursion
              (goto-char beg)
              (when (and fill-prefix
                         (not (looking-at-p (regexp-quote fill-prefix))))
                (insert fill-prefix))
              (while (re-search-forward "\n *" end t)
                (replace-match " ")))
            (setq beg (point))
            (skip-chars-forward " \n")
            (move-marker end (point))
            (when (>= (point) end-marker)
              (throw 'exit t))
            (when (/= beg end)
              (delete-region beg end))
            (insert "\n"))))
      (set-marker end-marker nil)
      (set-marker end nil)))

  (defun awe-org-setup-fill-hook ()
    (setq-local sentence-end-base
                (rx (any ".?!")
                    (? "[fn:" (+ (any "0-9" "a-f")) "]")
                    (* (any "]\"'”)}"))))
    (when (featurep 'filladapt)
      (setq-local fill-paragraph-function #'awe-org-fill-paragraph-function)
      (make-local-variable 'filladapt-token-table)
      (make-local-variable 'filladapt-token-match-table)
      (make-local-variable 'filladapt-token-conversion-table)
      (cl-pushnew `(,(rx "#+" (or "caption" "CAPTION") ": ") org-caption)
                  filladapt-token-table :test #'equal)
      (cl-pushnew '(org-caption org-caption)
                  filladapt-token-match-table :test #'equal)
      (cl-pushnew '(org-caption . exact)
                  filladapt-token-conversion-table :test #'equal))
    (visual-line-mode 1)
    (auto-fill-mode 0))
  (add-hook 'org-mode-hook #'awe-org-setup-fill-hook)
#+END_SRC

-- 
Aaron Ecay



reply via email to

[Prev in Thread] Current Thread [Next in Thread]