4 ms·
I had copied this Emacs macro just for doing that. ;; one sentence per line (defun wrap-at-sentences () "Fills the current paragraph, but starts each sentenc
by mad44 4y ago
I had copied this Emacs macro just for doing that.
;; one sentence per line
(defun wrap-at-sentences ()
"Fills the current paragraph, but starts each sentence on a new line."
(interactive)
(save-excursion
;; Select the entire paragraph.
(mark-paragraph)
;; Move to the start of the paragraph.
(goto-char (region-beginning))
;; Record the location of the end of the paragraph.
(setq end-of-paragraph (region-end))
;; Wrap lines with 'hard' newlines (i.e., real line breaks).
(let ((use-hard-newlines 't))
;; Loop over each sentence in the paragraph.
(while (< (point) end-of-paragraph)
;; Determine the region spanned by the sentence.
(setq start-of-sentence (point))
(forward-sentence)
;; Wrap the sentence with hard newlines.
(fill-region start-of-sentence (point))
;; Delete the whitespace following the period, if any.
(while (char-equal (char-syntax (preceding-char)) ?\s)
(delete-char -1))
;; Insert a newline before the next sentence.
(insert "\n")))))
(global-set-key (kbd "M-j") 'wrap-at-sentences)
- capableweb 4y agoIronically, this snippet seems to be a bit too much wrapped! You need to have a empty line between each line on HN for it to format correctly. If you also ident it by four space, it gets marked as code in the markup.
- yvdriess 4y agoI just love the irony that one needs to manually insert newlines to copy over code to automate inserting newlines.
- mad44 4y agoFine, here you go, with the line breaks ;; one sentence per line (defun wrap-at-sentences () "Fills the current paragraph, but starts each sentence on a new line." (interactive) (save-excursion ;; Select the entire paragraph. (mark-paragraph) ;; Move to the start of the paragraph. (goto-char (region-beginning)) ;; Record the location of the end of the paragraph. (setq end-of-paragraph (region-end)) ;; Wrap lines with 'hard' newlines (i.e., real line breaks). (let ((use-hard-newlines 't)) ;; Loop over each sentence in the paragraph. (while (< (point) end-of-paragraph) ;; Determine the region spanned by the sentence. (setq start-of-sentence (point)) (forward-sentence) ;; Wrap the sentence with hard newlines. (fill-region start-of-sentence (point)) ;; Delete the whitespace following the period, if any. (while (char-equal (char-syntax (preceding-char)) ?\s) (delete-char -1)) ;; Insert a newline before the next sentence. (insert "\n"))))) (global-set-key (kbd "M-j") 'wrap-at-sentences)