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