annotate lisp/comint/background.el @ 80:1ce6082ce73f r20-0b90

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