2

I use the function bbdb or bbdb-name to search for a particular record in my database using a regexp. Due to muscle-memory, I routinely hit the tab key inside the mini-buffer expecting to have auto-completion, but that feature appears to be lacking.

Q:  Is there an alternative function that would enable auto-completion to help locate records matching a particular regexp? If not, a suggested modification of the source code to achieve that goal would be greatly appreciated.

6
  • Are you using any particular completing-read function such as ivy or helm or ido?
    – Jules
    Jun 8, 2016 at 21:19
  • @JulesTamagnan: Other than the built-in pcomplete library, I'm not using anything special. I see that the Wanderlust library takes advantage of bbdb-complete-name to insert an email address, and I use that all the time. Perhaps combining the functionality of bbdb-complete-name with bbdb and/or bbdb-name would be the easiest way to handle this project. The difference between the former function and the latter two, is that the latter two generate a buffer containing the entire record. I generally use the entire record when I need to call someone or type an envelope.
    – lawlist
    Jun 8, 2016 at 21:31
  • @JulesTamagnan -- All of the following items that are built-in to the bbdb library look potentially very interesting, some of which might be able to be used to help with this particular project: bbdb-complete-clicked-name; bbdb-complete-name; bbdb-complete-name-cleanup; bbdb-completing-read-one-record; bbdb-completing-read-record; bbdb-completion-check-record; and, bbdb-completion-predicate
    – lawlist
    Jun 8, 2016 at 22:28
  • So is what you are looking for to enter a regexp, hit tab and then have a list of name completions? I don't think it would be possible for the regexp itself to be completed
    – Jules
    Jun 9, 2016 at 14:31
  • Yeah so I made a little function that asks for a regexp, which can match anything in the bbdb, like phone or email or name and then once you hit enter it lets you choose the name that you want from the list. Entering the regexp won't have any completion but choosing the name afterwards does
    – Jules
    Jun 9, 2016 at 15:34

2 Answers 2

2

The folowing solution was tested with bbdb version 2.35: http://bbdb.sourceforge.net/

I gave the bounty-bonus to @Jules Tamagnan for his interesting approach to displaying the entire record as a potential completion candidate.

The following answer provides for tab-completion using the name field of the database -- completing-read does not require a match because that optional argument has been omitted.

(defun bbdb-search-with-completion (elidep)
"Display all entries in the BBDB matching the regexp STRING
in either the name(s), company, network address, or notes.
Press the tab key for possible name completions without requiring a match."
(interactive "P")
  (let* (
      (bbdb-completion-type 'name)
      (ht (bbdb-hashtable))
      (completion-ignore-case 't)
      (string (completing-read "bbdb (regexp):  " ht 'bbdb-completion-predicate))
      (bbdb-display-layout (bbdb-grovel-elide-arg elidep))
      (notes (cons '* string))
      (records (bbdb-search (bbdb-records) string string string notes nil)))
    (if records
      (bbdb-display-records records)
      (message "No records matching '%s'" string))))
1
+50

Try this out if you want. It lets you enter a regexp and then shows the records when you hit tab. when you are satisfied with the chosen records hit enter. The main function is jat/bbdb-search.

(defun jat/minibuffer-text ()
  "only works on one line"
  (buffer-substring-no-properties
   (minibuffer-prompt-end)
   (line-end-position)))

(defun jat/display-records (records regexp &optional layout)
  (message "%s" regexp)
  (if records
      (bbdb-display-records records layout nil t)
    (message "No records matching '%s'" regexp)))

(defun jat/bbdb-search (&optional layout)
  (interactive)
  (let* ((regexp (read-from-minibuffer
              "Regexp: "
              nil
              (let ((map (make-sparse-keymap)))
            (set-keymap-parent map minibuffer-local-map)
            (define-key map (kbd "<tab>")
              (lambda ()
                (interactive)
                (save-selected-window
                  (let ((intext (jat/minibuffer-text)))
                (jat/display-records
                 (bbdb-search (bbdb-records) intext intext intext (cons '* intext) intext intext)
                 intext
                 layout)))))
            map)))
     (records (bbdb-search (bbdb-records) regexp regexp regexp (cons '* regexp) regexp regexp)))
    (jat/display-records records (jat/minibuffer-text) layout)))
6
  • I may need to try with a stock version of bbdb, because I get the following when I hit the tab key: Debugger entered--Lisp error: (wrong-number-of-arguments (lambda (records &optional name company net notes phone) "Search RECORDS for optional arguments NAME, COMPANY, NET, NOTES, PHONE. I have a slightly modified version of bbdb, and that may be the cause, but I'm not sure. I have some other projects to get out the door for work today, so I'll need to download a fresh version of bbdb later tonight and see if your code works out of the box.
    – lawlist
    Jun 9, 2016 at 18:03
  • Oh I only tried this on the stock version from melpa. The difference seems to be with the definition of bbdb-search
    – Jules
    Jun 9, 2016 at 18:06
  • I looked at bbdb version 2.35. I believe I found the issue -- your code uses seven (7) arguments in two places for bbdb-search; whereas, the macro at issue only contains six (6) arguments -- one is mandatory and the other five (5) are optional. So, I believe we need to remove one of the arguments from from both of your references to the macro bbdb-search. Since I'm not sure which one we should remove, I'll wait to hear back from you before I just take a wild guess on my own. The possible arguments are as follows: records &optional name company net notes phone.
    – lawlist
    Jun 10, 2016 at 5:38
  • I believe the issue is not so much that your version is not stock but more so that it is 9+ years old. You should be able to remove the last argument from bbdb-search in both instances to get the same result
    – Jules
    Jun 10, 2016 at 15:07
  • In my version of bbdb, I also had to remove an argument from bbdb-display-records -- i.e., bbdb-display-records records layout t. And, bbdb-search doesn't seem to accept (cons ...) as an argument, so I had to modify that to either intext or regexp. After that, completion of the whole record shows up. This is certainly enough for me to work with and adjust to personal liking, etc. Thank you very much. I'll leave the question open until tomorrow night to see if anyone else wants to take a stab at the answer . . . In the meantime, I gave you a thumbs-up . . .
    – lawlist
    Jun 10, 2016 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.