gmm-utils.el (gmm-called-interactively-p): Restore as a macro.
[gnus] / lisp / gnus-registry.el
index f4337a5..5e20f5f 100644 (file)
@@ -1,7 +1,6 @@
 ;;; gnus-registry.el --- article registry for Gnus
 
-;;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 
-;;; Free Software Foundation, Inc.
+;; Copyright (C) 2002-2012  Free Software Foundation, Inc.
 
 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
 ;; Keywords: news registry
@@ -24,7 +23,7 @@
 ;;; Commentary:
 
 ;; This is the gnus-registry.el package, which works with all
-;; backends, not just nnmail (e.g. NNTP).  The major issue is that it
+;; Gnus backends, not just nnmail.  The major issue is that it
 ;; doesn't go across backends, so for instance if an article is in
 ;; nnml:sys and you see a reference to it in nnimap splitting, the
 ;; article will end up in nnimap:sys
 ;; gnus-registry.el intercepts article respooling, moving, deleting,
 ;; and copying for all backends.  If it doesn't work correctly for
 ;; you, submit a bug report and I'll be glad to fix it.  It needs
-;; documentation in the manual (also on my to-do list).
+;; better documentation in the manual (also on my to-do list).
 
-;; Put this in your startup file (~/.gnus.el for instance)
+;; If you want to track recipients (and you should to make the
+;; gnus-registry splitting work better), you need the To and Cc
+;; headers collected by Gnus.  Note that in more recent Gnus versions
+;; this is already the case: look at `gnus-extra-headers' to be sure.
+
+;; ;;; you may also want Gcc Newsgroups Keywords X-Face
+;; (add-to-list 'gnus-extra-headers 'To)
+;; (add-to-list 'gnus-extra-headers 'Cc)
+;; (setq nnmail-extra-headers gnus-extra-headers)
+
+;; Put this in your startup file (~/.gnus.el for instance) or use Customize:
 
 ;; (setq gnus-registry-max-entries 2500
-;;       gnus-registry-use-long-group-names t)
+;;       gnus-registry-track-extra '(sender subject recipient))
 
 ;; (gnus-registry-initialize)
 
 
 ;; (: gnus-registry-split-fancy-with-parent)
 
+;; You should also consider using the nnregistry backend to look up
+;; articles.  See the Gnus manual for more information.
+
+;; Finally, you can put %uM in your summary line format to show the
+;; registry marks if you do this:
+
+;; show the marks as single characters (see the :char property in
+;; `gnus-registry-marks'):
+;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
+
+;; show the marks by name (see `gnus-registry-marks'):
+;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
+
 ;; TODO:
 
 ;; - get the correct group on spool actions
 
-;; - articles that are spooled to a different backend should be handled
+;; - articles that are spooled to a different backend should be moved
+;;   after splitting
 
 ;;; Code:
 
 (require 'gnus)
 (require 'gnus-int)
 (require 'gnus-sum)
+(require 'gnus-art)
 (require 'gnus-util)
 (require 'nnmail)
+(require 'easymenu)
+(require 'registry)
 
 (defvar gnus-adaptive-word-syntax-table)
 
 (defvar gnus-registry-dirty t
- "Boolean set to t when the registry is modified")
+ "Boolean set to t when the registry is modified.")
 
 (defgroup gnus-registry nil
   "The Gnus registry."
   :version "22.1"
   :group 'gnus)
 
-(defvar gnus-registry-hashtb (make-hash-table                      
-                             :size 256
-                             :test 'equal)
-  "*The article registry by Message ID.")
-
-(defcustom gnus-registry-marks
+(defvar gnus-registry-marks
   '((Important
      :char ?i
      :image "summary_important")
   "List of registry marks and their options.
 
 `gnus-registry-mark-article' will offer symbols from this list
-for completion.  
+for completion.
 
 Each entry must have a character to be useful for summary mode
 line display and for keyboard shortcuts.
 
 Each entry must have an image string to be useful for visual
-display."
-  :group 'gnus-registry
-  :type '(repeat :tag "Registry Marks"
-                (cons :tag "Mark"
-                      (symbol :tag "Name")
-                      (checklist :tag "Options" :greedy t
-                                 (group :inline t
-                                        (const :format "" :value :char)
-                                        (character :tag "Character code"))
-                                 (group :inline t
-                                        (const :format "" :value :image)
-                                        (string :tag "Image"))))))
+display.")
 
 (defcustom gnus-registry-default-mark 'To-Do
   "The default mark.  Should be a valid key for `gnus-registry-marks'."
   :group 'gnus-registry
   :type 'symbol)
 
-(defcustom gnus-registry-unfollowed-groups 
-  '("delayed$" "drafts$" "queue$" "INBOX$")
+(defcustom gnus-registry-unfollowed-addresses
+  (list (regexp-quote user-mail-address))
+  "List of addresses that gnus-registry-split-fancy-with-parent won't trace.
+The addresses are matched, they don't have to be fully qualified.
+In the messages, these addresses can be the sender or the
+recipients."
+  :version "24.1"
+  :group 'gnus-registry
+  :type '(repeat regexp))
+
+(defcustom gnus-registry-unfollowed-groups
+  '("delayed$" "drafts$" "queue$" "INBOX$" "^nnmairix:" "archive")
   "List of groups that gnus-registry-split-fancy-with-parent won't return.
 The group names are matched, they don't have to be fully
-qualified.  This parameter tells the Registry 'never split a
+qualified.  This parameter tells the Gnus registry 'never split a
 message into a group that matches one of these, regardless of
-references.'"
+references.'
+
+nnmairix groups are specifically excluded because they are ephemeral."
   :group 'gnus-registry
   :type '(repeat regexp))
 
@@ -134,77 +156,76 @@ references.'"
   "Whether the registry should be installed."
   :group 'gnus-registry
   :type '(choice (const :tag "Never Install" nil)
-                (const :tag "Always Install" t)
-                (const :tag "Ask Me" ask)))
+                 (const :tag "Always Install" t)
+                 (const :tag "Ask Me" ask)))
 
-(defcustom gnus-registry-clean-empty t
-  "Whether the empty registry entries should be deleted.
-Registry entries are considered empty when they have no groups
-and no extra data."
-  :group 'gnus-registry
-  :type 'boolean)
+(defvar gnus-registry-enabled nil)
 
-(defcustom gnus-registry-use-long-group-names t
-  "Whether the registry should use long group names."
-  :group 'gnus-registry
-  :type 'boolean)
+(defvar gnus-summary-misc-menu) ;; Avoid byte compiler warning.
 
-(defcustom gnus-registry-max-track-groups 20
-  "The maximum number of non-unique group matches to check for a message ID."
-  :group 'gnus-registry
-  :type '(radio (const :format "Unlimited " nil)
-               (integer :format "Maximum non-unique matches: %v")))
+(defvar gnus-registry-misc-menus nil)   ; ugly way to keep the menus
 
-(defcustom gnus-registry-track-extra nil
+(make-obsolete-variable 'gnus-registry-clean-empty nil "23.4")
+(make-obsolete-variable 'gnus-registry-use-long-group-names nil "23.4")
+(make-obsolete-variable 'gnus-registry-max-track-groups nil "23.4")
+(make-obsolete-variable 'gnus-registry-entry-caching nil "23.4")
+(make-obsolete-variable 'gnus-registry-trim-articles-without-groups nil "23.4")
+
+(defcustom gnus-registry-track-extra '(subject sender recipient)
   "Whether the registry should track extra data about a message.
-The Subject and Sender (From:) headers are currently tracked this
-way."
+The subject, recipients (To: and Cc:), and Sender (From:) headers
+are tracked this way by default."
   :group 'gnus-registry
   :type
   '(set :tag "Tracking choices"
     (const :tag "Track by subject (Subject: header)" subject)
+    (const :tag "Track by recipient (To: and Cc: headers)" recipient)
     (const :tag "Track by sender (From: header)"  sender)))
 
 (defcustom gnus-registry-split-strategy nil
-  "Whether the registry should track extra data about a message.
-The Subject and Sender (From:) headers are currently tracked this
-way."
-  :group 'gnus-registry
-  :type
-  '(choice :tag "Tracking choices"
-          (const :tag "Only use single choices, discard multiple matches" nil)
-          (const :tag "Majority of matches wins" majority)
-          (const :tag "First found wins"  first)))
+  "The splitting strategy applied to the keys in `gnus-registry-track-extra'.
+
+Given a set of unique found groups G and counts for each element
+of G, and a key K (typically 'sender or 'subject):
 
-(defcustom gnus-registry-entry-caching t
-  "Whether the registry should cache extra information."
+When nil, if G has only one element, use it.  Otherwise give up.
+This is the fastest but also least useful strategy.
+
+When 'majority, use the majority by count.  So if there is a
+group with the most articles counted by K, use that.  Ties are
+resolved in no particular order, simply the first one found wins.
+This is the slowest strategy but also the most accurate one.
+
+When 'first, the first element of G wins.  This is fast and
+should be OK if your senders and subjects don't \"bleed\" across
+groups."
   :group 'gnus-registry
-  :type 'boolean)
+  :type
+  '(choice :tag "Splitting strategy"
+           (const :tag "Only use single choices, discard multiple matches" nil)
+           (const :tag "Majority of matches wins" majority)
+           (const :tag "First found wins"  first)))
 
 (defcustom gnus-registry-minimum-subject-length 5
   "The minimum length of a subject before it's considered trackable."
   :group 'gnus-registry
   :type 'integer)
 
-(defcustom gnus-registry-trim-articles-without-groups t
-  "Whether the registry should clean out message IDs without groups."
-  :group 'gnus-registry
-  :type 'boolean)
-
-(defcustom gnus-registry-extra-entries-precious '(marks)
-  "What extra entries are precious, meaning they won't get trimmed.
-When you save the Gnus registry, it's trimmed to be no longer
-than `gnus-registry-max-entries' (which is nil by default, so no
-trimming happens).  Any entries with extra data in this list (by
-default, marks are included, so articles with marks are
-considered precious) will not be trimmed."
+(defcustom gnus-registry-extra-entries-precious '(mark)
+  "What extra keys are precious, meaning entries with them won't get pruned.
+By default, 'mark is included, so articles with marks are
+considered precious.
+
+Before you save the Gnus registry, it's pruned.  Any entries with
+keys in this list will not be pruned.  All other entries go to
+the Bit Bucket."
   :group 'gnus-registry
   :type '(repeat symbol))
 
-(defcustom gnus-registry-cache-file 
-  (nnheader-concat 
-   (or gnus-dribble-directory gnus-home-directory "~/") 
-   ".gnus.registry.eld")
+(defcustom gnus-registry-cache-file
+  (nnheader-concat
+   (or gnus-dribble-directory gnus-home-directory "~/")
+   ".gnus.registry.eioio")
   "File where the Gnus registry will be stored."
   :group 'gnus-registry
   :type 'file)
@@ -213,261 +234,187 @@ considered precious) will not be trimmed."
   "Maximum number of entries in the registry, nil for unlimited."
   :group 'gnus-registry
   :type '(radio (const :format "Unlimited " nil)
-               (integer :format "Maximum number: %v")))
-
-(defun gnus-registry-track-subject-p ()
-  (memq 'subject gnus-registry-track-extra))
+                (integer :format "Maximum number: %v")))
 
-(defun gnus-registry-track-sender-p ()
-  (memq 'sender gnus-registry-track-extra))
+(defcustom gnus-registry-max-pruned-entries nil
+  "Maximum number of pruned entries in the registry, nil for unlimited."
+  :version "24.1"
+  :group 'gnus-registry
+  :type '(radio (const :format "Unlimited " nil)
+                (integer :format "Maximum number: %v")))
+
+(defun gnus-registry-fixup-registry (db)
+  (when db
+    (let ((old (oref db :tracked)))
+      (oset db :precious
+            (append gnus-registry-extra-entries-precious
+                    '()))
+      (oset db :max-hard
+            (or gnus-registry-max-entries
+                most-positive-fixnum))
+      (oset db :prune-factor
+            0.1)
+      (oset db :max-soft
+            (or gnus-registry-max-pruned-entries
+                most-positive-fixnum))
+      (oset db :tracked
+            (append gnus-registry-track-extra
+                    '(mark group keyword)))
+      (when (not (equal old (oref db :tracked)))
+        (gnus-message 9 "Reindexing the Gnus registry (tracked change)")
+        (registry-reindex db))))
+  db)
+
+(defun gnus-registry-make-db (&optional file)
+  (interactive "fGnus registry persistence file: \n")
+  (gnus-registry-fixup-registry
+   (registry-db
+    "Gnus Registry"
+    :file (or file gnus-registry-cache-file)
+    ;; these parameters are set in `gnus-registry-fixup-registry'
+    :max-hard most-positive-fixnum
+    :max-soft most-positive-fixnum
+    :precious nil
+    :tracked nil)))
+
+(defvar gnus-registry-db (gnus-registry-make-db)
+  "The article registry by Message ID.  See `registry-db'.")
+
+;; top-level registry data management
+(defun gnus-registry-remake-db (&optional forsure)
+  "Remake the registry database after customization.
+This is not required after changing `gnus-registry-cache-file'."
+  (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
+  (when forsure
+    (gnus-message 4 "Remaking the Gnus registry")
+    (setq gnus-registry-db (gnus-registry-make-db))))
 
-(defun gnus-registry-cache-read ()
+(defun gnus-registry-read ()
   "Read the registry cache file."
   (interactive)
   (let ((file gnus-registry-cache-file))
-    (when (file-exists-p file)
-      (gnus-message 5 "Reading %s..." file)
-      (gnus-load file)
-      (gnus-message 5 "Reading %s...done" file))))
-
-;; FIXME: Get rid of duplicated code, cf. `gnus-save-newsrc-file' in
-;; `gnus-start.el'.  --rsteib
-(defun gnus-registry-cache-save ()
+    (condition-case nil
+        (progn
+          (gnus-message 5 "Reading Gnus registry from %s..." file)
+          (setq gnus-registry-db (gnus-registry-fixup-registry
+                                  (eieio-persistent-read file)))
+          (gnus-message 5 "Reading Gnus registry from %s...done" file))
+      (error
+       (gnus-message
+        1
+        "The Gnus registry could not be loaded from %s, creating a new one"
+        file)
+       (gnus-registry-remake-db t)))))
+
+(defun gnus-registry-save (&optional file db)
   "Save the registry cache file."
   (interactive)
-  (let ((file gnus-registry-cache-file))
-    (save-excursion
-      (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
-      (make-local-variable 'version-control)
-    (setq version-control gnus-backup-startup-file)
-    (setq buffer-file-name file)
-    (setq default-directory (file-name-directory buffer-file-name))
-    (buffer-disable-undo)
-    (erase-buffer)
-    (gnus-message 5 "Saving %s..." file)
-    (if gnus-save-startup-file-via-temp-buffer
-       (let ((coding-system-for-write gnus-ding-file-coding-system)
-             (standard-output (current-buffer)))
-         (gnus-gnus-to-quick-newsrc-format 
-          t "gnus registry startup file" 'gnus-registry-alist)
-         (gnus-registry-cache-whitespace file)
-         (save-buffer))
-      (let ((coding-system-for-write gnus-ding-file-coding-system)
-           (version-control gnus-backup-startup-file)
-           (startup-file file)
-           (working-dir (file-name-directory file))
-           working-file
-           (i -1))
-       ;; Generate the name of a non-existent file.
-       (while (progn (setq working-file
-                           (format
-                            (if (and (eq system-type 'ms-dos)
-                                     (not (gnus-long-file-names)))
-                                "%s#%d.tm#" ; MSDOS limits files to 8+3
-                              "%s#tmp#%d")
-                            working-dir (setq i (1+ i))))
-                     (file-exists-p working-file)))
-
-       (unwind-protect
-           (progn
-             (gnus-with-output-to-file working-file
-               (gnus-gnus-to-quick-newsrc-format 
-                t "gnus registry startup file" 'gnus-registry-alist))
-
-             ;; These bindings will mislead the current buffer
-             ;; into thinking that it is visiting the startup
-             ;; file.
-             (let ((buffer-backed-up nil)
-                   (buffer-file-name startup-file)
-                   (file-precious-flag t)
-                   (setmodes (file-modes startup-file)))
-               ;; Backup the current version of the startup file.
-               (backup-buffer)
-
-               ;; Replace the existing startup file with the temp file.
-               (rename-file working-file startup-file t)
-               (gnus-set-file-modes startup-file setmodes)))
-         (condition-case nil
-             (delete-file working-file)
-           (file-error nil)))))
-
-    (gnus-kill-buffer (current-buffer))
-    (gnus-message 5 "Saving %s...done" file))))
-
-;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
-;; Save the gnus-registry file with extra line breaks.
-(defun gnus-registry-cache-whitespace (filename)
-  (gnus-message 7 "Adding whitespace to %s" filename)
-  (save-excursion
-    (goto-char (point-min))
-    (while (re-search-forward "^(\\|(\\\"" nil t)
-      (replace-match "\n\\&" t))
-    (goto-char (point-min))
-    (while (re-search-forward " $" nil t)
-      (replace-match "" t t))))
-
-(defun gnus-registry-save (&optional force)
-  (when (or gnus-registry-dirty force)
-    (let ((caching gnus-registry-entry-caching))
-      ;; turn off entry caching, so mtime doesn't get recorded
-      (setq gnus-registry-entry-caching nil)
-      ;; remove entry caches
-      (maphash
-       (lambda (key value)
-        (if (hash-table-p value)
-            (remhash key gnus-registry-hashtb)))
-       gnus-registry-hashtb)
-      ;; remove empty entries
-      (when gnus-registry-clean-empty
-       (gnus-registry-clean-empty-function))
-      ;; now trim and clean text properties from the registry appropriately
-      (setq gnus-registry-alist 
-           (gnus-registry-remove-alist-text-properties
-            (gnus-registry-trim
-             (gnus-hashtable-to-alist
-              gnus-registry-hashtb))))
-      ;; really save
-      (gnus-registry-cache-save)
-      (setq gnus-registry-entry-caching caching)
-      (setq gnus-registry-dirty nil))))
-
-(defun gnus-registry-clean-empty-function ()
-  "Remove all empty entries from the registry.  Returns count thereof."
-  (let ((count 0))
-
-    (maphash
-     (lambda (key value)
-       (when (stringp key)
-        (dolist (group (gnus-registry-fetch-groups key))
-          (when (gnus-parameter-registry-ignore group)
-            (gnus-message
-             10 
-             "gnus-registry: deleted ignored group %s from key %s"
-             group key)
-            (gnus-registry-delete-group key group)))
-
-        (unless (gnus-registry-group-count key)
-          (gnus-registry-delete-id key))
-
-        (unless (or
-                 (gnus-registry-fetch-group key)
-                 ;; TODO: look for specific extra data here!
-                 ;; in this example, we look for 'label
-                 (gnus-registry-fetch-extra key 'label))
-          (incf count)
-          (gnus-registry-delete-id key))
-        
-        (unless (stringp key)
-          (gnus-message 
-           10 
-           "gnus-registry key %s was not a string, removing" 
-           key)
-          (gnus-registry-delete-id key))))
-       
-     gnus-registry-hashtb)
-    count))
-
-(defun gnus-registry-read ()
-  (gnus-registry-cache-read)
-  (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
-  (setq gnus-registry-dirty nil))
-
-(defun gnus-registry-remove-alist-text-properties (v)
-  "Remove text properties from all strings in alist."
-  (if (stringp v)
-      (gnus-string-remove-all-properties v)
-    (if (and (listp v) (listp (cdr v)))
-       (mapcar 'gnus-registry-remove-alist-text-properties v)
-      (if (and (listp v) (stringp (cdr v)))
-         (cons (gnus-registry-remove-alist-text-properties (car v))
-               (gnus-registry-remove-alist-text-properties (cdr v)))
-      v))))
-
-(defun gnus-registry-trim (alist)
-  "Trim alist to size, using gnus-registry-max-entries.
-Any entries with extra data (marks, currently) are left alone."
-  (if (null gnus-registry-max-entries)      
-      alist                             ; just return the alist
-    ;; else, when given max-entries, trim the alist
-    (let* ((timehash (make-hash-table
-                     :size 20000
-                     :test 'equal))
-          (precious (make-hash-table
-                     :size 20000
-                     :test 'equal))
-          (trim-length (- (length alist) gnus-registry-max-entries))
-          (trim-length (if (natnump trim-length) trim-length 0))
-          precious-list junk-list)
-      (maphash
-       (lambda (key value)
-        (let ((extra (gnus-registry-fetch-extra key)))
-          (dolist (item gnus-registry-extra-entries-precious)
-            (dolist (e extra)
-              (when (equal (nth 0 e) item)
-                (puthash key t precious)
-                (return))))
-          (puthash key (gnus-registry-fetch-extra key 'mtime) timehash)))
-       gnus-registry-hashtb)
-
-      (dolist (item alist)
-       (let ((key (nth 0 item)))
-         (if (gethash key precious)
-             (push item precious-list)
-           (push item junk-list))))
-
-      (sort 
-       junk-list
-       (lambda (a b)
-        (let ((t1 (or (cdr (gethash (car a) timehash)) 
-                      '(0 0 0)))
-              (t2 (or (cdr (gethash (car b) timehash)) 
-                      '(0 0 0))))
-          (time-less-p t1 t2))))
-
-      ;; we use the return value of this setq, which is the trimmed alist
-      (setq alist (append precious-list
-                         (nthcdr trim-length junk-list))))))
-  
+  (let ((file (or file gnus-registry-cache-file))
+        (db (or db gnus-registry-db)))
+    (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
+                  (registry-size db) file)
+    (registry-prune db)
+    ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
+    (eieio-persistent-save db file)
+    (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
+                  (registry-size db) file)))
+
+(defun gnus-registry-remove-ignored ()
+  (interactive)
+  (let* ((db gnus-registry-db)
+         (grouphashtb (registry-lookup-secondary db 'group))
+         (old-size (registry-size db)))
+    (registry-reindex db)
+    (loop for k being the hash-keys of grouphashtb
+          using (hash-values v)
+          when (gnus-registry-ignore-group-p k)
+          do (registry-delete db v nil))
+    (registry-reindex db)
+    (gnus-message 4 "Removed %d ignored entries from the Gnus registry"
+                  (- old-size (registry-size db)))))
+
+;; article move/copy/spool/delete actions
 (defun gnus-registry-action (action data-header from &optional to method)
   (let* ((id (mail-header-id data-header))
-        (subject (gnus-string-remove-all-properties
-                  (gnus-registry-simplify-subject
-                   (mail-header-subject data-header))))
-        (sender (gnus-string-remove-all-properties 
-                 (mail-header-from data-header)))
-        (from (gnus-group-guess-full-name-from-command-method from))
-        (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
-        (to-name (if to to "the Bit Bucket"))
-        (old-entry (gethash id gnus-registry-hashtb)))
-    (gnus-message 7 "Registry: article %s %s from %s to %s"
-                 id
-                 (if method "respooling" "going")
-                 from
-                 to)
-
-    ;; All except copy will need a delete
-    (gnus-registry-delete-group id from)
-
-    (when (equal 'copy action)
-      (gnus-registry-add-group id from subject sender)) ; undo the delete
-
-    (gnus-registry-add-group id to subject sender)))
-
-(defun gnus-registry-spool-action (id group &optional subject sender)
-  (let ((group (gnus-group-guess-full-name-from-command-method group)))
+         (subject (mail-header-subject data-header))
+         (extra (mail-header-extra data-header))
+         (recipients (gnus-registry-sort-addresses
+                      (or (cdr-safe (assq 'Cc extra)) "")
+                      (or (cdr-safe (assq 'To extra)) "")))
+         (sender (nth 0 (gnus-registry-extract-addresses
+                         (mail-header-from data-header))))
+         (from (gnus-group-guess-full-name-from-command-method from))
+         (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
+         (to-name (if to to "the Bit Bucket")))
+    (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
+                  id (if method "respooling" "going") from to)
+
+    (gnus-registry-handle-action
+     id
+     ;; unless copying, remove the old "from" group
+     (if (not (equal 'copy action)) from nil)
+     to subject sender recipients)))
+
+(defun gnus-registry-spool-action (id group &optional subject sender recipients)
+  (let ((to (gnus-group-guess-full-name-from-command-method group))
+        (recipients (or recipients
+                        (gnus-registry-sort-addresses
+                         (or (message-fetch-field "cc") "")
+                         (or (message-fetch-field "to") ""))))
+        (subject (or subject (message-fetch-field "subject")))
+        (sender (or sender (message-fetch-field "from"))))
     (when (and (stringp id) (string-match "\r$" id))
       (setq id (substring id 0 -1)))
-    (gnus-message 7 "Registry: article %s spooled to %s"
-                 id
-                 group)
-    (gnus-registry-add-group id group subject sender)))
+    (gnus-message 7 "Gnus registry: article %s spooled to %s"
+                  id
+                  to)
+    (gnus-registry-handle-action id nil to subject sender recipients)))
+
+(defun gnus-registry-handle-action (id from to subject sender
+                                       &optional recipients)
+  (gnus-message
+   10
+   "gnus-registry-handle-action %S" (list id from to subject sender recipients))
+  (let ((db gnus-registry-db)
+        ;; if the group is ignored, set the destination to nil (same as delete)
+        (to (if (gnus-registry-ignore-group-p to) nil to))
+        ;; safe if not found
+        (entry (gnus-registry-get-or-make-entry id))
+        (subject (gnus-string-remove-all-properties
+                  (gnus-registry-simplify-subject subject)))
+        (sender (gnus-string-remove-all-properties sender)))
+
+    ;; this could be done by calling `gnus-registry-set-id-key'
+    ;; several times but it's better to bunch the transactions
+    ;; together
+
+    (registry-delete db (list id) nil)
+    (when from
+      (setq entry (cons (delete from (assoc 'group entry))
+                        (assq-delete-all 'group entry))))
+
+    (dolist (kv `((group ,to)
+                  (sender ,sender)
+                  (recipient ,@recipients)
+                  (subject ,subject)))
+      (when (second kv)
+        (let ((new (or (assq (first kv) entry)
+                       (list (first kv)))))
+          (dolist (toadd (cdr kv))
+            (add-to-list 'new toadd t))
+          (setq entry (cons new
+                            (assq-delete-all (first kv) entry))))))
+    (gnus-message 10 "Gnus registry: new entry for %s is %S"
+                  id
+                  entry)
+    (gnus-registry-insert db id entry)))
 
 ;; Function for nn{mail|imap}-split-fancy: look up all references in
 ;; the cache and if a match is found, return that group.
 (defun gnus-registry-split-fancy-with-parent ()
-  "Split this message into the same group as its parent.  The parent
-is obtained from the registry.  This function can be used as an entry
-in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
+  "Split this message into the same group as its parent.
+The parent is obtained from the registry.  This function can be used as an
+entry in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
 this: (: gnus-registry-split-fancy-with-parent)
 
 This function tracks ALL backends, unlike
@@ -482,113 +429,153 @@ that group.
 
 See the Info node `(gnus)Fancy Mail Splitting' for more details."
   (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
-        (reply-to (message-fetch-field "in-reply-to"))      ; may be nil
-        ;; now, if reply-to is valid, append it to the References
-        (refstr (if reply-to 
-                    (concat refstr " " reply-to)
-                  refstr))
-        ;; these may not be used, but the code is cleaner having them up here
-        (sender (gnus-string-remove-all-properties
-                 (message-fetch-field "from")))
-        (subject (gnus-string-remove-all-properties
-                  (gnus-registry-simplify-subject
-                   (message-fetch-field "subject"))))
-
-        (nnmail-split-fancy-with-parent-ignore-groups
-         (if (listp nnmail-split-fancy-with-parent-ignore-groups)
-             nnmail-split-fancy-with-parent-ignore-groups
-           (list nnmail-split-fancy-with-parent-ignore-groups)))
-        (log-agent "gnus-registry-split-fancy-with-parent")
-        found found-full)
-
-    ;; this is a big if-else statement.  it uses
+         (reply-to (message-fetch-field "in-reply-to"))      ; may be nil
+         ;; now, if reply-to is valid, append it to the References
+         (refstr (if reply-to
+                     (concat refstr " " reply-to)
+                   refstr))
+         (references (and refstr (gnus-extract-references refstr)))
+         ;; these may not be used, but the code is cleaner having them up here
+         (sender (gnus-string-remove-all-properties
+                  (message-fetch-field "from")))
+         (recipients (gnus-registry-sort-addresses
+                      (or (message-fetch-field "cc") "")
+                      (or (message-fetch-field "to") "")))
+         (subject (gnus-string-remove-all-properties
+                   (gnus-registry-simplify-subject
+                    (message-fetch-field "subject"))))
+
+         (nnmail-split-fancy-with-parent-ignore-groups
+          (if (listp nnmail-split-fancy-with-parent-ignore-groups)
+              nnmail-split-fancy-with-parent-ignore-groups
+            (list nnmail-split-fancy-with-parent-ignore-groups))))
+    (gnus-registry--split-fancy-with-parent-internal
+     :references references
+     :refstr refstr
+     :sender sender
+     :recipients recipients
+     :subject subject
+     :log-agent "Gnus registry fancy splitting with parent")))
+
+(defun* gnus-registry--split-fancy-with-parent-internal
+    (&rest spec
+           &key references refstr sender subject recipients log-agent
+           &allow-other-keys)
+  (gnus-message
+   10
+   "gnus-registry--split-fancy-with-parent-internal %S" spec)
+  (let ((db gnus-registry-db)
+        found)
+    ;; this is a big chain of statements.  it uses
     ;; gnus-registry-post-process-groups to filter the results after
     ;; every step.
-    (cond
-     ;; the references string must be valid and parse to valid references
-     ((and refstr (gnus-extract-references refstr))
-      (dolist (reference (nreverse (gnus-extract-references refstr)))
-       (gnus-message
-        9
-        "%s is looking for matches for reference %s from [%s]"
-        log-agent reference refstr)
-       (dolist (group (gnus-registry-fetch-groups 
-                       reference 
-                       gnus-registry-max-track-groups))
-         (when (and group (gnus-registry-follow-group-p group))
-           (gnus-message
-            7
-            "%s traced the reference %s from [%s] to group %s"
-            log-agent reference refstr group)
-           (push group found))))
+    ;; the references string must be valid and parse to valid references
+    (when references
+      (gnus-message
+       9
+       "%s is tracing references %s"
+       log-agent refstr)
+      (dolist (reference (nreverse references))
+        (gnus-message 9 "%s is looking up %s" log-agent reference)
+        (loop for group in (gnus-registry-get-id-key reference 'group)
+              when (gnus-registry-follow-group-p group)
+              do
+              (progn
+                (gnus-message 7 "%s traced %s to %s" log-agent reference group)
+                (push group found))))
       ;; filter the found groups and return them
       ;; the found groups are the full groups
-      (setq found (gnus-registry-post-process-groups 
-                  "references" refstr found found)))
-     
-     ;; else: there were no matches, now try the extra tracking by sender
-     ((and (gnus-registry-track-sender-p)
-          sender
-          (not (equal (gnus-extract-address-component-email sender)
-                      user-mail-address)))
-      (maphash
-       (lambda (key value)
-        (let ((this-sender (cdr
-                            (gnus-registry-fetch-extra key 'sender)))
-              matches)
-          (when (and this-sender
-                     (equal sender this-sender))
-            (let ((groups (gnus-registry-fetch-groups 
-                           key
-                           gnus-registry-max-track-groups)))
-              (dolist (group groups)
-                (push group found-full)
-                (setq found (append (list group) (delete group found)))))
-            (push key matches)
-            (gnus-message
-             ;; raise level of messaging if gnus-registry-track-extra
-             (if gnus-registry-track-extra 7 9)
-             "%s (extra tracking) traced sender %s to groups %s (keys %s)"
-             log-agent sender found matches))))
-       gnus-registry-hashtb)
-      ;; filter the found groups and return them
-      ;; the found groups are NOT the full groups
-      (setq found (gnus-registry-post-process-groups 
-                  "sender" sender found found-full)))
-      
-     ;; else: there were no matches, now try the extra tracking by subject
-     ((and (gnus-registry-track-subject-p)
-          subject
-          (< gnus-registry-minimum-subject-length (length subject)))
-      (maphash
-       (lambda (key value)
-        (let ((this-subject (cdr
-                             (gnus-registry-fetch-extra key 'subject)))
-              matches)
-          (when (and this-subject
-                     (equal subject this-subject))
-            (let ((groups (gnus-registry-fetch-groups 
-                           key
-                           gnus-registry-max-track-groups)))
-              (dolist (group groups)
-                (push group found-full)
-                (setq found (append (list group) (delete group found)))))
-            (push key matches)
-            (gnus-message
-             ;; raise level of messaging if gnus-registry-track-extra
-             (if gnus-registry-track-extra 7 9)
-             "%s (extra tracking) traced subject %s to groups %s (keys %s)"
-             log-agent subject found matches))))
-       gnus-registry-hashtb)
-      ;; filter the found groups and return them
-      ;; the found groups are NOT the full groups
-      (setq found (gnus-registry-post-process-groups 
-                  "subject" subject found found-full))))
-    ;; after the (cond) we extract the actual value safely
-    (car-safe found)))
+      (setq found (gnus-registry-post-process-groups
+                   "references" refstr found)))
 
-(defun gnus-registry-post-process-groups (mode key groups groups-full)
-  "Modifies GROUPS found by MODE for KEY to determine which ones to follow.
+     ;; else: there were no matches, now try the extra tracking by subject
+     (when (and (null found)
+                (memq 'subject gnus-registry-track-extra)
+                subject
+                (< gnus-registry-minimum-subject-length (length subject)))
+       (let ((groups (apply
+                      'append
+                      (mapcar
+                       (lambda (reference)
+                         (gnus-registry-get-id-key reference 'group))
+                       (registry-lookup-secondary-value db 'subject subject)))))
+         (setq found
+               (loop for group in groups
+                     when (gnus-registry-follow-group-p group)
+                     do (gnus-message
+                         ;; warn more if gnus-registry-track-extra
+                         (if gnus-registry-track-extra 7 9)
+                         "%s (extra tracking) traced subject '%s' to %s"
+                         log-agent subject group)
+                    and collect group))
+         ;; filter the found groups and return them
+         ;; the found groups are NOT the full groups
+         (setq found (gnus-registry-post-process-groups
+                      "subject" subject found))))
+
+     ;; else: there were no matches, try the extra tracking by sender
+     (when (and (null found)
+                (memq 'sender gnus-registry-track-extra)
+                sender
+                (not (gnus-grep-in-list
+                      sender
+                      gnus-registry-unfollowed-addresses)))
+       (let ((groups (apply
+                      'append
+                      (mapcar
+                       (lambda (reference)
+                         (gnus-registry-get-id-key reference 'group))
+                       (registry-lookup-secondary-value db 'sender sender)))))
+         (setq found
+               (loop for group in groups
+                     when (gnus-registry-follow-group-p group)
+                     do (gnus-message
+                         ;; warn more if gnus-registry-track-extra
+                         (if gnus-registry-track-extra 7 9)
+                         "%s (extra tracking) traced sender '%s' to %s"
+                         log-agent sender group)
+                     and collect group)))
+
+       ;; filter the found groups and return them
+       ;; the found groups are NOT the full groups
+       (setq found (gnus-registry-post-process-groups
+                    "sender" sender found)))
+
+     ;; else: there were no matches, try the extra tracking by recipient
+     (when (and (null found)
+                (memq 'recipient gnus-registry-track-extra)
+                recipients)
+       (dolist (recp recipients)
+         (when (and (null found)
+                    (not (gnus-grep-in-list
+                          recp
+                          gnus-registry-unfollowed-addresses)))
+