]> code.delx.au - gnu-emacs/blobdiff - lisp/eshell/esh-proc.el
Update copyright year to 2016
[gnu-emacs] / lisp / eshell / esh-proc.el
index f697a40055694b02f37086d54e0020c95f627845..8c6bad089c58d3fad7914e7579f048d4decb8d71 100644 (file)
@@ -1,6 +1,6 @@
-;;; esh-proc.el --- process management
+;;; esh-proc.el --- process management  -*- lexical-binding:t -*-
 
-;; Copyright (C) 1999-201 Free Software Foundation, Inc.
+;; Copyright (C) 1999-2016 Free Software Foundation, Inc.
 
 ;; Author: John Wiegley <johnw@gnu.org>
 
@@ -25,9 +25,7 @@
 
 (provide 'esh-proc)
 
-(eval-when-compile
-  (require 'eshell)
-  (require 'esh-util))
+(require 'esh-cmd)
 
 (defgroup eshell-proc nil
   "When Eshell invokes external commands, it always does so
@@ -38,8 +36,9 @@ finish."
 
 ;;; User Variables:
 
-(defcustom eshell-proc-load-hook '(eshell-proc-initialize)
+(defcustom eshell-proc-load-hook nil
   "A hook that gets run when `eshell-proc' is loaded."
+  :version "24.1"                      ; removed eshell-proc-initialize
   :type 'hook
   :group 'eshell-proc)
 
@@ -94,13 +93,14 @@ is created."
   :type 'hook
   :group 'eshell-proc)
 
-(defcustom eshell-kill-hook '(eshell-reset-after-proc)
+(defcustom eshell-kill-hook nil
   "Called when a process run by `eshell-gather-process-output' has ended.
 It is passed two arguments: the process that was just ended, and the
 termination status (as a string).  Note that the first argument may be
 nil, in which case the user attempted to send a signal, but there was
 no relevant process.  This can be used for displaying help
 information, for example."
+  :version "24.1"                      ; removed eshell-reset-after-proc
   :type 'hook
   :group 'eshell-proc)
 
@@ -113,6 +113,16 @@ information, for example."
 
 ;;; Functions:
 
+(defun eshell-kill-process-function (proc status)
+  "Function run when killing a process.
+Runs `eshell-reset-after-proc' and `eshell-kill-hook', passing arguments
+PROC and STATUS to functions on the latter."
+  ;; Was there till 24.1, but it is not optional.
+  (if (memq 'eshell-reset-after-proc eshell-kill-hook)
+      (setq eshell-kill-hook (delq 'eshell-reset-after-proc eshell-kill-hook)))
+  (eshell-reset-after-proc status)
+  (run-hook-with-args 'eshell-kill-hook proc status))
+
 (defun eshell-proc-initialize ()
   "Initialize the process handling code."
   (make-local-variable 'eshell-process-list)
@@ -125,7 +135,7 @@ information, for example."
 ; (define-key eshell-command-map [(control ?z)]  'eshell-stop-process)
   (define-key eshell-command-map [(control ?\\)] 'eshell-quit-process))
 
-(defun eshell-reset-after-proc (proc status)
+(defun eshell-reset-after-proc (status)
   "Reset the command input location after a process terminates.
 The signals which will cause this to happen are matched by
 `eshell-reset-signals'."
@@ -155,43 +165,38 @@ The signals which will cause this to happen are matched by
        (list-processes)))
 
 (defun eshell/kill (&rest args)
-  "Kill processes, buffers, symbol or files."
-  (let ((ptr args)
-       (signum 'SIGINT))
-    (while ptr
-      (if (or (eshell-processp (car ptr))
-             (and (stringp (car ptr))
-                  (string-match "^[A-Za-z/][A-Za-z0-9<>/]+$"
-                                (car ptr))))
-         ;; What about when $lisp-variable is possible here?
-         ;; It could very well name a process.
-         (setcar ptr (get-process (car ptr))))
-      (setq ptr (cdr ptr)))
+  "Kill processes.
+Usage: kill [-<signal>] <pid>|<process> ...
+Accepts PIDs and process objects."
+  ;; If the first argument starts with a dash, treat it as the signal
+  ;; specifier.
+  (let ((signum 'SIGINT))
+    (let ((arg (car args))
+          (case-fold-search nil))
+      (when (stringp arg)
+        (cond
+         ((string-match "\\`-[[:digit:]]+\\'" arg)
+          (setq signum (abs (string-to-number arg))))
+         ((string-match "\\`-\\([[:upper:]]+\\|[[:lower:]]+\\)\\'" arg)
+          (setq signum (abs (string-to-number arg)))))
+        (setq args (cdr args))))
     (while args
-      (let ((id (if (eshell-processp (car args))
-                   (process-id (car args))
-                 (car args))))
-       (when id
-         (cond
-          ((null id)
-           (error "kill: bad signal spec"))
-          ((and (numberp id) (= id 0))
-           (error "kill: bad signal spec `%d'" id))
-          ((and (stringp id)
-                (string-match "^-?[0-9]+$" id))
-           (setq signum (abs (string-to-number id))))
-          ((stringp id)
-           (let (case-fold-search)
-             (if (string-match "^-\\([A-Z]+[12]?\\)$" id)
-                 (setq signum
-                       (intern (concat "SIG" (match-string 1 id))))
-               (error "kill: bad signal spec `%s'" id))))
-          ((< id 0)
-           (setq signum (abs id)))
-          (t
-           (signal-process id signum)))))
-      (setq args (cdr args)))
-    nil))
+      (let ((arg (if (eshell-processp (car args))
+                     (process-id (car args))
+                   (car args))))
+        (when arg
+          (cond
+           ((null arg)
+            (error "kill: null pid.  Process may actually be a network connection."))
+           ((not (numberp arg))
+            (error "kill: invalid argument type: %s" (type-of arg)))
+           ((and (numberp arg)
+                 (<= arg 0))
+            (error "kill: bad pid: %d" arg))
+           (t
+            (signal-process arg signum)))))
+      (setq args (cdr args))))
+  nil)
 
 (defun eshell-read-process-name (prompt)
   "Read the name of a process from the minibuffer, using completion.
@@ -346,7 +351,7 @@ See `eshell-needs-pipe'."
        (eshell-update-markers eshell-last-output-end)
        ;; Simulate the effect of eshell-sentinel.
        (eshell-close-handles (if (numberp exit-status) exit-status -1))
-       (run-hook-with-args 'eshell-kill-hook command exit-status)
+       (eshell-kill-process-function command exit-status)
        (or eshell-in-pipeline-p
            (setq eshell-last-sync-output-start nil))
        (if (not (numberp exit-status))
@@ -391,20 +396,21 @@ PROC is the process that's exiting.  STRING is the exit message."
                      (eshell-close-handles (process-exit-status proc) 'nil
                                            (cadr entry))))
                (eshell-remove-process-entry entry))))
-       (run-hook-with-args 'eshell-kill-hook proc string)))))
+       (eshell-kill-process-function proc string)))))
 
 (defun eshell-process-interact (func &optional all query)
   "Interact with a process, using PROMPT if more than one, via FUNC.
 If ALL is non-nil, background processes will be interacted with as well.
 If QUERY is non-nil, query the user with QUERY before calling FUNC."
   (let (defunct result)
-    (eshell-for entry eshell-process-list
+    (dolist (entry eshell-process-list)
       (if (and (memq (process-status (car entry))
                    '(run stop open closed))
               (or all
                   (not (nth 2 entry)))
               (or (not query)
-                  (y-or-n-p (format query (process-name (car entry))))))
+                  (y-or-n-p (format-message query
+                                            (process-name (car entry))))))
          (setq result (funcall func (car entry))))
       (unless (memq (process-status (car entry))
                    '(run stop open closed))
@@ -412,7 +418,7 @@ If QUERY is non-nil, query the user with QUERY before calling FUNC."
     ;; clean up the process list; this can get dirty if an error
     ;; occurred that brought the user into the debugger, and then they
     ;; quit, so that the sentinel was never called.
-    (eshell-for d defunct
+    (dolist (d defunct)
       (eshell-remove-process-entry d))
     result))
 
@@ -475,41 +481,39 @@ See the variable `eshell-kill-processes-on-exit'."
     (save-window-excursion
       (list-processes)
       (if (or (not (eq eshell-kill-processes-on-exit 'ask))
-             (y-or-n-p (format "Kill processes owned by `%s'? "
-                               (buffer-name))))
+             (y-or-n-p (format-message "Kill processes owned by `%s'? "
+                                       (buffer-name))))
          (eshell-round-robin-kill
           (if (eq eshell-kill-processes-on-exit 'every)
-              "Kill Eshell child process `%s'? ")))
+              (format-message "Kill Eshell child process `%s'? "))))
       (let ((buf (get-buffer "*Process List*")))
        (if (and buf (buffer-live-p buf))
            (kill-buffer buf)))
       (message nil))))
 
-(custom-add-option 'eshell-exit-hook 'eshell-query-kill-processes)
-
 (defun eshell-interrupt-process ()
   "Interrupt a process."
   (interactive)
   (unless (eshell-process-interact 'interrupt-process)
-    (run-hook-with-args 'eshell-kill-hook nil "interrupt")))
+    (eshell-kill-process-function nil "interrupt")))
 
 (defun eshell-kill-process ()
   "Kill a process."
   (interactive)
   (unless (eshell-process-interact 'kill-process)
-    (run-hook-with-args 'eshell-kill-hook nil "killed")))
+    (eshell-kill-process-function nil "killed")))
 
 (defun eshell-quit-process ()
   "Send quit signal to process."
   (interactive)
   (unless (eshell-process-interact 'quit-process)
-    (run-hook-with-args 'eshell-kill-hook nil "quit")))
+    (eshell-kill-process-function nil "quit")))
 
 ;(defun eshell-stop-process ()
 ;  "Send STOP signal to process."
 ;  (interactive)
 ;  (unless (eshell-process-interact 'stop-process)
-;    (run-hook-with-args 'eshell-kill-hook nil "stopped")))
+;    (eshell-kill-process-function nil "stopped")))
 
 ;(defun eshell-continue-process ()
 ;  "Send CONTINUE signal to process."
@@ -518,7 +522,7 @@ See the variable `eshell-kill-processes-on-exit'."
 ;    ;; jww (1999-09-17): this signal is not dealt with yet.  For
 ;    ;; example, `eshell-reset' will be called, and so will
 ;    ;; `eshell-resume-eval'.
-;    (run-hook-with-args 'eshell-kill-hook nil "continue")))
+;    (eshell-kill-process-function nil "continue")))
 
 (defun eshell-send-eof-to-process ()
   "Send EOF to process."