EmacsからInstapaperに記事を追加できるようにした

Emacsの設定シリーズ第二弾です。今回は後で読むサービスのうちの一つであるInstapaperにEmacsから記事を追加できるようにしました。

基本的にはleaf.elを使ってemacs-instapaperを設定するだけで使えますが、web feeds clientであるelfeedで見た記事を後で読みたいことが良くあるので、elfeedから記事を追加できるような設定にしました。以下に設定の全体を示します。

(leaf instapaper
  :ensure t
  :require t
  :init
  ;; auth-sourceでpassを使えるようにする
  (auth-source-pass-enable)
  :custom
  (auth-source-pass-extra-query-keywords . t)
  `(instapaper-username . ,(plist-get
                            (car (auth-source-search :host "instapaper.com"))
                            :user))
  `(instapaper-password . ,(auth-source-pick-first-password
                            :host "instapaper.com"
                            :user instapaper-username))
  :config
  (defun elfeed-show-link-instapaper-add ()
    (interactive)
    (let* ((url (elfeed-entry-link elfeed-show-entry)))
      (instapaper-add url)))
  :bind
  (:elfeed-show-mode-map
   ("I" . elfeed-show-link-instapaper-add)))

まず Instapaper のログイン情報を設定します。ここでは auth-source を使ってログイン情報を取得します。

(auth-source-pass-extra-query-keywords . t)
`(instapaper-username . ,(plist-get
                          (car (auth-source-search :host "instapaper.com"))
                          :user))
`(instapaper-password . ,(auth-source-pick-first-password
                          :host "instapaper.com"
                          :user instapaper-username))

特に auth-source から pass にあるログイン情報を取得できるような設定をしておきます。

;; auth-sourceでpassを使えるようにする
(auth-source-pass-enable)

次に、 elfeed-show-mode で entry のリンクを Instapaper に追加する関数を定義します。

(defun elfeed-show-link-instapaper-add ()
  (interactive)
  (let* ((url (elfeed-entry-link elfeed-show-entry)))
    (instapaper-add url)))

最後に elfeed-show-mode で上記の関数を呼ぶための key bind を設定します。

(:elfeed-show-mode-map
 ("I" . elfeed-show-link-instapaper-add))