среда, 3 сентября 2008 г.

++Emacs

Emacs, как-никак рулит. :)

На досуге озадачился автоматической вставкой нужного заголовка в файлы. Первый попавшийся зверь - функция auto-insert, которой передается некий "мега-регэксп", исполняя который в начало файла вставляется некий шаблон. Особенность в том, что в этом "мега-регэкспе" можно писать любой код на elisp => никаких ограничений, только воображение.

Мое хилое воображение за 15минут экспериментов сотворило такое:

'(auto-insert-alist (quote ((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header") (upcase (concat "_" (file-name-sans-extension (file-name-nondirectory buffer-file-name)) "_H_")) "#ifndef " str n "#define " str "
" _ "
/*
* This file is part of $project_name$
*
* Authors:
* - Nakonechny Pavel
* - ...
*
* Description:
*
*
*/


#endif") (("\\.\\([Cc]\\|cc\\|cpp\\)\\'" . "C / C++ program") nil "#include \"" (let ((stem (file-name-sans-extension buffer-file-name))) (cond ((file-exists-p (concat stem ".h")) (file-name-nondirectory (concat stem ".h"))) ((file-exists-p (concat stem ".hh")) (file-name-nondirectory (concat stem ".hh"))))) & 34 | -10) (("[Mm]akefile\\'" . "Makefile") . "makefile.inc") (html-mode lambda nil (sgml-tag "html")) (plain-tex-mode . "tex-insert.tex") (bibtex-mode . "tex-insert.tex") (latex-mode "options, RET: " "\\documentclass[" str & 93 | -1 123 (read-string "class: ") "}
" ("package, %s: " "\\usepackage[" (read-string "options, RET: ") & 93 | -1 123 str "}
") _ "
\\begin{document}
" _ "
\\end{document}") (("/bin/.*[^/]\\'" . "Shell-Script mode magic number") lambda nil (if (eq major-mode default-major-mode) (sh-mode))) (ada-mode . ada-header) (("\\.[1-9]\\'" . "Man page skeleton") "Short description: " ".\\\" Copyright (C), " (substring (current-time-string) -4) " " (getenv "ORGANIZATION") | (progn user-full-name) "
.\\\" You may distribute this file under the terms of the GNU Free
.\\\" Documentation License.
.TH " (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) " " (file-name-extension (buffer-file-name)) " " (format-time-string "%Y-%m-%d ") "
.SH NAME
" (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) " \\- " str "
.SH SYNOPSIS
.B " (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) "
" _ "
.SH DESCRIPTION
.SH OPTIONS
.SH FILES
.SH \"SEE ALSO\"
.SH BUGS
.SH AUTHOR
" (user-full-name) (quote (if (search-backward "&" (line-beginning-position) t) (replace-match (capitalize (user-login-name)) t t))) (quote (end-of-line 1)) " <" (progn user-mail-address) ">
") (("\\.el\\'" . "Emacs Lisp header") "Short description: " ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str "

;; Copyright (C) " (substring (current-time-string) -4) " " (getenv "ORGANIZATION") | (progn user-full-name) "

;; Author: " (user-full-name) (quote (if (search-backward "&" (line-beginning-position) t) (replace-match (capitalize (user-login-name)) t t))) (quote (end-of-line 1)) " <" (progn user-mail-address) ">
;; Keywords: " (quote (require (quote finder))) (quote (setq v1 (mapcar (lambda (x) (list (symbol-name (car x)))) finder-known-keywords) v2 (mapconcat (lambda (x) (format "%12s: %s" (car x) (cdr x))) finder-known-keywords "
"))) ((let ((minibuffer-help-form v2)) (completing-read "Keyword, C-h: " v1 nil t)) str ", ") & -2 "

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; " _ "

;;; Code:



(provide '" (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) ")
;;; " (file-name-nondirectory (buffer-file-name)) " ends here
"))))


Мои строки там только первые, остальное - умолчание, откуда можно нарыть много идей. Заметьте, что если мы выполним auto-insert в теле *.[c,cpp] файла, то получим на его первой строке: #include "<имя файла>.h". Супер. )

Вы скажете, что у вас в любимой IDE все уже давно есть? Ага, я вам поверю, но все же приятно что то своими руками сотворить..

Комментариев нет: