Auto-formatting a source file in emacs

前端 未结 6 779
旧巷少年郎
旧巷少年郎 2021-01-29 19:47

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

Specifically I have an assembly (*.s) file, but I would like a generic command

相关标签:
6条回答
  • 2021-01-29 20:26

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

    0 讨论(0)
  • 2021-01-29 20:27

    The major mode it's using for your .s files won't be cc-mode hence c-set-style makes no sense. However you can always manually enter cc-mode (M-x cc-mode) and then do the c-set-style you want. However as the C styles are keyed for C source code and not assembler this is almost certainly not what you want to do.

    0 讨论(0)
  • 2021-01-29 20:38

    Try M-x asm-mode. That will switch to assembler mode. Not sure how it will go with assembler embedded in the middle of a C file.

    0 讨论(0)
  • 2021-01-29 20:40

    if you want to indent from the command line use :

    emacs --batch  <filenames.v>  -f verilog-batch-indent
    
    0 讨论(0)
  • 2021-01-29 20:47

    Open the file and then indent it by indenting the entire region:

    M-x find-file /path/to/file RET
    C-x h                             (M-x mark-whole-buffer)
    C-M-\                             (M-x indent-region)
    

    Now, it looks like you're trying to apply C indentation to a buffer that's not in C mode. To get it into C mode

    M-x c-mode
    

    Or c++-mode, or whatever mode you want. But, since it's assembler code, you probably want assembler mode (which Emacs will do by default for .s files). In which case, the indentation command above (C-M-\ is also known as M-x indent-region) should work for you.

    Note: the command sequence at the top can be rolled into a single command like this:

    (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 major-modes with files based on extensions, check out the documentation for auto-mode-alist. To be fair, it's not necessarily extension based, just regular expressions matched against the filename.

    0 讨论(0)
  • 2021-01-29 20:47

    if you want indent current buffer

    (defun iwb ()
      "indent whole buffer"
      (interactive)
      (delete-trailing-whitespace)
      (indent-region (point-min) (point-max) nil)
      (untabify (point-min) (point-max)))
    
    0 讨论(0)
提交回复
热议问题