]> code.delx.au - gnu-emacs/blob - lisp/mail/pmailedit.el
mail/: Add new (temporary) libaries for which to test Rmail/mbox such
[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 'pmaildesc)
31 (require 'pmailsum))
32
33 (defcustom pmail-edit-mode-hook nil
34 "List of functions to call when editing an PMAIL message."
35 :type 'hook
36 :version "21.1"
37 :group 'pmail-edit)
38
39 (defvar pmail-old-text)
40
41 (defvar pmail-edit-map
42 (let ((map (make-sparse-keymap)))
43 (set-keymap-parent map text-mode-map)
44 (define-key map "\C-c\C-c" 'pmail-cease-edit)
45 (define-key map "\C-c\C-]" 'pmail-abort-edit)
46 map)
47 "Keymap for `pmail-edit-mode'.")
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 (delay-mode-hooks (text-mode))
65 (use-local-map pmail-edit-map)
66 (setq major-mode 'pmail-edit-mode)
67 (setq mode-name "PMAIL Edit")
68 (if (boundp 'mode-line-modified)
69 (setq mode-line-modified (default-value 'mode-line-modified))
70 (setq mode-line-format (default-value 'mode-line-format)))
71 (if (pmail-summary-exists)
72 (save-excursion
73 (set-buffer pmail-summary-buffer)
74 (pmail-summary-disable)))
75 (run-mode-hooks 'pmail-edit-mode-hook))
76
77 (defvar pmail-old-pruned nil)
78 (put 'pmail-old-pruned 'permanent-local t)
79
80 (defvar pmail-edit-saved-coding-system nil)
81 (put 'pmail-edit-saved-coding-system 'permanent-local t)
82
83 ;;;###autoload
84 (defun pmail-edit-current-message ()
85 "Edit the contents of this message."
86 (interactive)
87 (if (= pmail-total-messages 0)
88 (error "No messages in this file"))
89 (make-local-variable 'pmail-old-pruned)
90 (setq pmail-old-pruned (pmail-msg-is-pruned))
91 (make-local-variable 'pmail-edit-saved-coding-system)
92 (setq pmail-edit-saved-coding-system save-buffer-coding-system)
93 (pmail-header-show-headers)
94 (pmail-edit-mode)
95 ;; As the local value of save-buffer-coding-system is deleted by
96 ;; pmail-edit-mode, we restore the original value.
97 (make-local-variable 'save-buffer-coding-system)
98 (setq save-buffer-coding-system pmail-edit-saved-coding-system)
99 (make-local-variable 'pmail-old-text)
100 (setq pmail-old-text (buffer-substring (point-min) (point-max)))
101 (setq buffer-read-only nil)
102 (force-mode-line-update)
103 (if (and (eq (key-binding "\C-c\C-c") 'pmail-cease-edit)
104 (eq (key-binding "\C-c\C-]") 'pmail-abort-edit))
105 (message "Editing: Type C-c C-c to return to Pmail, C-c C-] to abort")
106 (message "%s" (substitute-command-keys
107 "Editing: Type \\[pmail-cease-edit] to return to Pmail, \\[pmail-abort-edit] to abort"))))
108
109 (defun pmail-cease-edit ()
110 "Finish editing message; switch back to Pmail proper."
111 (interactive)
112 (when (pmail-summary-exists)
113 (with-current-buffer pmail-summary-buffer
114 (pmail-summary-enable)))
115 ;; Make sure buffer ends with a newline.
116 (save-excursion
117 (goto-char (point-max))
118 (when (/= (preceding-char) ?\n)
119 (insert "\n"))
120 ;; Adjust the marker that points to the end of this message, unless
121 ;; we're at the last message.
122 (when (< pmail-current-message (length pmail-desc-vector))
123 (pmail-desc-set-start (1+ pmail-current-message) (point))))
124 (let ((old pmail-old-text))
125 (force-mode-line-update)
126 (kill-all-local-variables)
127 (pmail-mode-1)
128 (if (boundp 'tool-bar-map)
129 (set (make-local-variable 'tool-bar-map) pmail-tool-bar-map))
130 (pmail-variables)
131 ;; As the local value of save-buffer-coding-system is changed by
132 ;; pmail-variables, we restore the original value.
133 (setq save-buffer-coding-system pmail-edit-saved-coding-system)
134 (unless (and (= (length old) (- (point-max) (point-min)))
135 (string= old (buffer-substring (point-min) (point-max))))
136 (setq old nil)
137 (pmail-set-attribute "edited" t))
138 (save-excursion
139 (pmail-show-message)
140 ;; `pmail-show-message' always hides the headers, so we show them
141 ;; here if they were visible before starting the edit.
142 (when pmail-old-pruned
143 (pmail-header-show-headers))))
144 (run-hooks 'pmail-mode-hook)
145 (setq buffer-read-only t))
146
147 (defun pmail-abort-edit ()
148 "Abort edit of current message; restore original contents."
149 (interactive)
150 (delete-region (point-min) (point-max))
151 (insert pmail-old-text)
152 (pmail-cease-edit))
153
154 (provide 'pmailedit)
155
156 ;; arch-tag: 93c22709-a14a-46c1-ab91-52c3f5a0ec12
157 ;;; pmailedit.el ends here