i'm trying to indent a couple of C++-files (and untabify them; and remove trailing whitespace), so i thought about using emacs in batch mode.
Since I'm pretty much a noob when it comes to elpa (or lisp in general), i searched the web and came up with something like this:
$ cat myscript.el (defun myindent () "Format the whole buffer." (c-set-style "stroustrup") (indent-region (point-min) (point-max) nil) (untabify (point-min) (point-max)) (delete-trailing-whitespace) (save-buffer) ) $ emacs -batch "somefile.cpp" -l $(pwd)/myscript.el -f myindent This kind of works.
However, all of my header files contain a boilerplate in a C-style multiline comment, which is nicely formatted. Something like:
/*----------------------------------------------------------------- LOG Foo Bar - the Pizza man Copyright (c) 1997-2017 Me, You and others For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file "LICENSE.txt". CLASS Onion smells and bits like an onion KEYWORDS ingredients, vegetable DESCRIPTION hum, hmm...later -----------------------------------------------------------------*/ now when i run my scrip on that file, all the pre-formatting is lost, and it ends up as:
/*----------------------------------------------------------------- LOG Foo Bar - the Pizza man Copyright (c) 1997-2017 Me, You and others For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file "LICENSE.txt". CLASS Onion smells and bits like an onion KEYWORDS ingredients, vegetable DESCRIPTION hum, hmm...later -----------------------------------------------------------------*/ So the question is: Is there a way to indent my files in batch mode and preserve the formatting within multiline comments (and strings)?
I've tried using (indent-code-rigidly (point-min) (point-max) 0) instead of indent-region which leaves the comments intact, but I preservers too much of the original indentation of the code. So I guess I'd like something inbetween indent-region and indent-code-rigidly)
indent-code-rigidlysimply inserts aTABor equivalent amount ofSPCbefore the column where code originally started). What if in your original attempt you replaceindent-regionwithc-indent-region?indent-regionbasically callsc-indent-lineon each line (with a minor improvement in that it aligns backslashes in macro definitions). Perhaps, it would be possible to write an alternative indent-region-like function which skips lines in comments. (I'm not very familiar withc-mode, so, maybe this was already done somewhere).