Emacs lisp “shell-command-on-region”

后端 未结 3 1718
孤独总比滥情好
孤独总比滥情好 2021-02-04 01:25

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.

I have figured out how to do it using

相关标签:
3条回答
  • 2021-02-04 01:52

    Well, I'm not sure where the garbage is coming from, but the error itself is coming from shell-command-region. When used in elisp, it expects at least 3 arguments, START END and COMMAND.

    Also, in general, it is bad practice to mess with the mark in functions. Here is what the doc of push-mark has to say on the subject:

    Novice Emacs Lisp programmers often try to use the mark for the wrong purposes. See the documentation of `set-mark' for more information.

    0 讨论(0)
  • 2021-02-04 02:08

    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))
    

    The argument to interactive tells Emacs to pass the region (point and mark) in as the first two arguments to the command.

    0 讨论(0)
  • 2021-02-04 02:11

    It is not a very good idea to use an interactive command like shell-command-on-region in a lisp program. You should use call-process-region instead:

    (defun figlet-region (&optional b e) 
      (interactive "r")
      (call-process-region b e "figlet" t t)
      (comment-region (mark) (point)))
    

    It should be more resilient against various user options.

    0 讨论(0)
提交回复
热议问题