multi-term使用時にshellの履歴をhelm経由で挿入する

Emacsでshellを利用する場合,multi-termが便利であることは周知であろう.
このmulti-term使用時に,shellのhistoryをhelmのインターフェイスで挿入できると,さらに便利なはずである.
先人はすでにこの観点からコードを書いており,以下の記事で紹介されている.
multi-termにhelmでshell-historyを表示して挿入 - memo
本記事においてはhelmの勉強を兼ねて一から書きなおした.
以下にコードを示す.

(require 'helm)

(defvar helm-c-shell-history-buffer "*helm shell history*")

(defvar helm-shell-history-command "history | awk '{print $3,$4}'")

(defun helm-c-shell-history-init ()
  (with-current-buffer (helm-candidate-buffer 'global)
    (call-process-shell-command helm-shell-history-command nil t)))

(defun helm-c-shell-history-action (line)
  (insert line))

(defvar helm-c-shell-history-source
  '((name . "shell-history")
    (init . helm-c-shell-history-init)
    (candidates-in-buffer)
    (candidate-number-limit . 9999)
    (multiline)
    (action . helm-c-shell-history-action)
    (requires-pattern . 2)
    (delayed)))

(defun helm-shell-history ()
  (interactive)
  (helm :sources helm-c-shell-history-source
        :buffer helm-c-shell-history-buffer))

M-x helm-shell-historyとし,2文字以上入力することで絞り込み検索が開始される.
なお筆者はLinux上でtcshを利用している.
他のshell環境での動作確認はしていない.

車輪の再発明感がものすごくあるのは確かだが,実際に書いてみると勉強になるのもまた事実である.