(pmail-maybe-set-message-counters)
[bpt/emacs.git] / lisp / mail / rmailedit.el
CommitLineData
55535639 1;;; rmailedit.el --- "RMAIL edit mode" Edit the current message
c88ab9ce 2
e9c033a0
GM
3;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
4;; 2007, 2008 Free Software Foundation, Inc.
9750e079 5
e5167999 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: mail
e5167999 8
a2535589
JA
9;; This file is part of GNU Emacs.
10
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
a2535589 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
a2535589
JA
15
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.
20
21;; You should have received a copy of the GNU General Public License
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a2535589 23
55535639
PJ
24;;; Commentary:
25
e5167999 26;;; Code:
a2535589
JA
27
28(require 'rmail)
29
5092477a
GM
30(defcustom rmail-edit-mode-hook nil
31 "List of functions to call when editing an RMAIL message."
32 :type 'hook
1d053370 33 :version "21.1"
5092477a
GM
34 :group 'rmail-edit)
35
c241bb9c
KH
36(defvar rmail-old-text)
37
a2535589
JA
38(defvar rmail-edit-map nil)
39(if rmail-edit-map
40 nil
9a4cc6bd 41 ;; Make a keymap that inherits text-mode-map.
c241bb9c
KH
42 (setq rmail-edit-map (make-sparse-keymap))
43 (set-keymap-parent rmail-edit-map text-mode-map)
a2535589
JA
44 (define-key rmail-edit-map "\C-c\C-c" 'rmail-cease-edit)
45 (define-key rmail-edit-map "\C-c\C-]" 'rmail-abort-edit))
46
47;; Rmail Edit mode is suitable only for specially formatted data.
48(put 'rmail-edit-mode 'mode-class 'special)
49
2b54af74
DN
50(declare-function rmail-summary-disable "" ())
51(declare-function rmail-summary-enable "rmailsum" ())
52
a2535589
JA
53(defun rmail-edit-mode ()
54 "Major mode for editing the contents of an RMAIL message.
55The editing commands are the same as in Text mode, together with two commands
56to return to regular RMAIL:
5092477a 57 * \\[rmail-abort-edit] cancels the changes
a2535589 58 you have made and returns to RMAIL
5092477a
GM
59 * \\[rmail-cease-edit] makes them permanent.
60This functions runs the normal hook `rmail-edit-mode-hook'.
a2535589 61\\{rmail-edit-map}"
fb33e153 62 (delay-mode-hooks (text-mode))
a2535589
JA
63 (use-local-map rmail-edit-map)
64 (setq major-mode 'rmail-edit-mode)
65 (setq mode-name "RMAIL Edit")
66 (if (boundp 'mode-line-modified)
67 (setq mode-line-modified (default-value 'mode-line-modified))
68 (setq mode-line-format (default-value 'mode-line-format)))
c35fb216 69 (if (rmail-summary-exists)
56b25713
KH
70 (save-excursion
71 (set-buffer rmail-summary-buffer)
72 (rmail-summary-disable)))
7f9b6634 73 (run-mode-hooks 'rmail-edit-mode-hook))
a2535589 74
77fb8963
RS
75(defvar rmail-old-pruned nil)
76(put 'rmail-old-pruned 'permanent-local t)
77
7c968ca5
RS
78(defvar rmail-edit-saved-coding-system nil)
79(put 'rmail-edit-saved-coding-system 'permanent-local t)
80
523abd28 81;;;###autoload
a2535589
JA
82(defun rmail-edit-current-message ()
83 "Edit the contents of this message."
84 (interactive)
77fb8963
RS
85 (make-local-variable 'rmail-old-pruned)
86 (setq rmail-old-pruned (rmail-msg-is-pruned))
7c968ca5
RS
87 (make-local-variable 'rmail-edit-saved-coding-system)
88 (setq rmail-edit-saved-coding-system save-buffer-coding-system)
77fb8963 89 (rmail-toggle-header 0)
a2535589 90 (rmail-edit-mode)
7c968ca5
RS
91 ;; As the local value of save-buffer-coding-system is deleted by
92 ;; rmail-edit-mode, we restore the original value.
93 (make-local-variable 'save-buffer-coding-system)
94 (setq save-buffer-coding-system rmail-edit-saved-coding-system)
a2535589
JA
95 (make-local-variable 'rmail-old-text)
96 (setq rmail-old-text (buffer-substring (point-min) (point-max)))
97 (setq buffer-read-only nil)
cde63420 98 (force-mode-line-update)
a2535589
JA
99 (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
100 (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
101 (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
e26a12ac 102 (message "%s" (substitute-command-keys
77fb8963 103 "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
a2535589
JA
104
105(defun rmail-cease-edit ()
106 "Finish editing message; switch back to Rmail proper."
107 (interactive)
c35fb216 108 (if (rmail-summary-exists)
56b25713
KH
109 (save-excursion
110 (set-buffer rmail-summary-buffer)
111 (rmail-summary-enable)))
a2535589
JA
112 ;; Make sure buffer ends with a newline.
113 (save-excursion
114 (goto-char (point-max))
115 (if (/= (preceding-char) ?\n)
116 (insert "\n"))
117 ;; Adjust the marker that points to the end of this message.
118 (set-marker (aref rmail-message-vector (1+ rmail-current-message))
119 (point)))
120 (let ((old rmail-old-text))
cde63420 121 (force-mode-line-update)
c241bb9c 122 (kill-all-local-variables)
a2535589 123 (rmail-mode-1)
e9c033a0
GM
124 (if (boundp 'tool-bar-map)
125 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
c241bb9c 126 (rmail-variables)
7c968ca5
RS
127 ;; As the local value of save-buffer-coding-system is changed by
128 ;; rmail-variables, we restore the original value.
129 (setq save-buffer-coding-system rmail-edit-saved-coding-system)
a2535589
JA
130 (if (and (= (length old) (- (point-max) (point-min)))
131 (string= old (buffer-substring (point-min) (point-max))))
132 ()
133 (setq old nil)
134 (rmail-set-attribute "edited" t)
135 (if (boundp 'rmail-summary-vector)
136 (progn
137 (aset rmail-summary-vector (1- rmail-current-message) nil)
138 (save-excursion
139 (rmail-widen-to-current-msgbeg
cd95034f 140 (function (lambda ()
a2535589
JA
141 (forward-line 2)
142 (if (looking-at "Summary-line: ")
143 (let ((buffer-read-only nil))
144 (delete-region (point)
145 (progn (forward-line 1)
cd95034f
RS
146 (point))))))))))))
147 (save-excursion
77fb8963
RS
148 (rmail-show-message)
149 (rmail-toggle-header (if rmail-old-pruned 1 0))))
bd84ce86 150 (run-hooks 'rmail-mode-hook)
a2535589
JA
151 (setq buffer-read-only t))
152
153(defun rmail-abort-edit ()
154 "Abort edit of current message; restore original contents."
155 (interactive)
156 (delete-region (point-min) (point-max))
157 (insert rmail-old-text)
2ac32b29
RS
158 (rmail-cease-edit)
159 (rmail-highlight-headers))
a2535589 160
456f0b95
MR
161(provide 'rmailedit)
162
cbee283d 163;; arch-tag: 93c22709-a14a-46c1-ab91-52c3f5a0ec12
c88ab9ce 164;;; rmailedit.el ends here