]> code.delx.au - gnu-emacs/blob - lisp/mail/pmailedit.el
(pmail-edit-current-message): Change error message.
[gnu-emacs] / lisp / mail / pmailedit.el
1 ;;; pmailedit.el --- "PMAIL edit mode" Edit the current message
2
3 ;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008 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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile
29 (require 'pmail)
30 (require 'pmailsum))
31
32 (defcustom pmail-edit-mode-hook nil
33 "List of functions to call when editing an PMAIL message."
34 :type 'hook
35 :version "21.1"
36 :group 'pmail-edit)
37
38 (defvar pmail-old-text)
39
40 (defvar pmail-edit-map nil)
41 (if pmail-edit-map
42 nil
43 ;; Make a keymap that inherits text-mode-map.
44 (setq pmail-edit-map (make-sparse-keymap))
45 (set-keymap-parent pmail-edit-map text-mode-map)
46 (define-key pmail-edit-map "\C-c\C-c" 'pmail-cease-edit)
47 (define-key pmail-edit-map "\C-c\C-]" 'pmail-abort-edit))
48
49 ;; Pmail Edit mode is suitable only for specially formatted data.
50 (put 'pmail-edit-mode 'mode-class 'special)
51
52 (declare-function pmail-summary-disable "" ())
53 (declare-function pmail-summary-enable "pmailsum" ())
54
55 (defun pmail-edit-mode ()
56 "Major mode for editing the contents of an PMAIL message.
57 The editing commands are the same as in Text mode, together with two commands
58 to return to regular PMAIL:
59 * \\[pmail-abort-edit] cancels the changes
60 you have made and returns to PMAIL
61 * \\[pmail-cease-edit] makes them permanent.
62 This functions runs the normal hook `pmail-edit-mode-hook'.
63 \\{pmail-edit-map}"
64 ;; Changing the major mode unswaps the Pmail buffer
65 ;; via change-major-mode-hook.
66 (delay-mode-hooks (text-mode))
67 (use-local-map pmail-edit-map)
68 (setq major-mode 'pmail-edit-mode)
69 (setq mode-name "PMAIL Edit")
70 (if (boundp 'mode-line-modified)
71 (setq mode-line-modified (default-value 'mode-line-modified))
72 (setq mode-line-format (default-value 'mode-line-format)))
73 (if (pmail-summary-exists)
74 (save-excursion
75 (set-buffer pmail-summary-buffer)
76 (pmail-summary-disable)))
77 (run-mode-hooks 'pmail-edit-mode-hook))
78
79 (defvar pmail-old-pruned nil)
80 (put 'pmail-old-pruned 'permanent-local t)
81
82 ;;;###autoload
83 (defun pmail-edit-current-message ()
84 "Edit the contents of this message."
85 (interactive)
86 (if (= pmail-total-messages 0)
87 (error "No messages in this buffer"))
88 (make-local-variable 'pmail-old-pruned)
89 (setq pmail-old-pruned (eq pmail-header-style 'normal))
90 ;; Changing the major mode unswaps the Pmail buffer
91 ;; via change-major-mode-hook.
92 (pmail-edit-mode)
93 (make-local-variable 'pmail-old-text)
94 (setq pmail-old-text (buffer-substring (point-min) (point-max)))
95 (setq buffer-read-only nil)
96 (force-mode-line-update)
97 (if (and (eq (key-binding "\C-c\C-c") 'pmail-cease-edit)
98 (eq (key-binding "\C-c\C-]") 'pmail-abort-edit))
99 (message "Editing: Type C-c C-c to return to Pmail, C-c C-] to abort")
100 (message "%s" (substitute-command-keys
101 "Editing: Type \\[pmail-cease-edit] to return to Pmail, \\[pmail-abort-edit] to abort"))))
102
103 (defun pmail-cease-edit ()
104 "Finish editing message; switch back to Pmail proper."
105 (interactive)
106 (if (pmail-summary-exists)
107 (save-excursion
108 (set-buffer pmail-summary-buffer)
109 (pmail-summary-enable)))
110 ;; Make sure buffer ends with a blank line.
111 (save-excursion
112 (goto-char (point-max))
113 (if (/= (preceding-char) ?\n)
114 (insert "\n"))
115 (unless (looking-back "\n\n")
116 (insert "\n"))
117 ;; Adjust the marker that points to the end of this message.
118 (set-marker (aref pmail-message-vector (1+ pmail-current-message))
119 (point)))
120 (let ((old pmail-old-text))
121 ;; Go back to Pmail mode, but carefully.
122 (force-mode-line-update)
123 (kill-all-local-variables)
124 (pmail-mode-1)
125 (if (boundp 'tool-bar-map)
126 (set (make-local-variable 'tool-bar-map) pmail-tool-bar-map))
127 (pmail-variables)
128 ;; If text has really changed, mark message as edited.
129 (unless (and (= (length old) (- (point-max) (point-min)))
130 (string= old (buffer-substring (point-min) (point-max))))
131 (setq old nil)
132 (pmail-set-attribute pmail-edited-attr-index t)
133 ;;;??? BROKEN perhaps.
134 ;; I think that the Summary-Line header may not be kept there any more.
135 (if (boundp 'pmail-summary-vector)
136 (progn
137 (aset pmail-summary-vector (1- pmail-current-message) nil)
138 (save-excursion
139 (pmail-widen-to-current-msgbeg
140 (function (lambda ()
141 (forward-line 2)
142 (if (looking-at "Summary-line: ")
143 (let ((buffer-read-only nil))
144 (delete-region (point)
145 (progn (forward-line 1)
146 (point))))))))))))
147 (save-excursion
148 (pmail-show-message)
149 (pmail-toggle-header (if pmail-old-pruned 1 0))))
150 (run-hooks 'pmail-mode-hook))
151
152 (defun pmail-abort-edit ()
153 "Abort edit of current message; restore original contents."
154 (interactive)
155 (delete-region (point-min) (point-max))
156 (insert pmail-old-text)
157 (pmail-cease-edit)
158 (pmail-highlight-headers))
159
160 (provide 'pmailedit)
161
162 ;; Local Variables:
163 ;; change-log-default-name: "ChangeLog.pmail"
164 ;; End:
165
166 ;; arch-tag: 9524f335-12cc-4e95-9e9b-3208dc30550b
167 ;;; pmailedit.el ends here