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