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

4

7

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?

Chartres answered 4/4, 2009 at 14:22 Comment(0)
E
9

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.

Educative answered 4/4, 2009 at 14:46 Comment(2)
Unfortunately, the links to the blog posts are now dead.Heroine
Blog posts are broken again. The Internet Archive caught one redirecting to a different domain which works. Current links are here and here.Elegancy
B
3

I have done this before by using a keyboard defined macro. I would load all of the files into emacs (something like find . -name "*.cpp" | xargs emacs) and then type the following keys. I've annotated each key combination with what it does.

C-x-(                  'Begin recording macro
M-<                    'Go to start of file
C-space                'Mark current location (now start of file)
M->                    'Go to end of file
M-x indent-region      'Indent entire file according to coding style
C-x C-s                'Save the current buffer
C-x C-k                'Close the current buffer
C-x-)                  'End recording macro

Now you can run this on a buffer by typing C-x e. If you have loaded several files you can run something like C-u 100 C-x e to run this on 100 files. If this is more than the number of files, that is ok, you'll just get some "bell ring" or other error you can ignore once all the processing is complete.

Became answered 6/4, 2009 at 18:24 Comment(0)
B
2

I believe that this script does not do reformatting. Instead it's an example of how to build a custom "style" as described in: CC mode manual - Styles

CC-mode manual also says:

If you want to reformat old code, you're probably better off using some other tool instead, e.g. GNU indent, which has more powerful reformatting capabilities than CC Mode.

CC mode manual - Limitations-and-Known-Bugs

Bristling answered 4/4, 2009 at 14:36 Comment(4)
Yeah, so I want to load the style and apply the indent-buffer function. And then do that with all files.Chartres
Reindent, yes. Reformat, no. Newlines before/after braces for example do not get changed.Bristling
My suggestion is that you use indent instead of emacs.Bristling
But with indent, I can't use the emacs style file that defines all the formatting that I need ...?Chartres
B
0

If you want to mark the source files in a dired buffer and then run a function to format each you can do something like this:

(defun clean-file(filename)
  (your-function-goes-here))

(defun clean-each-dired-marked-file() 
  (interactive)
  (for-each-dired-marked-file 'clean-file))

(defun for-each-dired-marked-file(fn)
 "Do stuff for each marked file, only works in dired window"
 (interactive)
 (if (eq major-mode 'dired-mode)
     (let ((filenames (dired-get-marked-files)))
       (mapcar fn filenames))
   (error (format "Not a Dired buffer \(%s\)" major-mode))))
Barcot answered 15/4, 2009 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.