- 2007-01-30 (Tue) 12:32
- ソフトウェア ( Meadow/Emacs )
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-! で使えるようにしてみた.元のリージョンを上書きせずにコメントしてくれるほうが使いやすいかも…
- Newer: ことはじめ
Comments:2
- 夜更け 2009-04-26 (Sun) 15:22
-
Lisp よくわからないんですけど、
(defun filter-region (command)
(interactive "s| ")
(save-excursion
(shell-command-on-region (point) (mark) command nil t)))
(define-key ctl-x-map "|" 'filter-region)
じゃ駄目なの? - emoken 2009-04-26 (Sun) 18:48
-
駄目じゃないです.でも何故そうしなかったかの理由を思い出せない…