Nuke arch-tags.
[emacs.git] / lisp / mail / rmailkwd.el
blobb9b69231b884be335568cead13986646206e991e
1 ;;; rmailkwd.el --- part of the "RMAIL" mail reader for Emacs
3 ;; Copyright (C) 1985, 1988, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: mail
8 ;; Package: rmail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (require 'rmail)
31 ;; Global to all RMAIL buffers. It exists for the sake of completion.
32 ;; It is better to use strings with the label functions and let them
33 ;; worry about making the label.
34 (defvar rmail-label-obarray (make-vector 47 0)
35 "Obarray of labels used by Rmail.
36 `rmail-read-label' uses this to offer completion.")
38 ;; Initialize with the standard labels.
39 (mapc (lambda (s) (intern (cadr s) rmail-label-obarray))
40 rmail-attr-array)
42 (defun rmail-make-label (s)
43 "Intern string S as a downcased symbol in `rmail-label-obarray'."
44 (intern (downcase s) rmail-label-obarray))
46 ;;;###autoload
47 (defun rmail-add-label (label)
48 "Add LABEL to labels associated with current RMAIL message.
49 Completes (see `rmail-read-label') over known labels when reading.
50 LABEL may be a symbol or string. Only one label is allowed."
51 (interactive (list (rmail-read-label "Add label")))
52 (rmail-set-label label t))
54 ;;;###autoload
55 (defun rmail-kill-label (label)
56 "Remove LABEL from labels associated with current RMAIL message.
57 Completes (see `rmail-read-label') over known labels when reading.
58 LABEL may be a symbol or string. Only one label is allowed."
59 (interactive (list (rmail-read-label "Remove label")))
60 (rmail-set-label label nil))
62 ;;;###autoload
63 (defun rmail-read-label (prompt)
64 "Read a label with completion, prompting with PROMPT.
65 Completions are chosen from `rmail-label-obarray'. The default
66 is `rmail-last-label', if that is non-nil. Updates `rmail-last-label'
67 according to the choice made, and returns a symbol."
68 (let* ((old nil)
69 (result
70 (progn
71 ;; If the summary exists, we've already read all the
72 ;; existing labels. If not, read the ones in this message.
73 (or (eq major-mode 'rmail-summary-mode)
74 (rmail-summary-exists)
75 (and (setq old (rmail-get-keywords))
76 (mapc 'rmail-make-label (split-string old ", "))))
77 (completing-read (concat prompt
78 (if rmail-last-label
79 (concat " (default "
80 (symbol-name rmail-last-label)
81 "): ")
82 ": "))
83 rmail-label-obarray
84 nil
85 nil))))
86 (if (string= result "")
87 rmail-last-label
88 (setq rmail-last-label (rmail-make-label result)))))
90 (declare-function rmail-summary-update-line "rmailsum" (n))
92 (defun rmail-set-label (label state &optional msg)
93 "Set LABEL as present or absent according to STATE in message MSG.
94 LABEL may be a symbol or string."
95 (or (stringp label) (setq label (symbol-name label)))
96 (if (string-match "," label)
97 (error "More than one label specified"))
98 (with-current-buffer rmail-buffer
99 (rmail-maybe-set-message-counters)
100 (or msg (setq msg rmail-current-message))
101 ;; Force recalculation of summary for this message.
102 (aset rmail-summary-vector (1- msg) nil)
103 (let (attr-index)
104 ;; Is this label an attribute?
105 (dotimes (i (length rmail-attr-array))
106 (if (string= (cadr (aref rmail-attr-array i)) label)
107 (setq attr-index i)))
108 (if attr-index
109 ;; If so, set it as an attribute.
110 (rmail-set-attribute attr-index state msg)
111 ;; Is this keyword already present in msg's keyword list?
112 (let* ((header (rmail-get-keywords msg))
113 (regexp (concat ", " (regexp-quote label) ","))
114 (present (not (null
115 (string-match regexp (concat ", " header ","))))))
116 ;; If current state is not correct,
117 (unless (eq present state)
118 ;; either add it or delete it.
119 (rmail-set-header
120 rmail-keyword-header msg
121 (if state
122 ;; Add this keyword at the end.
123 (if (and header (not (string= header "")))
124 (concat header ", " label)
125 label)
126 ;; Delete this keyword.
127 (let ((before (substring header 0
128 (max 0 (- (match-beginning 0) 2))))
129 (after (substring header
130 (min (length header)
131 (- (match-end 0) 1)))))
132 (cond ((string= before "")
133 ;; If before and after both empty, delete the header.
134 (unless (string= after "")
135 after))
136 ((string= after "")
137 before)
138 (t (concat before ", " after))))))))))
139 (if (rmail-summary-exists)
140 (rmail-select-summary (rmail-summary-update-line msg)))
141 (if (= msg rmail-current-message)
142 (rmail-display-labels))))
144 ;; Motion on messages with keywords.
146 ;;;###autoload
147 (defun rmail-previous-labeled-message (n labels)
148 "Show previous message with one of the labels LABELS.
149 LABELS should be a comma-separated list of label names.
150 If LABELS is empty, the last set of labels specified is used.
151 With prefix argument N moves backward N messages with these labels."
152 (interactive "p\nsMove to previous msg with labels: ")
153 (rmail-next-labeled-message (- n) labels))
155 (declare-function mail-comma-list-regexp "mail-utils" (labels))
157 ;;;###autoload
158 (defun rmail-next-labeled-message (n labels)
159 "Show next message with one of the labels LABELS.
160 LABELS should be a comma-separated list of label names.
161 If LABELS is empty, the last set of labels specified is used.
162 With prefix argument N moves forward N messages with these labels."
163 ;; FIXME show the default in the prompt.
164 (interactive "p\nsMove to next msg with labels: ")
165 (if (string= labels "")
166 (setq labels rmail-last-multi-labels))
167 (or labels
168 (error "No labels to find have been specified previously"))
169 (set-buffer rmail-buffer)
170 (setq rmail-last-multi-labels labels)
171 (rmail-maybe-set-message-counters)
172 (let ((lastwin rmail-current-message)
173 (current rmail-current-message)
174 (regexp (concat " \\("
175 (mail-comma-list-regexp labels)
176 "\\)\\(,\\|\\'\\)")))
177 (while (and (> n 0) (< current rmail-total-messages))
178 (setq current (1+ current))
179 (if (string-match regexp (rmail-get-labels current))
180 (setq lastwin current n (1- n))))
181 (while (and (< n 0) (> current 1))
182 (setq current (1- current))
183 (if (string-match regexp (rmail-get-labels current))
184 (setq lastwin current n (1+ n))))
185 (if (< n 0)
186 (error "No previous message with labels %s" labels)
187 (if (> n 0)
188 (error "No following message with labels %s" labels)
189 (rmail-show-message-1 lastwin)))))
191 (provide 'rmailkwd)
193 ;; Local Variables:
194 ;; generated-autoload-file: "rmail.el"
195 ;; End:
197 ;;; rmailkwd.el ends here