Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement 'shell-command-on-region-to-string' emacs/elisp function?

Tags:

emacs

elisp

I need to get a range (or whole range) of current buffer, run program to process the range as an input, get the result and to put the string at the end of the current buffer.

I learned that shell-command-on-region can process a region.

I learned that shell-command-to-string can store the result of the program to a string.

Then, how can I implement 'shell-command-on-region-to-string'?

(defun shell-command-on-region-to-string (process &optional b e) 
  (interactive)
  (let ((b (if mark-active (min (point) (mark)) (point-min)))
        (e (if mark-active (max (point) (mark)) (point-max))))

  ?? (shell-command-on-region b e process (current-buffer) t)
  ?? how to store the return of a process to a string
  ?? how to insert the result at the end of current buffer
))
like image 334
prosseek Avatar asked Aug 26 '10 14:08

prosseek


People also ask

How do I run a script in Emacs?

The Basics. The simplest way to invoke something is to run the command shell-command , bound to the handy shortcut M-! . Emacs will try to display the output of the command in the echo area if it is not too large; otherwise, it will be sent to the *Shell Command Output* buffer.

How do I run a shell command in Emacs?

You can execute an external shell command from within Emacs using ` M-! ' ( 'shell-command' ). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size.


1 Answers

(defun shell-command-on-region-to-string (start end command)
  (with-output-to-string
    (shell-command-on-region start end command standard-output)))

(defun shell-command-on-region-with-output-to-end-of-buffer (start end command)
  (interactive
   (let ((command (read-shell-command "Shell command on region: ")))
     (if (use-region-p)
         (list (region-beginning) (region-end) command)
       (list (point-min) (point-max) command))))
  (save-excursion
    (goto-char (point-max))
    (insert (shell-command-on-region-to-string start end command))))
like image 85
huaiyuan Avatar answered Oct 11 '22 02:10

huaiyuan