2010-08-26 20 views
4

我需要獲取當前緩衝區的範圍(或整個範圍),運行程序將範圍處理爲輸入,獲取結果並將字符串放在當前緩衝區的末尾。如何實現'shell-command-on-region-to-string'emacs/elisp函數?

我瞭解到shell-command-on-region可以處理一個區域。

我瞭解到shell-command-to-string可以將程序的結果存儲到字符串中。

然後,我該如何實現'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 
)) 

回答

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