<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/atom.xsl" type="text/xsl"?><feed
	xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en-US"
	><title>Sacha Chua - tag - personalization</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/tag/personalization/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/tag/personalization" />
  <id>https://sachachua.com/blog/tag/personalization/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2008-03-24T08:26:48Z</updated>
<entry>
		<title type="html">Wicked Cool Emacs: BBDB: Use nicknames and custom salutations</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2008/03/wicked-cool-emacs-bbdb-use-nicknames-and-custom-salutations/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2014-05-14T03:06:41Z</updated>
    <published>2008-03-24T08:26:48Z</published>
    <category term="bbdb" />
<category term="emacs" />
<category term="wickedcoolemacs" />
		<id>https://sachachua.com/blog/?p=4808</id>
		<content type="html"><![CDATA[<div class="update">
<p>Update 2014-05-13: The original code is for BBDB version 2. Thomas Morgan sent this update which makes it work with BBDB version 3 &#8211; see below.</p>
</div>
<p>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&#8217;t sent e-mail to or received e-mail from someone in a while? In this project, you&#8217;ll learn how to set up my BBDB to remember people&#8217;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.</p>
<p>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&#8217;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.</p>
<p>To set up personalized nicknames and salutations, add the following code to your <code>~/.emacs</code>:</p>
<p>For BBDB v2</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/gnus-nick-threshold</span> 5 <span class="org-doc">"*Number of people to stop greeting individually. Nil means always greet individually."</span>)  <span class="org-comment-delimiter">;; </span><span class="org-comment">(1)</span>
(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/bbdb-hello-string</span> <span class="org-string">"Hello, %s!"</span> <span class="org-doc">"Format string for hello. Example: \"Hello, %s!\""</span>)
(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/bbdb-hello-all-string</span> <span class="org-string">"Hello, all!"</span> <span class="org-doc">"String for hello when there are many people. Example: \"Hello, all!\""</span>)
(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/bbdb-nick-field</span> 'nick <span class="org-doc">"Symbol name for nickname field in BBDB."</span>)
(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/bbdb-salutation-field</span> 'hello <span class="org-doc">"Symbol name for salutation field in BBDB."</span>)

(<span class="org-keyword">defun</span> <span class="org-function-name">wicked/gnus-add-nick-to-message</span> ()
  <span class="org-doc">"Inserts \"Hello, NICK!\" in messages based on the recipient's nick field."</span>
  (interactive)
  (<span class="org-keyword">save-excursion</span>
    (<span class="org-keyword">let*</span> ((bbdb-get-addresses-headers <span class="org-comment-delimiter">;; </span><span class="org-comment">(2)</span>
            (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))
      (<span class="org-keyword">when</span> (re-search-forward <span class="org-string">"&#45;&#45;text follows this line&#45;&#45;"</span> nil t)
        (forward-line 1)
        (<span class="org-keyword">if</span> (and wicked/gnus-nick-threshold 
                 (&gt;= (length recipients) wicked/gnus-nick-threshold))
            (insert wicked/bbdb-hello-all-string <span class="org-string">"\n\n"</span>) <span class="org-comment-delimiter">;; </span><span class="org-comment">(3)</span>
          (<span class="org-keyword">while</span> recipients
            (setq recipient (car (cddr (car recipients))))
            (setq net (nth 1 recipient))
            (setq rec (car (bbdb-search (bbdb-records) nil nil net)))
            (<span class="org-keyword">cond</span>
             ((null rec) <span class="org-comment-delimiter">;; </span><span class="org-comment">(4)</span>
              (add-to-list 'nicks (car recipient))) 
             ((bbdb-record-getprop rec wicked/bbdb-salutation-field) <span class="org-comment-delimiter">;; </span><span class="org-comment">(5)</span>
              (add-to-list 'salutations 
                           (bbdb-record-getprop rec wicked/bbdb-salutation-field))) 
             ((bbdb-record-getprop rec wicked/bbdb-nick-field) <span class="org-comment-delimiter">;; </span><span class="org-comment">(6)</span>
              (add-to-list 'nicks 
                           (bbdb-record-getprop rec wicked/bbdb-nick-field)))
             (t (bbdb-record-name rec))) <span class="org-comment-delimiter">;; </span><span class="org-comment">(7) </span>
            (setq recipients (cdr recipients))))
        (<span class="org-keyword">when</span> nicks <span class="org-comment-delimiter">;; </span><span class="org-comment">(8)</span>
          (insert (format wicked/bbdb-hello-string 
                          (mapconcat 'identity (nreverse nicks) <span class="org-string">", "</span>))
                  <span class="org-string">" "</span>))
        (<span class="org-keyword">when</span> salutations <span class="org-comment-delimiter">;; </span><span class="org-comment">(9)</span>
          (insert (mapconcat 'identity salutations <span class="org-string">" "</span>)))
        (<span class="org-keyword">when</span> (or nicks salutations)
          (insert <span class="org-string">"\n\n"</span>)))))
  (goto-char (point-min)))

(<span class="org-keyword">defadvice</span> <span class="org-function-name">gnus-post-news</span> (after wicked/bbdb activate)
  <span class="org-doc">"Insert nicknames or custom salutations."</span>
  (wicked/gnus-add-nick-to-message))

(<span class="org-keyword">defadvice</span> <span class="org-function-name">gnus-msg-mail</span> (after wicked/bbdb activate)
  <span class="org-doc">"Insert nicknames or custom salutations."</span>
  (wicked/gnus-add-nick-to-message))

(<span class="org-keyword">defadvice</span> <span class="org-function-name">gnus-summary-reply</span> (after wicked/bbdb activate)
  <span class="org-doc">"Insert nicknames or custom salutations."</span>
  (wicked/gnus-add-nick-to-message))
</pre>
</div>
<p>For BBDB v3</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-comment-delimiter">;; </span><span class="org-comment">This version is for BBDBv3 - thanks, Thomas!</span>

(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/gnus-nick-threshold</span> 5 <span class="org-doc">"*Number of people to stop greeting individually. Nil means always greet individually."</span>)  <span class="org-comment-delimiter">;; </span><span class="org-comment">(1)</span>
(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/bbdb-hello-string</span> <span class="org-string">"Hello, %s!"</span> <span class="org-doc">"Format string for hello. Example: \"Hello, %s!\""</span>)
(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/bbdb-hello-all-string</span> <span class="org-string">"Hello, all!"</span> <span class="org-doc">"String for hello when there are many people. Example: \"Hello, all!\""</span>)
(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/bbdb-nick-field</span> 'nick <span class="org-doc">"Symbol name for nickname field in BBDB."</span>)
(<span class="org-keyword">defvar</span> <span class="org-variable-name">wicked/bbdb-salutation-field</span> 'hello <span class="org-doc">"Symbol name for salutation field in BBDB."</span>)

(<span class="org-keyword">defun</span> <span class="org-function-name">wicked/gnus-add-nick-to-message</span> ()
  <span class="org-doc">"Inserts \"Hello, NICK!\" in messages based on the recipient's nick field."</span>
  (interactive)
  (<span class="org-keyword">let</span> ((recipients (bbdb-get-address-components))
        recipient nicks rec net salutations)
    (goto-char (point-min))
    (<span class="org-keyword">when</span> (re-search-forward <span class="org-string">"&#45;&#45;text follows this line&#45;&#45;"</span> nil t)
      (forward-line 1)
      (<span class="org-keyword">if</span> (and wicked/gnus-nick-threshold 
               (&gt;= (length recipients) wicked/gnus-nick-threshold))
          (insert wicked/bbdb-hello-all-string <span class="org-string">"\n\n"</span>) <span class="org-comment-delimiter">;; </span><span class="org-comment">(3)</span>
        (<span class="org-keyword">while</span> recipients
          (setq recipient (car recipients))
          (setq net (nth 1 recipient))
          (setq rec (car (bbdb-search (bbdb-records) nil nil net)))
          (<span class="org-keyword">cond</span>
           ((null rec) <span class="org-comment-delimiter">;; </span><span class="org-comment">(4)</span>
            (add-to-list 'nicks (car recipient))) 
           ((bbdb-record-xfield rec wicked/bbdb-salutation-field) <span class="org-comment-delimiter">;; </span><span class="org-comment">(5)</span>
            (add-to-list 'salutations 
                         (bbdb-record-xfield rec wicked/bbdb-salutation-field))) 
           ((bbdb-record-xfield rec wicked/bbdb-nick-field) <span class="org-comment-delimiter">;; </span><span class="org-comment">(6)</span>
            (add-to-list 'nicks 
                         (bbdb-record-xfield rec wicked/bbdb-nick-field)))
           (t
            (add-to-list 'nicks
                         (car (split-string (bbdb-record-name rec)))))) <span class="org-comment-delimiter">;; </span><span class="org-comment">(7) </span>
          (setq recipients (cdr recipients))))
      (<span class="org-keyword">when</span> nicks <span class="org-comment-delimiter">;; </span><span class="org-comment">(8)</span>
        (insert (format wicked/bbdb-hello-string 
                        (mapconcat 'identity (nreverse nicks) <span class="org-string">", "</span>))
                <span class="org-string">" "</span>))
      (<span class="org-keyword">when</span> salutations <span class="org-comment-delimiter">;; </span><span class="org-comment">(9)</span>
        (insert (mapconcat 'identity salutations <span class="org-string">" "</span>)))
      (<span class="org-keyword">when</span> (or nicks salutations)
        (insert <span class="org-string">"\n\n"</span>)))))

(<span class="org-keyword">defadvice</span> <span class="org-function-name">gnus-post-news</span> (after wicked/bbdb activate)
  <span class="org-doc">"Insert nicknames or custom salutations."</span>
  (wicked/gnus-add-nick-to-message))

(<span class="org-keyword">defadvice</span> <span class="org-function-name">gnus-msg-mail</span> (after wicked/bbdb activate)
  <span class="org-doc">"Insert nicknames or custom salutations."</span>
  (wicked/gnus-add-nick-to-message))

(<span class="org-keyword">defadvice</span> <span class="org-function-name">gnus-summary-reply</span> (after wicked/bbdb activate)
  <span class="org-doc">"Insert nicknames or custom salutations."</span>
  (wicked/gnus-add-nick-to-message))
</pre>
</div>
<p>After you add this code, you can store personalized nicknames and salutations in your BBDB. Nicknames and salutations will be looked up using people&#8217;s e-mail addresses. While in the <code>*BBDB*</code> buffer, you can type <code>C-o (bbdb-insert-new-field)</code> to add a field to the current record. Add a <code>nick</code> field with the person&#8217;s nickname, or a <code>hello</code> 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&#8217;s name will be used instead.</p>
<p>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 <code>wicked/gnus-nick-threshold</code> is to greet up to four people individually, and greet more people collectively. If you always want to greet people individually, add <code>(setq wicked/gnus-nick-threshold nil)</code> to your <code>~/.emacs</code>. If you want to change the strings used to greet people individually or collectively, change <code>wicked/bbdb-hello-string</code> and <code>wicked/bbdb-hello-all-string</code>. If you want to store the data into different fields, change <code>wicked/bbdb-nick-field</code> and <code>wicked/bbdb-salutation-field</code>, but note that old data will not be automatically copied to the new fields.</p>
<p>Here&#8217;s how the code works. First, it retrieves the list of addresses from the header(2). If there are more addresses than <code>wicked/gnus-nick-threshold</code>, then <code>wicked/bbdb-hello-all-string</code> 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&#8217;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.</p>
<p>You can <a href="https://sachachua.com/blog/2008/03/wicked-cool-emacs-bbdb-use-nicknames-and-custom-salutations/#comment">view 4 comments</a> or <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2008%2F03%2Fwicked-cool-emacs-bbdb-use-nicknames-and-custom-salutations%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></content>
		</entry>
</feed>