howmファイルを特定のフォルダに作る改−キーバインドと色付け

前回作ったのを改造してみた。追加したのは二点。

要はhowmに、はてな記法の見出しを実装したいということですね。
今までは、直接.xyzzyにコードを書いて実装していた。せっかくモードを設定したので、モードにくっつけることにする。hateda-mode.lを参考にしました。

howm-create2dir.l

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;本体
(defvar *temp-howm-dir* nil)

(defun make-howm-create2dir-file (howm-dir)
  (let* ((filename (format-date-string "%Y-%m-%d-%H%M%S.howm"))
		 (filepath (merge-pathnames filename howm-dir))
		 (directory (directory-namestring filepath)))
	(if (not (file-directory-p directory))
		(create-directory directory))
	(find-file filepath)
	(if (eq (point-max) (point-min))
		(progn
		  (insert (format-date-string "= \n\n[%Y-%m-%d %H:%M]\n"))
		  (goto-char (+ (point-min) 2))))
	))
(defun howm-create2dir ()
  (interactive)
  (if si:*command-line-args* (setq *howmdir* (pop si:*command-line-args*)))
  (make-howm-create2dir-file *temp-howm-dir*)
  (howm-create2dir-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;キーバインド
;;      C-*     行頭に"**"を挿入する
;;      C-]     行頭に"*"を挿入する
;;      C--     行頭に"-"を挿入する
;;      C-+     行頭に"+"を挿入する

(defun hateda-heading1 ()
  (interactive "*")
  (save-excursion
  (goto-bol)
  (insert "*")
  (fix-selection-point)))

(defun hateda-heading2 ()
  (interactive "*")
  (save-excursion
  (goto-bol)
  (insert "**")
  (fix-selection-point)))

(defun hateda-num-list ()
  (interactive "*")
  (save-excursion
  (goto-bol)
  (insert "+")
  (fix-selection-point)))

(defun hateda-list ()
  (interactive "*")
  (save-excursion
  (goto-bol)
  (insert "-")
  (fix-selection-point)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;モードの設定
(defun howm-create2dir-mode ()
  (interactive)
  (kill-all-local-variables)
  (setq buffer-mode 'howm-create2dir-mode)
  (setq mode-name "howm Create2dir")
  (make-local-variable 'regexp-keyword-list)
  (setq hateda-regexp-keyword-list
        (compile-regexp-keyword-list
  `(
      ("^[=].*" t (:color 7 13 :line :bold ))
    ("^[*][*][^*].*" t (:color 10 15 :line ))
    ("^[*][*][*][^*].*" t (:color 9 12 :line ))
    )))
  (setq regexp-keyword-list hateda-regexp-keyword-list)
  (use-keymap *howm-create2dir-mode-map*))


(defvar *howm-create2dir-mode-map* nil)
  (unless *howm-create2dir-mode-map*
    (setq *howm-create2dir-mode-map* (make-sparse-keymap))
    (define-key *howm-create2dir-mode-map* '#\C-] 'hateda-heading1)
    (define-key *howm-create2dir-mode-map* '#\C-: 'hateda-heading2)
    (define-key *howm-create2dir-mode-map* '#\C-+ 'hateda-num-list)
    (define-key *howm-create2dir-mode-map* '#\C-- 'hateda-list)
    (define-key *howm-create2dir-mode-map* '(#\C-c #\, #\c) 'howm-create2dir))

howm-modoki-create.l

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;本体

(defun make-howm-modoki-file (howm-dir)
  (let* ((filename (format-date-string "%Y/%m/%Y-%m-%d-%H%M%S.howm"))
		 (filepath (merge-pathnames filename howm-dir))
		 (directory (directory-namestring filepath)))
	(if (not (file-directory-p directory))
		(create-directory directory))
	(find-file filepath)
	(if (eq (point-max) (point-min))
		(progn
		  (insert (format-date-string "= \n\n[%Y-%m-%d %H:%M]\n"))
		  (goto-char (+ (point-min) 2))))
	))
(defun howm-modoki-create ()
  (interactive)
  (make-howm-modoki-file "~/howm")
  (howm-modoki-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;キーバインド
;;      C-*     行頭に"**"を挿入する
;;      C-]     行頭に"*"を挿入する
;;      C--     行頭に"-"を挿入する
;;      C-+     行頭に"+"を挿入する

(defun hateda-heading1 ()
  (interactive "*")
  (save-excursion
  (goto-bol)
  (insert "*")
  (fix-selection-point)))

(defun hateda-heading2 ()
  (interactive "*")
  (save-excursion
  (goto-bol)
  (insert "**")
  (fix-selection-point)))

(defun hateda-num-list ()
  (interactive "*")
  (save-excursion
  (goto-bol)
  (insert "+")
  (fix-selection-point)))

(defun hateda-list ()
  (interactive "*")
  (save-excursion
  (goto-bol)
  (insert "-")
  (fix-selection-point)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;モードの設定

(defun howm-modoki-mode ()
  (interactive)
  (kill-all-local-variables)
  (setq buffer-mode 'howm-modoki-mode)
  (setq mode-name "howm Modoki")
  (make-local-variable 'regexp-keyword-list)
  (setq hateda-regexp-keyword-list
        (compile-regexp-keyword-list
  `(
      ("^[=].*" t (:color 7 13 :line :bold ))
    ("^[*][*][^*].*" t (:color 10 15 :line ))
    ("^[*][*][*][^*].*" t (:color 9 12 :line ))
    )))
  (setq regexp-keyword-list hateda-regexp-keyword-list)
  (use-keymap *howm-modoki-mode-map*))


(defvar *howm-modoki-mode-map* nil)
  (unless *howm-modoki-mode-map*
    (setq *howm-modoki-mode-map* (make-sparse-keymap))
    (define-key *howm-modoki-mode-map* '#\C-] 'hateda-heading1)
    (define-key *howm-modoki-mode-map* '#\C-: 'hateda-heading2)
    (define-key *howm-modoki-mode-map* '#\C-+ 'hateda-num-list)
    (define-key *howm-modoki-mode-map* '#\C-- 'hateda-list)
    (define-key *howm-modoki-mode-map* '(#\C-c #\, #\c) 'howm-modoki-create))

.xyzzy

howmファイルを起動したときに、自動的にhowm-modokiモードになるようにする。
.xyzzyに、以下を追加。

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;howmモード

(pushnew '("\\.howm$" . howm-modoki-mode) *auto-mode-alist* :test #'equal)
(require "howm-create2dir")
(require "howm-modoki-create")

あまり仕組みがわからずに書いているが、今のところ不都合なく動いている。本当はもっとスマートに書けるんだろうけど、とりあえずはこれでいいや。