No one asked for this, but it's something I've wanted for a while, so I
did it, and am attaching a patch if it's of general interest.

I often add multiple records for different people at the same
organization, and have wanted tab completion when creating/editing the
organization field. This patch creates and populates a
bbdb-organization-list variable on startup, and offers completion from
that list when editing or creating organization fields.

Two things it doesn't do:

1. Offer completion at the '/ o' search prompt -- this might be a bad
idea anyway.

2. Update bbdb-organization-list when the database is saved. I could add
this functionality, and might do so on my own regardless.

If the general idea here is acceptable but there's something wrong with
the implementation, let me know and I'll be happy to edit. But it seemed
pretty straightforward.

Just out of curiosity, has there ever been a plan to make organizations
first-class citizens in the database?

Eric

>From d886366b44d453454a06493c3ffa80009493972b Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <e...@ericabrahamsen.net>
Date: Fri, 28 Dec 2012 11:37:03 +0800
Subject: [PATCH] TAB completion for editing/creating organization fields

---
 lisp/bbdb-com.el | 13 ++++++++-----
 lisp/bbdb.el     |  9 +++++++++
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/lisp/bbdb-com.el b/lisp/bbdb-com.el
index 79e2e7e..8623507 100644
--- a/lisp/bbdb-com.el
+++ b/lisp/bbdb-com.el
@@ -618,7 +618,7 @@ but does ensure that there will not be name collisions."
      (setq name (bbdb-read-name first-and-last))
      (bbdb-check-name (car name) (cdr name)))
     (let ((organizations (bbdb-split 'organization
-                                     (bbdb-read-string "Organizations: ")))
+                                     (bbdb-read-string "Organizations: " nil bbdb-organization-list)))
           ;; mail
           (mail (bbdb-split 'mail (bbdb-read-string "E-Mail Addresses: ")))
           ;; address
@@ -847,7 +847,7 @@ value of \"\", the default) means do not alter the address."
   (cond (;; affix
          (eq field 'affix) (bbdb-read-string "Affix: " init))
         ;; organization
-        ((eq field 'organization) (bbdb-read-string "Organization: " init))
+        ((eq field 'organization) (bbdb-read-string "Organization: " init bbdb-organization-list))
         ;; mail
         ((eq field 'mail)
          (let ((mail (bbdb-read-string "Mail: " init)))
@@ -939,7 +939,9 @@ a phone number or address with VALUE being nil."
           ((eq field 'address)
            (unless value (error "No address specified"))
            (bbdb-record-edit-address value nil flag))
-          ((setq edit-str (assq field '((affix . "Affix")
+          ((setq collection (if (eq field 'organization)
+				bbdb-organization-list nil)
+		 edit-str (assq field '((affix . "Affix")
                                         (organization . "Organization")
                                         (mail . "Mail") (aka . "AKA"))))
            (bbdb-record-set-field
@@ -947,8 +949,9 @@ a phone number or address with VALUE being nil."
             (bbdb-split field (bbdb-read-string
                                (format "%s: " (cdr edit-str))
                                (bbdb-concat field (funcall (intern (format "bbdb-record-%s" field))
-                                                           record))))))
-          (t ; xfield
+                                                           record))
+			       collection))))
+          (t				; xfield
            (bbdb-record-set-xfield
             record field
             (bbdb-read-string (format "%s: " field)
diff --git a/lisp/bbdb.el b/lisp/bbdb.el
index fdb9883..72aa144 100644
--- a/lisp/bbdb.el
+++ b/lisp/bbdb.el
@@ -1533,6 +1533,11 @@ In the *BBDB* buffers it includes the records that are actually displayed
 and its elements are (RECORD DISPLAY-FORMAT MARKER-POS).")
 (make-variable-buffer-local 'bbdb-records)
 
+(defvar bbdb-organization-list nil
+  "All organizations currently using in the database, for use as
+  completion candidates when adding or editing records'
+  organization field.")
+
 (defvar bbdb-changed-records nil
   "List of records that has been changed since BBDB was last saved.")
 
@@ -2789,6 +2794,10 @@ If `bbdb-file' uses an outdated format, it is migrated to `bbdb-file-format'."
             (dolist (xfield (bbdb-record-xfields record))
               (unless (memq (setq label (car xfield)) bbdb-xfield-label-list)
                 (push label bbdb-xfield-label-list)))
+	    ;; populate bbdb-organization-list
+	    (dolist (organization (bbdb-record-organization record))
+	      (unless (member organization bbdb-organization-list)
+		(push organization bbdb-organization-list)))
 
             (let ((name (bbdb-concat 'name-first-last
                                      (bbdb-record-firstname record)
-- 
1.8.0.2

------------------------------------------------------------------------------
Master HTML5, CSS3, ASP.NET, MVC, AJAX, Knockout.js, Web API and
much more. Get web development skills now with LearnDevNow -
350+ hours of step-by-step video tutorials by Microsoft MVPs and experts.
SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122812
_______________________________________________
bbdb-info@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bbdb-info
BBDB Home Page: http://bbdb.sourceforge.net/

Reply via email to