在emacs中自动格式化源文件 [英] Auto-formatting a source file in emacs

查看:221
本文介绍了在emacs中自动格式化源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一组格式规则应用于emacs中的现有源文件?



具体来说,我有一个程序集( *。 )文件,但我想要一个通用命令的所有类型的文件。



我正在尝试使用 Mx c-set-style gnu 样式,但我收到错误:



缓冲区* .s不是CC模式缓冲区(c-set-style)



< blockquote>

解决方案

打开文件,然后缩进整个区域:

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

现在,您正试图将C缩进应用于不处于C模式的缓冲区。要进入C模式

  Mx c-mode 

c ++ - mode ,或者你想要的任何模式。但是,由于它是汇编代码,您可能需要汇编器模式(默认情况下,Emacs将为.s文件执行)。在这种情况下,上面的缩进命令( CM-\ 也称为 Mx缩进区域)应该工作对于你。



注意:顶部的命令序列可以滚动成一个单一命令,如下所示:

 (defun indent-file(file)
提示文件并根据其主要模式缩进
(interactive)你想要缩进的文件:)
(查找文件文件)
;;取消注释下一行以强制缓冲区进入c模式
;;(c-mode)
(indent-region(point-min)(point-max)))

如果您想了解如何将主要模式与基于扩展名的文件相关联,请查看 auto-mode-alist的文档。为了公平起见,它不一定是扩展的,只是正则表达式与文件名匹配。


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 for all types of files.

I am trying to use M-x c-set-style with gnu style, but I am getting an error:

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

解决方案

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.

这篇关于在emacs中自动格式化源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆