1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 2002-2016 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 ;; better documentation in the manual (also on my to-do list).
36 ;; If you want to track recipients (and you should to make the
37 ;; gnus-registry splitting work better), you need the To and Cc
38 ;; headers collected by Gnus. Note that in more recent Gnus versions
39 ;; this is already the case: look at `gnus-extra-headers' to be sure.
41 ;; ;;; you may also want Gcc Newsgroups Keywords X-Face
42 ;; (add-to-list 'gnus-extra-headers 'To)
43 ;; (add-to-list 'gnus-extra-headers 'Cc)
44 ;; (setq nnmail-extra-headers gnus-extra-headers)
46 ;; Put this in your startup file (~/.gnus.el for instance) or use Customize:
48 ;; (setq gnus-registry-max-entries 2500
49 ;; gnus-registry-track-extra '(sender subject recipient))
51 ;; (gnus-registry-initialize)
53 ;; Then use this in your fancy-split:
55 ;; (: gnus-registry-split-fancy-with-parent)
57 ;; You should also consider using the nnregistry backend to look up
58 ;; articles. See the Gnus manual for more information.
60 ;; Finally, you can put %uM in your summary line format to show the
61 ;; registry marks if you do this:
63 ;; show the marks as single characters (see the :char property in
64 ;; `gnus-registry-marks'):
65 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
67 ;; show the marks by name (see `gnus-registry-marks'):
68 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
72 ;; - get the correct group on spool actions
74 ;; - articles that are spooled to a different backend should be moved
79 (eval-when-compile (require 'cl
))
90 (defvar gnus-adaptive-word-syntax-table
)
92 (defvar gnus-registry-dirty t
93 "Boolean set to t when the registry is modified.")
95 (defgroup gnus-registry nil
100 (defvar gnus-registry-marks
103 :image
"summary_important")
106 :image
"summary_work")
109 :image
"summary_personal")
112 :image
"summary_todo")
115 :image
"summary_later"))
117 "List of registry marks and their options.
119 `gnus-registry-mark-article' will offer symbols from this list
122 Each entry must have a character to be useful for summary mode
123 line display and for keyboard shortcuts.
125 Each entry must have an image string to be useful for visual
128 (defcustom gnus-registry-default-mark
'To-Do
129 "The default mark. Should be a valid key for `gnus-registry-marks'."
130 :group
'gnus-registry
133 (defcustom gnus-registry-unfollowed-addresses
134 (list (regexp-quote user-mail-address
))
135 "List of addresses that gnus-registry-split-fancy-with-parent won't trace.
136 The addresses are matched, they don't have to be fully qualified.
137 In the messages, these addresses can be the sender or the
140 :group
'gnus-registry
141 :type
'(repeat regexp
))
143 (defcustom gnus-registry-unfollowed-groups
144 '("delayed$" "drafts$" "queue$" "INBOX$" "^nnmairix:" "archive")
145 "List of groups that gnus-registry-split-fancy-with-parent won't return.
146 The group names are matched, they don't have to be fully
147 qualified. This parameter tells the Gnus registry 'never split a
148 message into a group that matches one of these, regardless of
151 nnmairix groups are specifically excluded because they are ephemeral."
152 :group
'gnus-registry
153 :type
'(repeat regexp
))
155 (defcustom gnus-registry-install
'ask
156 "Whether the registry should be installed."
157 :group
'gnus-registry
158 :type
'(choice (const :tag
"Never Install" nil
)
159 (const :tag
"Always Install" t
)
160 (const :tag
"Ask Me" ask
)))
162 (defvar gnus-registry-enabled nil
)
164 (defvar gnus-summary-misc-menu
) ;; Avoid byte compiler warning.
166 (defvar gnus-registry-misc-menus nil
) ; ugly way to keep the menus
168 (make-obsolete-variable 'gnus-registry-clean-empty nil
"23.4")
169 (make-obsolete-variable 'gnus-registry-use-long-group-names nil
"23.4")
170 (make-obsolete-variable 'gnus-registry-max-track-groups nil
"23.4")
171 (make-obsolete-variable 'gnus-registry-entry-caching nil
"23.4")
172 (make-obsolete-variable 'gnus-registry-trim-articles-without-groups nil
"23.4")
173 ;; FIXME it was simply deleted.
174 (make-obsolete-variable 'gnus-registry-max-pruned-entries nil
"25.1")
176 (defcustom gnus-registry-track-extra
'(subject sender recipient
)
177 "Whether the registry should track extra data about a message.
178 The subject, recipients (To: and Cc:), and Sender (From:) headers
179 are tracked this way by default."
180 :group
'gnus-registry
182 '(set :tag
"Tracking choices"
183 (const :tag
"Track by subject (Subject: header)" subject
)
184 (const :tag
"Track by recipient (To: and Cc: headers)" recipient
)
185 (const :tag
"Track by sender (From: header)" sender
)))
187 (defcustom gnus-registry-split-strategy nil
188 "The splitting strategy applied to the keys in `gnus-registry-track-extra'.
190 Given a set of unique found groups G and counts for each element
191 of G, and a key K (typically `sender' or `subject'):
193 When nil, if G has only one element, use it. Otherwise give up.
194 This is the fastest but also least useful strategy.
196 When `majority', use the majority by count. So if there is a
197 group with the most articles counted by K, use that. Ties are
198 resolved in no particular order, simply the first one found wins.
199 This is the slowest strategy but also the most accurate one.
201 When `first', the first element of G wins. This is fast and
202 should be OK if your senders and subjects don't \"bleed\" across
204 :group
'gnus-registry
206 '(choice :tag
"Splitting strategy"
207 (const :tag
"Only use single choices, discard multiple matches" nil
)
208 (const :tag
"Majority of matches wins" majority
)
209 (const :tag
"First found wins" first
)))
211 (defcustom gnus-registry-minimum-subject-length
5
212 "The minimum length of a subject before it's considered trackable."
213 :group
'gnus-registry
216 (defcustom gnus-registry-extra-entries-precious
'(mark)
217 "What extra keys are precious, meaning entries with them won't get pruned.
218 By default, 'mark is included, so articles with marks are
221 Before you save the Gnus registry, it's pruned. Any entries with
222 keys in this list will not be pruned. All other entries go to
224 :group
'gnus-registry
225 :type
'(repeat symbol
))
227 (defcustom gnus-registry-cache-file
229 (or gnus-dribble-directory gnus-home-directory
"~/")
230 ".gnus.registry.eieio")
231 "File where the Gnus registry will be stored."
232 :group
'gnus-registry
235 (defcustom gnus-registry-max-entries nil
236 "Maximum number of entries in the registry, nil for unlimited."
237 :group
'gnus-registry
238 :type
'(radio (const :format
"Unlimited " nil
)
239 (integer :format
"Maximum number: %v")))
241 (defcustom gnus-registry-prune-factor
0.1
242 "When pruning, try to prune back to this factor less than the maximum size.
244 In order to prevent constant pruning, we prune back to a number
245 somewhat less than the maximum size. This option controls
246 exactly how much less. For example, given a maximum size of
247 50000 and a prune factor of 0.1, the pruning process will try to
248 cut the registry back to \(- 50000 \(* 50000 0.1)) -> 45000
249 entries. The pruning process is constrained by the presence of
250 \"precious\" entries."
252 :group
'gnus-registry
255 (defcustom gnus-registry-default-sort-function
256 #'gnus-registry-sort-by-creation-time
257 "Sort function to use when pruning the registry.
258 Entries that sort to the front of the list are pruned first.
259 This can slow pruning down. Set to nil to perform no sorting."
261 :group
'gnus-registry
262 :type
'(choice (const :tag
"No sorting" nil
) function
))
264 (defun gnus-registry-sort-by-creation-time (l r
)
265 "Sort older entries to front of list."
266 ;; Pruning starts from the front of the list.
268 (cadr (assq 'creation-time r
))
269 (cadr (assq 'creation-time l
))))
271 (defun gnus-registry-fixup-registry (db)
273 (let ((old (oref db tracked
)))
274 (setf (oref db precious
)
275 (append gnus-registry-extra-entries-precious
277 (setf (oref db max-size
)
278 (or gnus-registry-max-entries
279 most-positive-fixnum
))
280 (setf (oref db prune-factor
)
281 (or gnus-registry-prune-factor
283 (setf (oref db tracked
)
284 (append gnus-registry-track-extra
285 '(mark group keyword
)))
286 (when (not (equal old
(oref db tracked
)))
287 (gnus-message 9 "Reindexing the Gnus registry (tracked change)")
288 (registry-reindex db
))))
291 (defun gnus-registry-make-db (&optional file
)
292 (interactive "fGnus registry persistence file: \n")
293 (gnus-registry-fixup-registry
294 (make-instance 'registry-db
295 :file
(or file gnus-registry-cache-file
)
296 ;; these parameters are set in `gnus-registry-fixup-registry'
297 :max-size most-positive-fixnum
298 :version registry-db-version
302 (defvar gnus-registry-db
(gnus-registry-make-db)
303 "The article registry by Message ID. See `registry-db'.")
305 ;; top-level registry data management
306 (defun gnus-registry-remake-db (&optional forsure
)
307 "Remake the registry database after customization.
308 This is not required after changing `gnus-registry-cache-file'."
309 (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
311 (gnus-message 4 "Remaking the Gnus registry")
312 (setq gnus-registry-db
(gnus-registry-make-db))))
314 (defun gnus-registry-load ()
315 "Load the registry from the cache file."
317 (let ((file gnus-registry-cache-file
))
319 (gnus-registry-read file
)
321 ;; Fix previous mis-naming of the registry file.
323 (concat (file-name-sans-extension
324 gnus-registry-cache-file
)
326 (if (and (file-exists-p old-file-name
)
328 (format "Rename registry file from %s to %s? "
329 old-file-name file
)))
331 (gnus-registry-read old-file-name
)
332 (setf (oref gnus-registry-db file
) file
)
333 (gnus-message 1 "Registry filename changed to %s" file
))
334 (gnus-registry-remake-db t
))))
338 "The Gnus registry could not be loaded from %s, creating a new one"
340 (gnus-registry-remake-db t
)))))
342 (defun gnus-registry-read (file)
343 "Do the actual reading of the registry persistence file."
344 (gnus-message 5 "Reading Gnus registry from %s..." file
)
345 (setq gnus-registry-db
346 (gnus-registry-fixup-registry
349 (eieio-persistent-read file
'registry-db
))
350 ;; Older EIEIO versions do not check the class name.
351 ('wrong-number-of-arguments
352 (eieio-persistent-read file
)))))
353 (gnus-message 5 "Reading Gnus registry from %s...done" file
))
355 (defun gnus-registry-save (&optional file db
)
356 "Save the registry cache file."
358 (let ((file (or file gnus-registry-cache-file
))
359 (db (or db gnus-registry-db
)))
360 (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
361 (registry-size db
) file
)
363 db gnus-registry-default-sort-function
)
364 ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
365 (eieio-persistent-save db file
)
366 (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
367 (registry-size db
) file
)))
369 (defun gnus-registry-remove-ignored ()
371 (let* ((db gnus-registry-db
)
372 (grouphashtb (registry-lookup-secondary db
'group
))
373 (old-size (registry-size db
)))
374 (registry-reindex db
)
375 (loop for k being the hash-keys of grouphashtb
376 using
(hash-values v
)
377 when
(gnus-registry-ignore-group-p k
)
378 do
(registry-delete db v nil
))
379 (registry-reindex db
)
380 (gnus-message 4 "Removed %d ignored entries from the Gnus registry"
381 (- old-size
(registry-size db
)))))
383 ;; article move/copy/spool/delete actions
384 (defun gnus-registry-action (action data-header from
&optional to method
)
385 (let* ((id (mail-header-id data-header
))
386 (subject (mail-header-subject data-header
))
387 (extra (mail-header-extra data-header
))
388 (recipients (gnus-registry-sort-addresses
389 (or (cdr-safe (assq 'Cc extra
)) "")
390 (or (cdr-safe (assq 'To extra
)) "")))
391 (sender (nth 0 (gnus-registry-extract-addresses
392 (mail-header-from data-header
))))
393 (from (gnus-group-guess-full-name-from-command-method from
))
394 (to (if to
(gnus-group-guess-full-name-from-command-method to
) nil
)))
395 (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
396 id
(if method
"respooling" "going") from to
)
398 (gnus-registry-handle-action
400 ;; unless copying, remove the old "from" group
401 (if (not (equal 'copy action
)) from nil
)
402 to subject sender recipients
)))
404 (defun gnus-registry-spool-action (id group
&optional subject sender recipients
)
405 (let ((to (gnus-group-guess-full-name-from-command-method group
))
406 (recipients (or recipients
407 (gnus-registry-sort-addresses
408 (or (message-fetch-field "cc") "")
409 (or (message-fetch-field "to") ""))))
410 (subject (or subject
(message-fetch-field "subject")))
411 (sender (or sender
(message-fetch-field "from"))))
412 (when (and (stringp id
) (string-match "\r$" id
))
413 (setq id
(substring id
0 -
1)))
414 (gnus-message 7 "Gnus registry: article %s spooled to %s"
417 (gnus-registry-handle-action id nil to subject sender recipients
)))
419 (defun gnus-registry-handle-action (id from to subject sender
420 &optional recipients
)
423 "gnus-registry-handle-action %S" (list id from to subject sender recipients
))
424 (let ((db gnus-registry-db
)
425 ;; if the group is ignored, set the destination to nil (same as delete)
426 (to (if (gnus-registry-ignore-group-p to
) nil to
))
428 (entry (gnus-registry-get-or-make-entry id
))
429 (subject (gnus-string-remove-all-properties
430 (gnus-registry-simplify-subject subject
)))
431 (sender (gnus-string-remove-all-properties sender
)))
433 ;; this could be done by calling `gnus-registry-set-id-key'
434 ;; several times but it's better to bunch the transactions
437 (registry-delete db
(list id
) nil
)
439 (setq entry
(cons (delete from
(assoc 'group entry
))
440 (assq-delete-all 'group entry
))))
442 (dolist (kv `((group ,to
)
444 (recipient ,@recipients
)
447 (let ((new (or (assq (first kv
) entry
)
449 (dolist (toadd (cdr kv
))
450 (unless (member toadd new
)
451 (setq new
(append new
(list toadd
)))))
452 (setq entry
(cons new
453 (assq-delete-all (first kv
) entry
))))))
454 (gnus-message 10 "Gnus registry: new entry for %s is %S"
457 (gnus-registry-insert db id entry
)))
459 ;; Function for nn{mail|imap}-split-fancy: look up all references in
460 ;; the cache and if a match is found, return that group.
461 (defun gnus-registry-split-fancy-with-parent ()
462 "Split this message into the same group as its parent.
463 The parent is obtained from the registry. This function can be used as an
464 entry in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
465 this: (: gnus-registry-split-fancy-with-parent)
467 This function tracks ALL backends, unlike
468 `nnmail-split-fancy-with-parent' which tracks only nnmail
471 For a message to be split, it looks for the parent message in the
472 References or In-Reply-To header and then looks in the registry
473 to see which group that message was put in. This group is
474 returned, unless `gnus-registry-follow-group-p' return nil for
477 See the Info node `(gnus)Fancy Mail Splitting' for more details."
478 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
479 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
480 ;; now, if reply-to is valid, append it to the References
482 (concat refstr
" " reply-to
)
484 (references (and refstr
(gnus-extract-references refstr
)))
485 ;; these may not be used, but the code is cleaner having them up here
486 (sender (gnus-string-remove-all-properties
487 (message-fetch-field "from")))
488 (recipients (gnus-registry-sort-addresses
489 (or (message-fetch-field "cc") "")
490 (or (message-fetch-field "to") "")))
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 (gnus-registry--split-fancy-with-parent-internal
500 :references references
503 :recipients recipients
505 :log-agent
"Gnus registry fancy splitting with parent")))
507 (defun* gnus-registry--split-fancy-with-parent-internal
509 &key references refstr sender subject recipients log-agent
513 "gnus-registry--split-fancy-with-parent-internal %S" spec
)
514 (let ((db gnus-registry-db
)
516 ;; this is a big chain of statements. it uses
517 ;; gnus-registry-post-process-groups to filter the results after
519 ;; the references string must be valid and parse to valid references
523 "%s is tracing references %s"
525 (dolist (reference (nreverse references
))
526 (gnus-message 9 "%s is looking up %s" log-agent reference
)
527 (loop for group in
(gnus-registry-get-id-key reference
'group
)
528 when
(gnus-registry-follow-group-p group
)
531 (gnus-message 7 "%s traced %s to %s" log-agent reference group
)
532 (push group found
))))
533 ;; filter the found groups and return them
534 ;; the found groups are the full groups
535 (setq found
(gnus-registry-post-process-groups
536 "references" refstr found
)))
538 ;; else: there were no matches, now try the extra tracking by subject
539 (when (and (null found
)
540 (memq 'subject gnus-registry-track-extra
)
542 (< gnus-registry-minimum-subject-length
(length subject
)))
547 (gnus-registry-get-id-key reference
'group
))
548 (registry-lookup-secondary-value db
'subject subject
)))))
550 (loop for group in groups
551 when
(gnus-registry-follow-group-p group
)
553 ;; warn more if gnus-registry-track-extra
554 (if gnus-registry-track-extra
7 9)
555 "%s (extra tracking) traced subject `%s' to %s"
556 log-agent subject group
)
558 ;; filter the found groups and return them
559 ;; the found groups are NOT the full groups
560 (setq found
(gnus-registry-post-process-groups
561 "subject" subject found
))))
563 ;; else: there were no matches, try the extra tracking by sender
564 (when (and (null found
)
565 (memq 'sender gnus-registry-track-extra
)
567 (not (gnus-grep-in-list
569 gnus-registry-unfollowed-addresses
)))
574 (gnus-registry-get-id-key reference
'group
))
575 (registry-lookup-secondary-value db
'sender sender
)))))
577 (loop for group in groups
578 when
(gnus-registry-follow-group-p group
)
580 ;; warn more if gnus-registry-track-extra
581 (if gnus-registry-track-extra
7 9)
582 "%s (extra tracking) traced sender `%s' to %s"
583 log-agent sender group
)
586 ;; filter the found groups and return them
587 ;; the found groups are NOT the full groups
588 (setq found
(gnus-registry-post-process-groups
589 "sender" sender found
)))
591 ;; else: there were no matches, try the extra tracking by recipient
592 (when (and (null found
)
593 (memq 'recipient gnus-registry-track-extra
)
595 (dolist (recp recipients
)
596 (when (and (null found
)
597 (not (gnus-grep-in-list
599 gnus-registry-unfollowed-addresses
)))
600 (let ((groups (apply 'append
603 (gnus-registry-get-id-key reference
'group
))
604 (registry-lookup-secondary-value
605 db
'recipient recp
)))))
607 (loop for group in groups
608 when
(gnus-registry-follow-group-p group
)
610 ;; warn more if gnus-registry-track-extra
611 (if gnus-registry-track-extra
7 9)
612 "%s (extra tracking) traced recipient `%s' to %s"
613 log-agent recp group
)
614 and collect group
)))))
616 ;; filter the found groups and return them
617 ;; the found groups are NOT the full groups
618 (setq found
(gnus-registry-post-process-groups
619 "recipients" (mapconcat 'identity recipients
", ") found
)))
621 ;; after the (cond) we extract the actual value safely
624 (defun gnus-registry-post-process-groups (mode key groups
)
625 "Inspects GROUPS found by MODE for KEY to determine which ones to follow.
627 MODE can be `subject' or `sender' for example. The KEY is the
628 value by which MODE was searched.
630 Transforms each group name to the equivalent short name.
632 Checks if the current Gnus method (from `gnus-command-method' or
633 from `gnus-newsgroup-name') is the same as the group's method.
634 Foreign methods are not supported so they are rejected.
636 Reduces the list to a single group, or complains if that's not
637 possible. Uses `gnus-registry-split-strategy'."
638 (let ((log-agent "gnus-registry-post-process-group")
639 (desc (format "%d groups" (length groups
)))
641 ;; the strategy can be nil, in which case chosen is nil
643 (case gnus-registry-split-strategy
644 ;; default, take only one-element lists into chosen
646 (and (= (length groups
) 1)
653 (let ((freq (make-hash-table
656 (mapc (lambda (x) (let ((x (gnus-group-short-name x
)))
657 (puthash x
(1+ (gethash x freq
0)) freq
)))
659 (setq desc
(format "%d groups, %d unique"
661 (hash-table-count freq
)))
665 (> (gethash (gnus-group-short-name a
) freq
0)
666 (gethash (gnus-group-short-name b
) freq
0)))))))))
671 "%s: strategy %s on %s produced %s"
672 log-agent gnus-registry-split-strategy desc chosen
)
675 "%s: strategy %s on %s did not produce an answer"
677 (or gnus-registry-split-strategy
"default")
680 (setq groups
(and chosen
(list chosen
)))
682 (dolist (group groups
)
683 (let ((m1 (gnus-find-method-for-group group
))
684 (m2 (or gnus-command-method
685 (gnus-find-method-for-group gnus-newsgroup-name
)))
686 (short-name (gnus-group-short-name group
)))
687 (if (gnus-methods-equal-p m1 m2
)
689 ;; this is REALLY just for debugging
690 (when (not (equal group short-name
))
693 "%s: stripped group %s to %s"
694 log-agent group short-name
))
695 (pushnew short-name out
:test
#'equal
))
699 "%s: ignored foreign group %s"
702 (setq out
(delq nil out
))
705 ((= (length out
) 1) out
)
709 "%s: no matches for %s `%s'."
714 "%s: too many extra matches (%s) for %s `%s'. Returning none."
715 log-agent out mode key
)
718 (defun gnus-registry-follow-group-p (group)
719 "Determines if a group name should be followed.
720 Consults `gnus-registry-unfollowed-groups' and
721 `nnmail-split-fancy-with-parent-ignore-groups'."
723 (not (or (gnus-grep-in-list
725 gnus-registry-unfollowed-groups
)
728 nnmail-split-fancy-with-parent-ignore-groups
)))))
730 ;; note that gnus-registry-ignored-groups is defined in gnus.el as a
731 ;; group/topic parameter and an associated variable!
733 ;; we do special logic for ignoring to accept regular expressions and
734 ;; nnmail-split-fancy-with-parent-ignore-groups as well
735 (defun gnus-registry-ignore-group-p (group)
736 "Determines if a group name should be ignored.
737 Consults `gnus-registry-ignored-groups' and
738 `nnmail-split-fancy-with-parent-ignore-groups'."
740 (or (gnus-grep-in-list
742 (delq nil
(mapcar (lambda (g)
745 ((and (listp g
) (nth 1 g
))
747 (t nil
))) gnus-registry-ignored-groups
)))
748 ;; only use `gnus-parameter-registry-ignore' if
749 ;; `gnus-registry-ignored-groups' is a list of lists
750 ;; (it can be a list of regexes)
751 (and (listp (nth 0 gnus-registry-ignored-groups
))
752 (get-buffer "*Group*") ; in automatic tests this is false
753 (gnus-parameter-registry-ignore group
))
756 nnmail-split-fancy-with-parent-ignore-groups
))))
758 (defun gnus-registry-wash-for-keywords (&optional force
)
759 "Get the keywords of the current article.
760 Overrides existing keywords with FORCE set non-nil."
762 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article
))
764 (if (or (not (gnus-registry-get-id-key id
'keyword
))
766 (with-current-buffer gnus-article-buffer
768 (save-window-excursion
770 (narrow-to-region (point) (point-max))
771 (with-syntax-table gnus-adaptive-word-syntax-table
772 (while (re-search-forward "\\b\\w+\\b" nil t
)
773 (setq word
(gnus-string-remove-all-properties
774 (downcase (buffer-substring
775 (match-beginning 0) (match-end 0)))))
776 (if (> (length word
) 2)
777 (push word words
))))))
778 (gnus-registry-set-id-key id
'keyword words
)))))
780 (defun gnus-registry-keywords ()
781 (let ((table (registry-lookup-secondary gnus-registry-db
'keyword
))
783 (when table
(maphash (lambda (k _v
) (push k ks
)) table
) ks
)))
785 (defun gnus-registry-find-keywords (keyword)
787 (completing-read "Keyword: " (gnus-registry-keywords) nil t
)))
788 (registry-lookup-secondary-value gnus-registry-db
'keyword keyword
))
790 (defun gnus-registry-register-message-ids ()
791 "Register the Message-ID of every article in the group."
792 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name
)
793 (dolist (article gnus-newsgroup-articles
)
794 (let* ((id (gnus-registry-fetch-message-id-fast article
))
795 (groups (gnus-registry-get-id-key id
'group
)))
796 (unless (member gnus-newsgroup-name groups
)
797 (gnus-message 9 "Registry: Registering article %d with group %s"
798 article gnus-newsgroup-name
)
799 (gnus-registry-handle-action id nil gnus-newsgroup-name
800 (gnus-registry-fetch-simplified-message-subject-fast article
)
801 (gnus-registry-fetch-sender-fast article
)
802 (gnus-registry-fetch-recipients-fast article
)))))))
804 ;; message field fetchers
805 (defun gnus-registry-fetch-message-id-fast (article)
806 "Fetch the Message-ID quickly, using the internal gnus-data-list function."
807 (if (and (numberp article
)
808 (assoc article
(gnus-data-list nil
)))
809 (mail-header-id (gnus-data-header (assoc article
(gnus-data-list nil
))))
812 (defun gnus-registry-extract-addresses (text)
813 "Extract all the addresses in a normalized way from TEXT.
814 Returns an unsorted list of strings in the name <address> format.
815 Addresses without a name will say \"noname\"."
816 (mapcar (lambda (add)
817 (gnus-string-remove-all-properties
818 (let* ((name (or (nth 0 add
) "noname"))
820 (addr (if (bufferp addr
)
821 (with-current-buffer addr
824 (format "%s <%s>" name addr
))))
825 (mail-extract-address-components text t
)))
827 (defun gnus-registry-sort-addresses (&rest addresses
)
828 "Return a normalized and sorted list of ADDRESSES."
829 (sort (apply 'nconc
(mapcar 'gnus-registry-extract-addresses addresses
))
832 (defun gnus-registry-simplify-subject (subject)
833 (if (stringp subject
)
834 (gnus-simplify-subject subject
)
837 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
838 "Fetch the Subject quickly, using the internal gnus-data-list function."
839 (if (and (numberp article
)
840 (assoc article
(gnus-data-list nil
)))
841 (gnus-string-remove-all-properties
842 (gnus-registry-simplify-subject
843 (mail-header-subject (gnus-data-header
844 (assoc article
(gnus-data-list nil
))))))
847 (defun gnus-registry-fetch-sender-fast (article)
848 (gnus-registry-fetch-header-fast "from" article
))
850 (defun gnus-registry-fetch-recipients-fast (article)
851 (gnus-registry-sort-addresses
852 (or (ignore-errors (gnus-registry-fetch-header-fast "Cc" article
)) "")
853 (or (ignore-errors (gnus-registry-fetch-header-fast "To" article
)) "")))
855 (defun gnus-registry-fetch-header-fast (article header
)
856 "Fetch the HEADER quickly, using the internal gnus-data-list function."
857 (if (and (numberp article
)
858 (assoc article
(gnus-data-list nil
)))
859 (gnus-string-remove-all-properties
860 (cdr (assq header
(gnus-data-header
861 (assoc article
(gnus-data-list nil
))))))
864 ;; registry marks glue
865 (defun gnus-registry-do-marks (type function
)
866 "For each known mark, call FUNCTION for each cell of type TYPE.
868 FUNCTION should take two parameters, a mark symbol and the cell value."
869 (dolist (mark-info gnus-registry-marks
)
870 (let* ((mark (car-safe mark-info
))
871 (data (cdr-safe mark-info
))
872 (cell-data (plist-get data type
)))
874 (funcall function mark cell-data
)))))
876 ;; FIXME: Why not merge gnus-registry--set/remove-mark and
877 ;; gnus-registry-set-article-mark-internal?
878 (defun gnus-registry--set/remove-mark
(mark remove articles
)
879 "Set/remove the MARK over process-marked ARTICLES."
880 ;; If this is called and the user doesn't want the
881 ;; registry enabled, we'll ask anyhow.
882 (unless gnus-registry-install
883 (let ((gnus-registry-install 'ask
))
884 (gnus-registry-install-p)))
886 ;; Now the user is asked if gnus-registry-install is `ask'.
887 (when (gnus-registry-install-p)
888 (gnus-registry-set-article-mark-internal
889 ;; All this just to get the mark, I must be doing it wrong.
890 mark articles remove t
)
891 ;; FIXME: Why do we do the above only here and not directly inside
892 ;; gnus-registry-set-article-mark-internal? I.e. we wouldn't we want to do
893 ;; the things below when gnus-registry-set-article-mark-internal is called
894 ;; from gnus-registry-set-article-mark or
895 ;; gnus-registry-remove-article-mark?
896 (gnus-message 9 "Applying mark %s to %d articles"
897 mark
(length articles
))
898 (dolist (article articles
)
899 (gnus-summary-update-article
901 (assoc article
(gnus-data-list nil
))))))
903 ;; This is ugly code, but I don't know how to do it better.
904 (defun gnus-registry-install-shortcuts ()
905 "Install the keyboard shortcuts and menus for the registry.
906 Uses `gnus-registry-marks' to find what shortcuts to install."
908 (setq gnus-registry-misc-menus nil
)
909 (gnus-registry-do-marks
912 (let ((function-format
913 (format "gnus-registry-%%s-article-%s-mark" mark
)))
915 ;;; The following generates these functions:
916 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
917 ;;; "Apply the Important mark to process-marked ARTICLES."
918 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
919 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
920 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
921 ;;; "Apply the Important mark to process-marked ARTICLES."
922 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
923 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
925 (dolist (remove '(t nil
))
926 (let* ((variant-name (if remove
"remove" "set"))
928 (intern (format function-format variant-name
)))
929 (shortcut (format "%c" (if remove
(upcase data
) data
))))
930 (defalias function-name
931 ;; If it weren't for the function's docstring, we could
932 ;; use a closure, with lexical-let :-(
933 `(lambda (&rest articles
)
935 "%s the %s mark over process-marked ARTICLES."
936 (upcase-initials variant-name
)
939 (gnus-summary-work-articles current-prefix-arg
))
940 (gnus-registry--set/remove-mark
',mark
',remove articles
)))
941 (push function-name keys-plist
)
942 (push shortcut keys-plist
)
943 (push (vector (format "%s %s"
944 (upcase-initials variant-name
)
947 gnus-registry-misc-menus
)
948 (gnus-message 9 "Defined mark handling function %s"
951 '(gnus-registry-mark-map "M" gnus-summary-mark-map
)
953 (add-hook 'gnus-summary-menu-hook
956 gnus-summary-misc-menu
958 (cons "Registry Marks" gnus-registry-misc-menus
))))))
960 (make-obsolete 'gnus-registry-user-format-function-M
961 'gnus-registry-article-marks-to-chars
"24.1") ?
963 (defalias 'gnus-registry-user-format-function-M
964 'gnus-registry-article-marks-to-chars
)
967 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
968 (defun gnus-registry-article-marks-to-chars (headers)
969 "Show the marks for an article by the :char property."
970 (let* ((id (mail-header-message-id headers
))
971 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
972 (mapconcat (lambda (mark)
975 (assoc mark gnus-registry-marks
))
980 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
981 (defun gnus-registry-article-marks-to-names (headers)
982 "Show the marks for an article by name."
983 (let* ((id (mail-header-message-id headers
))
984 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
985 (mapconcat (lambda (mark) (symbol-name mark
)) marks
",")))
987 (defun gnus-registry-read-mark ()
988 "Read a mark name from the user with completion."
989 (let ((mark (gnus-completing-read
991 (mapcar 'symbol-name
(mapcar 'car gnus-registry-marks
))
993 (symbol-name gnus-registry-default-mark
))))
997 (defun gnus-registry-set-article-mark (&rest articles
)
998 "Apply a mark to process-marked ARTICLES."
999 (interactive (gnus-summary-work-articles current-prefix-arg
))
1000 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
1003 (defun gnus-registry-remove-article-mark (&rest articles
)
1004 "Remove a mark from process-marked ARTICLES."
1005 (interactive (gnus-summary-work-articles current-prefix-arg
))
1006 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
1009 (defun gnus-registry-set-article-mark-internal (mark
1013 "Apply or remove MARK across a list of ARTICLES."
1014 (let ((article-id-list
1015 (mapcar 'gnus-registry-fetch-message-id-fast articles
)))
1016 (dolist (id article-id-list
)
1017 (let* ((marks (delq mark
(gnus-registry-get-id-key id
'mark
)))
1018 (marks (if remove marks
(cons mark marks
))))
1020 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
1021 (if remove
"Removing" "Adding")
1023 (gnus-registry-set-id-key id
'mark marks
)))))
1025 (defun gnus-registry-get-article-marks (&rest articles
)
1026 "Get the Gnus registry marks for ARTICLES and show them if interactive.
1027 Uses process/prefix conventions. For multiple articles,
1028 only the last one's marks are returned."
1029 (interactive (gnus-summary-work-articles 1))
1030 (let* ((article (last articles
))
1031 (id (gnus-registry-fetch-message-id-fast article
))
1032 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
1033 (when (called-interactively-p 'any
)
1034 (gnus-message 1 "Marks are %S" marks
))
1037 (defun gnus-registry-group-count (id)
1038 "Get the number of groups of a message, based on the message ID."
1039 (length (gnus-registry-get-id-key id
'group
)))
1041 (defun gnus-registry-get-or-make-entry (id)
1042 (let* ((db gnus-registry-db
)
1043 ;; safe if not found
1044 (entries (registry-lookup db
(list id
))))
1046 (when (null entries
)
1047 (gnus-registry-insert db id
(list (list 'creation-time
(current-time))
1048 '(group) '(sender) '(subject)))
1049 (setq entries
(registry-lookup db
(list id
))))
1051 (nth 1 (assoc id entries
))))
1053 (defun gnus-registry-delete-entries (idlist)
1054 (registry-delete gnus-registry-db idlist nil
))
1056 (defun gnus-registry-get-id-key (id key
)
1057 (cdr-safe (assq key
(gnus-registry-get-or-make-entry id
))))
1059 (defun gnus-registry-set-id-key (id key vals
)
1060 (let* ((db gnus-registry-db
)
1061 (entry (gnus-registry-get-or-make-entry id
)))
1062 (registry-delete db
(list id
) nil
)
1063 (setq entry
(cons (cons key vals
) (assq-delete-all key entry
)))
1064 (gnus-registry-insert db id entry
)
1067 (defun gnus-registry-insert (db id entry
)
1068 "Just like `registry-insert' but tries to prune on error."
1069 (when (registry-full db
)
1070 (message "Trying to prune the registry because it's full")
1072 db gnus-registry-default-sort-function
))
1073 (registry-insert db id entry
)
1076 (defun gnus-registry-import-eld (file)
1077 (interactive "fOld registry file to import? ")
1079 ;; (setq gnus-registry-alist '(
1080 ;; ("<messageID>" ((marks nil)
1081 ;; (mtime 19365 1776 440496)
1082 ;; (sender . "root (Cron Daemon)")
1083 ;; (subject . "Cron"))
1084 ;; "cron" "nnml+private:cron")
1086 (when (boundp 'gnus-registry-alist
)
1087 (let* ((old (symbol-value 'gnus-registry-alist
))
1089 (expected (length old
))
1091 (while (car-safe old
)
1093 ;; don't use progress reporters for backwards compatibility
1094 (when (and (< 0 expected
)
1095 (= 0 (mod count
100)))
1096 (message "importing: %d of %d (%.2f%%)"
1097 count expected
(/ (* 100.0 count
) expected
)))
1098 (setq entry
(car-safe old
)
1100 (let* ((id (car-safe entry
))
1101 (rest (cdr-safe entry
))
1102 (groups (loop for p in rest
1106 ;; remove all the strings from the entry
1108 (if (stringp elem
) (setq rest
(delq elem rest
))))
1109 (gnus-registry-set-id-key id
'group groups
)
1110 ;; just use the first extra element
1111 (setq rest
(car-safe rest
))
1112 (while (car-safe rest
)
1113 (setq extra-cell
(car-safe rest
)
1114 key
(car-safe extra-cell
)
1115 val
(cdr-safe extra-cell
)
1116 rest
(cdr-safe rest
))
1117 (when (and val
(atom val
))
1118 (setq val
(list val
)))
1119 (gnus-registry-set-id-key id key val
))))
1120 (message "Import done, collected %d entries" count
))))
1123 (defun gnus-registry-initialize ()
1124 "Initialize the Gnus registry."
1126 (gnus-message 5 "Initializing the registry")
1127 (gnus-registry-install-hooks)
1128 (gnus-registry-install-shortcuts)
1129 (gnus-registry-load))
1131 ;; FIXME: Why autoload this function?
1133 (defun gnus-registry-install-hooks ()
1134 "Install the registry hooks."
1136 (setq gnus-registry-enabled t
)
1137 (add-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1138 (add-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1139 (add-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1140 (add-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1142 (add-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1143 (add-hook 'gnus-read-newsrc-el-hook
'gnus-registry-load
)
1145 (add-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1147 (defun gnus-registry-unload-hook ()
1148 "Uninstall the registry hooks."
1150 (remove-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1151 (remove-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1152 (remove-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1153 (remove-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1155 (remove-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1156 (remove-hook 'gnus-read-newsrc-el-hook
'gnus-registry-load
)
1158 (remove-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
)
1159 (setq gnus-registry-enabled nil
))
1161 (add-hook 'gnus-registry-unload-hook
'gnus-registry-unload-hook
)
1163 (defun gnus-registry-install-p ()
1164 "Return non-nil if the registry is enabled (and maybe enable it first).
1165 If the registry is not already enabled, then if `gnus-registry-install'
1166 is `ask', ask the user; or if `gnus-registry-install' is non-nil, enable it."
1168 (unless gnus-registry-enabled
1169 (when (if (eq gnus-registry-install
'ask
)
1171 (concat "Enable the Gnus registry? "
1172 "See the variable `gnus-registry-install' "
1173 "to get rid of this query permanently. "))
1174 gnus-registry-install
)
1175 (gnus-registry-initialize)))
1176 gnus-registry-enabled
)
1178 ;; largely based on nnir-warp-to-article
1179 (defun gnus-try-warping-via-registry ()
1180 "Try to warp via the registry.
1181 This will be done via the current article's source group based on
1182 data stored in the registry."
1184 (when (gnus-summary-article-header)
1185 (let* ((message-id (mail-header-id (gnus-summary-article-header)))
1186 ;; Retrieve the message's group(s) from the registry
1187 (groups (gnus-registry-get-id-key message-id
'group
))
1188 ;; If starting from an ephemeral group, this describes
1189 ;; how to restore the window configuration
1191 (gnus-ephemeral-group-p gnus-newsgroup-name
))
1192 (seen-groups (list (gnus-group-group-name))))
1195 (dolist (group (mapcar 'gnus-simplify-group-name groups
))
1197 ;; skip over any groups we really don't want to warp to.
1198 (unless (or (member group seen-groups
)
1199 (gnus-ephemeral-group-p group
) ;; any ephemeral group
1200 (memq (car (gnus-find-method-for-group group
))
1201 ;; Specific methods; this list may need to expand.
1204 ;; remember that we've seen this group already
1205 (push group seen-groups
)
1207 ;; first exit from any ephemeral summary buffer.
1210 ;; and if the ephemeral summary buffer in turn came from
1211 ;; another summary buffer we have to clean that summary
1213 (when (eq (cdr quit-config
) 'summary
)
1214 (gnus-summary-exit))
1215 ;; remember that we've already done this part
1216 (setq quit-config nil
))
1218 ;; Try to activate the group. If that fails, just move
1219 ;; along. We may have more groups to work with
1222 (gnus-select-group-with-message-id group message-id
) t
)
1223 (throw 'found t
))))))))
1225 (defun gnus-registry-remove-extra-data (extra)
1226 "Remove tracked EXTRA data from the gnus registry.
1227 EXTRA is a list of symbols. Valid symbols are those contained in
1228 the docs of `gnus-registry-track-extra'. This command is useful
1229 when you stop tracking some extra data and now want to purge it
1230 from your existing entries."
1231 (interactive (list (mapcar 'intern
1232 (completing-read-multiple
1234 '("subject" "sender" "recipient")))))
1236 (let ((db gnus-registry-db
))
1237 (registry-reindex db
)
1238 (loop for k being the hash-keys of
(oref db data
)
1239 using
(hash-value v
)
1240 do
(let ((newv (delq nil
(mapcar #'(lambda (entry)
1241 (unless (member (car entry
) extra
)
1244 (registry-delete db
(list k
) nil
)
1245 (gnus-registry-insert db k newv
)))
1246 (registry-reindex db
))))
1248 ;; TODO: a few things
1250 (provide 'gnus-registry
)
1252 ;;; gnus-registry.el ends here