Re: Add integration to gnus-search library, if present
Eric Abrahamsen <[email protected]> Tue, 10 Nov 2020 21:12:47 -0800
| Newsgroups | gmane.emacs.bbdb.user |
|---|---|
| Message-ID | <[email protected]> |
"Roland Winkler" <[email protected]> writes: > On Fri Nov 6 2020 Eric Abrahamsen wrote: >> A package like BBDB is one step removed from this logic. Ideally its >> only job should be to provide a table of contact-related strings that >> the user might want to complete on. It shouldn't be involved in the >> "downstream" logic of examining user input, etc. > > If that's the case, it might make sense to have a new variable > bbdb-completion-at-point-hashtable the content of which bbdb users > can customize similar to bbdb-completion-list. Then this customized > hashtable can be made available to the "outside world" such as the > gnus-search library. I've been thinking about this a bit: there's no need to create a new hashtable, since all that's needed is a list of strings to complete on. We also want this list to be dynamic so that it reflects updates to the BBDB as the user makes them. The completion code provides a `completion-table-dynamic' function that wraps a function and returns a completion table, and that could be the ticket. I've attached another patch to show how that could work. Possible issues: - This will rebuild the string table at every single use of `completion-at-point' (in other words, at every use of TAB in the minibuffer). I haven't tried it, maybe it would be slow. But maybe not! I don't think it's worth optimizing before it's tested. - It reuses the value of `bbdb-completion-list' as a predicate. Perhaps it would be better to have a separate and equivalent option to govern this completion, but it seemed to me that if the user went to the trouble of customizing `bbdb-completion-list', they'd probably want the same behavior here. But it would be easy to add a new option that governed this in particular. How does this look? It might be good to check with Stefan Monnier, as he wrote most of the completion stuff and usually seems happy to tell me where I've gone wrong. Eric
bbdb-capf.diff
(text/x-patch, 1.5 KB)
diff --git a/lisp/bbdb-com.el b/lisp/bbdb-com.el
index a261c85..b9b6efa 100644
--- a/lisp/bbdb-com.el
+++ b/lisp/bbdb-com.el
@@ -2142,6 +2142,20 @@ completion with."
nil t)))
(nth (1- (string-to-number result)) records))))))
+(defun bbdb-completion-at-point ()
+ "Return a list of strings for completion from the database.
+Meant to be used as a completion table in a \"completion at
+point\" context."
+ (let (strings)
+ (maphash (lambda (str record)
+ (when (bbdb-hash-p str record #'bbdb-completion-predicate)
+ (push str strings))))
+ strings))
+
+(defvar bbdb-completion-at-point-table
+ (completion-table-dynamic #'bbdb-completion-at-point)
+ "Completion table for use in completion-at-point functions.")
+
;;;###autoload
(defun bbdb-completing-read-mails (prompt &optional init)
"Like `read-string', but allows `bbdb-complete-mail' style completion."
diff --git a/lisp/bbdb-gnus.el b/lisp/bbdb-gnus.el
index c2ac987..b36b06f 100644
--- a/lisp/bbdb-gnus.el
+++ b/lisp/bbdb-gnus.el
@@ -48,6 +48,10 @@ Do not call this in your init file. Use `bbdb-initialize'."
;; (define-key gnus-summary-mode-map "'" 'bbdb-mua-display-recipients)
;; (define-key gnus-summary-mode-map ";" 'bbdb-mua-edit-field-recipients)
+ (when (boundp 'gnus-search-contact-tables)
+ (push bbdb-completion-at-point-table
+ gnus-search-contact-tables))
+
;; Set up user field for use in `gnus-summary-line-format'
;; (1) Big solution: use whole name
(if bbdb-mua-summary-unify-format-letter