howmで複数のテンプレートを使う
機能としてはもともと組み込まれてるのね関数が全然ドキュメントされてないので追わないとわからない変数の方にはドキュメントされてましたごめんなさい。
howm-createは2つの引数を取るが、そのうち1つ目(C-uで渡せる)がテンプレートの番号の指定になっている。
howm-mode.el:
(defun howm-create (&optional which-template here) (interactive "p") (let* ((t-c (howm-create-default-title-content)) (title (car t-c)) (content (cdr t-c))) (howm-create-file-with-title title which-template nil here content)))
hereはファイルを今いるファイルに書き込むかどうかでデフォルトはそうしないで新しく作るようになってる。で、テンプレートの挿入はhowm-templateが文字列か関数かリストかによって適当に振舞うようになっていて、リストならwhich-template番目のを挿入するようになってる(1始まり、デフォルトは1)。howm-templateはデフォルトだと文字列になってますね。
howm-mode.el:
(defun howm-template-string (which-template previous-buffer) ;; which-template should be 1, 2, 3, ... (setq which-template (or which-template 1)) (cond ((stringp howm-template) howm-template) ((functionp howm-template) (let ((args (if howm-template-receive-buffer (list which-template previous-buffer) (list which-template)))) (apply howm-template args))) ((listp howm-template) (nth (- which-template 1) howm-template))))
そういうわけで.emacsに以下みたいにすればC-u 2 C-c , cあるいはC-c , nで日記のテンプレートを使ってファイルを作れる。
.emacs:
(setq howm-template '("= %title%cursor %date %file " "= nikki %date %cursor")) (defun howm-create-nikki () (interactive) (howm-create 2 nil)) (define-key global-map (concat howm-prefix "n") #'howm-create-nikki)
…とここまできてdescribe-variable howm-templateの方にはちゃんとかいてあったことに気付いてがっくり。どうせなので張り付けておく。
howm-template is a variable defined in `howm-mode.el'.
Documentation: Contents of new file. %xxx are replaced with specified items. If it is a list,-th one is used when you type C-u M-x howm-create. If it is a function, it is called to get template string with the argument .
ちなみに置換ルールは変数howm-template-rulesに定義されている。いじる場合はカーソルのは最後にしないといけないそうな。
howm-mode.el
(howm-defvar-risky howm-template-rules '(("%title" . howm-template-title) ("%date" . howm-template-date) ("%file" . howm-template-previous-file) ("%cursor" . howm-template-cursor))) ;; Cursor must be the last rule.
連想配列になってるのは引数としてタイトルと日付とファイル名の連想リストが渡される(howm-insert-templateの定義の最後)関数。
howm-mode.el
;; Use dynamic bindings dirtily! (defun howm-template-title (arg) (insert (cdr (assoc 'title arg)))) (defun howm-template-date (arg) (insert (cdr (assoc 'date arg)))) (defun howm-template-previous-file (arg) (insert (cdr (assoc 'file arg)))) (defun howm-template-cursor (arg)) ;; do nothing
howm-mode.el
(defun howm-insert-template (title &optional ;;snip (let ((arg `((title . ,title) (date . ,date) (file . ,file))) ;;snip