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