rmailedit.el 17 KB

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