Skip to content

Instantly share code, notes, and snippets.

@yoshinari-nomura
Created May 7, 2017 03:26
Show Gist options
  • Select an option

  • Save yoshinari-nomura/6b2df53aca016e3deb40c00258dd4d12 to your computer and use it in GitHub Desktop.

Select an option

Save yoshinari-nomura/6b2df53aca016e3deb40c00258dd4d12 to your computer and use it in GitHub Desktop.
Org-link-store for Mew with Gmail/IMAP
(with-eval-after-load 'org
(if (fboundp 'org-link-set-parameters)
;; org version 9
(org-link-set-parameters "gmail"
:follow #'glima-org-gmail-message-open
:store #'mewx-org-store-link)
;; org version 8
(org-add-link-type "gmail" 'glima-org-gmail-message-open)
(setq org-store-link-functions
(cons 'mewx-org-store-link org-store-link-functions))))
(defvar glima-gmail-top-url "https://mail.google.com"
"Gmail top URL.")
(defun glima-org-gmail-message-open (path)
"Open Gmail message PATH in via ``browse-url''.
PATH is a message-id in Gmail manner (NOT RFC822 one)."
(browse-url (format "%s/#all/%s"
glima-gmail-top-url
path)))
(defmacro mewx-with-current-message-buffer (&rest body)
"Eval BODY after switch from summary to message."
`(save-excursion
(mew-summary-goto-message)
(mew-sumsyn-match mew-regex-sumsyn-short)
(mew-summary-set-message-buffer
(mew-sumsyn-folder-name)
(mew-sumsyn-message-number))
,@body))
(defun mewx-org-store-link ()
"Store a link to a Gmail message/thread via Mew."
(when (or (equal major-mode 'mew-virtual-mode)
(equal major-mode 'mew-summary-mode)
(equal major-mode 'mew-message-mode))
(mewx-with-current-message-buffer
(let* ((from (mew-header-get-value "From:"))
(to (mew-header-get-value "To:"))
(message-id (mew-header-get-value "Message-Id:"))
(subject (mew-header-get-value "Subject:"))
(date (mew-header-get-value "Date:"))
(x-gm-msgid (mew-header-get-value "X-GM-MSGID:"))
(x-gm-thrid (mew-header-get-value "X-GM-THRID:"))
(date-ts (and date (format-time-string
(org-time-stamp-format t) (date-to-time date))))
(date-ts-ia (and date (format-time-string
(org-time-stamp-format t t)
(date-to-time date))))
link desc)
(org-store-link-props :type "mew" :from from :to to
:subject subject :message-id message-id)
(when date
(org-add-link-props :date date :date-timestamp date-ts
:date-timestamp-inactive date-ts-ia))
(when x-gm-msgid
(org-add-link-props :x-gm-msgid x-gm-msgid))
(when x-gm-thrid
(org-add-link-props :x-gm-thrid x-gm-thrid))
(setq desc (org-email-link-description))
(setq link (format "gmail://%s"
(or x-gm-msgid x-gm-thrid)))
(org-add-link-props :link link :description desc)
link))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment