Emacs BBDB magic: Greeting people with nicknames

I use Gnus to read my mail within the Emacs text editor. One of the
advantages of using a mail client that’s infinitely programmable is
that you can add all sorts of little tweaks to it. Gnus can be
integrated with Emacs’ Big Brother Database (BBDB), a semi-structured
text database in which I store all sorts of weird notes. This little
hack takes the nick field of the database and automatically inserts a
greeting. If someone signs himself as Mikong, I should call him that
instead of Joseph Michael. Similarly, I sign my messages as Sacha, not
Sandra Jean. This little tidbit makes it easier to remember to call
people by their nicknames.

(defun sacha/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 (list (assoc 'recipients bbdb-get-addresses-headers)))
          nicks)
      (setq nicks
            (delq nil
                  (mapcar (lambda (rec) (bbdb-record-getprop rec 'nick))
                          (bbdb-update-records
                           (bbdb-get-addresses nil gnus-ignored-from-addresses 'gnus-fetch-field)
                           nil
                           nil))))
      (goto-char (point-min))
      (when (and nicks
                 (re-search-forward "--text follows this line--" nil t))
        (forward-line 1)
        (insert "Hello, "
                (mapconcat 'identity nicks ", ")
                "!\n\n")))))

(defadvice gnus-post-news (after sacha activate)
  (sacha/gnus-add-nick-to-message))

On Technorati: , , ,

Random Japanese sentence: 虎を大きな猫というなら、同じように猫を小さな虎といってもよい。 You may as well call a cat a small tiger as call a tiger a big cat.

Emacs: Show only people whom I haven’t pinged since…

One of the things I want in a contact management system is a quick way
to find out who I haven’t pinged in a while. The following code
filters currently-displayed contacts to show who I might want to get
back in touch with. Call it from a *BBDB* window and specify the date
(could be 2006.01.01 for annual, -7 for the last seven days, etc.).
This works incredibly well with the following hacks:

I should write a small book about how to build a contact management
system with Emacs. ;) It’s insanely powerful, you know.

(require 'planner)
(require 'bbdb)
(defun sacha/bbdb-show-only-no-contact-since (date)
  "Show only people who haven't been pinged since DATE or at all."
  (interactive (list (planner-read-date)))
  (let ((records bbdb-records)
        new-records
        last-match
        omit
        notes)
    (while records
      ;; Find the latest date mentioned in the entry
      (setq notes (or (bbdb-record-notes (caar records)) ""))
      (setq last-match nil omit nil)
      (while (string-match
              "[0-9][0-9][0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9]"
              notes
              (or last-match 0))
        (unless (string> date (match-string 0 notes))
          (setq omit t)
          (setq last-match (length notes)))
        (setq last-match (match-end 0)))
      (unless (and last-match omit)
        (add-to-list 'new-records (caar records) t))
      (setq records (cdr records)))
    (bbdb-display-records new-records)))

One of the other things I’d like to smooth over is keeping track of
who owes whom e-mail… <laugh>

On Technorati: , , , , , ,

Emacs: BBDB rapid serial visualization

And because it’s good to quickly flash through records once in a while
to refresh my memory…

(defvar sacha/bbdb-rapid-serial-visualization-delay
 1
 "*Number of seconds to wait between records.
Set to 0 to wait for input.")

(defun sacha/bbdb-rapid-serial-visualization ()
  "Breeze through everyone's name and notes."
  (interactive)
  (window-configuration-to-register ?a)
  ;; Copy the currently visible records
  (let ((records bbdb-records)
        (default-size (face-attribute 'default :height))
        (new-size 400)
        (continue t))
    (set-face-attribute 'default nil :height new-size)
    (pop-to-buffer (get-buffer-create "BBDB-Serial"))
    (delete-other-windows)
    (while (and records continue)
      (insert (bbdb-record-name (caar records))
              "\n\n"
              (or (car (bbdb-record-net (caar records))) "No e-mail")
              "\n\n"
              (or (bbdb-record-notes (caar records)) "")
              (make-string 50 ?\n))
      (goto-char (point-min))
      (sit-for sacha/bbdb-rapid-serial-visualization-delay)
      (setq records (cdr records)))
    (set-face-attribute 'default nil :height default-size)
    (when continue
      (jump-to-register ?a))))

On Technorati: , , , ,

Emacs BBDB: Prioritize exact matches

I often include people’s names in my notes on other people, such as
when I’m tracking who introduced me to whom. The following code
modifies BBDB’s behavior to put exact matches for name, company, or
network address above matches for notes.

(defun sacha/bbdb (string elidep)
  "Display all entries in the BBDB matching the regexp STRING
in either the name(s), company, network address, or notes.
Prioritize non-note matches."
  (interactive
   (list (bbdb-search-prompt "Search records %m regexp: ")
         current-prefix-arg))
  (let* ((bbdb-display-layout (bbdb-grovel-elide-arg elidep))
         (notes (cons '* string))
         (records-top
          (bbdb-search (bbdb-records) string string string nil
                       nil))
         (records
          (bbdb-search (bbdb-records) string string string notes
                       nil))
         temp)
    (setq temp records-top)
    (while temp
      (setq records (delete (car temp) records))
      (setq temp (cdr temp)))
    (if (or records-top records)
        (bbdb-display-records (append
                               records-top
                               records))
      ;; we could use error here, but it's not really an error.
      (message "No records matching '%s'" string))))

(defalias 'bbdb 'sacha/bbdb)

On Technorati: , , , ,

Crazy idea for Emacs: Random Emacs taglines

Would anyone happen to know of a way to select a random symbol with a
description?

Actually. Hmm.

(progn
  (apropos ".")
  (write-file "~/.taglines.random-emacs-symbols")
  (delete-matching-lines "Plist")
  (delete-matching-lines "not documented")
  (replace-regexp "\n  " " - " nil)
  (delete-non-matching-lines " - "))

Et voila! Random Emacs taglines together with the code:

(defun sacha/random-tagline (&optional file)
  "Return a random tagline and put it in the kill ring."
  (interactive)
  (with-current-buffer (find-file-noselect (or file "~/.taglines"))
    (goto-char (random (point-max)))
    (let ((string
           (buffer-substring (line-beginning-position)
                             (line-end-position))))
      (kill-new string)
      string)))

(defadvice remember (after sacha-tagline activate)
  "Add random tagline."
  (save-excursion
  (goto-char (point-max))
  (insert "\n\nRandom Emacs symbol: "
          (sacha/random-tagline "~/.taglines.random-emacs-symbols")
          "\n\n")))))

On Technorati: , ,

Random Emacs symbol: eshell-remove-entries – Function: From PATH, remove all of the given FILES, perhaps interactively.

Crazy Emacs: Personalized signatures with random taglines

Of course, that naturally leads to the crazy idea: “What if I can
personalize my signatures?” Knowing that Paul Lussier is an Emacs geek, I can reward him for reading all the way
to the bottom of my message… ;)

(defun sacha/gnus-personalize-signature ()
  "Personalizes signature based on BBDB signature field.
BBDB signature field should be a lambda expression.
First person with a custom signature field gets used."
  (let* ((bbdb-get-addresses-headers
          (list (assoc 'recipients bbdb-get-addresses-headers)))
         (records (bbdb-update-records
                   (bbdb-get-addresses
                    nil
                    gnus-ignored-from-addresses 'gnus-fetch-field)
                   nil
                   nil))
         signature)
    (while (and records (not signature))
      (when (bbdb-record-getprop (car records) 'signature)
        (setq signature
              (eval (read (bbdb-record-getprop (car records)
                                               'signature)))))
      (setq records (cdr records)))
    (or signature t)))
(setq-default message-signature 'sacha/gnus-personalize-signature)

So then all I have to do is add the following field to his record:

      signature: (concat "Sacha Chua - Emacs geek
                 What crazy idea can I help you hack next?
                 Random Emacs symbol: "
                 (sacha/random-tagline
                  "~/.taglines.random-emacs-symbols"))

Emacs. One crazy idea at a time. Now I can use this to select random
information, like my favorite networking books or a list of my
upcoming events…

On Technorati: , , ,

Random Emacs symbol: sort-coding-systems-predicate – Variable: If non-nil, a predicate function to sort coding systems.