1 ;;; rmailkwd.el --- part of the "RMAIL" mail reader for Emacs.
3 ;; Copyright (C) 1985, 1988 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;; Global to all RMAIL buffers. It exists primarily for the sake of
23 ;; completion. It is better to use strings with the label functions
24 ;; and let them worry about making the label.
26 (defvar rmail-label-obarray
(make-vector 47 0))
28 ;; Named list of symbols representing valid message attributes in RMAIL.
30 (defconst rmail-attributes
32 (mapcar '(lambda (s) (intern s rmail-label-obarray
))
33 '("deleted" "answered" "filed" "forwarded" "unseen" "edited"))))
35 (defconst rmail-deleted-label
(intern "deleted" rmail-label-obarray
))
37 ;; Named list of symbols representing valid message keywords in RMAIL.
39 (defvar rmail-keywords nil
)
41 (defun rmail-add-label (string)
42 "Add LABEL to labels associated with current RMAIL message.
43 Completion is performed over known labels when reading."
44 (interactive (list (rmail-read-label "Add label")))
45 (rmail-set-label string t
))
47 (defun rmail-kill-label (string)
48 "Remove LABEL from labels associated with current RMAIL message.
49 Completion is performed over known labels when reading."
50 (interactive (list (rmail-read-label "Remove label")))
51 (rmail-set-label string nil
))
53 (defun rmail-read-label (prompt)
54 (if (not rmail-keywords
) (rmail-parse-file-keywords))
56 (completing-read (concat prompt
59 (symbol-name rmail-last-label
)
65 (if (string= result
"")
67 (setq rmail-last-label
(rmail-make-label result t
)))))
69 (defun rmail-set-label (l state
&optional n
)
70 (rmail-maybe-set-message-counters)
71 (if (not n
) (setq n rmail-current-message
))
72 (aset rmail-summary-vector
(1- n
) nil
)
73 (let* ((attribute (rmail-attribute-p l
))
74 (keyword (and (not attribute
)
75 (or (rmail-keyword-p l
)
76 (rmail-install-keyword l
))))
77 (label (or attribute keyword
)))
79 (let ((omax (- (buffer-size) (point-max)))
80 (omin (- (buffer-size) (point-min)))
81 (buffer-read-only nil
)
86 (goto-char (rmail-msgbeg n
))
88 (if (not (looking-at "[01],"))
90 (let ((start (1+ (point)))
92 (narrow-to-region (point) (progn (end-of-line) (point)))
93 (setq bound
(point-max))
94 (search-backward ",," nil t
)
96 (setq bound
(1+ (point)))
97 (setq start
(1+ (point))))
99 ; (while (re-search-forward "[ \t]*,[ \t]*" nil t)
100 ; (replace-match ","))
102 (if (re-search-forward
103 (concat ", " (rmail-quote-label-name label
) ",")
106 (if (not state
) (replace-match ","))
107 (if state
(insert " " (symbol-name label
) ",")))
108 (if (eq label rmail-deleted-label
)
109 (rmail-set-message-deleted-p n state
)))))
110 (narrow-to-region (- (buffer-size) omin
) (- (buffer-size) omax
))
111 (if (= n rmail-current-message
) (rmail-display-labels)))))))
113 ;; Commented functions aren't used by RMAIL but might be nice for user
114 ;; packages that do stuff with RMAIL. Note that rmail-message-labels-p
115 ;; is in rmail.el now.
117 ;(defun rmail-message-attribute-p (attribute &optional n)
118 ; "Returns t if ATTRIBUTE on NTH or current message."
119 ; (rmail-message-labels-p (rmail-make-label attribute t) n))
121 ;(defun rmail-message-keyword-p (keyword &optional n)
122 ; "Returns t if KEYWORD on NTH or current message."
123 ; (rmail-message-labels-p (rmail-make-label keyword t) n t))
125 ;(defun rmail-message-label-p (label &optional n)
126 ; "Returns symbol if LABEL (attribute or keyword) on NTH or current message."
127 ; (rmail-message-labels-p (rmail-make-label label t) n 'all))
129 ;; Not used by RMAIL but might be nice for user package.
131 ;(defun rmail-parse-message-labels (&optional n)
132 ; "Returns labels associated with NTH or current RMAIL message.
133 ;Results is a list of two lists. The first is the message attributes
134 ;and the second is the message keywords. Labels are represented as symbols."
135 ; (let ((omin (- (buffer-size) (point-min)))
136 ; (omax (- (buffer-size) (point-max)))
140 ; (let ((beg (rmail-msgbeg (or n rmail-current-message))))
144 ; (if (looking-at "[01],")
146 ; (narrow-to-region (point) (save-excursion (end-of-line) (point)))
147 ; (rmail-nuke-whitespace)
148 ; (goto-char (1+ (point-min)))
149 ; (list (mail-parse-comma-list) (mail-parse-comma-list))))))
150 ; (narrow-to-region (- (buffer-size) omin)
151 ; (- (buffer-size) omax))
154 (defun rmail-attribute-p (s)
155 (let ((symbol (rmail-make-label s
)))
156 (if (memq symbol
(cdr rmail-attributes
)) symbol
)))
158 (defun rmail-keyword-p (s)
159 (let ((symbol (rmail-make-label s
)))
160 (if (memq symbol
(cdr (rmail-keywords))) symbol
)))
162 (defun rmail-make-label (s &optional forcep
)
163 (cond ((symbolp s
) s
)
164 (forcep (intern (downcase s
) rmail-label-obarray
))
165 (t (intern-soft (downcase s
) rmail-label-obarray
))))
167 (defun rmail-force-make-label (s)
168 (intern (downcase s
) rmail-label-obarray
))
170 (defun rmail-quote-label-name (label)
171 (regexp-quote (symbol-name (rmail-make-label label t
))))
173 ;; Motion on messages with keywords.
175 (defun rmail-previous-labeled-message (n labels
)
176 "Show previous message with one of the labels LABELS.
177 LABELS should be a comma-separated list of label names.
178 If LABELS is empty, the last set of labels specified is used.
179 With prefix argument N moves backward N messages with these labels."
180 (interactive "p\nsMove to previous msg with labels: ")
181 (rmail-next-labeled-message (- n
) labels
))
183 (defun rmail-next-labeled-message (n labels
)
184 "Show next message with one of the labels LABELS.
185 LABELS should be a comma-separated list of label names.
186 If LABELS is empty, the last set of labels specified is used.
187 With prefix argument N moves forward N messages with these labels."
188 (interactive "p\nsMove to next msg with labels: ")
189 (if (string= labels
"")
190 (setq labels rmail-last-multi-labels
))
192 (error "No labels to find have been specified previously"))
193 (setq rmail-last-multi-labels labels
)
194 (rmail-maybe-set-message-counters)
195 (let ((lastwin rmail-current-message
)
196 (current rmail-current-message
)
197 (regexp (concat ", ?\\("
198 (mail-comma-list-regexp labels
)
202 (while (and (> n
0) (< current rmail-total-messages
))
203 (setq current
(1+ current
))
204 (if (rmail-message-labels-p current regexp
)
205 (setq lastwin current n
(1- n
))))
206 (while (and (< n
0) (> current
1))
207 (setq current
(1- current
))
208 (if (rmail-message-labels-p current regexp
)
209 (setq lastwin current n
(1+ n
)))))
210 (rmail-show-message lastwin
)
212 (message "No previous message with labels %s" labels
))
214 (message "No following message with labels %s" labels
))))
216 ;;; Manipulate the file's Labels option.
218 ;; Return a list of symbols for all
219 ;; the keywords (labels) recorded in this file's Labels option.
220 (defun rmail-keywords ()
221 (or rmail-keywords
(rmail-parse-file-keywords)))
223 ;; Set rmail-keywords to a list of symbols for all
224 ;; the keywords (labels) recorded in this file's Labels option.
225 (defun rmail-parse-file-keywords ()
231 (if (search-forward "\nLabels:" (rmail-msgbeg 1) t
)
233 (narrow-to-region (point) (progn (end-of-line) (point)))
234 (goto-char (point-min))
235 (cons 'rmail-keywords
236 (mapcar 'rmail-force-make-label
237 (mail-parse-comma-list)))))))))
239 ;; Add WORD to the list in the file's Labels option.
240 ;; Any keyword used for the first time needs this done.
241 (defun rmail-install-keyword (word)
242 (let ((keyword (rmail-make-label word t
))
243 (keywords (rmail-keywords)))
244 (if (not (or (rmail-attribute-p keyword
)
245 (rmail-keyword-p keyword
)))
246 (let ((omin (- (buffer-size) (point-min)))
247 (omax (- (buffer-size) (point-max))))
252 (let ((case-fold-search t
)
253 (buffer-read-only nil
))
254 (or (search-forward "\nLabels:" nil t
)
257 (insert "\nLabels:")))
258 (delete-region (point) (progn (end-of-line) (point)))
259 (setcdr keywords
(cons keyword
(cdr keywords
)))
260 (while (setq keywords
(cdr keywords
))
261 (insert (symbol-name (car keywords
)) ","))
263 (narrow-to-region (- (buffer-size) omin
)
264 (- (buffer-size) omax
)))))
267 ;;; rmailkwd.el ends here