Remove time-stamp annoyance.
[emacs.git] / lisp / gnus / gnus-registry.el
blob93ee0efce85b7df062605ccd7aced667dd89bda8
1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news
9 ;; This file is part of GNU Emacs.
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, or (at your option)
14 ;; any later version.
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.
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.
26 ;;; Commentary:
28 ;; This is the gnus-registry.el package, which works with all
29 ;; backends, not just nnmail (e.g. NNTP). The major issue is that it
30 ;; doesn't go across backends, so for instance if an article is in
31 ;; nnml:sys and you see a reference to it in nnimap splitting, the
32 ;; article will end up in nnimap:sys
34 ;; gnus-registry.el intercepts article respooling, moving, deleting,
35 ;; and copying for all backends. If it doesn't work correctly for
36 ;; you, submit a bug report and I'll be glad to fix it. It needs
37 ;; documentation in the manual (also on my to-do list).
39 ;; Put this in your startup file (~/.gnus.el for instance)
41 ;; (setq gnus-registry-max-entries 2500
42 ;; gnus-registry-use-long-group-names t)
44 ;; (gnus-registry-initialize)
46 ;; Then use this in your fancy-split:
48 ;; (: gnus-registry-split-fancy-with-parent)
50 ;; TODO:
52 ;; - get the correct group on spool actions
54 ;; - articles that are spooled to a different backend should be handled
56 ;;; Code:
58 (eval-when-compile (require 'cl))
60 (require 'gnus)
61 (require 'gnus-int)
62 (require 'gnus-sum)
63 (require 'gnus-util)
64 (require 'nnmail)
66 (defvar gnus-adaptive-word-syntax-table)
68 (defvar gnus-registry-dirty t
69 "Boolean set to t when the registry is modified")
71 (defgroup gnus-registry nil
72 "The Gnus registry."
73 :version "22.1"
74 :group 'gnus)
76 (defvar gnus-registry-hashtb (make-hash-table
77 :size 256
78 :test 'equal)
79 "*The article registry by Message ID.")
81 (defcustom gnus-registry-marks
82 '((Important
83 :char ?i
84 :image "summary_important")
85 (Work
86 :char ?w
87 :image "summary_work")
88 (Personal
89 :char ?p
90 :image "summary_personal")
91 (To-Do
92 :char ?t
93 :image "summary_todo")
94 (Later
95 :char ?l
96 :image "summary_later"))
98 "List of registry marks and their options.
100 `gnus-registry-mark-article' will offer symbols from this list
101 for completion.
103 Each entry must have a character to be useful for summary mode
104 line display and for keyboard shortcuts.
106 Each entry must have an image string to be useful for visual
107 display."
108 :group 'gnus-registry
109 :type '(repeat :tag "Registry Marks"
110 (cons :tag "Mark"
111 (symbol :tag "Name")
112 (checklist :tag "Options" :greedy t
113 (group :inline t
114 (const :format "" :value :char)
115 (character :tag "Character code"))
116 (group :inline t
117 (const :format "" :value :image)
118 (string :tag "Image"))))))
120 (defcustom gnus-registry-default-mark 'To-Do
121 "The default mark. Should be a valid key for `gnus-registry-marks'."
122 :group 'gnus-registry
123 :type 'symbol)
125 (defcustom gnus-registry-unfollowed-groups
126 '("delayed$" "drafts$" "queue$" "INBOX$")
127 "List of groups that gnus-registry-split-fancy-with-parent won't return.
128 The group names are matched, they don't have to be fully
129 qualified. This parameter tells the Registry 'never split a
130 message into a group that matches one of these, regardless of
131 references.'"
132 :group 'gnus-registry
133 :type '(repeat regexp))
135 (defcustom gnus-registry-install 'ask
136 "Whether the registry should be installed."
137 :group 'gnus-registry
138 :type '(choice (const :tag "Never Install" nil)
139 (const :tag "Always Install" t)
140 (const :tag "Ask Me" ask)))
142 (defcustom gnus-registry-clean-empty t
143 "Whether the empty registry entries should be deleted.
144 Registry entries are considered empty when they have no groups
145 and no extra data."
146 :group 'gnus-registry
147 :type 'boolean)
149 (defcustom gnus-registry-use-long-group-names nil
150 "Whether the registry should use long group names (BUGGY)."
151 :group 'gnus-registry
152 :type 'boolean)
154 (defcustom gnus-registry-track-extra nil
155 "Whether the registry should track extra data about a message.
156 The Subject and Sender (From:) headers are currently tracked this
157 way."
158 :group 'gnus-registry
159 :type
160 '(set :tag "Tracking choices"
161 (const :tag "Track by subject (Subject: header)" subject)
162 (const :tag "Track by sender (From: header)" sender)))
164 (defcustom gnus-registry-split-strategy nil
165 "Whether the registry should track extra data about a message.
166 The Subject and Sender (From:) headers are currently tracked this
167 way."
168 :group 'gnus-registry
169 :type
170 '(choice :tag "Tracking choices"
171 (const :tag "Only use single choices, discard multiple matches" nil)
172 (const :tag "Majority of matches wins" majority)
173 (const :tag "First found wins" first)))
175 (defcustom gnus-registry-entry-caching t
176 "Whether the registry should cache extra information."
177 :group 'gnus-registry
178 :type 'boolean)
180 (defcustom gnus-registry-minimum-subject-length 5
181 "The minimum length of a subject before it's considered trackable."
182 :group 'gnus-registry
183 :type 'integer)
185 (defcustom gnus-registry-trim-articles-without-groups t
186 "Whether the registry should clean out message IDs without groups."
187 :group 'gnus-registry
188 :type 'boolean)
190 (defcustom gnus-registry-extra-entries-precious '(marks)
191 "What extra entries are precious, meaning they won't get trimmed.
192 When you save the Gnus registry, it's trimmed to be no longer
193 than `gnus-registry-max-entries' (which is nil by default, so no
194 trimming happens). Any entries with extra data in this list (by
195 default, marks are included, so articles with marks are
196 considered precious) will not be trimmed."
197 :group 'gnus-registry
198 :type '(repeat symbol))
200 (defcustom gnus-registry-cache-file
201 (nnheader-concat
202 (or gnus-dribble-directory gnus-home-directory "~/")
203 ".gnus.registry.eld")
204 "File where the Gnus registry will be stored."
205 :group 'gnus-registry
206 :type 'file)
208 (defcustom gnus-registry-max-entries nil
209 "Maximum number of entries in the registry, nil for unlimited."
210 :group 'gnus-registry
211 :type '(radio (const :format "Unlimited " nil)
212 (integer :format "Maximum number: %v")))
214 (defun gnus-registry-track-subject-p ()
215 (memq 'subject gnus-registry-track-extra))
217 (defun gnus-registry-track-sender-p ()
218 (memq 'sender gnus-registry-track-extra))
220 (defun gnus-registry-cache-read ()
221 "Read the registry cache file."
222 (interactive)
223 (let ((file gnus-registry-cache-file))
224 (when (file-exists-p file)
225 (gnus-message 5 "Reading %s..." file)
226 (gnus-load file)
227 (gnus-message 5 "Reading %s...done" file))))
229 ;; FIXME: Get rid of duplicated code, cf. `gnus-save-newsrc-file' in
230 ;; `gnus-start.el'. --rsteib
231 (defun gnus-registry-cache-save ()
232 "Save the registry cache file."
233 (interactive)
234 (let ((file gnus-registry-cache-file))
235 (save-excursion
236 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
237 (make-local-variable 'version-control)
238 (setq version-control gnus-backup-startup-file)
239 (setq buffer-file-name file)
240 (setq default-directory (file-name-directory buffer-file-name))
241 (buffer-disable-undo)
242 (erase-buffer)
243 (gnus-message 5 "Saving %s..." file)
244 (if gnus-save-startup-file-via-temp-buffer
245 (let ((coding-system-for-write gnus-ding-file-coding-system)
246 (standard-output (current-buffer)))
247 (gnus-gnus-to-quick-newsrc-format
248 t "gnus registry startup file" 'gnus-registry-alist)
249 (gnus-registry-cache-whitespace file)
250 (save-buffer))
251 (let ((coding-system-for-write gnus-ding-file-coding-system)
252 (version-control gnus-backup-startup-file)
253 (startup-file file)
254 (working-dir (file-name-directory file))
255 working-file
256 (i -1))
257 ;; Generate the name of a non-existent file.
258 (while (progn (setq working-file
259 (format
260 (if (and (eq system-type 'ms-dos)
261 (not (gnus-long-file-names)))
262 "%s#%d.tm#" ; MSDOS limits files to 8+3
263 (if (memq system-type '(vax-vms axp-vms))
264 "%s$tmp$%d"
265 "%s#tmp#%d"))
266 working-dir (setq i (1+ i))))
267 (file-exists-p working-file)))
269 (unwind-protect
270 (progn
271 (gnus-with-output-to-file working-file
272 (gnus-gnus-to-quick-newsrc-format
273 t "gnus registry startup file" 'gnus-registry-alist))
275 ;; These bindings will mislead the current buffer
276 ;; into thinking that it is visiting the startup
277 ;; file.
278 (let ((buffer-backed-up nil)
279 (buffer-file-name startup-file)
280 (file-precious-flag t)
281 (setmodes (file-modes startup-file)))
282 ;; Backup the current version of the startup file.
283 (backup-buffer)
285 ;; Replace the existing startup file with the temp file.
286 (rename-file working-file startup-file t)
287 (gnus-set-file-modes startup-file setmodes)))
288 (condition-case nil
289 (delete-file working-file)
290 (file-error nil)))))
292 (gnus-kill-buffer (current-buffer))
293 (gnus-message 5 "Saving %s...done" file))))
295 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
296 ;; Save the gnus-registry file with extra line breaks.
297 (defun gnus-registry-cache-whitespace (filename)
298 (gnus-message 7 "Adding whitespace to %s" filename)
299 (save-excursion
300 (goto-char (point-min))
301 (while (re-search-forward "^(\\|(\\\"" nil t)
302 (replace-match "\n\\&" t))
303 (goto-char (point-min))
304 (while (re-search-forward " $" nil t)
305 (replace-match "" t t))))
307 (defun gnus-registry-save (&optional force)
308 (when (or gnus-registry-dirty force)
309 (let ((caching gnus-registry-entry-caching))
310 ;; turn off entry caching, so mtime doesn't get recorded
311 (setq gnus-registry-entry-caching nil)
312 ;; remove entry caches
313 (maphash
314 (lambda (key value)
315 (if (hash-table-p value)
316 (remhash key gnus-registry-hashtb)))
317 gnus-registry-hashtb)
318 ;; remove empty entries
319 (when gnus-registry-clean-empty
320 (gnus-registry-clean-empty-function))
321 ;; now trim and clean text properties from the registry appropriately
322 (setq gnus-registry-alist
323 (gnus-registry-remove-alist-text-properties
324 (gnus-registry-trim
325 (gnus-hashtable-to-alist
326 gnus-registry-hashtb))))
327 ;; really save
328 (gnus-registry-cache-save)
329 (setq gnus-registry-entry-caching caching)
330 (setq gnus-registry-dirty nil))))
332 (defun gnus-registry-clean-empty-function ()
333 "Remove all empty entries from the registry. Returns count thereof."
334 (let ((count 0))
336 (maphash
337 (lambda (key value)
338 (when (stringp key)
339 (dolist (group (gnus-registry-fetch-groups key))
340 (when (gnus-parameter-registry-ignore group)
341 (gnus-message
343 "gnus-registry: deleted ignored group %s from key %s"
344 group key)
345 (gnus-registry-delete-group key group)))
347 (unless (gnus-registry-group-count key)
348 (gnus-registry-delete-id key))
350 (unless (or
351 (gnus-registry-fetch-group key)
352 ;; TODO: look for specific extra data here!
353 ;; in this example, we look for 'label
354 (gnus-registry-fetch-extra key 'label))
355 (incf count)
356 (gnus-registry-delete-id key))
358 (unless (stringp key)
359 (gnus-message
361 "gnus-registry key %s was not a string, removing"
362 key)
363 (gnus-registry-delete-id key))))
365 gnus-registry-hashtb)
366 count))
368 (defun gnus-registry-read ()
369 (gnus-registry-cache-read)
370 (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
371 (setq gnus-registry-dirty nil))
373 (defun gnus-registry-remove-alist-text-properties (v)
374 "Remove text properties from all strings in alist."
375 (if (stringp v)
376 (gnus-string-remove-all-properties v)
377 (if (and (listp v) (listp (cdr v)))
378 (mapcar 'gnus-registry-remove-alist-text-properties v)
379 (if (and (listp v) (stringp (cdr v)))
380 (cons (gnus-registry-remove-alist-text-properties (car v))
381 (gnus-registry-remove-alist-text-properties (cdr v)))
382 v))))
384 (defun gnus-registry-trim (alist)
385 "Trim alist to size, using gnus-registry-max-entries.
386 Any entries with extra data (marks, currently) are left alone."
387 (if (null gnus-registry-max-entries)
388 alist ; just return the alist
389 ;; else, when given max-entries, trim the alist
390 (let* ((timehash (make-hash-table
391 :size 20000
392 :test 'equal))
393 (precious (make-hash-table
394 :size 20000
395 :test 'equal))
396 (trim-length (- (length alist) gnus-registry-max-entries))
397 (trim-length (if (natnump trim-length) trim-length 0))
398 precious-list junk-list)
399 (maphash
400 (lambda (key value)
401 (let ((extra (gnus-registry-fetch-extra key)))
402 (dolist (item gnus-registry-extra-entries-precious)
403 (dolist (e extra)
404 (when (equal (nth 0 e) item)
405 (puthash key t precious)
406 (return))))
407 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash)))
408 gnus-registry-hashtb)
410 (dolist (item alist)
411 (let ((key (nth 0 item)))
412 (if (gethash key precious)
413 (push item precious-list)
414 (push item junk-list))))
416 (sort
417 junk-list
418 (lambda (a b)
419 (let ((t1 (or (cdr (gethash (car a) timehash))
420 '(0 0 0)))
421 (t2 (or (cdr (gethash (car b) timehash))
422 '(0 0 0))))
423 (time-less-p t1 t2))))
425 ;; we use the return value of this setq, which is the trimmed alist
426 (setq alist (append precious-list
427 (nthcdr trim-length junk-list))))))
429 (defun gnus-registry-action (action data-header from &optional to method)
430 (let* ((id (mail-header-id data-header))
431 (subject (gnus-string-remove-all-properties
432 (gnus-registry-simplify-subject
433 (mail-header-subject data-header))))
434 (sender (gnus-string-remove-all-properties
435 (mail-header-from data-header)))
436 (from (gnus-group-guess-full-name-from-command-method from))
437 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
438 (to-name (if to to "the Bit Bucket"))
439 (old-entry (gethash id gnus-registry-hashtb)))
440 (gnus-message 7 "Registry: article %s %s from %s to %s"
442 (if method "respooling" "going")
443 from
446 ;; All except copy will need a delete
447 (gnus-registry-delete-group id from)
449 (when (equal 'copy action)
450 (gnus-registry-add-group id from subject sender)) ; undo the delete
452 (gnus-registry-add-group id to subject sender)))
454 (defun gnus-registry-spool-action (id group &optional subject sender)
455 (let ((group (gnus-group-guess-full-name-from-command-method group)))
456 (when (and (stringp id) (string-match "\r$" id))
457 (setq id (substring id 0 -1)))
458 (gnus-message 7 "Registry: article %s spooled to %s"
460 group)
461 (gnus-registry-add-group id group subject sender)))
463 ;; Function for nn{mail|imap}-split-fancy: look up all references in
464 ;; the cache and if a match is found, return that group.
465 (defun gnus-registry-split-fancy-with-parent ()
466 "Split this message into the same group as its parent. The parent
467 is obtained from the registry. This function can be used as an entry
468 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
469 this: (: gnus-registry-split-fancy-with-parent)
471 This function tracks ALL backends, unlike
472 `nnmail-split-fancy-with-parent' which tracks only nnmail
473 messages.
475 For a message to be split, it looks for the parent message in the
476 References or In-Reply-To header and then looks in the registry
477 to see which group that message was put in. This group is
478 returned, unless `gnus-registry-follow-group-p' return nil for
479 that group.
481 See the Info node `(gnus)Fancy Mail Splitting' for more details."
482 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
483 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
484 ;; now, if reply-to is valid, append it to the References
485 (refstr (if reply-to
486 (concat refstr " " reply-to)
487 refstr))
488 ;; these may not be used, but the code is cleaner having them up here
489 (sender (gnus-string-remove-all-properties
490 (message-fetch-field "from")))
491 (subject (gnus-string-remove-all-properties
492 (gnus-registry-simplify-subject
493 (message-fetch-field "subject"))))
495 (nnmail-split-fancy-with-parent-ignore-groups
496 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
497 nnmail-split-fancy-with-parent-ignore-groups
498 (list nnmail-split-fancy-with-parent-ignore-groups)))
499 (log-agent "gnus-registry-split-fancy-with-parent")
500 found found-full)
502 ;; this is a big if-else statement. it uses
503 ;; gnus-registry-post-process-groups to filter the results after
504 ;; every step.
505 (cond
506 ;; the references string must be valid and parse to valid references
507 ((and refstr (gnus-extract-references refstr))
508 (dolist (reference (nreverse (gnus-extract-references refstr)))
509 (gnus-message
511 "%s is looking for matches for reference %s from [%s]"
512 log-agent reference refstr)
513 (dolist (group (gnus-registry-fetch-groups reference))
514 (when (and group (gnus-registry-follow-group-p group))
515 (gnus-message
517 "%s traced the reference %s from [%s] to group %s"
518 log-agent reference refstr group)
519 (push group found))))
520 ;; filter the found groups and return them
521 ;; the found groups are the full groups
522 (setq found (gnus-registry-post-process-groups
523 "references" refstr found found)))
525 ;; else: there were no matches, now try the extra tracking by sender
526 ((and (gnus-registry-track-sender-p)
527 sender)
528 (maphash
529 (lambda (key value)
530 (let ((this-sender (cdr
531 (gnus-registry-fetch-extra key 'sender)))
532 matches)
533 (when (and this-sender
534 (equal sender this-sender))
535 (let ((groups (gnus-registry-fetch-groups key)))
536 (dolist (group groups)
537 (push group found-full)
538 (setq found (append (list group) (delete group found)))))
539 (push key matches)
540 (gnus-message
541 ;; raise level of messaging if gnus-registry-track-extra
542 (if gnus-registry-track-extra 7 9)
543 "%s (extra tracking) traced sender %s to groups %s (keys %s)"
544 log-agent sender found matches))))
545 gnus-registry-hashtb)
546 ;; filter the found groups and return them
547 ;; the found groups are NOT the full groups
548 (setq found (gnus-registry-post-process-groups
549 "sender" sender found found-full)))
551 ;; else: there were no matches, now try the extra tracking by subject
552 ((and (gnus-registry-track-subject-p)
553 subject
554 (< gnus-registry-minimum-subject-length (length subject)))
555 (maphash
556 (lambda (key value)
557 (let ((this-subject (cdr
558 (gnus-registry-fetch-extra key 'subject)))
559 matches)
560 (when (and this-subject
561 (equal subject this-subject))
562 (let ((groups (gnus-registry-fetch-groups key)))
563 (dolist (group groups)
564 (push group found-full)
565 (setq found (append (list group) (delete group found)))))
566 (push key matches)
567 (gnus-message
568 ;; raise level of messaging if gnus-registry-track-extra
569 (if gnus-registry-track-extra 7 9)
570 "%s (extra tracking) traced subject %s to groups %s (keys %s)"
571 log-agent subject found matches))))
572 gnus-registry-hashtb)
573 ;; filter the found groups and return them
574 ;; the found groups are NOT the full groups
575 (setq found (gnus-registry-post-process-groups
576 "subject" subject found found-full))))
577 ;; after the (cond) we extract the actual value safely
578 (car-safe found)))
580 (defun gnus-registry-post-process-groups (mode key groups groups-full)
581 "Modifies GROUPS found by MODE for KEY to determine which ones to follow.
583 MODE can be 'subject' or 'sender' for example. The KEY is the
584 value by which MODE was searched.
586 Transforms each group name to the equivalent short name.
588 Checks if the current Gnus method (from `gnus-command-method' or
589 from `gnus-newsgroup-name') is the same as the group's method.
590 This is not possible if gnus-registry-use-long-group-names is
591 false. Foreign methods are not supported so they are rejected.
593 Reduces the list to a single group, or complains if that's not
594 possible. Uses `gnus-registry-split-strategy' and GROUPS-FULL if
595 necessary."
596 (let ((log-agent "gnus-registry-post-process-group")
597 out)
599 ;; the strategy can be 'first, 'majority, or nil
600 (when (eq gnus-registry-split-strategy 'first)
601 (when groups
602 (setq groups (list (car-safe groups)))))
604 (when (eq gnus-registry-split-strategy 'majority)
605 (let ((freq (make-hash-table
606 :size 256
607 :test 'equal)))
608 (mapc (lambda(x) (puthash x (1+ (gethash x freq 0)) freq)) groups-full)
609 (setq groups (list (car-safe
610 (sort
611 groups
612 (lambda (a b)
613 (> (gethash a freq 0)
614 (gethash b freq 0)))))))))
616 (if gnus-registry-use-long-group-names
617 (dolist (group groups)
618 (let ((m1 (gnus-find-method-for-group group))
619 (m2 (or gnus-command-method
620 (gnus-find-method-for-group gnus-newsgroup-name)))
621 (short-name (gnus-group-short-name group)))
622 (if (gnus-methods-equal-p m1 m2)
623 (progn
624 ;; this is REALLY just for debugging
625 (gnus-message
627 "%s stripped group %s to %s"
628 log-agent group short-name)
629 (unless (member short-name out)
630 (push short-name out)))
631 ;; else...
632 (gnus-message
634 "%s ignored foreign group %s"
635 log-agent group))))
636 (setq out groups))
637 (when (cdr-safe out)
638 (gnus-message
640 "%s: too many extra matches (%s) for %s %s. Returning none."
641 log-agent out mode key)
642 (setq out nil))
643 out))
645 (defun gnus-registry-follow-group-p (group)
646 "Determines if a group name should be followed.
647 Consults `gnus-registry-unfollowed-groups' and
648 `nnmail-split-fancy-with-parent-ignore-groups'."
649 (not (or (gnus-registry-grep-in-list
650 group
651 gnus-registry-unfollowed-groups)
652 (gnus-registry-grep-in-list
653 group
654 nnmail-split-fancy-with-parent-ignore-groups))))
656 (defun gnus-registry-wash-for-keywords (&optional force)
657 (interactive)
658 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article))
659 word words)
660 (if (or (not (gnus-registry-fetch-extra id 'keywords))
661 force)
662 (save-excursion
663 (set-buffer gnus-article-buffer)
664 (article-goto-body)
665 (save-window-excursion
666 (save-restriction
667 (narrow-to-region (point) (point-max))
668 (with-syntax-table gnus-adaptive-word-syntax-table
669 (while (re-search-forward "\\b\\w+\\b" nil t)
670 (setq word (gnus-registry-remove-alist-text-properties
671 (downcase (buffer-substring
672 (match-beginning 0) (match-end 0)))))
673 (if (> (length word) 3)
674 (push word words))))))
675 (gnus-registry-store-extra-entry id 'keywords words)))))
677 (defun gnus-registry-find-keywords (keyword)
678 (interactive "skeyword: ")
679 (let (articles)
680 (maphash
681 (lambda (key value)
682 (when (gnus-registry-grep-in-list
683 keyword
684 (cdr (gnus-registry-fetch-extra key 'keywords)))
685 (push key articles)))
686 gnus-registry-hashtb)
687 articles))
689 (defun gnus-registry-register-message-ids ()
690 "Register the Message-ID of every article in the group"
691 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
692 (dolist (article gnus-newsgroup-articles)
693 (let ((id (gnus-registry-fetch-message-id-fast article)))
694 (unless (member gnus-newsgroup-name (gnus-registry-fetch-groups id))
695 (gnus-message 9 "Registry: Registering article %d with group %s"
696 article gnus-newsgroup-name)
697 (gnus-registry-add-group
699 gnus-newsgroup-name
700 (gnus-registry-fetch-simplified-message-subject-fast article)
701 (gnus-registry-fetch-sender-fast article)))))))
703 (defun gnus-registry-fetch-message-id-fast (article)
704 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
705 (if (and (numberp article)
706 (assoc article (gnus-data-list nil)))
707 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
708 nil))
710 (defun gnus-registry-simplify-subject (subject)
711 (if (stringp subject)
712 (gnus-simplify-subject subject)
713 nil))
715 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
716 "Fetch the Subject quickly, using the internal gnus-data-list function"
717 (if (and (numberp article)
718 (assoc article (gnus-data-list nil)))
719 (gnus-string-remove-all-properties
720 (gnus-registry-simplify-subject
721 (mail-header-subject (gnus-data-header
722 (assoc article (gnus-data-list nil))))))
723 nil))
725 (defun gnus-registry-fetch-sender-fast (article)
726 "Fetch the Sender quickly, using the internal gnus-data-list function"
727 (if (and (numberp article)
728 (assoc article (gnus-data-list nil)))
729 (gnus-string-remove-all-properties
730 (mail-header-from (gnus-data-header
731 (assoc article (gnus-data-list nil)))))
732 nil))
734 ;;; this should be redone with catch/throw
735 (defun gnus-registry-grep-in-list (word list)
736 (when word
737 (memq nil
738 (mapcar 'not
739 (mapcar
740 (lambda (x)
741 (string-match word x))
742 list)))))
744 (defun gnus-registry-do-marks (type function)
745 "For each known mark, call FUNCTION for each cell of type TYPE.
747 FUNCTION should take two parameters, a mark symbol and the cell value."
748 (dolist (mark-info gnus-registry-marks)
749 (let* ((mark (car-safe mark-info))
750 (data (cdr-safe mark-info))
751 (cell-data (plist-get data type)))
752 (when cell-data
753 (funcall function mark cell-data)))))
755 ;;; this is ugly code, but I don't know how to do it better
756 (defun gnus-registry-install-shortcuts ()
757 "Install the keyboard shortcuts and menus for the registry.
758 Uses `gnus-registry-marks' to find what shortcuts to install."
759 (let (keys-plist)
760 (gnus-registry-do-marks
761 :char
762 (lambda (mark data)
763 (let ((function-format
764 (format "gnus-registry-%%s-article-%s-mark" mark)))
766 ;;; The following generates these functions:
767 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
768 ;;; "Apply the Important mark to process-marked ARTICLES."
769 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
770 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
771 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
772 ;;; "Apply the Important mark to process-marked ARTICLES."
773 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
774 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
776 (dolist (remove '(t nil))
777 (let* ((variant-name (if remove "remove" "set"))
778 (function-name (format function-format variant-name))
779 (shortcut (format "%c" data))
780 (shortcut (if remove (upcase shortcut) shortcut)))
781 (unintern function-name)
782 (eval
783 `(defun
784 ;; function name
785 ,(intern function-name)
786 ;; parameter definition
787 (&rest articles)
788 ;; documentation
789 ,(format
790 "%s the %s mark over process-marked ARTICLES."
791 (upcase-initials variant-name)
792 mark)
793 ;; interactive definition
794 (interactive
795 (gnus-summary-work-articles current-prefix-arg))
796 ;; actual code
798 ;; if this is called and the user doesn't want the
799 ;; registry enabled, we'll ask anyhow
800 (when (eq gnus-registry-install nil)
801 (setq gnus-registry-install 'ask))
803 ;; now the user is asked if gnus-registry-install is 'ask
804 (when (gnus-registry-install-p)
805 (gnus-registry-set-article-mark-internal
806 ;; all this just to get the mark, I must be doing it wrong
807 (intern ,(symbol-name mark))
808 articles ,remove t)
809 (dolist (article articles)
810 (gnus-summary-update-article
811 article
812 (assoc article (gnus-data-list nil)))))))
813 (push (intern function-name) keys-plist)
814 (push shortcut keys-plist)
815 (gnus-message
817 "Defined mark handling function %s"
818 function-name))))))
819 (gnus-define-keys-1
820 '(gnus-registry-mark-map "M" gnus-summary-mark-map)
821 keys-plist)))
823 ;;; use like this:
824 ;;; (defalias 'gnus-user-format-function-M
825 ;;; 'gnus-registry-user-format-function-M)
826 (defun gnus-registry-user-format-function-M (headers)
827 (let* ((id (mail-header-message-id headers))
828 (marks (when id (gnus-registry-fetch-extra-marks id))))
829 (apply 'concat (mapcar (lambda(mark)
830 (let ((c
831 (plist-get
832 (cdr-safe
833 (assoc mark gnus-registry-marks))
834 :char)))
835 (if c
836 (list c)
837 nil)))
838 marks))))
840 (defun gnus-registry-read-mark ()
841 "Read a mark name from the user with completion."
842 (let ((mark (gnus-completing-read-with-default
843 (symbol-name gnus-registry-default-mark)
844 "Label"
845 (mapcar (lambda (x) ; completion list
846 (cons (symbol-name (car-safe x)) (car-safe x)))
847 gnus-registry-marks))))
848 (when (stringp mark)
849 (intern mark))))
851 (defun gnus-registry-set-article-mark (&rest articles)
852 "Apply a mark to process-marked ARTICLES."
853 (interactive (gnus-summary-work-articles current-prefix-arg))
854 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark) articles nil t))
856 (defun gnus-registry-remove-article-mark (&rest articles)
857 "Remove a mark from process-marked ARTICLES."
858 (interactive (gnus-summary-work-articles current-prefix-arg))
859 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark) articles t t))
861 (defun gnus-registry-set-article-mark-internal (mark articles &optional remove show-message)
862 "Apply a mark to a list of ARTICLES."
863 (let ((article-id-list
864 (mapcar 'gnus-registry-fetch-message-id-fast articles)))
865 (dolist (id article-id-list)
866 (let* (
867 ;; all the marks for this article without the mark of
868 ;; interest
869 (marks
870 (delq mark (gnus-registry-fetch-extra-marks id)))
871 ;; the new marks we want to use
872 (new-marks (if remove
873 marks
874 (cons mark marks))))
875 (when show-message
876 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
877 (if remove "Removing" "Adding")
878 mark id new-marks))
880 (apply 'gnus-registry-store-extra-marks ; set the extra marks
881 id ; for the message ID
882 new-marks)))))
884 (defun gnus-registry-get-article-marks (&rest articles)
885 "Get the Gnus registry marks for ARTICLES and show them if interactive.
886 Uses process/prefix conventions. For multiple articles,
887 only the last one's marks are returned."
888 (interactive (gnus-summary-work-articles 1))
889 (let (marks)
890 (dolist (article articles)
891 (let ((article-id
892 (gnus-registry-fetch-message-id-fast article)))
893 (setq marks (gnus-registry-fetch-extra-marks article-id))))
894 (when (interactive-p)
895 (gnus-message 1 "Marks are %S" marks))
896 marks))
898 ;;; if this extends to more than 'marks, it should be improved to be more generic.
899 (defun gnus-registry-fetch-extra-marks (id)
900 "Get the marks of a message, based on the message ID.
901 Returns a list of symbol marks or nil."
902 (car-safe (cdr (gnus-registry-fetch-extra id 'marks))))
904 (defun gnus-registry-has-extra-mark (id mark)
905 "Checks if a message has `mark', based on the message ID `id'."
906 (memq mark (gnus-registry-fetch-extra-marks id)))
908 (defun gnus-registry-store-extra-marks (id &rest mark-list)
909 "Set the marks of a message, based on the message ID.
910 The `mark-list' can be nil, in which case no marks are left."
911 (gnus-registry-store-extra-entry id 'marks (list mark-list)))
913 (defun gnus-registry-delete-extra-marks (id &rest mark-delete-list)
914 "Delete the message marks in `mark-delete-list', based on the message ID."
915 (let ((marks (gnus-registry-fetch-extra-marks id)))
916 (when marks
917 (dolist (mark mark-delete-list)
918 (setq marks (delq mark marks))))
919 (gnus-registry-store-extra-marks id (car marks))))
921 (defun gnus-registry-delete-all-extra-marks (id)
922 "Delete all the marks for a message ID."
923 (gnus-registry-store-extra-marks id nil))
925 (defun gnus-registry-fetch-extra (id &optional entry)
926 "Get the extra data of a message, based on the message ID.
927 Returns the first place where the trail finds a nonstring."
928 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
929 (if (and entry
930 (hash-table-p entry-cache)
931 (gethash id entry-cache))
932 (gethash id entry-cache)
933 ;; else, if there is no caching possible...
934 (let ((trail (gethash id gnus-registry-hashtb)))
935 (when (listp trail)
936 (dolist (crumb trail)
937 (unless (stringp crumb)
938 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
940 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
941 "Get the extra data of a message, or a specific entry in it.
942 Update the entry cache if needed."
943 (if (and entry id)
944 (let ((entry-cache (gethash entry gnus-registry-hashtb))
945 entree)
946 (when gnus-registry-entry-caching
947 ;; create the hash table
948 (unless (hash-table-p entry-cache)
949 (setq entry-cache (make-hash-table
950 :size 4096
951 :test 'equal))
952 (puthash entry entry-cache gnus-registry-hashtb))
954 ;; get the entree from the hash table or from the alist
955 (setq entree (gethash id entry-cache)))
957 (unless entree
958 (setq entree (assq entry alist))
959 (when gnus-registry-entry-caching
960 (puthash id entree entry-cache)))
961 entree)
962 alist))
964 (defun gnus-registry-store-extra (id extra)
965 "Store the extra data of a message, based on the message ID.
966 The message must have at least one group name."
967 (when (gnus-registry-group-count id)
968 ;; we now know the trail has at least 1 group name, so it's not empty
969 (let ((trail (gethash id gnus-registry-hashtb))
970 (old-extra (gnus-registry-fetch-extra id))
971 entry-cache)
972 (dolist (crumb trail)
973 (unless (stringp crumb)
974 (dolist (entry crumb)
975 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
976 (when entry-cache
977 (remhash id entry-cache))))
978 (puthash id (cons extra (delete old-extra trail))
979 gnus-registry-hashtb)
980 (setq gnus-registry-dirty t)))))
982 (defun gnus-registry-delete-extra-entry (id key)
983 "Delete a specific entry in the extras field of the registry entry for id."
984 (gnus-registry-store-extra-entry id key nil))
986 (defun gnus-registry-store-extra-entry (id key value)
987 "Put a specific entry in the extras field of the registry entry for id."
988 (let* ((extra (gnus-registry-fetch-extra id))
989 ;; all the entries except the one for `key'
990 (the-rest (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))
991 (alist (if value
992 (gnus-registry-remove-alist-text-properties
993 (cons (cons key value)
994 the-rest))
995 the-rest)))
996 (gnus-registry-store-extra id alist)))
998 (defun gnus-registry-fetch-group (id)
999 "Get the group of a message, based on the message ID.
1000 Returns the first place where the trail finds a group name."
1001 (when (gnus-registry-group-count id)
1002 ;; we now know the trail has at least 1 group name
1003 (let ((trail (gethash id gnus-registry-hashtb)))
1004 (dolist (crumb trail)
1005 (when (stringp crumb)
1006 (return (if gnus-registry-use-long-group-names
1007 crumb
1008 (gnus-group-short-name crumb))))))))
1010 (defun gnus-registry-fetch-groups (id)
1011 "Get the groups of a message, based on the message ID."
1012 (let ((trail (gethash id gnus-registry-hashtb))
1013 groups)
1014 (dolist (crumb trail)
1015 (when (stringp crumb)
1016 ;; push the group name into the list
1017 (setq
1018 groups
1019 (cons
1020 (if (or (not (stringp crumb)) gnus-registry-use-long-group-names)
1021 crumb
1022 (gnus-group-short-name crumb))
1023 groups))))
1024 ;; return the list of groups
1025 groups))
1027 (defun gnus-registry-group-count (id)
1028 "Get the number of groups of a message, based on the message ID."
1029 (let ((trail (gethash id gnus-registry-hashtb)))
1030 (if (and trail (listp trail))
1031 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
1032 0)))
1034 (defun gnus-registry-delete-group (id group)
1035 "Delete a group for a message, based on the message ID."
1036 (when (and group id)
1037 (let ((trail (gethash id gnus-registry-hashtb))
1038 (short-group (gnus-group-short-name group)))
1039 (puthash id (if trail
1040 (delete short-group (delete group trail))
1041 nil)
1042 gnus-registry-hashtb))
1043 ;; now, clear the entry if there are no more groups
1044 (when gnus-registry-trim-articles-without-groups
1045 (unless (gnus-registry-group-count id)
1046 (gnus-registry-delete-id id)))
1047 ;; is this ID still in the registry?
1048 (when (gethash id gnus-registry-hashtb)
1049 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
1051 (defun gnus-registry-delete-id (id)
1052 "Delete a message ID from the registry."
1053 (when (stringp id)
1054 (remhash id gnus-registry-hashtb)
1055 (maphash
1056 (lambda (key value)
1057 (when (hash-table-p value)
1058 (remhash id value)))
1059 gnus-registry-hashtb)))
1061 (defun gnus-registry-add-group (id group &optional subject sender)
1062 "Add a group for a message, based on the message ID."
1063 (when group
1064 (when (and id
1065 (not (string-match "totally-fudged-out-message-id" id)))
1066 (let ((full-group group)
1067 (group (if gnus-registry-use-long-group-names
1068 group
1069 (gnus-group-short-name group))))
1070 (gnus-registry-delete-group id group)
1072 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
1073 (gnus-registry-delete-group id full-group))
1075 (let ((trail (gethash id gnus-registry-hashtb)))
1076 (puthash id (if trail
1077 (cons group trail)
1078 (list group))
1079 gnus-registry-hashtb)
1081 (when (and (gnus-registry-track-subject-p)
1082 subject)
1083 (gnus-registry-store-extra-entry
1085 'subject
1086 (gnus-registry-simplify-subject subject)))
1087 (when (and (gnus-registry-track-sender-p)
1088 sender)
1089 (gnus-registry-store-extra-entry
1091 'sender
1092 sender))
1094 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
1096 (defun gnus-registry-clear ()
1097 "Clear the Gnus registry."
1098 (interactive)
1099 (setq gnus-registry-alist nil)
1100 (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
1101 (setq gnus-registry-dirty t))
1103 ;;;###autoload
1104 (defun gnus-registry-initialize ()
1105 "Initialize the Gnus registry."
1106 (interactive)
1107 (gnus-message 5 "Initializing the registry")
1108 (setq gnus-registry-install t) ; in case it was 'ask or nil
1109 (gnus-registry-install-hooks)
1110 (gnus-registry-install-shortcuts)
1111 (gnus-registry-read))
1113 ;;;###autoload
1114 (defun gnus-registry-install-hooks ()
1115 "Install the registry hooks."
1116 (interactive)
1117 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
1118 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
1119 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
1120 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
1122 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
1123 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
1125 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
1127 (defun gnus-registry-unload-hook ()
1128 "Uninstall the registry hooks."
1129 (interactive)
1130 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
1131 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
1132 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
1133 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
1135 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
1136 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
1138 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
1140 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
1142 (defun gnus-registry-install-p ()
1143 (interactive)
1144 (when (eq gnus-registry-install 'ask)
1145 (setq gnus-registry-install
1146 (gnus-y-or-n-p
1147 (concat "Enable the Gnus registry? "
1148 "See the variable `gnus-registry-install' "
1149 "to get rid of this query permanently. ")))
1150 (when gnus-registry-install
1151 ;; we just set gnus-registry-install to t, so initialize the registry!
1152 (gnus-registry-initialize)))
1153 ;;; we could call it here: (customize-variable 'gnus-registry-install)
1154 gnus-registry-install)
1156 (when (or (eq gnus-registry-install t)
1157 (gnus-registry-install-p))
1158 (gnus-registry-initialize))
1160 ;; TODO: a few things
1162 (provide 'gnus-registry)
1164 ;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
1165 ;;; gnus-registry.el ends here