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