[已解决]emacs格式化代码的问题

Vim、Emacs配置和使用
回复
hugo222
帖子: 9
注册时间: 2006-10-28 14:45
送出感谢: 0
接收感谢: 0

[已解决]emacs格式化代码的问题

#1

帖子 hugo222 » 2007-08-24 20:18

请教高手,在emacs中编辑时我想调用外部命令格式化代码
(defun formatCode ()
(interactive)
(shell-command
(concat "d:/emacs/bin/a.exe \"" buffer-file-name "\""))
)

可是每次都是在新开的 *Shell Command Output* buffer中打开并显示格式化后的结果,有没有办法直接替换正在编辑的buffer,并且不要打开新的buffer
上次由 hugo222 在 2007-08-26 14:15,总共编辑 1 次。
forcotton
帖子: 57
注册时间: 2006-10-06 21:10
送出感谢: 0
接收感谢: 0

#2

帖子 forcotton » 2007-08-25 3:47

C-h f shell-command RET
有output-buffer 参数

代码: 全选

shell-command is an interactive compiled Lisp function in `simple.el'.
It is bound to M-!, <menu-bar> <tools> <shell>.
(shell-command command &optional output-buffer error-buffer)

Execute string command in inferior shell; display output, if any.
With prefix argument, insert the command's output at point.

If command ends in ampersand, execute it asynchronously.
The output appears in the buffer `*Async Shell Command*'.
That buffer is in shell mode.

Otherwise, command is executed synchronously.  The output appears in
the buffer `*Shell Command Output*'.  If the output is short enough to
display in the echo area (which is determined by the variables
`resize-mini-windows' and `max-mini-window-height'), it is shown
there, but it is nonetheless available in buffer `*Shell Command
Output*' even though that buffer is not automatically displayed.

To specify a coding system for converting non-ASCII characters
in the shell command output, use C-x RET c
before this command.

Noninteractive callers can specify coding systems by binding
`coding-system-for-read' and `coding-system-for-write'.

The optional second argument output-buffer, if non-nil,
says to put the output in some other buffer.
If output-buffer is a buffer or buffer name, put the output there.
If output-buffer is not a buffer and not nil,
insert output in current buffer.  (This cannot be done asynchronously.)
In either case, the output is inserted after point (leaving mark after it).

If the command terminates without error, but generates output,
and you did not specify "insert it in the current buffer",
the output can be displayed in the echo area or in its buffer.
If the output is short enough to display in the echo area
(determined by the variable `max-mini-window-height' if
`resize-mini-windows' is non-nil), it is shown there.  Otherwise,
the buffer containing the output is displayed.

If there is output and an error, and you did not specify "insert it
in the current buffer", a message about the error goes at the end
of the output.

If there is no output, or if output is inserted in the current buffer,
then `*Shell Command Output*' is deleted.
hugo222
帖子: 9
注册时间: 2006-10-28 14:45
送出感谢: 0
接收感谢: 0

#3

帖子 hugo222 » 2007-08-25 12:49

谢谢二楼的,我试了一下,可能是对emacs实在不是太通,暂时只能将格式化后的代码插入到光标所在的位置,命令如下

(defun formatCode ()
(interactive)
(shell-command
(concat "d:/emacs/bin/a.exe \"" buffer-file-name "\"")
2 nil)
)
hugo222
帖子: 9
注册时间: 2006-10-28 14:45
送出感谢: 0
接收感谢: 0

#4

帖子 hugo222 » 2007-08-25 12:51

代码: 全选

The optional second argument output-buffer, if non-nil, 
says to put the output in some other buffer. 
If output-buffer is a buffer or buffer name, put the output there. 
If output-buffer is not a buffer and not nil, 
insert output in current buffer.  (This cannot be done asynchronously.) 
In either case, the output is inserted after point (leaving mark after it).
一个是在新buffer中打开,另一个是插入当前的buffer,我需要替换,有谁能再提示下
forcotton
帖子: 57
注册时间: 2006-10-06 21:10
送出感谢: 0
接收感谢: 0

#5

帖子 forcotton » 2007-08-25 22:02

呵呵,没有注意你的目的。
看看shell-command-on-region 命令。

M-| runs the command shell-command-on-region
which is an interactive compiled Lisp function in `simple.el'.
It is bound to M-|, <menu-bar> <tools> <shell-on-region>.
(shell-command-on-region start end command &optional output-buffer
replace error-buffer display-error-buffer)
hugo222
帖子: 9
注册时间: 2006-10-28 14:45
送出感谢: 0
接收感谢: 0

#6

帖子 hugo222 » 2007-08-25 23:59

哈哈,终于可以了,谢谢forcotton兄

代码: 全选

(defun formatCode ()
(interactive)
(shell-command-on-region (point-min) (point-max) 
(concat "d:/emacs/bin/a.exe \"" buffer-file-name "\"")
 nil t
)
) 

;; (region-beginning) (region-end) 只替换选择部分
;; (point-min) (point-max)   全部替换
回复

回到 “Vim和Emacs”