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