Provide/require epa-hook, not epa-file-hook.
[emacs.git] / lisp / gnus / gnus-registry.el
blob18163fe452ffee06c41d57ec696a9b4dfb34bfbb
1 ;;; gnus-registry.el --- article registry for Gnus
3 ;;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;;; Free Software Foundation, Inc.
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news registry
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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This is the gnus-registry.el package, which works with all
27 ;; backends, not just nnmail (e.g. NNTP). The major issue is that it
28 ;; doesn't go across backends, so for instance if an article is in
29 ;; nnml:sys and you see a reference to it in nnimap splitting, the
30 ;; article will end up in nnimap:sys
32 ;; gnus-registry.el intercepts article respooling, moving, deleting,
33 ;; and copying for all backends. If it doesn't work correctly for
34 ;; you, submit a bug report and I'll be glad to fix it. It needs
35 ;; documentation in the manual (also on my to-do list).
37 ;; Put this in your startup file (~/.gnus.el for instance)
39 ;; (setq gnus-registry-max-entries 2500
40 ;; gnus-registry-use-long-group-names t)
42 ;; (gnus-registry-initialize)
44 ;; Then use this in your fancy-split:
46 ;; (: gnus-registry-split-fancy-with-parent)
48 ;; TODO:
50 ;; - get the correct group on spool actions
52 ;; - articles that are spooled to a different backend should be handled
54 ;;; Code:
56 (eval-when-compile (require 'cl))
58 (require 'gnus)
59 (require 'gnus-int)
60 (require 'gnus-sum)
61 (require 'gnus-util)
62 (require 'nnmail)
64 (defvar gnus-adaptive-word-syntax-table)
66 (defvar gnus-registry-dirty t
67 "Boolean set to t when the registry is modified")
69 (defgroup gnus-registry nil
70 "The Gnus registry."
71 :version "22.1"
72 :group 'gnus)
74 (defvar gnus-registry-hashtb (make-hash-table
75 :size 256
76 :test 'equal)
77 "*The article registry by Message ID.")
79 (defcustom gnus-registry-marks
80 '((Important
81 :char ?i
82 :image "summary_important")
83 (Work
84 :char ?w
85 :image "summary_work")
86 (Personal
87 :char ?p
88 :image "summary_personal")
89 (To-Do
90 :char ?t
91 :image "summary_todo")
92 (Later
93 :char ?l
94 :image "summary_later"))
96 "List of registry marks and their options.
98 `gnus-registry-mark-article' will offer symbols from this list
99 for completion.
101 Each entry must have a character to be useful for summary mode
102 line display and for keyboard shortcuts.
104 Each entry must have an image string to be useful for visual
105 display."
106 :group 'gnus-registry
107 :type '(repeat :tag "Registry Marks"
108 (cons :tag "Mark"
109 (symbol :tag "Name")
110 (checklist :tag "Options" :greedy t
111 (group :inline t
112 (const :format "" :value :char)
113 (character :tag "Character code"))
114 (group :inline t
115 (const :format "" :value :image)
116 (string :tag "Image"))))))
118 (defcustom gnus-registry-default-mark 'To-Do
119 "The default mark. Should be a valid key for `gnus-registry-marks'."
120 :group 'gnus-registry
121 :type 'symbol)
123 (defcustom gnus-registry-unfollowed-groups
124 '("delayed$" "drafts$" "queue$" "INBOX$")
125 "List of groups that gnus-registry-split-fancy-with-parent won't return.
126 The group names are matched, they don't have to be fully
127 qualified. This parameter tells the Registry 'never split a
128 message into a group that matches one of these, regardless of
129 references.'"
130 :group 'gnus-registry
131 :type '(repeat regexp))
133 (defcustom gnus-registry-install 'ask
134 "Whether the registry should be installed."
135 :group 'gnus-registry
136 :type '(choice (const :tag "Never Install" nil)
137 (const :tag "Always Install" t)
138 (const :tag "Ask Me" ask)))
140 (defcustom gnus-registry-clean-empty t
141 "Whether the empty registry entries should be deleted.
142 Registry entries are considered empty when they have no groups
143 and no extra data."
144 :group 'gnus-registry
145 :type 'boolean)
147 (defcustom gnus-registry-use-long-group-names nil
148 "Whether the registry should use long group names (BUGGY)."
149 :group 'gnus-registry
150 :type 'boolean)
152 (defcustom gnus-registry-track-extra nil
153 "Whether the registry should track extra data about a message.
154 The Subject and Sender (From:) headers are currently tracked this
155 way."
156 :group 'gnus-registry
157 :type
158 '(set :tag "Tracking choices"
159 (const :tag "Track by subject (Subject: header)" subject)
160 (const :tag "Track by sender (From: header)" sender)))
162 (defcustom gnus-registry-split-strategy nil
163 "Whether the registry should track extra data about a message.
164 The Subject and Sender (From:) headers are currently tracked this
165 way."
166 :group 'gnus-registry
167 :type
168 '(choice :tag "Tracking choices"
169 (const :tag "Only use single choices, discard multiple matches" nil)
170 (const :tag "Majority of matches wins" majority)
171 (const :tag "First found wins" first)))
173 (defcustom gnus-registry-entry-caching t
174 "Whether the registry should cache extra information."
175 :group 'gnus-registry
176 :type 'boolean)
178 (defcustom gnus-registry-minimum-subject-length 5
179 "The minimum length of a subject before it's considered trackable."
180 :group 'gnus-registry
181 :type 'integer)
183 (defcustom gnus-registry-trim-articles-without-groups t
184 "Whether the registry should clean out message IDs without groups."
185 :group 'gnus-registry
186 :type 'boolean)
188 (defcustom gnus-registry-extra-entries-precious '(marks)
189 "What extra entries are precious, meaning they won't get trimmed.
190 When you save the Gnus registry, it's trimmed to be no longer
191 than `gnus-registry-max-entries' (which is nil by default, so no
192 trimming happens). Any entries with extra data in this list (by
193 default, marks are included, so articles with marks are
194 considered precious) will not be trimmed."
195 :group 'gnus-registry
196 :type '(repeat symbol))
198 (defcustom gnus-registry-cache-file
199 (nnheader-concat
200 (or gnus-dribble-directory gnus-home-directory "~/")
201 ".gnus.registry.eld")
202 "File where the Gnus registry will be stored."
203 :group 'gnus-registry
204 :type 'file)
206 (defcustom gnus-registry-max-entries nil
207 "Maximum number of entries in the registry, nil for unlimited."
208 :group 'gnus-registry
209 :type '(radio (const :format "Unlimited " nil)
210 (integer :format "Maximum number: %v")))
212 (defun gnus-registry-track-subject-p ()
213 (memq 'subject gnus-registry-track-extra))
215 (defun gnus-registry-track-sender-p ()
216 (memq 'sender gnus-registry-track-extra))
218 (defun gnus-registry-cache-read ()
219 "Read the registry cache file."
220 (interactive)
221 (let ((file gnus-registry-cache-file))
222 (when (file-exists-p file)
223 (gnus-message 5 "Reading %s..." file)
224 (gnus-load file)
225 (gnus-message 5 "Reading %s...done" file))))
227 ;; FIXME: Get rid of duplicated code, cf. `gnus-save-newsrc-file' in
228 ;; `gnus-start.el'. --rsteib
229 (defun gnus-registry-cache-save ()
230 "Save the registry cache file."
231 (interactive)
232 (let ((file gnus-registry-cache-file))
233 (save-excursion
234 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
235 (make-local-variable 'version-control)
236 (setq version-control gnus-backup-startup-file)
237 (setq buffer-file-name file)
238 (setq default-directory (file-name-directory buffer-file-name))
239 (buffer-disable-undo)
240 (erase-buffer)
241 (gnus-message 5 "Saving %s..." file)
242 (if gnus-save-startup-file-via-temp-buffer
243 (let ((coding-system-for-write gnus-ding-file-coding-system)
244 (standard-output (current-buffer)))
245 (gnus-gnus-to-quick-newsrc-format
246 t "gnus registry startup file" 'gnus-registry-alist)
247 (gnus-registry-cache-whitespace file)
248 (save-buffer))
249 (let ((coding-system-for-write gnus-ding-file-coding-system)
250 (version-control gnus-backup-startup-file)
251 (startup-file file)
252 (working-dir (file-name-directory file))
253 working-file
254 (i -1))
255 ;; Generate the name of a non-existent file.
256 (while (progn (setq working-file
257 (format
258 (if (and (eq system-type 'ms-dos)
259 (not (gnus-long-file-names)))
260 "%s#%d.tm#" ; MSDOS limits files to 8+3
261 (if (memq system-type '(vax-vms axp-vms))
262 "%s$tmp$%d"
263 "%s#tmp#%d"))
264 working-dir (setq i (1+ i))))
265 (file-exists-p working-file)))
267 (unwind-protect
268 (progn
269 (gnus-with-output-to-file working-file
270 (gnus-gnus-to-quick-newsrc-format
271 t "gnus registry startup file" 'gnus-registry-alist))
273 ;; These bindings will mislead the current buffer
274 ;; into thinking that it is visiting the startup
275 ;; file.
276 (let ((buffer-backed-up nil)
277 (buffer-file-name startup-file)
278 (file-precious-flag t)
279 (setmodes (file-modes startup-file)))
280 ;; Backup the current version of the startup file.
281 (backup-buffer)
283 ;; Replace the existing startup file with the temp file.
284 (rename-file working-file startup-file t)
285 (gnus-set-file-modes startup-file setmodes)))
286 (condition-case nil
287 (delete-file working-file)
288 (file-error nil)))))
290 (gnus-kill-buffer (current-buffer))
291 (gnus-message 5 "Saving %s...done" file))))
293 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
294 ;; Save the gnus-registry file with extra line breaks.
295 (defun gnus-registry-cache-whitespace (filename)
296 (gnus-message 7 "Adding whitespace to %s" filename)
297 (save-excursion
298 (goto-char (point-min))
299 (while (re-search-forward "^(\\|(\\\"" nil t)
300 (replace-match "\n\\&" t))
301 (goto-char (point-min))
302 (while (re-search-forward " $" nil t)
303 (replace-match "" t t))))
305 (defun gnus-registry-save (&optional force)
306 (when (or gnus-registry-dirty force)
307 (let ((caching gnus-registry-entry-caching))
308 ;; turn off entry caching, so mtime doesn't get recorded
309 (setq gnus-registry-entry-caching nil)
310 ;; remove entry caches
311 (maphash
312 (lambda (key value)
313 (if (hash-table-p value)
314 (remhash key gnus-registry-hashtb)))
315 gnus-registry-hashtb)
316 ;; remove empty entries
317 (when gnus-registry-clean-empty
318 (gnus-registry-clean-empty-function))
319 ;; now trim and clean text properties from the registry appropriately
320 (setq gnus-registry-alist
321 (gnus-registry-remove-alist-text-properties
322 (gnus-registry-trim
323 (gnus-hashtable-to-alist
324 gnus-registry-hashtb))))
325 ;; really save
326 (gnus-registry-cache-save)
327 (setq gnus-registry-entry-caching caching)
328 (setq gnus-registry-dirty nil))))
330 (defun gnus-registry-clean-empty-function ()
331 "Remove all empty entries from the registry. Returns count thereof."
332 (let ((count 0))
334 (maphash
335 (lambda (key value)
336 (when (stringp key)
337 (dolist (group (gnus-registry-fetch-groups key))
338 (when (gnus-parameter-registry-ignore group)
339 (gnus-message
341 "gnus-registry: deleted ignored group %s from key %s"
342 group key)
343 (gnus-registry-delete-group key group)))
345 (unless (gnus-registry-group-count key)
346 (gnus-registry-delete-id key))
348 (unless (or
349 (gnus-registry-fetch-group key)
350 ;; TODO: look for specific extra data here!
351 ;; in this example, we look for 'label
352 (gnus-registry-fetch-extra key 'label))
353 (incf count)
354 (gnus-registry-delete-id key))
356 (unless (stringp key)
357 (gnus-message
359 "gnus-registry key %s was not a string, removing"
360 key)
361 (gnus-registry-delete-id key))))
363 gnus-registry-hashtb)
364 count))
366 (defun gnus-registry-read ()
367 (gnus-registry-cache-read)
368 (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
369 (setq gnus-registry-dirty nil))
371 (defun gnus-registry-remove-alist-text-properties (v)
372 "Remove text properties from all strings in alist."
373 (if (stringp v)
374 (gnus-string-remove-all-properties v)
375 (if (and (listp v) (listp (cdr v)))
376 (mapcar 'gnus-registry-remove-alist-text-properties v)
377 (if (and (listp v) (stringp (cdr v)))
378 (cons (gnus-registry-remove-alist-text-properties (car v))
379 (gnus-registry-remove-alist-text-properties (cdr v)))
380 v))))
382 (defun gnus-registry-trim (alist)
383 "Trim alist to size, using gnus-registry-max-entries.
384 Any entries with extra data (marks, currently) are left alone."
385 (if (null gnus-registry-max-entries)
386 alist ; just return the alist
387 ;; else, when given max-entries, trim the alist
388 (let* ((timehash (make-hash-table
389 :size 20000
390 :test 'equal))
391 (precious (make-hash-table
392 :size 20000
393 :test 'equal))
394 (trim-length (- (length alist) gnus-registry-max-entries))
395 (trim-length (if (natnump trim-length) trim-length 0))
396 precious-list junk-list)
397 (maphash
398 (lambda (key value)
399 (let ((extra (gnus-registry-fetch-extra key)))
400 (dolist (item gnus-registry-extra-entries-precious)
401 (dolist (e extra)
402 (when (equal (nth 0 e) item)
403 (puthash key t precious)
404 (return))))
405 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash)))
406 gnus-registry-hashtb)
408 (dolist (item alist)
409 (let ((key (nth 0 item)))
410 (if (gethash key precious)
411 (push item precious-list)
412 (push item junk-list))))
414 (sort
415 junk-list
416 (lambda (a b)
417 (let ((t1 (or (cdr (gethash (car a) timehash))
418 '(0 0 0)))
419 (t2 (or (cdr (gethash (car b) timehash))
420 '(0 0 0))))
421 (time-less-p t1 t2))))
423 ;; we use the return value of this setq, which is the trimmed alist
424 (setq alist (append precious-list
425 (nthcdr trim-length junk-list))))))
427 (defun gnus-registry-action (action data-header from &optional to method)
428 (let* ((id (mail-header-id data-header))
429 (subject (gnus-string-remove-all-properties
430 (gnus-registry-simplify-subject
431 (mail-header-subject data-header))))
432 (sender (gnus-string-remove-all-properties
433 (mail-header-from data-header)))
434 (from (gnus-group-guess-full-name-from-command-method from))
435 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
436 (to-name (if to to "the Bit Bucket"))
437 (old-entry (gethash id gnus-registry-hashtb)))
438 (gnus-message 7 "Registry: article %s %s from %s to %s"
440 (if method "respooling" "going")
441 from
444 ;; All except copy will need a delete
445 (gnus-registry-delete-group id from)
447 (when (equal 'copy action)
448 (gnus-registry-add-group id from subject sender)) ; undo the delete
450 (gnus-registry-add-group id to subject sender)))
452 (defun gnus-registry-spool-action (id group &optional subject sender)
453 (let ((group (gnus-group-guess-full-name-from-command-method group)))
454 (when (and (stringp id) (string-match "\r$" id))
455 (setq id (substring id 0 -1)))
456 (gnus-message 7 "Registry: article %s spooled to %s"
458 group)
459 (gnus-registry-add-group id group subject sender)))
461 ;; Function for nn{mail|imap}-split-fancy: look up all references in
462 ;; the cache and if a match is found, return that group.
463 (defun gnus-registry-split-fancy-with-parent ()
464 "Split this message into the same group as its parent. The parent
465 is obtained from the registry. This function can be used as an entry
466 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
467 this: (: gnus-registry-split-fancy-with-parent)
469 This function tracks ALL backends, unlike
470 `nnmail-split-fancy-with-parent' which tracks only nnmail
471 messages.
473 For a message to be split, it looks for the parent message in the
474 References or In-Reply-To header and then looks in the registry
475 to see which group that message was put in. This group is
476 returned, unless `gnus-registry-follow-group-p' return nil for
477 that group.
479 See the Info node `(gnus)Fancy Mail Splitting' for more details."
480 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
481 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
482 ;; now, if reply-to is valid, append it to the References
483 (refstr (if reply-to
484 (concat refstr " " reply-to)
485 refstr))
486 ;; these may not be used, but the code is cleaner having them up here
487 (sender (gnus-string-remove-all-properties
488 (message-fetch-field "from")))
489 (subject (gnus-string-remove-all-properties
490 (gnus-registry-simplify-subject
491 (message-fetch-field "subject"))))
493 (nnmail-split-fancy-with-parent-ignore-groups
494 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
495 nnmail-split-fancy-with-parent-ignore-groups
496 (list nnmail-split-fancy-with-parent-ignore-groups)))
497 (log-agent "gnus-registry-split-fancy-with-parent")
498 found found-full)
500 ;; this is a big if-else statement. it uses
501 ;; gnus-registry-post-process-groups to filter the results after
502 ;; every step.
503 (cond
504 ;; the references string must be valid and parse to valid references
505 ((and refstr (gnus-extract-references refstr))
506 (dolist (reference (nreverse (gnus-extract-references refstr)))
507 (gnus-message
509 "%s is looking for matches for reference %s from [%s]"
510 log-agent reference refstr)
511 (dolist (group (gnus-registry-fetch-groups reference))
512 (when (and group (gnus-registry-follow-group-p group))
513 (gnus-message
515 "%s traced the reference %s from [%s] to group %s"
516 log-agent reference refstr group)
517 (push group found))))
518 ;; filter the found groups and return them
519 ;; the found groups are the full groups
520 (setq found (gnus-registry-post-process-groups
521 "references" refstr found found)))
523 ;; else: there were no matches, now try the extra tracking by sender
524 ((and (gnus-registry-track-sender-p)
525 sender
526 (not (equal (gnus-extract-address-component-email sender)
527 user-mail-address)))
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 (member keyword
683 (cdr-safe (gnus-registry-fetch-extra key 'keywords)))
684 (push key articles)))
685 gnus-registry-hashtb)
686 articles))
688 (defun gnus-registry-register-message-ids ()
689 "Register the Message-ID of every article in the group"
690 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
691 (dolist (article gnus-newsgroup-articles)
692 (let ((id (gnus-registry-fetch-message-id-fast article)))
693 (unless (member gnus-newsgroup-name (gnus-registry-fetch-groups id))
694 (gnus-message 9 "Registry: Registering article %d with group %s"
695 article gnus-newsgroup-name)
696 (gnus-registry-add-group
698 gnus-newsgroup-name
699 (gnus-registry-fetch-simplified-message-subject-fast article)
700 (gnus-registry-fetch-sender-fast article)))))))
702 (defun gnus-registry-fetch-message-id-fast (article)
703 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
704 (if (and (numberp article)
705 (assoc article (gnus-data-list nil)))
706 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
707 nil))
709 (defun gnus-registry-simplify-subject (subject)
710 (if (stringp subject)
711 (gnus-simplify-subject subject)
712 nil))
714 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
715 "Fetch the Subject quickly, using the internal gnus-data-list function"
716 (if (and (numberp article)
717 (assoc article (gnus-data-list nil)))
718 (gnus-string-remove-all-properties
719 (gnus-registry-simplify-subject
720 (mail-header-subject (gnus-data-header
721 (assoc article (gnus-data-list nil))))))
722 nil))
724 (defun gnus-registry-fetch-sender-fast (article)
725 "Fetch the Sender quickly, using the internal gnus-data-list function"
726 (if (and (numberp article)
727 (assoc article (gnus-data-list nil)))
728 (gnus-string-remove-all-properties
729 (mail-header-from (gnus-data-header
730 (assoc article (gnus-data-list nil)))))
731 nil))
733 (defun gnus-registry-grep-in-list (word list)
734 "Find if a WORD matches any regular expression in the given LIST."
735 (when (and word list)
736 (catch 'found
737 (dolist (r list)
738 (when (string-match r word)
739 (throw 'found r))))))
741 (defun gnus-registry-do-marks (type function)
742 "For each known mark, call FUNCTION for each cell of type TYPE.
744 FUNCTION should take two parameters, a mark symbol and the cell value."
745 (dolist (mark-info gnus-registry-marks)
746 (let* ((mark (car-safe mark-info))
747 (data (cdr-safe mark-info))
748 (cell-data (plist-get data type)))
749 (when cell-data
750 (funcall function mark cell-data)))))
752 ;;; this is ugly code, but I don't know how to do it better
753 (defun gnus-registry-install-shortcuts ()
754 "Install the keyboard shortcuts and menus for the registry.
755 Uses `gnus-registry-marks' to find what shortcuts to install."
756 (let (keys-plist)
757 (gnus-registry-do-marks
758 :char
759 (lambda (mark data)
760 (let ((function-format
761 (format "gnus-registry-%%s-article-%s-mark" mark)))
763 ;;; The following generates these functions:
764 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
765 ;;; "Apply the Important mark to process-marked ARTICLES."
766 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
767 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
768 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
769 ;;; "Apply the Important mark to process-marked ARTICLES."
770 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
771 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
773 (dolist (remove '(t nil))
774 (let* ((variant-name (if remove "remove" "set"))
775 (function-name (format function-format variant-name))
776 (shortcut (format "%c" data))
777 (shortcut (if remove (upcase shortcut) shortcut)))
778 (unintern function-name)
779 (eval
780 `(defun
781 ;; function name
782 ,(intern function-name)
783 ;; parameter definition
784 (&rest articles)
785 ;; documentation
786 ,(format
787 "%s the %s mark over process-marked ARTICLES."
788 (upcase-initials variant-name)
789 mark)
790 ;; interactive definition
791 (interactive
792 (gnus-summary-work-articles current-prefix-arg))
793 ;; actual code
795 ;; if this is called and the user doesn't want the
796 ;; registry enabled, we'll ask anyhow
797 (when (eq gnus-registry-install nil)
798 (setq gnus-registry-install 'ask))
800 ;; now the user is asked if gnus-registry-install is 'ask
801 (when (gnus-registry-install-p)
802 (gnus-registry-set-article-mark-internal
803 ;; all this just to get the mark, I must be doing it wrong
804 (intern ,(symbol-name mark))
805 articles ,remove t)
806 (dolist (article articles)
807 (gnus-summary-update-article
808 article
809 (assoc article (gnus-data-list nil)))))))
810 (push (intern function-name) keys-plist)
811 (push shortcut keys-plist)
812 (gnus-message
814 "Defined mark handling function %s"
815 function-name))))))
816 (gnus-define-keys-1
817 '(gnus-registry-mark-map "M" gnus-summary-mark-map)
818 keys-plist)))
820 ;;; use like this:
821 ;;; (defalias 'gnus-user-format-function-M
822 ;;; 'gnus-registry-user-format-function-M)
823 (defun gnus-registry-user-format-function-M (headers)
824 (let* ((id (mail-header-message-id headers))
825 (marks (when id (gnus-registry-fetch-extra-marks id))))
826 (apply 'concat (mapcar (lambda(mark)
827 (let ((c
828 (plist-get
829 (cdr-safe
830 (assoc mark gnus-registry-marks))
831 :char)))
832 (if c
833 (list c)
834 nil)))
835 marks))))
837 (defun gnus-registry-read-mark ()
838 "Read a mark name from the user with completion."
839 (let ((mark (gnus-completing-read-with-default
840 (symbol-name gnus-registry-default-mark)
841 "Label"
842 (mapcar (lambda (x) ; completion list
843 (cons (symbol-name (car-safe x)) (car-safe x)))
844 gnus-registry-marks))))
845 (when (stringp mark)
846 (intern mark))))
848 (defun gnus-registry-set-article-mark (&rest articles)
849 "Apply a mark to process-marked ARTICLES."
850 (interactive (gnus-summary-work-articles current-prefix-arg))
851 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark) articles nil t))
853 (defun gnus-registry-remove-article-mark (&rest articles)
854 "Remove a mark from process-marked ARTICLES."
855 (interactive (gnus-summary-work-articles current-prefix-arg))
856 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark) articles t t))
858 (defun gnus-registry-set-article-mark-internal (mark articles &optional remove show-message)
859 "Apply a mark to a list of ARTICLES."
860 (let ((article-id-list
861 (mapcar 'gnus-registry-fetch-message-id-fast articles)))
862 (dolist (id article-id-list)
863 (let* (
864 ;; all the marks for this article without the mark of
865 ;; interest
866 (marks
867 (delq mark (gnus-registry-fetch-extra-marks id)))
868 ;; the new marks we want to use
869 (new-marks (if remove
870 marks
871 (cons mark marks))))
872 (when show-message
873 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
874 (if remove "Removing" "Adding")
875 mark id new-marks))
877 (apply 'gnus-registry-store-extra-marks ; set the extra marks
878 id ; for the message ID
879 new-marks)))))
881 (defun gnus-registry-get-article-marks (&rest articles)
882 "Get the Gnus registry marks for ARTICLES and show them if interactive.
883 Uses process/prefix conventions. For multiple articles,
884 only the last one's marks are returned."
885 (interactive (gnus-summary-work-articles 1))
886 (let (marks)
887 (dolist (article articles)
888 (let ((article-id
889 (gnus-registry-fetch-message-id-fast article)))
890 (setq marks (gnus-registry-fetch-extra-marks article-id))))
891 (when (interactive-p)
892 (gnus-message 1 "Marks are %S" marks))
893 marks))
895 ;;; if this extends to more than 'marks, it should be improved to be more generic.
896 (defun gnus-registry-fetch-extra-marks (id)
897 "Get the marks of a message, based on the message ID.
898 Returns a list of symbol marks or nil."
899 (car-safe (cdr (gnus-registry-fetch-extra id 'marks))))
901 (defun gnus-registry-has-extra-mark (id mark)
902 "Checks if a message has `mark', based on the message ID `id'."
903 (memq mark (gnus-registry-fetch-extra-marks id)))
905 (defun gnus-registry-store-extra-marks (id &rest mark-list)
906 "Set the marks of a message, based on the message ID.
907 The `mark-list' can be nil, in which case no marks are left."
908 (gnus-registry-store-extra-entry id 'marks (list mark-list)))
910 (defun gnus-registry-delete-extra-marks (id &rest mark-delete-list)
911 "Delete the message marks in `mark-delete-list', based on the message ID."
912 (let ((marks (gnus-registry-fetch-extra-marks id)))
913 (when marks
914 (dolist (mark mark-delete-list)
915 (setq marks (delq mark marks))))
916 (gnus-registry-store-extra-marks id (car marks))))
918 (defun gnus-registry-delete-all-extra-marks (id)
919 "Delete all the marks for a message ID."
920 (gnus-registry-store-extra-marks id nil))
922 (defun gnus-registry-fetch-extra (id &optional entry)
923 "Get the extra data of a message, based on the message ID.
924 Returns the first place where the trail finds a nonstring."
925 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
926 (if (and entry
927 (hash-table-p entry-cache)
928 (gethash id entry-cache))
929 (gethash id entry-cache)
930 ;; else, if there is no caching possible...
931 (let ((trail (gethash id gnus-registry-hashtb)))
932 (when (listp trail)
933 (dolist (crumb trail)
934 (unless (stringp crumb)
935 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
937 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
938 "Get the extra data of a message, or a specific entry in it.
939 Update the entry cache if needed."
940 (if (and entry id)
941 (let ((entry-cache (gethash entry gnus-registry-hashtb))
942 entree)
943 (when gnus-registry-entry-caching
944 ;; create the hash table
945 (unless (hash-table-p entry-cache)
946 (setq entry-cache (make-hash-table
947 :size 4096
948 :test 'equal))
949 (puthash entry entry-cache gnus-registry-hashtb))
951 ;; get the entree from the hash table or from the alist
952 (setq entree (gethash id entry-cache)))
954 (unless entree
955 (setq entree (assq entry alist))
956 (when gnus-registry-entry-caching
957 (puthash id entree entry-cache)))
958 entree)
959 alist))
961 (defun gnus-registry-store-extra (id extra)
962 "Store the extra data of a message, based on the message ID.
963 The message must have at least one group name."
964 (when (gnus-registry-group-count id)
965 ;; we now know the trail has at least 1 group name, so it's not empty
966 (let ((trail (gethash id gnus-registry-hashtb))
967 (old-extra (gnus-registry-fetch-extra id))
968 entry-cache)
969 (dolist (crumb trail)
970 (unless (stringp crumb)
971 (dolist (entry crumb)
972 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
973 (when entry-cache
974 (remhash id entry-cache))))
975 (puthash id (cons extra (delete old-extra trail))
976 gnus-registry-hashtb)
977 (setq gnus-registry-dirty t)))))
979 (defun gnus-registry-delete-extra-entry (id key)
980 "Delete a specific entry in the extras field of the registry entry for id."
981 (gnus-registry-store-extra-entry id key nil))
983 (defun gnus-registry-store-extra-entry (id key value)
984 "Put a specific entry in the extras field of the registry entry for id."
985 (let* ((extra (gnus-registry-fetch-extra id))
986 ;; all the entries except the one for `key'
987 (the-rest (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))
988 (alist (if value
989 (gnus-registry-remove-alist-text-properties
990 (cons (cons key value)
991 the-rest))
992 the-rest)))
993 (gnus-registry-store-extra id alist)))
995 (defun gnus-registry-fetch-group (id)
996 "Get the group of a message, based on the message ID.
997 Returns the first place where the trail finds a group name."
998 (when (gnus-registry-group-count id)
999 ;; we now know the trail has at least 1 group name
1000 (let ((trail (gethash id gnus-registry-hashtb)))
1001 (dolist (crumb trail)
1002 (when (stringp crumb)
1003 (return (if gnus-registry-use-long-group-names
1004 crumb
1005 (gnus-group-short-name crumb))))))))
1007 (defun gnus-registry-fetch-groups (id)
1008 "Get the groups of a message, based on the message ID."
1009 (let ((trail (gethash id gnus-registry-hashtb))
1010 groups)
1011 (dolist (crumb trail)
1012 (when (stringp crumb)
1013 ;; push the group name into the list
1014 (setq
1015 groups
1016 (cons
1017 (if (or (not (stringp crumb)) gnus-registry-use-long-group-names)
1018 crumb
1019 (gnus-group-short-name crumb))
1020 groups))))
1021 ;; return the list of groups
1022 groups))
1024 (defun gnus-registry-group-count (id)
1025 "Get the number of groups of a message, based on the message ID."
1026 (let ((trail (gethash id gnus-registry-hashtb)))
1027 (if (and trail (listp trail))
1028 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
1029 0)))
1031 (defun gnus-registry-delete-group (id group)
1032 "Delete a group for a message, based on the message ID."
1033 (when (and group id)
1034 (let ((trail (gethash id gnus-registry-hashtb))
1035 (short-group (gnus-group-short-name group)))
1036 (puthash id (if trail
1037 (delete short-group (delete group trail))
1038 nil)
1039 gnus-registry-hashtb))
1040 ;; now, clear the entry if there are no more groups
1041 (when gnus-registry-trim-articles-without-groups
1042 (unless (gnus-registry-group-count id)
1043 (gnus-registry-delete-id id)))
1044 ;; is this ID still in the registry?
1045 (when (gethash id gnus-registry-hashtb)
1046 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
1048 (defun gnus-registry-delete-id (id)
1049 "Delete a message ID from the registry."
1050 (when (stringp id)
1051 (remhash id gnus-registry-hashtb)
1052 (maphash
1053 (lambda (key value)
1054 (when (hash-table-p value)
1055 (remhash id value)))
1056 gnus-registry-hashtb)))
1058 (defun gnus-registry-add-group (id group &optional subject sender)
1059 "Add a group for a message, based on the message ID."
1060 (when group
1061 (when (and id
1062 (not (string-match "totally-fudged-out-message-id" id)))
1063 (let ((full-group group)
1064 (group (if gnus-registry-use-long-group-names
1065 group
1066 (gnus-group-short-name group))))
1067 (gnus-registry-delete-group id group)
1069 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
1070 (gnus-registry-delete-group id full-group))
1072 (let ((trail (gethash id gnus-registry-hashtb)))
1073 (puthash id (if trail
1074 (cons group trail)
1075 (list group))
1076 gnus-registry-hashtb)
1078 (when (and (gnus-registry-track-subject-p)
1079 subject)
1080 (gnus-registry-store-extra-entry
1082 'subject
1083 (gnus-registry-simplify-subject subject)))
1084 (when (and (gnus-registry-track-sender-p)
1085 sender)
1086 (gnus-registry-store-extra-entry
1088 'sender
1089 sender))
1091 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
1093 (defun gnus-registry-clear ()
1094 "Clear the Gnus registry."
1095 (interactive)
1096 (setq gnus-registry-alist nil)
1097 (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
1098 (setq gnus-registry-dirty t))
1100 ;;;###autoload
1101 (defun gnus-registry-initialize ()
1102 "Initialize the Gnus registry."
1103 (interactive)
1104 (gnus-message 5 "Initializing the registry")
1105 (setq gnus-registry-install t) ; in case it was 'ask or nil
1106 (gnus-registry-install-hooks)
1107 (gnus-registry-install-shortcuts)
1108 (gnus-registry-read))
1110 ;;;###autoload
1111 (defun gnus-registry-install-hooks ()
1112 "Install the registry hooks."
1113 (interactive)
1114 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
1115 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
1116 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
1117 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
1119 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
1120 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
1122 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
1124 (defun gnus-registry-unload-hook ()
1125 "Uninstall the registry hooks."
1126 (interactive)
1127 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
1128 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
1129 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
1130 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
1132 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
1133 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
1135 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
1137 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
1139 (defun gnus-registry-install-p ()
1140 (interactive)
1141 (when (eq gnus-registry-install 'ask)
1142 (setq gnus-registry-install
1143 (gnus-y-or-n-p
1144 (concat "Enable the Gnus registry? "
1145 "See the variable `gnus-registry-install' "
1146 "to get rid of this query permanently. ")))
1147 (when gnus-registry-install
1148 ;; we just set gnus-registry-install to t, so initialize the registry!
1149 (gnus-registry-initialize)))
1150 ;;; we could call it here: (customize-variable 'gnus-registry-install)
1151 gnus-registry-install)
1153 (when (or (eq gnus-registry-install t)
1154 (gnus-registry-install-p))
1155 (gnus-registry-initialize))
1157 ;; TODO: a few things
1159 (provide 'gnus-registry)
1161 ;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
1162 ;;; gnus-registry.el ends here