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

前端 未结 4 2169
心在旅途
心在旅途 2020-12-31 21:08

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 sour

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 21:41

    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.

提交回复
热议问题