02 July, 2011

Orgmode Capture to Insert a Mairix Link, in VM

Problem -- inserting a Mairix type link when calling Orgmode capture in VM. Also, opening the link from an Orgmode buffer should retrieve the message thread through a Mairix search and display it in a VM folder.

I had a look at org-mairix.el, to maybe extend it with VM support, but being short on time I came up with a rather crude solution in my Emacs init file, ~/.emacs
;; Add mairix as new link type in org
(org-add-link-type "mairix" 'ra/org-vm-mairix-open)
(add-hook 'org-store-link-functions 'ra/org-vm-store-mairix-link)

;; Store link as mairix search for message ID
;; Most from org-vm-store-link in org-vm.el, stripped and modded
(defun ra/org-vm-store-mairix-link ()
  "Store a link to the current VM message as a Mairix search for
  its Message ID." 
  (when
      (and (or (eq major-mode 'vm-summary-mode)
               (eq major-mode 'vm-presentation-mode))
           (save-window-excursion
             (vm-select-folder-buffer) buffer-file-name))
    (and (eq major-mode 'vm-presentation-mode)
         (vm-summarize))
    (vm-follow-summary-cursor)
    (save-excursion
      (vm-select-folder-buffer)
      (let* (;; Collect properties
             (message (car vm-message-pointer))
             (subject (vm-su-subject message))
             (from (vm-get-header-contents message "From"))
             (to (vm-get-header-contents message "To"))
             (message-id (vm-su-message-id message))
             (link (org-make-link 
                    "mairix:"
                    (format "m:%s"
                            (org-remove-angle-brackets message-id)))))
        ;; TODO: Store even more properties so
        ;; org-email-link-description-format could be used in full
        (org-store-link-props :from from
                              :to to
                              :subject subject
                              :link link)
        (org-add-link-props :description (org-email-link-description))
        ;; Save mail folder and update the mairix db
        (vm-save-folder)
        (mairix-update-database) ; Make sure mairix-synchronous-update
                                 ; is nil, default, so we do not have
                                 ; to wait
        ;; TODO: Suppress message from
        ;; mairix-sentinel-mairix-update-finished
        link))))

;; Open the mairix link, making use of mairix.el to make the search
;; and display the results
(defun ra/org-vm-mairix-open (msearch)
  "Search for messages with mairix, and display them in a VM folder.
  This requires a proper mairix.el setup."
  (mairix-search msearch t))
Finally I can quickly orgmode-capture something when reading a message in VM, without the referring orgmode link becomming broken as soon as I file away the message to another folder.

Of course, I was first required to install and configure Mairix, as well as setting up the Emacs Mairix mode, mairix.el. Here is the contents of my Mairix configuration file, ~/.mairixrc
base=~/mail
mbox=*...
mformat=mbox
mfolder=search
database=~/.mairixdb
nochecks
omit=.*
omit=archive
omit=search
omit=mairix
omit=from
omit=magick*
omit=**~
omit=#*
omit=stunnel.log
and the mairix.el setup in my Emacs init file
(require 'mairix)
(setq mairix-file-path "~/mail"
      mairix-mail-program (quote vm)
      mairix-search-file "search")
I also want to direct a small thank you to VM's current maintainer Uday Reddy, who suggested using Mairix links.