Last active
July 13, 2019 19:20
-
-
Save jdahm/2a1f578a193ad605010cdc913e53e30f to your computer and use it in GitHub Desktop.
Complement to fill-paragraph. For LaTeX documents with version tracking, it is best practice to have sentences begin after newlines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun fill-sentences () | |
"Fills the current paragraph or region, starting each sentence on a new line." | |
(interactive) | |
(save-excursion | |
;; Determine region to operate on | |
(let ((beginning-of-region (if (use-region-p) (region-beginning) | |
(save-excursion (backward-paragraph) (point)))) | |
(end-of-region (if (use-region-p) (region-end) | |
(save-excursion (forward-paragraph) (point))))) | |
(goto-char beginning-of-region) | |
;; Loop over each sentence in the region | |
(while (< (point) end-of-region) | |
;; Determine the sentence bounds | |
(let ((start-of-sentence (point))) | |
(forward-sentence) | |
;; Fill the sentence, breaking at `fill-column' | |
(if (derived-mode-p 'LaTex-mode) | |
(LaTeX-fill-region start-of-sentence (point)) | |
(fill-region start-of-sentence (point))) | |
;; Delete extra space | |
(delete-horizontal-space) | |
;; If this does not end with a newline, add one and indent | |
(if (and (not (equal (point) end-of-region)) | |
(not (char-equal (char-after) ?\n))) | |
(newline-and-indent))))))) | |
(add-hook 'LaTeX-mode-hook | |
(define-key LaTeX-mode-map (kbd "M-j") #'fill-sentences)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment