0
|
1 ;;; background.el --- fun with background jobs
|
|
2
|
|
3 ;; Copyright (C) 1988 Joe Keane <jk3k+@andrew.cmu.edu>
|
|
4 ;; Keywords: processes
|
|
5
|
|
6 ;; This file is part of XEmacs.
|
72
|
7
|
0
|
8 ;; XEmacs is free software; you can redistribute it and/or modify
|
|
9 ;; it under the terms of the GNU General Public License as published by
|
|
10 ;; the Free Software Foundation; either version 2 of the License, or
|
|
11 ;; (at your option) any later version.
|
72
|
12
|
0
|
13 ;; XEmacs is distributed in the hope that it will be useful,
|
|
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 ;; GNU General Public License for more details.
|
72
|
17
|
0
|
18 ;; You should have received a copy of the GNU General Public License
|
|
19 ;; along with XEmacs; if not, write to the Free Software
|
72
|
20 ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
21 ;; 02111-1307, USA.
|
|
22
|
|
23 ;;; Synched up with: Not in FSF
|
|
24
|
|
25 ;;; Commentary:
|
0
|
26
|
|
27 ;; - Adapted to use comint and cleaned up somewhat. Olin Shivers 5/90
|
|
28 ;; - Background failed to set the process buffer's working directory
|
|
29 ;; in some cases. Fixed. Olin 6/14/90
|
|
30 ;; - Background failed to strip leading cd's off the command string
|
|
31 ;; after performing them. This screwed up relative pathnames.
|
|
32 ;; Furthermore, the proc buffer's default dir wasn't initialised
|
|
33 ;; to the user's buffer's default dir before doing the leading cd.
|
|
34 ;; This also screwed up relative pathnames if the proc buffer already
|
|
35 ;; existed and was set to a different default dir. Hopefully we've
|
|
36 ;; finally got it right. The pwd is now reported in the buffer
|
|
37 ;; just to let the user know. Bug reported by Piet Van Oostrum.
|
|
38 ;; Olin 10/19/90
|
|
39 ;; - Fixed up the sentinel to protect match-data around invocations.
|
|
40 ;; Also slightly rearranged the cd match code for similar reasons.
|
|
41 ;; Olin 7/16/91
|
|
42 ;; - Dec 29 1995: changed for new stuff (shell-command-switch, second
|
|
43 ;; arg to shell-command --> BUFFER-NAME arg to background) from
|
|
44 ;; FSF 19.30. Ben Wing
|
|
45
|
72
|
46 ;;; Code:
|
|
47
|
0
|
48 (provide 'background)
|
|
49 (require 'comint)
|
|
50
|
|
51 ;; user variables
|
|
52 (defvar background-show t
|
|
53 "*If non-nil, background jobs' buffers are shown when they're started.")
|
|
54 (defvar background-select nil
|
|
55 "*If non-nil, background jobs' buffers are selected when they're started.")
|
|
56
|
|
57 (defun background (command &optional buffer-name)
|
|
58 "Run COMMAND in the background like csh.
|
|
59 A message is displayed when the job starts and finishes. The buffer is in
|
|
60 comint mode, so you can send input and signals to the job. The process object
|
|
61 is returned if anyone cares. See also comint-mode and the variables
|
|
62 background-show and background-select.
|
|
63
|
|
64 Optional second argument BUFFER-NAME is a buffer to insert the output into.
|
|
65 If omitted, a buffer name is constructed from the command run."
|
|
66 (interactive "s%% ")
|
|
67 (let ((job-number 1)
|
|
68 job-name
|
|
69 (dir default-directory))
|
|
70 (while (get-process (setq job-name (format "background-%d" job-number)))
|
|
71 (setq job-number (1+ job-number)))
|
|
72 (or buffer-name
|
|
73 (setq buffer-name (format "*%s*" job-name)))
|
|
74 (if background-select (pop-to-buffer buffer-name)
|
|
75 (if background-show (with-output-to-temp-buffer buffer-name)) ; cute
|
|
76 (set-buffer (get-buffer-create buffer-name)))
|
|
77 (erase-buffer)
|
|
78
|
|
79 (setq default-directory dir) ; Do this first, in case cd is relative path.
|
|
80 (if (string-match "^cd[\t ]+\\([^\t ;]+\\)[\t ]*;[\t ]*" command)
|
|
81 (let ((dir (substring command (match-beginning 1) (match-end 1))))
|
|
82 (setq command (substring command (match-end 0)))
|
|
83 (setq default-directory
|
|
84 (file-name-as-directory (expand-file-name dir)))))
|
|
85
|
|
86 (insert "--- working directory: " default-directory
|
|
87 "\n% " command ?\n)
|
|
88
|
|
89 (let ((proc (get-buffer-process
|
|
90 (comint-exec buffer-name job-name shell-file-name
|
|
91 nil (list shell-command-switch command)))))
|
|
92 (comint-mode)
|
|
93 ;; COND because the proc may have died before the G-B-P is called.
|
|
94 (cond (proc (set-process-sentinel proc 'background-sentinel)
|
|
95 (message "[%d] %d" job-number (process-id proc))))
|
|
96 (setq mode-name "Background")
|
|
97 proc)))
|
|
98
|
|
99
|
|
100 (defun background-sentinel (process msg)
|
|
101 "Called when a background job changes state."
|
|
102 (let ((ms (match-data))) ; barf
|
|
103 (unwind-protect
|
|
104 (let ((msg (cond ((string= msg "finished\n") "Done")
|
|
105 ((string-match "^exited" msg)
|
|
106 (concat "Exit " (substring msg 28 -1)))
|
|
107 ((zerop (length msg)) "Continuing")
|
|
108 (t (concat (upcase (substring msg 0 1))
|
|
109 (substring msg 1 -1))))))
|
|
110 (message "[%s] %s %s" (process-name process)
|
|
111 msg
|
|
112 (nth 2 (process-command process)))
|
|
113 (if (null (buffer-name (process-buffer process)))
|
|
114 (set-process-buffer process nil) ; WHY? Olin.
|
|
115 (if (memq (process-status process) '(signal exit))
|
|
116 (save-excursion
|
|
117 (set-buffer (process-buffer process))
|
|
118 (let ((at-end (eobp)))
|
|
119 (save-excursion
|
|
120 (goto-char (point-max))
|
|
121 (insert ?\n msg ?
|
|
122 (substring (current-time-string) 11 19) ?\n))
|
|
123 (if at-end (goto-char (point-max))))
|
|
124 (set-buffer-modified-p nil)))))
|
|
125 (store-match-data ms))))
|
72
|
126
|
|
127 ;;; background.el ends here
|