(defun TT:c-header-file-ifndef-encapsulate () (let ((filename buffer-file-name) (commentform (if (eq major-mode 'c++-mode) " // %s " " /* %s */ ")) (identifier) (p)) (when (and (null (file-attributes filename)) ; new file (string-match "\.\(h\|hh\)$" filename)) (setq identifier (concat "_" (mapconcat 'upcase (split-string (file-name-nondirectory filename) "\.") "_"))) (goto-char (point-min)) (insert "#ifndef " identifier " #define " identifier " ") (setq p (point-max)) (insert " #endif" (format commentform identifier)) (goto-char p)))) (add-hook 'c-mode-common-hook (function (lambda () (TT:c-header-file-ifndef-encapsulate))))
うーん,便利だけどダサいね。 新規 file かどうかの判定って,どうやるのがカッチョいいのかな。