如何应用新的 Emacs C 样式来重新格式化所有源文件?

发布于 2024-07-16 13:50:33 字数 348 浏览 4 评论 0原文

我想使用 emacs 的 Google 格式化功能重新格式化所有源文件: google-c-style.el(请参阅 此处)。

如何立即将此功能应用于我的所有源文件,以便它们都按照 Google 风格正确格式化和缩进?

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 source files at once, so that they are all formatted and indented correctly according to the Google style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁许谁一生繁华 2024-07-23 13:50:33

这有几个部分:

  • 您需要提出 EMACS 函数来完成您想要的所有重新格式化。 indent-region 是一个开始,但您可能还想取消制表符或其他一些操作。
  • 您需要在每个文件上调用它们,并且由于缩进函数在范围内工作,因此您需要一个设置标记以覆盖整个文件的函数:mark-whole-buffer
  • 您需要对每个文件调用 EMACS:这意味着使用 --batch 文件调用 emacs。

这里有几篇关于执行此操作的不错的博客文章此处

There are several pieces to this:

  • you need to come up with EMACS functions to do all the reformatting you want. indent-region is a start, but you might also want to untabify or some other things.
  • you need to invoke them on each file, and since the indent functions work on ranges, you need a function that sets mark to cover the whole file: mark-whole-buffer.
  • you need to invoke EMACS on each file: this means invoking emacs with the --batch file.

There's a couple of nice blog posts on doing this here and here.

指尖凝香 2024-07-23 13:50:33

我之前已经通过使用键盘定义的宏完成了此操作。 我会将所有文件加载到 emacs 中(例如 find . -name "*.cpp" | xargs emacs),然后键入以下键。 我已经注释了每个组合键的作用。

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

现在,您可以通过键入 Cx e 在缓冲区上运行它。 如果您已加载多个文件,则可以运行 Cu 100 Cx e 之类的命令来对 100 个文件运行此操作。 如果这超过了文件数量,没关系,您只会收到一些“铃声”或其他错误,一旦所有处理完成,您就可以忽略。

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.

清风疏影 2024-07-23 13:50:33

我相信这个脚本不会重新格式化。 相反,它是如何构建自定义“样式”的示例,如下所述: CC 模式手册 - 样式

CC 模式手册还说:

如果您想重新格式化旧代码,您可能最好使用其他工具,例如 GNU indent,它具有比 CC 模式更强大的重新格式化功能。

CC 模式手册 - 限制和已知错误< /a>

I believe that this script does not do reformatting. Instead it's an example of how to build a custom "style" as described in: CC mode manual - Styles

CC-mode manual also says:

If you want to reformat old code, you're probably better off using some other tool instead, e.g. GNU indent, which has more powerful reformatting capabilities than CC Mode.

CC mode manual - Limitations-and-Known-Bugs

你好,陌生人 2024-07-23 13:50:33

如果您想在 Dired 缓冲区中标记源文件,然后运行一个函数来格式化每个文件,您可以执行以下操作:

(defun clean-file(filename)
  (your-function-goes-here))

(defun clean-each-dired-marked-file() 
  (interactive)
  (for-each-dired-marked-file 'clean-file))

(defun for-each-dired-marked-file(fn)
 "Do stuff for each marked file, only works in dired window"
 (interactive)
 (if (eq major-mode 'dired-mode)
     (let ((filenames (dired-get-marked-files)))
       (mapcar fn filenames))
   (error (format "Not a Dired buffer \(%s\)" major-mode))))

If you want to mark the source files in a dired buffer and then run a function to format each you can do something like this:

(defun clean-file(filename)
  (your-function-goes-here))

(defun clean-each-dired-marked-file() 
  (interactive)
  (for-each-dired-marked-file 'clean-file))

(defun for-each-dired-marked-file(fn)
 "Do stuff for each marked file, only works in dired window"
 (interactive)
 (if (eq major-mode 'dired-mode)
     (let ((filenames (dired-get-marked-files)))
       (mapcar fn filenames))
   (error (format "Not a Dired buffer \(%s\)" major-mode))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文