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