如何将一组格式规则应用于 emacs 中的现有源文件?
具体来说,我有一个程序集 (*.s
) 文件,但我想要一个适用于所有类型文件的通用命令。
我正在尝试将 M-x c-set-style
与 gnu
样式一起使用,但出现错误:
Buffer *.s is not a CC Mode buffer (c-set-style)
最佳答案
打开文件,然后通过缩进整个区域来缩进它:
M-x find-file /path/to/file RET
C-x h (M-x mark-whole-buffer)
C-M-\ (M-x indent-region)
现在,您似乎正在尝试将 C 缩进应用到不在 C 模式下的缓冲区。让它进入C模式
M-x c-mode
或c++-mode
,或任何你想要的模式。但是,由于它是汇编代码,您可能需要汇编模式(默认情况下,Emacs 将为 .s 文件执行该模式)。在这种情况下,上面的缩进命令(C-M-\
也称为 M-x indent-region
)应该适合你。
注意:顶部的命令序列可以像这样滚动成单个命令:
(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)))
而且,如果您想了解如何将主要模式与基于扩展名的文件相关联,请查看 documentation for auto-mode-alist .公平地说,它不一定是基于扩展名的,只是与文件名匹配的正则表达式。
关于emacs - 在 emacs 中自动格式化源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/992685/