Elisp - Auto Compile
Elispの勉強していることは昨日書いたが、さしあたって前から欲しかったものを練習がてら作ってみた。欲しかったものというのは、ちょっとしたCのプログラムを簡単にコンパイルするためのユティリティだ。下のElispを.emacsか任意のファイルに保存して、M-x auto-compileと呼び出すと、ファイルの先頭にある"-*- auto-compile: XXXX -*-"のXXXXの部分をコンパイルコマンドとしてコンパイルを開始する。もし、上記マークが見つからないときは普通にM-x compileを呼び出したときと同じようにコンパイルコマンドを聞いて来る
(defun auto-compile () "Auto Compile Sample: -*- auto-compile: gcc -o sample sample.c -*-" (interactive) (let ( (cmd "") ) (save-excursion (goto-char (point-min)) (if (re-search-forward "-\\*- auto-compile: \\(.*\\) -\\*-" nil t) (setq cmd (buffer-substring (match-beginning 1) (match-end 1)))) ) (if (string= cmd "") (progn (if (or (null compile-command) (string= compile-command "")) (progn (setq compile-command "make -k") (insert compile-command))) (setq cmd (read-from-minibuffer "Compile command: " compile-command)) ) ) (compile cmd)) )
コマンドにjavacなどを利用すれば、Javaでもいけるだろうし、別のコマンドにすれば基本的になんでもいけるはず。とにかく、はじめての実用性のある自作のコマンドなので、凄い達成感(笑)。
念のためキーにマップする方法を書いておく。ここでは、"C-x C"に割り当てる。
(define-key ctl-x-map "C" 'auto-compile)