1 ;; "RMAIL" mail reader for Emacs.
2 ;; Copyright (C) 1985, 1988 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;; Global to all RMAIL buffers. It exists primarily for the sake of
22 ;; completion. It is better to use strings with the label functions
23 ;; and let them worry about making the label.
25 (defvar rmail-label-obarray
(make-vector 47 0))
27 ;; Named list of symbols representing valid message attributes in RMAIL.
29 (defconst rmail-attributes
31 (mapcar '(lambda (s) (intern s rmail-label-obarray
))
32 '("deleted" "answered" "filed" "forwarded" "unseen" "edited"))))
34 (defconst rmail-deleted-label
(intern "deleted" rmail-label-obarray
))
36 ;; Named list of symbols representing valid message keywords in RMAIL.
38 (defvar rmail-keywords nil
)
40 (defun rmail-add-label (string)
41 "Add LABEL to labels associated with current RMAIL message.
42 Completion is performed over known labels when reading."
43 (interactive (list (rmail-read-label "Add label")))
44 (rmail-set-label string t
))
46 (defun rmail-kill-label (string)
47 "Remove LABEL from labels associated with current RMAIL message.
48 Completion is performed over known labels when reading."
49 (interactive (list (rmail-read-label "Remove label")))
50 (rmail-set-label string nil
))
52 (defun rmail-read-label (prompt)
53 (if (not rmail-keywords
) (rmail-parse-file-keywords))
55 (completing-read (concat prompt
58 (symbol-name rmail-last-label
)
64 (if (string= result
"")
66 (setq rmail-last-label
(rmail-make-label result t
)))))
68 (defun rmail-set-label (l state
&optional n
)
69 (rmail-maybe-set-message-counters)
70 (if (not n
) (setq n rmail-current-message
))
71 (aset rmail-summary-vector
(1- n
) nil
)
72 (let* ((attribute (rmail-attribute-p l
))
73 (keyword (and (not attribute
)
74 (or (rmail-keyword-p l
)
75 (rmail-install-keyword l
))))
76 (label (or attribute keyword
)))
78 (let ((omax (- (buffer-size) (point-max)))
79 (omin (- (buffer-size) (point-min)))
80 (buffer-read-only nil
)
85 (goto-char (rmail-msgbeg n
))
87 (if (not (looking-at "[01],"))
89 (let ((start (1+ (point)))
91 (narrow-to-region (point) (progn (end-of-line) (point)))
92 (setq bound
(point-max))
93 (search-backward ",," nil t
)
95 (setq bound
(1+ (point)))
96 (setq start
(1+ (point))))
98 ; (while (re-search-forward "[ \t]*,[ \t]*" nil t)
99 ; (replace-match ","))
101 (if (re-search-forward
102 (concat ", " (rmail-quote-label-name label
) ",")
105 (if (not state
) (replace-match ","))
106 (if state
(insert " " (symbol-name label
) ",")))
107 (if (eq label rmail-deleted-label
)
108 (rmail-set-message-deleted-p n state
)))))
109 (narrow-to-region (- (buffer-size) omin
) (- (buffer-size) omax
))
110 (if (= n rmail-current-message
) (rmail-display-labels)))))))
112 ;; Commented functions aren't used by RMAIL but might be nice for user
113 ;; packages that do stuff with RMAIL. Note that rmail-message-labels-p
114 ;; is in rmail.el now.
116 ;(defun rmail-message-attribute-p (attribute &optional n)
117 ; "Returns t if ATTRIBUTE on NTH or current message."
118 ; (rmail-message-labels-p (rmail-make-label attribute t) n))
120 ;(defun rmail-message-keyword-p (keyword &optional n)
121 ; "Returns t if KEYWORD on NTH or current message."
122 ; (rmail-message-labels-p (rmail-make-label keyword t) n t))
124 ;(defun rmail-message-label-p (label &optional n)
125 ; "Returns symbol if LABEL (attribute or keyword) on NTH or current message."
126 ; (rmail-message-labels-p (rmail-make-label label t) n 'all))
128 ;; Not used by RMAIL but might be nice for user package.
130 ;(defun rmail-parse-message-labels (&optional n)
131 ; "Returns labels associated with NTH or current RMAIL message.
132 ;Results is a list of two lists. The first is the message attributes
133 ;and the second is the message keywords. Labels are represented as symbols."
134 ; (let ((omin (- (buffer-size) (point-min)))
135 ; (omax (- (buffer-size) (point-max)))
139 ; (let ((beg (rmail-msgbeg (or n rmail-current-message))))
143 ; (if (looking-at "[01],")
145 ; (narrow-to-region (point) (save-excursion (end-of-line) (point)))
146 ; (rmail-nuke-whitespace)
147 ; (goto-char (1+ (point-min)))
148 ; (list (mail-parse-comma-list) (mail-parse-comma-list))))))
149 ; (narrow-to-region (- (buffer-size) omin)
150 ; (- (buffer-size) omax))
153 (defun rmail-attribute-p (s)
154 (let ((symbol (rmail-make-label s
)))
155 (if (memq symbol
(cdr rmail-attributes
)) symbol
)))
157 (defun rmail-keyword-p (s)
158 (let ((symbol (rmail-make-label s
)))
159 (if (memq symbol
(cdr (rmail-keywords))) symbol
)))
161 (defun rmail-make-label (s &optional forcep
)
162 (cond ((symbolp s
) s
)
163 (forcep (intern (downcase s
) rmail-label-obarray
))
164 (t (intern-soft (downcase s
) rmail-label-obarray
))))
166 (defun rmail-force-make-label (s)
167 (intern (downcase s
) rmail-label-obarray
))
169 (defun rmail-quote-label-name (label)
170 (regexp-quote (symbol-name (rmail-make-label label t
))))
172 ;; Motion on messages with keywords.
174 (defun rmail-previous-labeled-message (n labels
)
175 "Show previous message with one of the labels LABELS.
176 LABELS should be a comma-separated list of label names.
177 If LABELS is empty, the last set of labels specified is used.
178 With prefix argument N moves backward N messages with these labels."
179 (interactive "p\nsMove to previous msg with labels: ")
180 (rmail-next-labeled-message (- n
) labels
))
182 (defun rmail-next-labeled-message (n labels
)
183 "Show next message with one of the labels LABELS.
184 LABELS should be a comma-separated list of label names.
185 If LABELS is empty, the last set of labels specified is used.
186 With prefix argument N moves forward N messages with these labels."
187 (interactive "p\nsMove to next msg with labels: ")
188 (if (string= labels
"")
189 (setq labels rmail-last-multi-labels
))
191 (error "No labels to find have been specified previously"))
192 (setq rmail-last-multi-labels labels
)
193 (rmail-maybe-set-message-counters)
194 (let ((lastwin rmail-current-message
)
195 (current rmail-current-message
)
196 (regexp (concat ", ?\\("
197 (mail-comma-list-regexp labels
)
201 (while (and (> n
0) (< current rmail-total-messages
))
202 (setq current
(1+ current
))
203 (if (rmail-message-labels-p current regexp
)
204 (setq lastwin current n
(1- n
))))
205 (while (and (< n
0) (> current
1))
206 (setq current
(1- current
))
207 (if (rmail-message-labels-p current regexp
)
208 (setq lastwin current n
(1+ n
)))))
209 (rmail-show-message lastwin
)
211 (message "No previous message with labels %s" labels
))
213 (message "No following message with labels %s" labels
))))
215 ;;; Manipulate the file's Labels option.
217 ;; Return a list of symbols for all
218 ;; the keywords (labels) recorded in this file's Labels option.
219 (defun rmail-keywords ()
220 (or rmail-keywords
(rmail-parse-file-keywords)))
222 ;; Set rmail-keywords to a list of symbols for all
223 ;; the keywords (labels) recorded in this file's Labels option.
224 (defun rmail-parse-file-keywords ()
230 (if (search-forward "\nLabels:" (rmail-msgbeg 1) t
)
232 (narrow-to-region (point) (progn (end-of-line) (point)))
233 (goto-char (point-min))
234 (cons 'rmail-keywords
235 (mapcar 'rmail-force-make-label
236 (mail-parse-comma-list)))))))))
238 ;; Add WORD to the list in the file's Labels option.
239 ;; Any keyword used for the first time needs this done.
240 (defun rmail-install-keyword (word)
241 (let ((keyword (rmail-make-label word t
))
242 (keywords (rmail-keywords)))
243 (if (not (or (rmail-attribute-p keyword
)
244 (rmail-keyword-p keyword
)))
245 (let ((omin (- (buffer-size) (point-min)))
246 (omax (- (buffer-size) (point-max))))
251 (let ((case-fold-search t
)
252 (buffer-read-only nil
))
253 (or (search-forward "\nLabels:" nil t
)
256 (insert "\nLabels:")))
257 (delete-region (point) (progn (end-of-line) (point)))
258 (setcdr keywords
(cons keyword
(cdr keywords
)))
259 (while (setq keywords
(cdr keywords
))
260 (insert (symbol-name (car keywords
)) ","))
262 (narrow-to-region (- (buffer-size) omin
)
263 (- (buffer-size) omax
)))))