helm-github-stars.el という便利な Emacs Lisp ツールがあります。

「自分がつけた GitHub の Star」や「自分または Organization 所有の
リポジトリ」等を Helm interface で操作できます。

「Star 付けたリポジトリがあったけど、なんて名前だったかなー」
なんて時に、素早く検索してブラウザで開けたりします。便利です。

課題

helm-github-stars.el は便利なのですが、キャッシュファイル
(~/.emacs.d/hgs-cache)を作る hgs/generate-cache-file()
同期処理関数です。Emacs built-in の url-retrieve-synchronously()
を使っています。

うっかり M-x helm-github-stars すると、数十秒待たされます。辛いです。

※ helm-github-stars-refetch-time を設定に基づき、
M-x helm-github-stars のタイミングでキャッシュファイルが更新されます。

そういうわけで、最近は当該キーバインドをコメントアウトしている状態
でした。

つい先日の木曜日。重い腰を上げて、非同期処理版の
hgs/generate-cache-file() を作ろうとしました。でも、Star と Repo
両方の API を呼ぶ必要があり、なかなかに複雑です。コーディングが進
みません。

そんな中見つけたのが async.el でした。

async.el

今回使ったのは、async.el に含まれる async-start() です。

async-start START-FUNC FINISH-FUNC

  • START-FUNC は子プロセスの Emacs で実行される
  • FINISH-FUNC は現在の Emacs で実行される

上記のとおり、このライブラリはなんと Emacs の子プロセスとして、も
うひとつ Emacs を起動
し、そこで非同期処理をして結果を返してくれま
す。変態です。

子プロセスの Emacs は -Q オプションで高速に起動され、START-FUNC の
終了とともに終了します。

START-FUNC 実行中の Emacs プロセスの様子

ご存知の方も多いと思いますが、Emacs で非同期処理を実現するためには、
TCP コネクションを開くか、外部コマンドを起動しなくてはいけません。

ただ、外部コマンドの種類に制限はないので、Emacs に S 式を渡して処
理させたというわけです。天才か。

コード自体はそこまで凝ったことはしていません。子プロセスとして
Emacs を起動する
以外は start-process() を使った普通の非同期処理 です。

ですが、このようなシンタックスとして定義されていると、非同期処理を
感じさせません。Lisp 処理系の真骨頂ですね。

変更前

async.el で非同期化する前のコードです。

~/.emacs.d/hgs-cache を作ってから 0.5 日経った後に
M-x helm-github-stars すると、そのタイミングで同期処理が実行されます。
私の場合だと 20 秒ほど待たされます。

(defun my-lisp-load (filename)
"Load lisp from FILENAME"
(let ((fullname (expand-file-name (concat "spec/" filename) user-emacs-directory))
lisp)
(when (file-readable-p fullname)
(with-temp-buffer
(progn
(insert-file-contents fullname)
(setq lisp
(condition-case nil
(read (current-buffer))
(error ()))))))
lisp))
;;; helm-github-stars.el
(setq helm-github-stars-token (my-lisp-load "helm-github-stars-token"))
(setq helm-github-stars-name-length 50)
(setq helm-github-stars-refetch-time 0.5)
view raw 00before.el hosted with ❤ by GitHub

※ my-lisp-load() は [2016-05-06-2] で解説しています。

変更後

async.el で非同期化したコードです。

Emacs を起動すると、
my-helm-github-stars-async-generate-cache-file() が
Emacs Timer に登録されつつ、初回の実行をします。

このコードだと、この後 1 時間ごとに非同期(バックグラウンド)で実
行されます。

(defun my-lisp-load (filename)
"Load lisp from FILENAME"
(let ((fullname (expand-file-name (concat "spec/" filename) user-emacs-directory))
lisp)
(when (file-readable-p fullname)
(with-temp-buffer
(progn
(insert-file-contents fullname)
(setq lisp
(condition-case nil
(read (current-buffer))
(error ()))))))
lisp))
;;; helm-github-stars.el
(require 'helm-github-stars)
(defvar my-helm-github-stars-interval (* 3 60 60)
"Number of seconds to call `my-helm-github-stars-async-generate-cache-file'.")
(defvar my-helm-github-stars-timer nil
"Timer object for GitHub Stars caching will be stored here.
DO NOT SET VALUE MANUALLY.")
(setq helm-github-stars-name-length 50)
(defun my-helm-github-stars-async-generate-cache-file ()
"Generate `helm-github-stars-cache-file' in the child emacs process"
(async-start
;; START-FUNC
`(lambda ()
(let ((start-time (current-time)))
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(require 'helm-github-stars)
(setq helm-github-stars-token ,(my-lisp-load "helm-github-stars-token"))
(hgs/generate-cache-file)
start-time))
;; FINISH-FUNC
(lambda (start-time)
(let ((now (current-time)))
(message "[GH] Success to GET my GitHub Stars and Repos (%0.1fsec) at %s."
(time-to-seconds (time-subtract now start-time))
(format-time-string "%Y-%m-%d %H:%M:%S" now))))))
(defun my-helm-github-stars-set-timer ()
"Set helm-github-stars timer."
(setq my-helm-github-stars-timer
(run-at-time "0 sec"
my-helm-github-stars-interval
#'my-helm-github-stars-async-generate-cache-file)))
(defun my-helm-github-stars-cancel-timer ()
"Cancel helm-github-stars timer."
(when my-helm-github-stars-timer
(cancel-timer my-helm-github-stars-timer)
(setq my-helm-github-stars-timer nil)))
(my-helm-github-stars-set-timer)
view raw 01after.el hosted with ❤ by GitHub

my-helm-github-stars-async-generate-cache-file() は async-start()
を呼ぶだけの関数です。

START-FUNC では、まず load-path を通すために、package 関連の処理を
します。

次に helm-github-stars-token を設定するために、my-lisp-load() の
S 式を評価してから、この Lambda 式を評価しています。

※ (lambda …) の前のバッククォートと (my-lisp-load …) の前のカ
ンマがセットです。

async-inject-variables() を使っても良かったのですが、
helm-github-stars-token は現在の Emacs では nil で良いため、
この形に落ち着きました。

FINISH-FUNC は *Messages* バッファにこんなログを出すだけです。

[GH] Success to GET my GitHub Stars and Repos (28.2sec) at 2017-10-21 00:33:14.

まとめ

async.el を使って、helm-github-stars.el に変更を加えることなく、
非同期化しました。

-Q で起動しているとは言え、Emacs は大きなプロセスですので、インター
バルの短かすぎる処理は避けたほうが良いと思います。

今回の GitHub 関連の情報は、「自分のはてブ」や「自分の Qiita:Team
のストック」とともに helm-my-bookmark() という関数で横串検索してい
ます。詳しい設定は私の init.el をどうぞ。

追記(2017-12-30):
Issue で紹介してみました(無謀奴)。
https://github.com/Sliim/helm-github-stars/issues/23