1 ;;; rmailedit.el --- "RMAIL edit mode" Edit the current message
3 ;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 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/>.
32 (defcustom rmail-edit-mode-hook nil
33 "List of functions to call when editing an RMAIL message."
38 (defvar rmail-old-text)
40 (defvar rmail-edit-map
41 (let ((map (make-sparse-keymap)))
42 ;; Make a keymap that inherits text-mode-map.
43 (set-keymap-parent map text-mode-map)
44 (define-key map "\C-c\C-c" 'rmail-cease-edit)
45 (define-key map "\C-c\C-]" 'rmail-abort-edit)
49 ;; Rmail Edit mode is suitable only for specially formatted data.
50 (put 'rmail-edit-mode 'mode-class 'special)
52 (declare-function rmail-summary-disable "rmailsum" ())
53 (declare-function rmail-summary-enable "rmailsum" ())
55 (defun rmail-edit-mode ()
56 "Major mode for editing the contents of an RMAIL message.
57 The editing commands are the same as in Text mode, together with two commands
58 to return to regular RMAIL:
59 * \\[rmail-abort-edit] cancels the changes
60 you have made and returns to RMAIL
61 * \\[rmail-cease-edit] makes them permanent.
62 This functions runs the normal hook `rmail-edit-mode-hook'.
64 (if (rmail-summary-exists)
65 (with-current-buffer rmail-summary-buffer
66 (rmail-summary-disable)))
67 (let ((rmail-buffer-swapped nil)) ; Prevent change-major-mode-hook
68 ; from unswapping the buffers.
69 (delay-mode-hooks (text-mode))
70 (use-local-map rmail-edit-map)
71 (setq major-mode 'rmail-edit-mode)
72 (setq mode-name "RMAIL Edit")
73 (if (boundp 'mode-line-modified)
74 (setq mode-line-modified (default-value 'mode-line-modified))
75 (setq mode-line-format (default-value 'mode-line-format)))
76 (run-mode-hooks 'rmail-edit-mode-hook)))
78 (defvar rmail-old-pruned nil)
79 (put 'rmail-old-pruned 'permanent-local t)
82 (defun rmail-edit-current-message ()
83 "Edit the contents of this message."
85 (if (= rmail-total-messages 0)
86 (error "No messages in this buffer"))
87 (make-local-variable 'rmail-old-pruned)
88 (setq rmail-old-pruned (eq rmail-header-style 'normal))
90 (make-local-variable 'rmail-old-text)
93 (setq rmail-old-text (buffer-substring (point-min) (point-max))))
94 (setq buffer-read-only nil)
95 (setq buffer-undo-list nil)
96 (force-mode-line-update)
97 (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
98 (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
99 (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
100 (message "%s" (substitute-command-keys
101 "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
103 (defun rmail-cease-edit ()
104 "Finish editing message; switch back to Rmail proper."
106 (if (rmail-summary-exists)
107 (with-current-buffer rmail-summary-buffer
108 (rmail-summary-enable)))
110 ;; Disguise any "From " lines so they don't start a new message.
112 (goto-char (point-min))
113 (while (re-search-forward "^>*From " nil t)
117 ;; Make sure buffer ends with a blank line
118 ;; so as not to run this message together with the following one.
120 (goto-char (point-max))
121 (if (/= (preceding-char) ?\n)
123 (unless (looking-back "\n\n")
125 (let ((old rmail-old-text)
126 character-coding is-text-message coding-system
128 ;; Go back to Rmail mode, but carefully.
129 (force-mode-line-update)
130 (let ((rmail-buffer-swapped nil)) ; Prevent change-major-mode-hook
131 ; from unswapping the buffers.
132 (kill-all-local-variables)
134 (if (boundp 'tool-bar-map)
135 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
136 (setq buffer-undo-list t)
138 ;; If text has really changed, mark message as edited.
139 (unless (and (= (length old) (- (point-max) (point-min)))
140 (string= old (buffer-substring (point-min) (point-max))))
142 (goto-char (point-min))
143 (search-forward "\n\n")
144 (setq headers-end (point))
146 (rmail-swap-buffers-maybe)
148 (narrow-to-region (rmail-msgbeg rmail-current-message)
149 (rmail-msgend rmail-current-message))
154 (goto-char (point-min))
155 (search-forward "\n\n" nil t)))
156 ;; All 3 of the functions we call below assume the buffer was
157 ;; narrowed to just the headers of the message.
158 (narrow-to-region (rmail-msgbeg rmail-current-message) limit)
159 (setq character-coding
160 (mail-fetch-field "content-transfer-encoding")
161 is-text-message (rmail-is-text-p)
162 coding-system (rmail-get-coding-system)))
164 (setq character-coding (downcase character-coding)))
167 (let ((inhibit-read-only t))
168 (let ((data-buffer (current-buffer))
169 (end (copy-marker (point) t)))
170 (with-current-buffer rmail-view-buffer
171 (encode-coding-region headers-end (point-max) coding-system
173 (delete-region end (point-max)))
175 ;; Re-apply content-transfer-encoding, if any, on the message
178 ((string= character-coding "quoted-printable")
179 (mail-quote-printable-region (point) (point-max)))
180 ((and (string= character-coding "base64") is-text-message)
181 (base64-encode-region (point) (point-max)))
182 ((and (eq character-coding 'uuencode) is-text-message)
183 (error "uuencoded messages are not supported yet.")))
186 (rmail-set-attribute rmail-edited-attr-index t)
188 ;;??? BROKEN perhaps.
189 ;; I think that the Summary-Line header may not be kept there any more.
190 ;;; (if (boundp 'rmail-summary-vector)
192 ;;; (aset rmail-summary-vector (1- rmail-current-message) nil)
194 ;;; (rmail-widen-to-current-msgbeg
195 ;;; (function (lambda ()
197 ;;; (if (looking-at "Summary-line: ")
198 ;;; (let ((buffer-read-only nil))
199 ;;; (delete-region (point)
200 ;;; (progn (forward-line 1)
201 ;;; (point)))))))))))
206 (rmail-toggle-header (if rmail-old-pruned 1 0)))
207 (run-hooks 'rmail-mode-hook))
209 (defun rmail-abort-edit ()
210 "Abort edit of current message; restore original contents."
213 (delete-region (point-min) (point-max))
214 (insert rmail-old-text)
216 (rmail-highlight-headers))
220 ;; arch-tag: 9524f335-12cc-4e95-9e9b-3208dc30550b
221 ;;; rmailedit.el ends here