Ship more local files
[emacs.git] / .emacs.d / elisp / local / messages-are-flowing.el
1 ;;; messages-are-flowing.el --- visible indication when composing "flowed" emails -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2017 Magnus Henoch
4
5 ;; Author: Magnus Henoch <magnus.henoch@gmail.com>
6 ;; Keywords: mail
7 ;; Version: 0.1
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; If you're writing emails to people who might not view them on a
25 ;; display with the same width as yours, you probably want to send the
26 ;; messages as "flowed" (as per RFC 2646) in order to let the
27 ;; recipient's device disregard the line breaks in your message and
28 ;; rewrap the text for readability. In `message-mode', you can do
29 ;; that by turning on the `use-hard-newlines' minor mode.
30 ;;
31 ;; However, you probably want some of your newlines to stay put, for
32 ;; paragraph breaks, and for content where you really do want to break
33 ;; the lines yourself. You can do that with `use-hard-newlines', but
34 ;; it doesn't show you where it's going to put "hard" newlines and
35 ;; where it's going to put "soft" ones.
36 ;;
37 ;; That's where `messages-are-flowing' comes in. It marks all "hard"
38 ;; newlines with a `⏎' symbol, so that you can have an idea about what
39 ;; parts of your message might be reflowed when the recipient reads it.
40 ;;
41 ;; To activate `messages-are-flowing', add the following to your .emacs:
42 ;;
43 ;; (with-eval-after-load "message"
44 ;; (add-hook 'message-mode-hook 'messages-are-flowing-use-and-mark-hard-newlines))
45
46 ;;; Code:
47
48 ;;;###autoload
49 (defun messages-are-flowing-use-and-mark-hard-newlines ()
50 "Turn on `use-hard-newlines', and make hard newlines visible.
51 The main use of this is to send \"flowed\" email messages, where
52 line breaks within paragraphs are adjusted by the recipient's
53 device, such that messages remain readable on narrow displays."
54 (interactive)
55 (use-hard-newlines)
56 (add-hook 'after-change-functions 'messages-are-flowing--mark-hard-newlines nil t))
57
58 (defun messages-are-flowing--mark-hard-newlines (beg end &rest _ignore)
59 "Visibly mark hard newlines between BEG and END.
60 For each hard newline, add a display property that makes it visible.
61 For each soft newline, remove any display property."
62 ;; Uncomment for debugging:
63 ;;(interactive (list (point-min) (point-max)))
64 (save-excursion
65 (goto-char beg)
66 (while (search-forward "\n" end t)
67 (let ((pos (1- (point))))
68 (if (get-text-property pos 'hard)
69 ;; Use `copy-sequence', because display property values must not be `eq'!
70 (add-text-properties pos (1+ pos) (list 'display (copy-sequence "⏎\n")))
71 (remove-text-properties pos (1+ pos) '(display nil)))))))
72
73 (provide 'messages-are-flowing)
74 ;;; messages-are-flowing.el ends here