*** empty log message ***
[bpt/emacs.git] / lisp / mail / rmailedit.el
CommitLineData
a2535589
JA
1;; "RMAIL edit mode" Edit the current message.
2;; Copyright (C) 1985 Free Software Foundation, Inc.
3
4;; This file is part of GNU Emacs.
5
6;; GNU Emacs is free software; you can redistribute it and/or modify
7;; it under the terms of the GNU General Public License as published by
8;; the Free Software Foundation; either version 1, or (at your option)
9;; any later version.
10
11;; GNU Emacs is distributed in the hope that it will be useful,
12;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;; GNU General Public License for more details.
15
16;; You should have received a copy of the GNU General Public License
17;; along with GNU Emacs; see the file COPYING. If not, write to
18;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21(require 'rmail)
22
23(defvar rmail-edit-map nil)
24(if rmail-edit-map
25 nil
6e8290aa 26 (setq rmail-edit-map (nconc (make-sparse-keymap) (cdr text-mode-map)))
a2535589
JA
27 (define-key rmail-edit-map "\C-c\C-c" 'rmail-cease-edit)
28 (define-key rmail-edit-map "\C-c\C-]" 'rmail-abort-edit))
29
30;; Rmail Edit mode is suitable only for specially formatted data.
31(put 'rmail-edit-mode 'mode-class 'special)
32
33(defun rmail-edit-mode ()
34 "Major mode for editing the contents of an RMAIL message.
35The editing commands are the same as in Text mode, together with two commands
36to return to regular RMAIL:
37 * rmail-abort-edit cancels the changes
38 you have made and returns to RMAIL
39 * rmail-cease-edit makes them permanent.
40\\{rmail-edit-map}"
41 (use-local-map rmail-edit-map)
42 (setq major-mode 'rmail-edit-mode)
43 (setq mode-name "RMAIL Edit")
44 (if (boundp 'mode-line-modified)
45 (setq mode-line-modified (default-value 'mode-line-modified))
46 (setq mode-line-format (default-value 'mode-line-format)))
47 (run-hooks 'text-mode-hook 'rmail-edit-mode-hook))
48
49(defun rmail-edit-current-message ()
50 "Edit the contents of this message."
51 (interactive)
52 (rmail-edit-mode)
53 (make-local-variable 'rmail-old-text)
54 (setq rmail-old-text (buffer-substring (point-min) (point-max)))
55 (setq buffer-read-only nil)
56 (set-buffer-modified-p (buffer-modified-p))
57 ;; Make mode line update.
58 (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
59 (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
60 (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
61 (message (substitute-command-keys
62 "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
63
64(defun rmail-cease-edit ()
65 "Finish editing message; switch back to Rmail proper."
66 (interactive)
67 ;; Make sure buffer ends with a newline.
68 (save-excursion
69 (goto-char (point-max))
70 (if (/= (preceding-char) ?\n)
71 (insert "\n"))
72 ;; Adjust the marker that points to the end of this message.
73 (set-marker (aref rmail-message-vector (1+ rmail-current-message))
74 (point)))
75 (let ((old rmail-old-text))
76 ;; Update the mode line.
77 (set-buffer-modified-p (buffer-modified-p))
78 (rmail-mode-1)
79 (if (and (= (length old) (- (point-max) (point-min)))
80 (string= old (buffer-substring (point-min) (point-max))))
81 ()
82 (setq old nil)
83 (rmail-set-attribute "edited" t)
84 (if (boundp 'rmail-summary-vector)
85 (progn
86 (aset rmail-summary-vector (1- rmail-current-message) nil)
87 (save-excursion
88 (rmail-widen-to-current-msgbeg
89 (function (lambda ()
90 (forward-line 2)
91 (if (looking-at "Summary-line: ")
92 (let ((buffer-read-only nil))
93 (delete-region (point)
94 (progn (forward-line 1)
95 (point))))))))
96 (rmail-show-message))))))
97 (setq buffer-read-only t))
98
99(defun rmail-abort-edit ()
100 "Abort edit of current message; restore original contents."
101 (interactive)
102 (delete-region (point-min) (point-max))
103 (insert rmail-old-text)
104 (rmail-cease-edit))
105