rmailedit.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. ;;; rmailedit.el --- "RMAIL edit mode" Edit the current message
  2. ;; Copyright (C) 1985, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: mail
  5. ;; Package: rmail
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (require 'rmail)
  20. (defcustom rmail-edit-mode-hook nil
  21. "List of functions to call when editing an RMAIL message."
  22. :type 'hook
  23. :version "21.1"
  24. :group 'rmail-edit)
  25. (defvar rmail-edit-map
  26. (let ((map (make-sparse-keymap)))
  27. ;; Make a keymap that inherits text-mode-map.
  28. (set-keymap-parent map text-mode-map)
  29. (define-key map "\C-c\C-c" 'rmail-cease-edit)
  30. (define-key map "\C-c\C-]" 'rmail-abort-edit)
  31. map))
  32. (declare-function rmail-summary-disable "rmailsum" ())
  33. (defun rmail-edit-mode ()
  34. "Major mode for editing the contents of an Rmail message.
  35. The editing commands are the same as in Text mode, together with
  36. two commands to return to regular Rmail:
  37. * \\[rmail-abort-edit] cancels any changes and returns to Rmail
  38. * \\[rmail-cease-edit] makes them permanent.
  39. This function runs the hooks `text-mode-hook' and `rmail-edit-mode-hook'.
  40. \\{rmail-edit-map}"
  41. (if (rmail-summary-exists)
  42. (with-current-buffer rmail-summary-buffer
  43. (rmail-summary-disable)))
  44. ;; Prevent change-major-mode-hook from unswapping the buffers.
  45. (let ((rmail-buffer-swapped nil))
  46. (delay-mode-hooks (text-mode))
  47. (use-local-map rmail-edit-map)
  48. (setq major-mode 'rmail-edit-mode)
  49. (setq mode-name "RMAIL Edit")
  50. (if (boundp 'mode-line-modified)
  51. (setq mode-line-modified (default-value 'mode-line-modified))
  52. (setq mode-line-format (default-value 'mode-line-format)))
  53. ;; Don't turn off auto-saving based on the size of the buffer
  54. ;; because that code does not understand buffer-swapping.
  55. (make-local-variable 'auto-save-include-big-deletions)
  56. (setq auto-save-include-big-deletions t)
  57. ;; If someone uses C-x C-s, don't clobber the rmail file (bug#2625).
  58. (add-hook 'write-region-annotate-functions
  59. 'rmail-write-region-annotate nil t)
  60. (run-mode-hooks 'rmail-edit-mode-hook)))
  61. ;; Rmail Edit mode is suitable only for specially formatted data.
  62. (put 'rmail-edit-mode 'mode-class 'special)
  63. (defvar rmail-old-text)
  64. (defvar rmail-old-mime-state)
  65. (defvar rmail-old-pruned nil
  66. "Non-nil means the message being edited originally had pruned headers.")
  67. (put 'rmail-old-pruned 'permanent-local t)
  68. (defvar rmail-old-headers nil
  69. "Holds the headers of this message before editing started.")
  70. (put 'rmail-old-headers 'permanent-local t)
  71. ;; Everything we use from here is a defsubst.
  72. (eval-when-compile
  73. (require 'rmailmm))
  74. ;;;###autoload
  75. (defun rmail-edit-current-message ()
  76. "Edit the contents of this message."
  77. (interactive)
  78. (if (zerop rmail-total-messages)
  79. (error "No messages in this buffer"))
  80. (rmail-modify-format)
  81. (make-local-variable 'rmail-old-pruned)
  82. (setq rmail-old-pruned (rmail-msg-is-pruned))
  83. (rmail-edit-mode)
  84. (set (make-local-variable 'rmail-old-mime-state)
  85. (and rmail-enable-mime
  86. ;; If you use something else, you are on your own.
  87. (eq rmail-mime-feature 'rmailmm)
  88. (rmail-mime-message-p)
  89. (let ((entity (get-text-property (point-min) 'rmail-mime-entity)))
  90. ;; rmailmm has got its hands on the message.
  91. ;; Even if the message is in `raw' state, boundaries etc
  92. ;; are still missing. All we can do is insert the real
  93. ;; raw message. (Bug#9840)
  94. (when (and entity
  95. (not (equal "text/plain"
  96. (car (rmail-mime-entity-type entity)))))
  97. (let ((inhibit-read-only t))
  98. (erase-buffer)
  99. (insert-buffer-substring
  100. rmail-view-buffer
  101. (aref (rmail-mime-entity-header entity) 0)
  102. (aref (rmail-mime-entity-body entity) 1)))
  103. (goto-char (point-min))
  104. ;; t = decoded; raw = raw.
  105. (aref (aref (rmail-mime-entity-display entity) 0) 0)))))
  106. (make-local-variable 'rmail-old-text)
  107. (setq rmail-old-text
  108. (save-restriction
  109. (widen)
  110. (buffer-substring (point-min) (point-max))))
  111. (make-local-variable 'rmail-old-headers)
  112. (setq rmail-old-headers (rmail-edit-headers-alist t))
  113. (setq buffer-read-only nil)
  114. (setq buffer-undo-list nil)
  115. ;; Whether the buffer is initially marked as modified or not
  116. ;; depends on whether or not the underlying rmail buffer was so marked.
  117. ;; Given the way this works, it has to.
  118. ;; If you kill the edit buffer, you've killed your rmail buffer.
  119. (force-mode-line-update)
  120. (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
  121. (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
  122. (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
  123. (message "%s" (substitute-command-keys
  124. "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
  125. (declare-function rmail-summary-enable "rmailsum" ())
  126. (defun rmail-cease-edit ()
  127. "Finish editing message; switch back to Rmail proper."
  128. (interactive)
  129. (if (rmail-summary-exists)
  130. (with-current-buffer rmail-summary-buffer
  131. (rmail-summary-enable)))
  132. (widen)
  133. (goto-char (point-min))
  134. ;; This is far from ideal. The edit may have inadvertently
  135. ;; removed the blank line at the end of the headers, but there
  136. ;; are almost certainly other blank lines.
  137. (or (search-forward "\n\n" nil t)
  138. (error "There must be a blank line at the end of the headers"))
  139. ;; Disguise any "From " lines so they don't start a new message.
  140. (goto-char (point-min))
  141. ;; This tries to skip the mbox From. FIXME less fragile to go to EOH?
  142. (if (or rmail-old-mime-state
  143. (not rmail-old-pruned))
  144. (forward-line 1))
  145. (while (re-search-forward "^>*From " nil t)
  146. (beginning-of-line)
  147. (insert ">")
  148. (forward-line))
  149. ;; Make sure buffer ends with a blank line so as not to run this
  150. ;; message together with the following one.
  151. (goto-char (point-max))
  152. (rmail-ensure-blank-line)
  153. (let ((old rmail-old-text)
  154. (pruned rmail-old-pruned)
  155. (mime-state rmail-old-mime-state)
  156. ;; People who know what they are doing might have modified the
  157. ;; buffer's encoding if editing the message included inserting
  158. ;; characters that were unencodable by the original message's
  159. ;; encoding. Make note of the new encoding and use it for
  160. ;; encoding the edited message.
  161. (edited-coding buffer-file-coding-system)
  162. new-headers
  163. character-coding is-text-message coding-system
  164. headers-end limit)
  165. ;; Make sure `edited-coding' can safely encode the edited message.
  166. (setq edited-coding
  167. (select-safe-coding-system (point-min) (point-max) edited-coding))
  168. ;; Go back to Rmail mode, but carefully.
  169. (force-mode-line-update)
  170. (let ((rmail-buffer-swapped nil)) ; Prevent change-major-mode-hook
  171. ; from unswapping the buffers.
  172. (kill-all-local-variables)
  173. (rmail-mode-1)
  174. (if (boundp 'tool-bar-map)
  175. (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
  176. (setq buffer-undo-list t)
  177. (rmail-variables))
  178. ;; If text has really changed, mark message as edited.
  179. (unless (and (= (length old) (- (point-max) (point-min)))
  180. (string= old (buffer-substring (point-min) (point-max))))
  181. (setq old nil)
  182. (goto-char (point-min))
  183. (search-forward "\n\n")
  184. (setq headers-end (point-marker))
  185. (goto-char (point-min))
  186. (save-restriction
  187. (narrow-to-region (point) headers-end)
  188. ;; If they changed the message's encoding, rewrite the charset=
  189. ;; header for them, so that subsequent rmail-show-message
  190. ;; decodes it correctly.
  191. (let* ((buffer-read-only nil)
  192. (new-coding (coding-system-base edited-coding))
  193. (mime-charset (symbol-name
  194. (or (coding-system-get new-coding :mime-charset)
  195. (if (coding-system-equal new-coding
  196. 'undecided)
  197. 'us-ascii
  198. new-coding))))
  199. old-coding mime-beg mime-end content-type)
  200. (if (re-search-forward rmail-mime-charset-pattern nil 'move)
  201. (setq mime-beg (match-beginning 1)
  202. mime-end (match-end 1)
  203. old-coding (coding-system-from-name (match-string 1)))
  204. (setq content-type (mail-fetch-field "Content-Type")))
  205. (cond
  206. ;; No match for rmail-mime-charset-pattern, but there was some
  207. ;; other Content-Type. We should not insert another. (Bug#4624)
  208. (content-type)
  209. ((null old-coding)
  210. ;; If there was no charset= spec, insert one.
  211. (backward-char 1)
  212. (insert "Content-type: text/plain; charset=" mime-charset "\n"))
  213. ((not (coding-system-equal (coding-system-base old-coding)
  214. new-coding))
  215. (goto-char mime-end)
  216. (delete-region mime-beg mime-end)
  217. (insert mime-charset)))))
  218. (setq new-headers (rmail-edit-headers-alist t))
  219. (rmail-swap-buffers-maybe)
  220. (narrow-to-region (rmail-msgbeg rmail-current-message)
  221. (rmail-msgend rmail-current-message))
  222. (goto-char (point-min))
  223. (setq limit (search-forward "\n\n"))
  224. (save-restriction
  225. ;; All 3 of the functions we call below assume the buffer was
  226. ;; narrowed to just the headers of the message.
  227. (narrow-to-region (point-min) limit)
  228. (setq character-coding
  229. (mail-fetch-field "content-transfer-encoding")
  230. is-text-message (rmail-is-text-p)
  231. coding-system (if (and edited-coding
  232. (not (coding-system-equal
  233. (coding-system-base edited-coding)
  234. 'undecided)))
  235. edited-coding
  236. (rmail-get-coding-system))))
  237. (if character-coding
  238. (setq character-coding (downcase character-coding)))
  239. (goto-char limit)
  240. (let ((inhibit-read-only t))
  241. (let ((data-buffer (current-buffer))
  242. (end (copy-marker (point) t)))
  243. (with-current-buffer rmail-view-buffer
  244. (encode-coding-region headers-end (point-max) coding-system
  245. data-buffer))
  246. (delete-region end (point-max)))
  247. ;; Apply to the mbox buffer any changes in header fields
  248. ;; that the user made while editing in the view buffer.
  249. (rmail-edit-update-headers (rmail-edit-diff-headers
  250. rmail-old-headers new-headers))
  251. ;; Re-apply content-transfer-encoding, if any, on the message body.
  252. (cond
  253. ((string= character-coding "quoted-printable")
  254. (mail-quote-printable-region (point) (point-max)))
  255. ((and (string= character-coding "base64") is-text-message)
  256. (base64-encode-region (point) (point-max)))
  257. ((and (eq character-coding 'uuencode) is-text-message)
  258. (error "uuencoded messages are not supported"))))
  259. (rmail-set-attribute rmail-edited-attr-index t))
  260. ;;??? BROKEN perhaps.
  261. ;;; (if (boundp 'rmail-summary-vector)
  262. ;;; (aset rmail-summary-vector (1- rmail-current-message) nil))
  263. (rmail-show-message)
  264. (rmail-toggle-header (if pruned 1 0))
  265. ;; Restore mime display state.
  266. (and mime-state (rmail-mime nil mime-state)))
  267. (run-hooks 'rmail-mode-hook))
  268. (defun rmail-abort-edit ()
  269. "Abort edit of current message; restore original contents."
  270. (interactive)
  271. (widen)
  272. (delete-region (point-min) (point-max))
  273. (insert rmail-old-text)
  274. (rmail-cease-edit)
  275. (rmail-highlight-headers))
  276. (defun rmail-edit-headers-alist (&optional widen markers)
  277. "Return an alist of the headers of the message in the current buffer.
  278. Each element has the form (HEADER-NAME . ENTIRE-STRING).
  279. ENTIRE-STRING includes the name of the header field (which is HEADER-NAME)
  280. and has a final newline.
  281. If part of the text is not valid as a header field, HEADER-NAME
  282. is an integer and we use consecutive integers.
  283. If WIDEN is non-nil, operate on the entire buffer.
  284. If MARKERS is non-nil, the value looks like
  285. \(HEADER-NAME ENTIRE-STRING BEG-MARKER END-MARKER)."
  286. (let (header-alist (no-good-header-count 1))
  287. (save-excursion
  288. (save-restriction
  289. (if widen (widen))
  290. (goto-char (point-min))
  291. (search-forward "\n\n")
  292. (narrow-to-region (point-min) (1- (point)))
  293. (goto-char (point-min))
  294. (while (not (eobp))
  295. (let ((start (point))
  296. name header)
  297. ;; Match the name.
  298. (if (looking-at "[ \t]*\\([^:\n \t]\\(\\|[^:\n]*[^:\n \t]\\)\\)[ \t]*:")
  299. (setq name (match-string-no-properties 1))
  300. (setq name no-good-header-count
  301. no-good-header-count (1+ no-good-header-count)))
  302. (forward-line 1)
  303. (while (looking-at "[ \t]")
  304. (forward-line 1))
  305. (setq header (buffer-substring-no-properties start (point)))
  306. (if markers
  307. (push (list header (copy-marker start) (point-marker))
  308. header-alist)
  309. (push (cons name header) header-alist))))))
  310. (nreverse header-alist)))
  311. (defun rmail-edit-diff-headers (old-headers new-headers)
  312. "Compare OLD-HEADERS and NEW-HEADERS and return field differences.
  313. The value is a list of three lists, (INSERTED DELETED CHANGED).
  314. INSERTED's elements describe inserted header fields
  315. and each looks like (AFTER-WHAT INSERT-WHAT)
  316. INSERT-WHAT is the header field to insert (a member of NEW-HEADERS).
  317. AFTER-WHAT is the field to insert it after (a member of NEW-HEADERS)
  318. or else nil to insert it at the beginning.
  319. DELETED's elements are elements of OLD-HEADERS.
  320. CHANGED's elements have the form (OLD . NEW)
  321. where OLD is a element of OLD-HEADERS and NEW is an element of NEW-HEADERS."
  322. (let ((reverse-new (reverse new-headers))
  323. inserted deleted changed)
  324. (dolist (old old-headers)
  325. (let ((new (assoc (car old) new-headers)))
  326. ;; If it's in OLD-HEADERS and has no new counterpart,
  327. ;; it is a deletion.
  328. (if (null new)
  329. (push old deleted)
  330. ;; If it has a new counterpart, maybe it was changed.
  331. (unless (equal (cdr old) (cdr new))
  332. (push (cons old new) changed))
  333. ;; Remove the new counterpart, since it has been spoken for.
  334. (setq new-headers (remq new new-headers)))))
  335. ;; Look at the new headers with no old counterpart.
  336. (dolist (new new-headers)
  337. (let ((prev (cadr (member new reverse-new))))
  338. ;; Mark each one as an insertion.
  339. ;; Record the previous new header, to insert it after that.
  340. (push (list prev new) inserted)))
  341. ;; It is crucial to return the insertions in buffer order
  342. ;; so that `rmail-edit-update-headers' can insert a field
  343. ;; after a new field.
  344. (list (nreverse inserted)
  345. (nreverse deleted)
  346. (nreverse changed))))
  347. (defun rmail-edit-update-headers (header-diff)
  348. "Edit the mail headers in the buffer based on HEADER-DIFF.
  349. HEADER-DIFF should be a return value from `rmail-edit-diff-headers'."
  350. (let ((buf-headers (rmail-edit-headers-alist nil t)))
  351. ;; Change all the fields scheduled for being changed.
  352. (dolist (chg (nth 2 header-diff))
  353. (let* ((match (assoc (cdar chg) buf-headers))
  354. (end (marker-position (nth 2 match))))
  355. (goto-char end)
  356. ;; Insert the new, then delete the old.
  357. ;; That avoids collapsing markers.
  358. (insert-before-markers (cddr chg))
  359. (delete-region (nth 1 match) end)
  360. ;; Remove the old field from BUF-HEADERS.
  361. (setq buf-headers (delq match buf-headers))
  362. ;; Update BUF-HEADERS to show the changed field.
  363. (push (list (cddr chg) (point-marker)
  364. (copy-marker (- (point) (length (cddr chg))))
  365. (point-marker))
  366. buf-headers)))
  367. ;; Delete all the fields scheduled for deletion.
  368. ;; We do deletion after changes
  369. ;; because when two fields look alike and get replaced by one,
  370. ;; the first of them is considered changed
  371. ;; and the second is considered deleted.
  372. (dolist (del (nth 1 header-diff))
  373. (let ((match (assoc (cdr del) buf-headers)))
  374. (delete-region (nth 1 match) (nth 2 match))))
  375. ;; Insert all the fields scheduled for insertion.
  376. (dolist (ins (nth 0 header-diff))
  377. (let* ((new (cadr ins))
  378. (after (car ins))
  379. (match (assoc (cdr after) buf-headers)))
  380. (goto-char (if match (nth 2 match) (point-min)))
  381. (insert (cdr new))
  382. ;; Update BUF-HEADERS to show the inserted field.
  383. (push (list (cdr new)
  384. (copy-marker (- (point) (length (cdr new))))
  385. (point-marker))
  386. buf-headers)))
  387. ;; Disconnect the markers
  388. (dolist (hdr buf-headers)
  389. (set-marker (nth 1 hdr) nil)
  390. (set-marker (nth 2 hdr) nil))))
  391. (provide 'rmailedit)
  392. ;; Local Variables:
  393. ;; generated-autoload-file: "rmail.el"
  394. ;; End:
  395. ;;; rmailedit.el ends here