1;;; rmailedit.el --- "RMAIL edit mode"  Edit the current message
2
3;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004,
4;;   2005, 2006, 2007 Free Software Foundation, Inc.
5
6;; Maintainer: FSF
7;; Keywords: mail
8
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
13;; the Free Software Foundation; either version 2, or (at your option)
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
22;; along with GNU Emacs; see the file COPYING.  If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
25
26;;; Commentary:
27
28;;; Code:
29
30(require 'rmail)
31
32(defcustom rmail-edit-mode-hook nil
33  "List of functions to call when editing an RMAIL message."
34  :type 'hook
35  :version "21.1"
36  :group 'rmail-edit)
37
38(defvar rmail-old-text)
39
40(defvar rmail-edit-map nil)
41(if rmail-edit-map
42    nil
43  ;; Make a keymap that inherits text-mode-map.
44  (setq rmail-edit-map (make-sparse-keymap))
45  (set-keymap-parent rmail-edit-map text-mode-map)
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:
56  *  \\[rmail-abort-edit] cancels the changes
57     you have made and returns to RMAIL
58  *  \\[rmail-cease-edit] makes them permanent.
59This functions runs the normal hook `rmail-edit-mode-hook'.
60\\{rmail-edit-map}"
61  (delay-mode-hooks (text-mode))
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)))
68  (if (rmail-summary-exists)
69      (save-excursion
70	(set-buffer rmail-summary-buffer)
71	(rmail-summary-disable)))
72  (run-mode-hooks 'rmail-edit-mode-hook))
73
74(defvar rmail-old-pruned nil)
75(put 'rmail-old-pruned 'permanent-local t)
76
77(defvar rmail-edit-saved-coding-system nil)
78(put 'rmail-edit-saved-coding-system 'permanent-local t)
79
80;;;###autoload
81(defun rmail-edit-current-message ()
82  "Edit the contents of this message."
83  (interactive)
84  (make-local-variable 'rmail-old-pruned)
85  (setq rmail-old-pruned (rmail-msg-is-pruned))
86  (make-local-variable 'rmail-edit-saved-coding-system)
87  (setq rmail-edit-saved-coding-system save-buffer-coding-system)
88  (rmail-toggle-header 0)
89  (rmail-edit-mode)
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)
94  (make-local-variable 'rmail-old-text)
95  (setq rmail-old-text (buffer-substring (point-min) (point-max)))
96  (setq buffer-read-only nil)
97  (force-mode-line-update)
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")
101    (message "%s" (substitute-command-keys
102		   "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
103
104(defun rmail-cease-edit ()
105  "Finish editing message; switch back to Rmail proper."
106  (interactive)
107  (if (rmail-summary-exists)
108      (save-excursion
109	(set-buffer rmail-summary-buffer)
110	(rmail-summary-enable)))
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))
120    (force-mode-line-update)
121    (kill-all-local-variables)
122    (rmail-mode-1)
123    (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map)
124    (rmail-variables)
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)
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
138		(function (lambda ()
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)
144							(point))))))))))))
145    (save-excursion
146      (rmail-show-message)
147      (rmail-toggle-header (if rmail-old-pruned 1 0))))
148  (run-hooks 'rmail-mode-hook)
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)
156  (rmail-cease-edit)
157  (rmail-highlight-headers))
158
159(provide 'rmailedit)
160
161;;; arch-tag: 93c22709-a14a-46c1-ab91-52c3f5a0ec12
162;;; rmailedit.el ends here
163