(rmail-next-labeled-message): Correctly handle return value of
[bpt/emacs.git] / lisp / mail / rmailkwd.el
CommitLineData
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
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(mapc (function (lambda (s) (intern s rmail-label-obarray)))
37 '("deleted" "answered" "filed" "forwarded" "unseen" "edited"
38 "resent"))
39
40(defun rmail-make-label (s)
41 (intern (downcase s) rmail-label-obarray))
42\f
43;;;###autoload
44(defun rmail-add-label (string)
45 "Add LABEL to labels associated with current RMAIL message.
46Performs completion over known labels when reading."
47 (interactive (list (rmail-read-label "Add label")))
48 (rmail-set-label string t))
49
50;;;###autoload
51(defun rmail-kill-label (string)
52 "Remove LABEL from labels associated with current RMAIL message.
53Performs completion over known labels when reading."
54 (interactive (list (rmail-read-label "Remove label")))
55 (rmail-set-label string nil))
56
57;;;###autoload
58(defun rmail-read-label (prompt)
59 (let ((result
60 (completing-read (concat prompt
61 (if rmail-last-label
62 (concat " (default "
63 (symbol-name rmail-last-label)
64 "): ")
65 ": "))
66 rmail-label-obarray
67 nil
68 nil)))
69 (if (string= result "")
70 rmail-last-label
71 (setq rmail-last-label (rmail-make-label result)))))
72
73(defun rmail-set-label (label state &optional msg)
74 "Set LABEL as present or absent according to STATE in message MSG."
75 (with-current-buffer rmail-buffer
76 (rmail-maybe-set-message-counters)
77 (if (not msg) (setq msg rmail-current-message))
78 ;; Force recalculation of summary for this message.
79 (aset rmail-summary-vector (1- msg) nil)
80 (let (attr-index)
81 ;; Is this label an attribute?
82 (dotimes (i (length rmail-attr-array))
83 (if (string= (cadr (aref rmail-attr-array i)) label)
84 (setq attr-index i)))
85 (if attr-index
86 ;; If so, set it as an attribute.
87 (rmail-set-attribute attr-index state msg)
88 ;; Is this keyword already present in msg's keyword list?
89 (let* ((header (rmail-get-header rmail-keyword-header msg))
90 (regexp (concat ", " (regexp-quote (symbol-name label)) ","))
91 (present (string-match regexp (concat ", " header ","))))
92 ;; If current state is not correct,
93 (unless (eq present state)
94 ;; either add it or delete it.
95 (rmail-set-header
96 rmail-keyword-header msg
97 (if state
98 ;; Add this keyword at the end.
99 (if (and header (not (string= header "")))
100 (concat header ", " (symbol-name label))
101 (symbol-name label))
102 ;; Delete this keyword.
103 (let ((before (substring header 0
104 (max 0 (- (match-beginning 0) 2))))
105 (after (substring header
106 (min (length header)
107 (- (match-end 0) 1)))))
108 (cond ((string= before "")
109 after)
110 ((string= after "")
111 before)
112 (t (concat before ", " after)))))))))
113 (if (= msg rmail-current-message)
114 (rmail-display-labels)))))
115\f
116;; Motion on messages with keywords.
117
118;;;###autoload
119(defun rmail-previous-labeled-message (n labels)
120 "Show previous message with one of the labels LABELS.
121LABELS should be a comma-separated list of label names.
122If LABELS is empty, the last set of labels specified is used.
123With prefix argument N moves backward N messages with these labels."
124 (interactive "p\nsMove to previous msg with labels: ")
125 (rmail-next-labeled-message (- n) labels))
126
127(declare-function mail-comma-list-regexp "mail-utils" (labels))
128
129;;;###autoload
130(defun rmail-next-labeled-message (n labels)
131 "Show next message with one of the labels LABELS.
132LABELS should be a comma-separated list of label names.
133If LABELS is empty, the last set of labels specified is used.
134With prefix argument N moves forward N messages with these labels."
135 (interactive "p\nsMove to next msg with labels: ")
136 (if (string= labels "")
137 (setq labels rmail-last-multi-labels))
138 (or labels
139 (error "No labels to find have been specified previously"))
140 (set-buffer rmail-buffer)
141 (setq rmail-last-multi-labels labels)
142 (rmail-maybe-set-message-counters)
143 (let ((lastwin rmail-current-message)
144 (current rmail-current-message)
80cb4057 145 (regexp (concat " ?\\("
537ab246 146 (mail-comma-list-regexp labels)
80cb4057 147 "\\)")))
537ab246
BG
148 (while (and (> n 0) (< current rmail-total-messages))
149 (setq current (1+ current))
150 (if (string-match regexp (rmail-get-labels current))
151 (setq lastwin current n (1- n))))
152 (while (and (< n 0) (> current 1))
153 (setq current (1- current))
154 (if (string-match regexp (rmail-get-labels current))
155 (setq lastwin current n (1+ n))))
156 (if (< n 0)
157 (error "No previous message with labels %s" labels)
158 (if (> n 0)
159 (error "No following message with labels %s" labels)
160 (rmail-show-message lastwin)))))
161
162(provide 'rmailkwd)
163
537ab246
BG
164;; arch-tag: 1149979c-8e47-4333-9629-cf3dc887a6a7
165;;; rmailkwd.el ends here