Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jdahm
Last active July 13, 2019 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdahm/2a1f578a193ad605010cdc913e53e30f to your computer and use it in GitHub Desktop.
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.
(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