Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / mail / rmailkwd.el
CommitLineData
55535639 1;;; rmailkwd.el --- part of the "RMAIL" mail reader for Emacs
c88ab9ce 2
e84b4b86 3;; Copyright (C) 1985, 1988, 1994, 2001, 2002, 2003, 2004,
2f043267 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
9750e079 5
e5167999 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: mail
e5167999 8
a2535589
JA
9;; This file is part of GNU Emacs.
10
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
a2535589 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
a2535589
JA
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
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a2535589 23
55535639
PJ
24;;; Commentary:
25
e5167999 26;;; Code:
a2535589 27
46163fc9
JB
28(defvar rmail-buffer)
29(defvar rmail-current-message)
30(defvar rmail-last-label)
31(defvar rmail-last-multi-labels)
32(defvar rmail-summary-vector)
33(defvar rmail-total-messages)
34
a2535589
JA
35;; Global to all RMAIL buffers. It exists primarily for the sake of
36;; completion. It is better to use strings with the label functions
37;; and let them worry about making the label.
38
39(defvar rmail-label-obarray (make-vector 47 0))
40
41;; Named list of symbols representing valid message attributes in RMAIL.
42
43(defconst rmail-attributes
44 (cons 'rmail-keywords
06562727
KH
45 (mapcar (function (lambda (s) (intern s rmail-label-obarray)))
46 '("deleted" "answered" "filed" "forwarded" "unseen" "edited"
47 "resent"))))
a2535589
JA
48
49(defconst rmail-deleted-label (intern "deleted" rmail-label-obarray))
50
51;; Named list of symbols representing valid message keywords in RMAIL.
52
013da4a5 53(defvar rmail-keywords)
a2535589 54\f
3457b06a 55;;;###autoload
a2535589
JA
56(defun rmail-add-label (string)
57 "Add LABEL to labels associated with current RMAIL message.
58Completion is performed over known labels when reading."
59 (interactive (list (rmail-read-label "Add label")))
60 (rmail-set-label string t))
61
3457b06a 62;;;###autoload
a2535589
JA
63(defun rmail-kill-label (string)
64 "Remove LABEL from labels associated with current RMAIL message.
65Completion is performed over known labels when reading."
66 (interactive (list (rmail-read-label "Remove label")))
67 (rmail-set-label string nil))
68
3457b06a 69;;;###autoload
a2535589 70(defun rmail-read-label (prompt)
eba7fdff
GM
71 (with-current-buffer rmail-buffer
72 (if (not rmail-keywords) (rmail-parse-file-keywords))
73 (let ((result
74 (completing-read (concat prompt
75 (if rmail-last-label
76 (concat " (default "
77 (symbol-name rmail-last-label)
78 "): ")
79 ": "))
80 rmail-label-obarray
81 nil
82 nil)))
83 (if (string= result "")
84 rmail-last-label
85 (setq rmail-last-label (rmail-make-label result t))))))
a2535589 86
73e72da4
DN
87(declare-function rmail-maybe-set-message-counters "rmail" ())
88(declare-function rmail-display-labels "rmail" ())
89(declare-function rmail-msgbeg "rmail" (n))
90(declare-function rmail-set-message-deleted-p "rmail" (n state))
91(declare-function rmail-message-labels-p "rmail" (msg labels))
92(declare-function rmail-show-message "rmail" (&optional n no-summary))
93(declare-function mail-comma-list-regexp "mail-utils" (labels))
94(declare-function mail-parse-comma-list "mail-utils.el" ())
95
a2535589 96(defun rmail-set-label (l state &optional n)
eba7fdff
GM
97 (with-current-buffer rmail-buffer
98 (rmail-maybe-set-message-counters)
99 (if (not n) (setq n rmail-current-message))
100 (aset rmail-summary-vector (1- n) nil)
101 (let* ((attribute (rmail-attribute-p l))
102 (keyword (and (not attribute)
103 (or (rmail-keyword-p l)
104 (rmail-install-keyword l))))
105 (label (or attribute keyword)))
106 (if label
107 (let ((omax (- (buffer-size) (point-max)))
108 (omin (- (buffer-size) (point-min)))
109 (buffer-read-only nil)
110 (case-fold-search t))
111 (unwind-protect
112 (save-excursion
113 (widen)
114 (goto-char (rmail-msgbeg n))
115 (forward-line 1)
116 (if (not (looking-at "[01],"))
117 nil
118 (let ((start (1+ (point)))
119 (bound))
120 (narrow-to-region (point) (progn (end-of-line) (point)))
121 (setq bound (point-max))
122 (search-backward ",," nil t)
123 (if attribute
124 (setq bound (1+ (point)))
125 (setq start (1+ (point))))
126 (goto-char start)
127; (while (re-search-forward "[ \t]*,[ \t]*" nil t)
128; (replace-match ","))
129; (goto-char start)
130 (if (re-search-forward
a2535589
JA
131 (concat ", " (rmail-quote-label-name label) ",")
132 bound
133 'move)
eba7fdff
GM
134 (if (not state) (replace-match ","))
135 (if state (insert " " (symbol-name label) ",")))
136 (if (eq label rmail-deleted-label)
137 (rmail-set-message-deleted-p n state)))))
138 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax))
139 (if (= n rmail-current-message) (rmail-display-labels))))))))
a2535589
JA
140\f
141;; Commented functions aren't used by RMAIL but might be nice for user
142;; packages that do stuff with RMAIL. Note that rmail-message-labels-p
ac13660a 143;; is in rmail.el now.
a2535589 144
a2535589
JA
145;(defun rmail-message-label-p (label &optional n)
146; "Returns symbol if LABEL (attribute or keyword) on NTH or current message."
745d9433 147; (rmail-message-labels-p (or n rmail-current-message) (regexp-quote label)))
a2535589
JA
148
149;(defun rmail-parse-message-labels (&optional n)
150; "Returns labels associated with NTH or current RMAIL message.
745d9433
KH
151;The result is a list of two lists of strings. The first is the
152;message attributes and the second is the message keywords."
153; (let (atts keys)
154; (save-restriction
155; (widen)
156; (goto-char (rmail-msgbeg (or n rmail-current-message)))
157; (forward-line 1)
158; (or (looking-at "[01],") (error "Malformed label line"))
159; (forward-char 2)
160; (while (looking-at "[ \t]*\\([^ \t\n,]+\\),")
161; (setq atts (cons (buffer-substring (match-beginning 1) (match-end 1))
162; atts))
163; (goto-char (match-end 0)))
164; (or (looking-at ",") (error "Malformed label line"))
165; (forward-char 1)
166; (while (looking-at "[ \t]*\\([^ \t\n,]+\\),")
167; (setq keys (cons (buffer-substring (match-beginning 1) (match-end 1))
168; keys))
169; (goto-char (match-end 0)))
170; (or (looking-at "[ \t]*$") (error "Malformed label line"))
171; (list (nreverse atts) (nreverse keys)))))
a2535589
JA
172
173(defun rmail-attribute-p (s)
174 (let ((symbol (rmail-make-label s)))
175 (if (memq symbol (cdr rmail-attributes)) symbol)))
176
177(defun rmail-keyword-p (s)
178 (let ((symbol (rmail-make-label s)))
179 (if (memq symbol (cdr (rmail-keywords))) symbol)))
180
181(defun rmail-make-label (s &optional forcep)
182 (cond ((symbolp s) s)
183 (forcep (intern (downcase s) rmail-label-obarray))
184 (t (intern-soft (downcase s) rmail-label-obarray))))
185
186(defun rmail-force-make-label (s)
187 (intern (downcase s) rmail-label-obarray))
188
189(defun rmail-quote-label-name (label)
190 (regexp-quote (symbol-name (rmail-make-label label t))))
191\f
192;; Motion on messages with keywords.
193
3457b06a 194;;;###autoload
ac13660a
JB
195(defun rmail-previous-labeled-message (n labels)
196 "Show previous message with one of the labels LABELS.
197LABELS should be a comma-separated list of label names.
198If LABELS is empty, the last set of labels specified is used.
a2535589
JA
199With prefix argument N moves backward N messages with these labels."
200 (interactive "p\nsMove to previous msg with labels: ")
ac13660a 201 (rmail-next-labeled-message (- n) labels))
a2535589 202
3457b06a 203;;;###autoload
a2535589 204(defun rmail-next-labeled-message (n labels)
ac13660a
JB
205 "Show next message with one of the labels LABELS.
206LABELS should be a comma-separated list of label names.
207If LABELS is empty, the last set of labels specified is used.
a2535589
JA
208With prefix argument N moves forward N messages with these labels."
209 (interactive "p\nsMove to next msg with labels: ")
210 (if (string= labels "")
211 (setq labels rmail-last-multi-labels))
212 (or labels
213 (error "No labels to find have been specified previously"))
eba7fdff 214 (set-buffer rmail-buffer)
a2535589
JA
215 (setq rmail-last-multi-labels labels)
216 (rmail-maybe-set-message-counters)
217 (let ((lastwin rmail-current-message)
218 (current rmail-current-message)
219 (regexp (concat ", ?\\("
220 (mail-comma-list-regexp labels)
221 "\\),")))
222 (save-restriction
223 (widen)
224 (while (and (> n 0) (< current rmail-total-messages))
225 (setq current (1+ current))
226 (if (rmail-message-labels-p current regexp)
227 (setq lastwin current n (1- n))))
228 (while (and (< n 0) (> current 1))
229 (setq current (1- current))
230 (if (rmail-message-labels-p current regexp)
231 (setq lastwin current n (1+ n)))))
232 (rmail-show-message lastwin)
233 (if (< n 0)
234 (message "No previous message with labels %s" labels))
235 (if (> n 0)
236 (message "No following message with labels %s" labels))))
237\f
238;;; Manipulate the file's Labels option.
239
240;; Return a list of symbols for all
241;; the keywords (labels) recorded in this file's Labels option.
242(defun rmail-keywords ()
243 (or rmail-keywords (rmail-parse-file-keywords)))
244
245;; Set rmail-keywords to a list of symbols for all
246;; the keywords (labels) recorded in this file's Labels option.
247(defun rmail-parse-file-keywords ()
248 (save-restriction
249 (save-excursion
250 (widen)
251 (goto-char 1)
252 (setq rmail-keywords
253 (if (search-forward "\nLabels:" (rmail-msgbeg 1) t)
254 (progn
255 (narrow-to-region (point) (progn (end-of-line) (point)))
256 (goto-char (point-min))
257 (cons 'rmail-keywords
258 (mapcar 'rmail-force-make-label
259 (mail-parse-comma-list)))))))))
260
261;; Add WORD to the list in the file's Labels option.
262;; Any keyword used for the first time needs this done.
263(defun rmail-install-keyword (word)
264 (let ((keyword (rmail-make-label word t))
265 (keywords (rmail-keywords)))
266 (if (not (or (rmail-attribute-p keyword)
267 (rmail-keyword-p keyword)))
268 (let ((omin (- (buffer-size) (point-min)))
269 (omax (- (buffer-size) (point-max))))
270 (unwind-protect
271 (save-excursion
272 (widen)
273 (goto-char 1)
274 (let ((case-fold-search t)
275 (buffer-read-only nil))
276 (or (search-forward "\nLabels:" nil t)
277 (progn
278 (end-of-line)
279 (insert "\nLabels:")))
280 (delete-region (point) (progn (end-of-line) (point)))
281 (setcdr keywords (cons keyword (cdr keywords)))
282 (while (setq keywords (cdr keywords))
283 (insert (symbol-name (car keywords)) ","))
284 (delete-char -1)))
285 (narrow-to-region (- (buffer-size) omin)
286 (- (buffer-size) omax)))))
287 keyword))
c88ab9ce 288
cbee283d 289;; arch-tag: b26b3392-99ca-4e1d-933a-dab59b04e9a8
c88ab9ce 290;;; rmailkwd.el ends here