使用 Emacs 漂亮地打印 ActionScript 3.0

发布于 2024-09-30 11:55:20 字数 777 浏览 8 评论 0原文

我一直在尝试在批处理模式下使用 emacs 来漂亮地打印 ActionScript 文件,但它并没有完全达到我的预期。 我从这里下载了actionscript-mode.el和as-config.el http://blog.pettomato.com /?p=12 并将它们放入我的主目录,将 actionscript-mode.el 复制到我的工作目录中。 然后我也在我的主目录中创建了一个文件“.emacs”,其中包含以下内容:

(load "~/actionscript-mode.el")
(actionscript-mode)
(indent-region 0 (buffer-size)  nil)
(save-buffer)

然后我转到格式不正确的 .as 文件的目录并在命令行上运行此命令:

emacs -batch WrongFormat.as -l ~/.emacs -l actionscript-mode.el

And I get:

Loading /Users/pnm/actionscript-mode.el (source)...
Indenting region...
Indenting region... done
(No changes need to be saved)

现在的问题是:

如何获取emacs 重新格式化大括号而不是换行? 我越来越绝望了,我不确定用木棍敲击 50 次是否是执行代码风格指南的良好替代方法。

I've been trying to use emacs in batch mode to pretty-print an ActionScript file but it doesn't quite do what I'd expect it to.
I downloaded actionscript-mode.el and as-config.el from here http://blog.pettomato.com/?p=12
and put them into my home directory, duplicating actionscript-mode.el into my working dir.
Then I made a file ".emacs" also in my home directory with the following contents:

(load "~/actionscript-mode.el")
(actionscript-mode)
(indent-region 0 (buffer-size)  nil)
(save-buffer)

Then I go to the directory with the incorrectly formatted .as file and run this command on the command line:

emacs -batch WrongFormat.as -l ~/.emacs -l actionscript-mode.el

And I get:

Loading /Users/pnm/actionscript-mode.el (source)...
Indenting region...
Indenting region... done
(No changes need to be saved)

Now the question is:

How do I get emacs to reformat curly braces not to be on a new line?
I'm getting desperate and I'm not sure 50 hits with a wooden stick is a good alternative means to enforce the code style guide.

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

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

发布评论

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

评论(1

第几種人 2024-10-07 11:55:20

你在这里遇到了几个问题。我会先处理小问题。

  1. .emacs 不适合放置批处理命令。 .emacs 文件是您的个人配置文件,每当您以交互方式启动 Emacs 时都会运行该文件。您不希望批处理命令以交互方式执行。

  2. 当使用 --batch 执行 Emacs 时,“动作参数"(如-l)按从左到右的顺序计算。因此,在加载并保存文件后再执行 -l actionscript-mode.el 为时已晚。

  3. 更好的 Emacs 编码风格是调用 (point-min) 来获取缓冲区(可见部分)的开头,并调用 (point-max)获取缓冲区的末尾。 (在这种情况下没什么大不了的,但把事情做好是件好事。)

所以你的 shell 脚本应该如下所示。 (为了最大程度地清晰起见,我已经完整地写出了所有选项。)

emacs --batch \
      --load=~/actionscript-mode.el \
      --visit=FILE \
      --funcall=actionscript-mode \
      --execute='(indent-region (point-min) (point-max))' \
      --funcall=save-buffer

现在,讨论主要问题。您想要移动左大括号,使它们出现在行尾,而不是行首。命令 indent-region 不会为您执行此操作 - 它只会添加或删除您指定的行的缩进。因此,对于您的应用程序,您实际上需要编辑该文件。例如,您可以将此参数添加到批处理脚本中(在调用 actionscript-mode 之后和调用 indent-region 之前):

      --execute='(replace-regexp " *\n *{" " {")' 

这有点粗糙,并且可能会错误地移动一些大括号(例如字符串内的大括号),但它可能足以满足您的应用程序。如果还有问题,请再次询问。

You are having several problems here. I will deal with the minor problems first.

  1. .emacs is a bad place to put your batch commands. The .emacs file is your personal configuration file, which gets run whenever you launch Emacs interactively. You don't want your batch commands to execute interactively.

  2. When Emacs is executed with --batch, the "action arguments" (like -l) are evaluated in order from left to right. So it is too late to do -l actionscript-mode.el after you have already loaded and saved the file.

  3. It is better Emacs coding style to call (point-min) to get the beginning of the (visible portion of the) buffer and (point-max) to get the end of the buffer. (Not a big deal in this case, but it's nice to get things right.)

So your shell script should look something like the following. (I've written out all the options in full for maximum clarity.)

emacs --batch \
      --load=~/actionscript-mode.el \
      --visit=FILE \
      --funcall=actionscript-mode \
      --execute='(indent-region (point-min) (point-max))' \
      --funcall=save-buffer

Now, on to the main problem. You want to move opening braces so they appear at the end of lines, not at the start of lines. The command indent-region won't do this for you—it only add or removes indentation to the lines that you give it. So for your application, you actually need to edit the file. For example, you might add this argument to your batch script (after the call to actionscript-mode and before the call to indent-region):

      --execute='(replace-regexp " *\n *{" " {")' 

This is a bit crude, and might move some braces incorrectly (e.g. braces inside strings), but it might be good enough for your application. Ask again if you still have trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文