]> code.delx.au - gnu-emacs/blob - lisp/mail/pmailedit.el
Require pmailhdr.
[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 (require 'pmailhdr)
33
34 (defcustom pmail-edit-mode-hook nil
35 "List of functions to call when editing an PMAIL message."
36 :type 'hook
37 :version "21.1"
38 :group 'pmail-edit)
39
40 (defvar pmail-old-text)
41
42 (defvar pmail-edit-map nil)
43 (if pmail-edit-map
44 nil
45 ;; Make a keymap that inherits text-mode-map.
46 (setq pmail-edit-map (make-sparse-keymap))
47 (set-keymap-parent pmail-edit-map text-mode-map)
48 (define-key pmail-edit-map "\C-c\C-c" 'pmail-cease-edit)
49 (define-key pmail-edit-map "\C-c\C-]" 'pmail-abort-edit))
50
51 ;; Pmail Edit mode is suitable only for specially formatted data.
52 (put 'pmail-edit-mode 'mode-class 'special)
53
54 (declare-function pmail-summary-disable "" ())
55 (declare-function pmail-summary-enable "pmailsum" ())
56
57 (defun pmail-edit-mode ()
58 "Major mode for editing the contents of an PMAIL message.
59 The editing commands are the same as in Text mode, together with two commands
60 to return to regular PMAIL:
61 * \\[pmail-abort-edit] cancels the changes
62 you have made and returns to PMAIL
63 * \\[pmail-cease-edit] makes them permanent.
64 This functions runs the normal hook `pmail-edit-mode-hook'.
65 \\{pmail-edit-map}"
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 (defvar pmail-edit-saved-coding-system nil)
83 (put 'pmail-edit-saved-coding-system 'permanent-local t)
84
85 ;;;###autoload
86 (defun pmail-edit-current-message ()
87 "Edit the contents of this message."
88 (interactive)
89 (if (= pmail-total-messages 0)
90 (error "No messages in this file"))
91 (make-local-variable 'pmail-old-pruned)
92 (setq pmail-old-pruned (eq pmail-header-style 'normal))
93 (make-local-variable 'pmail-edit-saved-coding-system)
94 (setq pmail-edit-saved-coding-system save-buffer-coding-system)
95 (pmail-header-show-headers)
96 (pmail-edit-mode)
97 ;; As the local value of save-buffer-coding-system is deleted by
98 ;; pmail-edit-mode, we restore the original value.
99 (make-local-variable 'save-buffer-coding-system)
100 (setq save-buffer-coding-system pmail-edit-saved-coding-system)
101 (make-local-variable 'pmail-old-text)
102 (setq pmail-old-text (buffer-substring (point-min) (point-max)))
103 (setq buffer-read-only nil)
104 (force-mode-line-update)
105 (if (and (eq (key-binding "\C-c\C-c") 'pmail-cease-edit)
106 (eq (key-binding "\C-c\C-]") 'pmail-abort-edit))
107 (message "Editing: Type C-c C-c to return to Pmail, C-c C-] to abort")
108 (message "%s" (substitute-command-keys
109 "Editing: Type \\[pmail-cease-edit] to return to Pmail, \\[pmail-abort-edit] to abort"))))
110
111 (defun pmail-cease-edit ()
112 "Finish editing message; switch back to Pmail proper."
113 (interactive)
114 (if (pmail-summary-exists)
115 (save-excursion
116 (set-buffer pmail-summary-buffer)
117 (pmail-summary-enable)))
118 ;; Make sure buffer ends with a newline.
119 (save-excursion
120 (goto-char (point-max))
121 (if (/= (preceding-char) ?\n)
122 (insert "\n"))
123 ;; Adjust the marker that points to the end of this message.
124 (set-marker (aref pmail-message-vector (1+ pmail-current-message))
125 (point)))
126 (let ((old pmail-old-text))
127 (force-mode-line-update)
128 (kill-all-local-variables)
129 (pmail-mode-1)
130 (if (boundp 'tool-bar-map)
131 (set (make-local-variable 'tool-bar-map) pmail-tool-bar-map))
132 (pmail-variables)
133 ;; As the local value of save-buffer-coding-system is changed by
134 ;; pmail-variables, we restore the original value.
135 (setq save-buffer-coding-system pmail-edit-saved-coding-system)
136 (if (and (= (length old) (- (point-max) (point-min)))
137 (string= old (buffer-substring (point-min) (point-max))))
138 ()
139 (setq old nil)
140 (pmail-set-attribute "edited" t)
141 (if (boundp 'pmail-summary-vector)
142 (progn
143 (aset pmail-summary-vector (1- pmail-current-message) nil)
144 (save-excursion
145 (pmail-widen-to-current-msgbeg
146 (function (lambda ()
147 (forward-line 2)
148 (if (looking-at "Summary-line: ")
149 (let ((buffer-read-only nil))
150 (delete-region (point)
151 (progn (forward-line 1)
152 (point))))))))))))
153 (save-excursion
154 (pmail-show-message)
155 (pmail-toggle-header (if pmail-old-pruned 1 0))))
156 (run-hooks 'pmail-mode-hook)
157 (setq buffer-read-only t))
158
159 (defun pmail-abort-edit ()
160 "Abort edit of current message; restore original contents."
161 (interactive)
162 (delete-region (point-min) (point-max))
163 (insert pmail-old-text)
164 (pmail-cease-edit)
165 (pmail-highlight-headers))
166
167 (provide 'pmailedit)
168
169 ;; Local Variables:
170 ;; change-log-default-name: "ChangeLog.pmail"
171 ;; End:
172
173 ;; arch-tag: 9524f335-12cc-4e95-9e9b-3208dc30550b
174 ;;; pmailedit.el ends here