rmailedit.el 15 KB
Newer Older
1 2
;;; rmailedit.el --- "RMAIL edit mode"  Edit the current message

Glenn Morris's avatar
Glenn Morris committed
3 4
;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
;;   2008, 2009  Free Software Foundation, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

;; Maintainer: FSF
;; Keywords: mail

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

Glenn Morris's avatar
Glenn Morris committed
28
(require 'rmail)
29 30 31 32 33 34 35 36

(defcustom rmail-edit-mode-hook nil
  "List of functions to call when editing an RMAIL message."
  :type 'hook
  :version "21.1"
  :group 'rmail-edit)


37 38 39 40 41 42 43 44
(defvar rmail-edit-map
  (let ((map (make-sparse-keymap)))
    ;; Make a keymap that inherits text-mode-map.
    (set-keymap-parent map text-mode-map)
    (define-key map "\C-c\C-c" 'rmail-cease-edit)
    (define-key map "\C-c\C-]" 'rmail-abort-edit)
    map))

45
(declare-function rmail-summary-disable "rmailsum" ())
46 47

(defun rmail-edit-mode ()
Glenn Morris's avatar
Glenn Morris committed
48 49 50 51
  "Major mode for editing the contents of an Rmail message.
The editing commands are the same as in Text mode, together with
two commands to return to regular Rmail:
  *  \\[rmail-abort-edit] cancels any changes and returns to Rmail
52
  *  \\[rmail-cease-edit] makes them permanent.
Glenn Morris's avatar
Glenn Morris committed
53
This function runs the hooks `text-mode-hook' and `rmail-edit-mode-hook'.
54 55
\\{rmail-edit-map}"
  (if (rmail-summary-exists)
56
      (with-current-buffer rmail-summary-buffer
57
	(rmail-summary-disable)))
Glenn Morris's avatar
Glenn Morris committed
58 59
  ;; Prevent change-major-mode-hook from unswapping the buffers.
  (let ((rmail-buffer-swapped nil))
60 61 62 63 64 65 66
    (delay-mode-hooks (text-mode))
    (use-local-map rmail-edit-map)
    (setq major-mode 'rmail-edit-mode)
    (setq mode-name "RMAIL Edit")
    (if (boundp 'mode-line-modified)
	(setq mode-line-modified (default-value 'mode-line-modified))
      (setq mode-line-format (default-value 'mode-line-format)))
67 68 69 70
    ;; Don't turn off auto-saving based on the size of the buffer
    ;; because that code does not understand buffer-swapping.
    (make-local-variable 'auto-save-include-big-deletions)
    (setq auto-save-include-big-deletions t)
71 72 73
    ;; If someone uses C-x C-s, don't clobber the rmail file (bug#2625).
    (add-hook 'write-region-annotate-functions
	      'rmail-write-region-annotate nil t)
74 75
    (run-mode-hooks 'rmail-edit-mode-hook)))

Glenn Morris's avatar
Glenn Morris committed
76 77
;; Rmail Edit mode is suitable only for specially formatted data.
(put 'rmail-edit-mode 'mode-class 'special)
Glenn Morris's avatar
Glenn Morris committed
78

Glenn Morris's avatar
Glenn Morris committed
79 80 81 82

(defvar rmail-old-text)
(defvar rmail-old-pruned nil
  "Non-nil means the message being edited originally had pruned headers.")
83 84
(put 'rmail-old-pruned 'permanent-local t)

85 86 87 88
(defvar rmail-old-headers nil
  "Holds the headers of this message before editing started.")
(put 'rmail-old-headers 'permanent-local t)

89 90 91 92
;;;###autoload
(defun rmail-edit-current-message ()
  "Edit the contents of this message."
  (interactive)
Glenn Morris's avatar
Glenn Morris committed
93
  (if (zerop rmail-total-messages)
94
      (error "No messages in this buffer"))
95
  (rmail-modify-format)
96 97
  (make-local-variable 'rmail-old-pruned)
  (setq rmail-old-pruned (rmail-msg-is-pruned))
98
  (rmail-edit-mode)
99 100 101 102 103 104 105
  (make-local-variable 'rmail-old-text)
  (setq rmail-old-text
	(save-restriction
	  (widen)
	  (buffer-substring (point-min) (point-max))))
  (make-local-variable 'rmail-old-headers)
  (setq rmail-old-headers (rmail-edit-headers-alist t))
106 107
  (setq buffer-read-only nil)
  (setq buffer-undo-list nil)
Glenn Morris's avatar
Glenn Morris committed
108
  ;; Whether the buffer is initially marked as modified or not
Glenn Morris's avatar
Glenn Morris committed
109
  ;; depends on whether or not the underlying rmail buffer was so marked.
Glenn Morris's avatar
Glenn Morris committed
110 111
  ;; Given the way this works, it has to.
  ;; If you kill the edit buffer, you've killed your rmail buffer.
112 113 114 115 116 117 118
  (force-mode-line-update)
  (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
	   (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
      (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
    (message "%s" (substitute-command-keys
		   "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))

Glenn Morris's avatar
Glenn Morris committed
119 120 121

(declare-function rmail-summary-enable "rmailsum" ())

122 123 124 125
(defun rmail-cease-edit ()
  "Finish editing message; switch back to Rmail proper."
  (interactive)
  (if (rmail-summary-exists)
126
      (with-current-buffer rmail-summary-buffer
127 128
	(rmail-summary-enable)))
  (widen)
129 130 131 132 133 134
  (goto-char (point-min))
  ;; This is far from ideal.  The edit may have inadvertently
  ;; removed the blank line at the end of the headers, but there
  ;; are almost certainly other blank lines.
  (or (search-forward "\n\n" nil t)
      (error "There must be a blank line at the end of the headers"))
135
  ;; Disguise any "From " lines so they don't start a new message.
136 137 138 139 140 141
  (goto-char (point-min))
  (or rmail-old-pruned (forward-line 1))
  (while (re-search-forward "^>*From " nil t)
    (beginning-of-line)
    (insert ">")
    (forward-line))
142 143
  ;; Make sure buffer ends with a blank line so as not to run this
  ;; message together with the following one.
144 145
  (goto-char (point-max))
  (rmail-ensure-blank-line)
146
  (let ((old rmail-old-text)
Glenn Morris's avatar
Glenn Morris committed
147
	(pruned rmail-old-pruned)
148 149 150 151 152 153
	;; People who know what they are doing might have modified the
	;; buffer's encoding if editing the message included inserting
	;; characters that were unencodable by the original message's
	;; encoding.  Make note of the new encoding and use it for
	;; encoding the edited message.
	(edited-coding buffer-file-coding-system)
154
	new-headers
155
	character-coding is-text-message coding-system
156
	headers-end limit)
157 158 159
    ;; Make sure `edited-coding' can safely encode the edited message.
    (setq edited-coding
	  (select-safe-coding-system (point-min) (point-max) edited-coding))
160 161
    ;; Go back to Rmail mode, but carefully.
    (force-mode-line-update)
162 163
    (let ((rmail-buffer-swapped nil)) ; Prevent change-major-mode-hook
                                      ; from unswapping the buffers.
164 165 166 167 168 169 170 171 172 173 174 175
      (kill-all-local-variables)
      (rmail-mode-1)
      (if (boundp 'tool-bar-map)
	  (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
      (setq buffer-undo-list t)
      (rmail-variables))
    ;; If text has really changed, mark message as edited.
    (unless (and (= (length old) (- (point-max) (point-min)))
		 (string= old (buffer-substring (point-min) (point-max))))
      (setq old nil)
      (goto-char (point-min))
      (search-forward "\n\n")
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
      (setq headers-end (point-marker))
      (goto-char (point-min))
      (save-restriction
	(narrow-to-region (point) headers-end)
	;; If they changed the message's encoding, rewrite the charset=
	;; header for them, so that subsequent rmail-show-message
	;; decodes it correctly.
	(let* ((buffer-read-only nil)
	       (new-coding (coding-system-base edited-coding))
	       (mime-charset (symbol-name
			      (or (coding-system-get new-coding :mime-charset)
				  (if (coding-system-equal new-coding
							   'undecided)
				      'us-ascii
				    new-coding))))
	       old-coding mime-beg mime-end content-type)
	  (if (re-search-forward rmail-mime-charset-pattern nil 'move)
	      (setq mime-beg (match-beginning 1)
		    mime-end (match-end 1)
		    old-coding (coding-system-from-name (match-string 1)))
	    (setq content-type (mail-fetch-field "Content-Type")))
	  (cond
	   ;; No match for rmail-mime-charset-pattern, but there was some
	   ;; other Content-Type.  We should not insert another.  (Bug#4624)
	   (content-type)
	   ((null old-coding)
	    ;; If there was no charset= spec, insert one.
	    (backward-char 1)
	    (insert "Content-type: text/plain; charset=" mime-charset "\n"))
	   ((not (coding-system-equal (coding-system-base old-coding)
				      new-coding))
	    (goto-char mime-end)
	    (delete-region mime-beg mime-end)
	    (insert mime-charset)))))
210
      (setq new-headers (rmail-edit-headers-alist t))
211
      (rmail-swap-buffers-maybe)
212
      (narrow-to-region (rmail-msgbeg rmail-current-message)
213
			(rmail-msgend rmail-current-message))
214 215
      (goto-char (point-min))
      (setq limit (search-forward "\n\n"))
216 217 218
      (save-restriction
	;; All 3 of the functions we call below assume the buffer was
	;; narrowed to just the headers of the message.
219
	(narrow-to-region (point-min) limit)
220 221 222
	(setq character-coding
	      (mail-fetch-field "content-transfer-encoding")
	      is-text-message (rmail-is-text-p)
223 224 225 226 227 228
	      coding-system (if (and edited-coding
				     (not (coding-system-equal
					   (coding-system-base edited-coding)
					   'undecided)))
				edited-coding
			      (rmail-get-coding-system))))
229 230 231
      (if character-coding
	  (setq character-coding (downcase character-coding)))

232
      (goto-char limit)
233 234 235 236 237 238 239
      (let ((inhibit-read-only t))
	(let ((data-buffer (current-buffer))
	      (end (copy-marker (point) t)))
	  (with-current-buffer rmail-view-buffer
	    (encode-coding-region headers-end (point-max) coding-system
				  data-buffer))
	  (delete-region end (point-max)))
240

241 242 243 244 245
	;; Apply to the mbox buffer any changes in header fields
	;; that the user made while editing in the view buffer.
	(rmail-edit-update-headers (rmail-edit-diff-headers
				    rmail-old-headers new-headers))

Glenn Morris's avatar
Glenn Morris committed
246
	;; Re-apply content-transfer-encoding, if any, on the message body.
247 248
	(cond
	 ((string= character-coding "quoted-printable")
249
	  (mail-quote-printable-region (point) (point-max)))
250
	 ((and (string= character-coding "base64") is-text-message)
251
	  (base64-encode-region (point) (point-max)))
252
	 ((and (eq character-coding 'uuencode) is-text-message)
Glenn Morris's avatar
Glenn Morris committed
253 254
	  (error "uuencoded messages are not supported"))))
      (rmail-set-attribute rmail-edited-attr-index t))
255
    ;;??? BROKEN perhaps.
Glenn Morris's avatar
Glenn Morris committed
256 257
;;;    (if (boundp 'rmail-summary-vector)
;;;	(aset rmail-summary-vector (1- rmail-current-message) nil))
258 259
    (rmail-show-message)
    (rmail-toggle-header (if pruned 1 0)))
260 261 262 263 264 265 266 267 268 269
  (run-hooks 'rmail-mode-hook))

(defun rmail-abort-edit ()
  "Abort edit of current message; restore original contents."
  (interactive)
  (widen)
  (delete-region (point-min) (point-max))
  (insert rmail-old-text)
  (rmail-cease-edit)
  (rmail-highlight-headers))
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

(defun rmail-edit-headers-alist (&optional widen markers)
  "Return an alist of the headers of the message in the current buffer.
Each element has the form (HEADER-NAME . ENTIRE-STRING).
ENTIRE-STRING includes the name of the header field (which is HEADER-NAME)
and has a final newline.
If part of the text is not valid as a header field, HEADER-NAME
is an integer and we use consecutive integers.

If WIDEN is non-nil, operate on the entire buffer.

If MARKERS is non-nil, the value looks like
 \(HEADER-NAME ENTIRE-STRING BEG-MARKER END-MARKER)."
  (let (header-alist (no-good-header-count 1))
    (save-excursion
      (save-restriction
	(if widen (widen))
	(goto-char (point-min))
	(search-forward "\n\n")
	(narrow-to-region (point-min) (1- (point)))
	(goto-char (point-min))
	(while (not (eobp))
	  (let ((start (point))
		name header)
	    ;; Match the name.
	    (if (looking-at "[ \t]*\\([^:\n \t]\\(\\|[^:\n]*[^:\n \t]\\)\\)[ \t]*:")
		(setq name (match-string-no-properties 1))
	      (setq name no-good-header-count
		    no-good-header-count (1+ no-good-header-count)))
	    (forward-line 1)
	    (while (looking-at "[ \t]")
	      (forward-line 1))
	    (setq header (buffer-substring-no-properties start (point)))
	    (if markers
		(push (list header (copy-marker start) (point-marker))
		      header-alist)
	      (push (cons name header) header-alist))))))
    (nreverse header-alist)))


(defun rmail-edit-diff-headers (old-headers new-headers)
  "Compare OLD-HEADERS and NEW-HEADERS and return field differences.
The value is a list of three lists, (INSERTED DELETED CHANGED).

INSERTED's elements describe inserted header fields
and each looks like (AFTER-WHAT INSERT-WHAT)
INSERT-WHAT is the header field to insert (a member of NEW-HEADERS).
AFTER-WHAT is the field to insert it after (a member of NEW-HEADERS)
or else nil to insert it at the beginning.

DELETED's elements are elements of OLD-HEADERS.
CHANGED's elements have the form (OLD . NEW)
where OLD is a element of OLD-HEADERS and NEW is an element of NEW-HEADERS."

  (let ((reverse-new (reverse new-headers))
	inserted deleted changed)
    (dolist (old old-headers)
      (let ((new (assoc (car old) new-headers)))
	;; If it's in OLD-HEADERS and has no new counterpart,
	;; it is a deletion.
	(if (null new)
	    (push old deleted)
	  ;; If it has a new counterpart, maybe it was changed.
	  (unless (equal (cdr old) (cdr new))
	    (push (cons old new) changed))
	  ;; Remove the new counterpart, since it has been spoken for.
	  (setq new-headers (remq new new-headers)))))
    ;; Look at the new headers with no old counterpart.
    (dolist (new new-headers)
      (let ((prev (cadr (member new reverse-new))))
340 341 342
	;; Mark each one as an insertion.
	;; Record the previous new header, to insert it after that.
	(push (list prev new) inserted)))
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    ;; It is crucial to return the insertions in buffer order
    ;; so that `rmail-edit-update-headers' can insert a field
    ;; after a new field.
    (list (nreverse inserted)
	  (nreverse deleted)
	  (nreverse changed))))

(defun rmail-edit-update-headers (header-diff)
  "Edit the mail headers in the buffer based on HEADER-DIFF.
HEADER-DIFF should be a return value from `rmail-edit-diff-headers'."
  (let ((buf-headers (rmail-edit-headers-alist nil t)))
    ;; Change all the fields scheduled for being changed.
    (dolist (chg (nth 2 header-diff))
      (let* ((match (assoc (cdar chg) buf-headers))
	     (end (marker-position (nth 2 match))))
	(goto-char end)
	;; Insert the new, then delete the old.
	;; That avoids collapsing markers.
	(insert-before-markers (cddr chg))
	(delete-region (nth 1 match) end)
	;; Remove the old field from BUF-HEADERS.
	(setq buf-headers (delq match buf-headers))
	;; Update BUF-HEADERS to show the changed field.
	(push (list (cddr chg) (point-marker)
		    (copy-marker (- (point) (length (cddr chg))))
		    (point-marker))
	      buf-headers)))
    ;; Delete all the fields scheduled for deletion.
    ;; We do deletion after changes
    ;; because when two fields look alike and get replaced by one,
    ;; the first of them is considered changed
    ;; and the second is considered deleted.
    (dolist (del (nth 1 header-diff))
      (let ((match (assoc (cdr del) buf-headers)))
	(delete-region (nth 1 match) (nth 2 match))))
    ;; Insert all the fields scheduled for insertion.
    (dolist (ins (nth 0 header-diff))
      (let* ((new (cadr ins))
	     (after (car ins))
	     (match (assoc (cdr after) buf-headers)))
	(goto-char (if match (nth 2 match) (point-min)))
	(insert (cdr new))
	;; Update BUF-HEADERS to show the inserted field.
	(push (list (cdr new)
		    (copy-marker (- (point) (length (cdr new))))
		    (point-marker))
	      buf-headers)))
    ;; Disconnect the markers
    (dolist (hdr buf-headers)
      (set-marker (nth 1 hdr) nil)
      (set-marker (nth 2 hdr) nil))))
394 395 396

(provide 'rmailedit)

397 398 399 400
;; Local Variables:
;; generated-autoload-file: "rmail.el"
;; End:

401 402
;; arch-tag: 9524f335-12cc-4e95-9e9b-3208dc30550b
;;; rmailedit.el ends here