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.
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/>.
30 (defcustom rmail-edit-mode-hook nil
31 "List of functions to call when editing an RMAIL message."
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)
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'.
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)
86 (defun rmail-edit-current-message ()
87 "Edit the contents of this message."
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))
94 (make-local-variable 'rmail-old-text)
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."
120 (if (rmail-summary-exists)
121 (with-current-buffer rmail-summary-buffer
122 (rmail-summary-enable)))
124 ;; Disguise any "From " lines so they don't start a new message.
126 (goto-char (point-min))
127 (or rmail-old-pruned (forward-line 1))
128 (while (re-search-forward "^>*From " nil t)
132 ;; Make sure buffer ends with a blank line so as not to run this
133 ;; message together with the following one.
135 (goto-char (point-max))
136 (rmail-ensure-blank-line))
137 (let ((old rmail-old-text)
138 (pruned rmail-old-pruned)
139 ;; People who know what they are doing might have modified the
140 ;; buffer's encoding if editing the message included inserting
141 ;; characters that were unencodable by the original message's
142 ;; encoding. Make note of the new encoding and use it for
143 ;; encoding the edited message.
144 (edited-coding buffer-file-coding-system)
146 character-coding is-text-message coding-system
148 ;; Make sure `edited-coding' can safely encode the edited message.
150 (select-safe-coding-system (point-min) (point-max) edited-coding))
151 ;; Go back to Rmail mode, but carefully.
152 (force-mode-line-update)
153 (let ((rmail-buffer-swapped nil)) ; Prevent change-major-mode-hook
154 ; from unswapping the buffers.
155 (kill-all-local-variables)
157 (if (boundp 'tool-bar-map)
158 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
159 (setq buffer-undo-list t)
161 ;; If text has really changed, mark message as edited.
162 (unless (and (= (length old) (- (point-max) (point-min)))
163 (string= old (buffer-substring (point-min) (point-max))))
165 (goto-char (point-min))
166 ;; If they changed the message's encoding, rewrite the charset=
167 ;; header for them, so that subsequent rmail-show-message
168 ;; decodes it correctly.
169 (let ((buffer-read-only nil)
170 (new-coding (coding-system-base edited-coding))
171 old-coding mime-charset mime-beg mime-end)
172 (when (re-search-forward rmail-mime-charset-pattern
173 (1- (save-excursion (search-forward "\n\n")))
175 (setq mime-beg (match-beginning 1)
176 mime-end (match-end 1)
177 old-coding (coding-system-from-name (match-string 1))))
180 (or (coding-system-get new-coding :mime-charset)
181 (if (coding-system-equal new-coding 'undecided)
186 ;; If there was no charset= spec, insert one.
187 (insert "Content-type: text/plain; charset=" mime-charset "\n"))
188 ((not (coding-system-equal (coding-system-base old-coding)
190 (delete-region mime-beg mime-end)
191 (insert mime-charset))))
192 (goto-char (point-min))
193 (search-forward "\n\n")
194 (setq headers-end (point))
195 (setq new-headers (rmail-edit-headers-alist t))
196 (rmail-swap-buffers-maybe)
197 (narrow-to-region (rmail-msgbeg rmail-current-message)
198 (rmail-msgend rmail-current-message))
202 (goto-char (point-min))
203 (search-forward "\n\n" nil t)))
204 ;; All 3 of the functions we call below assume the buffer was
205 ;; narrowed to just the headers of the message.
206 (narrow-to-region (rmail-msgbeg rmail-current-message) limit)
207 (setq character-coding
208 (mail-fetch-field "content-transfer-encoding")
209 is-text-message (rmail-is-text-p)
210 coding-system (if (and edited-coding
211 (not (coding-system-equal
212 (coding-system-base edited-coding)
215 (rmail-get-coding-system))))
217 (setq character-coding (downcase character-coding)))
220 (let ((inhibit-read-only t))
221 (let ((data-buffer (current-buffer))
222 (end (copy-marker (point) t)))
223 (with-current-buffer rmail-view-buffer
224 (encode-coding-region headers-end (point-max) coding-system
226 (delete-region end (point-max)))
228 ;; Apply to the mbox buffer any changes in header fields
229 ;; that the user made while editing in the view buffer.
230 (rmail-edit-update-headers (rmail-edit-diff-headers
231 rmail-old-headers new-headers))
233 ;; Re-apply content-transfer-encoding, if any, on the message body.
235 ((string= character-coding "quoted-printable")
236 (mail-quote-printable-region (point) (point-max)))
237 ((and (string= character-coding "base64") is-text-message)
238 (base64-encode-region (point) (point-max)))
239 ((and (eq character-coding 'uuencode) is-text-message)
240 (error "uuencoded messages are not supported"))))
241 (rmail-set-attribute rmail-edited-attr-index t))
242 ;;??? BROKEN perhaps.
243 ;;; (if (boundp 'rmail-summary-vector)
244 ;;; (aset rmail-summary-vector (1- rmail-current-message) nil))
247 (rmail-toggle-header (if pruned 1 0))))
248 (run-hooks 'rmail-mode-hook))
250 (defun rmail-abort-edit ()
251 "Abort edit of current message; restore original contents."
254 (delete-region (point-min) (point-max))
255 (insert rmail-old-text)
257 (rmail-highlight-headers))
259 (defun rmail-edit-headers-alist (&optional widen markers)
260 "Return an alist of the headers of the message in the current buffer.
261 Each element has the form (HEADER-NAME . ENTIRE-STRING).
262 ENTIRE-STRING includes the name of the header field (which is HEADER-NAME)
263 and has a final newline.
264 If part of the text is not valid as a header field, HEADER-NAME
265 is an integer and we use consecutive integers.
267 If WIDEN is non-nil, operate on the entire buffer.
269 If MARKERS is non-nil, the value looks like
270 \(HEADER-NAME ENTIRE-STRING BEG-MARKER END-MARKER)."
271 (let (header-alist (no-good-header-count 1))
275 (goto-char (point-min))
276 (search-forward "\n\n")
277 (narrow-to-region (point-min) (1- (point)))
278 (goto-char (point-min))
280 (let ((start (point))
283 (if (looking-at "[ \t]*\\([^:\n \t]\\(\\|[^:\n]*[^:\n \t]\\)\\)[ \t]*:")
284 (setq name (match-string-no-properties 1))
285 (setq name no-good-header-count
286 no-good-header-count (1+ no-good-header-count)))
288 (while (looking-at "[ \t]")
290 (setq header (buffer-substring-no-properties start (point)))
292 (push (list header (copy-marker start) (point-marker))
294 (push (cons name header) header-alist))))))
295 (nreverse header-alist)))
298 (defun rmail-edit-diff-headers (old-headers new-headers)
299 "Compare OLD-HEADERS and NEW-HEADERS and return field differences.
300 The value is a list of three lists, (INSERTED DELETED CHANGED).
302 INSERTED's elements describe inserted header fields
303 and each looks like (AFTER-WHAT INSERT-WHAT)
304 INSERT-WHAT is the header field to insert (a member of NEW-HEADERS).
305 AFTER-WHAT is the field to insert it after (a member of NEW-HEADERS)
306 or else nil to insert it at the beginning.
308 DELETED's elements are elements of OLD-HEADERS.
309 CHANGED's elements have the form (OLD . NEW)
310 where OLD is a element of OLD-HEADERS and NEW is an element of NEW-HEADERS."
312 (let ((reverse-new (reverse new-headers))
313 inserted deleted changed)
314 (dolist (old old-headers)
315 (let ((new (assoc (car old) new-headers)))
316 ;; If it's in OLD-HEADERS and has no new counterpart,
320 ;; If it has a new counterpart, maybe it was changed.
321 (unless (equal (cdr old) (cdr new))
322 (push (cons old new) changed))
323 ;; Remove the new counterpart, since it has been spoken for.
324 (setq new-headers (remq new new-headers)))))
325 ;; Look at the new headers with no old counterpart.
326 (dolist (new new-headers)
327 (let ((prev (cadr (member new reverse-new))))
328 ;; Mark each one as an insertion.
329 ;; Record the previous new header, to insert it after that.
330 (push (list prev new) inserted)))
331 ;; It is crucial to return the insertions in buffer order
332 ;; so that `rmail-edit-update-headers' can insert a field
333 ;; after a new field.
334 (list (nreverse inserted)
336 (nreverse changed))))
338 (defun rmail-edit-update-headers (header-diff)
339 "Edit the mail headers in the buffer based on HEADER-DIFF.
340 HEADER-DIFF should be a return value from `rmail-edit-diff-headers'."
341 (let ((buf-headers (rmail-edit-headers-alist nil t)))
342 ;; Change all the fields scheduled for being changed.
343 (dolist (chg (nth 2 header-diff))
344 (let* ((match (assoc (cdar chg) buf-headers))
345 (end (marker-position (nth 2 match))))
347 ;; Insert the new, then delete the old.
348 ;; That avoids collapsing markers.
349 (insert-before-markers (cddr chg))
350 (delete-region (nth 1 match) end)
351 ;; Remove the old field from BUF-HEADERS.
352 (setq buf-headers (delq match buf-headers))
353 ;; Update BUF-HEADERS to show the changed field.
354 (push (list (cddr chg) (point-marker)
355 (copy-marker (- (point) (length (cddr chg))))
358 ;; Delete all the fields scheduled for deletion.
359 ;; We do deletion after changes
360 ;; because when two fields look alike and get replaced by one,
361 ;; the first of them is considered changed
362 ;; and the second is considered deleted.
363 (dolist (del (nth 1 header-diff))
364 (let ((match (assoc (cdr del) buf-headers)))
365 (delete-region (nth 1 match) (nth 2 match))))
366 ;; Insert all the fields scheduled for insertion.
367 (dolist (ins (nth 0 header-diff))
368 (let* ((new (cadr ins))
370 (match (assoc (cdr after) buf-headers)))
371 (goto-char (if match (nth 2 match) (point-min)))
373 ;; Update BUF-HEADERS to show the inserted field.
374 (push (list (cdr new)
375 (copy-marker (- (point) (length (cdr new))))
378 ;; Disconnect the markers
379 (dolist (hdr buf-headers)
380 (set-marker (nth 1 hdr) nil)
381 (set-marker (nth 2 hdr) nil))))
385 ;; arch-tag: 9524f335-12cc-4e95-9e9b-3208dc30550b
386 ;;; rmailedit.el ends here