开发者

Use Emacs to pretty-print ActionScript 3.0

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 refo开发者_开发百科rmat 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜