在GNU Emacs中,我想在当前选定的文本上运行一个程序figlet.然后我想评论生产的区域.
我已经弄清楚如何使用标准的Emacs命令来做到这一点:
但是,我没有弄清楚如何编写Emacs lisp程序来完成所有这些工作.这是我的尝试:
(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)
Run Code Online (Sandbox Code Playgroud)
然后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)))
Run Code Online (Sandbox Code Playgroud)
(这是基于Trey Jackson的答案.)
;; _ _ _
;; | |_| |__ __ _ _ __ | | _____
;; | __| '_ \ / _` | '_ \| |/ / __|
;; | |_| | | | (_| | | | | <\__ \
;; \__|_| |_|\__,_|_| |_|_|\_\___/
Run Code Online (Sandbox Code Playgroud)
# _ _ _
# | |_| |__ __ _ _ __ | | _____
# | __| '_ \ / _` | '_ \| |/ / __|
# | |_| | | | (_| | | | | <\__ \
# \__|_| |_|\__,_|_| |_|_|\_\___/
Run Code Online (Sandbox Code Playgroud)
Tre*_*son 23
我不确定你想要通过推动和弹出标记来完成什么,我相信你会通过这样做获得相同的功能:
(defun figlet-region (&optional b e)
(interactive "r")
(shell-command-on-region b e "figlet")
(comment-region b e))
Run Code Online (Sandbox Code Playgroud)
交互式参数告诉Emacs将区域(点和标记)作为命令的前两个参数传递.
使用像shell-command-on-region
lisp程序中的交互式命令不是一个好主意.您应该使用call-process-region
:
(defun figlet-region (&optional b e)
(interactive "r")
(call-process-region b e "figlet" t t)
(comment-region (mark) (point)))
Run Code Online (Sandbox Code Playgroud)
它应该对各种用户选项更具弹性.
好吧,我不确定垃圾从哪里来,但是错误本身是从哪里来的shell-command-region
。在中使用时elisp
,它至少需要3个参数,START
END
和COMMAND
。
而且,通常,在功能中弄乱标记是不好的做法。这是“推”标记文档必须对此主题说的:
新手Emacs Lisp程序员经常尝试将标记用于错误的目的。有关更多信息,请参见“ set-mark”的文档。