(defgroup notmuch-coolj nil "Automatic wrapping of long lines when displaying notmuch articles." :group 'notmuch) (defcustom notmuch-coolj-prefix-regexp " *\\(>+ +\\)?" "A regexp matching potential line prefixes.") (defun notmuch-coolj-wrap-region (beg end) "Wrap lines in the region." (goto-char beg) (forward-line -1) (while (not (>= (point) end)) (notmuch-coolj-wrap-line) (forward-line))) (defun notmuch-coolj-wrap-line () "Wrap the current line, if necessary." (let ((prefix (notmuch-coolj-determine-prefix)) (start (point)) (end (make-marker)) (width (window-width))) (set-marker end (save-excursion (end-of-line) (point))) (while (> end (+ (point) width)) (forward-char (window-width)) (if (re-search-backward "[^ ]\\( \\)" start t) (progn (goto-char (match-beginning 1)) (insert-before-markers ?\n) (re-search-forward "\\( +\\)[^ ]" nil t) (delete-region (match-beginning 1) (match-end 1)) (backward-char 1) (insert-before-markers prefix))) (beginning-of-line)))) (defun notmuch-coolj-determine-prefix () "Determine the prefix for the current line." (if (looking-at notmuch-coolj-prefix-regexp) (buffer-substring-no-properties (match-beginning 0) (match-end 0)))) (provide 'notmuch-coolj)