Home > Archives > 2007年01月30日

2007年01月30日

リージョンを シェルコマンドの実行結果で置換

vi の ! コマンドに対応するコマンドを shell-command-on-region を簡略化して作ってみる.キモは call-process-region という組み込み関数.

;; replace specified region with output of 
;; a shell command against the content of the regon
(defun filter-command-region (start end command)
  (interactive 
   (let (string)
     (unless (mark)
       (error "The mark is not set now, so there is no region"))
     ;; Do this before calling region-beginning
     ;; and region-end, in case subprocess output
     ;; relocates them while we are in the minibuffer.
     (setq string (read-from-minibuffer "Shell command on region: "
					nil nil nil
					'shell-command-history))
     ;; call-interactively recognizes region-beginning and
     ;; region-end specially, leaving them in the history.
     (list (region-beginning) (region-end)  string)))
  (let (exit-status)
    ;; Replace specified region with output from command.
    (let ((swap (< start end)))
      (goto-char start)
      (push-mark (point) 'nomsg)
      (setq exit-status
	    ;; see describe-function
	    ;; error output is marged into standard output
	    (call-process-region start end shell-file-name t
				 (list t t)
				 nil shell-command-switch command))
      ;; swap the point and mark if necessary
      (and swap (exchange-point-and-mark)))
    exit-status))
(define-key global-map [(control ?!)] 'filter-command-region)

shell-command-on-region が M-! なので C-! で使えるようにしてみた.元のリージョンを上書きせずにコメントしてくれるほうが使いやすいかも…

リージョンを シェルコマンドの実行結果で置換2

ついでなので,元のリージョンの内容を出力の上部にコメントとして出力するものも作った.

(defun perform-command-region (start end command)
  (interactive 
   (let (string)
     (unless (mark)
       (error "The mark is not set now, so there is no region"))
     ;; Do this before calling region-beginning
     ;; and region-end, in case subprocess output
     ;; relocates them while we are in the minibuffer.
     (setq string (read-from-minibuffer "Shell command on region: "
					nil nil nil
					'shell-command-history))
     ;; call-interactively recognizes region-beginning and
     ;; region-end specially, leaving them in the history.
     (list (region-beginning) (region-end)  string)))
  (let (exit-status)
    ;; Replace specified region with output from command.
    (let ((swap (< start end)))
      (goto-char start)
      (kill-region start end)
      (yank)
      (push-mark (point) 'nomsg)
      (setq exit-status
	    ;; see describe-function
	    ;; error output is marged into standard output
	    (call-process-region start end shell-file-name t
				 (list t t)
				 nil shell-command-switch command))
      ;; swap the point and mark if necessary
      (and swap (exchange-point-and-mark))
      ;; mark current position
      (setq p1 (point-marker))
      (set-marker-insertion-type p1 nil)
      ;; yank the original contents
      (yank nil) 
      (setq p2 (point-marker)) 
      ;; comment the yanked contents
      (comment-region (marker-position p1) (marker-position p2))
      (setq p1 nil)
      (setq p1 nil))
    exit-status))
(define-key global-map [(control meta ?!)] 'perform-command-region)

そして C-M-! で発動すると.これって comment-region がない場合にどうなるんだろう?

Home > Archives > 2007年01月30日

Search
Feeds

Page Top