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