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