Emacs Lisp的"shell-on-region"命令 [英] Emacs lisp "shell-command-on-region"
问题描述
在GNU Emacs中,我要在当前选定的文本上运行一个程序figlet.然后,我想评论产生的区域.
In GNU Emacs, I want to run a program, figlet, on the currently selected text. I then want to comment the region which is produced.
我已经弄清楚了如何使用标准的Emacs命令来做到这一点:
I have figured out how to do it using the standard Emacs commands:
- 使用C-< space>设置标记在单词的开头
- 将光标移到单词的末尾
- C-u M-x区域上的shell命令RET figlet RET
- M-x评论区域RET
但是,我没有弄清楚如何编写一个Emacs lisp程序来完成所有这些工作.这是我的尝试:
However, I have failed to work out how to write an Emacs lisp program to do all this. Here is my attempt:
(defun figlet-region ()
(interactive)
(push-mark)
(shell-command-on-region "figlet")
(comment-region (mark) (point))
(pop-mark)
)
(global-set-key "\C-c\C-f" 'figlet-region)
然后C-<space>; M-x figlet-region
产生垃圾:
figlet-region: Wrong number of arguments: #[(start end command &optional output-buffer replace error-buffer display-error-buffer) "ÆÇÈ
\"!É
'jÊ!j;j
0Wb
?Ë`Ì\"Í ÎQÎDRÎÉ!\"&
ffÏ )ãÐqÑ!#Ò#p=¬É$]d|e^|Íed Î ÎD¡ÎÉ!\"&â%qÉ$Á&%Ó *Í ÉØ#DÚ#É!\"&*#Ô!#ÕÖ×!8WrÐ!qd`Z'o ØcÙÉ\"d'Zb)(Úp!)Û!*" [error-buffer small-temporary-file-directory temporary-file-directory exit-status error-file replace make-temp-file expand-file-name "scor" nil ...] 9 1945557 (let (string) (unless (mark) (error "The mark is not set now, so there is no region")) (setq string (read-from-minibuffer "Shell command on region: " nil nil nil (quote shell-command-history))) (list (region-beginning) (region-end) string current-prefix-arg current-prefix-arg shell-command-default-error-buffer t))], 1
答案
(defun figlet-region (&optional b e)
(interactive "r")
(shell-command-on-region b e "figlet" (current-buffer) t)
(comment-region (mark) (point)))
(这基于Trey Jackson的回答.)
(This is based on Trey Jackson's answer.)
;; _ _ _
;; | |_| |__ __ _ _ __ | | _____
;; | __| '_ \ / _` | '_ \| |/ / __|
;; | |_| | | | (_| | | | | <\__ \
;; \__|_| |_|\__,_|_| |_|_|\_\___/
示例(CPerl模式)
# _ _ _
# | |_| |__ __ _ _ __ | | _____
# | __| '_ \ / _` | '_ \| |/ / __|
# | |_| | | | (_| | | | | <\__ \
# \__|_| |_|\__,_|_| |_|_|\_\___/
推荐答案
我不确定您要通过推送和弹出标记来完成什么工作,我相信通过这样做,您将获得相同的功能:
I'm unsure what you're trying to accomplish with the pushing and popping of the marks, I believe you'd get the same functionality by doing this:
(defun figlet-region (&optional b e)
(interactive "r")
(shell-command-on-region b e "figlet")
(comment-region b e))
interactive的参数告诉Emacs将区域(点和标记)作为命令的前两个参数传递.
The argument to interactive tells Emacs to pass the region (point and mark) in as the first two arguments to the command.
这篇关于Emacs Lisp的"shell-on-region"命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!