comparison lisp/mail/pmaildesc.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 1b1837ac37e2
comparison
equal deleted inserted replaced
97527:059ec03cfe4e 97528:184bb2071e3f
1 ;;; pmaildesc.el --- Low level message descriptor library for Pmail.
2
3 ;; Copyright (C) 2002, 2006 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; This package provides low level functions for tracking messages in Pmail.
28
29 ;;; Code:
30
31 (require 'pmailhdr)
32
33 (defvar pmail-desc-attributes nil
34 "A private variable providing temporary access to message attributes.")
35
36 (defvar pmail-desc-delete-callback nil
37 "A function pointer called after a message has been deleted.
38 It expects one argument --- the message number.")
39
40 (defvar pmail-desc-vector nil
41 "A vector of message descriptors.
42 A message descriptor contains data formatted as follows:
43
44 (START ATTRIBUTES KEYWORDS DATE LINE-COUNT SENDER SUBJECT)
45
46 where
47
48 START is a marker at the beginning of the header
49
50 ATTRIBUTES is a string where each character encodes an
51 attribute. A hyphen (-) indicates that the attribute is not
52 set:
53
54 ANSWERED The message has been replied to (A).
55 DELETED The message has been marked for deletion (D).
56 EDITED The message has been edited (E).
57 FILED The message has been filed (F).
58 RESENT The message has been resent (R).
59 STORED The message has been saved to a file (S).
60 UNSEEN The message has not been read (-).
61
62 KEYWORDS is a list of User defined label strings.
63
64 DATE is a list of strings describing the message date:
65
66 DAY-OF-WEEK Mon, Sun, etc.
67 DAY-NUMBER 9, 13, 15, etc.
68 MONTH Feb, Jun, etc.
69 YEAR 2001, 2002, etc.
70 TIME 12:03:25, etc.
71
72 LINE-COUNT is the number of lines in the message.
73
74 SENDER is the name of the User sending the message.
75
76 SUBJECT is the subject header, cached to support fast summary line generation.
77 ")
78 (put 'pmail-desc-vector 'permanent-local t)
79
80 ;;;; Constants supporting message vector processing.
81
82 (defconst pmail-desc-default-attrs "------U"
83 "The default attributes for a new message.")
84
85 ;;; Message component indexes.
86
87 (defconst pmail-desc-beg-index 0
88 "The message descriptor element index for the start of the message text.")
89
90 (defconst pmail-desc-attrs-index 1
91 "The message descriptor element index for the attributes string.")
92
93 (defconst pmail-desc-keywords-index 2
94 "The message descriptor element index for the User defined labels.")
95
96 (defconst pmail-desc-date-index 3
97 "The message descriptor element index for the message date information.")
98
99 (defconst pmail-desc-line-count-index 4
100 "The message descriptor element index for the message line count.")
101
102 (defconst pmail-desc-sender-index 5
103 "The message descriptor element index for the message line count.")
104
105 (defconst pmail-desc-subject-index 6
106 "The message descriptor element index for the message line count.")
107
108 ;;; Attribute indexes
109
110 (defconst pmail-desc-answered-index 0
111 "The index for the `answered' attribute.")
112
113 (defconst pmail-desc-deleted-index 1
114 "The index for the `deleted' attribute.")
115
116 (defconst pmail-desc-edited-index 2
117 "The index for the `edited' attribute.")
118
119 (defconst pmail-desc-filed-index 3
120 "The index for the `filed' attribute.")
121
122 (defconst pmail-desc-resent-index 4
123 "The index for the `resent' attribute.")
124
125 (defconst pmail-desc-stored-index 5
126 "The index for the `stored' attribute.")
127
128 (defconst pmail-desc-unseen-index 6
129 "The index for the `unseen' attribute.")
130
131 (defconst pmail-desc-attr-code-index 0
132 "The index for the attibute code.")
133
134 (defconst pmail-desc-attr-keyword-index 1
135 "The index for the attribute keyword.")
136
137 (defconst pmail-desc-attr-summary-offset-index 2
138 "The index for the attribute offset in a summary buffer.")
139
140 (defconst pmail-desc-attr-alist
141 (list (cons pmail-desc-answered-index (list ?A "answered" 1))
142 (cons pmail-desc-deleted-index (list ?D "deleted" 0))
143 (cons pmail-desc-edited-index (list ?E "edited" 3))
144 (cons pmail-desc-filed-index (list ?F "filed" 2))
145 (cons pmail-desc-resent-index (list ?R "resent" nil))
146 (cons pmail-desc-stored-index (list ?S "stored" 4))
147 (cons pmail-desc-unseen-index (list ? "unseen" 0)))
148 "An alist mapping an attribute to a keycode, keyword and summary offset.")
149
150 (defconst pmail-desc-attr-index-map
151 (list (cons "answered" pmail-desc-answered-index)
152 (cons "deleted" pmail-desc-deleted-index)
153 (cons "edited" pmail-desc-edited-index)
154 (cons "filed" pmail-desc-filed-index)
155 (cons "resent" pmail-desc-resent-index)
156 (cons "stored" pmail-desc-stored-index)
157 (cons "unseen" pmail-desc-unseen-index)))
158
159 ;;; Date indexes
160
161 (defconst pmail-desc-date-day-of-week-index 0
162 "The DAY-OF-WEEK index into the list of date information.")
163
164 (defconst pmail-desc-date-day-number-index 1
165 "The DAY-NUMBER index into the list of date information.")
166
167 (defconst pmail-desc-date-month-index 2
168 "The MONTH index into the list of date information.")
169
170 (defconst pmail-desc-date-year-index 3
171 "The YEAR index into the list of date information.")
172
173 (defconst pmail-desc-date-time-index 4
174 "The TIME index into the list of date information.")
175
176 (defsubst pmail-desc-get-descriptor (n)
177 "Return a descriptor for message N.
178 N is 1 based, i.e. the first message number is 1."
179 (aref pmail-desc-vector (1- n)))
180
181 (defsubst pmail-desc-get-start (n)
182 "Return the position of the start of message N."
183 (marker-position
184 (nth pmail-desc-beg-index (pmail-desc-get-descriptor n))))
185
186 (defun pmail-desc-get-end (n)
187 "Return the position of the end of message N."
188 (if (= n (length pmail-desc-vector))
189 (save-restriction
190 (widen)
191 (point-max))
192 (pmail-desc-get-start (1+ n))))
193
194 (defun pmail-desc-add-descriptors (descriptor-list)
195 "Append DESCRIPTOR-LIST to the Pmail message descriptor vector."
196 (setq pmail-desc-vector
197 (vconcat pmail-desc-vector descriptor-list)))
198
199 (defun pmail-desc-add-keyword (keyword n)
200 "Add KEYWORD to the list of keywords for message N.
201 The current buffer must be narrowed to message N. Both
202 `pmail-desc-vector' and the message headers are updated."
203 (save-excursion
204 (save-restriction
205 (let ((keywords (pmail-desc-get-keywords n))
206 (display-state (pmail-desc-get-header-display-state n)))
207 (unless (member keyword keywords)
208 (setq keywords (cons keyword keywords))
209 (setcar (nthcdr pmail-desc-keywords-index (pmail-desc-get-descriptor n))
210 keywords)
211 (pmail-header-show-headers)
212 (pmail-header-add-header pmail-header-keyword-header
213 (mapconcat 'identity keywords ","))
214 (pmail-header-toggle-visibility display-state))))))
215
216 (defun pmail-desc-remove-keyword (keyword n)
217 "Remove KEYWORD from the list of keywords for message N.
218 The current buffer must be narrowed to message N. Both
219 `pmail-desc-vector' and the message headers are updated."
220 (save-excursion
221 (save-restriction
222 (let ((keywords (pmail-desc-get-keywords n))
223 (display-state (pmail-desc-get-header-display-state n)))
224 (when (member keyword keywords)
225 (setq keywords (delete keyword keywords))
226 (setcar (nthcdr pmail-desc-keywords-index (pmail-desc-get-descriptor n))
227 keywords)
228 (pmail-header-show-headers)
229 (pmail-header-add-header pmail-header-keyword-header
230 (mapconcat 'identity keywords ","))
231 (pmail-header-toggle-visibility display-state))))))
232
233 (defun pmail-desc-attr-p (attr-index n)
234 "Return the state of the the attribute denoted by ATTR-INDEX in
235 message N."
236 (let ((attrs (nth pmail-desc-attrs-index
237 (pmail-desc-get-descriptor n))))
238 (not (equal "-" (substring attrs attr-index (1+ attr-index))))))
239
240 (defun pmail-desc-clear-descriptors ()
241 "Clear the Pmail message vector of all messages."
242 (setq pmail-desc-vector nil))
243
244 (defun pmail-desc-deleted-p (n)
245 "Return non-nil if message N is marked for deletion."
246 (pmail-desc-attr-p pmail-desc-deleted-index n))
247 (defalias 'pmail-message-deleted-p 'pmail-desc-deleted-p)
248
249 (defun pmail-desc-delete-maybe (n)
250 "Determine if message N is marked for deletion. If so then delete it.
251 Return t if the message is deleted, nil if not."
252 (if (pmail-desc-deleted-p n)
253 (progn
254 (pmail-desc-delete n)
255 t)))
256
257 (defun pmail-desc-delete (n)
258 "Remove message N from the Pmail buffer and from the descriptor vector."
259 (save-excursion
260 (save-restriction
261 ;; Enable the buffer to be written, ignore intangibility and do
262 ;; not record these changes in the undo list.
263 (let ((inhibit-read-only t)
264 (inhibit-point-motion-hooks t)
265 (buffer-undo-list t)
266 start end)
267 (widen)
268
269 ;; Remove the message from the buffer and neutralize the
270 ;; marker pointing to the start of the message.
271 (delete-region (pmail-desc-get-start n) (pmail-desc-get-end n))
272 (move-marker (nth pmail-desc-beg-index (pmail-desc-get-descriptor n)) nil)
273
274 ;; Remove the message descriptor from the Pmail message vector
275 ;; and execute the callback indicating the message has been
276 ;; deleted.
277 (aset pmail-desc-vector (1- n) t)
278 (funcall pmail-desc-delete-callback n)))))
279
280 (defun pmail-desc-get-attr-code (attr-index n)
281 "Return the attribute code for ATTR-INDEX in message N.
282 If the attribute is not set, return nil."
283 (if (pmail-desc-attr-p attr-index n)
284 (string (nth pmail-desc-attr-code-index
285 (cdr (assoc attr-index pmail-desc-attr-alist))))))
286
287 (defun pmail-desc-get-attr-index (attr)
288 "Return the attribute index associated with attribute ATTR, a string."
289 (cdr (assoc attr pmail-desc-attr-index-map)))
290
291 (defun pmail-desc-get-attributes (n)
292 "Return the attribute vector for message N."
293 (nth pmail-desc-attrs-index (pmail-desc-get-descriptor n)))
294
295 (defsubst pmail-desc-get-count ()
296 "Return the number of messages described in the Pmail descriptor vector."
297 (length pmail-desc-vector))
298
299 (defun pmail-desc-get-date (n)
300 "Return the date list generated when the messages were read in."
301 (nth pmail-desc-date-index (pmail-desc-get-descriptor n)))
302
303 (defun pmail-desc-get-day-number (n)
304 "Return the day number (1..31) from the date associated with message N."
305 (nth pmail-desc-date-day-number-index
306 (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
307
308 (defun pmail-desc-get-day-of-week (n)
309 "Return the day of week (Sun .. Sat) from the date associated with message N."
310 (nth pmail-desc-date-day-of-week-index
311 (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
312
313 (defun pmail-desc-get-header-display-state (n)
314 "Return t if ignorable headers are being displayed, nil otherwise."
315 (save-excursion
316 (save-restriction
317 (pmail-narrow-to-header n)
318 (null (overlays-in (point-min) (point-max))))))
319
320 (defun pmail-desc-get-keyword (attr-index)
321 "Return the keyword string associated with ATTR-INDEX."
322 (nth pmail-desc-attr-keyword-index
323 (cdr (assoc attr-index pmail-desc-attr-alist))))
324
325 (defun pmail-desc-get-keyword-list (n)
326 "Return the list of user-defined labels for message N."
327 (nth pmail-desc-keywords-index (pmail-desc-get-descriptor n)))
328
329 (defun pmail-desc-get-keyword-maybe (attribute)
330 "Return the keyword associated with ATTRIBUTE if it is set, nil otherwise.
331 ATTRIBUTE is a cons cell associating an attribute index with a keyword string."
332 (let ((index (car attribute)))
333 (if (not (equal "-" (substring pmail-desc-attributes index (1+ index))))
334 (nth pmail-desc-attr-keyword-index (cdr attribute)))))
335
336 (defun pmail-desc-get-keywords (n)
337 "Return a list of keywords for message N.
338 This includes the attributes."
339 (setq pmail-desc-attributes (pmail-desc-get-attributes n))
340 (append (delq nil (mapcar
341 'pmail-desc-get-keyword-maybe
342 pmail-desc-attr-alist))
343 (pmail-desc-get-keyword-list n)))
344
345 (defun pmail-desc-get-line-count (n)
346 "Return the message body line count."
347 (nth pmail-desc-line-count-index (pmail-desc-get-descriptor n)))
348
349 (defun pmail-desc-get-month (n)
350 "Return the month (Jan .. Dec) from the date associated with message N."
351 (nth pmail-desc-date-month-index
352 (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
353
354 (defun pmail-desc-get-sender (n)
355 "Return the User registered as the mail sender."
356 (nth pmail-desc-sender-index (pmail-desc-get-descriptor n)))
357
358 (defun pmail-desc-get-subject (n)
359 "Return the cached subject header."
360 (nth pmail-desc-subject-index (pmail-desc-get-descriptor n)))
361
362 (defun pmail-desc-get-summary-offset (attr-index)
363 "Return the summary buffer offset associated with ATTR-INDEX.
364 This is the relative position where the attribute code letter is
365 displayed in the Pmail summary buffer."
366 (nth pmail-desc-attr-summary-offset-index
367 (cdr (assoc attr-index pmail-desc-attr-alist))))
368
369 (defun pmail-desc-get-time (n)
370 "Return the time (hh:mm:ss) from the date associated with message N."
371 (nth pmail-desc-date-time-index
372 (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
373
374 (defun pmail-desc-get-year (n)
375 "Return the year (1969 ... 2###) from the date associated with message N."
376 (nth pmail-desc-date-year-index
377 (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
378
379 ;; This is a strange thing to use.
380 ;; Why not write a simple loop instead?
381 (defun pmail-desc-make-index-list ()
382 "Return a list of integers from 1 to the total number of messages."
383 (let ((result (make-vector (length pmail-desc-vector) nil))
384 (index 0))
385 (while (< index (length result))
386 (aset result index (1+ index))
387 (setq index (1+ index)))
388 (append result nil)))
389
390 (defun pmail-desc-prune-deleted-messages (callback)
391 "Remove all messages marked for marked for deletion.
392 Return the number of messages removed. Invoke CALLBACK immediately
393 after a message has been deleted.."
394
395 ;; Set the callback.
396 (setq pmail-desc-delete-callback callback)
397
398 ;; Remove all messages marked for deletion from the Pmail buffer and
399 ;; their descriptors from the Pmail message vector.
400 (let ((result (length (delq t (mapcar 'pmail-desc-delete-maybe
401 (pmail-desc-make-index-list))))))
402 (setq pmail-desc-vector
403 (vconcat (delq t (append pmail-desc-vector nil))))
404 result))
405
406 (defun pmail-desc-set-attribute (attr-index state n)
407 "Set the attribute denoted by ATTR-INDEX in message N according to STATE.
408 If STATE is non-nil the attribute will be set to the single character code
409 associated with ATTR-INDEX in pmail-desc-attr-alist, otherwise the attribute is
410 set to the hyphen character (-)."
411 (let ((attributes (nth pmail-desc-attrs-index (pmail-desc-get-descriptor n)))
412 code)
413 (setq code (if state
414 (car (cdr (assoc attr-index pmail-desc-attr-alist)))
415 ?-))
416 (aset attributes attr-index code)
417 (pmail-header-add-header pmail-header-attribute-header attributes)))
418
419 (defun pmail-desc-set-start (n pos)
420 "Set the start position for message N to POS."
421 (set-marker (nth pmail-desc-beg-index (pmail-desc-get-descriptor n)) pos))
422
423 (defun pmail-desc-showing-message-p (n)
424 "Return t if the current buffer is displaying message N, nil otherwise."
425 (let ((beg (pmail-desc-get-start n))
426 (end (pmail-desc-get-end n))
427 (curpos (point)))
428 (and (>= curpos beg) (< curpos end))))
429
430 (provide 'pmaildesc)
431
432 ;;; pmaildesc.el ends here