Auto Compile Insert

kt-blackout2004-04-22


昨日作ったauto-compile(id:kt-blackout:20040421#p1参照)に関連したユティリティを作った。と、言っても大したものではないけど。

(defvar auto-compile-command "gcc -o %e %f")

(defun auto-compile-insert ()
  "Auto Compile Command Insert"
  (interactive)
  (let ( (execmd "")
        (file (buffer-name)))
    (if (string-match "\\(.*\\)\\.[^\\.]" file)
        (setq execmd (substring file (match-beginning 1) (match-end 1)))
      (error (format "Unknown file suffix(%s{%s})" file execmd)))
    (save-excursion
      (goto-char (point-min))
      (insert (format "/* -*- auto-compile: %s -*- */\n"
                      (get-compile-command auto-compile-command file execmd)))
      )
    ))


(defun get-compile-command (cmd file exe)
  (setq cmd (replace-regexp-in-string "%e" exe cmd))
  (setq cmd (replace-regexp-in-string "%f" file cmd)))


このElispを適当な箇所*1に保存して、M-x auto-compile-commandと呼び出すと現在のファイルの先頭に

/* -*- auto-compile: gcc -o %e %f -*- */

と書きこんでくれる(%eはファイル名から拡張子を抜いたもの、%fはファイル名に変換される)。コメントのフォーマットは独断と偏見によりC形式固定。コンパイル用のコマンドは次のようにして変更できる。

(setq auto-compile-command "gcc -Wall -O2 -o %e %f")

*1:一番簡単なのは~/.emacsの中かな