Commit | Line | Data |
---|---|---|
537ab246 BG |
1 | ;;; rmailkwd.el --- part of the "RMAIL" mail reader for Emacs |
2 | ||
3 | ;; Copyright (C) 1985, 1988, 1994, 2001, 2002, 2003, 2004, 2005, 2006, | |
4 | ;; 2007, 2008, 2009 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 3 of the License, or | |
14 | ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;;; Code: | |
27 | ||
28 | (require 'rmail) | |
29 | ||
c36e6767 GM |
30 | ;; Global to all RMAIL buffers. It exists for the sake of completion. |
31 | ;; It is better to use strings with the label functions and let them | |
32 | ;; worry about making the label. | |
33 | (defvar rmail-label-obarray (make-vector 47 0) | |
34 | "Obarray of labels used by Rmail. | |
35 | `rmail-read-label' uses this to offer completion.") | |
537ab246 | 36 | |
c36e6767 GM |
37 | ;; Initialize with the standard labels. |
38 | (mapc (lambda (s) (intern (cadr s) rmail-label-obarray)) | |
39 | rmail-attr-array) | |
537ab246 BG |
40 | |
41 | (defun rmail-make-label (s) | |
8e87c426 | 42 | "Intern string S as a downcased symbol in `rmail-label-obarray'." |
537ab246 BG |
43 | (intern (downcase s) rmail-label-obarray)) |
44 | \f | |
45 | ;;;###autoload | |
c36e6767 | 46 | (defun rmail-add-label (label) |
537ab246 | 47 | "Add LABEL to labels associated with current RMAIL message. |
c36e6767 GM |
48 | Completes (see `rmail-read-label') over known labels when reading. |
49 | LABEL may be a symbol or string." | |
537ab246 | 50 | (interactive (list (rmail-read-label "Add label"))) |
c36e6767 | 51 | (rmail-set-label label t)) |
537ab246 BG |
52 | |
53 | ;;;###autoload | |
c36e6767 | 54 | (defun rmail-kill-label (label) |
537ab246 | 55 | "Remove LABEL from labels associated with current RMAIL message. |
c36e6767 GM |
56 | Completes (see `rmail-read-label') over known labels when reading. |
57 | LABEL may be a symbol or string." | |
537ab246 | 58 | (interactive (list (rmail-read-label "Remove label"))) |
c36e6767 | 59 | (rmail-set-label label nil)) |
537ab246 BG |
60 | |
61 | ;;;###autoload | |
62 | (defun rmail-read-label (prompt) | |
c36e6767 GM |
63 | "Read a label with completion, prompting with PROMPT. |
64 | Completions are chosen from `rmail-label-obarray'. The default | |
65 | is `rmail-last-label', if that is non-nil. Updates `rmail-last-label' | |
66 | according to the choice made, and returns a symbol." | |
28e50631 GM |
67 | (let* ((old nil) |
68 | (result | |
69 | (progn | |
70 | ;; If the summary exists, we've already read all the | |
71 | ;; existing labels. If not, read the ones in this message. | |
72 | (or (eq major-mode 'rmail-summary-mode) | |
73 | (rmail-summary-exists) | |
74 | (and (setq old (rmail-get-keywords)) | |
75 | (mapc 'rmail-make-label (split-string old ", ")))) | |
76 | (completing-read (concat prompt | |
77 | (if rmail-last-label | |
78 | (concat " (default " | |
79 | (symbol-name rmail-last-label) | |
80 | "): ") | |
81 | ": ")) | |
82 | rmail-label-obarray | |
83 | nil | |
84 | nil)))) | |
537ab246 BG |
85 | (if (string= result "") |
86 | rmail-last-label | |
87 | (setq rmail-last-label (rmail-make-label result))))) | |
88 | ||
314a8fc3 GM |
89 | (declare-function rmail-summary-update-line "rmailsum" (n)) |
90 | ||
537ab246 | 91 | (defun rmail-set-label (label state &optional msg) |
c36e6767 GM |
92 | "Set LABEL as present or absent according to STATE in message MSG. |
93 | LABEL may be a symbol or string." | |
94 | (or (stringp label) (setq label (symbol-name label))) | |
537ab246 BG |
95 | (with-current-buffer rmail-buffer |
96 | (rmail-maybe-set-message-counters) | |
8e87c426 | 97 | (or msg (setq msg rmail-current-message)) |
537ab246 BG |
98 | ;; Force recalculation of summary for this message. |
99 | (aset rmail-summary-vector (1- msg) nil) | |
100 | (let (attr-index) | |
101 | ;; Is this label an attribute? | |
102 | (dotimes (i (length rmail-attr-array)) | |
103 | (if (string= (cadr (aref rmail-attr-array i)) label) | |
104 | (setq attr-index i))) | |
105 | (if attr-index | |
106 | ;; If so, set it as an attribute. | |
107 | (rmail-set-attribute attr-index state msg) | |
108 | ;; Is this keyword already present in msg's keyword list? | |
3e8fc1b6 | 109 | (let* ((header (rmail-get-keywords msg)) |
655a218a | 110 | (regexp (concat ", " (regexp-quote label) ",")) |
4b84883a GM |
111 | (present (not (null |
112 | (string-match regexp (concat ", " header ",")))))) | |
537ab246 BG |
113 | ;; If current state is not correct, |
114 | (unless (eq present state) | |
115 | ;; either add it or delete it. | |
116 | (rmail-set-header | |
117 | rmail-keyword-header msg | |
118 | (if state | |
119 | ;; Add this keyword at the end. | |
120 | (if (and header (not (string= header ""))) | |
c36e6767 GM |
121 | (concat header ", " label) |
122 | label) | |
537ab246 BG |
123 | ;; Delete this keyword. |
124 | (let ((before (substring header 0 | |
125 | (max 0 (- (match-beginning 0) 2)))) | |
126 | (after (substring header | |
127 | (min (length header) | |
128 | (- (match-end 0) 1))))) | |
129 | (cond ((string= before "") | |
a99e0e1b GM |
130 | ;; If before and after both empty, delete the header. |
131 | (unless (string= after "") | |
132 | after)) | |
537ab246 BG |
133 | ((string= after "") |
134 | before) | |
c36e6767 GM |
135 | (t (concat before ", " after)))))))))) |
136 | (if (rmail-summary-exists) | |
8e87c426 | 137 | (rmail-select-summary (rmail-summary-update-line msg))) |
c36e6767 GM |
138 | (if (= msg rmail-current-message) |
139 | (rmail-display-labels)))) | |
537ab246 BG |
140 | \f |
141 | ;; Motion on messages with keywords. | |
142 | ||
143 | ;;;###autoload | |
144 | (defun rmail-previous-labeled-message (n labels) | |
145 | "Show previous message with one of the labels LABELS. | |
146 | LABELS should be a comma-separated list of label names. | |
147 | If LABELS is empty, the last set of labels specified is used. | |
148 | With prefix argument N moves backward N messages with these labels." | |
149 | (interactive "p\nsMove to previous msg with labels: ") | |
150 | (rmail-next-labeled-message (- n) labels)) | |
151 | ||
152 | (declare-function mail-comma-list-regexp "mail-utils" (labels)) | |
153 | ||
154 | ;;;###autoload | |
155 | (defun rmail-next-labeled-message (n labels) | |
156 | "Show next message with one of the labels LABELS. | |
157 | LABELS should be a comma-separated list of label names. | |
158 | If LABELS is empty, the last set of labels specified is used. | |
159 | With prefix argument N moves forward N messages with these labels." | |
160 | (interactive "p\nsMove to next msg with labels: ") | |
161 | (if (string= labels "") | |
162 | (setq labels rmail-last-multi-labels)) | |
163 | (or labels | |
164 | (error "No labels to find have been specified previously")) | |
165 | (set-buffer rmail-buffer) | |
166 | (setq rmail-last-multi-labels labels) | |
167 | (rmail-maybe-set-message-counters) | |
168 | (let ((lastwin rmail-current-message) | |
169 | (current rmail-current-message) | |
6e77127f | 170 | (regexp (concat " \\(" |
537ab246 | 171 | (mail-comma-list-regexp labels) |
ee1b0cc0 | 172 | "\\)\\(,\\|\\'\\)"))) |
537ab246 BG |
173 | (while (and (> n 0) (< current rmail-total-messages)) |
174 | (setq current (1+ current)) | |
175 | (if (string-match regexp (rmail-get-labels current)) | |
176 | (setq lastwin current n (1- n)))) | |
177 | (while (and (< n 0) (> current 1)) | |
178 | (setq current (1- current)) | |
179 | (if (string-match regexp (rmail-get-labels current)) | |
180 | (setq lastwin current n (1+ n)))) | |
181 | (if (< n 0) | |
182 | (error "No previous message with labels %s" labels) | |
183 | (if (> n 0) | |
184 | (error "No following message with labels %s" labels) | |
185 | (rmail-show-message lastwin))))) | |
186 | ||
187 | (provide 'rmailkwd) | |
188 | ||
537ab246 BG |
189 | ;; arch-tag: 1149979c-8e47-4333-9629-cf3dc887a6a7 |
190 | ;;; rmailkwd.el ends here |