1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 2002-2018 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 <https://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-lib
))
80 (eval-when-compile (require 'subr-x
))
91 (defvar gnus-adaptive-word-syntax-table
)
93 (defvar gnus-registry-dirty t
94 "Boolean set to t when the registry is modified.")
96 (defgroup gnus-registry nil
101 (defvar gnus-registry-marks
104 :image
"summary_important")
107 :image
"summary_work")
110 :image
"summary_personal")
113 :image
"summary_todo")
116 :image
"summary_later"))
118 "List of registry marks and their options.
120 `gnus-registry-mark-article' will offer symbols from this list
123 Each entry must have a character to be useful for summary mode
124 line display and for keyboard shortcuts.
126 Each entry must have an image string to be useful for visual
129 (defcustom gnus-registry-default-mark
'To-Do
130 "The default mark. Should be a valid key for `gnus-registry-marks'."
131 :group
'gnus-registry
134 (defcustom gnus-registry-unfollowed-addresses
135 (list (regexp-quote user-mail-address
))
136 "List of addresses that gnus-registry-split-fancy-with-parent won't trace.
137 The addresses are matched, they don't have to be fully qualified.
138 In the messages, these addresses can be the sender or the
141 :group
'gnus-registry
142 :type
'(repeat regexp
))
144 (defcustom gnus-registry-unfollowed-groups
145 '("delayed$" "drafts$" "queue$" "INBOX$" "^nnmairix:" "archive")
146 "List of groups that gnus-registry-split-fancy-with-parent won't return.
147 The group names are matched, they don't have to be fully
148 qualified. This parameter tells the Gnus registry 'never split a
149 message into a group that matches one of these, regardless of
152 nnmairix groups are specifically excluded because they are ephemeral."
153 :group
'gnus-registry
154 :type
'(repeat regexp
))
156 (defcustom gnus-registry-install
'ask
157 "Whether the registry should be installed."
158 :group
'gnus-registry
159 :type
'(choice (const :tag
"Never Install" nil
)
160 (const :tag
"Always Install" t
)
161 (const :tag
"Ask Me" ask
)))
163 (defvar gnus-registry-enabled nil
)
165 (defvar gnus-summary-misc-menu
) ;; Avoid byte compiler warning.
167 (defvar gnus-registry-misc-menus nil
) ; ugly way to keep the menus
169 ;; It was simply deleted.
170 (make-obsolete-variable 'gnus-registry-max-pruned-entries nil
"25.1")
172 (defcustom gnus-registry-track-extra
'(subject sender recipient
)
173 "Whether the registry should track extra data about a message.
174 The subject, recipients (To: and Cc:), and Sender (From:) headers
175 are tracked this way by default."
176 :group
'gnus-registry
178 '(set :tag
"Tracking choices"
179 (const :tag
"Track by subject (Subject: header)" subject
)
180 (const :tag
"Track by recipient (To: and Cc: headers)" recipient
)
181 (const :tag
"Track by sender (From: header)" sender
)))
183 (defcustom gnus-registry-split-strategy nil
184 "The splitting strategy applied to the keys in `gnus-registry-track-extra'.
186 Given a set of unique found groups G and counts for each element
187 of G, and a key K (typically `sender' or `subject'):
189 When nil, if G has only one element, use it. Otherwise give up.
190 This is the fastest but also least useful strategy.
192 When `majority', use the majority by count. So if there is a
193 group with the most articles counted by K, use that. Ties are
194 resolved in no particular order, simply the first one found wins.
195 This is the slowest strategy but also the most accurate one.
197 When `first', the first element of G wins. This is fast and
198 should be OK if your senders and subjects don't \"bleed\" across
200 :group
'gnus-registry
202 '(choice :tag
"Splitting strategy"
203 (const :tag
"Only use single choices, discard multiple matches" nil
)
204 (const :tag
"Majority of matches wins" majority
)
205 (const :tag
"First found wins" first
)))
207 (defcustom gnus-registry-minimum-subject-length
5
208 "The minimum length of a subject before it's considered trackable."
209 :group
'gnus-registry
212 (defcustom gnus-registry-extra-entries-precious
'(mark)
213 "What extra keys are precious, meaning entries with them won't get pruned.
214 By default, 'mark is included, so articles with marks are
217 Before you save the Gnus registry, it's pruned. Any entries with
218 keys in this list will not be pruned. All other entries go to
220 :group
'gnus-registry
221 :type
'(repeat symbol
))
223 (defcustom gnus-registry-cache-file
225 (or gnus-dribble-directory gnus-home-directory
"~/")
226 ".gnus.registry.eieio")
227 "File where the Gnus registry will be stored."
228 :group
'gnus-registry
231 (defcustom gnus-registry-max-entries nil
232 "Maximum number of entries in the registry, nil for unlimited."
233 :group
'gnus-registry
234 :type
'(radio (const :format
"Unlimited " nil
)
235 (integer :format
"Maximum number: %v")))
237 (defcustom gnus-registry-prune-factor
0.1
238 "When pruning, try to prune back to this factor less than the maximum size.
240 In order to prevent constant pruning, we prune back to a number
241 somewhat less than the maximum size. This option controls
242 exactly how much less. For example, given a maximum size of
243 50000 and a prune factor of 0.1, the pruning process will try to
244 cut the registry back to \(- 50000 \(* 50000 0.1)) -> 45000
245 entries. The pruning process is constrained by the presence of
246 \"precious\" entries."
248 :group
'gnus-registry
251 (defcustom gnus-registry-default-sort-function
252 #'gnus-registry-sort-by-creation-time
253 "Sort function to use when pruning the registry.
254 Entries that sort to the front of the list are pruned first.
255 This can slow pruning down. Set to nil to perform no sorting."
257 :group
'gnus-registry
258 :type
'(choice (const :tag
"No sorting" nil
) function
))
260 (defun gnus-registry-sort-by-creation-time (l r
)
261 "Sort older entries to front of list."
262 ;; Pruning starts from the front of the list.
264 (cadr (assq 'creation-time r
))
265 (cadr (assq 'creation-time l
))))
267 (defun gnus-registry-fixup-registry (db)
269 (let ((old (oref db tracked
)))
270 (setf (oref db precious
)
271 (append gnus-registry-extra-entries-precious
273 (setf (oref db max-size
)
274 (or gnus-registry-max-entries
275 most-positive-fixnum
))
276 (setf (oref db prune-factor
)
277 (or gnus-registry-prune-factor
279 (setf (oref db tracked
)
280 (append gnus-registry-track-extra
281 '(mark group keyword
)))
282 (when (not (equal old
(oref db tracked
)))
283 (gnus-message 9 "Reindexing the Gnus registry (tracked change)")
284 (registry-reindex db
))))
287 (defun gnus-registry-make-db (&optional file
)
288 (interactive "fGnus registry persistence file: \n")
289 (gnus-registry-fixup-registry
290 (make-instance 'registry-db
291 :file
(or file gnus-registry-cache-file
)
292 ;; these parameters are set in `gnus-registry-fixup-registry'
293 :max-size most-positive-fixnum
294 :version registry-db-version
298 (defvar gnus-registry-db
(gnus-registry-make-db)
299 "The article registry by Message ID. See `registry-db'.")
301 ;; top-level registry data management
302 (defun gnus-registry-remake-db (&optional forsure
)
303 "Remake the registry database after customization.
304 This is not required after changing `gnus-registry-cache-file'."
305 (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
307 (gnus-message 4 "Remaking the Gnus registry")
308 (setq gnus-registry-db
(gnus-registry-make-db))))
310 (defun gnus-registry-load ()
311 "Load the registry from the cache file."
313 (let ((file gnus-registry-cache-file
))
315 (gnus-registry-read file
)
317 ;; Fix previous mis-naming of the registry file.
319 (concat (file-name-sans-extension
320 gnus-registry-cache-file
)
322 (if (and (file-exists-p old-file-name
)
324 (format "Rename registry file from %s to %s? "
325 old-file-name file
)))
327 (gnus-registry-read old-file-name
)
328 (setf (oref gnus-registry-db file
) file
)
329 (gnus-message 1 "Registry filename changed to %s" file
))
330 (gnus-registry-remake-db t
))))
334 "The Gnus registry could not be loaded from %s, creating a new one"
336 (gnus-registry-remake-db t
)))))
338 (defun gnus-registry-read (file)
339 "Do the actual reading of the registry persistence file."
340 (gnus-message 5 "Reading Gnus registry from %s..." file
)
341 (setq gnus-registry-db
342 (gnus-registry-fixup-registry
345 (eieio-persistent-read file
'registry-db
))
346 ;; Older EIEIO versions do not check the class name.
347 ('wrong-number-of-arguments
348 (eieio-persistent-read file
)))))
349 (gnus-message 5 "Reading Gnus registry from %s...done" file
))
351 (defun gnus-registry-save (&optional file db
)
352 "Save the registry cache file."
354 (let ((file (or file gnus-registry-cache-file
))
355 (db (or db gnus-registry-db
)))
356 (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
357 (registry-size db
) file
)
359 db gnus-registry-default-sort-function
)
360 ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
361 (eieio-persistent-save db file
)
362 (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
363 (registry-size db
) file
)))
365 (defun gnus-registry-remove-ignored ()
367 (let* ((db gnus-registry-db
)
368 (grouphashtb (registry-lookup-secondary db
'group
))
369 (old-size (registry-size db
)))
370 (registry-reindex db
)
371 (cl-loop for k being the hash-keys of grouphashtb
372 using
(hash-values v
)
373 when
(gnus-registry-ignore-group-p k
)
374 do
(registry-delete db v nil
))
375 (registry-reindex db
)
376 (gnus-message 4 "Removed %d ignored entries from the Gnus registry"
377 (- old-size
(registry-size db
)))))
379 ;; article move/copy/spool/delete actions
380 (defun gnus-registry-action (action data-header from
&optional to method
)
381 (let* ((id (mail-header-id data-header
))
382 (subject (mail-header-subject data-header
))
383 (extra (mail-header-extra data-header
))
384 (recipients (gnus-registry-sort-addresses
385 (or (cdr-safe (assq 'Cc extra
)) "")
386 (or (cdr-safe (assq 'To extra
)) "")))
387 (sender (nth 0 (gnus-registry-extract-addresses
388 (mail-header-from data-header
))))
389 (from (gnus-group-guess-full-name-from-command-method from
))
390 (to (if to
(gnus-group-guess-full-name-from-command-method to
) nil
)))
391 (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
392 id
(if method
"respooling" "going") from to
)
394 (gnus-registry-handle-action
396 ;; unless copying, remove the old "from" group
397 (if (not (equal 'copy action
)) from nil
)
398 to subject sender recipients
)))
400 (defun gnus-registry-spool-action (id group
&optional subject sender recipients
)
401 (let ((to (gnus-group-guess-full-name-from-command-method group
))
402 (recipients (or recipients
403 (gnus-registry-sort-addresses
404 (or (message-fetch-field "cc") "")
405 (or (message-fetch-field "to") ""))))
406 (subject (or subject
(message-fetch-field "subject")))
407 (sender (or sender
(message-fetch-field "from"))))
408 (when (and (stringp id
) (string-match "\r$" id
))
409 (setq id
(substring id
0 -
1)))
410 (gnus-message 7 "Gnus registry: article %s spooled to %s"
413 (gnus-registry-handle-action id nil to subject sender recipients
)))
415 (defun gnus-registry-handle-action (id from to subject sender
416 &optional recipients
)
419 "gnus-registry-handle-action %S" (list id from to subject sender recipients
))
420 (let ((db gnus-registry-db
)
421 ;; if the group is ignored, set the destination to nil (same as delete)
422 (to (if (gnus-registry-ignore-group-p to
) nil to
))
424 (entry (gnus-registry-get-or-make-entry id
))
425 (subject (gnus-string-remove-all-properties
426 (gnus-registry-simplify-subject subject
)))
427 (sender (gnus-string-remove-all-properties sender
)))
429 ;; this could be done by calling `gnus-registry-set-id-key'
430 ;; several times but it's better to bunch the transactions
433 (registry-delete db
(list id
) nil
)
435 (setq entry
(cons (delete from
(assoc 'group entry
))
436 (assq-delete-all 'group entry
))))
438 (dolist (kv `((group ,to
)
440 (recipient ,@recipients
)
443 (let ((new (or (assq (car kv
) entry
)
445 (dolist (toadd (cdr kv
))
446 (unless (member toadd new
)
447 (setq new
(append new
(list toadd
)))))
448 (setq entry
(cons new
449 (assq-delete-all (car kv
) entry
))))))
450 (gnus-message 10 "Gnus registry: new entry for %s is %S"
453 (gnus-registry-insert db id entry
)))
455 ;; Function for nn{mail|imap}-split-fancy: look up all references in
456 ;; the cache and if a match is found, return that group.
457 (defun gnus-registry-split-fancy-with-parent ()
458 "Split this message into the same group as its parent.
459 The parent is obtained from the registry. This function can be used as an
460 entry in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
461 this: (: gnus-registry-split-fancy-with-parent)
463 This function tracks ALL backends, unlike
464 `nnmail-split-fancy-with-parent' which tracks only nnmail
467 For a message to be split, it looks for the parent message in the
468 References or In-Reply-To header and then looks in the registry
469 to see which group that message was put in. This group is
470 returned, unless `gnus-registry-follow-group-p' return nil for
473 See the Info node `(gnus)Fancy Mail Splitting' for more details."
474 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
475 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
476 ;; now, if reply-to is valid, append it to the References
478 (concat refstr
" " reply-to
)
480 (references (and refstr
(gnus-extract-references refstr
)))
481 ;; these may not be used, but the code is cleaner having them up here
482 (sender (gnus-string-remove-all-properties
483 (message-fetch-field "from")))
484 (recipients (gnus-registry-sort-addresses
485 (or (message-fetch-field "cc") "")
486 (or (message-fetch-field "to") "")))
487 (subject (gnus-string-remove-all-properties
488 (gnus-registry-simplify-subject
489 (message-fetch-field "subject"))))
491 (nnmail-split-fancy-with-parent-ignore-groups
492 (if (listp nnmail-split-fancy-with-parent-ignore-groups
)
493 nnmail-split-fancy-with-parent-ignore-groups
494 (list nnmail-split-fancy-with-parent-ignore-groups
))))
495 (gnus-registry--split-fancy-with-parent-internal
496 :references references
499 :recipients recipients
501 :log-agent
"Gnus registry fancy splitting with parent")))
503 (cl-defun gnus-registry--split-fancy-with-parent-internal
505 &key references refstr sender subject recipients log-agent
509 "gnus-registry--split-fancy-with-parent-internal %S" spec
)
510 (let ((db gnus-registry-db
)
512 ;; this is a big chain of statements. it uses
513 ;; gnus-registry-post-process-groups to filter the results after
515 ;; the references string must be valid and parse to valid references
519 "%s is tracing references %s"
521 (dolist (reference (nreverse references
))
522 (gnus-message 9 "%s is looking up %s" log-agent reference
)
523 (cl-loop for group in
(gnus-registry-get-id-key reference
'group
)
524 when
(gnus-registry-follow-group-p group
)
527 (gnus-message 7 "%s traced %s to %s" log-agent reference group
)
528 (push group found
))))
529 ;; filter the found groups and return them
530 ;; the found groups are the full groups
531 (setq found
(gnus-registry-post-process-groups
532 "references" refstr found
)))
534 ;; else: there were no matches, now try the extra tracking by subject
535 (when (and (null found
)
536 (memq 'subject gnus-registry-track-extra
)
538 (< gnus-registry-minimum-subject-length
(length subject
)))
543 (gnus-registry-get-id-key reference
'group
))
544 (registry-lookup-secondary-value db
'subject subject
)))))
546 (cl-loop for group in groups
547 when
(gnus-registry-follow-group-p group
)
549 ;; warn more if gnus-registry-track-extra
550 (if gnus-registry-track-extra
7 9)
551 "%s (extra tracking) traced subject `%s' to %s"
552 log-agent subject group
)
554 ;; filter the found groups and return them
555 ;; the found groups are NOT the full groups
556 (setq found
(gnus-registry-post-process-groups
557 "subject" subject found
))))
559 ;; else: there were no matches, try the extra tracking by sender
560 (when (and (null found
)
561 (memq 'sender gnus-registry-track-extra
)
563 (not (gnus-grep-in-list
565 gnus-registry-unfollowed-addresses
)))
570 (gnus-registry-get-id-key reference
'group
))
571 (registry-lookup-secondary-value db
'sender sender
)))))
573 (cl-loop for group in groups
574 when
(gnus-registry-follow-group-p group
)
576 ;; warn more if gnus-registry-track-extra
577 (if gnus-registry-track-extra
7 9)
578 "%s (extra tracking) traced sender `%s' to %s"
579 log-agent sender group
)
582 ;; filter the found groups and return them
583 ;; the found groups are NOT the full groups
584 (setq found
(gnus-registry-post-process-groups
585 "sender" sender found
)))
587 ;; else: there were no matches, try the extra tracking by recipient
588 (when (and (null found
)
589 (memq 'recipient gnus-registry-track-extra
)
591 (dolist (recp recipients
)
592 (when (and (null found
)
593 (not (gnus-grep-in-list
595 gnus-registry-unfollowed-addresses
)))
596 (let ((groups (apply 'append
599 (gnus-registry-get-id-key reference
'group
))
600 (registry-lookup-secondary-value
601 db
'recipient recp
)))))
603 (cl-loop for group in groups
604 when
(gnus-registry-follow-group-p group
)
606 ;; warn more if gnus-registry-track-extra
607 (if gnus-registry-track-extra
7 9)
608 "%s (extra tracking) traced recipient `%s' to %s"
609 log-agent recp group
)
610 and collect group
)))))
612 ;; filter the found groups and return them
613 ;; the found groups are NOT the full groups
614 (setq found
(gnus-registry-post-process-groups
615 "recipients" (mapconcat 'identity recipients
", ") found
)))
617 ;; after the (cond) we extract the actual value safely
620 (defun gnus-registry-post-process-groups (mode key groups
)
621 "Inspects GROUPS found by MODE for KEY to determine which ones to follow.
623 MODE can be `subject' or `sender' for example. The KEY is the
624 value by which MODE was searched.
626 Transforms each group name to the equivalent short name.
628 Checks if the current Gnus method (from `gnus-command-method' or
629 from `gnus-newsgroup-name') is the same as the group's method.
630 Foreign methods are not supported so they are rejected.
632 Reduces the list to a single group, or complains if that's not
633 possible. Uses `gnus-registry-split-strategy'."
634 (let ((log-agent "gnus-registry-post-process-group")
635 (desc (format "%d groups" (length groups
)))
637 ;; the strategy can be nil, in which case chosen is nil
639 (cl-case gnus-registry-split-strategy
640 ;; default, take only one-element lists into chosen
642 (and (= (length groups
) 1)
649 (let ((freq (make-hash-table
652 (mapc (lambda (x) (let ((x (gnus-group-short-name x
)))
653 (puthash x
(1+ (gethash x freq
0)) freq
)))
655 (setq desc
(format "%d groups, %d unique"
657 (hash-table-count freq
)))
661 (> (gethash (gnus-group-short-name a
) freq
0)
662 (gethash (gnus-group-short-name b
) freq
0)))))))))
667 "%s: strategy %s on %s produced %s"
668 log-agent gnus-registry-split-strategy desc chosen
)
671 "%s: strategy %s on %s did not produce an answer"
673 (or gnus-registry-split-strategy
"default")
676 (setq groups
(and chosen
(list chosen
)))
678 (dolist (group groups
)
679 (let ((m1 (gnus-find-method-for-group group
))
680 (m2 (or gnus-command-method
681 (gnus-find-method-for-group gnus-newsgroup-name
)))
682 (short-name (gnus-group-short-name group
)))
683 (if (gnus-methods-equal-p m1 m2
)
685 ;; this is REALLY just for debugging
686 (when (not (equal group short-name
))
689 "%s: stripped group %s to %s"
690 log-agent group short-name
))
691 (cl-pushnew short-name out
:test
#'equal
))
695 "%s: ignored foreign group %s"
698 (setq out
(delq nil out
))
701 ((= (length out
) 1) out
)
705 "%s: no matches for %s `%s'."
710 "%s: too many extra matches (%s) for %s `%s'. Returning none."
711 log-agent out mode key
)
714 (defun gnus-registry-follow-group-p (group)
715 "Determines if a group name should be followed.
716 Consults `gnus-registry-unfollowed-groups' and
717 `nnmail-split-fancy-with-parent-ignore-groups'."
719 (not (or (gnus-grep-in-list
721 gnus-registry-unfollowed-groups
)
724 nnmail-split-fancy-with-parent-ignore-groups
)))))
726 ;; note that gnus-registry-ignored-groups is defined in gnus.el as a
727 ;; group/topic parameter and an associated variable!
729 ;; we do special logic for ignoring to accept regular expressions and
730 ;; nnmail-split-fancy-with-parent-ignore-groups as well
731 (defun gnus-registry-ignore-group-p (group)
732 "Determines if a group name should be ignored.
733 Consults `gnus-registry-ignored-groups' and
734 `nnmail-split-fancy-with-parent-ignore-groups'."
736 (or (gnus-grep-in-list
738 (delq nil
(mapcar (lambda (g)
741 ((and (listp g
) (nth 1 g
))
743 (t nil
))) gnus-registry-ignored-groups
)))
744 ;; only use `gnus-parameter-registry-ignore' if
745 ;; `gnus-registry-ignored-groups' is a list of lists
746 ;; (it can be a list of regexes)
747 (and (listp (nth 0 gnus-registry-ignored-groups
))
748 (get-buffer "*Group*") ; in automatic tests this is false
749 (gnus-parameter-registry-ignore group
))
752 nnmail-split-fancy-with-parent-ignore-groups
))))
754 (defun gnus-registry-wash-for-keywords (&optional force
)
755 "Get the keywords of the current article.
756 Overrides existing keywords with FORCE set non-nil."
758 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article
))
760 (if (or (not (gnus-registry-get-id-key id
'keyword
))
762 (with-current-buffer gnus-article-buffer
764 (save-window-excursion
766 (narrow-to-region (point) (point-max))
767 (with-syntax-table gnus-adaptive-word-syntax-table
768 (while (re-search-forward "\\b\\w+\\b" nil t
)
769 (setq word
(gnus-string-remove-all-properties
770 (downcase (buffer-substring
771 (match-beginning 0) (match-end 0)))))
772 (if (> (length word
) 2)
773 (push word words
))))))
774 (gnus-registry-set-id-key id
'keyword words
)))))
776 (defun gnus-registry-keywords ()
777 (let ((table (registry-lookup-secondary gnus-registry-db
'keyword
))
779 (when table
(maphash (lambda (k _v
) (push k ks
)) table
) ks
)))
781 (defun gnus-registry-find-keywords (keyword)
783 (completing-read "Keyword: " (gnus-registry-keywords) nil t
)))
784 (registry-lookup-secondary-value gnus-registry-db
'keyword keyword
))
786 (defun gnus-registry-register-message-ids ()
787 "Register the Message-ID of every article in the group."
788 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name
)
789 (dolist (article gnus-newsgroup-articles
)
790 (let* ((id (gnus-registry-fetch-message-id-fast article
))
791 (groups (gnus-registry-get-id-key id
'group
)))
792 (unless (member gnus-newsgroup-name groups
)
793 (gnus-message 9 "Registry: Registering article %d with group %s"
794 article gnus-newsgroup-name
)
795 (gnus-registry-handle-action id nil gnus-newsgroup-name
796 (gnus-registry-fetch-simplified-message-subject-fast article
)
797 (gnus-registry-fetch-sender-fast article
)
798 (gnus-registry-fetch-recipients-fast article
)))))))
800 ;; message field fetchers
801 (defun gnus-registry-fetch-message-id-fast (article)
802 "Fetch the Message-ID quickly, using the internal gnus-data-list function."
803 (if (and (numberp article
)
804 (assoc article
(gnus-data-list nil
)))
805 (mail-header-id (gnus-data-header (assoc article
(gnus-data-list nil
))))
808 (defun gnus-registry-extract-addresses (text)
809 "Extract all the addresses in a normalized way from TEXT.
810 Returns an unsorted list of strings in the name <address> format.
811 Addresses without a name will say \"noname\"."
812 (mapcar (lambda (add)
813 (gnus-string-remove-all-properties
814 (let* ((name (or (nth 0 add
) "noname"))
816 (addr (if (bufferp addr
)
817 (with-current-buffer addr
820 (format "%s <%s>" name addr
))))
821 (mail-extract-address-components text t
)))
823 (defun gnus-registry-sort-addresses (&rest addresses
)
824 "Return a normalized and sorted list of ADDRESSES."
825 (sort (mapcan 'gnus-registry-extract-addresses addresses
) 'string-lessp
))
827 (defun gnus-registry-simplify-subject (subject)
828 (if (stringp subject
)
829 (gnus-simplify-subject subject
)
832 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
833 "Fetch the Subject quickly, using the internal gnus-data-list function."
834 (if (and (numberp article
)
835 (assoc article
(gnus-data-list nil
)))
836 (gnus-string-remove-all-properties
837 (gnus-registry-simplify-subject
838 (mail-header-subject (gnus-data-header
839 (assoc article
(gnus-data-list nil
))))))
842 (defun gnus-registry-fetch-sender-fast (article)
843 (when-let* ((data (and (numberp article
)
844 (assoc article
(gnus-data-list nil
)))))
845 (mail-header-from (gnus-data-header data
))))
847 (defun gnus-registry-fetch-recipients-fast (article)
848 (when-let* ((data (and (numberp article
)
849 (assoc article
(gnus-data-list nil
))))
850 (extra (mail-header-extra (gnus-data-header data
))))
851 (gnus-registry-sort-addresses
852 (or (cdr (assq 'Cc extra
)) "")
853 (or (cdr (assq 'To extra
)) ""))))
855 ;; registry marks glue
856 (defun gnus-registry-do-marks (type function
)
857 "For each known mark, call FUNCTION for each cell of type TYPE.
859 FUNCTION should take two parameters, a mark symbol and the cell value."
860 (dolist (mark-info gnus-registry-marks
)
861 (let* ((mark (car-safe mark-info
))
862 (data (cdr-safe mark-info
))
863 (cell-data (plist-get data type
)))
865 (funcall function mark cell-data
)))))
867 ;; FIXME: Why not merge gnus-registry--set/remove-mark and
868 ;; gnus-registry-set-article-mark-internal?
869 (defun gnus-registry--set/remove-mark
(mark remove articles
)
870 "Set/remove the MARK over process-marked ARTICLES."
871 ;; If this is called and the user doesn't want the
872 ;; registry enabled, we'll ask anyhow.
873 (unless gnus-registry-install
874 (let ((gnus-registry-install 'ask
))
875 (gnus-registry-install-p)))
877 ;; Now the user is asked if gnus-registry-install is `ask'.
878 (when (gnus-registry-install-p)
879 (gnus-registry-set-article-mark-internal
880 ;; All this just to get the mark, I must be doing it wrong.
881 mark articles remove t
)
882 ;; FIXME: Why do we do the above only here and not directly inside
883 ;; gnus-registry-set-article-mark-internal? I.e. we wouldn't we want to do
884 ;; the things below when gnus-registry-set-article-mark-internal is called
885 ;; from gnus-registry-set-article-mark or
886 ;; gnus-registry-remove-article-mark?
887 (gnus-message 9 "Applying mark %s to %d articles"
888 mark
(length articles
))
889 (dolist (article articles
)
890 (gnus-summary-update-article
892 (assoc article
(gnus-data-list nil
))))))
894 ;; This is ugly code, but I don't know how to do it better.
895 (defun gnus-registry-install-shortcuts ()
896 "Install the keyboard shortcuts and menus for the registry.
897 Uses `gnus-registry-marks' to find what shortcuts to install."
899 (setq gnus-registry-misc-menus nil
)
900 (gnus-registry-do-marks
903 (let ((function-format
904 (format "gnus-registry-%%s-article-%s-mark" mark
)))
906 ;;; The following generates these functions:
907 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
908 ;;; "Apply the Important mark to process-marked ARTICLES."
909 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
910 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
911 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
912 ;;; "Apply the Important mark to process-marked ARTICLES."
913 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
914 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
916 (dolist (remove '(t nil
))
917 (let* ((variant-name (if remove
"remove" "set"))
919 (intern (format function-format variant-name
)))
920 (shortcut (format "%c" (if remove
(upcase data
) data
))))
921 (defalias function-name
922 ;; If it weren't for the function's docstring, we could
923 ;; use a closure, with lexical-let :-(
924 `(lambda (&rest articles
)
926 "%s the %s mark over process-marked ARTICLES."
927 (upcase-initials variant-name
)
930 (gnus-summary-work-articles current-prefix-arg
))
931 (gnus-registry--set/remove-mark
',mark
',remove articles
)))
932 (push function-name keys-plist
)
933 (push shortcut keys-plist
)
934 (push (vector (format "%s %s"
935 (upcase-initials variant-name
)
938 gnus-registry-misc-menus
)
939 (gnus-message 9 "Defined mark handling function %s"
942 '(gnus-registry-mark-map "M" gnus-summary-mark-map
)
944 (add-hook 'gnus-summary-menu-hook
947 gnus-summary-misc-menu
949 (cons "Registry Marks" gnus-registry-misc-menus
))))))
951 (make-obsolete 'gnus-registry-user-format-function-M
952 'gnus-registry-article-marks-to-chars
"24.1") ?
954 (defalias 'gnus-registry-user-format-function-M
955 'gnus-registry-article-marks-to-chars
)
958 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
959 (defun gnus-registry-article-marks-to-chars (headers)
960 "Show the marks for an article by the :char property."
961 (let* ((id (mail-header-message-id headers
))
962 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
967 (cdr-safe (assoc m gnus-registry-marks
))
972 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
973 (defun gnus-registry-article-marks-to-names (headers)
974 "Show the marks for an article by name."
975 (let* ((id (mail-header-message-id headers
))
976 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
977 (mapconcat (lambda (mark) (symbol-name mark
)) marks
",")))
979 (defun gnus-registry-read-mark ()
980 "Read a mark name from the user with completion."
981 (let ((mark (gnus-completing-read
983 (mapcar 'symbol-name
(mapcar 'car gnus-registry-marks
))
985 (symbol-name gnus-registry-default-mark
))))
989 (defun gnus-registry-set-article-mark (&rest articles
)
990 "Apply a mark to process-marked ARTICLES."
991 (interactive (gnus-summary-work-articles current-prefix-arg
))
992 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
995 (defun gnus-registry-remove-article-mark (&rest articles
)
996 "Remove a mark from process-marked ARTICLES."
997 (interactive (gnus-summary-work-articles current-prefix-arg
))
998 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
1001 (defun gnus-registry-set-article-mark-internal (mark
1005 "Apply or remove MARK across a list of ARTICLES."
1006 (let ((article-id-list
1007 (mapcar 'gnus-registry-fetch-message-id-fast articles
)))
1008 (dolist (id article-id-list
)
1009 (let* ((marks (delq mark
(gnus-registry-get-id-key id
'mark
)))
1010 (marks (if remove marks
(cons mark marks
))))
1012 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
1013 (if remove
"Removing" "Adding")
1015 (gnus-registry-set-id-key id
'mark marks
)))))
1017 (defun gnus-registry-get-article-marks (&rest articles
)
1018 "Get the Gnus registry marks for ARTICLES and show them if interactive.
1019 Uses process/prefix conventions. For multiple articles,
1020 only the last one's marks are returned."
1021 (interactive (gnus-summary-work-articles 1))
1022 (let* ((article (last articles
))
1023 (id (gnus-registry-fetch-message-id-fast article
))
1024 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
1025 (when (called-interactively-p 'any
)
1026 (gnus-message 1 "Marks are %S" marks
))
1029 (defun gnus-registry-group-count (id)
1030 "Get the number of groups of a message, based on the message ID."
1031 (length (gnus-registry-get-id-key id
'group
)))
1033 (defun gnus-registry-get-or-make-entry (id)
1034 (let* ((db gnus-registry-db
)
1035 ;; safe if not found
1036 (entries (registry-lookup db
(list id
))))
1038 (when (null entries
)
1039 (gnus-registry-insert db id
(list (list 'creation-time
(current-time))
1040 '(group) '(sender) '(subject)))
1041 (setq entries
(registry-lookup db
(list id
))))
1043 (nth 1 (assoc id entries
))))
1045 (defun gnus-registry-delete-entries (idlist)
1046 (registry-delete gnus-registry-db idlist nil
))
1048 (defun gnus-registry-get-id-key (id key
)
1049 (cdr-safe (assq key
(gnus-registry-get-or-make-entry id
))))
1051 (defun gnus-registry-set-id-key (id key vals
)
1052 (let* ((db gnus-registry-db
)
1053 (entry (gnus-registry-get-or-make-entry id
)))
1054 (registry-delete db
(list id
) nil
)
1055 (setq entry
(cons (cons key vals
) (assq-delete-all key entry
)))
1056 (gnus-registry-insert db id entry
)
1059 (defun gnus-registry-insert (db id entry
)
1060 "Just like `registry-insert' but tries to prune on error."
1061 (when (registry-full db
)
1062 (message "Trying to prune the registry because it's full")
1064 db gnus-registry-default-sort-function
))
1065 (registry-insert db id entry
)
1068 (defun gnus-registry-import-eld (file)
1069 (interactive "fOld registry file to import? ")
1071 ;; (setq gnus-registry-alist '(
1072 ;; ("<messageID>" ((marks nil)
1073 ;; (mtime 19365 1776 440496)
1074 ;; (sender . "root (Cron Daemon)")
1075 ;; (subject . "Cron"))
1076 ;; "cron" "nnml+private:cron")
1078 (when (boundp 'gnus-registry-alist
)
1079 (let* ((old (symbol-value 'gnus-registry-alist
))
1081 (expected (length old
))
1083 (while (car-safe old
)
1085 ;; don't use progress reporters for backwards compatibility
1086 (when (and (< 0 expected
)
1087 (= 0 (mod count
100)))
1088 (message "importing: %d of %d (%.2f%%)"
1089 count expected
(/ (* 100.0 count
) expected
)))
1090 (setq entry
(car-safe old
)
1092 (let* ((id (car-safe entry
))
1093 (rest (cdr-safe entry
))
1094 (groups (cl-loop for p in rest
1098 ;; remove all the strings from the entry
1100 (if (stringp elem
) (setq rest
(delq elem rest
))))
1101 (gnus-registry-set-id-key id
'group groups
)
1102 ;; just use the first extra element
1103 (setq rest
(car-safe rest
))
1104 (while (car-safe rest
)
1105 (setq extra-cell
(car-safe rest
)
1106 key
(car-safe extra-cell
)
1107 val
(cdr-safe extra-cell
)
1108 rest
(cdr-safe rest
))
1109 (when (and val
(atom val
))
1110 (setq val
(list val
)))
1111 (gnus-registry-set-id-key id key val
))))
1112 (message "Import done, collected %d entries" count
))))
1115 (defun gnus-registry-initialize ()
1116 "Initialize the Gnus registry."
1118 (gnus-message 5 "Initializing the registry")
1119 (gnus-registry-install-hooks)
1120 (gnus-registry-install-shortcuts)
1121 (gnus-registry-load))
1123 ;; FIXME: Why autoload this function?
1125 (defun gnus-registry-install-hooks ()
1126 "Install the registry hooks."
1128 (setq gnus-registry-enabled t
)
1129 (add-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1130 (add-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1131 (add-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1132 (add-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1134 (add-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1135 (add-hook 'gnus-read-newsrc-el-hook
'gnus-registry-load
)
1137 (add-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1139 (defun gnus-registry-unload-hook ()
1140 "Uninstall the registry hooks."
1142 (remove-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1143 (remove-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1144 (remove-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1145 (remove-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1147 (remove-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1148 (remove-hook 'gnus-read-newsrc-el-hook
'gnus-registry-load
)
1150 (remove-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
)
1151 (setq gnus-registry-enabled nil
))
1153 (add-hook 'gnus-registry-unload-hook
'gnus-registry-unload-hook
)
1155 (defun gnus-registry-install-p ()
1156 "Return non-nil if the registry is enabled (and maybe enable it first).
1157 If the registry is not already enabled, then if `gnus-registry-install'
1158 is `ask', ask the user; or if `gnus-registry-install' is non-nil, enable it."
1160 (unless gnus-registry-enabled
1161 (when (if (eq gnus-registry-install
'ask
)
1163 (concat "Enable the Gnus registry? "
1164 "See the variable `gnus-registry-install' "
1165 "to get rid of this query permanently. "))
1166 gnus-registry-install
)
1167 (gnus-registry-initialize)))
1168 gnus-registry-enabled
)
1170 ;; largely based on nnir-warp-to-article
1171 (defun gnus-try-warping-via-registry ()
1172 "Try to warp via the registry.
1173 This will be done via the current article's source group based on
1174 data stored in the registry."
1176 (when (gnus-summary-article-header)
1177 (let* ((message-id (mail-header-id (gnus-summary-article-header)))
1178 ;; Retrieve the message's group(s) from the registry
1179 (groups (gnus-registry-get-id-key message-id
'group
))
1180 ;; If starting from an ephemeral group, this describes
1181 ;; how to restore the window configuration
1183 (gnus-ephemeral-group-p gnus-newsgroup-name
))
1184 (seen-groups (list (gnus-group-group-name))))
1187 (dolist (group (mapcar 'gnus-simplify-group-name groups
))
1189 ;; skip over any groups we really don't want to warp to.
1190 (unless (or (member group seen-groups
)
1191 (gnus-ephemeral-group-p group
) ;; any ephemeral group
1192 (memq (car (gnus-find-method-for-group group
))
1193 ;; Specific methods; this list may need to expand.
1196 ;; remember that we've seen this group already
1197 (push group seen-groups
)
1199 ;; first exit from any ephemeral summary buffer.
1202 ;; and if the ephemeral summary buffer in turn came from
1203 ;; another summary buffer we have to clean that summary
1205 (when (eq (cdr quit-config
) 'summary
)
1206 (gnus-summary-exit))
1207 ;; remember that we've already done this part
1208 (setq quit-config nil
))
1210 ;; Try to activate the group. If that fails, just move
1211 ;; along. We may have more groups to work with
1214 (gnus-select-group-with-message-id group message-id
) t
)
1215 (throw 'found t
))))))))
1217 (defun gnus-registry-remove-extra-data (extra)
1218 "Remove tracked EXTRA data from the gnus registry.
1219 EXTRA is a list of symbols. Valid symbols are those contained in
1220 the docs of `gnus-registry-track-extra'. This command is useful
1221 when you stop tracking some extra data and now want to purge it
1222 from your existing entries."
1223 (interactive (list (mapcar 'intern
1224 (completing-read-multiple
1226 '("subject" "sender" "recipient")))))
1228 (let ((db gnus-registry-db
))
1229 (registry-reindex db
)
1230 (cl-loop for k being the hash-keys of
(oref db data
)
1231 using
(hash-value v
)
1232 do
(let ((newv (delq nil
(mapcar #'(lambda (entry)
1233 (unless (member (car entry
) extra
)
1236 (registry-delete db
(list k
) nil
)
1237 (gnus-registry-insert db k newv
)))
1238 (registry-reindex db
))))
1240 ;; TODO: a few things
1242 (provide 'gnus-registry
)
1244 ;;; gnus-registry.el ends here