1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: news registry
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This is the gnus-registry.el package, which works with all
26 ;; Gnus backends, not just nnmail. The major issue is that it
27 ;; doesn't go across backends, so for instance if an article is in
28 ;; nnml:sys and you see a reference to it in nnimap splitting, the
29 ;; article will end up in nnimap:sys
31 ;; gnus-registry.el intercepts article respooling, moving, deleting,
32 ;; and copying for all backends. If it doesn't work correctly for
33 ;; you, submit a bug report and I'll be glad to fix it. It needs
34 ;; documentation in the manual (also on my to-do list).
36 ;; Put this in your startup file (~/.gnus.el for instance) or use Customize:
38 ;; (setq gnus-registry-max-entries 2500
39 ;; gnus-registry-track-extra '(sender subject))
41 ;; (gnus-registry-initialize)
43 ;; Then use this in your fancy-split:
45 ;; (: gnus-registry-split-fancy-with-parent)
47 ;; You should also consider using the nnregistry backend to look up
48 ;; articles. See the Gnus manual for more information.
52 ;; - get the correct group on spool actions
54 ;; - articles that are spooled to a different backend should be moved
59 (eval-when-compile (require 'cl))
62 (when (null (ignore-errors (require 'ert)))
63 (defmacro* ert-deftest (name () &body docstring-keys-and-body))))
76 (defvar gnus-adaptive-word-syntax-table)
78 (defvar gnus-registry-dirty t
79 "Boolean set to t when the registry is modified")
81 (defgroup gnus-registry nil
86 (defvar gnus-registry-marks
89 :image "summary_important")
92 :image "summary_work")
95 :image "summary_personal")
98 :image "summary_todo")
101 :image "summary_later"))
103 "List of registry marks and their options.
105 `gnus-registry-mark-article' will offer symbols from this list
108 Each entry must have a character to be useful for summary mode
109 line display and for keyboard shortcuts.
111 Each entry must have an image string to be useful for visual
114 (defcustom gnus-registry-default-mark 'To-Do
115 "The default mark. Should be a valid key for `gnus-registry-marks'."
116 :group 'gnus-registry
119 (defcustom gnus-registry-unfollowed-addresses
120 (list (regexp-quote user-mail-address))
121 "List of addresses that gnus-registry-split-fancy-with-parent won't trace.
122 The addresses are matched, they don't have to be fully qualified."
123 :group 'gnus-registry
124 :type '(repeat regexp))
126 (defcustom gnus-registry-unfollowed-groups
127 '("delayed$" "drafts$" "queue$" "INBOX$" "^nnmairix:" "archive")
128 "List of groups that gnus-registry-split-fancy-with-parent won't return.
129 The group names are matched, they don't have to be fully
130 qualified. This parameter tells the Gnus registry 'never split a
131 message into a group that matches one of these, regardless of
134 nnmairix groups are specifically excluded because they are ephemeral."
135 :group 'gnus-registry
136 :type '(repeat regexp))
138 (defcustom gnus-registry-install 'ask
139 "Whether the registry should be installed."
140 :group 'gnus-registry
141 :type '(choice (const :tag "Never Install" nil)
142 (const :tag "Always Install" t)
143 (const :tag "Ask Me" ask)))
145 (defvar gnus-summary-misc-menu) ;; Avoid byte compiler warning.
147 (defvar gnus-registry-misc-menus nil) ; ugly way to keep the menus
149 (make-obsolete-variable 'gnus-registry-clean-empty nil "23.4")
150 (make-obsolete-variable 'gnus-registry-use-long-group-names nil "23.4")
151 (make-obsolete-variable 'gnus-registry-max-track-groups nil "23.4")
152 (make-obsolete-variable 'gnus-registry-entry-caching nil "23.4")
153 (make-obsolete-variable 'gnus-registry-trim-articles-without-groups nil "23.4")
155 (defcustom gnus-registry-track-extra '(subject sender)
156 "Whether the registry should track extra data about a message.
157 The Subject and Sender (From:) headers are tracked this way by
159 :group 'gnus-registry
161 '(set :tag "Tracking choices"
162 (const :tag "Track by subject (Subject: header)" subject)
163 (const :tag "Track by sender (From: header)" sender)))
165 (defcustom gnus-registry-split-strategy nil
166 "The splitting strategy applied to the keys in `gnus-registry-track-extra'.
168 Given a set of unique found groups G and counts for each element
169 of G, and a key K (typically 'sender or 'subject):
171 When nil, if G has only one element, use it. Otherwise give up.
172 This is the fastest but also least useful strategy.
174 When 'majority, use the majority by count. So if there is a
175 group with the most articles counted by K, use that. Ties are
176 resolved in no particular order, simply the first one found wins.
177 This is the slowest strategy but also the most accurate one.
179 When 'first, the first element of G wins. This is fast and
180 should be OK if your senders and subjects don't \"bleed\" across
182 :group 'gnus-registry
184 '(choice :tag "Splitting strategy"
185 (const :tag "Only use single choices, discard multiple matches" nil)
186 (const :tag "Majority of matches wins" majority)
187 (const :tag "First found wins" first)))
189 (defcustom gnus-registry-minimum-subject-length 5
190 "The minimum length of a subject before it's considered trackable."
191 :group 'gnus-registry
194 (defcustom gnus-registry-extra-entries-precious '(mark)
195 "What extra keys are precious, meaning entries with them won't get pruned.
196 By default, 'mark is included, so articles with marks are
199 Before you save the Gnus registry, it's pruned. Any entries with
200 keys in this list will not be pruned. All other entries go to
202 :group 'gnus-registry
203 :type '(repeat symbol))
205 (defcustom gnus-registry-cache-file
207 (or gnus-dribble-directory gnus-home-directory "~/")
208 ".gnus.registry.eioio")
209 "File where the Gnus registry will be stored."
210 :group 'gnus-registry
213 (defcustom gnus-registry-max-entries nil
214 "Maximum number of entries in the registry, nil for unlimited."
215 :group 'gnus-registry
216 :type '(radio (const :format "Unlimited " nil)
217 (integer :format "Maximum number: %v")))
219 (defcustom gnus-registry-max-pruned-entries nil
220 "Maximum number of pruned entries in the registry, nil for unlimited."
221 :group 'gnus-registry
222 :type '(radio (const :format "Unlimited " nil)
223 (integer :format "Maximum number: %v")))
225 (defun gnus-registry-fixup-registry (db)
228 (append gnus-registry-extra-entries-precious
231 (or gnus-registry-max-entries
232 most-positive-fixnum))
234 (or gnus-registry-max-pruned-entries
235 most-positive-fixnum))
237 (append gnus-registry-track-extra
238 '(mark group keyword))))
241 (defun gnus-registry-make-db (&optional file)
242 (interactive "fGnus registry persistence file: \n")
243 (gnus-registry-fixup-registry
246 :file (or file gnus-registry-cache-file)
247 ;; these parameters are set in `gnus-registry-fixup-registry'
248 :max-hard most-positive-fixnum
249 :max-soft most-positive-fixnum
253 (defvar gnus-registry-db (gnus-registry-make-db)
254 "*The article registry by Message ID. See `registry-db'")
256 ;; top-level registry data management
257 (defun gnus-registry-remake-db (&optional forsure)
258 "Remake the registry database after customization.
259 This is not required after changing `gnus-registry-cache-file'."
260 (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
262 (gnus-message 4 "Remaking the Gnus registry")
263 (setq gnus-registry-db (gnus-registry-make-db))))
265 (defun gnus-registry-read ()
266 "Read the registry cache file."
268 (let ((file gnus-registry-cache-file))
271 (gnus-message 5 "Reading Gnus registry from %s..." file)
272 (setq gnus-registry-db (gnus-registry-fixup-registry
273 (eieio-persistent-read file)))
274 (gnus-message 5 "Reading Gnus registry from %s...done" file))
278 "The Gnus registry could not be loaded from %s, creating a new one"
280 (gnus-registry-remake-db t)))))
282 (defun gnus-registry-save (&optional file db)
283 "Save the registry cache file."
285 (let ((file (or file gnus-registry-cache-file))
286 (db (or db gnus-registry-db)))
287 (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
288 (registry-size db) file)
290 ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
291 (eieio-persistent-save db file)
292 (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
293 (registry-size db) file)))
295 ;; article move/copy/spool/delete actions
296 (defun gnus-registry-action (action data-header from &optional to method)
297 (let* ((id (mail-header-id data-header))
298 (subject (mail-header-subject data-header))
299 (sender (mail-header-from data-header))
300 (from (gnus-group-guess-full-name-from-command-method from))
301 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
302 (to-name (if to to "the Bit Bucket")))
303 (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
304 id (if method "respooling" "going") from to)
306 (gnus-registry-handle-action
308 ;; unless copying, remove the old "from" group
309 (if (not (equal 'copy action)) from nil)
312 (defun gnus-registry-spool-action (id group &optional subject sender)
313 (let ((to (gnus-group-guess-full-name-from-command-method group))
314 (subject (or subject (message-fetch-field "subject")))
315 (sender (or sender (message-fetch-field "from"))))
316 (when (and (stringp id) (string-match "\r$" id))
317 (setq id (substring id 0 -1)))
318 (gnus-message 7 "Gnus registry: article %s spooled to %s"
321 (gnus-registry-handle-action id nil to subject sender)))
323 (defun gnus-registry-handle-action (id from to subject sender)
326 "gnus-registry-handle-action %S" (list id from to subject sender))
327 (let ((db gnus-registry-db)
329 (entry (gnus-registry-get-or-make-entry id))
330 (subject (gnus-string-remove-all-properties
331 (gnus-registry-simplify-subject subject)))
332 (sender (gnus-string-remove-all-properties sender)))
334 ;; this could be done by calling `gnus-registry-set-id-key'
335 ;; several times but it's better to bunch the transactions
338 (registry-delete db (list id) nil)
340 (setq entry (cons (delete from (assoc 'group entry))
341 (assq-delete-all 'group entry))))
343 (dolist (kv `((group ,to) (sender ,sender) (subject ,subject)))
345 (let ((new (or (assq (first kv) entry)
347 (add-to-list 'new (second kv) t)
348 (setq entry (cons new
349 (assq-delete-all (first kv) entry))))))
350 (gnus-message 10 "Gnus registry: new entry for %s is %S"
353 (registry-insert db id entry)))
355 ;; Function for nn{mail|imap}-split-fancy: look up all references in
356 ;; the cache and if a match is found, return that group.
357 (defun gnus-registry-split-fancy-with-parent ()
358 "Split this message into the same group as its parent. The parent
359 is obtained from the registry. This function can be used as an entry
360 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
361 this: (: gnus-registry-split-fancy-with-parent)
363 This function tracks ALL backends, unlike
364 `nnmail-split-fancy-with-parent' which tracks only nnmail
367 For a message to be split, it looks for the parent message in the
368 References or In-Reply-To header and then looks in the registry
369 to see which group that message was put in. This group is
370 returned, unless `gnus-registry-follow-group-p' return nil for
373 See the Info node `(gnus)Fancy Mail Splitting' for more details."
374 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
375 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
376 ;; now, if reply-to is valid, append it to the References
378 (concat refstr " " reply-to)
380 (references (and refstr (gnus-extract-references refstr)))
381 ;; these may not be used, but the code is cleaner having them up here
382 (sender (gnus-string-remove-all-properties
383 (message-fetch-field "from")))
384 (subject (gnus-string-remove-all-properties
385 (gnus-registry-simplify-subject
386 (message-fetch-field "subject"))))
388 (nnmail-split-fancy-with-parent-ignore-groups
389 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
390 nnmail-split-fancy-with-parent-ignore-groups
391 (list nnmail-split-fancy-with-parent-ignore-groups))))
392 (gnus-registry--split-fancy-with-parent-internal
393 :references references
397 :log-agent "Gnus registry fancy splitting with parent")))
399 (defun* gnus-registry--split-fancy-with-parent-internal
401 &key references refstr sender subject log-agent
405 "gnus-registry--split-fancy-with-parent-internal %S" spec)
406 (let ((db gnus-registry-db)
408 ;; this is a big chain of statements. it uses
409 ;; gnus-registry-post-process-groups to filter the results after
411 ;; the references string must be valid and parse to valid references
415 "%s is tracing references %s"
417 (dolist (reference (nreverse references))
418 (gnus-message 9 "%s is looking up %s" log-agent reference)
419 (loop for group in (gnus-registry-get-id-key reference 'group)
420 when (gnus-registry-follow-group-p group)
421 do (gnus-message 7 "%s traced %s to %s" log-agent reference group)
422 do (push group found)))
423 ;; filter the found groups and return them
424 ;; the found groups are the full groups
425 (setq found (gnus-registry-post-process-groups
426 "references" refstr found)))
428 ;; else: there were no matches, try the extra tracking by sender
429 (when (and (null found)
430 (memq 'sender gnus-registry-track-extra)
432 (not (gnus-grep-in-list
434 gnus-registry-unfollowed-addresses)))
439 (gnus-registry-get-id-key reference 'group))
440 (registry-lookup-secondary-value db 'sender sender)))))
442 (loop for group in groups
443 when (gnus-registry-follow-group-p group)
445 ;; warn more if gnus-registry-track-extra
446 (if gnus-registry-track-extra 7 9)
447 "%s (extra tracking) traced sender '%s' to %s"
448 log-agent sender group)
451 ;; filter the found groups and return them
452 ;; the found groups are NOT the full groups
453 (setq found (gnus-registry-post-process-groups
454 "sender" sender found)))
456 ;; else: there were no matches, now try the extra tracking by subject
457 (when (and (null found)
458 (memq 'subject gnus-registry-track-extra)
460 (< gnus-registry-minimum-subject-length (length subject)))
465 (gnus-registry-get-id-key reference 'group))
466 (registry-lookup-secondary-value db 'subject subject)))))
468 (loop for group in groups
469 when (gnus-registry-follow-group-p group)
471 ;; warn more if gnus-registry-track-extra
472 (if gnus-registry-track-extra 7 9)
473 "%s (extra tracking) traced subject '%s' to %s"
474 log-agent subject group)
476 ;; filter the found groups and return them
477 ;; the found groups are NOT the full groups
478 (setq found (gnus-registry-post-process-groups
479 "subject" subject found))))
480 ;; after the (cond) we extract the actual value safely
483 (defun gnus-registry-post-process-groups (mode key groups)
484 "Inspects GROUPS found by MODE for KEY to determine which ones to follow.
486 MODE can be 'subject' or 'sender' for example. The KEY is the
487 value by which MODE was searched.
489 Transforms each group name to the equivalent short name.
491 Checks if the current Gnus method (from `gnus-command-method' or
492 from `gnus-newsgroup-name') is the same as the group's method.
493 Foreign methods are not supported so they are rejected.
495 Reduces the list to a single group, or complains if that's not
496 possible. Uses `gnus-registry-split-strategy'."
497 (let ((log-agent "gnus-registry-post-process-group")
498 (desc (format "%d groups" (length groups)))
500 ;; the strategy can be nil, in which case chosen is nil
502 (case gnus-registry-split-strategy
503 ;; default, take only one-element lists into chosen
505 (and (= (length groups) 1)
512 (let ((freq (make-hash-table
515 (mapc (lambda (x) (let ((x (gnus-group-short-name x)))
516 (puthash x (1+ (gethash x freq 0)) freq)))
518 (setq desc (format "%d groups, %d unique"
520 (hash-table-count freq)))
524 (> (gethash (gnus-group-short-name a) freq 0)
525 (gethash (gnus-group-short-name b) freq 0)))))))))
530 "%s: strategy %s on %s produced %s"
531 log-agent gnus-registry-split-strategy desc chosen)
534 "%s: strategy %s on %s did not produce an answer"
536 (or gnus-registry-split-strategy "default")
539 (setq groups (and chosen (list chosen)))
541 (dolist (group groups)
542 (let ((m1 (gnus-find-method-for-group group))
543 (m2 (or gnus-command-method
544 (gnus-find-method-for-group gnus-newsgroup-name)))
545 (short-name (gnus-group-short-name group)))
546 (if (gnus-methods-equal-p m1 m2)
548 ;; this is REALLY just for debugging
549 (when (not (equal group short-name))
552 "%s: stripped group %s to %s"
553 log-agent group short-name))
554 (add-to-list 'out short-name))
558 "%s: ignored foreign group %s"
561 (setq out (delq nil out))
564 ((= (length out) 1) out)
568 "%s: no matches for %s '%s'."
573 "%s: too many extra matches (%s) for %s '%s'. Returning none."
574 log-agent out mode key)
577 (defun gnus-registry-follow-group-p (group)
578 "Determines if a group name should be followed.
579 Consults `gnus-registry-unfollowed-groups' and
580 `nnmail-split-fancy-with-parent-ignore-groups'."
582 (not (or (gnus-grep-in-list
584 gnus-registry-unfollowed-groups)
587 nnmail-split-fancy-with-parent-ignore-groups)))))
589 (defun gnus-registry-wash-for-keywords (&optional force)
590 "Get the keywords of the current article.
591 Overrides existing keywords with FORCE set non-nil."
593 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article))
595 (if (or (not (gnus-registry-get-id-key id 'keyword))
597 (with-current-buffer gnus-article-buffer
599 (save-window-excursion
601 (narrow-to-region (point) (point-max))
602 (with-syntax-table gnus-adaptive-word-syntax-table
603 (while (re-search-forward "\\b\\w+\\b" nil t)
604 (setq word (gnus-string-remove-all-properties
605 (downcase (buffer-substring
606 (match-beginning 0) (match-end 0)))))
607 (if (> (length word) 2)
608 (push word words))))))
609 (gnus-registry-set-id-key id 'keyword words)))))
611 (defun gnus-registry-keywords ()
612 (let ((table (registry-lookup-secondary gnus-registry-db 'keyword)))
613 (when table (maphash (lambda (k v) k) table))))
615 (defun gnus-registry-find-keywords (keyword)
617 (completing-read "Keyword: " (gnus-registry-keywords) nil t)))
618 (registry-lookup-secondary-value gnus-registry-db 'keyword keyword))
620 (defun gnus-registry-register-message-ids ()
621 "Register the Message-ID of every article in the group"
622 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
623 (dolist (article gnus-newsgroup-articles)
624 (let* ((id (gnus-registry-fetch-message-id-fast article))
625 (groups (gnus-registry-get-id-key id 'group)))
626 (unless (member gnus-newsgroup-name groups)
627 (gnus-message 9 "Registry: Registering article %d with group %s"
628 article gnus-newsgroup-name)
629 (gnus-registry-handle-action id nil gnus-newsgroup-name
630 (gnus-registry-fetch-simplified-message-subject-fast article)
631 (gnus-registry-fetch-sender-fast article)))))))
633 ;; message field fetchers
634 (defun gnus-registry-fetch-message-id-fast (article)
635 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
636 (if (and (numberp article)
637 (assoc article (gnus-data-list nil)))
638 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
641 (defun gnus-registry-simplify-subject (subject)
642 (if (stringp subject)
643 (gnus-simplify-subject subject)
646 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
647 "Fetch the Subject quickly, using the internal gnus-data-list function"
648 (if (and (numberp article)
649 (assoc article (gnus-data-list nil)))
650 (gnus-string-remove-all-properties
651 (gnus-registry-simplify-subject
652 (mail-header-subject (gnus-data-header
653 (assoc article (gnus-data-list nil))))))
656 (defun gnus-registry-fetch-sender-fast (article)
657 "Fetch the Sender quickly, using the internal gnus-data-list function"
658 (if (and (numberp article)
659 (assoc article (gnus-data-list nil)))
660 (gnus-string-remove-all-properties
661 (mail-header-from (gnus-data-header
662 (assoc article (gnus-data-list nil)))))
665 ;; registry marks glue
666 (defun gnus-registry-do-marks (type function)
667 "For each known mark, call FUNCTION for each cell of type TYPE.
669 FUNCTION should take two parameters, a mark symbol and the cell value."
670 (dolist (mark-info gnus-registry-marks)
671 (let* ((mark (car-safe mark-info))
672 (data (cdr-safe mark-info))
673 (cell-data (plist-get data type)))
675 (funcall function mark cell-data)))))
677 ;;; this is ugly code, but I don't know how to do it better
678 (defun gnus-registry-install-shortcuts ()
679 "Install the keyboard shortcuts and menus for the registry.
680 Uses `gnus-registry-marks' to find what shortcuts to install."
682 (setq gnus-registry-misc-menus nil)
683 (gnus-registry-do-marks
686 (let ((function-format
687 (format "gnus-registry-%%s-article-%s-mark" mark)))
689 ;;; The following generates these functions:
690 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
691 ;;; "Apply the Important mark to process-marked ARTICLES."
692 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
693 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
694 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
695 ;;; "Apply the Important mark to process-marked ARTICLES."
696 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
697 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
699 (dolist (remove '(t nil))
700 (let* ((variant-name (if remove "remove" "set"))
701 (function-name (format function-format variant-name))
702 (shortcut (format "%c" data))
703 (shortcut (if remove (upcase shortcut) shortcut)))
704 (unintern function-name obarray)
708 ,(intern function-name)
709 ;; parameter definition
713 "%s the %s mark over process-marked ARTICLES."
714 (upcase-initials variant-name)
716 ;; interactive definition
718 (gnus-summary-work-articles current-prefix-arg))
721 ;; if this is called and the user doesn't want the
722 ;; registry enabled, we'll ask anyhow
723 (when (eq gnus-registry-install nil)
724 (setq gnus-registry-install 'ask))
726 ;; now the user is asked if gnus-registry-install is 'ask
727 (when (gnus-registry-install-p)
728 (gnus-registry-set-article-mark-internal
729 ;; all this just to get the mark, I must be doing it wrong
730 (intern ,(symbol-name mark))
734 "Applying mark %s to %d articles"
735 ,(symbol-name mark) (length articles))
736 (dolist (article articles)
737 (gnus-summary-update-article
739 (assoc article (gnus-data-list nil)))))))
740 (push (intern function-name) keys-plist)
741 (push shortcut keys-plist)
742 (push (vector (format "%s %s"
743 (upcase-initials variant-name)
745 (intern function-name) t)
746 gnus-registry-misc-menus)
749 "Defined mark handling function %s"
752 '(gnus-registry-mark-map "M" gnus-summary-mark-map)
754 (add-hook 'gnus-summary-menu-hook
757 gnus-summary-misc-menu
759 (cons "Registry Marks" gnus-registry-misc-menus))))))
762 ;;; (defalias 'gnus-user-format-function-M
763 ;;; 'gnus-registry-user-format-function-M)
764 (defun gnus-registry-user-format-function-M (headers)
765 (let* ((id (mail-header-message-id headers))
766 (marks (when id (gnus-registry-get-id-key id 'mark))))
767 (apply 'concat (mapcar (lambda (mark)
771 (assoc mark gnus-registry-marks))
778 (defun gnus-registry-read-mark ()
779 "Read a mark name from the user with completion."
780 (let ((mark (gnus-completing-read
782 (mapcar 'symbol-name (mapcar 'car gnus-registry-marks))
784 (symbol-name gnus-registry-default-mark))))
788 (defun gnus-registry-set-article-mark (&rest articles)
789 "Apply a mark to process-marked ARTICLES."
790 (interactive (gnus-summary-work-articles current-prefix-arg))
791 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
794 (defun gnus-registry-remove-article-mark (&rest articles)
795 "Remove a mark from process-marked ARTICLES."
796 (interactive (gnus-summary-work-articles current-prefix-arg))
797 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
800 (defun gnus-registry-set-article-mark-internal (mark
804 "Apply or remove MARK across a list of ARTICLES."
805 (let ((article-id-list
806 (mapcar 'gnus-registry-fetch-message-id-fast articles)))
807 (dolist (id article-id-list)
808 (let* ((marks (delq mark (gnus-registry-get-id-key id 'mark)))
809 (marks (if remove marks (cons mark marks))))
811 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
812 (if remove "Removing" "Adding")
814 (gnus-registry-set-id-key id 'mark marks)))))
816 (defun gnus-registry-get-article-marks (&rest articles)
817 "Get the Gnus registry marks for ARTICLES and show them if interactive.
818 Uses process/prefix conventions. For multiple articles,
819 only the last one's marks are returned."
820 (interactive (gnus-summary-work-articles 1))
821 (let* ((article (last articles))
822 (id (gnus-registry-fetch-message-id-fast article))
823 (marks (when id (gnus-registry-get-id-key id 'mark))))
824 (when (interactive-p)
825 (gnus-message 1 "Marks are %S" marks))
828 (defun gnus-registry-group-count (id)
829 "Get the number of groups of a message, based on the message ID."
830 (length (gnus-registry-get-id-key id 'group)))
832 (defun gnus-registry-get-or-make-entry (id)
833 (let* ((db gnus-registry-db)
835 (entries (registry-lookup db (list id))))
838 (registry-insert db id (list (list 'creation-time (current-time))
839 '(group) '(sender) '(subject)))
840 (setq entries (registry-lookup db (list id))))
842 (nth 1 (assoc id entries))))
844 (defun gnus-registry-delete-entries (idlist)
845 (registry-delete gnus-registry-db idlist nil))
847 (defun gnus-registry-get-id-key (id key)
848 (cdr-safe (assq key (gnus-registry-get-or-make-entry id))))
850 (defun gnus-registry-set-id-key (id key vals)
851 (let* ((db gnus-registry-db)
852 (entry (gnus-registry-get-or-make-entry id)))
853 (registry-delete db (list id) nil)
854 (setq entry (cons (cons key vals) (assq-delete-all key entry)))
855 (registry-insert db id entry)
858 (defun gnus-registry-import-eld (file)
859 (interactive "fOld registry file to import? ")
861 ;; (setq gnus-registry-alist '(
862 ;; ("<messageID>" ((marks nil)
863 ;; (mtime 19365 1776 440496)
864 ;; (sender . "root (Cron Daemon)")
865 ;; (subject . "Cron"))
866 ;; "cron" "nnml+private:cron")
868 (when (boundp 'gnus-registry-alist)
869 (let* ((old (symbol-value 'gnus-registry-alist))
871 (expected (length old))
873 (while (car-safe old)
875 ;; don't use progress reporters for backwards compatibility
876 (when (and (< 0 expected)
877 (= 0 (mod count 100)))
878 (message "importing: %d of %d (%.2f%%)"
879 count expected (/ (* 100 count) expected)))
880 (setq entry (car-safe old)
882 (let* ((id (car-safe entry))
883 (new-entry (gnus-registry-get-or-make-entry id))
884 (rest (cdr-safe entry))
885 (groups (loop for p in rest
889 ;; remove all the strings from the entry
890 (delete* nil rest :test (lambda (a b) (stringp b)))
891 (gnus-registry-set-id-key id 'group groups)
892 ;; just use the first extra element
893 (setq rest (car-safe rest))
894 (while (car-safe rest)
895 (setq extra-cell (car-safe rest)
896 key (car-safe extra-cell)
897 val (cdr-safe extra-cell)
898 rest (cdr-safe rest))
899 (when (and val (atom val))
900 (setq val (list val)))
901 (gnus-registry-set-id-key id key val))))
902 (message "Import done, collected %d entries" count))))
904 (ert-deftest gnus-registry-usage-test ()
906 (tempfile (make-temp-file "gnus-registry-persist"))
907 (db (gnus-registry-make-db tempfile))
908 (gnus-registry-db db)
910 (message "Adding %d keys to the test Gnus registry" n)
912 (let ((id (number-to-string i)))
913 (gnus-registry-handle-action id
914 (if (>= 50 i) "fromgroup" nil)
917 (format "subject %d" (mod i 10)))
919 (format "sender %d" (mod i 10))))))
920 (message "Testing Gnus registry size is %d" n)
921 (should (= n (registry-size db)))
922 (message "Looking up individual keys (registry-lookup)")
923 (should (equal (loop for e
925 (registry-lookup db '("20" "83" "72")))
926 collect (assq 'subject e)
927 collect (assq 'sender e)
928 collect (assq 'group e))
929 '((subject "subject 0") (sender "sender 0") (group "togroup")
930 (subject) (sender) (group "togroup")
931 (subject) (sender "sender 2") (group "togroup"))))
933 (message "Looking up individual keys (gnus-registry-id-key)")
934 (should (equal (gnus-registry-get-id-key "34" 'group) '("togroup")))
935 (should (equal (gnus-registry-get-id-key "34" 'subject) '("subject 4")))
936 (message "Trying to insert a duplicate key")
937 (should-error (registry-insert db "55" '()))
938 (message "Looking up individual keys (gnus-registry-get-or-make-entry)")
939 (should (gnus-registry-get-or-make-entry "22"))
940 (message "Saving the Gnus registry to %s" tempfile)
941 (should (gnus-registry-save tempfile db))
942 (setq size (nth 7 (file-attributes tempfile)))
943 (message "Saving the Gnus registry to %s: size %d" tempfile size)
946 (insert-file-contents-literally tempfile)
947 (should (looking-at (concat ";; Object "
949 "\n;; EIEIO PERSISTENT OBJECT"))))
950 (message "Reading Gnus registry back")
951 (setq back (eieio-persistent-read tempfile))
953 (message "Read Gnus registry back: %d keys, expected %d==%d"
954 (registry-size back) n (registry-size db))
955 (should (= (registry-size back) n))
956 (should (= (registry-size back) (registry-size db)))
957 (delete-file tempfile)
958 (message "Pruning Gnus registry to 0 by setting :max-soft")
959 (oset db :max-soft 0)
961 (should (= (registry-size db) 0)))
962 (message "Done with Gnus registry usage testing."))
965 (defun gnus-registry-initialize ()
966 "Initialize the Gnus registry."
968 (gnus-message 5 "Initializing the registry")
969 (setq gnus-registry-install t) ; in case it was 'ask or nil
970 (gnus-registry-install-hooks)
971 (gnus-registry-install-shortcuts)
972 (gnus-registry-read))
975 (defun gnus-registry-install-hooks ()
976 "Install the registry hooks."
978 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
979 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
980 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
981 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
983 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
984 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
986 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
988 (defun gnus-registry-unload-hook ()
989 "Uninstall the registry hooks."
991 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
992 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
993 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
994 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
996 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
997 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
999 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
1001 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
1003 (defun gnus-registry-install-p ()
1005 (when (eq gnus-registry-install 'ask)
1006 (setq gnus-registry-install
1008 (concat "Enable the Gnus registry? "
1009 "See the variable `gnus-registry-install' "
1010 "to get rid of this query permanently. ")))
1011 (when gnus-registry-install
1012 ;; we just set gnus-registry-install to t, so initialize the registry!
1013 (gnus-registry-initialize)))
1014 ;;; we could call it here: (customize-variable 'gnus-registry-install)
1015 gnus-registry-install)
1017 ;; TODO: a few things
1019 (provide 'gnus-registry)
1021 ;;; gnus-registry.el ends here