Switching between auto-inserts on the fly / by context
Using emacs for multiple projects from legacy to current ones, I have to use multiple conventions, e.g. for comments or file headers. For example, I use this function to insert file headers for C++ files:
(defun mg-c-file-header()
"Inserts a c/c++ file header"
(if (boundp 'mg-auto-insert-style)
(case mg-auto-insert-style
(project-a
(insert
" * @file "(file-name-nondirectory buffer-file-name)"\n"
" * \n"
" * (c) 2000 - "( format-time-string "%Y" )" by someone\n"))
(project-b
(insert "/** another header*/\n"))
(otherwise (message "Meh.")))))
I call this function in this context:
(setq auto-insert-alist
'(
(("\\.\\([C]\\|cc\\|cpp\\)\\'" . "C++ Program")
nil
开发者_开发知识库 ( mg-c-file-header )
"// --- includes --- \n"
;; and do on....
I can set mg-auto-insert-style with M-x set-variable, that's ok. But I would like to automatically set the variable depending on the path of the file for the current buffer. So if I open a buffer or visit a new file in ~/projects/project-a, mg-auto-insert-style should be project-a.
This would work, but not perfectly:
(ede-cpp-root-project "project-a"
:file "~/projects/project-a/trunk/src/Makefile"
:local-variables (list
(cons 'mg-auto-insert-style 'project-a )))
If I create a new file, mg-auto-insert-style is not bound when the auto-insertion is done. Later, there's no problem...
Do you have an idea?
cu, Markus
I use auto-insert package, together with different templates to implement this. See my config as example (+ templates)
See library header2.el
and this Emacs wiki page: Automatic File Headers. You can configure the headers as you like.
精彩评论