comparison lisp/mail/pmailkwd.el @ 97528:184bb2071e3f

mail/: Add new (temporary) libaries for which to test Rmail/mbox such that Rmail/babyl is not affected. This creates a facility/feature called "pmail" (analagous to "rmail") that can be used independently from Rmail for testing purposes. The plan is to replace the "rmail" files eventually and remove "pmail" entirely at that point. In the interim, interested developers can use either Rmail or Pmail or both (which is not recommended for the casual User or the faint of heart).
author Paul Reilly <pmr@pajato.com>
date Mon, 18 Aug 2008 04:51:28 +0000
parents
children e54462e6c673
comparison
equal deleted inserted replaced
97527:059ec03cfe4e 97528:184bb2071e3f
1 ;;; pmailkwd.el --- part of the "PMAIL" mail reader for Emacs
2
3 ;; Copyright (C) 1985, 1988, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This library manages keywords (labels). Labels are stored in the
29 ;; variable `pmail-keywords'.
30
31 ;;; Code:
32
33 (defvar pmail-buffer)
34 (defvar pmail-current-message)
35 (defvar pmail-last-label)
36 (defvar pmail-last-multi-labels)
37 (defvar pmail-summary-vector)
38 (defvar pmail-total-messages)
39
40 ;; Global to all PMAIL buffers. It exists primarily for the sake of
41 ;; completion. It is better to use strings with the label functions
42 ;; and let them worry about making the label.
43
44 (provide 'pmailkwd)
45
46 (eval-when-compile
47 (require 'mail-utils))
48
49 ;; Named list of symbols representing valid message attributes in PMAIL.
50
51 (defconst pmail-attributes
52 '(deleted answered filed forwarded unseen edited resent)
53 "Keywords with defined semantics used to label messages.
54 These have a well-defined meaning to the PMAIL system.")
55
56 (defconst pmail-deleted-label 'deleted)
57
58 ;; Named list of symbols representing valid message keywords in PMAIL.
59
60 (defvar pmail-keywords nil
61 "Keywords used to label messages.
62 These are all user-defined, unlike `pmail-attributes'.")
63
64 ;;;; Low-level functions.
65
66 (defun pmail-attribute-p (s)
67 "Non-nil if S is a known attribute.
68 See `pmail-attributes'."
69 (let ((symbol (pmail-make-label s)))
70 (memq symbol pmail-attributes)))
71
72 (defun pmail-keyword-p (s)
73 "Non-nil if S is a known keyword for this Pmail file.
74 See `pmail-keywords'."
75 (let ((symbol (pmail-make-label s)))
76 (memq symbol pmail-keywords)))
77
78 (defun pmail-make-label (s &optional forcep)
79 (cond ((symbolp s) s)
80 (forcep (intern (downcase s)))
81 (t (intern-soft (downcase s)))))
82
83 (defun pmail-quote-label-name (label)
84 (regexp-quote (symbol-name (pmail-make-label label t))))
85
86 ;;;###autoload
87 (defun pmail-register-keywords (words)
88 "Add the strings in WORDS to `pmail-keywords'."
89 (dolist (word words)
90 (pmail-register-keyword word)))
91
92 (defun pmail-register-keyword (word)
93 "Append the string WORD to `pmail-keywords',
94 unless it already is a keyword or an attribute."
95 (let ((keyword (pmail-make-label word t)))
96 (unless (or (pmail-attribute-p keyword)
97 (pmail-keyword-p keyword))
98 (setq pmail-keywords (cons keyword pmail-keywords)))))
99
100 ;;;; Adding and removing message keywords.
101
102 ;;;###autoload
103 (defun pmail-add-label (string)
104 "Add LABEL to labels associated with current PMAIL message."
105 (interactive (list (pmail-read-label "Add label")))
106 (pmail-set-label (pmail-make-label string) t)
107 (pmail-display-labels))
108
109 ;;;###autoload
110 (defun pmail-kill-label (string)
111 "Remove LABEL from labels associated with current PMAIL message."
112 (interactive (list (pmail-read-label "Remove label" t)))
113 (pmail-set-label (pmail-make-label string) nil))
114
115 ;;;###autoload
116 (defun pmail-read-label (prompt &optional existing)
117 "Ask for a label using PROMPT.
118 If EXISTING is non-nil, ask for one of the labels of the current
119 message."
120 (when (= pmail-total-messages 0)
121 (error "No messages in this file"))
122 (with-current-buffer pmail-buffer
123 (let ((result (if existing
124 (let* ((keywords (pmail-desc-get-keywords
125 pmail-current-message))
126 (last (symbol-name pmail-last-label))
127 (default (if (member last keywords)
128 last
129 (car keywords))))
130 (unless keywords
131 (error "No labels for the current message"))
132 (completing-read
133 (concat prompt " (default " default "): ")
134 keywords nil t nil nil default))
135 (let ((default (symbol-name pmail-last-label)))
136 (completing-read
137 (concat prompt (if pmail-last-label
138 (concat " (default " default "): ")
139 ": "))
140 (mapcar 'list pmail-keywords)
141 nil nil nil nil default)))))
142 (setq pmail-last-label (pmail-make-label result t))
143 ;; return the string, not the symbol
144 result)))
145
146 (declare-function pmail-maybe-set-message-counters "pmail" ())
147 (declare-function pmail-display-labels "pmail" ())
148 (declare-function pmail-msgbeg "pmail" (n))
149 (declare-function pmail-set-message-deleted-p "pmail" (n state))
150 (declare-function pmail-message-labels-p "pmail" (msg labels))
151 (declare-function pmail-show-message "pmail" (&optional n no-summary))
152 (declare-function mail-comma-list-regexp "mail-utils" (labels))
153 (declare-function mail-parse-comma-list "mail-utils.el" ())
154
155 (defun pmail-set-label (l state &optional n)
156 "Add or remove label L in message N.
157 The label L is added when STATE is non-nil, otherwise it is
158 removed. If N is nil then use the current Pmail message. The
159 current buffer, possibly narrowed, displays a message."
160 (if (= pmail-total-messages 0)
161 (error "No messages in this file"))
162 (with-current-buffer pmail-buffer
163 (if (not n) (setq n pmail-current-message))
164 (save-restriction
165 (widen)
166 (narrow-to-region (pmail-desc-get-start n) (pmail-desc-get-end n))
167 ;; FIXME: we should move all string-using functions to symbols!
168 (let ((str (symbol-name l)))
169 (if (pmail-attribute-p l)
170 (pmail-set-attribute str state n)
171 ;; Make sure the keyword is registered.
172 (pmail-register-keyword l)
173 (if state
174 (pmail-desc-add-keyword str n)
175 (pmail-desc-remove-keyword str n))))))
176 (pmail-display-labels)
177 ;; Deal with the summary buffer.
178 (when pmail-summary-buffer
179 (pmail-summary-update n)))
180
181 ;; Motion on messages with keywords.
182
183 ;;;###autoload
184 (defun pmail-previous-labeled-message (n labels)
185 "Show previous message with one of the labels LABELS.
186 LABELS should be a comma-separated list of label names.
187 If LABELS is empty, the last set of labels specified is used.
188 With prefix argument N moves backward N messages with these labels."
189 (interactive "p\nsMove to previous msg with labels: ")
190 (pmail-next-labeled-message (- n) labels))
191
192 ;;;###autoload
193 (defun pmail-next-labeled-message (n labels)
194 "Show next message with one of the labels LABELS.
195 LABELS should be a comma-separated list of label names.
196 If LABELS is empty, the last set of labels specified is used.
197 With prefix argument N moves forward N messages with these labels."
198 (interactive "p\nsMove to next msg with labels: ")
199 (when (string= labels "")
200 (setq labels pmail-last-multi-labels))
201 (unless labels
202 (error "No labels to find have been specified previously"))
203 (with-current-buffer pmail-buffer
204 (setq pmail-last-multi-labels labels)
205 (let ((lastwin pmail-current-message)
206 (current pmail-current-message)
207 (regexp (concat ", ?\\("
208 (mail-comma-list-regexp labels)
209 "\\),")))
210 (save-restriction
211 (widen)
212 (while (and (> n 0) (< current pmail-total-messages))
213 (setq current (1+ current))
214 (when (pmail-message-labels-p current regexp)
215 (setq lastwin current n (1- n))))
216 (while (and (< n 0) (> current 1))
217 (setq current (1- current))
218 (when (pmail-message-labels-p current regexp)
219 (setq lastwin current n (1+ n)))))
220 (pmail-show-message lastwin)
221 (when (< n 0)
222 (message "No previous message with labels %s" labels))
223 (when (> n 0)
224 (message "No following message with labels %s" labels)))))
225
226 ;; arch-tag: b26b3392-99ca-4e1d-933a-dab59b04e9a8
227 ;;; pmailkwd.el ends here