I am working on a project where some people use vi, some use emacs and some others (including gedit). The most simple yet global way (although not perfect) to enforce (at least visual) style was to add the following lines to the end of each file:
...
return 0;
}
// Editor modelines - generated by http://www.wireshark.org/tools/modelines.html
// Local variables:
// c-basic-offset: 4
// ta开发者_运维百科b-width: 4
// indent-tabs-mode: t
// truncate-lines: 1
// End:
// vim:set ft=cpp ts=4 sw=4 sts=4 nowrap: cindent:
the question is: how can I convert the emacs portion in a "one-line" code (as vim can)? and yet keep it at the end of the source file (not at the top).
(Probably this can be recasted as Lisp question but I am not familiar with it)
Directory Local Variables are probably a better approach.
- http://www.emacswiki.org/emacs/DirectoryVariables
- http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html
The single .dir-locals.el
file will be processed by everyone, and no need for file local variables at all.
vim may well have a similar mechanism?
You can use the eval:
declaration, but Emacs will ask you to confirm that it is safe to evaluate. If you tell Emacs to accept it permanently, it won't ask about that expression again (it stores it in safe-local-variable-values
in the custom-set-variables section of your init file).
;;; Local Variables:
;;; eval:(setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1)
;;; End:
You can wrap multiple expressions in progn
:
;;; Local Variables:
;;; eval:(progn (setq c-basic-offset 4) (message "hello"))
;;; End:
Or use any other constructs (I don't think there are any restrictions).
You would need to use the // Local Variables:
and // End:
. The rest can be made into one line as in // eval: (setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1)
.
Looks like you might be SOL:
Specifying File Variables: http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html
Variables in Emacs: http://www.gnu.org/software/emacs/manual/html_node/emacs/Variables.html#Variables
Unless you use Phils's eval style.
(setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1)
add this to your .emacs. If you wish to do this for specific file types you can always use the add-hook call, such as (add-hook 'c-mode-common-hook 'your-func-here)
where your-func-here could be a function that simply sets those variables.
Refer to phils method of using Local Variables and eval to accomplish this with the first line of code given above. Sorry I didn't quite understand that this was only for a single file or very few files.
I'm going to turn the question around? Do you really need any file-local variables? If you use the same style throughout all source files, it might be better to define a C indentation setup and distribute this among all developers.
(defconst my-c-style
'((c-basic-offset . 2)
(c-offsets-alist
. ((substatement-open . 0)
(statement-case-open . +)
(inline-open . 0)
(arglist-cont-nonempty . (c-indent-operator-lineup-arglist-operators
c-lineup-arglist)))))
"My indentation style")
(defun my-c-mode-common-hook ()
(interactive)
(c-add-style "my" my-c-style t))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
精彩评论