Merge branch 'master' into comment-cache
[emacs.git] / lisp / gnus / gnus-registry.el
blobf728b191110b5149782e031a520de65eef208d19
1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 2002-2017 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/>.
23 ;;; Commentary:
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)
70 ;; TODO:
72 ;; - get the correct group on spool actions
74 ;; - articles that are spooled to a different backend should be moved
75 ;; after splitting
77 ;;; Code:
79 (eval-when-compile (require 'cl))
81 (require 'gnus)
82 (require 'gnus-int)
83 (require 'gnus-sum)
84 (require 'gnus-art)
85 (require 'gnus-util)
86 (require 'nnmail)
87 (require 'easymenu)
88 (require 'registry)
90 (defvar gnus-adaptive-word-syntax-table)
92 (defvar gnus-registry-dirty t
93 "Boolean set to t when the registry is modified.")
95 (defgroup gnus-registry nil
96 "The Gnus registry."
97 :version "22.1"
98 :group 'gnus)
100 (defvar gnus-registry-marks
101 '((Important
102 :char ?i
103 :image "summary_important")
104 (Work
105 :char ?w
106 :image "summary_work")
107 (Personal
108 :char ?p
109 :image "summary_personal")
110 (To-Do
111 :char ?t
112 :image "summary_todo")
113 (Later
114 :char ?l
115 :image "summary_later"))
117 "List of registry marks and their options.
119 `gnus-registry-mark-article' will offer symbols from this list
120 for completion.
122 Each entry must have a character to be useful for summary mode
123 line display and for keyboard shortcuts.
125 Each entry must have an image string to be useful for visual
126 display.")
128 (defcustom gnus-registry-default-mark 'To-Do
129 "The default mark. Should be a valid key for `gnus-registry-marks'."
130 :group 'gnus-registry
131 :type 'symbol)
133 (defcustom gnus-registry-unfollowed-addresses
134 (list (regexp-quote user-mail-address))
135 "List of addresses that gnus-registry-split-fancy-with-parent won't trace.
136 The addresses are matched, they don't have to be fully qualified.
137 In the messages, these addresses can be the sender or the
138 recipients."
139 :version "24.1"
140 :group 'gnus-registry
141 :type '(repeat regexp))
143 (defcustom gnus-registry-unfollowed-groups
144 '("delayed$" "drafts$" "queue$" "INBOX$" "^nnmairix:" "archive")
145 "List of groups that gnus-registry-split-fancy-with-parent won't return.
146 The group names are matched, they don't have to be fully
147 qualified. This parameter tells the Gnus registry 'never split a
148 message into a group that matches one of these, regardless of
149 references.'
151 nnmairix groups are specifically excluded because they are ephemeral."
152 :group 'gnus-registry
153 :type '(repeat regexp))
155 (defcustom gnus-registry-install 'ask
156 "Whether the registry should be installed."
157 :group 'gnus-registry
158 :type '(choice (const :tag "Never Install" nil)
159 (const :tag "Always Install" t)
160 (const :tag "Ask Me" ask)))
162 (defvar gnus-registry-enabled nil)
164 (defvar gnus-summary-misc-menu) ;; Avoid byte compiler warning.
166 (defvar gnus-registry-misc-menus nil) ; ugly way to keep the menus
168 (make-obsolete-variable 'gnus-registry-clean-empty nil "23.4")
169 (make-obsolete-variable 'gnus-registry-use-long-group-names nil "23.4")
170 (make-obsolete-variable 'gnus-registry-max-track-groups nil "23.4")
171 (make-obsolete-variable 'gnus-registry-entry-caching nil "23.4")
172 (make-obsolete-variable 'gnus-registry-trim-articles-without-groups nil "23.4")
173 ;; FIXME it was simply deleted.
174 (make-obsolete-variable 'gnus-registry-max-pruned-entries nil "25.1")
176 (defcustom gnus-registry-track-extra '(subject sender recipient)
177 "Whether the registry should track extra data about a message.
178 The subject, recipients (To: and Cc:), and Sender (From:) headers
179 are tracked this way by default."
180 :group 'gnus-registry
181 :type
182 '(set :tag "Tracking choices"
183 (const :tag "Track by subject (Subject: header)" subject)
184 (const :tag "Track by recipient (To: and Cc: headers)" recipient)
185 (const :tag "Track by sender (From: header)" sender)))
187 (defcustom gnus-registry-split-strategy nil
188 "The splitting strategy applied to the keys in `gnus-registry-track-extra'.
190 Given a set of unique found groups G and counts for each element
191 of G, and a key K (typically `sender' or `subject'):
193 When nil, if G has only one element, use it. Otherwise give up.
194 This is the fastest but also least useful strategy.
196 When `majority', use the majority by count. So if there is a
197 group with the most articles counted by K, use that. Ties are
198 resolved in no particular order, simply the first one found wins.
199 This is the slowest strategy but also the most accurate one.
201 When `first', the first element of G wins. This is fast and
202 should be OK if your senders and subjects don't \"bleed\" across
203 groups."
204 :group 'gnus-registry
205 :type
206 '(choice :tag "Splitting strategy"
207 (const :tag "Only use single choices, discard multiple matches" nil)
208 (const :tag "Majority of matches wins" majority)
209 (const :tag "First found wins" first)))
211 (defcustom gnus-registry-minimum-subject-length 5
212 "The minimum length of a subject before it's considered trackable."
213 :group 'gnus-registry
214 :type 'integer)
216 (defcustom gnus-registry-extra-entries-precious '(mark)
217 "What extra keys are precious, meaning entries with them won't get pruned.
218 By default, 'mark is included, so articles with marks are
219 considered precious.
221 Before you save the Gnus registry, it's pruned. Any entries with
222 keys in this list will not be pruned. All other entries go to
223 the Bit Bucket."
224 :group 'gnus-registry
225 :type '(repeat symbol))
227 (defcustom gnus-registry-cache-file
228 (nnheader-concat
229 (or gnus-dribble-directory gnus-home-directory "~/")
230 ".gnus.registry.eieio")
231 "File where the Gnus registry will be stored."
232 :group 'gnus-registry
233 :type 'file)
235 (defcustom gnus-registry-max-entries nil
236 "Maximum number of entries in the registry, nil for unlimited."
237 :group 'gnus-registry
238 :type '(radio (const :format "Unlimited " nil)
239 (integer :format "Maximum number: %v")))
241 (defcustom gnus-registry-prune-factor 0.1
242 "When pruning, try to prune back to this factor less than the maximum size.
244 In order to prevent constant pruning, we prune back to a number
245 somewhat less than the maximum size. This option controls
246 exactly how much less. For example, given a maximum size of
247 50000 and a prune factor of 0.1, the pruning process will try to
248 cut the registry back to \(- 50000 \(* 50000 0.1)) -> 45000
249 entries. The pruning process is constrained by the presence of
250 \"precious\" entries."
251 :version "25.1"
252 :group 'gnus-registry
253 :type 'float)
255 (defcustom gnus-registry-default-sort-function
256 #'gnus-registry-sort-by-creation-time
257 "Sort function to use when pruning the registry.
258 Entries that sort to the front of the list are pruned first.
259 This can slow pruning down. Set to nil to perform no sorting."
260 :version "25.1"
261 :group 'gnus-registry
262 :type '(choice (const :tag "No sorting" nil) function))
264 (defun gnus-registry-sort-by-creation-time (l r)
265 "Sort older entries to front of list."
266 ;; Pruning starts from the front of the list.
267 (time-less-p
268 (cadr (assq 'creation-time r))
269 (cadr (assq 'creation-time l))))
271 (defun gnus-registry-fixup-registry (db)
272 (when db
273 (let ((old (oref db tracked)))
274 (setf (oref db precious)
275 (append gnus-registry-extra-entries-precious
276 '()))
277 (setf (oref db max-size)
278 (or gnus-registry-max-entries
279 most-positive-fixnum))
280 (setf (oref db prune-factor)
281 (or gnus-registry-prune-factor
282 0.1))
283 (setf (oref db tracked)
284 (append gnus-registry-track-extra
285 '(mark group keyword)))
286 (when (not (equal old (oref db tracked)))
287 (gnus-message 9 "Reindexing the Gnus registry (tracked change)")
288 (registry-reindex db))))
291 (defun gnus-registry-make-db (&optional file)
292 (interactive "fGnus registry persistence file: \n")
293 (gnus-registry-fixup-registry
294 (make-instance 'registry-db
295 :file (or file gnus-registry-cache-file)
296 ;; these parameters are set in `gnus-registry-fixup-registry'
297 :max-size most-positive-fixnum
298 :version registry-db-version
299 :precious nil
300 :tracked nil)))
302 (defvar gnus-registry-db (gnus-registry-make-db)
303 "The article registry by Message ID. See `registry-db'.")
305 ;; top-level registry data management
306 (defun gnus-registry-remake-db (&optional forsure)
307 "Remake the registry database after customization.
308 This is not required after changing `gnus-registry-cache-file'."
309 (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
310 (when forsure
311 (gnus-message 4 "Remaking the Gnus registry")
312 (setq gnus-registry-db (gnus-registry-make-db))))
314 (defun gnus-registry-load ()
315 "Load the registry from the cache file."
316 (interactive)
317 (let ((file gnus-registry-cache-file))
318 (condition-case nil
319 (gnus-registry-read file)
320 (file-error
321 ;; Fix previous mis-naming of the registry file.
322 (let ((old-file-name
323 (concat (file-name-sans-extension
324 gnus-registry-cache-file)
325 ".eioio")))
326 (if (and (file-exists-p old-file-name)
327 (yes-or-no-p
328 (format "Rename registry file from %s to %s? "
329 old-file-name file)))
330 (progn
331 (gnus-registry-read old-file-name)
332 (setf (oref gnus-registry-db file) file)
333 (gnus-message 1 "Registry filename changed to %s" file))
334 (gnus-registry-remake-db t))))
335 (error
336 (gnus-message
338 "The Gnus registry could not be loaded from %s, creating a new one"
339 file)
340 (gnus-registry-remake-db t)))))
342 (defun gnus-registry-read (file)
343 "Do the actual reading of the registry persistence file."
344 (gnus-message 5 "Reading Gnus registry from %s..." file)
345 (setq gnus-registry-db
346 (gnus-registry-fixup-registry
347 (condition-case nil
348 (with-no-warnings
349 (eieio-persistent-read file 'registry-db))
350 ;; Older EIEIO versions do not check the class name.
351 ('wrong-number-of-arguments
352 (eieio-persistent-read file)))))
353 (gnus-message 5 "Reading Gnus registry from %s...done" file))
355 (defun gnus-registry-save (&optional file db)
356 "Save the registry cache file."
357 (interactive)
358 (let ((file (or file gnus-registry-cache-file))
359 (db (or db gnus-registry-db)))
360 (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
361 (registry-size db) file)
362 (registry-prune
363 db gnus-registry-default-sort-function)
364 ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
365 (eieio-persistent-save db file)
366 (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
367 (registry-size db) file)))
369 (defun gnus-registry-remove-ignored ()
370 (interactive)
371 (let* ((db gnus-registry-db)
372 (grouphashtb (registry-lookup-secondary db 'group))
373 (old-size (registry-size db)))
374 (registry-reindex db)
375 (loop for k being the hash-keys of grouphashtb
376 using (hash-values v)
377 when (gnus-registry-ignore-group-p k)
378 do (registry-delete db v nil))
379 (registry-reindex db)
380 (gnus-message 4 "Removed %d ignored entries from the Gnus registry"
381 (- old-size (registry-size db)))))
383 ;; article move/copy/spool/delete actions
384 (defun gnus-registry-action (action data-header from &optional to method)
385 (let* ((id (mail-header-id data-header))
386 (subject (mail-header-subject data-header))
387 (extra (mail-header-extra data-header))
388 (recipients (gnus-registry-sort-addresses
389 (or (cdr-safe (assq 'Cc extra)) "")
390 (or (cdr-safe (assq 'To extra)) "")))
391 (sender (nth 0 (gnus-registry-extract-addresses
392 (mail-header-from data-header))))
393 (from (gnus-group-guess-full-name-from-command-method from))
394 (to (if to (gnus-group-guess-full-name-from-command-method to) nil)))
395 (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
396 id (if method "respooling" "going") from to)
398 (gnus-registry-handle-action
400 ;; unless copying, remove the old "from" group
401 (if (not (equal 'copy action)) from nil)
402 to subject sender recipients)))
404 (defun gnus-registry-spool-action (id group &optional subject sender recipients)
405 (let ((to (gnus-group-guess-full-name-from-command-method group))
406 (recipients (or recipients
407 (gnus-registry-sort-addresses
408 (or (message-fetch-field "cc") "")
409 (or (message-fetch-field "to") ""))))
410 (subject (or subject (message-fetch-field "subject")))
411 (sender (or sender (message-fetch-field "from"))))
412 (when (and (stringp id) (string-match "\r$" id))
413 (setq id (substring id 0 -1)))
414 (gnus-message 7 "Gnus registry: article %s spooled to %s"
417 (gnus-registry-handle-action id nil to subject sender recipients)))
419 (defun gnus-registry-handle-action (id from to subject sender
420 &optional recipients)
421 (gnus-message
423 "gnus-registry-handle-action %S" (list id from to subject sender recipients))
424 (let ((db gnus-registry-db)
425 ;; if the group is ignored, set the destination to nil (same as delete)
426 (to (if (gnus-registry-ignore-group-p to) nil to))
427 ;; safe if not found
428 (entry (gnus-registry-get-or-make-entry id))
429 (subject (gnus-string-remove-all-properties
430 (gnus-registry-simplify-subject subject)))
431 (sender (gnus-string-remove-all-properties sender)))
433 ;; this could be done by calling `gnus-registry-set-id-key'
434 ;; several times but it's better to bunch the transactions
435 ;; together
437 (registry-delete db (list id) nil)
438 (when from
439 (setq entry (cons (delete from (assoc 'group entry))
440 (assq-delete-all 'group entry))))
442 (dolist (kv `((group ,to)
443 (sender ,sender)
444 (recipient ,@recipients)
445 (subject ,subject)))
446 (when (second kv)
447 (let ((new (or (assq (first kv) entry)
448 (list (first kv)))))
449 (dolist (toadd (cdr kv))
450 (unless (member toadd new)
451 (setq new (append new (list toadd)))))
452 (setq entry (cons new
453 (assq-delete-all (first kv) entry))))))
454 (gnus-message 10 "Gnus registry: new entry for %s is %S"
456 entry)
457 (gnus-registry-insert db id entry)))
459 ;; Function for nn{mail|imap}-split-fancy: look up all references in
460 ;; the cache and if a match is found, return that group.
461 (defun gnus-registry-split-fancy-with-parent ()
462 "Split this message into the same group as its parent.
463 The parent is obtained from the registry. This function can be used as an
464 entry in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
465 this: (: gnus-registry-split-fancy-with-parent)
467 This function tracks ALL backends, unlike
468 `nnmail-split-fancy-with-parent' which tracks only nnmail
469 messages.
471 For a message to be split, it looks for the parent message in the
472 References or In-Reply-To header and then looks in the registry
473 to see which group that message was put in. This group is
474 returned, unless `gnus-registry-follow-group-p' return nil for
475 that group.
477 See the Info node `(gnus)Fancy Mail Splitting' for more details."
478 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
479 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
480 ;; now, if reply-to is valid, append it to the References
481 (refstr (if reply-to
482 (concat refstr " " reply-to)
483 refstr))
484 (references (and refstr (gnus-extract-references refstr)))
485 ;; these may not be used, but the code is cleaner having them up here
486 (sender (gnus-string-remove-all-properties
487 (message-fetch-field "from")))
488 (recipients (gnus-registry-sort-addresses
489 (or (message-fetch-field "cc") "")
490 (or (message-fetch-field "to") "")))
491 (subject (gnus-string-remove-all-properties
492 (gnus-registry-simplify-subject
493 (message-fetch-field "subject"))))
495 (nnmail-split-fancy-with-parent-ignore-groups
496 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
497 nnmail-split-fancy-with-parent-ignore-groups
498 (list nnmail-split-fancy-with-parent-ignore-groups))))
499 (gnus-registry--split-fancy-with-parent-internal
500 :references references
501 :refstr refstr
502 :sender sender
503 :recipients recipients
504 :subject subject
505 :log-agent "Gnus registry fancy splitting with parent")))
507 (defun* gnus-registry--split-fancy-with-parent-internal
508 (&rest spec
509 &key references refstr sender subject recipients log-agent
510 &allow-other-keys)
511 (gnus-message
513 "gnus-registry--split-fancy-with-parent-internal %S" spec)
514 (let ((db gnus-registry-db)
515 found)
516 ;; this is a big chain of statements. it uses
517 ;; gnus-registry-post-process-groups to filter the results after
518 ;; every step.
519 ;; the references string must be valid and parse to valid references
520 (when references
521 (gnus-message
523 "%s is tracing references %s"
524 log-agent refstr)
525 (dolist (reference (nreverse references))
526 (gnus-message 9 "%s is looking up %s" log-agent reference)
527 (loop for group in (gnus-registry-get-id-key reference 'group)
528 when (gnus-registry-follow-group-p group)
530 (progn
531 (gnus-message 7 "%s traced %s to %s" log-agent reference group)
532 (push group found))))
533 ;; filter the found groups and return them
534 ;; the found groups are the full groups
535 (setq found (gnus-registry-post-process-groups
536 "references" refstr found)))
538 ;; else: there were no matches, now try the extra tracking by subject
539 (when (and (null found)
540 (memq 'subject gnus-registry-track-extra)
541 subject
542 (< gnus-registry-minimum-subject-length (length subject)))
543 (let ((groups (apply
544 'append
545 (mapcar
546 (lambda (reference)
547 (gnus-registry-get-id-key reference 'group))
548 (registry-lookup-secondary-value db 'subject subject)))))
549 (setq found
550 (loop for group in groups
551 when (gnus-registry-follow-group-p group)
552 do (gnus-message
553 ;; warn more if gnus-registry-track-extra
554 (if gnus-registry-track-extra 7 9)
555 "%s (extra tracking) traced subject `%s' to %s"
556 log-agent subject group)
557 and collect group))
558 ;; filter the found groups and return them
559 ;; the found groups are NOT the full groups
560 (setq found (gnus-registry-post-process-groups
561 "subject" subject found))))
563 ;; else: there were no matches, try the extra tracking by sender
564 (when (and (null found)
565 (memq 'sender gnus-registry-track-extra)
566 sender
567 (not (gnus-grep-in-list
568 sender
569 gnus-registry-unfollowed-addresses)))
570 (let ((groups (apply
571 'append
572 (mapcar
573 (lambda (reference)
574 (gnus-registry-get-id-key reference 'group))
575 (registry-lookup-secondary-value db 'sender sender)))))
576 (setq found
577 (loop for group in groups
578 when (gnus-registry-follow-group-p group)
579 do (gnus-message
580 ;; warn more if gnus-registry-track-extra
581 (if gnus-registry-track-extra 7 9)
582 "%s (extra tracking) traced sender `%s' to %s"
583 log-agent sender group)
584 and collect group)))
586 ;; filter the found groups and return them
587 ;; the found groups are NOT the full groups
588 (setq found (gnus-registry-post-process-groups
589 "sender" sender found)))
591 ;; else: there were no matches, try the extra tracking by recipient
592 (when (and (null found)
593 (memq 'recipient gnus-registry-track-extra)
594 recipients)
595 (dolist (recp recipients)
596 (when (and (null found)
597 (not (gnus-grep-in-list
598 recp
599 gnus-registry-unfollowed-addresses)))
600 (let ((groups (apply 'append
601 (mapcar
602 (lambda (reference)
603 (gnus-registry-get-id-key reference 'group))
604 (registry-lookup-secondary-value
605 db 'recipient recp)))))
606 (setq found
607 (loop for group in groups
608 when (gnus-registry-follow-group-p group)
609 do (gnus-message
610 ;; warn more if gnus-registry-track-extra
611 (if gnus-registry-track-extra 7 9)
612 "%s (extra tracking) traced recipient `%s' to %s"
613 log-agent recp group)
614 and collect group)))))
616 ;; filter the found groups and return them
617 ;; the found groups are NOT the full groups
618 (setq found (gnus-registry-post-process-groups
619 "recipients" (mapconcat 'identity recipients ", ") found)))
621 ;; after the (cond) we extract the actual value safely
622 (car-safe found)))
624 (defun gnus-registry-post-process-groups (mode key groups)
625 "Inspects GROUPS found by MODE for KEY to determine which ones to follow.
627 MODE can be `subject' or `sender' for example. The KEY is the
628 value by which MODE was searched.
630 Transforms each group name to the equivalent short name.
632 Checks if the current Gnus method (from `gnus-command-method' or
633 from `gnus-newsgroup-name') is the same as the group's method.
634 Foreign methods are not supported so they are rejected.
636 Reduces the list to a single group, or complains if that's not
637 possible. Uses `gnus-registry-split-strategy'."
638 (let ((log-agent "gnus-registry-post-process-group")
639 (desc (format "%d groups" (length groups)))
640 out chosen)
641 ;; the strategy can be nil, in which case chosen is nil
642 (setq chosen
643 (case gnus-registry-split-strategy
644 ;; default, take only one-element lists into chosen
645 ((nil)
646 (and (= (length groups) 1)
647 (car-safe groups)))
649 ((first)
650 (car-safe groups))
652 ((majority)
653 (let ((freq (make-hash-table
654 :size 256
655 :test 'equal)))
656 (mapc (lambda (x) (let ((x (gnus-group-short-name x)))
657 (puthash x (1+ (gethash x freq 0)) freq)))
658 groups)
659 (setq desc (format "%d groups, %d unique"
660 (length groups)
661 (hash-table-count freq)))
662 (car-safe
663 (sort groups
664 (lambda (a b)
665 (> (gethash (gnus-group-short-name a) freq 0)
666 (gethash (gnus-group-short-name b) freq 0)))))))))
668 (if chosen
669 (gnus-message
671 "%s: strategy %s on %s produced %s"
672 log-agent gnus-registry-split-strategy desc chosen)
673 (gnus-message
675 "%s: strategy %s on %s did not produce an answer"
676 log-agent
677 (or gnus-registry-split-strategy "default")
678 desc))
680 (setq groups (and chosen (list chosen)))
682 (dolist (group groups)
683 (let ((m1 (gnus-find-method-for-group group))
684 (m2 (or gnus-command-method
685 (gnus-find-method-for-group gnus-newsgroup-name)))
686 (short-name (gnus-group-short-name group)))
687 (if (gnus-methods-equal-p m1 m2)
688 (progn
689 ;; this is REALLY just for debugging
690 (when (not (equal group short-name))
691 (gnus-message
693 "%s: stripped group %s to %s"
694 log-agent group short-name))
695 (pushnew short-name out :test #'equal))
696 ;; else...
697 (gnus-message
699 "%s: ignored foreign group %s"
700 log-agent group))))
702 (setq out (delq nil out))
704 (cond
705 ((= (length out) 1) out)
706 ((null out)
707 (gnus-message
709 "%s: no matches for %s `%s'."
710 log-agent mode key)
711 nil)
712 (t (gnus-message
714 "%s: too many extra matches (%s) for %s `%s'. Returning none."
715 log-agent out mode key)
716 nil))))
718 (defun gnus-registry-follow-group-p (group)
719 "Determines if a group name should be followed.
720 Consults `gnus-registry-unfollowed-groups' and
721 `nnmail-split-fancy-with-parent-ignore-groups'."
722 (and group
723 (not (or (gnus-grep-in-list
724 group
725 gnus-registry-unfollowed-groups)
726 (gnus-grep-in-list
727 group
728 nnmail-split-fancy-with-parent-ignore-groups)))))
730 ;; note that gnus-registry-ignored-groups is defined in gnus.el as a
731 ;; group/topic parameter and an associated variable!
733 ;; we do special logic for ignoring to accept regular expressions and
734 ;; nnmail-split-fancy-with-parent-ignore-groups as well
735 (defun gnus-registry-ignore-group-p (group)
736 "Determines if a group name should be ignored.
737 Consults `gnus-registry-ignored-groups' and
738 `nnmail-split-fancy-with-parent-ignore-groups'."
739 (and group
740 (or (gnus-grep-in-list
741 group
742 (delq nil (mapcar (lambda (g)
743 (cond
744 ((stringp g) g)
745 ((and (listp g) (nth 1 g))
746 (nth 0 g))
747 (t nil))) gnus-registry-ignored-groups)))
748 ;; only use `gnus-parameter-registry-ignore' if
749 ;; `gnus-registry-ignored-groups' is a list of lists
750 ;; (it can be a list of regexes)
751 (and (listp (nth 0 gnus-registry-ignored-groups))
752 (get-buffer "*Group*") ; in automatic tests this is false
753 (gnus-parameter-registry-ignore group))
754 (gnus-grep-in-list
755 group
756 nnmail-split-fancy-with-parent-ignore-groups))))
758 (defun gnus-registry-wash-for-keywords (&optional force)
759 "Get the keywords of the current article.
760 Overrides existing keywords with FORCE set non-nil."
761 (interactive)
762 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article))
763 word words)
764 (if (or (not (gnus-registry-get-id-key id 'keyword))
765 force)
766 (with-current-buffer gnus-article-buffer
767 (article-goto-body)
768 (save-window-excursion
769 (save-restriction
770 (narrow-to-region (point) (point-max))
771 (with-syntax-table gnus-adaptive-word-syntax-table
772 (while (re-search-forward "\\b\\w+\\b" nil t)
773 (setq word (gnus-string-remove-all-properties
774 (downcase (buffer-substring
775 (match-beginning 0) (match-end 0)))))
776 (if (> (length word) 2)
777 (push word words))))))
778 (gnus-registry-set-id-key id 'keyword words)))))
780 (defun gnus-registry-keywords ()
781 (let ((table (registry-lookup-secondary gnus-registry-db 'keyword))
782 (ks ()))
783 (when table (maphash (lambda (k _v) (push k ks)) table) ks)))
785 (defun gnus-registry-find-keywords (keyword)
786 (interactive (list
787 (completing-read "Keyword: " (gnus-registry-keywords) nil t)))
788 (registry-lookup-secondary-value gnus-registry-db 'keyword keyword))
790 (defun gnus-registry-register-message-ids ()
791 "Register the Message-ID of every article in the group."
792 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
793 (dolist (article gnus-newsgroup-articles)
794 (let* ((id (gnus-registry-fetch-message-id-fast article))
795 (groups (gnus-registry-get-id-key id 'group)))
796 (unless (member gnus-newsgroup-name groups)
797 (gnus-message 9 "Registry: Registering article %d with group %s"
798 article gnus-newsgroup-name)
799 (gnus-registry-handle-action id nil gnus-newsgroup-name
800 (gnus-registry-fetch-simplified-message-subject-fast article)
801 (gnus-registry-fetch-sender-fast article)
802 (gnus-registry-fetch-recipients-fast article)))))))
804 ;; message field fetchers
805 (defun gnus-registry-fetch-message-id-fast (article)
806 "Fetch the Message-ID quickly, using the internal gnus-data-list function."
807 (if (and (numberp article)
808 (assoc article (gnus-data-list nil)))
809 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
810 nil))
812 (defun gnus-registry-extract-addresses (text)
813 "Extract all the addresses in a normalized way from TEXT.
814 Returns an unsorted list of strings in the name <address> format.
815 Addresses without a name will say \"noname\"."
816 (mapcar (lambda (add)
817 (gnus-string-remove-all-properties
818 (let* ((name (or (nth 0 add) "noname"))
819 (addr (nth 1 add))
820 (addr (if (bufferp addr)
821 (with-current-buffer addr
822 (buffer-string))
823 addr)))
824 (format "%s <%s>" name addr))))
825 (mail-extract-address-components text t)))
827 (defun gnus-registry-sort-addresses (&rest addresses)
828 "Return a normalized and sorted list of ADDRESSES."
829 (sort (mapcan 'gnus-registry-extract-addresses addresses) 'string-lessp))
831 (defun gnus-registry-simplify-subject (subject)
832 (if (stringp subject)
833 (gnus-simplify-subject subject)
834 nil))
836 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
837 "Fetch the Subject quickly, using the internal gnus-data-list function."
838 (if (and (numberp article)
839 (assoc article (gnus-data-list nil)))
840 (gnus-string-remove-all-properties
841 (gnus-registry-simplify-subject
842 (mail-header-subject (gnus-data-header
843 (assoc article (gnus-data-list nil))))))
844 nil))
846 (defun gnus-registry-fetch-sender-fast (article)
847 (gnus-registry-fetch-header-fast "from" article))
849 (defun gnus-registry-fetch-recipients-fast (article)
850 (gnus-registry-sort-addresses
851 (or (ignore-errors (gnus-registry-fetch-header-fast "Cc" article)) "")
852 (or (ignore-errors (gnus-registry-fetch-header-fast "To" article)) "")))
854 (defun gnus-registry-fetch-header-fast (article header)
855 "Fetch the HEADER quickly, using the internal gnus-data-list function."
856 (if (and (numberp article)
857 (assoc article (gnus-data-list nil)))
858 (gnus-string-remove-all-properties
859 (cdr (assq header (gnus-data-header
860 (assoc article (gnus-data-list nil))))))
861 nil))
863 ;; registry marks glue
864 (defun gnus-registry-do-marks (type function)
865 "For each known mark, call FUNCTION for each cell of type TYPE.
867 FUNCTION should take two parameters, a mark symbol and the cell value."
868 (dolist (mark-info gnus-registry-marks)
869 (let* ((mark (car-safe mark-info))
870 (data (cdr-safe mark-info))
871 (cell-data (plist-get data type)))
872 (when cell-data
873 (funcall function mark cell-data)))))
875 ;; FIXME: Why not merge gnus-registry--set/remove-mark and
876 ;; gnus-registry-set-article-mark-internal?
877 (defun gnus-registry--set/remove-mark (mark remove articles)
878 "Set/remove the MARK over process-marked ARTICLES."
879 ;; If this is called and the user doesn't want the
880 ;; registry enabled, we'll ask anyhow.
881 (unless gnus-registry-install
882 (let ((gnus-registry-install 'ask))
883 (gnus-registry-install-p)))
885 ;; Now the user is asked if gnus-registry-install is `ask'.
886 (when (gnus-registry-install-p)
887 (gnus-registry-set-article-mark-internal
888 ;; All this just to get the mark, I must be doing it wrong.
889 mark articles remove t)
890 ;; FIXME: Why do we do the above only here and not directly inside
891 ;; gnus-registry-set-article-mark-internal? I.e. we wouldn't we want to do
892 ;; the things below when gnus-registry-set-article-mark-internal is called
893 ;; from gnus-registry-set-article-mark or
894 ;; gnus-registry-remove-article-mark?
895 (gnus-message 9 "Applying mark %s to %d articles"
896 mark (length articles))
897 (dolist (article articles)
898 (gnus-summary-update-article
899 article
900 (assoc article (gnus-data-list nil))))))
902 ;; This is ugly code, but I don't know how to do it better.
903 (defun gnus-registry-install-shortcuts ()
904 "Install the keyboard shortcuts and menus for the registry.
905 Uses `gnus-registry-marks' to find what shortcuts to install."
906 (let (keys-plist)
907 (setq gnus-registry-misc-menus nil)
908 (gnus-registry-do-marks
909 :char
910 (lambda (mark data)
911 (let ((function-format
912 (format "gnus-registry-%%s-article-%s-mark" mark)))
914 ;;; The following generates these functions:
915 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
916 ;;; "Apply the Important mark to process-marked ARTICLES."
917 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
918 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
919 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
920 ;;; "Apply the Important mark to process-marked ARTICLES."
921 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
922 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
924 (dolist (remove '(t nil))
925 (let* ((variant-name (if remove "remove" "set"))
926 (function-name
927 (intern (format function-format variant-name)))
928 (shortcut (format "%c" (if remove (upcase data) data))))
929 (defalias function-name
930 ;; If it weren't for the function's docstring, we could
931 ;; use a closure, with lexical-let :-(
932 `(lambda (&rest articles)
933 ,(format
934 "%s the %s mark over process-marked ARTICLES."
935 (upcase-initials variant-name)
936 mark)
937 (interactive
938 (gnus-summary-work-articles current-prefix-arg))
939 (gnus-registry--set/remove-mark ',mark ',remove articles)))
940 (push function-name keys-plist)
941 (push shortcut keys-plist)
942 (push (vector (format "%s %s"
943 (upcase-initials variant-name)
944 (symbol-name mark))
945 function-name t)
946 gnus-registry-misc-menus)
947 (gnus-message 9 "Defined mark handling function %s"
948 function-name))))))
949 (gnus-define-keys-1
950 '(gnus-registry-mark-map "M" gnus-summary-mark-map)
951 keys-plist)
952 (add-hook 'gnus-summary-menu-hook
953 (lambda ()
954 (easy-menu-add-item
955 gnus-summary-misc-menu
957 (cons "Registry Marks" gnus-registry-misc-menus))))))
959 (make-obsolete 'gnus-registry-user-format-function-M
960 'gnus-registry-article-marks-to-chars "24.1") ?
962 (defalias 'gnus-registry-user-format-function-M
963 'gnus-registry-article-marks-to-chars)
965 ;; use like this:
966 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
967 (defun gnus-registry-article-marks-to-chars (headers)
968 "Show the marks for an article by the :char property."
969 (let* ((id (mail-header-message-id headers))
970 (marks (when id (gnus-registry-get-id-key id 'mark))))
971 (mapconcat (lambda (mark)
972 (plist-get
973 (cdr-safe
974 (assoc mark gnus-registry-marks))
975 :char))
976 marks "")))
978 ;; use like this:
979 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
980 (defun gnus-registry-article-marks-to-names (headers)
981 "Show the marks for an article by name."
982 (let* ((id (mail-header-message-id headers))
983 (marks (when id (gnus-registry-get-id-key id 'mark))))
984 (mapconcat (lambda (mark) (symbol-name mark)) marks ",")))
986 (defun gnus-registry-read-mark ()
987 "Read a mark name from the user with completion."
988 (let ((mark (gnus-completing-read
989 "Label"
990 (mapcar 'symbol-name (mapcar 'car gnus-registry-marks))
991 nil nil nil
992 (symbol-name gnus-registry-default-mark))))
993 (when (stringp mark)
994 (intern mark))))
996 (defun gnus-registry-set-article-mark (&rest articles)
997 "Apply a mark to process-marked ARTICLES."
998 (interactive (gnus-summary-work-articles current-prefix-arg))
999 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
1000 articles nil t))
1002 (defun gnus-registry-remove-article-mark (&rest articles)
1003 "Remove a mark from process-marked ARTICLES."
1004 (interactive (gnus-summary-work-articles current-prefix-arg))
1005 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
1006 articles t t))
1008 (defun gnus-registry-set-article-mark-internal (mark
1009 articles
1010 &optional remove
1011 show-message)
1012 "Apply or remove MARK across a list of ARTICLES."
1013 (let ((article-id-list
1014 (mapcar 'gnus-registry-fetch-message-id-fast articles)))
1015 (dolist (id article-id-list)
1016 (let* ((marks (delq mark (gnus-registry-get-id-key id 'mark)))
1017 (marks (if remove marks (cons mark marks))))
1018 (when show-message
1019 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
1020 (if remove "Removing" "Adding")
1021 mark id marks))
1022 (gnus-registry-set-id-key id 'mark marks)))))
1024 (defun gnus-registry-get-article-marks (&rest articles)
1025 "Get the Gnus registry marks for ARTICLES and show them if interactive.
1026 Uses process/prefix conventions. For multiple articles,
1027 only the last one's marks are returned."
1028 (interactive (gnus-summary-work-articles 1))
1029 (let* ((article (last articles))
1030 (id (gnus-registry-fetch-message-id-fast article))
1031 (marks (when id (gnus-registry-get-id-key id 'mark))))
1032 (when (called-interactively-p 'any)
1033 (gnus-message 1 "Marks are %S" marks))
1034 marks))
1036 (defun gnus-registry-group-count (id)
1037 "Get the number of groups of a message, based on the message ID."
1038 (length (gnus-registry-get-id-key id 'group)))
1040 (defun gnus-registry-get-or-make-entry (id)
1041 (let* ((db gnus-registry-db)
1042 ;; safe if not found
1043 (entries (registry-lookup db (list id))))
1045 (when (null entries)
1046 (gnus-registry-insert db id (list (list 'creation-time (current-time))
1047 '(group) '(sender) '(subject)))
1048 (setq entries (registry-lookup db (list id))))
1050 (nth 1 (assoc id entries))))
1052 (defun gnus-registry-delete-entries (idlist)
1053 (registry-delete gnus-registry-db idlist nil))
1055 (defun gnus-registry-get-id-key (id key)
1056 (cdr-safe (assq key (gnus-registry-get-or-make-entry id))))
1058 (defun gnus-registry-set-id-key (id key vals)
1059 (let* ((db gnus-registry-db)
1060 (entry (gnus-registry-get-or-make-entry id)))
1061 (registry-delete db (list id) nil)
1062 (setq entry (cons (cons key vals) (assq-delete-all key entry)))
1063 (gnus-registry-insert db id entry)
1064 entry))
1066 (defun gnus-registry-insert (db id entry)
1067 "Just like `registry-insert' but tries to prune on error."
1068 (when (registry-full db)
1069 (message "Trying to prune the registry because it's full")
1070 (registry-prune
1071 db gnus-registry-default-sort-function))
1072 (registry-insert db id entry)
1073 entry)
1075 (defun gnus-registry-import-eld (file)
1076 (interactive "fOld registry file to import? ")
1077 ;; example content:
1078 ;; (setq gnus-registry-alist '(
1079 ;; ("<messageID>" ((marks nil)
1080 ;; (mtime 19365 1776 440496)
1081 ;; (sender . "root (Cron Daemon)")
1082 ;; (subject . "Cron"))
1083 ;; "cron" "nnml+private:cron")
1084 (load file t)
1085 (when (boundp 'gnus-registry-alist)
1086 (let* ((old (symbol-value 'gnus-registry-alist))
1087 (count 0)
1088 (expected (length old))
1089 entry)
1090 (while (car-safe old)
1091 (incf count)
1092 ;; don't use progress reporters for backwards compatibility
1093 (when (and (< 0 expected)
1094 (= 0 (mod count 100)))
1095 (message "importing: %d of %d (%.2f%%)"
1096 count expected (/ (* 100.0 count) expected)))
1097 (setq entry (car-safe old)
1098 old (cdr-safe old))
1099 (let* ((id (car-safe entry))
1100 (rest (cdr-safe entry))
1101 (groups (loop for p in rest
1102 when (stringp p)
1103 collect p))
1104 extra-cell key val)
1105 ;; remove all the strings from the entry
1106 (dolist (elem rest)
1107 (if (stringp elem) (setq rest (delq elem rest))))
1108 (gnus-registry-set-id-key id 'group groups)
1109 ;; just use the first extra element
1110 (setq rest (car-safe rest))
1111 (while (car-safe rest)
1112 (setq extra-cell (car-safe rest)
1113 key (car-safe extra-cell)
1114 val (cdr-safe extra-cell)
1115 rest (cdr-safe rest))
1116 (when (and val (atom val))
1117 (setq val (list val)))
1118 (gnus-registry-set-id-key id key val))))
1119 (message "Import done, collected %d entries" count))))
1121 ;;;###autoload
1122 (defun gnus-registry-initialize ()
1123 "Initialize the Gnus registry."
1124 (interactive)
1125 (gnus-message 5 "Initializing the registry")
1126 (gnus-registry-install-hooks)
1127 (gnus-registry-install-shortcuts)
1128 (gnus-registry-load))
1130 ;; FIXME: Why autoload this function?
1131 ;;;###autoload
1132 (defun gnus-registry-install-hooks ()
1133 "Install the registry hooks."
1134 (interactive)
1135 (setq gnus-registry-enabled t)
1136 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
1137 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
1138 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
1139 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
1141 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
1142 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-load)
1144 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
1146 (defun gnus-registry-unload-hook ()
1147 "Uninstall the registry hooks."
1148 (interactive)
1149 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
1150 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
1151 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
1152 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
1154 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
1155 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-load)
1157 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids)
1158 (setq gnus-registry-enabled nil))
1160 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
1162 (defun gnus-registry-install-p ()
1163 "Return non-nil if the registry is enabled (and maybe enable it first).
1164 If the registry is not already enabled, then if `gnus-registry-install'
1165 is `ask', ask the user; or if `gnus-registry-install' is non-nil, enable it."
1166 (interactive)
1167 (unless gnus-registry-enabled
1168 (when (if (eq gnus-registry-install 'ask)
1169 (gnus-y-or-n-p
1170 (concat "Enable the Gnus registry? "
1171 "See the variable `gnus-registry-install' "
1172 "to get rid of this query permanently. "))
1173 gnus-registry-install)
1174 (gnus-registry-initialize)))
1175 gnus-registry-enabled)
1177 ;; largely based on nnir-warp-to-article
1178 (defun gnus-try-warping-via-registry ()
1179 "Try to warp via the registry.
1180 This will be done via the current article's source group based on
1181 data stored in the registry."
1182 (interactive)
1183 (when (gnus-summary-article-header)
1184 (let* ((message-id (mail-header-id (gnus-summary-article-header)))
1185 ;; Retrieve the message's group(s) from the registry
1186 (groups (gnus-registry-get-id-key message-id 'group))
1187 ;; If starting from an ephemeral group, this describes
1188 ;; how to restore the window configuration
1189 (quit-config
1190 (gnus-ephemeral-group-p gnus-newsgroup-name))
1191 (seen-groups (list (gnus-group-group-name))))
1193 (catch 'found
1194 (dolist (group (mapcar 'gnus-simplify-group-name groups))
1196 ;; skip over any groups we really don't want to warp to.
1197 (unless (or (member group seen-groups)
1198 (gnus-ephemeral-group-p group) ;; any ephemeral group
1199 (memq (car (gnus-find-method-for-group group))
1200 ;; Specific methods; this list may need to expand.
1201 '(nnir)))
1203 ;; remember that we've seen this group already
1204 (push group seen-groups)
1206 ;; first exit from any ephemeral summary buffer.
1207 (when quit-config
1208 (gnus-summary-exit)
1209 ;; and if the ephemeral summary buffer in turn came from
1210 ;; another summary buffer we have to clean that summary
1211 ;; up too.
1212 (when (eq (cdr quit-config) 'summary)
1213 (gnus-summary-exit))
1214 ;; remember that we've already done this part
1215 (setq quit-config nil))
1217 ;; Try to activate the group. If that fails, just move
1218 ;; along. We may have more groups to work with
1219 (when
1220 (ignore-errors
1221 (gnus-select-group-with-message-id group message-id) t)
1222 (throw 'found t))))))))
1224 (defun gnus-registry-remove-extra-data (extra)
1225 "Remove tracked EXTRA data from the gnus registry.
1226 EXTRA is a list of symbols. Valid symbols are those contained in
1227 the docs of `gnus-registry-track-extra'. This command is useful
1228 when you stop tracking some extra data and now want to purge it
1229 from your existing entries."
1230 (interactive (list (mapcar 'intern
1231 (completing-read-multiple
1232 "Extra data: "
1233 '("subject" "sender" "recipient")))))
1234 (when extra
1235 (let ((db gnus-registry-db))
1236 (registry-reindex db)
1237 (loop for k being the hash-keys of (oref db data)
1238 using (hash-value v)
1239 do (let ((newv (delq nil (mapcar #'(lambda (entry)
1240 (unless (member (car entry) extra)
1241 entry))
1242 v))))
1243 (registry-delete db (list k) nil)
1244 (gnus-registry-insert db k newv)))
1245 (registry-reindex db))))
1247 ;; TODO: a few things
1249 (provide 'gnus-registry)
1251 ;;; gnus-registry.el ends here