(mmap_free_1): Avoid a compiler warning.
[emacs.git] / lisp / mail / rmailedit.el
blob144b81a6470525cf1b9b40265a1ca9f8248d1f54
1 ;;; rmailedit.el --- "RMAIL edit mode" Edit the current message.
3 ;; Copyright (C) 1985, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: mail
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
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.
25 ;;; Code:
27 (require 'rmail)
29 (defvar rmail-old-text)
31 (defvar rmail-edit-map nil)
32 (if rmail-edit-map
33 nil
34 ;; Make a keymap that inherits text-mode-map.
35 (setq rmail-edit-map (make-sparse-keymap))
36 (set-keymap-parent rmail-edit-map text-mode-map)
37 (define-key rmail-edit-map "\C-c\C-c" 'rmail-cease-edit)
38 (define-key rmail-edit-map "\C-c\C-]" 'rmail-abort-edit))
40 ;; Rmail Edit mode is suitable only for specially formatted data.
41 (put 'rmail-edit-mode 'mode-class 'special)
43 (defun rmail-edit-mode ()
44 "Major mode for editing the contents of an RMAIL message.
45 The editing commands are the same as in Text mode, together with two commands
46 to return to regular RMAIL:
47 * rmail-abort-edit cancels the changes
48 you have made and returns to RMAIL
49 * rmail-cease-edit makes them permanent.
50 \\{rmail-edit-map}"
51 (text-mode)
52 (use-local-map rmail-edit-map)
53 (setq major-mode 'rmail-edit-mode)
54 (setq mode-name "RMAIL Edit")
55 (if (boundp 'mode-line-modified)
56 (setq mode-line-modified (default-value 'mode-line-modified))
57 (setq mode-line-format (default-value 'mode-line-format)))
58 (if (rmail-summary-exists)
59 (save-excursion
60 (set-buffer rmail-summary-buffer)
61 (rmail-summary-disable)))
62 (run-hooks 'rmail-edit-mode-hook))
64 (defvar rmail-old-pruned nil)
65 (put 'rmail-old-pruned 'permanent-local t)
67 (defvar rmail-edit-saved-coding-system nil)
68 (put 'rmail-edit-saved-coding-system 'permanent-local t)
70 ;;;###autoload
71 (defun rmail-edit-current-message ()
72 "Edit the contents of this message."
73 (interactive)
74 (make-local-variable 'rmail-old-pruned)
75 (setq rmail-old-pruned (rmail-msg-is-pruned))
76 (make-local-variable 'rmail-edit-saved-coding-system)
77 (setq rmail-edit-saved-coding-system save-buffer-coding-system)
78 (rmail-toggle-header 0)
79 (rmail-edit-mode)
80 ;; As the local value of save-buffer-coding-system is deleted by
81 ;; rmail-edit-mode, we restore the original value.
82 (make-local-variable 'save-buffer-coding-system)
83 (setq save-buffer-coding-system rmail-edit-saved-coding-system)
84 (make-local-variable 'rmail-old-text)
85 (setq rmail-old-text (buffer-substring (point-min) (point-max)))
86 (setq buffer-read-only nil)
87 (force-mode-line-update)
88 (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
89 (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
90 (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
91 (message "%s" (substitute-command-keys
92 "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
94 (defun rmail-cease-edit ()
95 "Finish editing message; switch back to Rmail proper."
96 (interactive)
97 (if (rmail-summary-exists)
98 (save-excursion
99 (set-buffer rmail-summary-buffer)
100 (rmail-summary-enable)))
101 ;; Make sure buffer ends with a newline.
102 (save-excursion
103 (goto-char (point-max))
104 (if (/= (preceding-char) ?\n)
105 (insert "\n"))
106 ;; Adjust the marker that points to the end of this message.
107 (set-marker (aref rmail-message-vector (1+ rmail-current-message))
108 (point)))
109 (let ((old rmail-old-text))
110 (force-mode-line-update)
111 (kill-all-local-variables)
112 (rmail-mode-1)
113 (rmail-variables)
114 ;; As the local value of save-buffer-coding-system is changed by
115 ;; rmail-variables, we restore the original value.
116 (setq save-buffer-coding-system rmail-edit-saved-coding-system)
117 (if (and (= (length old) (- (point-max) (point-min)))
118 (string= old (buffer-substring (point-min) (point-max))))
120 (setq old nil)
121 (rmail-set-attribute "edited" t)
122 (if (boundp 'rmail-summary-vector)
123 (progn
124 (aset rmail-summary-vector (1- rmail-current-message) nil)
125 (save-excursion
126 (rmail-widen-to-current-msgbeg
127 (function (lambda ()
128 (forward-line 2)
129 (if (looking-at "Summary-line: ")
130 (let ((buffer-read-only nil))
131 (delete-region (point)
132 (progn (forward-line 1)
133 (point))))))))))))
134 (save-excursion
135 (rmail-show-message)
136 (rmail-toggle-header (if rmail-old-pruned 1 0))))
137 (run-hooks 'rmail-mode-hook)
138 (setq buffer-read-only t))
140 (defun rmail-abort-edit ()
141 "Abort edit of current message; restore original contents."
142 (interactive)
143 (delete-region (point-min) (point-max))
144 (insert rmail-old-text)
145 (rmail-cease-edit)
146 (rmail-highlight-headers))
148 ;;; rmailedit.el ends here