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
)))
280 (setf (oref db precious
)
281 (append gnus-registry-extra-entries-precious
283 (setf (oref db max-size
)
284 (or gnus-registry-max-entries
285 most-positive-fixnum
))
286 (setf (oref db prune-factor
)
287 (or gnus-registry-prune-factor
289 (setf (oref db tracked
)
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
300 (make-instance 'registry-db
301 :file
(or file gnus-registry-cache-file
)
302 ;; these parameters are set in `gnus-registry-fixup-registry'
303 :max-size most-positive-fixnum
304 :version registry-db-version
308 (defvar gnus-registry-db
(gnus-registry-make-db)
309 "The article registry by Message ID. See `registry-db'.")
311 ;; top-level registry data management
312 (defun gnus-registry-remake-db (&optional forsure
)
313 "Remake the registry database after customization.
314 This is not required after changing `gnus-registry-cache-file'."
315 (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
317 (gnus-message 4 "Remaking the Gnus registry")
318 (setq gnus-registry-db
(gnus-registry-make-db))))
320 (defun gnus-registry-load ()
321 "Load the registry from the cache file."
323 (let ((file gnus-registry-cache-file
))
325 (gnus-registry-read file
)
327 ;; Fix previous mis-naming of the registry file.
329 (concat (file-name-sans-extension
330 gnus-registry-cache-file
)
332 (if (and (file-exists-p old-file-name
)
334 (format "Rename registry file from %s to %s? "
335 old-file-name file
)))
337 (gnus-registry-read old-file-name
)
338 (setf (oref gnus-registry-db file
) file
)
339 (gnus-message 1 "Registry filename changed to %s" file
))
340 (gnus-registry-remake-db t
))))
344 "The Gnus registry could not be loaded from %s, creating a new one"
346 (gnus-registry-remake-db t
)))))
348 (defun gnus-registry-read (file)
349 "Do the actual reading of the registry persistence file."
350 (gnus-message 5 "Reading Gnus registry from %s..." file
)
351 (setq gnus-registry-db
352 (gnus-registry-fixup-registry
355 (eieio-persistent-read file
'registry-db
))
356 ;; Older EIEIO versions do not check the class name.
357 ('wrong-number-of-arguments
358 (eieio-persistent-read file
)))))
359 (gnus-message 5 "Reading Gnus registry from %s...done" file
))
361 (defun gnus-registry-save (&optional file db
)
362 "Save the registry cache file."
364 (let ((file (or file gnus-registry-cache-file
))
365 (db (or db gnus-registry-db
)))
366 (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
367 (registry-size db
) file
)
369 db gnus-registry-default-sort-function
)
370 ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
371 (eieio-persistent-save db file
)
372 (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
373 (registry-size db
) file
)))
375 (defun gnus-registry-remove-ignored ()
377 (let* ((db gnus-registry-db
)
378 (grouphashtb (registry-lookup-secondary db
'group
))
379 (old-size (registry-size db
)))
380 (registry-reindex db
)
381 (loop for k being the hash-keys of grouphashtb
382 using
(hash-values v
)
383 when
(gnus-registry-ignore-group-p k
)
384 do
(registry-delete db v nil
))
385 (registry-reindex db
)
386 (gnus-message 4 "Removed %d ignored entries from the Gnus registry"
387 (- old-size
(registry-size db
)))))
389 ;; article move/copy/spool/delete actions
390 (defun gnus-registry-action (action data-header from
&optional to method
)
391 (let* ((id (mail-header-id data-header
))
392 (subject (mail-header-subject data-header
))
393 (extra (mail-header-extra data-header
))
394 (recipients (gnus-registry-sort-addresses
395 (or (cdr-safe (assq 'Cc extra
)) "")
396 (or (cdr-safe (assq 'To extra
)) "")))
397 (sender (nth 0 (gnus-registry-extract-addresses
398 (mail-header-from data-header
))))
399 (from (gnus-group-guess-full-name-from-command-method from
))
400 (to (if to
(gnus-group-guess-full-name-from-command-method to
) nil
)))
401 (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
402 id
(if method
"respooling" "going") from to
)
404 (gnus-registry-handle-action
406 ;; unless copying, remove the old "from" group
407 (if (not (equal 'copy action
)) from nil
)
408 to subject sender recipients
)))
410 (defun gnus-registry-spool-action (id group
&optional subject sender recipients
)
411 (let ((to (gnus-group-guess-full-name-from-command-method group
))
412 (recipients (or recipients
413 (gnus-registry-sort-addresses
414 (or (message-fetch-field "cc") "")
415 (or (message-fetch-field "to") ""))))
416 (subject (or subject
(message-fetch-field "subject")))
417 (sender (or sender
(message-fetch-field "from"))))
418 (when (and (stringp id
) (string-match "\r$" id
))
419 (setq id
(substring id
0 -
1)))
420 (gnus-message 7 "Gnus registry: article %s spooled to %s"
423 (gnus-registry-handle-action id nil to subject sender recipients
)))
425 (defun gnus-registry-handle-action (id from to subject sender
426 &optional recipients
)
429 "gnus-registry-handle-action %S" (list id from to subject sender recipients
))
430 (let ((db gnus-registry-db
)
431 ;; if the group is ignored, set the destination to nil (same as delete)
432 (to (if (gnus-registry-ignore-group-p to
) nil to
))
434 (entry (gnus-registry-get-or-make-entry id
))
435 (subject (gnus-string-remove-all-properties
436 (gnus-registry-simplify-subject subject
)))
437 (sender (gnus-string-remove-all-properties sender
)))
439 ;; this could be done by calling `gnus-registry-set-id-key'
440 ;; several times but it's better to bunch the transactions
443 (registry-delete db
(list id
) nil
)
445 (setq entry
(cons (delete from
(assoc 'group entry
))
446 (assq-delete-all 'group entry
))))
448 (dolist (kv `((group ,to
)
450 (recipient ,@recipients
)
453 (let ((new (or (assq (first kv
) entry
)
455 (dolist (toadd (cdr kv
))
456 (unless (member toadd new
)
457 (setq new
(append new
(list toadd
)))))
458 (setq entry
(cons new
459 (assq-delete-all (first kv
) entry
))))))
460 (gnus-message 10 "Gnus registry: new entry for %s is %S"
463 (gnus-registry-insert db id entry
)))
465 ;; Function for nn{mail|imap}-split-fancy: look up all references in
466 ;; the cache and if a match is found, return that group.
467 (defun gnus-registry-split-fancy-with-parent ()
468 "Split this message into the same group as its parent.
469 The parent is obtained from the registry. This function can be used as an
470 entry in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
471 this: (: gnus-registry-split-fancy-with-parent)
473 This function tracks ALL backends, unlike
474 `nnmail-split-fancy-with-parent' which tracks only nnmail
477 For a message to be split, it looks for the parent message in the
478 References or In-Reply-To header and then looks in the registry
479 to see which group that message was put in. This group is
480 returned, unless `gnus-registry-follow-group-p' return nil for
483 See the Info node `(gnus)Fancy Mail Splitting' for more details."
484 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
485 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
486 ;; now, if reply-to is valid, append it to the References
488 (concat refstr
" " reply-to
)
490 (references (and refstr
(gnus-extract-references refstr
)))
491 ;; these may not be used, but the code is cleaner having them up here
492 (sender (gnus-string-remove-all-properties
493 (message-fetch-field "from")))
494 (recipients (gnus-registry-sort-addresses
495 (or (message-fetch-field "cc") "")
496 (or (message-fetch-field "to") "")))
497 (subject (gnus-string-remove-all-properties
498 (gnus-registry-simplify-subject
499 (message-fetch-field "subject"))))
501 (nnmail-split-fancy-with-parent-ignore-groups
502 (if (listp nnmail-split-fancy-with-parent-ignore-groups
)
503 nnmail-split-fancy-with-parent-ignore-groups
504 (list nnmail-split-fancy-with-parent-ignore-groups
))))
505 (gnus-registry--split-fancy-with-parent-internal
506 :references references
509 :recipients recipients
511 :log-agent
"Gnus registry fancy splitting with parent")))
513 (defun* gnus-registry--split-fancy-with-parent-internal
515 &key references refstr sender subject recipients log-agent
519 "gnus-registry--split-fancy-with-parent-internal %S" spec
)
520 (let ((db gnus-registry-db
)
522 ;; this is a big chain of statements. it uses
523 ;; gnus-registry-post-process-groups to filter the results after
525 ;; the references string must be valid and parse to valid references
529 "%s is tracing references %s"
531 (dolist (reference (nreverse references
))
532 (gnus-message 9 "%s is looking up %s" log-agent reference
)
533 (loop for group in
(gnus-registry-get-id-key reference
'group
)
534 when
(gnus-registry-follow-group-p group
)
537 (gnus-message 7 "%s traced %s to %s" log-agent reference group
)
538 (push group found
))))
539 ;; filter the found groups and return them
540 ;; the found groups are the full groups
541 (setq found
(gnus-registry-post-process-groups
542 "references" refstr found
)))
544 ;; else: there were no matches, now try the extra tracking by subject
545 (when (and (null found
)
546 (memq 'subject gnus-registry-track-extra
)
548 (< gnus-registry-minimum-subject-length
(length subject
)))
553 (gnus-registry-get-id-key reference
'group
))
554 (registry-lookup-secondary-value db
'subject subject
)))))
556 (loop for group in groups
557 when
(gnus-registry-follow-group-p group
)
559 ;; warn more if gnus-registry-track-extra
560 (if gnus-registry-track-extra
7 9)
561 "%s (extra tracking) traced subject '%s' to %s"
562 log-agent subject group
)
564 ;; filter the found groups and return them
565 ;; the found groups are NOT the full groups
566 (setq found
(gnus-registry-post-process-groups
567 "subject" subject found
))))
569 ;; else: there were no matches, try the extra tracking by sender
570 (when (and (null found
)
571 (memq 'sender gnus-registry-track-extra
)
573 (not (gnus-grep-in-list
575 gnus-registry-unfollowed-addresses
)))
580 (gnus-registry-get-id-key reference
'group
))
581 (registry-lookup-secondary-value db
'sender sender
)))))
583 (loop for group in groups
584 when
(gnus-registry-follow-group-p group
)
586 ;; warn more if gnus-registry-track-extra
587 (if gnus-registry-track-extra
7 9)
588 "%s (extra tracking) traced sender '%s' to %s"
589 log-agent sender group
)
592 ;; filter the found groups and return them
593 ;; the found groups are NOT the full groups
594 (setq found
(gnus-registry-post-process-groups
595 "sender" sender found
)))
597 ;; else: there were no matches, try the extra tracking by recipient
598 (when (and (null found
)
599 (memq 'recipient gnus-registry-track-extra
)
601 (dolist (recp recipients
)
602 (when (and (null found
)
603 (not (gnus-grep-in-list
605 gnus-registry-unfollowed-addresses
)))
606 (let ((groups (apply 'append
609 (gnus-registry-get-id-key reference
'group
))
610 (registry-lookup-secondary-value
611 db
'recipient recp
)))))
613 (loop for group in groups
614 when
(gnus-registry-follow-group-p group
)
616 ;; warn more if gnus-registry-track-extra
617 (if gnus-registry-track-extra
7 9)
618 "%s (extra tracking) traced recipient '%s' to %s"
619 log-agent recp group
)
620 and collect group
)))))
622 ;; filter the found groups and return them
623 ;; the found groups are NOT the full groups
624 (setq found
(gnus-registry-post-process-groups
625 "recipients" (mapconcat 'identity recipients
", ") found
)))
627 ;; after the (cond) we extract the actual value safely
630 (defun gnus-registry-post-process-groups (mode key groups
)
631 "Inspects GROUPS found by MODE for KEY to determine which ones to follow.
633 MODE can be 'subject' or 'sender' for example. The KEY is the
634 value by which MODE was searched.
636 Transforms each group name to the equivalent short name.
638 Checks if the current Gnus method (from `gnus-command-method' or
639 from `gnus-newsgroup-name') is the same as the group's method.
640 Foreign methods are not supported so they are rejected.
642 Reduces the list to a single group, or complains if that's not
643 possible. Uses `gnus-registry-split-strategy'."
644 (let ((log-agent "gnus-registry-post-process-group")
645 (desc (format "%d groups" (length groups
)))
647 ;; the strategy can be nil, in which case chosen is nil
649 (case gnus-registry-split-strategy
650 ;; default, take only one-element lists into chosen
652 (and (= (length groups
) 1)
659 (let ((freq (make-hash-table
662 (mapc (lambda (x) (let ((x (gnus-group-short-name x
)))
663 (puthash x
(1+ (gethash x freq
0)) freq
)))
665 (setq desc
(format "%d groups, %d unique"
667 (hash-table-count freq
)))
671 (> (gethash (gnus-group-short-name a
) freq
0)
672 (gethash (gnus-group-short-name b
) freq
0)))))))))
677 "%s: strategy %s on %s produced %s"
678 log-agent gnus-registry-split-strategy desc chosen
)
681 "%s: strategy %s on %s did not produce an answer"
683 (or gnus-registry-split-strategy
"default")
686 (setq groups
(and chosen
(list chosen
)))
688 (dolist (group groups
)
689 (let ((m1 (gnus-find-method-for-group group
))
690 (m2 (or gnus-command-method
691 (gnus-find-method-for-group gnus-newsgroup-name
)))
692 (short-name (gnus-group-short-name group
)))
693 (if (gnus-methods-equal-p m1 m2
)
695 ;; this is REALLY just for debugging
696 (when (not (equal group short-name
))
699 "%s: stripped group %s to %s"
700 log-agent group short-name
))
701 (pushnew short-name out
:test
#'equal
))
705 "%s: ignored foreign group %s"
708 (setq out
(delq nil out
))
711 ((= (length out
) 1) out
)
715 "%s: no matches for %s '%s'."
720 "%s: too many extra matches (%s) for %s '%s'. Returning none."
721 log-agent out mode key
)
724 (defun gnus-registry-follow-group-p (group)
725 "Determines if a group name should be followed.
726 Consults `gnus-registry-unfollowed-groups' and
727 `nnmail-split-fancy-with-parent-ignore-groups'."
729 (not (or (gnus-grep-in-list
731 gnus-registry-unfollowed-groups
)
734 nnmail-split-fancy-with-parent-ignore-groups
)))))
736 ;; note that gnus-registry-ignored-groups is defined in gnus.el as a
737 ;; group/topic parameter and an associated variable!
739 ;; we do special logic for ignoring to accept regular expressions and
740 ;; nnmail-split-fancy-with-parent-ignore-groups as well
741 (defun gnus-registry-ignore-group-p (group)
742 "Determines if a group name should be ignored.
743 Consults `gnus-registry-ignored-groups' and
744 `nnmail-split-fancy-with-parent-ignore-groups'."
746 (or (gnus-grep-in-list
748 (delq nil
(mapcar (lambda (g)
751 ((and (listp g
) (nth 1 g
))
753 (t nil
))) gnus-registry-ignored-groups
)))
754 ;; only use `gnus-parameter-registry-ignore' if
755 ;; `gnus-registry-ignored-groups' is a list of lists
756 ;; (it can be a list of regexes)
757 (and (listp (nth 0 gnus-registry-ignored-groups
))
758 (get-buffer "*Group*") ; in automatic tests this is false
759 (gnus-parameter-registry-ignore group
))
762 nnmail-split-fancy-with-parent-ignore-groups
))))
764 (defun gnus-registry-wash-for-keywords (&optional force
)
765 "Get the keywords of the current article.
766 Overrides existing keywords with FORCE set non-nil."
768 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article
))
770 (if (or (not (gnus-registry-get-id-key id
'keyword
))
772 (with-current-buffer gnus-article-buffer
774 (save-window-excursion
776 (narrow-to-region (point) (point-max))
777 (with-syntax-table gnus-adaptive-word-syntax-table
778 (while (re-search-forward "\\b\\w+\\b" nil t
)
779 (setq word
(gnus-string-remove-all-properties
780 (downcase (buffer-substring
781 (match-beginning 0) (match-end 0)))))
782 (if (> (length word
) 2)
783 (push word words
))))))
784 (gnus-registry-set-id-key id
'keyword words
)))))
786 (defun gnus-registry-keywords ()
787 (let ((table (registry-lookup-secondary gnus-registry-db
'keyword
))
789 (when table
(maphash (lambda (k _v
) (push k ks
)) table
) ks
)))
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 (rest (cdr-safe entry
))
1108 (groups (loop for p in rest
1112 ;; remove all the strings from the entry
1114 (if (stringp elem
) (setq rest
(delq elem rest
))))
1115 (gnus-registry-set-id-key id
'group groups
)
1116 ;; just use the first extra element
1117 (setq rest
(car-safe rest
))
1118 (while (car-safe rest
)
1119 (setq extra-cell
(car-safe rest
)
1120 key
(car-safe extra-cell
)
1121 val
(cdr-safe extra-cell
)
1122 rest
(cdr-safe rest
))
1123 (when (and val
(atom val
))
1124 (setq val
(list val
)))
1125 (gnus-registry-set-id-key id key val
))))
1126 (message "Import done, collected %d entries" count
))))
1129 (defun gnus-registry-initialize ()
1130 "Initialize the Gnus registry."
1132 (gnus-message 5 "Initializing the registry")
1133 (gnus-registry-install-hooks)
1134 (gnus-registry-install-shortcuts)
1135 (gnus-registry-load))
1137 ;; FIXME: Why autoload this function?
1139 (defun gnus-registry-install-hooks ()
1140 "Install the registry hooks."
1142 (setq gnus-registry-enabled t
)
1143 (add-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1144 (add-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1145 (add-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1146 (add-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1148 (add-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1149 (add-hook 'gnus-read-newsrc-el-hook
'gnus-registry-load
)
1151 (add-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1153 (defun gnus-registry-unload-hook ()
1154 "Uninstall the registry hooks."
1156 (remove-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1157 (remove-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1158 (remove-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1159 (remove-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1161 (remove-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1162 (remove-hook 'gnus-read-newsrc-el-hook
'gnus-registry-load
)
1164 (remove-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
)
1165 (setq gnus-registry-enabled nil
))
1167 (add-hook 'gnus-registry-unload-hook
'gnus-registry-unload-hook
)
1169 (defun gnus-registry-install-p ()
1170 "Return non-nil if the registry is enabled (and maybe enable it first).
1171 If the registry is not already enabled, then if `gnus-registry-install'
1172 is `ask', ask the user; or if `gnus-registry-install' is non-nil, enable it."
1174 (unless gnus-registry-enabled
1175 (when (if (eq gnus-registry-install
'ask
)
1177 (concat "Enable the Gnus registry? "
1178 "See the variable `gnus-registry-install' "
1179 "to get rid of this query permanently. "))
1180 gnus-registry-install
)
1181 (gnus-registry-initialize)))
1182 gnus-registry-enabled
)
1184 ;; largely based on nnir-warp-to-article
1185 (defun gnus-try-warping-via-registry ()
1186 "Try to warp via the registry.
1187 This will be done via the current article's source group based on
1188 data stored in the registry."
1190 (when (gnus-summary-article-header)
1191 (let* ((message-id (mail-header-id (gnus-summary-article-header)))
1192 ;; Retrieve the message's group(s) from the registry
1193 (groups (gnus-registry-get-id-key message-id
'group
))
1194 ;; If starting from an ephemeral group, this describes
1195 ;; how to restore the window configuration
1197 (gnus-ephemeral-group-p gnus-newsgroup-name
))
1198 (seen-groups (list (gnus-group-group-name))))
1201 (dolist (group (mapcar 'gnus-simplify-group-name groups
))
1203 ;; skip over any groups we really don't want to warp to.
1204 (unless (or (member group seen-groups
)
1205 (gnus-ephemeral-group-p group
) ;; any ephemeral group
1206 (memq (car (gnus-find-method-for-group group
))
1207 ;; Specific methods; this list may need to expand.
1210 ;; remember that we've seen this group already
1211 (push group seen-groups
)
1213 ;; first exit from any ephemeral summary buffer.
1216 ;; and if the ephemeral summary buffer in turn came from
1217 ;; another summary buffer we have to clean that summary
1219 (when (eq (cdr quit-config
) 'summary
)
1220 (gnus-summary-exit))
1221 ;; remember that we've already done this part
1222 (setq quit-config nil
))
1224 ;; Try to activate the group. If that fails, just move
1225 ;; along. We may have more groups to work with
1228 (gnus-select-group-with-message-id group message-id
) t
)
1229 (throw 'found t
))))))))
1231 (defun gnus-registry-remove-extra-data (extra)
1232 "Remove tracked EXTRA data from the gnus registry.
1233 EXTRA is a list of symbols. Valid symbols are those contained in
1234 the docs of `gnus-registry-track-extra'. This command is useful
1235 when you stop tracking some extra data and now want to purge it
1236 from your existing entries."
1237 (interactive (list (mapcar 'intern
1238 (completing-read-multiple
1240 '("subject" "sender" "recipient")))))
1242 (let ((db gnus-registry-db
))
1243 (registry-reindex db
)
1244 (loop for k being the hash-keys of
(oref db data
)
1245 using
(hash-value v
)
1246 do
(let ((newv (delq nil
(mapcar #'(lambda (entry)
1247 (unless (member (car entry
) extra
)
1250 (registry-delete db
(list k
) nil
)
1251 (gnus-registry-insert db k newv
)))
1252 (registry-reindex db
))))
1254 ;; TODO: a few things
1256 (provide 'gnus-registry
)
1258 ;;; gnus-registry.el ends here