]> code.delx.au - gnu-emacs/blob - lisp/mail/rmailedit.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / mail / rmailedit.el
1 ;;; rmailedit.el --- "RMAIL edit mode" Edit the current message
2
3 ;; Copyright (C) 1985, 1994, 2001-2016 Free Software Foundation, Inc.
4
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: mail
7 ;; Package: rmail
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'rmail)
29
30 (defcustom rmail-edit-mode-hook nil
31 "List of functions to call when editing an RMAIL message."
32 :type 'hook
33 :version "21.1"
34 :group 'rmail-edit)
35
36
37 (defvar rmail-edit-map
38 (let ((map (make-sparse-keymap)))
39 ;; Make a keymap that inherits text-mode-map.
40 (set-keymap-parent map text-mode-map)
41 (define-key map "\C-c\C-c" 'rmail-cease-edit)
42 (define-key map "\C-c\C-]" 'rmail-abort-edit)
43 map))
44
45 (declare-function rmail-summary-disable "rmailsum" ())
46
47 ;; We can't straightforwardly make this derive from text-mode, because
48 ;; we need to bind (rmail-buffer-swapped) around the text-mode call. :(
49 (defun rmail-edit-mode ()
50 "Major mode for editing the contents of an Rmail message.
51 The editing commands are the same as in Text mode, together with
52 two commands to return to regular Rmail:
53 * \\[rmail-abort-edit] cancels any changes and returns to Rmail
54 * \\[rmail-cease-edit] makes them permanent.
55 This function runs the hooks `text-mode-hook' and `rmail-edit-mode-hook'.
56 \\{rmail-edit-map}"
57 (if (rmail-summary-exists)
58 (with-current-buffer rmail-summary-buffer
59 (rmail-summary-disable)))
60 ;; Prevent change-major-mode-hook from unswapping the buffers.
61 (let ((rmail-buffer-swapped nil))
62 (delay-mode-hooks (text-mode))
63 (use-local-map rmail-edit-map)
64 (setq major-mode 'rmail-edit-mode)
65 (setq mode-name "RMAIL Edit")
66 (if (boundp 'mode-line-modified)
67 (setq mode-line-modified (default-value 'mode-line-modified))
68 (setq mode-line-format (default-value 'mode-line-format)))
69 ;; Don't turn off auto-saving based on the size of the buffer
70 ;; because that code does not understand buffer-swapping.
71 (make-local-variable 'auto-save-include-big-deletions)
72 (setq auto-save-include-big-deletions t)
73 ;; If someone uses C-x C-s, don't clobber the rmail file (bug#2625).
74 (add-hook 'write-region-annotate-functions
75 'rmail-write-region-annotate nil t)
76 (run-mode-hooks 'rmail-edit-mode-hook)))
77
78 ;; Rmail Edit mode is suitable only for specially formatted data.
79 (put 'rmail-edit-mode 'mode-class 'special)
80 \f
81
82 (defvar rmail-old-text)
83 (defvar rmail-old-mime-state)
84 (defvar rmail-old-pruned nil
85 "Non-nil means the message being edited originally had pruned headers.")
86 (put 'rmail-old-pruned 'permanent-local t)
87
88 (defvar rmail-old-headers nil
89 "Holds the headers of this message before editing started.")
90 (put 'rmail-old-headers 'permanent-local t)
91
92 ;; Everything we use from here is a defsubst.
93 (eval-when-compile
94 (require 'rmailmm))
95
96 ;;;###autoload
97 (defun rmail-edit-current-message ()
98 "Edit the contents of this message."
99 (interactive)
100 (if (zerop rmail-total-messages)
101 (error "No messages in this buffer"))
102 (rmail-modify-format)
103 (make-local-variable 'rmail-old-pruned)
104 (setq rmail-old-pruned (rmail-msg-is-pruned))
105 (rmail-edit-mode)
106 (set (make-local-variable 'rmail-old-mime-state)
107 (and rmail-enable-mime
108 ;; If you use something else, you are on your own.
109 (eq rmail-mime-feature 'rmailmm)
110 (rmail-mime-message-p)
111 (let ((entity (get-text-property (point-min) 'rmail-mime-entity)))
112 ;; rmailmm has got its hands on the message.
113 ;; Even if the message is in `raw' state, boundaries etc
114 ;; are still missing. All we can do is insert the real
115 ;; raw message. (Bug#9840)
116 ;; FIXME? Since the 2012-09-17 changes to rmail-mime,
117 ;; can we just use that function now?
118 (when (and entity
119 (not (equal "text/plain"
120 (car (rmail-mime-entity-type entity)))))
121 (let ((inhibit-read-only t))
122 (erase-buffer)
123 (insert-buffer-substring
124 rmail-view-buffer
125 (aref (rmail-mime-entity-header entity) 0)
126 (aref (rmail-mime-entity-body entity) 1)))
127 (goto-char (point-min))
128 ;; t = decoded; raw = raw.
129 (aref (aref (rmail-mime-entity-display entity) 0) 0)))))
130 (make-local-variable 'rmail-old-text)
131 (setq rmail-old-text
132 (save-restriction
133 (widen)
134 (buffer-substring (point-min) (point-max))))
135 (make-local-variable 'rmail-old-headers)
136 (setq rmail-old-headers (rmail-edit-headers-alist t))
137 (setq buffer-read-only nil)
138 (setq buffer-undo-list nil)
139 ;; Whether the buffer is initially marked as modified or not
140 ;; depends on whether or not the underlying rmail buffer was so marked.
141 ;; Given the way this works, it has to.
142 ;; If you kill the edit buffer, you've killed your rmail buffer.
143 (force-mode-line-update)
144 (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
145 (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
146 (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
147 (message "%s" (substitute-command-keys
148 "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
149
150
151 (declare-function rmail-summary-enable "rmailsum" ())
152
153 (defun rmail-cease-edit ()
154 "Finish editing message; switch back to Rmail proper."
155 (interactive)
156 (if (rmail-summary-exists)
157 (with-current-buffer rmail-summary-buffer
158 (rmail-summary-enable)))
159 (widen)
160 (goto-char (point-min))
161 ;; This is far from ideal. The edit may have inadvertently
162 ;; removed the blank line at the end of the headers, but there
163 ;; are almost certainly other blank lines.
164 (or (search-forward "\n\n" nil t)
165 (error "There must be a blank line at the end of the headers"))
166 ;; Disguise any "From " lines so they don't start a new message.
167 (goto-char (point-min))
168 ;; This tries to skip the mbox From. FIXME less fragile to go to EOH?
169 (if (or rmail-old-mime-state
170 (not rmail-old-pruned))
171 (forward-line 1))
172 ;; When editing a non-MIME message, rmail-show-message-1 has unescaped
173 ;; ^>*From lines according to rmail-mbox-format. We are editing
174 ;; the message as it was displayed, and need to put the escapes when done.
175 ;; When editing a MIME message, we are editing the "raw" message.
176 ;; ^>*From lines have not been escaped, but we still need to ensure
177 ;; a "^From " line is escaped so as not to break later parsing (?).
178 ;; With ^>+From lines, we have no way of knowing whether the person
179 ;; doing the editing escaped them or not, so it seems best to leave
180 ;; them alone. (This all assumes you are using rmailmm rather than
181 ;; something else that behaves differently.)
182 (let ((fromline (if (or (eq 'mboxo rmail-mbox-format)
183 rmail-mime-decoded)
184 "^From "
185 "^>*From "))
186 case-fold-search)
187 (while (re-search-forward fromline nil t)
188 (beginning-of-line)
189 (insert ">")
190 (forward-line)))
191 ;; Make sure buffer ends with a blank line so as not to run this
192 ;; message together with the following one.
193 (goto-char (point-max))
194 (rmail-ensure-blank-line)
195 (let ((old rmail-old-text)
196 (pruned rmail-old-pruned)
197 (mime-state rmail-old-mime-state)
198 ;; People who know what they are doing might have modified the
199 ;; buffer's encoding if editing the message included inserting
200 ;; characters that were unencodable by the original message's
201 ;; encoding. Make note of the new encoding and use it for
202 ;; encoding the edited message.
203 (edited-coding buffer-file-coding-system)
204 new-headers
205 character-coding is-text-message coding-system
206 headers-end limit)
207 ;; Make sure `edited-coding' can safely encode the edited message.
208 (setq edited-coding
209 (select-safe-coding-system (point-min) (point-max) edited-coding))
210 ;; Go back to Rmail mode, but carefully.
211 (force-mode-line-update)
212 (let ((rmail-buffer-swapped nil)) ; Prevent change-major-mode-hook
213 ; from unswapping the buffers.
214 (kill-all-local-variables)
215 (rmail-mode-1)
216 (if (boundp 'tool-bar-map)
217 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
218 (setq buffer-undo-list t)
219 (rmail-variables))
220 ;; If text has really changed, mark message as edited.
221 ;; FIXME we should do the comparison before escaping From lines.
222 (unless (and (= (length old) (- (point-max) (point-min)))
223 (string= old (buffer-substring (point-min) (point-max))))
224 (setq old nil)
225 (goto-char (point-min))
226 (search-forward "\n\n")
227 (setq headers-end (point-marker))
228 (goto-char (point-min))
229 (save-restriction
230 (narrow-to-region (point) headers-end)
231 ;; If they changed the message's encoding, rewrite the charset=
232 ;; header for them, so that subsequent rmail-show-message
233 ;; decodes it correctly.
234 (let* ((buffer-read-only nil)
235 (new-coding (coding-system-base edited-coding))
236 (mime-charset (symbol-name
237 (or (coding-system-get new-coding :mime-charset)
238 (if (coding-system-equal new-coding
239 'undecided)
240 'us-ascii
241 new-coding))))
242 old-coding mime-beg mime-end content-type)
243 (if (re-search-forward rmail-mime-charset-pattern nil 'move)
244 (setq mime-beg (match-beginning 1)
245 mime-end (match-end 1)
246 old-coding (coding-system-from-name (match-string 1)))
247 (setq content-type (mail-fetch-field "Content-Type")))
248 (cond
249 ;; No match for rmail-mime-charset-pattern, but there was some
250 ;; other Content-Type. We should not insert another. (Bug#4624)
251 (content-type)
252 ((null old-coding)
253 ;; If there was no charset= spec, insert one.
254 (backward-char 1)
255 (insert "Content-type: text/plain; charset=" mime-charset "\n"))
256 ((not (coding-system-equal (coding-system-base old-coding)
257 new-coding))
258 (goto-char mime-end)
259 (delete-region mime-beg mime-end)
260 (insert mime-charset)))))
261 (setq new-headers (rmail-edit-headers-alist t))
262 (rmail-swap-buffers-maybe)
263 (narrow-to-region (rmail-msgbeg rmail-current-message)
264 (rmail-msgend rmail-current-message))
265 (goto-char (point-min))
266 (setq limit (search-forward "\n\n"))
267 (save-restriction
268 ;; All 3 of the functions we call below assume the buffer was
269 ;; narrowed to just the headers of the message.
270 (narrow-to-region (point-min) limit)
271 (setq character-coding
272 (mail-fetch-field "content-transfer-encoding")
273 is-text-message (rmail-is-text-p)
274 coding-system (if (and edited-coding
275 (not (coding-system-equal
276 (coding-system-base edited-coding)
277 'undecided)))
278 edited-coding
279 (rmail-get-coding-system))))
280 (if character-coding
281 (setq character-coding (downcase character-coding)))
282
283 (goto-char limit)
284 (let ((inhibit-read-only t))
285 (let ((data-buffer (current-buffer))
286 (end (copy-marker (point) t)))
287 (with-current-buffer rmail-view-buffer
288 (encode-coding-region headers-end (point-max) coding-system
289 data-buffer))
290 (delete-region end (point-max)))
291
292 ;; Apply to the mbox buffer any changes in header fields
293 ;; that the user made while editing in the view buffer.
294 (rmail-edit-update-headers (rmail-edit-diff-headers
295 rmail-old-headers new-headers))
296
297 ;; Re-apply content-transfer-encoding, if any, on the message body.
298 (cond
299 ((string= character-coding "quoted-printable")
300 (mail-quote-printable-region (point) (point-max)))
301 ((and (string= character-coding "base64") is-text-message)
302 (base64-encode-region (point) (point-max)))
303 ((and (eq character-coding 'uuencode) is-text-message)
304 (error "uuencoded messages are not supported"))))
305 (rmail-set-attribute rmail-edited-attr-index t))
306 ;;??? BROKEN perhaps.
307 ;;; (if (boundp 'rmail-summary-vector)
308 ;;; (aset rmail-summary-vector (1- rmail-current-message) nil))
309 (rmail-show-message)
310 (rmail-toggle-header (if pruned 1 0))
311 ;; Restore mime display state.
312 (and mime-state (rmail-mime nil mime-state)))
313 (run-hooks 'rmail-mode-hook))
314
315 (defun rmail-abort-edit ()
316 "Abort edit of current message; restore original contents."
317 (interactive)
318 (widen)
319 (delete-region (point-min) (point-max))
320 (insert rmail-old-text)
321 (rmail-cease-edit)
322 (rmail-highlight-headers))
323 \f
324 (defun rmail-edit-headers-alist (&optional widen markers)
325 "Return an alist of the headers of the message in the current buffer.
326 Each element has the form (HEADER-NAME . ENTIRE-STRING).
327 ENTIRE-STRING includes the name of the header field (which is HEADER-NAME)
328 and has a final newline.
329 If part of the text is not valid as a header field, HEADER-NAME
330 is an integer and we use consecutive integers.
331
332 If WIDEN is non-nil, operate on the entire buffer.
333
334 If MARKERS is non-nil, the value looks like
335 \(HEADER-NAME ENTIRE-STRING BEG-MARKER END-MARKER)."
336 (let (header-alist (no-good-header-count 1))
337 (save-excursion
338 (save-restriction
339 (if widen (widen))
340 (goto-char (point-min))
341 (search-forward "\n\n")
342 (narrow-to-region (point-min) (1- (point)))
343 (goto-char (point-min))
344 (while (not (eobp))
345 (let ((start (point))
346 name header)
347 ;; Match the name.
348 (if (looking-at "[ \t]*\\([^:\n \t]\\(\\|[^:\n]*[^:\n \t]\\)\\)[ \t]*:")
349 (setq name (match-string-no-properties 1))
350 (setq name no-good-header-count
351 no-good-header-count (1+ no-good-header-count)))
352 (forward-line 1)
353 (while (looking-at "[ \t]")
354 (forward-line 1))
355 (setq header (buffer-substring-no-properties start (point)))
356 (if markers
357 (push (list header (copy-marker start) (point-marker))
358 header-alist)
359 (push (cons name header) header-alist))))))
360 (nreverse header-alist)))
361
362
363 (defun rmail-edit-diff-headers (old-headers new-headers)
364 "Compare OLD-HEADERS and NEW-HEADERS and return field differences.
365 The value is a list of three lists, (INSERTED DELETED CHANGED).
366
367 INSERTED's elements describe inserted header fields
368 and each looks like (AFTER-WHAT INSERT-WHAT)
369 INSERT-WHAT is the header field to insert (a member of NEW-HEADERS).
370 AFTER-WHAT is the field to insert it after (a member of NEW-HEADERS)
371 or else nil to insert it at the beginning.
372
373 DELETED's elements are elements of OLD-HEADERS.
374 CHANGED's elements have the form (OLD . NEW)
375 where OLD is a element of OLD-HEADERS and NEW is an element of NEW-HEADERS."
376
377 (let ((reverse-new (reverse new-headers))
378 inserted deleted changed)
379 (dolist (old old-headers)
380 (let ((new (assoc (car old) new-headers)))
381 ;; If it's in OLD-HEADERS and has no new counterpart,
382 ;; it is a deletion.
383 (if (null new)
384 (push old deleted)
385 ;; If it has a new counterpart, maybe it was changed.
386 (unless (equal (cdr old) (cdr new))
387 (push (cons old new) changed))
388 ;; Remove the new counterpart, since it has been spoken for.
389 (setq new-headers (remq new new-headers)))))
390 ;; Look at the new headers with no old counterpart.
391 (dolist (new new-headers)
392 (let ((prev (cadr (member new reverse-new))))
393 ;; Mark each one as an insertion.
394 ;; Record the previous new header, to insert it after that.
395 (push (list prev new) inserted)))
396 ;; It is crucial to return the insertions in buffer order
397 ;; so that `rmail-edit-update-headers' can insert a field
398 ;; after a new field.
399 (list (nreverse inserted)
400 (nreverse deleted)
401 (nreverse changed))))
402
403 (defun rmail-edit-update-headers (header-diff)
404 "Edit the mail headers in the buffer based on HEADER-DIFF.
405 HEADER-DIFF should be a return value from `rmail-edit-diff-headers'."
406 (let ((buf-headers (rmail-edit-headers-alist nil t)))
407 ;; Change all the fields scheduled for being changed.
408 (dolist (chg (nth 2 header-diff))
409 (let* ((match (assoc (cdar chg) buf-headers))
410 (end (marker-position (nth 2 match))))
411 (goto-char end)
412 ;; Insert the new, then delete the old.
413 ;; That avoids collapsing markers.
414 (insert-before-markers (cddr chg))
415 (delete-region (nth 1 match) end)
416 ;; Remove the old field from BUF-HEADERS.
417 (setq buf-headers (delq match buf-headers))
418 ;; Update BUF-HEADERS to show the changed field.
419 (push (list (cddr chg) (point-marker)
420 (copy-marker (- (point) (length (cddr chg))))
421 (point-marker))
422 buf-headers)))
423 ;; Delete all the fields scheduled for deletion.
424 ;; We do deletion after changes
425 ;; because when two fields look alike and get replaced by one,
426 ;; the first of them is considered changed
427 ;; and the second is considered deleted.
428 (dolist (del (nth 1 header-diff))
429 (let ((match (assoc (cdr del) buf-headers)))
430 (delete-region (nth 1 match) (nth 2 match))))
431 ;; Insert all the fields scheduled for insertion.
432 (dolist (ins (nth 0 header-diff))
433 (let* ((new (cadr ins))
434 (after (car ins))
435 (match (assoc (cdr after) buf-headers)))
436 (goto-char (if match (nth 2 match) (point-min)))
437 (insert (cdr new))
438 ;; Update BUF-HEADERS to show the inserted field.
439 (push (list (cdr new)
440 (copy-marker (- (point) (length (cdr new))))
441 (point-marker))
442 buf-headers)))
443 ;; Disconnect the markers
444 (dolist (hdr buf-headers)
445 (set-marker (nth 1 hdr) nil)
446 (set-marker (nth 2 hdr) nil))))
447
448 (provide 'rmailedit)
449
450 ;; Local Variables:
451 ;; generated-autoload-file: "rmail-loaddefs.el"
452 ;; End:
453
454 ;;; rmailedit.el ends here