Search code examples
emacslispelispcode-formatting

How can I apply a new Emacs C style to reformat all my source files?


I'd like to re-format all my source files using the Google formatting function for emacs: google-c-style.el (see here).

How can I apply this function to all my source files at once, so that they are all formatted and indented correctly according to the Google style?


Solution

  • There are several pieces to this:

    • you need to come up with EMACS functions to do all the reformatting you want. indent-region is a start, but you might also want to untabify or some other things.
    • you need to invoke them on each file, and since the indent functions work on ranges, you need a function that sets mark to cover the whole file: mark-whole-buffer.
    • you need to invoke EMACS on each file: this means invoking emacs with the --batch file.

    There's a couple of nice blog posts on doing this here and here.