1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 2002-2015 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 ;; Silence XEmacs byte compiler, which will otherwise complain about
91 ;; call to `eieio-persistent-read'.
92 (when (featurep 'xemacs
)
93 (byte-compiler-options
94 (warnings (- callargs
))))
96 (defvar gnus-adaptive-word-syntax-table
)
98 (defvar gnus-registry-dirty t
99 "Boolean set to t when the registry is modified.")
101 (defgroup gnus-registry nil
106 (defvar gnus-registry-marks
109 :image
"summary_important")
112 :image
"summary_work")
115 :image
"summary_personal")
118 :image
"summary_todo")
121 :image
"summary_later"))
123 "List of registry marks and their options.
125 `gnus-registry-mark-article' will offer symbols from this list
128 Each entry must have a character to be useful for summary mode
129 line display and for keyboard shortcuts.
131 Each entry must have an image string to be useful for visual
134 (defcustom gnus-registry-default-mark
'To-Do
135 "The default mark. Should be a valid key for `gnus-registry-marks'."
136 :group
'gnus-registry
139 (defcustom gnus-registry-unfollowed-addresses
140 (list (regexp-quote user-mail-address
))
141 "List of addresses that gnus-registry-split-fancy-with-parent won't trace.
142 The addresses are matched, they don't have to be fully qualified.
143 In the messages, these addresses can be the sender or the
146 :group
'gnus-registry
147 :type
'(repeat regexp
))
149 (defcustom gnus-registry-unfollowed-groups
150 '("delayed$" "drafts$" "queue$" "INBOX$" "^nnmairix:" "archive")
151 "List of groups that gnus-registry-split-fancy-with-parent won't return.
152 The group names are matched, they don't have to be fully
153 qualified. This parameter tells the Gnus registry 'never split a
154 message into a group that matches one of these, regardless of
157 nnmairix groups are specifically excluded because they are ephemeral."
158 :group
'gnus-registry
159 :type
'(repeat regexp
))
161 (defcustom gnus-registry-install
'ask
162 "Whether the registry should be installed."
163 :group
'gnus-registry
164 :type
'(choice (const :tag
"Never Install" nil
)
165 (const :tag
"Always Install" t
)
166 (const :tag
"Ask Me" ask
)))
168 (defvar gnus-registry-enabled nil
)
170 (defvar gnus-summary-misc-menu
) ;; Avoid byte compiler warning.
172 (defvar gnus-registry-misc-menus nil
) ; ugly way to keep the menus
174 (make-obsolete-variable 'gnus-registry-clean-empty nil
"23.4")
175 (make-obsolete-variable 'gnus-registry-use-long-group-names nil
"23.4")
176 (make-obsolete-variable 'gnus-registry-max-track-groups nil
"23.4")
177 (make-obsolete-variable 'gnus-registry-entry-caching nil
"23.4")
178 (make-obsolete-variable 'gnus-registry-trim-articles-without-groups nil
"23.4")
179 ;; FIXME it was simply deleted.
180 (make-obsolete-variable 'gnus-registry-max-pruned-entries nil
"25.1")
182 (defcustom gnus-registry-track-extra
'(subject sender recipient
)
183 "Whether the registry should track extra data about a message.
184 The subject, recipients (To: and Cc:), and Sender (From:) headers
185 are tracked this way by default."
186 :group
'gnus-registry
188 '(set :tag
"Tracking choices"
189 (const :tag
"Track by subject (Subject: header)" subject
)
190 (const :tag
"Track by recipient (To: and Cc: headers)" recipient
)
191 (const :tag
"Track by sender (From: header)" sender
)))
193 (defcustom gnus-registry-split-strategy nil
194 "The splitting strategy applied to the keys in `gnus-registry-track-extra'.
196 Given a set of unique found groups G and counts for each element
197 of G, and a key K (typically 'sender or 'subject):
199 When nil, if G has only one element, use it. Otherwise give up.
200 This is the fastest but also least useful strategy.
202 When 'majority, use the majority by count. So if there is a
203 group with the most articles counted by K, use that. Ties are
204 resolved in no particular order, simply the first one found wins.
205 This is the slowest strategy but also the most accurate one.
207 When 'first, the first element of G wins. This is fast and
208 should be OK if your senders and subjects don't \"bleed\" across
210 :group
'gnus-registry
212 '(choice :tag
"Splitting strategy"
213 (const :tag
"Only use single choices, discard multiple matches" nil
)
214 (const :tag
"Majority of matches wins" majority
)
215 (const :tag
"First found wins" first
)))
217 (defcustom gnus-registry-minimum-subject-length
5
218 "The minimum length of a subject before it's considered trackable."
219 :group
'gnus-registry
222 (defcustom gnus-registry-extra-entries-precious
'(mark)
223 "What extra keys are precious, meaning entries with them won't get pruned.
224 By default, 'mark is included, so articles with marks are
227 Before you save the Gnus registry, it's pruned. Any entries with
228 keys in this list will not be pruned. All other entries go to
230 :group
'gnus-registry
231 :type
'(repeat symbol
))
233 (defcustom gnus-registry-cache-file
235 (or gnus-dribble-directory gnus-home-directory
"~/")
236 ".gnus.registry.eieio")
237 "File where the Gnus registry will be stored."
238 :group
'gnus-registry
241 (defcustom gnus-registry-max-entries nil
242 "Maximum number of entries in the registry, nil for unlimited."
243 :group
'gnus-registry
244 :type
'(radio (const :format
"Unlimited " nil
)
245 (integer :format
"Maximum number: %v")))
247 (defcustom gnus-registry-prune-factor
0.1
248 "When pruning, try to prune back to this factor less than the maximum size.
250 In order to prevent constant pruning, we prune back to a number
251 somewhat less than the maximum size. This option controls
252 exactly how much less. For example, given a maximum size of
253 50000 and a prune factor of 0.1, the pruning process will try to
254 cut the registry back to \(- 50000 \(* 50000 0.1\)\) -> 45000
255 entries. The pruning process is constrained by the presence of
256 \"precious\" entries."
258 :group
'gnus-registry
261 (defcustom gnus-registry-default-sort-function
262 #'gnus-registry-sort-by-creation-time
263 "Sort function to use when pruning the registry.
264 Entries that sort to the front of the list are pruned first.
265 This can slow pruning down. Set to nil to perform no sorting."
267 :group
'gnus-registry
268 :type
'(choice (const :tag
"No sorting" nil
) function
))
270 (defun gnus-registry-sort-by-creation-time (l r
)
271 "Sort older entries to front of list."
272 ;; Pruning starts from the front of the list.
274 (cadr (assq 'creation-time r
))
275 (cadr (assq 'creation-time l
))))
277 (defun gnus-registry-fixup-registry (db)
279 (let ((old (oref db
:tracked
)))
281 (append gnus-registry-extra-entries-precious
284 (or gnus-registry-max-entries
285 most-positive-fixnum
))
286 (oset db
:prune-factor
287 (or gnus-registry-prune-factor
290 (append gnus-registry-track-extra
291 '(mark group keyword
)))
292 (when (not (equal old
(oref db
:tracked
)))
293 (gnus-message 9 "Reindexing the Gnus registry (tracked change)")
294 (registry-reindex db
))))
297 (defun gnus-registry-make-db (&optional file
)
298 (interactive "fGnus registry persistence file: \n")
299 (gnus-registry-fixup-registry
302 :file
(or file gnus-registry-cache-file
)
303 ;; these parameters are set in `gnus-registry-fixup-registry'
304 :max-size most-positive-fixnum
305 :version registry-db-version
309 (defvar gnus-registry-db
(gnus-registry-make-db)
310 "The article registry by Message ID. See `registry-db'.")
312 ;; top-level registry data management
313 (defun gnus-registry-remake-db (&optional forsure
)
314 "Remake the registry database after customization.
315 This is not required after changing `gnus-registry-cache-file'."
316 (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
318 (gnus-message 4 "Remaking the Gnus registry")
319 (setq gnus-registry-db
(gnus-registry-make-db))))
321 (defun gnus-registry-load ()
322 "Load the registry from the cache file."
324 (let ((file gnus-registry-cache-file
))
326 (gnus-registry-read file
)
328 ;; Fix previous mis-naming of the registry file.
330 (concat (file-name-sans-extension
331 gnus-registry-cache-file
)
333 (if (and (file-exists-p old-file-name
)
335 (format "Rename registry file from %s to %s? "
336 old-file-name file
)))
338 (gnus-registry-read old-file-name
)
339 (oset gnus-registry-db
:file file
)
340 (gnus-message 1 "Registry filename changed to %s" file
))
341 (gnus-registry-remake-db t
))))
345 "The Gnus registry could not be loaded from %s, creating a new one"
347 (gnus-registry-remake-db t
)))))
349 (defun gnus-registry-read (file)
350 "Do the actual reading of the registry persistence file."
351 (gnus-message 5 "Reading Gnus registry from %s..." file
)
352 (setq gnus-registry-db
353 (gnus-registry-fixup-registry
356 (eieio-persistent-read file
'registry-db
))
357 ;; Older EIEIO versions do not check the class name.
358 ('wrong-number-of-arguments
359 (eieio-persistent-read file
)))))
360 (gnus-message 5 "Reading Gnus registry from %s...done" file
))
362 (defun gnus-registry-save (&optional file db
)
363 "Save the registry cache file."
365 (let ((file (or file gnus-registry-cache-file
))
366 (db (or db gnus-registry-db
)))
367 (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
368 (registry-size db
) file
)
370 db gnus-registry-default-sort-function
)
371 ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
372 (eieio-persistent-save db file
)
373 (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
374 (registry-size db
) file
)))
376 (defun gnus-registry-remove-ignored ()
378 (let* ((db gnus-registry-db
)
379 (grouphashtb (registry-lookup-secondary db
'group
))
380 (old-size (registry-size db
)))
381 (registry-reindex db
)
382 (loop for k being the hash-keys of grouphashtb
383 using
(hash-values v
)
384 when
(gnus-registry-ignore-group-p k
)
385 do
(registry-delete db v nil
))
386 (registry-reindex db
)
387 (gnus-message 4 "Removed %d ignored entries from the Gnus registry"
388 (- old-size
(registry-size db
)))))
390 ;; article move/copy/spool/delete actions
391 (defun gnus-registry-action (action data-header from
&optional to method
)
392 (let* ((id (mail-header-id data-header
))
393 (subject (mail-header-subject data-header
))
394 (extra (mail-header-extra data-header
))
395 (recipients (gnus-registry-sort-addresses
396 (or (cdr-safe (assq 'Cc extra
)) "")
397 (or (cdr-safe (assq 'To extra
)) "")))
398 (sender (nth 0 (gnus-registry-extract-addresses
399 (mail-header-from data-header
))))
400 (from (gnus-group-guess-full-name-from-command-method from
))
401 (to (if to
(gnus-group-guess-full-name-from-command-method to
) nil
))
402 (to-name (if to to
"the Bit Bucket")))
403 (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
404 id
(if method
"respooling" "going") from to
)
406 (gnus-registry-handle-action
408 ;; unless copying, remove the old "from" group
409 (if (not (equal 'copy action
)) from nil
)
410 to subject sender recipients
)))
412 (defun gnus-registry-spool-action (id group
&optional subject sender recipients
)
413 (let ((to (gnus-group-guess-full-name-from-command-method group
))
414 (recipients (or recipients
415 (gnus-registry-sort-addresses
416 (or (message-fetch-field "cc") "")
417 (or (message-fetch-field "to") ""))))
418 (subject (or subject
(message-fetch-field "subject")))
419 (sender (or sender
(message-fetch-field "from"))))
420 (when (and (stringp id
) (string-match "\r$" id
))
421 (setq id
(substring id
0 -
1)))
422 (gnus-message 7 "Gnus registry: article %s spooled to %s"
425 (gnus-registry-handle-action id nil to subject sender recipients
)))
427 (defun gnus-registry-handle-action (id from to subject sender
428 &optional recipients
)
431 "gnus-registry-handle-action %S" (list id from to subject sender recipients
))
432 (let ((db gnus-registry-db
)
433 ;; if the group is ignored, set the destination to nil (same as delete)
434 (to (if (gnus-registry-ignore-group-p to
) nil to
))
436 (entry (gnus-registry-get-or-make-entry id
))
437 (subject (gnus-string-remove-all-properties
438 (gnus-registry-simplify-subject subject
)))
439 (sender (gnus-string-remove-all-properties sender
)))
441 ;; this could be done by calling `gnus-registry-set-id-key'
442 ;; several times but it's better to bunch the transactions
445 (registry-delete db
(list id
) nil
)
447 (setq entry
(cons (delete from
(assoc 'group entry
))
448 (assq-delete-all 'group entry
))))
450 (dolist (kv `((group ,to
)
452 (recipient ,@recipients
)
455 (let ((new (or (assq (first kv
) entry
)
457 (dolist (toadd (cdr kv
))
458 (add-to-list 'new toadd t
))
459 (setq entry
(cons new
460 (assq-delete-all (first kv
) entry
))))))
461 (gnus-message 10 "Gnus registry: new entry for %s is %S"
464 (gnus-registry-insert db id entry
)))
466 ;; Function for nn{mail|imap}-split-fancy: look up all references in
467 ;; the cache and if a match is found, return that group.
468 (defun gnus-registry-split-fancy-with-parent ()
469 "Split this message into the same group as its parent.
470 The parent is obtained from the registry. This function can be used as an
471 entry in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
472 this: (: gnus-registry-split-fancy-with-parent)
474 This function tracks ALL backends, unlike
475 `nnmail-split-fancy-with-parent' which tracks only nnmail
478 For a message to be split, it looks for the parent message in the
479 References or In-Reply-To header and then looks in the registry
480 to see which group that message was put in. This group is
481 returned, unless `gnus-registry-follow-group-p' return nil for
484 See the Info node `(gnus)Fancy Mail Splitting' for more details."
485 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
486 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
487 ;; now, if reply-to is valid, append it to the References
489 (concat refstr
" " reply-to
)
491 (references (and refstr
(gnus-extract-references refstr
)))
492 ;; these may not be used, but the code is cleaner having them up here
493 (sender (gnus-string-remove-all-properties
494 (message-fetch-field "from")))
495 (recipients (gnus-registry-sort-addresses
496 (or (message-fetch-field "cc") "")
497 (or (message-fetch-field "to") "")))
498 (subject (gnus-string-remove-all-properties
499 (gnus-registry-simplify-subject
500 (message-fetch-field "subject"))))
502 (nnmail-split-fancy-with-parent-ignore-groups
503 (if (listp nnmail-split-fancy-with-parent-ignore-groups
)
504 nnmail-split-fancy-with-parent-ignore-groups
505 (list nnmail-split-fancy-with-parent-ignore-groups
))))
506 (gnus-registry--split-fancy-with-parent-internal
507 :references references
510 :recipients recipients
512 :log-agent
"Gnus registry fancy splitting with parent")))
514 (defun* gnus-registry--split-fancy-with-parent-internal
516 &key references refstr sender subject recipients log-agent
520 "gnus-registry--split-fancy-with-parent-internal %S" spec
)
521 (let ((db gnus-registry-db
)
523 ;; this is a big chain of statements. it uses
524 ;; gnus-registry-post-process-groups to filter the results after
526 ;; the references string must be valid and parse to valid references
530 "%s is tracing references %s"
532 (dolist (reference (nreverse references
))
533 (gnus-message 9 "%s is looking up %s" log-agent reference
)
534 (loop for group in
(gnus-registry-get-id-key reference
'group
)
535 when
(gnus-registry-follow-group-p group
)
538 (gnus-message 7 "%s traced %s to %s" log-agent reference group
)
539 (push group found
))))
540 ;; filter the found groups and return them
541 ;; the found groups are the full groups
542 (setq found
(gnus-registry-post-process-groups
543 "references" refstr found
)))
545 ;; else: there were no matches, now try the extra tracking by subject
546 (when (and (null found
)
547 (memq 'subject gnus-registry-track-extra
)
549 (< gnus-registry-minimum-subject-length
(length subject
)))
554 (gnus-registry-get-id-key reference
'group
))
555 (registry-lookup-secondary-value db
'subject subject
)))))
557 (loop for group in groups
558 when
(gnus-registry-follow-group-p group
)
560 ;; warn more if gnus-registry-track-extra
561 (if gnus-registry-track-extra
7 9)
562 "%s (extra tracking) traced subject '%s' to %s"
563 log-agent subject group
)
565 ;; filter the found groups and return them
566 ;; the found groups are NOT the full groups
567 (setq found
(gnus-registry-post-process-groups
568 "subject" subject found
))))
570 ;; else: there were no matches, try the extra tracking by sender
571 (when (and (null found
)
572 (memq 'sender gnus-registry-track-extra
)
574 (not (gnus-grep-in-list
576 gnus-registry-unfollowed-addresses
)))
581 (gnus-registry-get-id-key reference
'group
))
582 (registry-lookup-secondary-value db
'sender sender
)))))
584 (loop for group in groups
585 when
(gnus-registry-follow-group-p group
)
587 ;; warn more if gnus-registry-track-extra
588 (if gnus-registry-track-extra
7 9)
589 "%s (extra tracking) traced sender '%s' to %s"
590 log-agent sender group
)
593 ;; filter the found groups and return them
594 ;; the found groups are NOT the full groups
595 (setq found
(gnus-registry-post-process-groups
596 "sender" sender found
)))
598 ;; else: there were no matches, try the extra tracking by recipient
599 (when (and (null found
)
600 (memq 'recipient gnus-registry-track-extra
)
602 (dolist (recp recipients
)
603 (when (and (null found
)
604 (not (gnus-grep-in-list
606 gnus-registry-unfollowed-addresses
)))
607 (let ((groups (apply 'append
610 (gnus-registry-get-id-key reference
'group
))
611 (registry-lookup-secondary-value
612 db
'recipient recp
)))))
614 (loop for group in groups
615 when
(gnus-registry-follow-group-p group
)
617 ;; warn more if gnus-registry-track-extra
618 (if gnus-registry-track-extra
7 9)
619 "%s (extra tracking) traced recipient '%s' to %s"
620 log-agent recp group
)
621 and collect group
)))))
623 ;; filter the found groups and return them
624 ;; the found groups are NOT the full groups
625 (setq found
(gnus-registry-post-process-groups
626 "recipients" (mapconcat 'identity recipients
", ") found
)))
628 ;; after the (cond) we extract the actual value safely
631 (defun gnus-registry-post-process-groups (mode key groups
)
632 "Inspects GROUPS found by MODE for KEY to determine which ones to follow.
634 MODE can be 'subject' or 'sender' for example. The KEY is the
635 value by which MODE was searched.
637 Transforms each group name to the equivalent short name.
639 Checks if the current Gnus method (from `gnus-command-method' or
640 from `gnus-newsgroup-name') is the same as the group's method.
641 Foreign methods are not supported so they are rejected.
643 Reduces the list to a single group, or complains if that's not
644 possible. Uses `gnus-registry-split-strategy'."
645 (let ((log-agent "gnus-registry-post-process-group")
646 (desc (format "%d groups" (length groups
)))
648 ;; the strategy can be nil, in which case chosen is nil
650 (case gnus-registry-split-strategy
651 ;; default, take only one-element lists into chosen
653 (and (= (length groups
) 1)
660 (let ((freq (make-hash-table
663 (mapc (lambda (x) (let ((x (gnus-group-short-name x
)))
664 (puthash x
(1+ (gethash x freq
0)) freq
)))
666 (setq desc
(format "%d groups, %d unique"
668 (hash-table-count freq
)))
672 (> (gethash (gnus-group-short-name a
) freq
0)
673 (gethash (gnus-group-short-name b
) freq
0)))))))))
678 "%s: strategy %s on %s produced %s"
679 log-agent gnus-registry-split-strategy desc chosen
)
682 "%s: strategy %s on %s did not produce an answer"
684 (or gnus-registry-split-strategy
"default")
687 (setq groups
(and chosen
(list chosen
)))
689 (dolist (group groups
)
690 (let ((m1 (gnus-find-method-for-group group
))
691 (m2 (or gnus-command-method
692 (gnus-find-method-for-group gnus-newsgroup-name
)))
693 (short-name (gnus-group-short-name group
)))
694 (if (gnus-methods-equal-p m1 m2
)
696 ;; this is REALLY just for debugging
697 (when (not (equal group short-name
))
700 "%s: stripped group %s to %s"
701 log-agent group short-name
))
702 (add-to-list 'out short-name
))
706 "%s: ignored foreign group %s"
709 (setq out
(delq nil out
))
712 ((= (length out
) 1) out
)
716 "%s: no matches for %s '%s'."
721 "%s: too many extra matches (%s) for %s '%s'. Returning none."
722 log-agent out mode key
)
725 (defun gnus-registry-follow-group-p (group)
726 "Determines if a group name should be followed.
727 Consults `gnus-registry-unfollowed-groups' and
728 `nnmail-split-fancy-with-parent-ignore-groups'."
730 (not (or (gnus-grep-in-list
732 gnus-registry-unfollowed-groups
)
735 nnmail-split-fancy-with-parent-ignore-groups
)))))
737 ;; note that gnus-registry-ignored-groups is defined in gnus.el as a
738 ;; group/topic parameter and an associated variable!
740 ;; we do special logic for ignoring to accept regular expressions and
741 ;; nnmail-split-fancy-with-parent-ignore-groups as well
742 (defun gnus-registry-ignore-group-p (group)
743 "Determines if a group name should be ignored.
744 Consults `gnus-registry-ignored-groups' and
745 `nnmail-split-fancy-with-parent-ignore-groups'."
747 (or (gnus-grep-in-list
749 (delq nil
(mapcar (lambda (g)
752 ((and (listp g
) (nth 1 g
))
754 (t nil
))) gnus-registry-ignored-groups
)))
755 ;; only use `gnus-parameter-registry-ignore' if
756 ;; `gnus-registry-ignored-groups' is a list of lists
757 ;; (it can be a list of regexes)
758 (and (listp (nth 0 gnus-registry-ignored-groups
))
759 (get-buffer "*Group*") ; in automatic tests this is false
760 (gnus-parameter-registry-ignore group
))
763 nnmail-split-fancy-with-parent-ignore-groups
))))
765 (defun gnus-registry-wash-for-keywords (&optional force
)
766 "Get the keywords of the current article.
767 Overrides existing keywords with FORCE set non-nil."
769 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article
))
771 (if (or (not (gnus-registry-get-id-key id
'keyword
))
773 (with-current-buffer gnus-article-buffer
775 (save-window-excursion
777 (narrow-to-region (point) (point-max))
778 (with-syntax-table gnus-adaptive-word-syntax-table
779 (while (re-search-forward "\\b\\w+\\b" nil t
)
780 (setq word
(gnus-string-remove-all-properties
781 (downcase (buffer-substring
782 (match-beginning 0) (match-end 0)))))
783 (if (> (length word
) 2)
784 (push word words
))))))
785 (gnus-registry-set-id-key id
'keyword words
)))))
787 (defun gnus-registry-keywords ()
788 (let ((table (registry-lookup-secondary gnus-registry-db
'keyword
)))
789 (when table
(maphash (lambda (k v
) k
) table
))))
791 (defun gnus-registry-find-keywords (keyword)
793 (completing-read "Keyword: " (gnus-registry-keywords) nil t
)))
794 (registry-lookup-secondary-value gnus-registry-db
'keyword keyword
))
796 (defun gnus-registry-register-message-ids ()
797 "Register the Message-ID of every article in the group."
798 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name
)
799 (dolist (article gnus-newsgroup-articles
)
800 (let* ((id (gnus-registry-fetch-message-id-fast article
))
801 (groups (gnus-registry-get-id-key id
'group
)))
802 (unless (member gnus-newsgroup-name groups
)
803 (gnus-message 9 "Registry: Registering article %d with group %s"
804 article gnus-newsgroup-name
)
805 (gnus-registry-handle-action id nil gnus-newsgroup-name
806 (gnus-registry-fetch-simplified-message-subject-fast article
)
807 (gnus-registry-fetch-sender-fast article
)
808 (gnus-registry-fetch-recipients-fast article
)))))))
810 ;; message field fetchers
811 (defun gnus-registry-fetch-message-id-fast (article)
812 "Fetch the Message-ID quickly, using the internal gnus-data-list function."
813 (if (and (numberp article
)
814 (assoc article
(gnus-data-list nil
)))
815 (mail-header-id (gnus-data-header (assoc article
(gnus-data-list nil
))))
818 (defun gnus-registry-extract-addresses (text)
819 "Extract all the addresses in a normalized way from TEXT.
820 Returns an unsorted list of strings in the name <address> format.
821 Addresses without a name will say \"noname\"."
822 (mapcar (lambda (add)
823 (gnus-string-remove-all-properties
824 (let* ((name (or (nth 0 add
) "noname"))
826 (addr (if (bufferp addr
)
827 (with-current-buffer addr
830 (format "%s <%s>" name addr
))))
831 (mail-extract-address-components text t
)))
833 (defun gnus-registry-sort-addresses (&rest addresses
)
834 "Return a normalized and sorted list of ADDRESSES."
835 (sort (apply 'nconc
(mapcar 'gnus-registry-extract-addresses addresses
))
838 (defun gnus-registry-simplify-subject (subject)
839 (if (stringp subject
)
840 (gnus-simplify-subject subject
)
843 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
844 "Fetch the Subject quickly, using the internal gnus-data-list function."
845 (if (and (numberp article
)
846 (assoc article
(gnus-data-list nil
)))
847 (gnus-string-remove-all-properties
848 (gnus-registry-simplify-subject
849 (mail-header-subject (gnus-data-header
850 (assoc article
(gnus-data-list nil
))))))
853 (defun gnus-registry-fetch-sender-fast (article)
854 (gnus-registry-fetch-header-fast "from" article
))
856 (defun gnus-registry-fetch-recipients-fast (article)
857 (gnus-registry-sort-addresses
858 (or (ignore-errors (gnus-registry-fetch-header-fast "Cc" article
)) "")
859 (or (ignore-errors (gnus-registry-fetch-header-fast "To" article
)) "")))
861 (defun gnus-registry-fetch-header-fast (article header
)
862 "Fetch the HEADER quickly, using the internal gnus-data-list function."
863 (if (and (numberp article
)
864 (assoc article
(gnus-data-list nil
)))
865 (gnus-string-remove-all-properties
866 (cdr (assq header
(gnus-data-header
867 (assoc article
(gnus-data-list nil
))))))
870 ;; registry marks glue
871 (defun gnus-registry-do-marks (type function
)
872 "For each known mark, call FUNCTION for each cell of type TYPE.
874 FUNCTION should take two parameters, a mark symbol and the cell value."
875 (dolist (mark-info gnus-registry-marks
)
876 (let* ((mark (car-safe mark-info
))
877 (data (cdr-safe mark-info
))
878 (cell-data (plist-get data type
)))
880 (funcall function mark cell-data
)))))
882 ;; FIXME: Why not merge gnus-registry--set/remove-mark and
883 ;; gnus-registry-set-article-mark-internal?
884 (defun gnus-registry--set/remove-mark
(remove mark articles
)
885 "Set/remove the MARK over process-marked ARTICLES."
886 ;; If this is called and the user doesn't want the
887 ;; registry enabled, we'll ask anyhow.
888 (unless gnus-registry-install
889 (let ((gnus-registry-install 'ask
))
890 (gnus-registry-install-p)))
892 ;; Now the user is asked if gnus-registry-install is `ask'.
893 (when (gnus-registry-install-p)
894 (gnus-registry-set-article-mark-internal
895 ;; All this just to get the mark, I must be doing it wrong.
896 mark articles remove t
)
897 ;; FIXME: Why do we do the above only here and not directly inside
898 ;; gnus-registry-set-article-mark-internal? I.e. we wouldn't we want to do
899 ;; the things below when gnus-registry-set-article-mark-internal is called
900 ;; from gnus-registry-set-article-mark or
901 ;; gnus-registry-remove-article-mark?
902 (gnus-message 9 "Applying mark %s to %d articles"
903 mark
(length articles
))
904 (dolist (article articles
)
905 (gnus-summary-update-article
907 (assoc article
(gnus-data-list nil
))))))
909 ;; This is ugly code, but I don't know how to do it better.
910 (defun gnus-registry-install-shortcuts ()
911 "Install the keyboard shortcuts and menus for the registry.
912 Uses `gnus-registry-marks' to find what shortcuts to install."
914 (setq gnus-registry-misc-menus nil
)
915 (gnus-registry-do-marks
918 (let ((function-format
919 (format "gnus-registry-%%s-article-%s-mark" mark
)))
921 ;;; The following generates these functions:
922 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
923 ;;; "Apply the Important mark to process-marked ARTICLES."
924 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
925 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
926 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
927 ;;; "Apply the Important mark to process-marked ARTICLES."
928 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
929 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
931 (dolist (remove '(t nil
))
932 (let* ((variant-name (if remove
"remove" "set"))
934 (intern (format function-format variant-name
)))
935 (shortcut (format "%c" (if remove
(upcase data
) data
))))
936 (defalias function-name
937 ;; If it weren't for the function's docstring, we could
938 ;; use a closure, with lexical-let :-(
939 `(lambda (&rest articles
)
941 "%s the %s mark over process-marked ARTICLES."
942 (upcase-initials variant-name
)
945 (gnus-summary-work-articles current-prefix-arg
))
946 (gnus-registry--set/remove-mark
',mark
',remove articles
)))
947 (push function-name keys-plist
)
948 (push shortcut keys-plist
)
949 (push (vector (format "%s %s"
950 (upcase-initials variant-name
)
953 gnus-registry-misc-menus
)
954 (gnus-message 9 "Defined mark handling function %s"
957 '(gnus-registry-mark-map "M" gnus-summary-mark-map
)
959 (add-hook 'gnus-summary-menu-hook
962 gnus-summary-misc-menu
964 (cons "Registry Marks" gnus-registry-misc-menus
))))))
966 (make-obsolete 'gnus-registry-user-format-function-M
967 'gnus-registry-article-marks-to-chars
"24.1") ?
969 (defalias 'gnus-registry-user-format-function-M
970 'gnus-registry-article-marks-to-chars
)
973 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
974 (defun gnus-registry-article-marks-to-chars (headers)
975 "Show the marks for an article by the :char property."
976 (let* ((id (mail-header-message-id headers
))
977 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
978 (mapconcat (lambda (mark)
981 (assoc mark gnus-registry-marks
))
986 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
987 (defun gnus-registry-article-marks-to-names (headers)
988 "Show the marks for an article by name."
989 (let* ((id (mail-header-message-id headers
))
990 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
991 (mapconcat (lambda (mark) (symbol-name mark
)) marks
",")))
993 (defun gnus-registry-read-mark ()
994 "Read a mark name from the user with completion."
995 (let ((mark (gnus-completing-read
997 (mapcar 'symbol-name
(mapcar 'car gnus-registry-marks
))
999 (symbol-name gnus-registry-default-mark
))))
1000 (when (stringp mark
)
1003 (defun gnus-registry-set-article-mark (&rest articles
)
1004 "Apply a mark to 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-remove-article-mark (&rest articles
)
1010 "Remove a mark from process-marked ARTICLES."
1011 (interactive (gnus-summary-work-articles current-prefix-arg
))
1012 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
1015 (defun gnus-registry-set-article-mark-internal (mark
1019 "Apply or remove MARK across a list of ARTICLES."
1020 (let ((article-id-list
1021 (mapcar 'gnus-registry-fetch-message-id-fast articles
)))
1022 (dolist (id article-id-list
)
1023 (let* ((marks (delq mark
(gnus-registry-get-id-key id
'mark
)))
1024 (marks (if remove marks
(cons mark marks
))))
1026 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
1027 (if remove
"Removing" "Adding")
1029 (gnus-registry-set-id-key id
'mark marks
)))))
1031 (defun gnus-registry-get-article-marks (&rest articles
)
1032 "Get the Gnus registry marks for ARTICLES and show them if interactive.
1033 Uses process/prefix conventions. For multiple articles,
1034 only the last one's marks are returned."
1035 (interactive (gnus-summary-work-articles 1))
1036 (let* ((article (last articles
))
1037 (id (gnus-registry-fetch-message-id-fast article
))
1038 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
1039 (when (gmm-called-interactively-p 'any
)
1040 (gnus-message 1 "Marks are %S" marks
))
1043 (defun gnus-registry-group-count (id)
1044 "Get the number of groups of a message, based on the message ID."
1045 (length (gnus-registry-get-id-key id
'group
)))
1047 (defun gnus-registry-get-or-make-entry (id)
1048 (let* ((db gnus-registry-db
)
1049 ;; safe if not found
1050 (entries (registry-lookup db
(list id
))))
1052 (when (null entries
)
1053 (gnus-registry-insert db id
(list (list 'creation-time
(current-time))
1054 '(group) '(sender) '(subject)))
1055 (setq entries
(registry-lookup db
(list id
))))
1057 (nth 1 (assoc id entries
))))
1059 (defun gnus-registry-delete-entries (idlist)
1060 (registry-delete gnus-registry-db idlist nil
))
1062 (defun gnus-registry-get-id-key (id key
)
1063 (cdr-safe (assq key
(gnus-registry-get-or-make-entry id
))))
1065 (defun gnus-registry-set-id-key (id key vals
)
1066 (let* ((db gnus-registry-db
)
1067 (entry (gnus-registry-get-or-make-entry id
)))
1068 (registry-delete db
(list id
) nil
)
1069 (setq entry
(cons (cons key vals
) (assq-delete-all key entry
)))
1070 (gnus-registry-insert db id entry
)
1073 (defun gnus-registry-insert (db id entry
)
1074 "Just like `registry-insert' but tries to prune on error."
1075 (when (registry-full db
)
1076 (message "Trying to prune the registry because it's full")
1078 db gnus-registry-default-sort-function
))
1079 (registry-insert db id entry
)
1082 (defun gnus-registry-import-eld (file)
1083 (interactive "fOld registry file to import? ")
1085 ;; (setq gnus-registry-alist '(
1086 ;; ("<messageID>" ((marks nil)
1087 ;; (mtime 19365 1776 440496)
1088 ;; (sender . "root (Cron Daemon)")
1089 ;; (subject . "Cron"))
1090 ;; "cron" "nnml+private:cron")
1092 (when (boundp 'gnus-registry-alist
)
1093 (let* ((old (symbol-value 'gnus-registry-alist
))
1095 (expected (length old
))
1097 (while (car-safe old
)
1099 ;; don't use progress reporters for backwards compatibility
1100 (when (and (< 0 expected
)
1101 (= 0 (mod count
100)))
1102 (message "importing: %d of %d (%.2f%%)"
1103 count expected
(/ (* 100 count
) expected
)))
1104 (setq entry
(car-safe old
)
1106 (let* ((id (car-safe entry
))
1107 (new-entry (gnus-registry-get-or-make-entry id
))
1108 (rest (cdr-safe entry
))
1109 (groups (loop for p in rest
1113 ;; remove all the strings from the entry
1115 (if (stringp elem
) (setq rest
(delq elem rest
))))
1116 (gnus-registry-set-id-key id
'group groups
)
1117 ;; just use the first extra element
1118 (setq rest
(car-safe rest
))
1119 (while (car-safe rest
)
1120 (setq extra-cell
(car-safe rest
)
1121 key
(car-safe extra-cell
)
1122 val
(cdr-safe extra-cell
)
1123 rest
(cdr-safe rest
))
1124 (when (and val
(atom val
))
1125 (setq val
(list val
)))
1126 (gnus-registry-set-id-key id key val
))))
1127 (message "Import done, collected %d entries" count
))))
1130 (defun gnus-registry-initialize ()
1131 "Initialize the Gnus registry."
1133 (gnus-message 5 "Initializing the registry")
1134 (gnus-registry-install-hooks)
1135 (gnus-registry-install-shortcuts)
1136 (gnus-registry-load))
1138 ;; FIXME: Why autoload this function?
1140 (defun gnus-registry-install-hooks ()
1141 "Install the registry hooks."
1143 (setq gnus-registry-enabled t
)
1144 (add-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1145 (add-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1146 (add-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1147 (add-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1149 (add-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1150 (add-hook 'gnus-read-newsrc-el-hook
'gnus-registry-load
)
1152 (add-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1154 (defun gnus-registry-unload-hook ()
1155 "Uninstall the registry hooks."
1157 (remove-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1158 (remove-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1159 (remove-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1160 (remove-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1162 (remove-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1163 (remove-hook 'gnus-read-newsrc-el-hook
'gnus-registry-load
)
1165 (remove-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
)
1166 (setq gnus-registry-enabled nil
))
1168 (add-hook 'gnus-registry-unload-hook
'gnus-registry-unload-hook
)
1170 (defun gnus-registry-install-p ()
1171 "Return non-nil if the registry is enabled (and maybe enable it first).
1172 If the registry is not already enabled, then if `gnus-registry-install'
1173 is `ask', ask the user; or if `gnus-registry-install' is non-nil, enable it."
1175 (unless gnus-registry-enabled
1176 (when (if (eq gnus-registry-install
'ask
)
1178 (concat "Enable the Gnus registry? "
1179 "See the variable `gnus-registry-install' "
1180 "to get rid of this query permanently. "))
1181 gnus-registry-install
)
1182 (gnus-registry-initialize)))
1183 gnus-registry-enabled
)
1185 ;; largely based on nnir-warp-to-article
1186 (defun gnus-try-warping-via-registry ()
1187 "Try to warp via the registry.
1188 This will be done via the current article's source group based on
1189 data stored in the registry."
1191 (when (gnus-summary-article-header)
1192 (let* ((message-id (mail-header-id (gnus-summary-article-header)))
1193 ;; Retrieve the message's group(s) from the registry
1194 (groups (gnus-registry-get-id-key message-id
'group
))
1195 ;; If starting from an ephemeral group, this describes
1196 ;; how to restore the window configuration
1198 (gnus-ephemeral-group-p gnus-newsgroup-name
))
1199 (seen-groups (list (gnus-group-group-name))))
1202 (dolist (group (mapcar 'gnus-simplify-group-name groups
))
1204 ;; skip over any groups we really don't want to warp to.
1205 (unless (or (member group seen-groups
)
1206 (gnus-ephemeral-group-p group
) ;; any ephemeral group
1207 (memq (car (gnus-find-method-for-group group
))
1208 ;; Specific methods; this list may need to expand.
1211 ;; remember that we've seen this group already
1212 (push group seen-groups
)
1214 ;; first exit from any ephemeral summary buffer.
1217 ;; and if the ephemeral summary buffer in turn came from
1218 ;; another summary buffer we have to clean that summary
1220 (when (eq (cdr quit-config
) 'summary
)
1221 (gnus-summary-exit))
1222 ;; remember that we've already done this part
1223 (setq quit-config nil
))
1225 ;; Try to activate the group. If that fails, just move
1226 ;; along. We may have more groups to work with
1229 (gnus-select-group-with-message-id group message-id
) t
)
1230 (throw 'found t
))))))))
1232 (defun gnus-registry-remove-extra-data (extra)
1233 "Remove tracked EXTRA data from the gnus registry.
1234 EXTRA is a list of symbols. Valid symbols are those contained in
1235 the docs of `gnus-registry-track-extra'. This command is useful
1236 when you stop tracking some extra data and now want to purge it
1237 from your existing entries."
1238 (interactive (list (mapcar 'intern
1239 (completing-read-multiple
1241 '("subject" "sender" "recipient")))))
1243 (let ((db gnus-registry-db
))
1244 (registry-reindex db
)
1245 (loop for k being the hash-keys of
(oref db
:data
)
1246 using
(hash-value v
)
1247 do
(let ((newv (delq nil
(mapcar #'(lambda (entry)
1248 (unless (member (car entry
) extra
)
1251 (registry-delete db
(list k
) nil
)
1252 (gnus-registry-insert db k newv
)))
1253 (registry-reindex db
))))
1255 ;; TODO: a few things
1257 (provide 'gnus-registry
)
1259 ;;; gnus-registry.el ends here