1 ;;; gnus-registry.el --- article registry for Gnus
3 ;;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;;; Free Software Foundation, Inc.
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news registry
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This is the gnus-registry.el package, which works with all
27 ;; backends, not just nnmail (e.g. NNTP). The major issue is that it
28 ;; doesn't go across backends, so for instance if an article is in
29 ;; nnml:sys and you see a reference to it in nnimap splitting, the
30 ;; article will end up in nnimap:sys
32 ;; gnus-registry.el intercepts article respooling, moving, deleting,
33 ;; and copying for all backends. If it doesn't work correctly for
34 ;; you, submit a bug report and I'll be glad to fix it. It needs
35 ;; documentation in the manual (also on my to-do list).
37 ;; Put this in your startup file (~/.gnus.el for instance)
39 ;; (setq gnus-registry-max-entries 2500
40 ;; gnus-registry-use-long-group-names t)
42 ;; (gnus-registry-initialize)
44 ;; Then use this in your fancy-split:
46 ;; (: gnus-registry-split-fancy-with-parent)
50 ;; - get the correct group on spool actions
52 ;; - articles that are spooled to a different backend should be handled
56 (eval-when-compile (require 'cl
))
64 (defvar gnus-adaptive-word-syntax-table
)
66 (defvar gnus-registry-dirty t
67 "Boolean set to t when the registry is modified")
69 (defgroup gnus-registry nil
74 (defvar gnus-registry-hashtb
(make-hash-table
77 "*The article registry by Message ID.")
79 (defcustom gnus-registry-marks
82 :image
"summary_important")
85 :image
"summary_work")
88 :image
"summary_personal")
91 :image
"summary_todo")
94 :image
"summary_later"))
96 "List of registry marks and their options.
98 `gnus-registry-mark-article' will offer symbols from this list
101 Each entry must have a character to be useful for summary mode
102 line display and for keyboard shortcuts.
104 Each entry must have an image string to be useful for visual
106 :group
'gnus-registry
107 :type
'(repeat :tag
"Registry Marks"
110 (checklist :tag
"Options" :greedy t
112 (const :format
"" :value
:char
)
113 (character :tag
"Character code"))
115 (const :format
"" :value
:image
)
116 (string :tag
"Image"))))))
118 (defcustom gnus-registry-default-mark
'To-Do
119 "The default mark. Should be a valid key for `gnus-registry-marks'."
120 :group
'gnus-registry
123 (defcustom gnus-registry-unfollowed-groups
124 '("delayed$" "drafts$" "queue$" "INBOX$")
125 "List of groups that gnus-registry-split-fancy-with-parent won't return.
126 The group names are matched, they don't have to be fully
127 qualified. This parameter tells the Registry 'never split a
128 message into a group that matches one of these, regardless of
130 :group
'gnus-registry
131 :type
'(repeat regexp
))
133 (defcustom gnus-registry-install
'ask
134 "Whether the registry should be installed."
135 :group
'gnus-registry
136 :type
'(choice (const :tag
"Never Install" nil
)
137 (const :tag
"Always Install" t
)
138 (const :tag
"Ask Me" ask
)))
140 (defcustom gnus-registry-clean-empty t
141 "Whether the empty registry entries should be deleted.
142 Registry entries are considered empty when they have no groups
144 :group
'gnus-registry
147 (defcustom gnus-registry-use-long-group-names nil
148 "Whether the registry should use long group names (BUGGY)."
149 :group
'gnus-registry
152 (defcustom gnus-registry-track-extra nil
153 "Whether the registry should track extra data about a message.
154 The Subject and Sender (From:) headers are currently tracked this
156 :group
'gnus-registry
158 '(set :tag
"Tracking choices"
159 (const :tag
"Track by subject (Subject: header)" subject
)
160 (const :tag
"Track by sender (From: header)" sender
)))
162 (defcustom gnus-registry-split-strategy nil
163 "Whether the registry should track extra data about a message.
164 The Subject and Sender (From:) headers are currently tracked this
166 :group
'gnus-registry
168 '(choice :tag
"Tracking choices"
169 (const :tag
"Only use single choices, discard multiple matches" nil
)
170 (const :tag
"Majority of matches wins" majority
)
171 (const :tag
"First found wins" first
)))
173 (defcustom gnus-registry-entry-caching t
174 "Whether the registry should cache extra information."
175 :group
'gnus-registry
178 (defcustom gnus-registry-minimum-subject-length
5
179 "The minimum length of a subject before it's considered trackable."
180 :group
'gnus-registry
183 (defcustom gnus-registry-trim-articles-without-groups t
184 "Whether the registry should clean out message IDs without groups."
185 :group
'gnus-registry
188 (defcustom gnus-registry-extra-entries-precious
'(marks)
189 "What extra entries are precious, meaning they won't get trimmed.
190 When you save the Gnus registry, it's trimmed to be no longer
191 than `gnus-registry-max-entries' (which is nil by default, so no
192 trimming happens). Any entries with extra data in this list (by
193 default, marks are included, so articles with marks are
194 considered precious) will not be trimmed."
195 :group
'gnus-registry
196 :type
'(repeat symbol
))
198 (defcustom gnus-registry-cache-file
200 (or gnus-dribble-directory gnus-home-directory
"~/")
201 ".gnus.registry.eld")
202 "File where the Gnus registry will be stored."
203 :group
'gnus-registry
206 (defcustom gnus-registry-max-entries nil
207 "Maximum number of entries in the registry, nil for unlimited."
208 :group
'gnus-registry
209 :type
'(radio (const :format
"Unlimited " nil
)
210 (integer :format
"Maximum number: %v")))
212 (defun gnus-registry-track-subject-p ()
213 (memq 'subject gnus-registry-track-extra
))
215 (defun gnus-registry-track-sender-p ()
216 (memq 'sender gnus-registry-track-extra
))
218 (defun gnus-registry-cache-read ()
219 "Read the registry cache file."
221 (let ((file gnus-registry-cache-file
))
222 (when (file-exists-p file
)
223 (gnus-message 5 "Reading %s..." file
)
225 (gnus-message 5 "Reading %s...done" file
))))
227 ;; FIXME: Get rid of duplicated code, cf. `gnus-save-newsrc-file' in
228 ;; `gnus-start.el'. --rsteib
229 (defun gnus-registry-cache-save ()
230 "Save the registry cache file."
232 (let ((file gnus-registry-cache-file
))
234 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
235 (make-local-variable 'version-control
)
236 (setq version-control gnus-backup-startup-file
)
237 (setq buffer-file-name file
)
238 (setq default-directory
(file-name-directory buffer-file-name
))
239 (buffer-disable-undo)
241 (gnus-message 5 "Saving %s..." file
)
242 (if gnus-save-startup-file-via-temp-buffer
243 (let ((coding-system-for-write gnus-ding-file-coding-system
)
244 (standard-output (current-buffer)))
245 (gnus-gnus-to-quick-newsrc-format
246 t
"gnus registry startup file" 'gnus-registry-alist
)
247 (gnus-registry-cache-whitespace file
)
249 (let ((coding-system-for-write gnus-ding-file-coding-system
)
250 (version-control gnus-backup-startup-file
)
252 (working-dir (file-name-directory file
))
255 ;; Generate the name of a non-existent file.
256 (while (progn (setq working-file
258 (if (and (eq system-type
'ms-dos
)
259 (not (gnus-long-file-names)))
260 "%s#%d.tm#" ; MSDOS limits files to 8+3
261 (if (memq system-type
'(vax-vms axp-vms
))
264 working-dir
(setq i
(1+ i
))))
265 (file-exists-p working-file
)))
269 (gnus-with-output-to-file working-file
270 (gnus-gnus-to-quick-newsrc-format
271 t
"gnus registry startup file" 'gnus-registry-alist
))
273 ;; These bindings will mislead the current buffer
274 ;; into thinking that it is visiting the startup
276 (let ((buffer-backed-up nil
)
277 (buffer-file-name startup-file
)
278 (file-precious-flag t
)
279 (setmodes (file-modes startup-file
)))
280 ;; Backup the current version of the startup file.
283 ;; Replace the existing startup file with the temp file.
284 (rename-file working-file startup-file t
)
285 (gnus-set-file-modes startup-file setmodes
)))
287 (delete-file working-file
)
290 (gnus-kill-buffer (current-buffer))
291 (gnus-message 5 "Saving %s...done" file
))))
293 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
294 ;; Save the gnus-registry file with extra line breaks.
295 (defun gnus-registry-cache-whitespace (filename)
296 (gnus-message 7 "Adding whitespace to %s" filename
)
298 (goto-char (point-min))
299 (while (re-search-forward "^(\\|(\\\"" nil t
)
300 (replace-match "\n\\&" t
))
301 (goto-char (point-min))
302 (while (re-search-forward " $" nil t
)
303 (replace-match "" t t
))))
305 (defun gnus-registry-save (&optional force
)
306 (when (or gnus-registry-dirty force
)
307 (let ((caching gnus-registry-entry-caching
))
308 ;; turn off entry caching, so mtime doesn't get recorded
309 (setq gnus-registry-entry-caching nil
)
310 ;; remove entry caches
313 (if (hash-table-p value
)
314 (remhash key gnus-registry-hashtb
)))
315 gnus-registry-hashtb
)
316 ;; remove empty entries
317 (when gnus-registry-clean-empty
318 (gnus-registry-clean-empty-function))
319 ;; now trim and clean text properties from the registry appropriately
320 (setq gnus-registry-alist
321 (gnus-registry-remove-alist-text-properties
323 (gnus-hashtable-to-alist
324 gnus-registry-hashtb
))))
326 (gnus-registry-cache-save)
327 (setq gnus-registry-entry-caching caching
)
328 (setq gnus-registry-dirty nil
))))
330 (defun gnus-registry-clean-empty-function ()
331 "Remove all empty entries from the registry. Returns count thereof."
337 (dolist (group (gnus-registry-fetch-groups key
))
338 (when (gnus-parameter-registry-ignore group
)
341 "gnus-registry: deleted ignored group %s from key %s"
343 (gnus-registry-delete-group key group
)))
345 (unless (gnus-registry-group-count key
)
346 (gnus-registry-delete-id key
))
349 (gnus-registry-fetch-group key
)
350 ;; TODO: look for specific extra data here!
351 ;; in this example, we look for 'label
352 (gnus-registry-fetch-extra key
'label
))
354 (gnus-registry-delete-id key
))
356 (unless (stringp key
)
359 "gnus-registry key %s was not a string, removing"
361 (gnus-registry-delete-id key
))))
363 gnus-registry-hashtb
)
366 (defun gnus-registry-read ()
367 (gnus-registry-cache-read)
368 (setq gnus-registry-hashtb
(gnus-alist-to-hashtable gnus-registry-alist
))
369 (setq gnus-registry-dirty nil
))
371 (defun gnus-registry-remove-alist-text-properties (v)
372 "Remove text properties from all strings in alist."
374 (gnus-string-remove-all-properties v
)
375 (if (and (listp v
) (listp (cdr v
)))
376 (mapcar 'gnus-registry-remove-alist-text-properties v
)
377 (if (and (listp v
) (stringp (cdr v
)))
378 (cons (gnus-registry-remove-alist-text-properties (car v
))
379 (gnus-registry-remove-alist-text-properties (cdr v
)))
382 (defun gnus-registry-trim (alist)
383 "Trim alist to size, using gnus-registry-max-entries.
384 Any entries with extra data (marks, currently) are left alone."
385 (if (null gnus-registry-max-entries
)
386 alist
; just return the alist
387 ;; else, when given max-entries, trim the alist
388 (let* ((timehash (make-hash-table
391 (precious (make-hash-table
394 (trim-length (- (length alist
) gnus-registry-max-entries
))
395 (trim-length (if (natnump trim-length
) trim-length
0))
396 precious-list junk-list
)
399 (let ((extra (gnus-registry-fetch-extra key
)))
400 (dolist (item gnus-registry-extra-entries-precious
)
402 (when (equal (nth 0 e
) item
)
403 (puthash key t precious
)
405 (puthash key
(gnus-registry-fetch-extra key
'mtime
) timehash
)))
406 gnus-registry-hashtb
)
409 (let ((key (nth 0 item
)))
410 (if (gethash key precious
)
411 (push item precious-list
)
412 (push item junk-list
))))
417 (let ((t1 (or (cdr (gethash (car a
) timehash
))
419 (t2 (or (cdr (gethash (car b
) timehash
))
421 (time-less-p t1 t2
))))
423 ;; we use the return value of this setq, which is the trimmed alist
424 (setq alist
(append precious-list
425 (nthcdr trim-length junk-list
))))))
427 (defun gnus-registry-action (action data-header from
&optional to method
)
428 (let* ((id (mail-header-id data-header
))
429 (subject (gnus-string-remove-all-properties
430 (gnus-registry-simplify-subject
431 (mail-header-subject data-header
))))
432 (sender (gnus-string-remove-all-properties
433 (mail-header-from data-header
)))
434 (from (gnus-group-guess-full-name-from-command-method from
))
435 (to (if to
(gnus-group-guess-full-name-from-command-method to
) nil
))
436 (to-name (if to to
"the Bit Bucket"))
437 (old-entry (gethash id gnus-registry-hashtb
)))
438 (gnus-message 7 "Registry: article %s %s from %s to %s"
440 (if method
"respooling" "going")
444 ;; All except copy will need a delete
445 (gnus-registry-delete-group id from
)
447 (when (equal 'copy action
)
448 (gnus-registry-add-group id from subject sender
)) ; undo the delete
450 (gnus-registry-add-group id to subject sender
)))
452 (defun gnus-registry-spool-action (id group
&optional subject sender
)
453 (let ((group (gnus-group-guess-full-name-from-command-method group
)))
454 (when (and (stringp id
) (string-match "\r$" id
))
455 (setq id
(substring id
0 -
1)))
456 (gnus-message 7 "Registry: article %s spooled to %s"
459 (gnus-registry-add-group id group subject sender
)))
461 ;; Function for nn{mail|imap}-split-fancy: look up all references in
462 ;; the cache and if a match is found, return that group.
463 (defun gnus-registry-split-fancy-with-parent ()
464 "Split this message into the same group as its parent. The parent
465 is obtained from the registry. This function can be used as an entry
466 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
467 this: (: gnus-registry-split-fancy-with-parent)
469 This function tracks ALL backends, unlike
470 `nnmail-split-fancy-with-parent' which tracks only nnmail
473 For a message to be split, it looks for the parent message in the
474 References or In-Reply-To header and then looks in the registry
475 to see which group that message was put in. This group is
476 returned, unless `gnus-registry-follow-group-p' return nil for
479 See the Info node `(gnus)Fancy Mail Splitting' for more details."
480 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
481 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
482 ;; now, if reply-to is valid, append it to the References
484 (concat refstr
" " reply-to
)
486 ;; these may not be used, but the code is cleaner having them up here
487 (sender (gnus-string-remove-all-properties
488 (message-fetch-field "from")))
489 (subject (gnus-string-remove-all-properties
490 (gnus-registry-simplify-subject
491 (message-fetch-field "subject"))))
493 (nnmail-split-fancy-with-parent-ignore-groups
494 (if (listp nnmail-split-fancy-with-parent-ignore-groups
)
495 nnmail-split-fancy-with-parent-ignore-groups
496 (list nnmail-split-fancy-with-parent-ignore-groups
)))
497 (log-agent "gnus-registry-split-fancy-with-parent")
500 ;; this is a big if-else statement. it uses
501 ;; gnus-registry-post-process-groups to filter the results after
504 ;; the references string must be valid and parse to valid references
505 ((and refstr
(gnus-extract-references refstr
))
506 (dolist (reference (nreverse (gnus-extract-references refstr
)))
509 "%s is looking for matches for reference %s from [%s]"
510 log-agent reference refstr
)
511 (dolist (group (gnus-registry-fetch-groups reference
))
512 (when (and group
(gnus-registry-follow-group-p group
))
515 "%s traced the reference %s from [%s] to group %s"
516 log-agent reference refstr group
)
517 (push group found
))))
518 ;; filter the found groups and return them
519 ;; the found groups are the full groups
520 (setq found
(gnus-registry-post-process-groups
521 "references" refstr found found
)))
523 ;; else: there were no matches, now try the extra tracking by sender
524 ((and (gnus-registry-track-sender-p)
526 (not (equal (gnus-extract-address-component-email sender
)
530 (let ((this-sender (cdr
531 (gnus-registry-fetch-extra key
'sender
)))
533 (when (and this-sender
534 (equal sender this-sender
))
535 (let ((groups (gnus-registry-fetch-groups key
)))
536 (dolist (group groups
)
537 (push group found-full
)
538 (setq found
(append (list group
) (delete group found
)))))
541 ;; raise level of messaging if gnus-registry-track-extra
542 (if gnus-registry-track-extra
7 9)
543 "%s (extra tracking) traced sender %s to groups %s (keys %s)"
544 log-agent sender found matches
))))
545 gnus-registry-hashtb
)
546 ;; filter the found groups and return them
547 ;; the found groups are NOT the full groups
548 (setq found
(gnus-registry-post-process-groups
549 "sender" sender found found-full
)))
551 ;; else: there were no matches, now try the extra tracking by subject
552 ((and (gnus-registry-track-subject-p)
554 (< gnus-registry-minimum-subject-length
(length subject
)))
557 (let ((this-subject (cdr
558 (gnus-registry-fetch-extra key
'subject
)))
560 (when (and this-subject
561 (equal subject this-subject
))
562 (let ((groups (gnus-registry-fetch-groups key
)))
563 (dolist (group groups
)
564 (push group found-full
)
565 (setq found
(append (list group
) (delete group found
)))))
568 ;; raise level of messaging if gnus-registry-track-extra
569 (if gnus-registry-track-extra
7 9)
570 "%s (extra tracking) traced subject %s to groups %s (keys %s)"
571 log-agent subject found matches
))))
572 gnus-registry-hashtb
)
573 ;; filter the found groups and return them
574 ;; the found groups are NOT the full groups
575 (setq found
(gnus-registry-post-process-groups
576 "subject" subject found found-full
))))
577 ;; after the (cond) we extract the actual value safely
580 (defun gnus-registry-post-process-groups (mode key groups groups-full
)
581 "Modifies GROUPS found by MODE for KEY to determine which ones to follow.
583 MODE can be 'subject' or 'sender' for example. The KEY is the
584 value by which MODE was searched.
586 Transforms each group name to the equivalent short name.
588 Checks if the current Gnus method (from `gnus-command-method' or
589 from `gnus-newsgroup-name') is the same as the group's method.
590 This is not possible if gnus-registry-use-long-group-names is
591 false. Foreign methods are not supported so they are rejected.
593 Reduces the list to a single group, or complains if that's not
594 possible. Uses `gnus-registry-split-strategy' and GROUPS-FULL if
596 (let ((log-agent "gnus-registry-post-process-group")
599 ;; the strategy can be 'first, 'majority, or nil
600 (when (eq gnus-registry-split-strategy
'first
)
602 (setq groups
(list (car-safe groups
)))))
604 (when (eq gnus-registry-split-strategy
'majority
)
605 (let ((freq (make-hash-table
608 (mapc (lambda(x) (puthash x
(1+ (gethash x freq
0)) freq
)) groups-full
)
609 (setq groups
(list (car-safe
613 (> (gethash a freq
0)
614 (gethash b freq
0)))))))))
616 (if gnus-registry-use-long-group-names
617 (dolist (group groups
)
618 (let ((m1 (gnus-find-method-for-group group
))
619 (m2 (or gnus-command-method
620 (gnus-find-method-for-group gnus-newsgroup-name
)))
621 (short-name (gnus-group-short-name group
)))
622 (if (gnus-methods-equal-p m1 m2
)
624 ;; this is REALLY just for debugging
627 "%s stripped group %s to %s"
628 log-agent group short-name
)
629 (unless (member short-name out
)
630 (push short-name out
)))
634 "%s ignored foreign group %s"
640 "%s: too many extra matches (%s) for %s %s. Returning none."
641 log-agent out mode key
)
645 (defun gnus-registry-follow-group-p (group)
646 "Determines if a group name should be followed.
647 Consults `gnus-registry-unfollowed-groups' and
648 `nnmail-split-fancy-with-parent-ignore-groups'."
649 (not (or (gnus-registry-grep-in-list
651 gnus-registry-unfollowed-groups
)
652 (gnus-registry-grep-in-list
654 nnmail-split-fancy-with-parent-ignore-groups
))))
656 (defun gnus-registry-wash-for-keywords (&optional force
)
658 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article
))
660 (if (or (not (gnus-registry-fetch-extra id
'keywords
))
663 (set-buffer gnus-article-buffer
)
665 (save-window-excursion
667 (narrow-to-region (point) (point-max))
668 (with-syntax-table gnus-adaptive-word-syntax-table
669 (while (re-search-forward "\\b\\w+\\b" nil t
)
670 (setq word
(gnus-registry-remove-alist-text-properties
671 (downcase (buffer-substring
672 (match-beginning 0) (match-end 0)))))
673 (if (> (length word
) 3)
674 (push word words
))))))
675 (gnus-registry-store-extra-entry id
'keywords words
)))))
677 (defun gnus-registry-find-keywords (keyword)
678 (interactive "skeyword: ")
682 (when (member keyword
683 (cdr-safe (gnus-registry-fetch-extra key
'keywords
)))
684 (push key articles
)))
685 gnus-registry-hashtb
)
688 (defun gnus-registry-register-message-ids ()
689 "Register the Message-ID of every article in the group"
690 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name
)
691 (dolist (article gnus-newsgroup-articles
)
692 (let ((id (gnus-registry-fetch-message-id-fast article
)))
693 (unless (member gnus-newsgroup-name
(gnus-registry-fetch-groups id
))
694 (gnus-message 9 "Registry: Registering article %d with group %s"
695 article gnus-newsgroup-name
)
696 (gnus-registry-add-group
699 (gnus-registry-fetch-simplified-message-subject-fast article
)
700 (gnus-registry-fetch-sender-fast article
)))))))
702 (defun gnus-registry-fetch-message-id-fast (article)
703 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
704 (if (and (numberp article
)
705 (assoc article
(gnus-data-list nil
)))
706 (mail-header-id (gnus-data-header (assoc article
(gnus-data-list nil
))))
709 (defun gnus-registry-simplify-subject (subject)
710 (if (stringp subject
)
711 (gnus-simplify-subject subject
)
714 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
715 "Fetch the Subject quickly, using the internal gnus-data-list function"
716 (if (and (numberp article
)
717 (assoc article
(gnus-data-list nil
)))
718 (gnus-string-remove-all-properties
719 (gnus-registry-simplify-subject
720 (mail-header-subject (gnus-data-header
721 (assoc article
(gnus-data-list nil
))))))
724 (defun gnus-registry-fetch-sender-fast (article)
725 "Fetch the Sender quickly, using the internal gnus-data-list function"
726 (if (and (numberp article
)
727 (assoc article
(gnus-data-list nil
)))
728 (gnus-string-remove-all-properties
729 (mail-header-from (gnus-data-header
730 (assoc article
(gnus-data-list nil
)))))
733 (defun gnus-registry-grep-in-list (word list
)
734 "Find if a WORD matches any regular expression in the given LIST."
735 (when (and word list
)
738 (when (string-match r word
)
739 (throw 'found r
))))))
741 (defun gnus-registry-do-marks (type function
)
742 "For each known mark, call FUNCTION for each cell of type TYPE.
744 FUNCTION should take two parameters, a mark symbol and the cell value."
745 (dolist (mark-info gnus-registry-marks
)
746 (let* ((mark (car-safe mark-info
))
747 (data (cdr-safe mark-info
))
748 (cell-data (plist-get data type
)))
750 (funcall function mark cell-data
)))))
752 ;;; this is ugly code, but I don't know how to do it better
753 (defun gnus-registry-install-shortcuts ()
754 "Install the keyboard shortcuts and menus for the registry.
755 Uses `gnus-registry-marks' to find what shortcuts to install."
757 (gnus-registry-do-marks
760 (let ((function-format
761 (format "gnus-registry-%%s-article-%s-mark" mark
)))
763 ;;; The following generates these functions:
764 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
765 ;;; "Apply the Important mark to process-marked ARTICLES."
766 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
767 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
768 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
769 ;;; "Apply the Important mark to process-marked ARTICLES."
770 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
771 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
773 (dolist (remove '(t nil
))
774 (let* ((variant-name (if remove
"remove" "set"))
775 (function-name (format function-format variant-name
))
776 (shortcut (format "%c" data
))
777 (shortcut (if remove
(upcase shortcut
) shortcut
)))
778 (unintern function-name
)
782 ,(intern function-name
)
783 ;; parameter definition
787 "%s the %s mark over process-marked ARTICLES."
788 (upcase-initials variant-name
)
790 ;; interactive definition
792 (gnus-summary-work-articles current-prefix-arg
))
795 ;; if this is called and the user doesn't want the
796 ;; registry enabled, we'll ask anyhow
797 (when (eq gnus-registry-install nil
)
798 (setq gnus-registry-install
'ask
))
800 ;; now the user is asked if gnus-registry-install is 'ask
801 (when (gnus-registry-install-p)
802 (gnus-registry-set-article-mark-internal
803 ;; all this just to get the mark, I must be doing it wrong
804 (intern ,(symbol-name mark
))
806 (dolist (article articles
)
807 (gnus-summary-update-article
809 (assoc article
(gnus-data-list nil
)))))))
810 (push (intern function-name
) keys-plist
)
811 (push shortcut keys-plist
)
814 "Defined mark handling function %s"
817 '(gnus-registry-mark-map "M" gnus-summary-mark-map
)
821 ;;; (defalias 'gnus-user-format-function-M
822 ;;; 'gnus-registry-user-format-function-M)
823 (defun gnus-registry-user-format-function-M (headers)
824 (let* ((id (mail-header-message-id headers
))
825 (marks (when id
(gnus-registry-fetch-extra-marks id
))))
826 (apply 'concat
(mapcar (lambda(mark)
830 (assoc mark gnus-registry-marks
))
837 (defun gnus-registry-read-mark ()
838 "Read a mark name from the user with completion."
839 (let ((mark (gnus-completing-read-with-default
840 (symbol-name gnus-registry-default-mark
)
842 (mapcar (lambda (x) ; completion list
843 (cons (symbol-name (car-safe x
)) (car-safe x
)))
844 gnus-registry-marks
))))
848 (defun gnus-registry-set-article-mark (&rest articles
)
849 "Apply a mark to process-marked ARTICLES."
850 (interactive (gnus-summary-work-articles current-prefix-arg
))
851 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark) articles nil t
))
853 (defun gnus-registry-remove-article-mark (&rest articles
)
854 "Remove a mark from process-marked ARTICLES."
855 (interactive (gnus-summary-work-articles current-prefix-arg
))
856 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark) articles t t
))
858 (defun gnus-registry-set-article-mark-internal (mark articles
&optional remove show-message
)
859 "Apply a mark to a list of ARTICLES."
860 (let ((article-id-list
861 (mapcar 'gnus-registry-fetch-message-id-fast articles
)))
862 (dolist (id article-id-list
)
864 ;; all the marks for this article without the mark of
867 (delq mark
(gnus-registry-fetch-extra-marks id
)))
868 ;; the new marks we want to use
869 (new-marks (if remove
873 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
874 (if remove
"Removing" "Adding")
877 (apply 'gnus-registry-store-extra-marks
; set the extra marks
878 id
; for the message ID
881 (defun gnus-registry-get-article-marks (&rest articles
)
882 "Get the Gnus registry marks for ARTICLES and show them if interactive.
883 Uses process/prefix conventions. For multiple articles,
884 only the last one's marks are returned."
885 (interactive (gnus-summary-work-articles 1))
887 (dolist (article articles
)
889 (gnus-registry-fetch-message-id-fast article
)))
890 (setq marks
(gnus-registry-fetch-extra-marks article-id
))))
891 (when (interactive-p)
892 (gnus-message 1 "Marks are %S" marks
))
895 ;;; if this extends to more than 'marks, it should be improved to be more generic.
896 (defun gnus-registry-fetch-extra-marks (id)
897 "Get the marks of a message, based on the message ID.
898 Returns a list of symbol marks or nil."
899 (car-safe (cdr (gnus-registry-fetch-extra id
'marks
))))
901 (defun gnus-registry-has-extra-mark (id mark
)
902 "Checks if a message has `mark', based on the message ID `id'."
903 (memq mark
(gnus-registry-fetch-extra-marks id
)))
905 (defun gnus-registry-store-extra-marks (id &rest mark-list
)
906 "Set the marks of a message, based on the message ID.
907 The `mark-list' can be nil, in which case no marks are left."
908 (gnus-registry-store-extra-entry id
'marks
(list mark-list
)))
910 (defun gnus-registry-delete-extra-marks (id &rest mark-delete-list
)
911 "Delete the message marks in `mark-delete-list', based on the message ID."
912 (let ((marks (gnus-registry-fetch-extra-marks id
)))
914 (dolist (mark mark-delete-list
)
915 (setq marks
(delq mark marks
))))
916 (gnus-registry-store-extra-marks id
(car marks
))))
918 (defun gnus-registry-delete-all-extra-marks (id)
919 "Delete all the marks for a message ID."
920 (gnus-registry-store-extra-marks id nil
))
922 (defun gnus-registry-fetch-extra (id &optional entry
)
923 "Get the extra data of a message, based on the message ID.
924 Returns the first place where the trail finds a nonstring."
925 (let ((entry-cache (gethash entry gnus-registry-hashtb
)))
927 (hash-table-p entry-cache
)
928 (gethash id entry-cache
))
929 (gethash id entry-cache
)
930 ;; else, if there is no caching possible...
931 (let ((trail (gethash id gnus-registry-hashtb
)))
933 (dolist (crumb trail
)
934 (unless (stringp crumb
)
935 (return (gnus-registry-fetch-extra-entry crumb entry id
)))))))))
937 (defun gnus-registry-fetch-extra-entry (alist &optional entry id
)
938 "Get the extra data of a message, or a specific entry in it.
939 Update the entry cache if needed."
941 (let ((entry-cache (gethash entry gnus-registry-hashtb
))
943 (when gnus-registry-entry-caching
944 ;; create the hash table
945 (unless (hash-table-p entry-cache
)
946 (setq entry-cache
(make-hash-table
949 (puthash entry entry-cache gnus-registry-hashtb
))
951 ;; get the entree from the hash table or from the alist
952 (setq entree
(gethash id entry-cache
)))
955 (setq entree
(assq entry alist
))
956 (when gnus-registry-entry-caching
957 (puthash id entree entry-cache
)))
961 (defun gnus-registry-store-extra (id extra
)
962 "Store the extra data of a message, based on the message ID.
963 The message must have at least one group name."
964 (when (gnus-registry-group-count id
)
965 ;; we now know the trail has at least 1 group name, so it's not empty
966 (let ((trail (gethash id gnus-registry-hashtb
))
967 (old-extra (gnus-registry-fetch-extra id
))
969 (dolist (crumb trail
)
970 (unless (stringp crumb
)
971 (dolist (entry crumb
)
972 (setq entry-cache
(gethash (car entry
) gnus-registry-hashtb
))
974 (remhash id entry-cache
))))
975 (puthash id
(cons extra
(delete old-extra trail
))
976 gnus-registry-hashtb
)
977 (setq gnus-registry-dirty t
)))))
979 (defun gnus-registry-delete-extra-entry (id key
)
980 "Delete a specific entry in the extras field of the registry entry for id."
981 (gnus-registry-store-extra-entry id key nil
))
983 (defun gnus-registry-store-extra-entry (id key value
)
984 "Put a specific entry in the extras field of the registry entry for id."
985 (let* ((extra (gnus-registry-fetch-extra id
))
986 ;; all the entries except the one for `key'
987 (the-rest (gnus-assq-delete-all key
(gnus-registry-fetch-extra id
)))
989 (gnus-registry-remove-alist-text-properties
990 (cons (cons key value
)
993 (gnus-registry-store-extra id alist
)))
995 (defun gnus-registry-fetch-group (id)
996 "Get the group of a message, based on the message ID.
997 Returns the first place where the trail finds a group name."
998 (when (gnus-registry-group-count id
)
999 ;; we now know the trail has at least 1 group name
1000 (let ((trail (gethash id gnus-registry-hashtb
)))
1001 (dolist (crumb trail
)
1002 (when (stringp crumb
)
1003 (return (if gnus-registry-use-long-group-names
1005 (gnus-group-short-name crumb
))))))))
1007 (defun gnus-registry-fetch-groups (id)
1008 "Get the groups of a message, based on the message ID."
1009 (let ((trail (gethash id gnus-registry-hashtb
))
1011 (dolist (crumb trail
)
1012 (when (stringp crumb
)
1013 ;; push the group name into the list
1017 (if (or (not (stringp crumb
)) gnus-registry-use-long-group-names
)
1019 (gnus-group-short-name crumb
))
1021 ;; return the list of groups
1024 (defun gnus-registry-group-count (id)
1025 "Get the number of groups of a message, based on the message ID."
1026 (let ((trail (gethash id gnus-registry-hashtb
)))
1027 (if (and trail
(listp trail
))
1028 (apply '+ (mapcar (lambda (x) (if (stringp x
) 1 0)) trail
))
1031 (defun gnus-registry-delete-group (id group
)
1032 "Delete a group for a message, based on the message ID."
1033 (when (and group id
)
1034 (let ((trail (gethash id gnus-registry-hashtb
))
1035 (short-group (gnus-group-short-name group
)))
1036 (puthash id
(if trail
1037 (delete short-group
(delete group trail
))
1039 gnus-registry-hashtb
))
1040 ;; now, clear the entry if there are no more groups
1041 (when gnus-registry-trim-articles-without-groups
1042 (unless (gnus-registry-group-count id
)
1043 (gnus-registry-delete-id id
)))
1044 ;; is this ID still in the registry?
1045 (when (gethash id gnus-registry-hashtb
)
1046 (gnus-registry-store-extra-entry id
'mtime
(current-time)))))
1048 (defun gnus-registry-delete-id (id)
1049 "Delete a message ID from the registry."
1051 (remhash id gnus-registry-hashtb
)
1054 (when (hash-table-p value
)
1055 (remhash id value
)))
1056 gnus-registry-hashtb
)))
1058 (defun gnus-registry-add-group (id group
&optional subject sender
)
1059 "Add a group for a message, based on the message ID."
1062 (not (string-match "totally-fudged-out-message-id" id
)))
1063 (let ((full-group group
)
1064 (group (if gnus-registry-use-long-group-names
1066 (gnus-group-short-name group
))))
1067 (gnus-registry-delete-group id group
)
1069 (unless gnus-registry-use-long-group-names
;; unnecessary in this case
1070 (gnus-registry-delete-group id full-group
))
1072 (let ((trail (gethash id gnus-registry-hashtb
)))
1073 (puthash id
(if trail
1076 gnus-registry-hashtb
)
1078 (when (and (gnus-registry-track-subject-p)
1080 (gnus-registry-store-extra-entry
1083 (gnus-registry-simplify-subject subject
)))
1084 (when (and (gnus-registry-track-sender-p)
1086 (gnus-registry-store-extra-entry
1091 (gnus-registry-store-extra-entry id
'mtime
(current-time)))))))
1093 (defun gnus-registry-clear ()
1094 "Clear the Gnus registry."
1096 (setq gnus-registry-alist nil
)
1097 (setq gnus-registry-hashtb
(gnus-alist-to-hashtable gnus-registry-alist
))
1098 (setq gnus-registry-dirty t
))
1101 (defun gnus-registry-initialize ()
1102 "Initialize the Gnus registry."
1104 (gnus-message 5 "Initializing the registry")
1105 (setq gnus-registry-install t
) ; in case it was 'ask or nil
1106 (gnus-registry-install-hooks)
1107 (gnus-registry-install-shortcuts)
1108 (gnus-registry-read))
1111 (defun gnus-registry-install-hooks ()
1112 "Install the registry hooks."
1114 (add-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1115 (add-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1116 (add-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1117 (add-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1119 (add-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1120 (add-hook 'gnus-read-newsrc-el-hook
'gnus-registry-read
)
1122 (add-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1124 (defun gnus-registry-unload-hook ()
1125 "Uninstall the registry hooks."
1127 (remove-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1128 (remove-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1129 (remove-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1130 (remove-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1132 (remove-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1133 (remove-hook 'gnus-read-newsrc-el-hook
'gnus-registry-read
)
1135 (remove-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1137 (add-hook 'gnus-registry-unload-hook
'gnus-registry-unload-hook
)
1139 (defun gnus-registry-install-p ()
1141 (when (eq gnus-registry-install
'ask
)
1142 (setq gnus-registry-install
1144 (concat "Enable the Gnus registry? "
1145 "See the variable `gnus-registry-install' "
1146 "to get rid of this query permanently. ")))
1147 (when gnus-registry-install
1148 ;; we just set gnus-registry-install to t, so initialize the registry!
1149 (gnus-registry-initialize)))
1150 ;;; we could call it here: (customize-variable 'gnus-registry-install)
1151 gnus-registry-install
)
1153 (when (or (eq gnus-registry-install t
)
1154 (gnus-registry-install-p))
1155 (gnus-registry-initialize))
1157 ;; TODO: a few things
1159 (provide 'gnus-registry
)
1161 ;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
1162 ;;; gnus-registry.el ends here