Auto formatting source file in emacs

How to apply a set of formatting rules to an existing source file in emacs?

In particular, I have a build file ( *.s ), but I need a generic command for all file types.

I am trying to use the Mx c-set-style style Mx c-set-style with gnu , but I get an error:

Buffer * .s is not a CC mode buffer (c-set-style)

+41
formatting emacs
Jun 14 '09 at 11:49
source share
6 answers

Open the file, and then backtrack it, holding the entire area:

 Mx find-file /path/to/file RET Cx h (Mx mark-whole-buffer) CM-\ (Mx indent-region) 

Now it looks like you are trying to indent C into a buffer that is not in C mode. To switch to C mode

 Mx c-mode 

Or c++-mode or any other mode. But, since this is assembler code, you probably need assembly mode (which Emacs will do by default for .s files). In this case, the indent command above ( CM-\ also known as Mx indent-region ) should work for you.

Note: the sequence of commands from above can be folded into one command as follows:

 (defun indent-file (file) "prompt for a file and indent it according to its major mode" (interactive "fWhich file do you want to indent: ") (find-file file) ;; uncomment the next line to force the buffer into a c-mode ;; (c-mode) (indent-region (point-min) (point-max))) 

And if you want to learn how to associate basic modes with files based on extensions, see the documentation for auto-mode-alist . In fairness, this is not necessarily an extension based, just regular expressions matching the file name.

+99
Jun 14 '09 at 13:43
source share

Try Mx asm-mode . This will switch to assembler mode. You do not know how this will happen with the assembler embedded in the middle of the C file.

+6
Jun 14 '09 at 12:49
source share

if you want the current indent buffer

 (defun iwb () "indent whole buffer" (interactive) (delete-trailing-whitespace) (indent-region (point-min) (point-max) nil) (untabify (point-min) (point-max))) 
+3
Mar 13 '15 at 9:11
source share

emacs will use the file name extension to identify the mode, you should add some layout style style to your custom.el file

+1
Jun 14 '09 at 12:25
source share

The main mode that it uses for your .s files will not be cc-mode, so c-set-style does not make sense. However, you can always manually enter cc-mode (Mx cc-mode), and then execute the required c-set style. However, since C styles are used for C source code and not for assembler, this is almost not what you want to do.

0
Jun 15 '09 at 8:04
source share

if you want to backtrack from the command line use:

 emacs --batch <filenames.v> -f verilog-batch-indent 
0
03 Mar. '16 at 8:37
source share



All Articles