How do I get Emacs to fill sentences, but not paragraphs?

前端 未结 10 1067
予麋鹿
予麋鹿 2020-12-23 02:23

I\'ve seen at least two recommendations on StackOverflow to insert newlines between sentences when editing LaTeX documents. The reason being that the practice facilitates so

10条回答
  •  一向
    一向 (楼主)
    2020-12-23 03:04

    I have been meaning to do this forever and I recently found this blog post which worked fairly well for me. So here is (a slightly modified version of) what I have been using for a few days.

    (defun auto-fill-by-sentences ()
      (if (looking-back (sentence-end))
          ;; Break at a sentence
          (progn
            (LaTeX-newline)
            t)
        ;; Fall back to the default
        (do-auto-fill)))
    (add-hook 'LaTeX-mode-hook (lambda () (setq auto-fill-function 'auto-fill-by-sentences)))
    
    ;; Modified from http://pleasefindattached.blogspot.com/2011/12/emacsauctex-sentence-fill-greatly.html
    (defadvice LaTeX-fill-region-as-paragraph (around LaTeX-sentence-filling)
      "Start each sentence on a new line."
      (let ((from (ad-get-arg 0))
            (to-marker (set-marker (make-marker) (ad-get-arg 1)))
            tmp-end)
        (while (< from (marker-position to-marker))
          (forward-sentence)
          ;; might have gone beyond to-marker---use whichever is smaller:
          (ad-set-arg 1 (setq tmp-end (min (point) (marker-position to-marker))))
          ad-do-it
          (ad-set-arg 0 (setq from (point)))
          (unless (or (looking-back "^\\s *")
                      (looking-at "\\s *$"))
            (LaTeX-newline)))
        (set-marker to-marker nil)))
    (ad-activate 'LaTeX-fill-region-as-paragraph)
    

提交回复
热议问题