From fd2bb1afe16487c909674c989de60b54083d1f3c Mon Sep 17 00:00:00 2001 From: Ryan Kavanagh Date: Mon, 1 Aug 2022 17:36:53 -0400 Subject: Customize whitespace-cleanup to not clobber '-- ' signature separators in message mode --- dot_emacs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'dot_emacs') diff --git a/dot_emacs b/dot_emacs index bd649d2..bbb1bd2 100644 --- a/dot_emacs +++ b/dot_emacs @@ -468,6 +468,49 @@ Otherwise split the current paragraph into one sentence per line." ;; ocaml assistant :ensure t) +(use-package message + :config + (defun whitespace-cleanup-mail () + "Kill the whitespace in a buffer while preserving the last instance of '-- '." + (interactive) + (save-excursion + (save-restriction + (widen) + (let ((signature-delimiter + (progn + ;; Find the last occurence of "-- " in the buffer + ;; and set signature-delimiter to the position of the first - + ;; This means we must subtract 2, because re-search-backward + ;; returns the end of the occurence found + (goto-char (point-max)) + (- (re-search-backward "^-- $" nil t) 2)))) + (when signature-delimiter + (progn + ;; Run whitespace-cleanup on the region + ;; [start, signature-delimiter]. Include the first dash + ;; to avoid deleting any empty lines leading up to the + ;; delimiter. + (narrow-to-region (point-min) signature-delimiter) + (whitespace-cleanup) + (widen) + ;; Run it on the region [signature-delimiter, end). + ;; We include the signature delimiter so that we do not + ;; delete any empty lines immediately following it. + ;; A side-effect is that the first line might become "^--$". + ;; This means that we must re-add the space at the end of + ;; the first line. Easiest just replace the first line of the + ;; buffer by "^-- $". + (narrow-to-region signature-delimiter (point-max)) + (whitespace-cleanup) + (goto-char (point-min)) + (kill-whole-line 0) + (insert "-- "))))))) + :hook + ((message-mode . (lambda () (remove-hook 'before-save-hook 'whitespace-cleanup t))) + (before-save . whitespace-cleanup-mail)) + :custom + (message-send-mail-function 'message-send-mail-with-sendmail)) + (use-package mingus ;; mpd client :ensure t) -- cgit v1.2.3