rmailedit.el 5.66 KB
Newer Older
1
;;; rmailedit.el --- "RMAIL edit mode"  Edit the current message
Eric S. Raymond's avatar
Eric S. Raymond committed
2

3
;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2005, 2006, 2007 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
7
;; Keywords: mail
Eric S. Raymond's avatar
Eric S. Raymond committed
8

Joseph Arceneaux's avatar
Joseph Arceneaux committed
9 10 11 12
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
13
;; the Free Software Foundation; either version 3, or (at your option)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
14 15 16 17 18 19 20 21
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
22
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
23 24
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
25

26 27
;;; Commentary:

Eric S. Raymond's avatar
Eric S. Raymond committed
28
;;; Code:
Joseph Arceneaux's avatar
Joseph Arceneaux committed
29 30 31

(require 'rmail)

Gerd Moellmann's avatar
Gerd Moellmann committed
32 33 34
(defcustom rmail-edit-mode-hook nil
  "List of functions to call when editing an RMAIL message."
  :type 'hook
35
  :version "21.1"
Gerd Moellmann's avatar
Gerd Moellmann committed
36 37
  :group 'rmail-edit)

38 39
(defvar rmail-old-text)

Joseph Arceneaux's avatar
Joseph Arceneaux committed
40 41 42
(defvar rmail-edit-map nil)
(if rmail-edit-map
    nil
43
  ;; Make a keymap that inherits text-mode-map.
44 45
  (setq rmail-edit-map (make-sparse-keymap))
  (set-keymap-parent rmail-edit-map text-mode-map)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
46 47 48 49 50 51
  (define-key rmail-edit-map "\C-c\C-c" 'rmail-cease-edit)
  (define-key rmail-edit-map "\C-c\C-]" 'rmail-abort-edit))

;; Rmail Edit mode is suitable only for specially formatted data.
(put 'rmail-edit-mode 'mode-class 'special)

52 53 54
(declare-function rmail-summary-disable "" ())
(declare-function rmail-summary-enable "rmailsum" ())

Joseph Arceneaux's avatar
Joseph Arceneaux committed
55 56 57 58
(defun rmail-edit-mode ()
  "Major mode for editing the contents of an RMAIL message.
The editing commands are the same as in Text mode, together with two commands
to return to regular RMAIL:
Gerd Moellmann's avatar
Gerd Moellmann committed
59
  *  \\[rmail-abort-edit] cancels the changes
Joseph Arceneaux's avatar
Joseph Arceneaux committed
60
     you have made and returns to RMAIL
Gerd Moellmann's avatar
Gerd Moellmann committed
61 62
  *  \\[rmail-cease-edit] makes them permanent.
This functions runs the normal hook `rmail-edit-mode-hook'.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
63
\\{rmail-edit-map}"
64
  (delay-mode-hooks (text-mode))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
65 66 67 68 69 70
  (use-local-map rmail-edit-map)
  (setq major-mode 'rmail-edit-mode)
  (setq mode-name "RMAIL Edit")
  (if (boundp 'mode-line-modified)
      (setq mode-line-modified (default-value 'mode-line-modified))
    (setq mode-line-format (default-value 'mode-line-format)))
71
  (if (rmail-summary-exists)
Karl Heuer's avatar
Karl Heuer committed
72 73 74
      (save-excursion
	(set-buffer rmail-summary-buffer)
	(rmail-summary-disable)))
75
  (run-mode-hooks 'rmail-edit-mode-hook))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
76

77 78 79
(defvar rmail-old-pruned nil)
(put 'rmail-old-pruned 'permanent-local t)

80 81 82
(defvar rmail-edit-saved-coding-system nil)
(put 'rmail-edit-saved-coding-system 'permanent-local t)

83
;;;###autoload
Joseph Arceneaux's avatar
Joseph Arceneaux committed
84 85 86
(defun rmail-edit-current-message ()
  "Edit the contents of this message."
  (interactive)
87 88
  (make-local-variable 'rmail-old-pruned)
  (setq rmail-old-pruned (rmail-msg-is-pruned))
89 90
  (make-local-variable 'rmail-edit-saved-coding-system)
  (setq rmail-edit-saved-coding-system save-buffer-coding-system)
91
  (rmail-toggle-header 0)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
92
  (rmail-edit-mode)
93 94 95 96
  ;; As the local value of save-buffer-coding-system is deleted by
  ;; rmail-edit-mode, we restore the original value.
  (make-local-variable 'save-buffer-coding-system)
  (setq save-buffer-coding-system rmail-edit-saved-coding-system)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
97 98 99
  (make-local-variable 'rmail-old-text)
  (setq rmail-old-text (buffer-substring (point-min) (point-max)))
  (setq buffer-read-only nil)
100
  (force-mode-line-update)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
101 102 103
  (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
	   (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
      (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
104
    (message "%s" (substitute-command-keys
105
		   "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
106 107 108 109

(defun rmail-cease-edit ()
  "Finish editing message; switch back to Rmail proper."
  (interactive)
110
  (if (rmail-summary-exists)
Karl Heuer's avatar
Karl Heuer committed
111 112 113
      (save-excursion
	(set-buffer rmail-summary-buffer)
	(rmail-summary-enable)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
114 115 116 117 118 119 120 121 122
  ;; Make sure buffer ends with a newline.
  (save-excursion
    (goto-char (point-max))
    (if (/= (preceding-char) ?\n)
	(insert "\n"))
    ;; Adjust the marker that points to the end of this message.
    (set-marker (aref rmail-message-vector (1+ rmail-current-message))
		(point)))
  (let ((old rmail-old-text))
123
    (force-mode-line-update)
124
    (kill-all-local-variables)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
125
    (rmail-mode-1)
126
    (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map)
127
    (rmail-variables)
128 129 130
    ;; As the local value of save-buffer-coding-system is changed by
    ;; rmail-variables, we restore the original value.
    (setq save-buffer-coding-system rmail-edit-saved-coding-system)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
131 132 133 134 135 136 137 138 139 140
    (if (and (= (length old) (- (point-max) (point-min)))
	     (string= old (buffer-substring (point-min) (point-max))))
	()
      (setq old nil)
      (rmail-set-attribute "edited" t)
      (if (boundp 'rmail-summary-vector)
	  (progn
	    (aset rmail-summary-vector (1- rmail-current-message) nil)
	    (save-excursion
	      (rmail-widen-to-current-msgbeg
141
		(function (lambda ()
Joseph Arceneaux's avatar
Joseph Arceneaux committed
142 143 144 145 146
			    (forward-line 2)
			    (if (looking-at "Summary-line: ")
				(let ((buffer-read-only nil))
				  (delete-region (point)
						 (progn (forward-line 1)
147 148
							(point))))))))))))
    (save-excursion
149 150
      (rmail-show-message)
      (rmail-toggle-header (if rmail-old-pruned 1 0))))
151
  (run-hooks 'rmail-mode-hook)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
152 153 154 155 156 157 158
  (setq buffer-read-only t))

(defun rmail-abort-edit ()
  "Abort edit of current message; restore original contents."
  (interactive)
  (delete-region (point-min) (point-max))
  (insert rmail-old-text)
159 160
  (rmail-cease-edit)
  (rmail-highlight-headers))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
161

Markus Rost's avatar
Markus Rost committed
162 163
(provide 'rmailedit)

Miles Bader's avatar
Miles Bader committed
164
;;; arch-tag: 93c22709-a14a-46c1-ab91-52c3f5a0ec12
Eric S. Raymond's avatar
Eric S. Raymond committed
165
;;; rmailedit.el ends here