2014年1月19日日曜日

Emacsでhowmファイルを任意のフォルダに作成

xyzzyで使ってたhowm-create2dir.lをEmacsに移植してみる。



howm-create2dir.は、引数に取ったディレクトリにhowm形式のファイルを作成する拡張Lisp。PPxと組み合わせての使用を想定している。

Emacsの設定


以下をsite-lispフォルダに保存する。

howm-create2dir.el

(defun make-howm-create2dir-file (&optional howm-dir)
  (interactive)
  (let* ((filename (format-time-string "%Y-%m-%d-%H%M%S.howm"))
  (filepath
   (if howm-dir
       (concat (file-name-as-directory howm-dir) (format-time-string "%Y-%m-%d-%H%M%S.howm"))
     (format-time-string "%Y-%m-%d-%H%M%S.howm"))))
    (find-file filepath)
    (if (eq (point-max) (point-min))
 (progn
   (insert (format-time-string "= \n\n[%Y-%m-%d %H:%M]\n"))
   (goto-char (+ (point-min) 2))))
    ))

(defun howm-create2dir (arg)
  (if (file-directory-p arg)
      (progn
 (make-howm-create2dir-file arg)
 (howm-create2dir-mode))))

(define-derived-mode howm-create2dir-mode nil "howm2dir"
       "homu mode"
       (define-key howm-create2dir-mode-map "\C-c,c" 'make-howm-create2dir-file))

(provide 'howm-create2dir)
(provide 'howm-create2dir-mode)

以下を.emacsにコピペ。

;; howm-create2dir読み込み
(require 'howm-create2dir)

;; howmファイルをhowm2dirモードで開く
(setq auto-mode-alist
      (cons (cons "\\.howm$" 'howm-create2dir-mode) auto-mode-alist))

PPxから呼び出す


ここからはPPx側の設定。
%Ob E:\emacs\bin\runemacs.exe -execute ("howm-create2dir \"%*regexp("%1","s/\\/\\\\/g")\"")

みたいなのを適当なキーかメニューに登録して実行すれば、カレントディレクトリにhowmファイルを作成する。
%*regexpの箇所でやってるのは、%1で取得したパスの修正。たとえばD:\howm\hogeならD:\\howm\\hogeとなるようにしている。 僕は、このコマンドをファイル作成メニューの一つとして登録している。

-|M_makefile =

M_makefile = { ** comment **
&howm = %Ob E:\emacs\bin\runemacs.exe -execute ("howm-create2dir \"%*regexp("%1","s/\\/\\\\/g")\"")
&TXT = *set name=%1\%"テキストファイル"%{%|.txt%} %: *makefile "%'name'" %: editor "%'name'" %: *jumppath "%'name'" /entry
&CFG = *set name=%1\%"CFGFile"%{%|.cfg%} %: *makefile "%'name'" %: editor "%'name'" %: *jumppath "%'name'" /entry
&PlainFile = *set name=%1\%"PlainFile"%E %: *makefile "%'name'" %: editor "%'name'" %: *jumppath "%'name'" /entry
&VBS = *set name=%1\%"VBScript"%{%|.vbs%} %: *makefile "%'name'" %: editor "%'name'" %: *jumppath "%'name'" /entry
&JS = *set name=%1\%"JScript"%{%|.js%} %: *makefile "%'name'" %: editor "%'name'" %: *jumppath "%'name'" /entry
}

KC_main = { ; PPcメイン窓
\K ,%M_makefile,H
}

0 件のコメント:

コメントを投稿