Embiggen pictures slightly.
[gnus] / lisp / gnus-registry.el
1 ;;; gnus-registry.el --- article registry for Gnus
2
3 ;;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;;; Free Software Foundation, Inc.
5
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news registry
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
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
31
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).
36
37 ;; Put this in your startup file (~/.gnus.el for instance)
38
39 ;; (setq gnus-registry-max-entries 2500
40 ;;       gnus-registry-use-long-group-names t)
41
42 ;; (gnus-registry-initialize)
43
44 ;; Then use this in your fancy-split:
45
46 ;; (: gnus-registry-split-fancy-with-parent)
47
48 ;; TODO:
49
50 ;; - get the correct group on spool actions
51
52 ;; - articles that are spooled to a different backend should be handled
53
54 ;;; Code:
55
56 (eval-when-compile (require 'cl))
57
58 (require 'gnus)
59 (require 'gnus-int)
60 (require 'gnus-sum)
61 (require 'gnus-util)
62 (require 'nnmail)
63 (require 'easymenu)
64
65 (defvar gnus-adaptive-word-syntax-table)
66
67 (defvar gnus-registry-dirty t
68  "Boolean set to t when the registry is modified")
69
70 (defgroup gnus-registry nil
71   "The Gnus registry."
72   :version "22.1"
73   :group 'gnus)
74
75 (defvar gnus-registry-hashtb (make-hash-table
76                               :size 256
77                               :test 'equal)
78   "*The article registry by Message ID.")
79
80 (defcustom gnus-registry-marks
81   '((Important
82      :char ?i
83      :image "summary_important")
84     (Work
85      :char ?w
86      :image "summary_work")
87     (Personal
88      :char ?p
89      :image "summary_personal")
90     (To-Do
91      :char ?t
92      :image "summary_todo")
93     (Later
94      :char ?l
95      :image "summary_later"))
96
97   "List of registry marks and their options.
98
99 `gnus-registry-mark-article' will offer symbols from this list
100 for completion.
101
102 Each entry must have a character to be useful for summary mode
103 line display and for keyboard shortcuts.
104
105 Each entry must have an image string to be useful for visual
106 display."
107   :group 'gnus-registry
108   :type '(repeat :tag "Registry Marks"
109                  (cons :tag "Mark"
110                        (symbol :tag "Name")
111                        (checklist :tag "Options" :greedy t
112                                   (group :inline t
113                                          (const :format "" :value :char)
114                                          (character :tag "Character code"))
115                                   (group :inline t
116                                          (const :format "" :value :image)
117                                          (string :tag "Image"))))))
118
119 (defcustom gnus-registry-default-mark 'To-Do
120   "The default mark.  Should be a valid key for `gnus-registry-marks'."
121   :group 'gnus-registry
122   :type 'symbol)
123
124 (defcustom gnus-registry-unfollowed-groups
125   '("delayed$" "drafts$" "queue$" "INBOX$")
126   "List of groups that gnus-registry-split-fancy-with-parent won't return.
127 The group names are matched, they don't have to be fully
128 qualified.  This parameter tells the Registry 'never split a
129 message into a group that matches one of these, regardless of
130 references.'"
131   :group 'gnus-registry
132   :type '(repeat regexp))
133
134 (defcustom gnus-registry-install 'ask
135   "Whether the registry should be installed."
136   :group 'gnus-registry
137   :type '(choice (const :tag "Never Install" nil)
138                  (const :tag "Always Install" t)
139                  (const :tag "Ask Me" ask)))
140
141 (defvar gnus-summary-misc-menu) ;; Avoid byte compiler warning.
142
143 (defvar gnus-registry-misc-menus nil)   ; ugly way to keep the menus
144
145 (defcustom gnus-registry-clean-empty t
146   "Whether the empty registry entries should be deleted.
147 Registry entries are considered empty when they have no groups
148 and no extra data."
149   :group 'gnus-registry
150   :type 'boolean)
151
152 (defcustom gnus-registry-use-long-group-names t
153   "Whether the registry should use long group names."
154   :group 'gnus-registry
155   :type 'boolean)
156
157 (defcustom gnus-registry-max-track-groups 20
158   "The maximum number of non-unique group matches to check for a message ID."
159   :group 'gnus-registry
160   :type '(radio (const :format "Unlimited " nil)
161                 (integer :format "Maximum non-unique matches: %v")))
162
163 (defcustom gnus-registry-track-extra nil
164   "Whether the registry should track extra data about a message.
165 The Subject and Sender (From:) headers are currently tracked this
166 way."
167   :group 'gnus-registry
168   :type
169   '(set :tag "Tracking choices"
170     (const :tag "Track by subject (Subject: header)" subject)
171     (const :tag "Track by sender (From: header)"  sender)))
172
173 (defcustom gnus-registry-split-strategy nil
174   "Whether the registry should track extra data about a message.
175 The Subject and Sender (From:) headers are currently tracked this
176 way."
177   :group 'gnus-registry
178   :type
179   '(choice :tag "Tracking choices"
180            (const :tag "Only use single choices, discard multiple matches" nil)
181            (const :tag "Majority of matches wins" majority)
182            (const :tag "First found wins"  first)))
183
184 (defcustom gnus-registry-entry-caching t
185   "Whether the registry should cache extra information."
186   :group 'gnus-registry
187   :type 'boolean)
188
189 (defcustom gnus-registry-minimum-subject-length 5
190   "The minimum length of a subject before it's considered trackable."
191   :group 'gnus-registry
192   :type 'integer)
193
194 (defcustom gnus-registry-trim-articles-without-groups t
195   "Whether the registry should clean out message IDs without groups."
196   :group 'gnus-registry
197   :type 'boolean)
198
199 (defcustom gnus-registry-extra-entries-precious '(marks)
200   "What extra entries are precious, meaning they won't get trimmed.
201 When you save the Gnus registry, it's trimmed to be no longer
202 than `gnus-registry-max-entries' (which is nil by default, so no
203 trimming happens).  Any entries with extra data in this list (by
204 default, marks are included, so articles with marks are
205 considered precious) will not be trimmed."
206   :group 'gnus-registry
207   :type '(repeat symbol))
208
209 (defcustom gnus-registry-cache-file
210   (nnheader-concat
211    (or gnus-dribble-directory gnus-home-directory "~/")
212    ".gnus.registry.eld")
213   "File where the Gnus registry will be stored."
214   :group 'gnus-registry
215   :type 'file)
216
217 (defcustom gnus-registry-max-entries nil
218   "Maximum number of entries in the registry, nil for unlimited."
219   :group 'gnus-registry
220   :type '(radio (const :format "Unlimited " nil)
221                 (integer :format "Maximum number: %v")))
222
223 (defun gnus-registry-track-subject-p ()
224   (memq 'subject gnus-registry-track-extra))
225
226 (defun gnus-registry-track-sender-p ()
227   (memq 'sender gnus-registry-track-extra))
228
229 (defun gnus-registry-cache-read ()
230   "Read the registry cache file."
231   (interactive)
232   (let ((file gnus-registry-cache-file))
233     (when (file-exists-p file)
234       (gnus-message 5 "Reading %s..." file)
235       (gnus-load file)
236       (gnus-message 5 "Reading %s...done" file))))
237
238 ;; FIXME: Get rid of duplicated code, cf. `gnus-save-newsrc-file' in
239 ;; `gnus-start.el'.  --rsteib
240 (defun gnus-registry-cache-save ()
241   "Save the registry cache file."
242   (interactive)
243   (let ((file gnus-registry-cache-file))
244     (with-current-buffer (gnus-get-buffer-create " *Gnus-registry-cache*")
245       (make-local-variable 'version-control)
246     (setq version-control gnus-backup-startup-file)
247     (setq buffer-file-name file)
248     (setq default-directory (file-name-directory buffer-file-name))
249     (buffer-disable-undo)
250     (erase-buffer)
251     (gnus-message 5 "Saving %s..." file)
252     (if gnus-save-startup-file-via-temp-buffer
253         (let ((coding-system-for-write gnus-ding-file-coding-system)
254               (standard-output (current-buffer)))
255           (gnus-gnus-to-quick-newsrc-format
256            t "gnus registry startup file" 'gnus-registry-alist)
257           (gnus-registry-cache-whitespace file)
258           (save-buffer))
259       (let ((coding-system-for-write gnus-ding-file-coding-system)
260             (version-control gnus-backup-startup-file)
261             (startup-file file)
262             (working-dir (file-name-directory file))
263             working-file
264             (i -1))
265         ;; Generate the name of a non-existent file.
266         (while (progn (setq working-file
267                             (format
268                              (if (and (eq system-type 'ms-dos)
269                                       (not (gnus-long-file-names)))
270                                  "%s#%d.tm#" ; MSDOS limits files to 8+3
271                                "%s#tmp#%d")
272                              working-dir (setq i (1+ i))))
273                       (file-exists-p working-file)))
274
275         (unwind-protect
276             (progn
277               (gnus-with-output-to-file working-file
278                 (gnus-gnus-to-quick-newsrc-format
279                  t "gnus registry startup file" 'gnus-registry-alist))
280
281               ;; These bindings will mislead the current buffer
282               ;; into thinking that it is visiting the startup
283               ;; file.
284               (let ((buffer-backed-up nil)
285                     (buffer-file-name startup-file)
286                     (file-precious-flag t)
287                     (setmodes (file-modes startup-file)))
288                 ;; Backup the current version of the startup file.
289                 (backup-buffer)
290
291                 ;; Replace the existing startup file with the temp file.
292                 (rename-file working-file startup-file t)
293                 (gnus-set-file-modes startup-file setmodes)))
294           (condition-case nil
295               (delete-file working-file)
296             (file-error nil)))))
297
298     (gnus-kill-buffer (current-buffer))
299     (gnus-message 5 "Saving %s...done" file))))
300
301 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
302 ;; Save the gnus-registry file with extra line breaks.
303 (defun gnus-registry-cache-whitespace (filename)
304   (gnus-message 7 "Adding whitespace to %s" filename)
305   (save-excursion
306     (goto-char (point-min))
307     (while (re-search-forward "^(\\|(\\\"" nil t)
308       (replace-match "\n\\&" t))
309     (goto-char (point-min))
310     (while (re-search-forward " $" nil t)
311       (replace-match "" t t))))
312
313 (defun gnus-registry-save (&optional force)
314   (when (or gnus-registry-dirty force)
315     (let ((caching gnus-registry-entry-caching))
316       ;; turn off entry caching, so mtime doesn't get recorded
317       (setq gnus-registry-entry-caching nil)
318       ;; remove entry caches
319       (maphash
320        (lambda (key value)
321          (if (hash-table-p value)
322              (remhash key gnus-registry-hashtb)))
323        gnus-registry-hashtb)
324       ;; remove empty entries
325       (when gnus-registry-clean-empty
326         (gnus-registry-clean-empty-function))
327       ;; now trim and clean text properties from the registry appropriately
328       (setq gnus-registry-alist
329             (gnus-registry-remove-alist-text-properties
330              (gnus-registry-trim
331               (gnus-hashtable-to-alist
332                gnus-registry-hashtb))))
333       ;; really save
334       (gnus-registry-cache-save)
335       (setq gnus-registry-entry-caching caching)
336       (setq gnus-registry-dirty nil))))
337
338 (defun gnus-registry-clean-empty-function ()
339   "Remove all empty entries from the registry.  Returns count thereof."
340   (let ((count 0))
341
342     (maphash
343      (lambda (key value)
344        (when (stringp key)
345          (dolist (group (gnus-registry-fetch-groups key))
346            (when (gnus-parameter-registry-ignore group)
347              (gnus-message
348               10
349               "gnus-registry: deleted ignored group %s from key %s"
350               group key)
351              (gnus-registry-delete-group key group)))
352
353          (unless (gnus-registry-group-count key)
354            (gnus-registry-delete-id key))
355
356          (unless (or
357                   (gnus-registry-fetch-group key)
358                   ;; TODO: look for specific extra data here!
359                   ;; in this example, we look for 'label
360                   (gnus-registry-fetch-extra key 'label))
361            (incf count)
362            (gnus-registry-delete-id key))
363
364          (unless (stringp key)
365            (gnus-message
366             10
367             "gnus-registry key %s was not a string, removing"
368             key)
369            (gnus-registry-delete-id key))))
370
371      gnus-registry-hashtb)
372     count))
373
374 (defun gnus-registry-read ()
375   (gnus-registry-cache-read)
376   (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
377   (setq gnus-registry-dirty nil))
378
379 (defun gnus-registry-remove-alist-text-properties (v)
380   "Remove text properties from all strings in alist."
381   (if (stringp v)
382       (gnus-string-remove-all-properties v)
383     (if (and (listp v) (listp (cdr v)))
384         (mapcar 'gnus-registry-remove-alist-text-properties v)
385       (if (and (listp v) (stringp (cdr v)))
386           (cons (gnus-registry-remove-alist-text-properties (car v))
387                 (gnus-registry-remove-alist-text-properties (cdr v)))
388       v))))
389
390 (defun gnus-registry-trim (alist)
391   "Trim alist to size, using gnus-registry-max-entries.
392 Any entries with extra data (marks, currently) are left alone."
393   (if (null gnus-registry-max-entries)
394       alist                             ; just return the alist
395     ;; else, when given max-entries, trim the alist
396     (let* ((timehash (make-hash-table
397                       :size 20000
398                       :test 'equal))
399            (precious (make-hash-table
400                       :size 20000
401                       :test 'equal))
402            (trim-length (- (length alist) gnus-registry-max-entries))
403            (trim-length (if (natnump trim-length) trim-length 0))
404            precious-list junk-list)
405       (maphash
406        (lambda (key value)
407          (let ((extra (gnus-registry-fetch-extra key)))
408            (dolist (item gnus-registry-extra-entries-precious)
409              (dolist (e extra)
410                (when (equal (nth 0 e) item)
411                  (puthash key t precious)
412                  (return))))
413            (puthash key (gnus-registry-fetch-extra key 'mtime) timehash)))
414        gnus-registry-hashtb)
415
416       (dolist (item alist)
417         (let ((key (nth 0 item)))
418           (if (gethash key precious)
419               (push item precious-list)
420             (push item junk-list))))
421
422       (sort
423        junk-list
424        (lambda (a b)
425          (let ((t1 (or (cdr (gethash (car a) timehash))
426                        '(0 0 0)))
427                (t2 (or (cdr (gethash (car b) timehash))
428                        '(0 0 0))))
429            (time-less-p t1 t2))))
430
431       ;; we use the return value of this setq, which is the trimmed alist
432       (setq alist (append precious-list
433                           (nthcdr trim-length junk-list))))))
434
435 (defun gnus-registry-action (action data-header from &optional to method)
436   (let* ((id (mail-header-id data-header))
437          (subject (gnus-string-remove-all-properties
438                    (gnus-registry-simplify-subject
439                     (mail-header-subject data-header))))
440          (sender (gnus-string-remove-all-properties
441                   (mail-header-from data-header)))
442          (from (gnus-group-guess-full-name-from-command-method from))
443          (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
444          (to-name (if to to "the Bit Bucket"))
445          (old-entry (gethash id gnus-registry-hashtb)))
446     (gnus-message 7 "Registry: article %s %s from %s to %s"
447                   id
448                   (if method "respooling" "going")
449                   from
450                   to)
451
452     ;; All except copy will need a delete
453     (gnus-registry-delete-group id from)
454
455     (when (equal 'copy action)
456       (gnus-registry-add-group id from subject sender)) ; undo the delete
457
458     (gnus-registry-add-group id to subject sender)))
459
460 (defun gnus-registry-spool-action (id group &optional subject sender)
461   (let ((group (gnus-group-guess-full-name-from-command-method group)))
462     (when (and (stringp id) (string-match "\r$" id))
463       (setq id (substring id 0 -1)))
464     (gnus-message 7 "Registry: article %s spooled to %s"
465                   id
466                   group)
467     (gnus-registry-add-group id group subject sender)))
468
469 ;; Function for nn{mail|imap}-split-fancy: look up all references in
470 ;; the cache and if a match is found, return that group.
471 (defun gnus-registry-split-fancy-with-parent ()
472   "Split this message into the same group as its parent.  The parent
473 is obtained from the registry.  This function can be used as an entry
474 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
475 this: (: gnus-registry-split-fancy-with-parent)
476
477 This function tracks ALL backends, unlike
478 `nnmail-split-fancy-with-parent' which tracks only nnmail
479 messages.
480
481 For a message to be split, it looks for the parent message in the
482 References or In-Reply-To header and then looks in the registry
483 to see which group that message was put in.  This group is
484 returned, unless `gnus-registry-follow-group-p' return nil for
485 that group.
486
487 See the Info node `(gnus)Fancy Mail Splitting' for more details."
488   (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
489          (reply-to (message-fetch-field "in-reply-to"))      ; may be nil
490          ;; now, if reply-to is valid, append it to the References
491          (refstr (if reply-to
492                      (concat refstr " " reply-to)
493                    refstr))
494          ;; these may not be used, but the code is cleaner having them up here
495          (sender (gnus-string-remove-all-properties
496                   (message-fetch-field "from")))
497          (subject (gnus-string-remove-all-properties
498                    (gnus-registry-simplify-subject
499                     (message-fetch-field "subject"))))
500
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          (log-agent "gnus-registry-split-fancy-with-parent")
506          found found-full)
507
508     ;; this is a big if-else statement.  it uses
509     ;; gnus-registry-post-process-groups to filter the results after
510     ;; every step.
511     (cond
512      ;; the references string must be valid and parse to valid references
513      ((and refstr (gnus-extract-references refstr))
514       (dolist (reference (nreverse (gnus-extract-references refstr)))
515         (gnus-message
516          9
517          "%s is looking for matches for reference %s from [%s]"
518          log-agent reference refstr)
519         (dolist (group (gnus-registry-fetch-groups
520                         reference
521                         gnus-registry-max-track-groups))
522           (when (and group (gnus-registry-follow-group-p group))
523             (gnus-message
524              7
525              "%s traced the reference %s from [%s] to group %s"
526              log-agent reference refstr group)
527             (push group found))))
528       ;; filter the found groups and return them
529       ;; the found groups are the full groups
530       (setq found (gnus-registry-post-process-groups
531                    "references" refstr found found)))
532
533      ;; else: there were no matches, now try the extra tracking by sender
534      ((and (gnus-registry-track-sender-p)
535            sender
536            (not (equal (gnus-extract-address-component-email sender)
537                        user-mail-address)))
538       (maphash
539        (lambda (key value)
540          (let ((this-sender (cdr
541                              (gnus-registry-fetch-extra key 'sender)))
542                matches)
543            (when (and this-sender
544                       (equal sender this-sender))
545              (let ((groups (gnus-registry-fetch-groups
546                             key
547                             gnus-registry-max-track-groups)))
548                (dolist (group groups)
549                  (push group found-full)
550                  (setq found (append (list group) (delete group found)))))
551              (push key matches)
552              (gnus-message
553               ;; raise level of messaging if gnus-registry-track-extra
554               (if gnus-registry-track-extra 7 9)
555               "%s (extra tracking) traced sender %s to groups %s (keys %s)"
556               log-agent sender found matches))))
557        gnus-registry-hashtb)
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                    "sender" sender found found-full)))
562
563      ;; else: there were no matches, now try the extra tracking by subject
564      ((and (gnus-registry-track-subject-p)
565            subject
566            (< gnus-registry-minimum-subject-length (length subject)))
567       (maphash
568        (lambda (key value)
569          (let ((this-subject (cdr
570                               (gnus-registry-fetch-extra key 'subject)))
571                matches)
572            (when (and this-subject
573                       (equal subject this-subject))
574              (let ((groups (gnus-registry-fetch-groups
575                             key
576                             gnus-registry-max-track-groups)))
577                (dolist (group groups)
578                  (push group found-full)
579                  (setq found (append (list group) (delete group found)))))
580              (push key matches)
581              (gnus-message
582               ;; raise level of messaging if gnus-registry-track-extra
583               (if gnus-registry-track-extra 7 9)
584               "%s (extra tracking) traced subject %s to groups %s (keys %s)"
585               log-agent subject found matches))))
586        gnus-registry-hashtb)
587       ;; filter the found groups and return them
588       ;; the found groups are NOT the full groups
589       (setq found (gnus-registry-post-process-groups
590                    "subject" subject found found-full))))
591     ;; after the (cond) we extract the actual value safely
592     (car-safe found)))
593
594 (defun gnus-registry-post-process-groups (mode key groups groups-full)
595   "Modifies GROUPS found by MODE for KEY to determine which ones to follow.
596
597 MODE can be 'subject' or 'sender' for example.  The KEY is the
598 value by which MODE was searched.
599
600 Transforms each group name to the equivalent short name.
601
602 Checks if the current Gnus method (from `gnus-command-method' or
603 from `gnus-newsgroup-name') is the same as the group's method.
604 This is not possible if gnus-registry-use-long-group-names is
605 false.  Foreign methods are not supported so they are rejected.
606
607 Reduces the list to a single group, or complains if that's not
608 possible.  Uses `gnus-registry-split-strategy' and GROUPS-FULL if
609 necessary."
610   (let ((log-agent "gnus-registry-post-process-group")
611         out)
612
613     ;; the strategy can be 'first, 'majority, or nil
614     (when (eq gnus-registry-split-strategy 'first)
615       (when groups
616         (setq groups (list (car-safe groups)))))
617
618     (when (eq gnus-registry-split-strategy 'majority)
619       (let ((freq (make-hash-table
620                    :size 256
621                    :test 'equal)))
622         (mapc (lambda(x) (puthash x (1+ (gethash x freq 0)) freq)) groups-full)
623         (setq groups (list (car-safe
624                             (sort
625                              groups
626                              (lambda (a b)
627                                (> (gethash a freq 0)
628                                   (gethash b freq 0)))))))))
629
630     (if gnus-registry-use-long-group-names
631         (dolist (group groups)
632           (let ((m1 (gnus-find-method-for-group group))
633                 (m2 (or gnus-command-method
634                         (gnus-find-method-for-group gnus-newsgroup-name)))
635                 (short-name (gnus-group-short-name group)))
636             (if (gnus-methods-equal-p m1 m2)
637                 (progn
638                   ;; this is REALLY just for debugging
639                   (gnus-message
640                    10
641                    "%s stripped group %s to %s"
642                    log-agent group short-name)
643                   (unless (member short-name out)
644                     (push short-name out)))
645               ;; else...
646               (gnus-message
647                7
648                "%s ignored foreign group %s"
649                log-agent group))))
650       (setq out groups))
651     (when (cdr-safe out)
652         (gnus-message
653          5
654          "%s: too many extra matches (%s) for %s %s.  Returning none."
655          log-agent out mode key)
656         (setq out nil))
657     out))
658
659 (defun gnus-registry-follow-group-p (group)
660   "Determines if a group name should be followed.
661 Consults `gnus-registry-unfollowed-groups' and
662 `nnmail-split-fancy-with-parent-ignore-groups'."
663   (not (or (gnus-grep-in-list
664             group
665             gnus-registry-unfollowed-groups)
666            (gnus-grep-in-list
667             group
668             nnmail-split-fancy-with-parent-ignore-groups))))
669
670 (defun gnus-registry-wash-for-keywords (&optional force)
671   (interactive)
672   (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article))
673         word words)
674     (if (or (not (gnus-registry-fetch-extra id 'keywords))
675             force)
676         (with-current-buffer gnus-article-buffer
677           (article-goto-body)
678           (save-window-excursion
679             (save-restriction
680               (narrow-to-region (point) (point-max))
681               (with-syntax-table gnus-adaptive-word-syntax-table
682                 (while (re-search-forward "\\b\\w+\\b" nil t)
683                   (setq word (gnus-registry-remove-alist-text-properties
684                               (downcase (buffer-substring
685                                          (match-beginning 0) (match-end 0)))))
686                   (if (> (length word) 3)
687                       (push word words))))))
688           (gnus-registry-store-extra-entry id 'keywords words)))))
689
690 (defun gnus-registry-find-keywords (keyword)
691   (interactive "skeyword: ")
692   (let (articles)
693     (maphash
694      (lambda (key value)
695        (when (member keyword
696                    (cdr-safe (gnus-registry-fetch-extra key 'keywords)))
697          (push key articles)))
698      gnus-registry-hashtb)
699     articles))
700
701 (defun gnus-registry-register-message-ids ()
702   "Register the Message-ID of every article in the group"
703   (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
704     (dolist (article gnus-newsgroup-articles)
705       (let ((id (gnus-registry-fetch-message-id-fast article)))
706         (unless (member gnus-newsgroup-name (gnus-registry-fetch-groups id))
707           (gnus-message 9 "Registry: Registering article %d with group %s"
708                         article gnus-newsgroup-name)
709           (gnus-registry-add-group
710            id
711            gnus-newsgroup-name
712            (gnus-registry-fetch-simplified-message-subject-fast article)
713            (gnus-registry-fetch-sender-fast article)))))))
714
715 (defun gnus-registry-fetch-message-id-fast (article)
716   "Fetch the Message-ID quickly, using the internal gnus-data-list function"
717   (if (and (numberp article)
718            (assoc article (gnus-data-list nil)))
719       (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
720     nil))
721
722 (defun gnus-registry-simplify-subject (subject)
723   (if (stringp subject)
724       (gnus-simplify-subject subject)
725     nil))
726
727 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
728   "Fetch the Subject quickly, using the internal gnus-data-list function"
729   (if (and (numberp article)
730            (assoc article (gnus-data-list nil)))
731       (gnus-string-remove-all-properties
732        (gnus-registry-simplify-subject
733         (mail-header-subject (gnus-data-header
734                               (assoc article (gnus-data-list nil))))))
735     nil))
736
737 (defun gnus-registry-fetch-sender-fast (article)
738   "Fetch the Sender quickly, using the internal gnus-data-list function"
739   (if (and (numberp article)
740            (assoc article (gnus-data-list nil)))
741       (gnus-string-remove-all-properties
742        (mail-header-from (gnus-data-header
743                           (assoc article (gnus-data-list nil)))))
744     nil))
745
746 (defun gnus-registry-do-marks (type function)
747   "For each known mark, call FUNCTION for each cell of type TYPE.
748
749 FUNCTION should take two parameters, a mark symbol and the cell value."
750   (dolist (mark-info gnus-registry-marks)
751     (let* ((mark (car-safe mark-info))
752            (data (cdr-safe mark-info))
753            (cell-data (plist-get data type)))
754       (when cell-data
755         (funcall function mark cell-data)))))
756
757 ;;; this is ugly code, but I don't know how to do it better
758 (defun gnus-registry-install-shortcuts ()
759   "Install the keyboard shortcuts and menus for the registry.
760 Uses `gnus-registry-marks' to find what shortcuts to install."
761   (let (keys-plist)
762     (setq gnus-registry-misc-menus nil)
763     (gnus-registry-do-marks
764      :char
765      (lambda (mark data)
766        (let ((function-format
767               (format "gnus-registry-%%s-article-%s-mark" mark)))
768
769 ;;; The following generates these functions:
770 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
771 ;;;   "Apply the Important mark to process-marked ARTICLES."
772 ;;;   (interactive (gnus-summary-work-articles current-prefix-arg))
773 ;;;   (gnus-registry-set-article-mark-internal 'Important articles nil t))
774 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
775 ;;;   "Apply the Important mark to process-marked ARTICLES."
776 ;;;   (interactive (gnus-summary-work-articles current-prefix-arg))
777 ;;;   (gnus-registry-set-article-mark-internal 'Important articles t t))
778
779          (dolist (remove '(t nil))
780            (let* ((variant-name (if remove "remove" "set"))
781                   (function-name (format function-format variant-name))
782                   (shortcut (format "%c" data))
783                   (shortcut (if remove (upcase shortcut) shortcut)))
784              (unintern function-name obarray)
785              (eval
786               `(defun
787                  ;; function name
788                  ,(intern function-name)
789                  ;; parameter definition
790                  (&rest articles)
791                  ;; documentation
792                  ,(format
793                    "%s the %s mark over process-marked ARTICLES."
794                    (upcase-initials variant-name)
795                    mark)
796                  ;; interactive definition
797                  (interactive
798                   (gnus-summary-work-articles current-prefix-arg))
799                  ;; actual code
800
801                  ;; if this is called and the user doesn't want the
802                  ;; registry enabled, we'll ask anyhow
803                  (when (eq gnus-registry-install nil)
804                    (setq gnus-registry-install 'ask))
805
806                  ;; now the user is asked if gnus-registry-install is 'ask
807                  (when (gnus-registry-install-p)
808                    (gnus-registry-set-article-mark-internal
809                     ;; all this just to get the mark, I must be doing it wrong
810                     (intern ,(symbol-name mark))
811                     articles ,remove t)
812                    (gnus-message
813                     9
814                     "Applying mark %s to %d articles"
815                     ,(symbol-name mark) (length articles))
816                    (dolist (article articles)
817                      (gnus-summary-update-article
818                       article
819                       (assoc article (gnus-data-list nil)))))))
820              (push (intern function-name) keys-plist)
821              (push shortcut keys-plist)
822              (push (vector (format "%s %s"
823                                    (upcase-initials variant-name)
824                                    (symbol-name mark))
825                            (intern function-name) t)
826                    gnus-registry-misc-menus)
827              (gnus-message
828               9
829               "Defined mark handling function %s"
830               function-name))))))
831     (gnus-define-keys-1
832      '(gnus-registry-mark-map "M" gnus-summary-mark-map)
833      keys-plist)
834     (add-hook 'gnus-summary-menu-hook
835               (lambda ()
836                 (easy-menu-add-item
837                  gnus-summary-misc-menu
838                  nil
839                  (cons "Registry Marks" gnus-registry-misc-menus))))))
840
841 ;;; use like this:
842 ;;; (defalias 'gnus-user-format-function-M
843 ;;;           'gnus-registry-user-format-function-M)
844 (defun gnus-registry-user-format-function-M (headers)
845   (let* ((id (mail-header-message-id headers))
846          (marks (when id (gnus-registry-fetch-extra-marks id))))
847     (apply 'concat (mapcar (lambda(mark)
848                              (let ((c
849                                     (plist-get
850                                      (cdr-safe
851                                       (assoc mark gnus-registry-marks))
852                                      :char)))
853                                (if c
854                                    (list c)
855                                  nil)))
856                            marks))))
857
858 (defun gnus-registry-read-mark ()
859   "Read a mark name from the user with completion."
860   (let ((mark (gnus-completing-read-with-default
861                (symbol-name gnus-registry-default-mark)
862                "Label"
863                (mapcar (lambda (x)      ; completion list
864                          (cons (symbol-name (car-safe x)) (car-safe x)))
865                        gnus-registry-marks))))
866     (when (stringp mark)
867       (intern mark))))
868
869 (defun gnus-registry-set-article-mark (&rest articles)
870   "Apply a mark to process-marked ARTICLES."
871   (interactive (gnus-summary-work-articles current-prefix-arg))
872   (gnus-registry-set-article-mark-internal (gnus-registry-read-mark) articles nil t))
873
874 (defun gnus-registry-remove-article-mark (&rest articles)
875   "Remove a mark from process-marked ARTICLES."
876   (interactive (gnus-summary-work-articles current-prefix-arg))
877   (gnus-registry-set-article-mark-internal (gnus-registry-read-mark) articles t t))
878
879 (defun gnus-registry-set-article-mark-internal (mark articles &optional remove show-message)
880   "Apply a mark to a list of ARTICLES."
881   (let ((article-id-list
882          (mapcar 'gnus-registry-fetch-message-id-fast articles)))
883     (dolist (id article-id-list)
884       (let* (
885              ;; all the marks for this article without the mark of
886              ;; interest
887              (marks
888               (delq mark (gnus-registry-fetch-extra-marks id)))
889              ;; the new marks we want to use
890              (new-marks (if remove
891                             marks
892                           (cons mark marks))))
893         (when show-message
894           (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
895                         (if remove "Removing" "Adding")
896                         mark id new-marks))
897
898         (apply 'gnus-registry-store-extra-marks ; set the extra marks
899                id                               ; for the message ID
900                new-marks)))))
901
902 (defun gnus-registry-get-article-marks (&rest articles)
903   "Get the Gnus registry marks for ARTICLES and show them if interactive.
904 Uses process/prefix conventions.  For multiple articles,
905 only the last one's marks are returned."
906   (interactive (gnus-summary-work-articles 1))
907   (let (marks)
908     (dolist (article articles)
909       (let ((article-id
910              (gnus-registry-fetch-message-id-fast article)))
911         (setq marks (gnus-registry-fetch-extra-marks article-id))))
912     (when (interactive-p)
913         (gnus-message 1 "Marks are %S" marks))
914     marks))
915
916 ;;; if this extends to more than 'marks, it should be improved to be more generic.
917 (defun gnus-registry-fetch-extra-marks (id)
918   "Get the marks of a message, based on the message ID.
919 Returns a list of symbol marks or nil."
920   (car-safe (cdr (gnus-registry-fetch-extra id 'marks))))
921
922 (defun gnus-registry-has-extra-mark (id mark)
923   "Checks if a message has `mark', based on the message ID `id'."
924   (memq mark (gnus-registry-fetch-extra-marks id)))
925
926 (defun gnus-registry-store-extra-marks (id &rest mark-list)
927   "Set the marks of a message, based on the message ID.
928 The `mark-list' can be nil, in which case no marks are left."
929   (gnus-registry-store-extra-entry id 'marks (list mark-list)))
930
931 (defun gnus-registry-delete-extra-marks (id &rest mark-delete-list)
932   "Delete the message marks in `mark-delete-list', based on the message ID."
933   (let ((marks (gnus-registry-fetch-extra-marks id)))
934     (when marks
935       (dolist (mark mark-delete-list)
936         (setq marks (delq mark marks))))
937     (gnus-registry-store-extra-marks id (car marks))))
938
939 (defun gnus-registry-delete-all-extra-marks (id)
940   "Delete all the marks for a message ID."
941   (gnus-registry-store-extra-marks id nil))
942
943 (defun gnus-registry-fetch-extra (id &optional entry)
944   "Get the extra data of a message, based on the message ID.
945 Returns the first place where the trail finds a nonstring."
946   (let ((entry-cache (gethash entry gnus-registry-hashtb)))
947     (if (and entry
948              (hash-table-p entry-cache)
949              (gethash id entry-cache))
950         (gethash id entry-cache)
951       ;; else, if there is no caching possible...
952       (let ((trail (gethash id gnus-registry-hashtb)))
953         (when (listp trail)
954           (dolist (crumb trail)
955             (unless (stringp crumb)
956               (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
957
958 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
959   "Get the extra data of a message, or a specific entry in it.
960 Update the entry cache if needed."
961   (if (and entry id)
962       (let ((entry-cache (gethash entry gnus-registry-hashtb))
963             entree)
964         (when gnus-registry-entry-caching
965           ;; create the hash table
966           (unless (hash-table-p entry-cache)
967             (setq entry-cache (make-hash-table
968                                :size 4096
969                                :test 'equal))
970             (puthash entry entry-cache gnus-registry-hashtb))
971
972           ;; get the entree from the hash table or from the alist
973           (setq entree (gethash id entry-cache)))
974
975         (unless entree
976           (setq entree (assq entry alist))
977           (when gnus-registry-entry-caching
978             (puthash id entree entry-cache)))
979         entree)
980     alist))
981
982 (defun gnus-registry-store-extra (id extra)
983   "Store the extra data of a message, based on the message ID.
984 The message must have at least one group name."
985   (when (gnus-registry-group-count id)
986     ;; we now know the trail has at least 1 group name, so it's not empty
987     (let ((trail (gethash id gnus-registry-hashtb))
988           (old-extra (gnus-registry-fetch-extra id))
989           entry-cache)
990       (dolist (crumb trail)
991         (unless (stringp crumb)
992           (dolist (entry crumb)
993             (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
994           (when entry-cache
995             (remhash id entry-cache))))
996       (puthash id (cons extra (delete old-extra trail))
997                gnus-registry-hashtb)
998       (setq gnus-registry-dirty t)))))
999
1000 (defun gnus-registry-delete-extra-entry (id key)
1001   "Delete a specific entry in the extras field of the registry entry for id."
1002   (gnus-registry-store-extra-entry id key nil))
1003
1004 (defun gnus-registry-store-extra-entry (id key value)
1005   "Put a specific entry in the extras field of the registry entry for id."
1006   (let* ((extra (gnus-registry-fetch-extra id))
1007          ;; all the entries except the one for `key'
1008          (the-rest (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))
1009          (alist (if value
1010                     (gnus-registry-remove-alist-text-properties
1011                      (cons (cons key value)
1012                            the-rest))
1013                   the-rest)))
1014     (gnus-registry-store-extra id alist)))
1015
1016 (defun gnus-registry-fetch-group (id)
1017   "Get the group of a message, based on the message ID.
1018 Returns the first place where the trail finds a group name."
1019   (when (gnus-registry-group-count id)
1020     ;; we now know the trail has at least 1 group name
1021     (let ((trail (gethash id gnus-registry-hashtb)))
1022       (dolist (crumb trail)
1023         (when (stringp crumb)
1024           (return (if gnus-registry-use-long-group-names
1025                        crumb
1026                      (gnus-group-short-name crumb))))))))
1027
1028 (defun gnus-registry-fetch-groups (id &optional max)
1029   "Get the groups (up to MAX, if given) of a message, based on the message ID."
1030   (let ((trail (gethash id gnus-registry-hashtb))
1031         groups)
1032     (dolist (crumb trail)
1033       (when (stringp crumb)
1034         ;; push the group name into the list
1035         (setq
1036          groups
1037          (cons
1038           (if (or (not (stringp crumb)) gnus-registry-use-long-group-names)
1039               crumb
1040             (gnus-group-short-name crumb))
1041          groups))
1042         (when (and max (> (length groups) max))
1043           (return))))
1044     ;; return the list of groups
1045     groups))
1046
1047 (defun gnus-registry-group-count (id)
1048   "Get the number of groups of a message, based on the message ID."
1049   (let ((trail (gethash id gnus-registry-hashtb)))
1050     (if (and trail (listp trail))
1051         (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
1052       0)))
1053
1054 (defun gnus-registry-delete-group (id group)
1055   "Delete a group for a message, based on the message ID."
1056   (when (and group id)
1057       (let ((trail (gethash id gnus-registry-hashtb))
1058             (short-group (gnus-group-short-name group)))
1059         (puthash id (if trail
1060                         (delete short-group (delete group trail))
1061                       nil)
1062                  gnus-registry-hashtb))
1063       ;; now, clear the entry if there are no more groups
1064       (when gnus-registry-trim-articles-without-groups
1065         (unless (gnus-registry-group-count id)
1066           (gnus-registry-delete-id id)))
1067       ;; is this ID still in the registry?
1068       (when (gethash id gnus-registry-hashtb)
1069         (gnus-registry-store-extra-entry id 'mtime (current-time)))))
1070
1071 (defun gnus-registry-delete-id (id)
1072   "Delete a message ID from the registry."
1073   (when (stringp id)
1074     (remhash id gnus-registry-hashtb)
1075     (maphash
1076      (lambda (key value)
1077        (when (hash-table-p value)
1078          (remhash id value)))
1079      gnus-registry-hashtb)))
1080
1081 (defun gnus-registry-add-group (id group &optional subject sender)
1082   "Add a group for a message, based on the message ID."
1083   (when group
1084     (when (and id
1085                (not (string-match "totally-fudged-out-message-id" id)))
1086       (let ((full-group group)
1087             (group (if gnus-registry-use-long-group-names
1088                        group
1089                      (gnus-group-short-name group))))
1090         (gnus-registry-delete-group id group)
1091
1092         (unless gnus-registry-use-long-group-names ;; unnecessary in this case
1093           (gnus-registry-delete-group id full-group))
1094
1095         (let ((trail (gethash id gnus-registry-hashtb)))
1096           (puthash id (if trail
1097                           (cons group trail)
1098                         (list group))
1099                    gnus-registry-hashtb)
1100
1101           (when (and (gnus-registry-track-subject-p)
1102                      subject)
1103             (gnus-registry-store-extra-entry
1104              id
1105              'subject
1106              (gnus-registry-simplify-subject subject)))
1107           (when (and (gnus-registry-track-sender-p)
1108                      sender)
1109             (gnus-registry-store-extra-entry
1110              id
1111              'sender
1112              sender))
1113
1114           (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
1115
1116 (defun gnus-registry-clear ()
1117   "Clear the Gnus registry."
1118   (interactive)
1119   (setq gnus-registry-alist nil)
1120   (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
1121   (setq gnus-registry-dirty t))
1122
1123 ;;;###autoload
1124 (defun gnus-registry-initialize ()
1125 "Initialize the Gnus registry."
1126   (interactive)
1127   (gnus-message 5 "Initializing the registry")
1128   (setq gnus-registry-install t)        ; in case it was 'ask or nil
1129   (gnus-registry-install-hooks)
1130   (gnus-registry-install-shortcuts)
1131   (gnus-registry-read))
1132
1133 ;;;###autoload
1134 (defun gnus-registry-install-hooks ()
1135   "Install the registry hooks."
1136   (interactive)
1137   (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
1138   (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
1139   (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
1140   (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
1141
1142   (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
1143   (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
1144
1145   (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
1146
1147 (defun gnus-registry-unload-hook ()
1148   "Uninstall the registry hooks."
1149   (interactive)
1150   (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
1151   (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
1152   (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
1153   (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
1154
1155   (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
1156   (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
1157
1158   (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
1159
1160 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
1161
1162 (defun gnus-registry-install-p ()
1163   (interactive)
1164   (when (eq gnus-registry-install 'ask)
1165     (setq gnus-registry-install
1166           (gnus-y-or-n-p
1167            (concat "Enable the Gnus registry?  "
1168                    "See the variable `gnus-registry-install' "
1169                    "to get rid of this query permanently. ")))
1170     (when gnus-registry-install
1171       ;; we just set gnus-registry-install to t, so initialize the registry!
1172       (gnus-registry-initialize)))
1173 ;;; we could call it here: (customize-variable 'gnus-registry-install)
1174   gnus-registry-install)
1175
1176 (when (or (eq gnus-registry-install t)
1177           (gnus-registry-install-p))
1178   (gnus-registry-initialize))
1179
1180 ;; TODO: a few things
1181
1182 (provide 'gnus-registry)
1183
1184 ;;; gnus-registry.el ends here