2 | 2 |
;; Copyright (C) 2010-2016 SAKURAI Masashi
|
3 | 3 |
|
4 | 4 |
;; Author: SAKURAI Masashi <m.sakurai at kiwanami.net>
|
5 | |
;; Version: 0.5.0
|
|
5 |
;; Version: 0.5.1
|
6 | 6 |
;; Keywords: deferred, async
|
7 | |
;; Package-Requires: ((emacs "24.3"))
|
|
7 |
;; Package-Requires: ((emacs "24.4"))
|
8 | 8 |
;; URL: https://github.com/kiwanami/emacs-deferred
|
9 | 9 |
|
10 | 10 |
;; This program is free software; you can redistribute it and/or modify
|
|
66 | 66 |
;; deferred.el.
|
67 | 67 |
|
68 | 68 |
(require 'cl-lib)
|
|
69 |
(require 'subr-x)
|
69 | 70 |
|
70 | 71 |
(declare-function pp-display-expression 'pp)
|
71 | 72 |
|
72 | 73 |
(defvar deferred:version nil "deferred.el version")
|
73 | |
(setq deferred:version "0.4.0")
|
|
74 |
(setq deferred:version "0.5.0")
|
74 | 75 |
|
75 | 76 |
;;; Code:
|
76 | 77 |
|
|
742 | 743 |
"A deferred wrapper of `start-process'. Return a deferred
|
743 | 744 |
object. The process name and buffer name of the argument of the
|
744 | 745 |
`start-process' are generated by this function automatically.
|
745 | |
The next deferred object receives stdout string from the command
|
746 | |
process."
|
|
746 |
The next deferred object receives stdout and stderr string from
|
|
747 |
the command process."
|
747 | 748 |
(deferred:process-gen 'start-process command args))
|
748 | 749 |
|
749 | 750 |
(defun deferred:process-shell (command &rest args)
|
750 | 751 |
"A deferred wrapper of `start-process-shell-command'. Return a deferred
|
751 | 752 |
object. The process name and buffer name of the argument of the
|
752 | 753 |
`start-process-shell-command' are generated by this function automatically.
|
753 | |
The next deferred object receives stdout string from the command
|
754 | |
process."
|
|
754 |
The next deferred object receives stdout and stderr string from
|
|
755 |
the command process."
|
755 | 756 |
(deferred:process-gen 'start-process-shell-command command args))
|
756 | 757 |
|
757 | 758 |
(defun deferred:process-buffer (command &rest args)
|
758 | 759 |
"A deferred wrapper of `start-process'. Return a deferred
|
759 | 760 |
object. The process name and buffer name of the argument of the
|
760 | 761 |
`start-process' are generated by this function automatically.
|
761 | |
The next deferred object receives stdout buffer from the command
|
762 | |
process."
|
|
762 |
The next deferred object receives stdout and stderr buffer from
|
|
763 |
the command process."
|
763 | 764 |
(deferred:process-buffer-gen 'start-process command args))
|
764 | 765 |
|
765 | 766 |
(defun deferred:process-shell-buffer (command &rest args)
|
766 | 767 |
"A deferred wrapper of `start-process-shell-command'. Return a deferred
|
767 | 768 |
object. The process name and buffer name of the argument of the
|
768 | 769 |
`start-process-shell-command' are generated by this function automatically.
|
769 | |
The next deferred object receives stdout buffer from the command
|
770 | |
process."
|
|
770 |
The next deferred object receives stdout and stderr buffer from
|
|
771 |
the command process."
|
771 | 772 |
(deferred:process-buffer-gen 'start-process-shell-command command args))
|
772 | 773 |
|
773 | 774 |
(defun deferred:process-gen (f command args)
|
|
806 | 807 |
(apply f proc-name buf-name command args)))
|
807 | 808 |
(set-process-sentinel
|
808 | 809 |
proc
|
809 | |
(lambda (_proc event)
|
810 | |
(cond
|
811 | |
((string-match "exited abnormally" event)
|
812 | |
(let ((msg (if (buffer-live-p proc-buf)
|
813 | |
(format "Process [%s] exited abnormally : %s"
|
814 | |
command
|
815 | |
(with-current-buffer proc-buf (buffer-string)))
|
816 | |
(concat "Process exited abnormally: " proc-name))))
|
817 | |
(kill-buffer proc-buf)
|
818 | |
(deferred:post-task nd 'ng msg)))
|
819 | |
((equal event "finished\n")
|
820 | |
(deferred:post-task nd 'ok proc-buf)))))
|
821 | |
(setf (deferred-cancel nd)
|
822 | |
(lambda (x) (deferred:default-cancel x)
|
823 | |
(when proc
|
824 | |
(kill-process proc)
|
825 | |
(kill-buffer proc-buf)))))
|
826 | |
(error (deferred:post-task nd 'ng err)))
|
827 | |
nil))
|
|
810 |
(lambda (proc event)
|
|
811 |
(unless (process-live-p proc)
|
|
812 |
(if (zerop (process-exit-status proc))
|
|
813 |
(deferred:post-task nd 'ok proc-buf)
|
|
814 |
(let ((msg (format "Deferred process exited abnormally:\n command: %s\n exit status: %s %s\n event: %s\n buffer contents: %S"
|
|
815 |
command
|
|
816 |
(process-status proc)
|
|
817 |
(process-exit-status proc)
|
|
818 |
(string-trim-right event)
|
|
819 |
(if (buffer-live-p proc-buf)
|
|
820 |
(with-current-buffer proc-buf
|
|
821 |
(buffer-string))
|
|
822 |
"(unavailable)"))))
|
|
823 |
(kill-buffer proc-buf)
|
|
824 |
(deferred:post-task nd 'ng msg))))))
|
|
825 |
(setf (deferred-cancel nd)
|
|
826 |
(lambda (x) (deferred:default-cancel x)
|
|
827 |
(when proc
|
|
828 |
(kill-process proc)
|
|
829 |
(kill-buffer proc-buf)))))
|
|
830 |
(error (deferred:post-task nd 'ng err)))
|
|
831 |
nil))
|
828 | 832 |
nd)))
|
829 | 833 |
|
830 | 834 |
(defmacro deferred:processc (d command &rest args)
|