emacsからtimeout付きでprocessを呼び出す2
emacs側では以下の2つのようなプロセスを呼び出せばいいことに気づいた。
. | 動作 | その後 |
this-process | 渡されたコマンドを実行して | もう一方のprocessが終わってなかったら殺す。 |
that-process | sleepして | もう一方のprocessが終わってなかったら殺す。 |
;(require 'cl) (defvar spscwt-hooked-process nil) (defvar spscwt-sentinel-function nil) (defun spscwt-process-will-be-kill (that-process &optional this-buf) (lexical-let ((that that-process) (buf this-buf)) (lambda (&rest args) (when (eq 'run (process-status that)) (kill-process that)) (when (and buf (get-buffer buf)) (kill-buffer buf)) (when spscwt-sentinel-function (funcall spscwt-sentinel-function))))) (defun spscwt-set-mutual-dependent-process (this-process that-process &optional gensym-buf) (set-process-sentinel this-process (spscwt-process-will-be-kill that-process gensym-buf))) (defun spscwt-gensym-buf (&optional buffer) (or buffer (format "%s" (gensym)))) (defun start-process-shell-command-with-timeout (sec name buf command &rest args) "start process with time-out. this function return called-process. if BUFFER value is nil, then this function using gensym-buffer, this buffer will be killed when called-process ends" (let ((this-buf (spscwt-gensym-buf buf)) (that-buf (spscwt-gensym-buf))) (let ((this-process (apply 'start-process-shell-command name this-buf command args)) (that-process (start-process-shell-command that-buf that-buf (format "sleep %s" sec)))) (spscwt-set-mutual-dependent-process this-process that-process (and (null buf) this-buf)) (spscwt-set-mutual-dependent-process that-process this-process that-buf) this-process))) (defun shell-command-with-timeout (wait-sec command &optional output-buffer) "using start-process-shell-command-with-timeout (spscwt-hooked-process)" (lexical-let ((buf (or output-buffer "*Shell Command Output*"))) (when (get-buffer buf) (with-current-buffer buf (erase-buffer))) (setq spscwt-sentinel-function (lambda (&rest args) (set-window-start (display-buffer buf) 1))) (process-exit-status (start-process-shell-command-with-timeout wait-sec buf buf command))))
e.g.
(shell-command-with-timeout 1 "ls") (shell-command-with-timeout 2 "for i in `seq 100`; do echo $i; sleep 0.1; done") (shell-command-with-timeout 20 "for i in `seq 100`; do echo $i; sleep 0.01; done")