6085 comments
2357 subscribers
6222 on Twitter
Subscribe! Feed reader E-mail

Wicked Cool Emacs: BBDB: Use nicknames and custom salutations

I like starting my e-mail with a short salutation such as “Hello, Mike!”, “Hello, Michael”, or “Hello, Mikong!”, but it can be hard to remember which nicknames people prefer to use, and calling someone by the wrong name is a bit of a faux pas. Sometimes people sign e-mail with their preferred name, but what if you haven’t sent e-mail to or received e-mail from someone in a while? In this project, you’ll learn how to set up my BBDB to remember people’s nicknames for you using a custom “nick” field, and to use those nicknames when replying to messages in Gnus or composing messages from my BBDB.

The nickname code worked so well that I started thinking of what else I could customize. It was easy to go from nicknames to personalized salutations. This hack started because one of my friends is from Romania, so I thought I’d greet her in Romanian with “Salut, Letitia!” instead of just “Hello, Letitia!”. The code in this project uses a “hello” field to store these salutations in your BBDB.

To set up personalized nicknames and salutations, add the following code to your ~/.emacs:

(defvar wicked/gnus-nick-threshold 5 "*Number of people to stop greeting individually. Nil means always greet individually.")  ;; (1)
(defvar wicked/bbdb-hello-string "Hello, %s!" "Format string for hello. Example: \"Hello, %s!\"")
(defvar wicked/bbdb-hello-all-string "Hello, all!" "String for hello when there are many people. Example: \"Hello, all!\"")
(defvar wicked/bbdb-nick-field 'nick "Symbol name for nickname field in BBDB.")
(defvar wicked/bbdb-salutation-field 'hello "Symbol name for salutation field in BBDB.")

(defun wicked/gnus-add-nick-to-message ()
  "Inserts \"Hello, NICK!\" in messages based on the recipient's nick field."
  (interactive)
  (save-excursion
    (let* ((bbdb-get-addresses-headers ;; (2)
            (list (assoc 'recipients bbdb-get-addresses-headers)))
           (recipients (bbdb-get-addresses
                        nil
                        gnus-ignored-from-addresses
                        'gnus-fetch-field))
           recipient nicks rec net salutations)
      (goto-char (point-min))
      (when (re-search-forward "--text follows this line--" nil t)
        (forward-line 1)
        (if (and wicked/gnus-nick-threshold 
                 (>= (length recipients) wicked/gnus-nick-threshold))
            (insert wicked/bbdb-hello-all-string "\n\n") ;; (3)
          (while recipients
            (setq recipient (car (cddr (car recipients))))
            (setq net (nth 1 recipient))
            (setq rec (car (bbdb-search (bbdb-records) nil nil net)))
            (cond
             ((null rec) ;; (4)
              (add-to-list 'nicks (car recipient))) 
             ((bbdb-record-getprop rec wicked/bbdb-salutation-field) ;; (5)
              (add-to-list 'salutations 
                           (bbdb-record-getprop rec wicked/bbdb-salutation-field))) 
             ((bbdb-record-getprop rec wicked/bbdb-nick-field) ;; (6)
              (add-to-list 'nicks 
                           (bbdb-record-getprop rec wicked/bbdb-nick-field)))
             (t (bbdb-record-name rec))) ;; (7) 
            (setq recipients (cdr recipients))))
        (when nicks ;; (8)
          (insert (format wicked/bbdb-hello-string 
                          (mapconcat 'identity (nreverse nicks) ", "))
                  " "))
        (when salutations ;; (9)
          (insert (mapconcat 'identity salutations " ")))
        (when (or nicks salutations)
          (insert "\n\n")))))
  (goto-char (point-min)))

(defadvice gnus-post-news (after wicked/bbdb activate)
  "Insert nicknames or custom salutations."
  (wicked/gnus-add-nick-to-message))

(defadvice gnus-msg-mail (after wicked/bbdb activate)
  "Insert nicknames or custom salutations."
  (wicked/gnus-add-nick-to-message))

(defadvice gnus-summary-reply (after wicked/bbdb activate)
  "Insert nicknames or custom salutations."
  (wicked/gnus-add-nick-to-message))

After you add this code, you can store personalized nicknames and salutations in your BBDB. Nicknames and salutations will be looked up using people’s e-mail addresses. While in the *BBDB* buffer, you can type C-o (bbdb-insert-new-field) to add a field to the current record. Add a “nick” field with the person’s nickname, or a “hello” field with a custom salutation. When you compose a message to or reply to a message from that person, the salutation or nickname will be included. If no nickname can be found, the recipient’s name will be used instead.

A number of variables can be used to modify the behavior of this code(1). For example, you may or may not want to greet 20 people individually. The default value of wicked/gnus-nick-threshold is to greet up to four people individually, and greet more people collectively. If you always want to greet people individually, add (setq wicked/gnus-nick-threshold nil) to your ~/.emacs. If you want to change the strings used to greet people individually or collectively, change wicked/bbdb-hello-string and wicked/bbdb-hello-all-string. If you want to store the data into different fields, change wicked/bbdb-nick-field and wicked/bbdb-salutation-field, but note that old data will not be automatically copied to the new fields.

Here’s how the code works. First, it retrieves the list of addresses from the header(2). If there are more addresses than wicked/gnus-nick-threshold, then wicked/bbdb-hello-all-string is used to greet everyone. If not, each recipient address is looked up. If the recipient cannot be found in your BBDB, then the recipient’s name or e-mail address is used(4). If there is a personalized salutation, it is used(5). If there is a nickname, it is used(6). If the person has a record but neither salutation or nickname, then the name of the record is used(7). After all recipients have been processed, the names are added to the message(8), followed by the salutations(9). This function is added to the different Gnus message-posting functions, so it should be called whenever you compose or reply to messages.

You can use BBDB to personalize even more. Check out “Project XXX: Personalize signatures” for more ideas.

Short URL: http://sachachua.com/blog/p/4808
  • http://blog.printf.net/ Chris

    Hi Sacha!

    I tried this out, but the new advice never seems to be called. I haven’t used advice much before, but maybe it’s because I don’t have a “after wicked/bbdb activate” hook present?

    If it sounds like something’s goofy with my setup, just let me know and I’ll look around some more. :)

    Thanks!

    - Chris.

  • hmw

    Hi Sacha,

    I’ve thought about implementing such an automatism several moons ago,too. But I dropped the idea, because I find the result very impersonal. YMMV, but the generated salutations have the charme of the mass advertisings beginning with ‘Dear Ms. Chua’ you are getting every day with your email and snail mail. So I continue to start every email with a ‘handmade’ salutation, maybe I use the native tongue of the addressee (if I know it). Technically the result is the same as yours, but the feeling (for me and maybe the addressee) is better.

    Regards
    hmw

  • http://vyazovoi.info Vyazovoi Pavel

    Great solution, but AFAIK, field “AKA” reserved for nicknames.
    Not needed to define new field “nick”

    • http://sachachua.com Sacha Chua

      I also use AKA for maiden names, former names, and things like that, so I needed a short nick field as well. =) But yes, people can certainly use the aka field…

On This Day...

  • 2013: Debugging my brain: typos (write-os?) in my sketchnotes — Embarrassing mistakes are excellent ways to find and deal with bugs in your life. A couple of months ago, I [...]
  • 2012: Weekly review: Week ending March 23, 2012 — From last week’s plans Business [X] Earn: Project E1: More consulting Training Theming [X] Plan: Share my ideas behind visual [...]
  • 2010: How to get people to read your blog post — … is a useful question, but it’s the wrong one. Catchy titles and controversial topics are good at drawing eyes, [...]
  • 2009: Tips for managing virtual assistants — There are plenty of tips out there for becoming a virtual assistant, but not that many for managing virtual assistants. [...]
  • 2009: The Enchantress of Numbers; Happy Ada Lovelace Day! — Today is the first Ada Lovelace Day, dedicated to the celebration of women in technology. =) It’s interesting to think about [...]
  • 2009: Digraphs with Graphviz — And for the geeks, here’s the Graphviz dot file that created the graph in How to do a lot. Posting [...]
  • 2009: How to do a lot — People often ask me how I get so much done. It gets almost funny, even: some people seem to think [...]
  • 2008: Vision + Value + Voice = Connection — Gary Brown e-mailed me this insightful manifesto from Michael Lee Stallard, an expert on client and employee engagement. In it, [...]
  • 2008: Jetlag-assisted wakefulness — I was out like a light last night at maybe 8:00 or so. Jetlag. Today I woke up at 3:30. [...]
  • 2007: Eclairs — The house smells of eclairs, and stories are baked into those light-brown shells! Random Emacs symbol: ebnf-spool-directory – Command: Generate and spool [...]
  • 2006: Level up: Salmon! — I cooked salmon for the first time today. =D I followed the recipe for salmon with avocado with lots of [...]
  • 2006: Level up! Steak and potatoes — After giving that cook-or-die speech, I felt like treating myself to something special. So for today: pepper steak and mashed potatoes! This [...]
  • 2006: Toastmasters — If I give the universe a chance, it does its best to be absolutely wonderful. =) I had misgivings about the Toastmasters [...]
  • 2006: Back in the groove — I had a totally awesome day at IBM today, crunching some data and doing some nifty photocloud visualizations of all the [...]
  • 2005: Edwin’s comment on financial literacy — edwin of technobiography said: Hi Sacha, Many of us, like me, have a lot to learn about financial literacy. I’m reading up [...]
  • 2005: Difficult choices — UToronto is offering me admission. An interesting startup is recruiting me. Choices are always difficult. I would like to defer my U of [...]
  • 2005: MSU-IIT student wins IBM Linux Scholar Challenge — http://news.inq7.net/infotech/index.php?index=1&story_id=31491 Filipino software developer wins IBM Linux Scholar tilt Posted 00:33am (Mla time) Mar 24, 2005 By Erwin Lemuel Oliva INQ7.net FILIPINO software development talent [...]
  • 2004: CS planning meeting — Dr. Sarmenta: research. Thinking of research programs. On the positive side, CS197 this year went particularly well. Lot of good projects. One [...]
  • 2004: Free.net.ph scheduled downtime — sacha@free.net.ph will be unreachable from 2004.04.07 to 2004.04.11 as Jijo’ll be working on upgrades. I can be reached… hmmm… through erc://irc.freenode.net/#emacs and [...]
  • 2003: eshell extensions — emacs — In a March 6 post to the help-gnu-emacs@gnu.org mailing list, Romain Francoise mentions em-last.el, an eshell module that lets you cycle [...]
  • 2003: kana-kanji user dictionary for Japanese LEIM — emacs — Charles Muller figured out how to add to the kana-kanji conversion dictionary – modify ja-dic.el and recompile.
  • 2003: substring completion — emacs — Le Wang mentions icomplete and mcomplete in reference to substring completion in a March 4 post on the help-gnu-emacs@gnu.org mailing list. Hmmmm. [...]
  • 2003: Emacs menu accelerators — emacs — Jeffery B. Rancier’s March 12 post on help-gnu-emacs@gnu.org has this useful LISP snippet for Windows Emacs users: (defun jbr-w32-simulate-Alt-tap () (interactive) (w32-send-sys-command 61696)) (global-set-key [C-tab] [...]
  • 2003: Info-goto-emacs-command-mode — emacs — Useful Emacs documentation thing. See Info-goto-emacs-command-mode, normally bound to C-h F in CVS Emacs.
  • 2003: scroll / wheel mouse in Emacs — emacs — According to fsbot on freenode#emacs, , wheel is - http://koala.ilog.fr/colas/mouse-wheel-scroll/ - Put (mwheel-install) in .emacs
  • 2003: recentf.el and session.el — emacs — On the help-gnu-emacs@gnu.org mailing list, Hans Larsen says that session.el replaced recentf.el a long time ago. I used to use recentf.el. Maybe [...]
  • 2003: Remote X terminals — The link for remote X terminals is http://www.linuxgazette.com/issue27/kaszeta.html
  • 2003: Finite-state automata in LaTeX — In a really old post on debian-user@lists.debian.org“>debian-user@lists.debian.org (Jan 27 2003), Mark Zimmerman suggests the use of metapost to generate pretty finite state [...]
  • 2003: “Hello, tech designers? This stuff is too small” — ACM TechNews links to http://www.usatoday.com/usatonline/20030304/4914082s.htm, an article about how small things are getting. Americans seem to not like the idea of tiny devices, [...]
  • 2003: news.freshmeat.net — Google:gnus+freshmeat leads me to nntp://news.freshmeat.net, an NNTP server that provides Freshmeat news with the right subjects! Yay. I am definitely going to [...]
  • 2003: pointless presentation package — http://freshmeat.net/projects/pointless/ is a text-source presentation package that might be worth looking into, although it’s just on its first public release.
  • 2003: dasher and jogdial? — I wonder if there’s any way of getting dasher to work with the Sony jogdial. That might be a nice input [...]
  • 2003: dvorakng – a dvorak typing tutor — The 2003.03.18 edition of Freshmeat news lists dvorakng, a GPL Dvorak typing tutor based on dvorak7min but with extra features. URL: http://freshmeat.net/projects/dvorakng/
  • 2003: Essay about Linux in University CS curricula — On the open-source-now-list@redhat.com, Dan Kegel links to his essay on the industry’s need for graduates with Linux/OSS experience, and how universities can [...]
  • 2003: Gnus CVS for Debian — emacs — Add this to your /etc/apt/sources.list # For daily Gnus snapshots deb http://people.debian.org/~srivasta ./packages/ Source: EmacsWiki:GnusCvsForDebian
  • 2003: Linux accessibility — On the debian-user@lists.debian.org mailing list, Jerome Acks Jr. thinks that http://trace.wisc.edu/linux/index.htm is about the best Linux accessibility resource.
  • 2003: disabling plugin requests in mozilla — You can also delete libnullplugin.so in the mozilla plugins directory.
  • 2003: mail oops — Apparently, i-manila doesn’t take -k very well. fetchmail kept fetching old mail again and again and again… Ooops, my bad. [...]
  • 2003: killing flash plugin requests — In a thread on debian-user@lists.debian.org about disabling the annoying “Download Flash Plugin?” prompt, Karsten M. Self links to http://twiki.iwethey.org/twiki/bin/view/Main/UserContentCSS, which not only gets [...]
  • 2003: Elbert T. Or — Elbert T. Or writes in telling me that Marcelle had been “gushing about” OnLove, of all things. Elbert is into comics. [...]

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging