annotate lisp/mail/rmailedit.el @ 102407:4c603ec78d71

(rmail-cease-edit): Use rmail-ensure-blank-line.
author Glenn Morris <rgm@gnu.org>
date Thu, 05 Mar 2009 03:45:51 +0000
parents b5125b379b62
children f4571df6b6cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
1 ;;; rmailedit.el --- "RMAIL edit mode" Edit the current message
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
2
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
3 ;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
4 ;; 2008, 2009 Free Software Foundation, Inc.
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
5
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
6 ;; Maintainer: FSF
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
7 ;; Keywords: mail
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
8
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
10
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
13 ;; the Free Software Foundation, either version 3 of the License, or
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
14 ;; (at your option) any later version.
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
15
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
20
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
23
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
24 ;;; Commentary:
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
25
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
26 ;;; Code:
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
27
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
28 (require 'rmail)
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
29
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
30 (defcustom rmail-edit-mode-hook nil
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
31 "List of functions to call when editing an RMAIL message."
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
32 :type 'hook
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
33 :version "21.1"
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
34 :group 'rmail-edit)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
35
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
36
101724
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
37 (defvar rmail-edit-map
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
38 (let ((map (make-sparse-keymap)))
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
39 ;; Make a keymap that inherits text-mode-map.
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
40 (set-keymap-parent map text-mode-map)
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
41 (define-key map "\C-c\C-c" 'rmail-cease-edit)
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
42 (define-key map "\C-c\C-]" 'rmail-abort-edit)
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
43 map))
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
44
101633
c92166611a26 (rmail-summary-disable): Fix declaration.
Glenn Morris <rgm@gnu.org>
parents: 101629
diff changeset
45 (declare-function rmail-summary-disable "rmailsum" ())
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
46
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
47 (defun rmail-edit-mode ()
102388
b5125b379b62 (rmail-edit-mode): Doc fix.
Glenn Morris <rgm@gnu.org>
parents: 102165
diff changeset
48 "Major mode for editing the contents of an Rmail message.
b5125b379b62 (rmail-edit-mode): Doc fix.
Glenn Morris <rgm@gnu.org>
parents: 102165
diff changeset
49 The editing commands are the same as in Text mode, together with
b5125b379b62 (rmail-edit-mode): Doc fix.
Glenn Morris <rgm@gnu.org>
parents: 102165
diff changeset
50 two commands to return to regular Rmail:
b5125b379b62 (rmail-edit-mode): Doc fix.
Glenn Morris <rgm@gnu.org>
parents: 102165
diff changeset
51 * \\[rmail-abort-edit] cancels any changes and returns to Rmail
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
52 * \\[rmail-cease-edit] makes them permanent.
102388
b5125b379b62 (rmail-edit-mode): Doc fix.
Glenn Morris <rgm@gnu.org>
parents: 102165
diff changeset
53 This function runs the hooks `text-mode-hook' and `rmail-edit-mode-hook'.
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
54 \\{rmail-edit-map}"
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
55 (if (rmail-summary-exists)
101724
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
56 (with-current-buffer rmail-summary-buffer
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
57 (rmail-summary-disable)))
102388
b5125b379b62 (rmail-edit-mode): Doc fix.
Glenn Morris <rgm@gnu.org>
parents: 102165
diff changeset
58 ;; Prevent change-major-mode-hook from unswapping the buffers.
b5125b379b62 (rmail-edit-mode): Doc fix.
Glenn Morris <rgm@gnu.org>
parents: 102165
diff changeset
59 (let ((rmail-buffer-swapped nil))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
60 (delay-mode-hooks (text-mode))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
61 (use-local-map rmail-edit-map)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
62 (setq major-mode 'rmail-edit-mode)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
63 (setq mode-name "RMAIL Edit")
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
64 (if (boundp 'mode-line-modified)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
65 (setq mode-line-modified (default-value 'mode-line-modified))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
66 (setq mode-line-format (default-value 'mode-line-format)))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
67 (run-mode-hooks 'rmail-edit-mode-hook)))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
68
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
69 ;; Rmail Edit mode is suitable only for specially formatted data.
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
70 (put 'rmail-edit-mode 'mode-class 'special)
102388
b5125b379b62 (rmail-edit-mode): Doc fix.
Glenn Morris <rgm@gnu.org>
parents: 102165
diff changeset
71
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
72
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
73 (defvar rmail-old-text)
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
74 (defvar rmail-old-pruned nil
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
75 "Non-nil means the message being edited originally had pruned headers.")
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
76 (put 'rmail-old-pruned 'permanent-local t)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
77
102023
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
78 (defvar rmail-old-headers nil
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
79 "Holds the headers of this message before editing started.")
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
80 (put 'rmail-old-headers 'permanent-local t)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
81
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
82 ;;;###autoload
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
83 (defun rmail-edit-current-message ()
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
84 "Edit the contents of this message."
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
85 (interactive)
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
86 (if (zerop rmail-total-messages)
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
87 (error "No messages in this buffer"))
102023
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
88 (make-local-variable 'rmail-old-pruned)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
89 (setq rmail-old-pruned (rmail-msg-is-pruned))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
90 (rmail-edit-mode)
102023
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
91 (make-local-variable 'rmail-old-text)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
92 (setq rmail-old-text
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
93 (save-restriction
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
94 (widen)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
95 (buffer-substring (point-min) (point-max))))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
96 (make-local-variable 'rmail-old-headers)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
97 (setq rmail-old-headers (rmail-edit-headers-alist t))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
98 (setq buffer-read-only nil)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
99 (setq buffer-undo-list nil)
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
100 ;; FIXME whether the buffer is initially marked as modified or not
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
101 ;; depends on whether or not the underlying rmail buffer was so marked.
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
102 ;; Seems poor.
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
103 (force-mode-line-update)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
104 (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
105 (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
106 (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
107 (message "%s" (substitute-command-keys
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
108 "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
109
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
110
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
111 (declare-function rmail-summary-enable "rmailsum" ())
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
112
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
113 (defun rmail-cease-edit ()
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
114 "Finish editing message; switch back to Rmail proper."
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
115 (interactive)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
116 (if (rmail-summary-exists)
101724
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
117 (with-current-buffer rmail-summary-buffer
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
118 (rmail-summary-enable)))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
119 (widen)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
120 ;; Disguise any "From " lines so they don't start a new message.
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
121 (save-excursion
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
122 (goto-char (point-min))
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
123 (or rmail-old-pruned (forward-line 1))
101629
caac907da88d (rmail-cease-edit): Add additional quotes to quoted From lines.
Chong Yidong <cyd@stupidchicken.com>
parents: 101369
diff changeset
124 (while (re-search-forward "^>*From " nil t)
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
125 (beginning-of-line)
101629
caac907da88d (rmail-cease-edit): Add additional quotes to quoted From lines.
Chong Yidong <cyd@stupidchicken.com>
parents: 101369
diff changeset
126 (insert ">")
caac907da88d (rmail-cease-edit): Add additional quotes to quoted From lines.
Chong Yidong <cyd@stupidchicken.com>
parents: 101369
diff changeset
127 (forward-line)))
102407
4c603ec78d71 (rmail-cease-edit): Use rmail-ensure-blank-line.
Glenn Morris <rgm@gnu.org>
parents: 102388
diff changeset
128 ;; Make sure buffer ends with a blank line so as not to run this
4c603ec78d71 (rmail-cease-edit): Use rmail-ensure-blank-line.
Glenn Morris <rgm@gnu.org>
parents: 102388
diff changeset
129 ;; message together with the following one.
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
130 (save-excursion
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
131 (goto-char (point-max))
102407
4c603ec78d71 (rmail-cease-edit): Use rmail-ensure-blank-line.
Glenn Morris <rgm@gnu.org>
parents: 102388
diff changeset
132 (rmail-ensure-blank-line))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
133 (let ((old rmail-old-text)
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
134 (pruned rmail-old-pruned)
102165
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
135 ;; People who know what they are doing might have modified the
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
136 ;; buffer's encoding if editing the message included inserting
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
137 ;; characters that were unencodable by the original message's
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
138 ;; encoding. Make note of the new encoding and use it for
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
139 ;; encoding the edited message.
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
140 (edited-coding buffer-file-coding-system)
102023
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
141 new-headers
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
142 character-coding is-text-message coding-system
101871
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
143 headers-end limit)
102165
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
144 ;; Make sure `edited-coding' can safely encode the edited message.
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
145 (setq edited-coding
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
146 (select-safe-coding-system (point-min) (point-max) edited-coding))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
147 ;; Go back to Rmail mode, but carefully.
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
148 (force-mode-line-update)
101724
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
149 (let ((rmail-buffer-swapped nil)) ; Prevent change-major-mode-hook
b87c9a6e8490 (rmail-edit-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 101633
diff changeset
150 ; from unswapping the buffers.
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
151 (kill-all-local-variables)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
152 (rmail-mode-1)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
153 (if (boundp 'tool-bar-map)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
154 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
155 (setq buffer-undo-list t)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
156 (rmail-variables))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
157 ;; If text has really changed, mark message as edited.
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
158 (unless (and (= (length old) (- (point-max) (point-min)))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
159 (string= old (buffer-substring (point-min) (point-max))))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
160 (setq old nil)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
161 (goto-char (point-min))
102165
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
162 ;; If they changed the message's encoding, rewrite the charset=
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
163 ;; header for them, so that subsequent rmail-show-message
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
164 ;; decodes it correctly.
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
165 (let ((buffer-read-only nil)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
166 (new-coding (coding-system-base edited-coding))
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
167 old-coding mime-charset mime-beg mime-end)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
168 (when (re-search-forward rmail-mime-charset-pattern
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
169 (1- (save-excursion (search-forward "\n\n")))
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
170 'move)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
171 (setq mime-beg (match-beginning 1)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
172 mime-end (match-end 1)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
173 old-coding (coding-system-from-name (match-string 1))))
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
174 (setq mime-charset
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
175 (symbol-name
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
176 (or (coding-system-get new-coding :mime-charset)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
177 (if (coding-system-equal new-coding 'undecided)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
178 'us-ascii
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
179 new-coding))))
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
180 (cond
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
181 ((null old-coding)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
182 ;; If there was no charset= spec, insert one.
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
183 (insert "Content-type: text/plain; charset=" mime-charset "\n"))
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
184 ((not (coding-system-equal (coding-system-base old-coding)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
185 new-coding))
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
186 (delete-region mime-beg mime-end)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
187 (insert mime-charset))))
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
188 (goto-char (point-min))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
189 (search-forward "\n\n")
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
190 (setq headers-end (point))
102023
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
191 (setq new-headers (rmail-edit-headers-alist t))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
192 (rmail-swap-buffers-maybe)
101867
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
193 (narrow-to-region (rmail-msgbeg rmail-current-message)
101871
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
194 (rmail-msgend rmail-current-message))
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
195 (save-restriction
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
196 (setq limit
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
197 (save-excursion
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
198 (goto-char (point-min))
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
199 (search-forward "\n\n" nil t)))
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
200 ;; All 3 of the functions we call below assume the buffer was
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
201 ;; narrowed to just the headers of the message.
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
202 (narrow-to-region (rmail-msgbeg rmail-current-message) limit)
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
203 (setq character-coding
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
204 (mail-fetch-field "content-transfer-encoding")
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
205 is-text-message (rmail-is-text-p)
102165
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
206 coding-system (if (and edited-coding
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
207 (not (coding-system-equal
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
208 (coding-system-base edited-coding)
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
209 'undecided)))
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
210 edited-coding
f93f5035af02 (rmail-cease-edit): Notice changes in buffer's encoding during editing.
Eli Zaretskii <eliz@gnu.org>
parents: 102046
diff changeset
211 (rmail-get-coding-system))))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
212 (if character-coding
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
213 (setq character-coding (downcase character-coding)))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
214
101871
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
215 (goto-char limit)
101867
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
216 (let ((inhibit-read-only t))
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
217 (let ((data-buffer (current-buffer))
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
218 (end (copy-marker (point) t)))
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
219 (with-current-buffer rmail-view-buffer
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
220 (encode-coding-region headers-end (point-max) coding-system
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
221 data-buffer))
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
222 (delete-region end (point-max)))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
223
102023
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
224 ;; Apply to the mbox buffer any changes in header fields
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
225 ;; that the user made while editing in the view buffer.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
226 (rmail-edit-update-headers (rmail-edit-diff-headers
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
227 rmail-old-headers new-headers))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
228
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
229 ;; Re-apply content-transfer-encoding, if any, on the message body.
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
230 (cond
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
231 ((string= character-coding "quoted-printable")
101867
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
232 (mail-quote-printable-region (point) (point-max)))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
233 ((and (string= character-coding "base64") is-text-message)
101867
07f6502ced98 (rmail-cease-edit): narrow-to-region before extracting mail message headers.
Eli Zaretskii <eliz@gnu.org>
parents: 101724
diff changeset
234 (base64-encode-region (point) (point-max)))
101871
cf870c811594 (rmail-cease-edit): Look for the message's encoding, and its
Eli Zaretskii <eliz@gnu.org>
parents: 101867
diff changeset
235 ((and (eq character-coding 'uuencode) is-text-message)
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
236 (error "uuencoded messages are not supported"))))
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
237 (rmail-set-attribute rmail-edited-attr-index t))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
238 ;;??? BROKEN perhaps.
101917
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
239 ;;; (if (boundp 'rmail-summary-vector)
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
240 ;;; (aset rmail-summary-vector (1- rmail-current-message) nil))
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
241 (save-excursion
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
242 (rmail-show-message)
4b1bce6e82a6 Just require rmail.
Glenn Morris <rgm@gnu.org>
parents: 101871
diff changeset
243 (rmail-toggle-header (if pruned 1 0))))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
244 (run-hooks 'rmail-mode-hook))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
245
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
246 (defun rmail-abort-edit ()
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
247 "Abort edit of current message; restore original contents."
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
248 (interactive)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
249 (widen)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
250 (delete-region (point-min) (point-max))
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
251 (insert rmail-old-text)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
252 (rmail-cease-edit)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
253 (rmail-highlight-headers))
102023
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
254
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
255 (defun rmail-edit-headers-alist (&optional widen markers)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
256 "Return an alist of the headers of the message in the current buffer.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
257 Each element has the form (HEADER-NAME . ENTIRE-STRING).
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
258 ENTIRE-STRING includes the name of the header field (which is HEADER-NAME)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
259 and has a final newline.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
260 If part of the text is not valid as a header field, HEADER-NAME
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
261 is an integer and we use consecutive integers.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
262
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
263 If WIDEN is non-nil, operate on the entire buffer.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
264
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
265 If MARKERS is non-nil, the value looks like
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
266 \(HEADER-NAME ENTIRE-STRING BEG-MARKER END-MARKER)."
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
267 (let (header-alist (no-good-header-count 1))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
268 (save-excursion
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
269 (save-restriction
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
270 (if widen (widen))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
271 (goto-char (point-min))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
272 (search-forward "\n\n")
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
273 (narrow-to-region (point-min) (1- (point)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
274 (goto-char (point-min))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
275 (while (not (eobp))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
276 (let ((start (point))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
277 name header)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
278 ;; Match the name.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
279 (if (looking-at "[ \t]*\\([^:\n \t]\\(\\|[^:\n]*[^:\n \t]\\)\\)[ \t]*:")
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
280 (setq name (match-string-no-properties 1))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
281 (setq name no-good-header-count
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
282 no-good-header-count (1+ no-good-header-count)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
283 (forward-line 1)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
284 (while (looking-at "[ \t]")
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
285 (forward-line 1))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
286 (setq header (buffer-substring-no-properties start (point)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
287 (if markers
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
288 (push (list header (copy-marker start) (point-marker))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
289 header-alist)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
290 (push (cons name header) header-alist))))))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
291 (nreverse header-alist)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
292
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
293
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
294 (defun rmail-edit-diff-headers (old-headers new-headers)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
295 "Compare OLD-HEADERS and NEW-HEADERS and return field differences.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
296 The value is a list of three lists, (INSERTED DELETED CHANGED).
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
297
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
298 INSERTED's elements describe inserted header fields
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
299 and each looks like (AFTER-WHAT INSERT-WHAT)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
300 INSERT-WHAT is the header field to insert (a member of NEW-HEADERS).
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
301 AFTER-WHAT is the field to insert it after (a member of NEW-HEADERS)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
302 or else nil to insert it at the beginning.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
303
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
304 DELETED's elements are elements of OLD-HEADERS.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
305 CHANGED's elements have the form (OLD . NEW)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
306 where OLD is a element of OLD-HEADERS and NEW is an element of NEW-HEADERS."
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
307
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
308 (let ((reverse-new (reverse new-headers))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
309 inserted deleted changed)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
310 (dolist (old old-headers)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
311 (let ((new (assoc (car old) new-headers)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
312 ;; If it's in OLD-HEADERS and has no new counterpart,
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
313 ;; it is a deletion.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
314 (if (null new)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
315 (push old deleted)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
316 ;; If it has a new counterpart, maybe it was changed.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
317 (unless (equal (cdr old) (cdr new))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
318 (push (cons old new) changed))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
319 ;; Remove the new counterpart, since it has been spoken for.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
320 (setq new-headers (remq new new-headers)))))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
321 ;; Look at the new headers with no old counterpart.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
322 (dolist (new new-headers)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
323 (let ((prev (cadr (member new reverse-new))))
102046
9b33e73e70e3 (rmail-edit-diff-headers): Delete spurious reference to OLD.
Richard M. Stallman <rms@gnu.org>
parents: 102023
diff changeset
324 ;; Mark each one as an insertion.
9b33e73e70e3 (rmail-edit-diff-headers): Delete spurious reference to OLD.
Richard M. Stallman <rms@gnu.org>
parents: 102023
diff changeset
325 ;; Record the previous new header, to insert it after that.
9b33e73e70e3 (rmail-edit-diff-headers): Delete spurious reference to OLD.
Richard M. Stallman <rms@gnu.org>
parents: 102023
diff changeset
326 (push (list prev new) inserted)))
102023
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
327 ;; It is crucial to return the insertions in buffer order
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
328 ;; so that `rmail-edit-update-headers' can insert a field
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
329 ;; after a new field.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
330 (list (nreverse inserted)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
331 (nreverse deleted)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
332 (nreverse changed))))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
333
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
334 (defun rmail-edit-update-headers (header-diff)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
335 "Edit the mail headers in the buffer based on HEADER-DIFF.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
336 HEADER-DIFF should be a return value from `rmail-edit-diff-headers'."
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
337 (let ((buf-headers (rmail-edit-headers-alist nil t)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
338 ;; Change all the fields scheduled for being changed.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
339 (dolist (chg (nth 2 header-diff))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
340 (let* ((match (assoc (cdar chg) buf-headers))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
341 (end (marker-position (nth 2 match))))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
342 (goto-char end)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
343 ;; Insert the new, then delete the old.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
344 ;; That avoids collapsing markers.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
345 (insert-before-markers (cddr chg))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
346 (delete-region (nth 1 match) end)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
347 ;; Remove the old field from BUF-HEADERS.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
348 (setq buf-headers (delq match buf-headers))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
349 ;; Update BUF-HEADERS to show the changed field.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
350 (push (list (cddr chg) (point-marker)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
351 (copy-marker (- (point) (length (cddr chg))))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
352 (point-marker))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
353 buf-headers)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
354 ;; Delete all the fields scheduled for deletion.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
355 ;; We do deletion after changes
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
356 ;; because when two fields look alike and get replaced by one,
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
357 ;; the first of them is considered changed
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
358 ;; and the second is considered deleted.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
359 (dolist (del (nth 1 header-diff))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
360 (let ((match (assoc (cdr del) buf-headers)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
361 (delete-region (nth 1 match) (nth 2 match))))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
362 ;; Insert all the fields scheduled for insertion.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
363 (dolist (ins (nth 0 header-diff))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
364 (let* ((new (cadr ins))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
365 (after (car ins))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
366 (match (assoc (cdr after) buf-headers)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
367 (goto-char (if match (nth 2 match) (point-min)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
368 (insert (cdr new))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
369 ;; Update BUF-HEADERS to show the inserted field.
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
370 (push (list (cdr new)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
371 (copy-marker (- (point) (length (cdr new))))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
372 (point-marker))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
373 buf-headers)))
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
374 ;; Disconnect the markers
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
375 (dolist (hdr buf-headers)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
376 (set-marker (nth 1 hdr) nil)
a92cde59fabc Handle editing of header fields.
Richard M. Stallman <rms@gnu.org>
parents: 101917
diff changeset
377 (set-marker (nth 2 hdr) nil))))
101360
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
378
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
379 (provide 'rmailedit)
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
380
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
381 ;; arch-tag: 9524f335-12cc-4e95-9e9b-3208dc30550b
b10df8502c4a Renamed all pmail* files to rmail*.
Bastien Guerry <bzg@altern.org>
parents:
diff changeset
382 ;;; rmailedit.el ends here