Mercurial > emacs
annotate lisp/shell.el @ 783:59dc833c4e0c
*** empty log message ***
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Wed, 15 Jul 1992 18:48:42 +0000 |
parents | 7cbd4fcd8b0f |
children | 3cece0106722 |
rev | line source |
---|---|
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
1 ;;; shell.el --- general command interpreter in a window stuff |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
2 |
252 | 3 ;;; Copyright Olin Shivers (1988). |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
4 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
5 ;;; This file is part of GNU Emacs. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
6 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
7 ;;; GNU Emacs is free software; you can redistribute it and/or modify |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
8 ;;; it under the terms of the GNU General Public License as published by |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
9 ;;; the Free Software Foundation; either version 1, or (at your option) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
10 ;;; any later version. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
11 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
12 ;;; GNU Emacs is distributed in the hope that it will be useful, |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
13 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
15 ;;; GNU General Public License for more details. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
16 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
17 ;;; You should have received a copy of the GNU General Public License |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
18 ;;; along with GNU Emacs; see the file COPYING. If not, write to |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
19 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
114 | 20 |
252 | 21 ;;; The changelog is at the end of file. |
114 | 22 |
252 | 23 ;;; Please send me bug reports, bug fixes, and extensions, so that I can |
24 ;;; merge them into the master source. | |
25 ;;; - Olin Shivers (shivers@cs.cmu.edu) | |
114 | 26 |
252 | 27 ;;; This file defines a a shell-in-a-buffer package (shell mode) built |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
28 ;;; on top of comint mode. This is actually cmushell with things |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
29 ;;; renamed to replace its counterpart in Emacs 18. cmushell is more |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
30 ;;; featureful, robust, and uniform than the Emacs 18 version. |
114 | 31 |
32 ;;; Since this mode is built on top of the general command-interpreter-in- | |
33 ;;; a-buffer mode (comint mode), it shares a common base functionality, | |
34 ;;; and a common set of bindings, with all modes derived from comint mode. | |
252 | 35 ;;; This makes these modes easier to use. |
114 | 36 |
37 ;;; For documentation on the functionality provided by comint mode, and | |
38 ;;; the hooks available for customising it, see the file comint.el. | |
252 | 39 ;;; For further information on shell mode, see the comments below. |
114 | 40 |
41 ;;; Needs fixin: | |
42 ;;; When sending text from a source file to a subprocess, the process-mark can | |
43 ;;; move off the window, so you can lose sight of the process interactions. | |
44 ;;; Maybe I should ensure the process mark is in the window when I send | |
45 ;;; text to the process? Switch selectable? | |
46 | |
47 (require 'comint) | |
252 | 48 ;; YOUR .EMACS FILE |
49 ;;============================================================================= | |
50 ;; Some suggestions for your .emacs file. | |
51 ;; | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
52 ;; ; If cmushell lives in some non-standard directory, you must tell emacs |
252 | 53 ;; ; where to get it. This may or may not be necessary. |
54 ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path)) | |
55 ;; | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
56 ;; ; Autoload cmushell from file cmushell.el |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
57 ;; (autoload 'cmushell "cmushell" |
252 | 58 ;; "Run an inferior shell process." |
59 ;; t) | |
60 ;; | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
61 ;; ; Define C-c t to run my favorite command in cmushell mode: |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
62 ;; (setq cmushell-load-hook |
252 | 63 ;; '((lambda () |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
64 ;; (define-key cmushell-mode-map "\C-ct" 'favorite-cmd)))) |
252 | 65 |
66 | |
67 ;;; Brief Command Documentation: | |
68 ;;;============================================================================ | |
69 ;;; Comint Mode Commands: (common to shell and all comint-derived modes) | |
70 ;;; | |
71 ;;; m-p comint-previous-input Cycle backwards in input history | |
72 ;;; m-n comint-next-input Cycle forwards | |
73 ;;; m-c-r comint-previous-input-matching Search backwards in input history | |
74 ;;; return comint-send-input | |
75 ;;; c-a comint-bol Beginning of line; skip prompt. | |
76 ;;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff. | |
77 ;;; c-c c-u comint-kill-input ^u | |
78 ;;; c-c c-w backward-kill-word ^w | |
79 ;;; c-c c-c comint-interrupt-subjob ^c | |
80 ;;; c-c c-z comint-stop-subjob ^z | |
81 ;;; c-c c-\ comint-quit-subjob ^\ | |
82 ;;; c-c c-o comint-kill-output Delete last batch of process output | |
83 ;;; c-c c-r comint-show-output Show last batch of process output | |
84 ;;; send-invisible Read line w/o echo & send to proc | |
85 ;;; comint-continue-subjob Useful if you accidentally suspend | |
86 ;;; top-level job. | |
87 ;;; comint-mode-hook is the comint mode hook. | |
88 | |
89 ;;; Shell Mode Commands: | |
90 ;;; shell Fires up the shell process. | |
91 ;;; tab comint-dynamic-complete Complete a partial file name | |
92 ;;; m-? comint-dynamic-list-completions List completions in help buffer | |
93 ;;; dirs Resync the buffer's dir stack. | |
94 ;;; dirtrack-toggle Turn dir tracking on/off. | |
95 ;;; | |
96 ;;; The shell mode hook is shell-mode-hook | |
97 ;;; The shell-load-hook is run after this file is loaded. | |
98 ;;; comint-prompt-regexp is initialised to shell-prompt-pattern, for backwards | |
99 ;;; compatibility. | |
100 | |
101 ;;; Read the rest of this file for more information. | |
102 | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
103 ;;; SHELL.EL COMPATIBILITY |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
104 ;;; Notes from when this was called cmushell, and was not the standard emacs |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
105 ;;; shell package. |
252 | 106 ;;;============================================================================ |
107 ;;; In brief: this package should have no trouble coexisting with shell.el. | |
108 ;;; | |
109 ;;; Most customising variables -- e.g., explicit-shell-file-name -- are the | |
110 ;;; same, so the users shouldn't have much trouble. Hooks have different | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
111 ;;; names, however, so you can customise shell mode differently from cmushell |
252 | 112 ;;; mode. You basically just have to remember to type M-x cmushell instead of |
113 ;;; M-x shell. | |
114 ;;; | |
115 ;;; It would be nice if this file was completely plug-compatible with the old | |
116 ;;; shell package -- if you could just name this file shell.el, and have it | |
117 ;;; transparently replace the old one. But you can't. Several other packages | |
118 ;;; (tex-mode, background, dbx, gdb, kermit, monkey, prolog, telnet) are also | |
119 ;;; clients of shell mode. These packages assume detailed knowledge of shell | |
120 ;;; mode internals in ways that are incompatible with cmushell mode (mostly | |
121 ;;; because of cmushell mode's greater functionality). So, unless we are | |
122 ;;; willing to port all of these packages, we can't have this file be a | |
123 ;;; complete replacement for shell.el -- that is, we can't name this file | |
124 ;;; shell.el, and its main entry point (shell), because dbx.el will break | |
125 ;;; when it loads it in and tries to use it. | |
126 ;;; | |
127 ;;; There are two ways to fix this. One: rewrite these other modes to use the | |
128 ;;; new package. This is a win, but can't be assumed. The other, backwards | |
129 ;;; compatible route, is to make this package non-conflict with shell.el, so | |
130 ;;; both files can be loaded in at the same time. And *that* is why some | |
131 ;;; functions and variables have different names: (cmushell), | |
132 ;;; cmushell-mode-map, that sort of thing. All the names have been carefully | |
133 ;;; chosen so that shell.el and cmushell.el won't tromp on each other. | |
134 | |
135 ;;; Customisation and Buffer Variables | |
136 ;;; =========================================================================== | |
137 ;;; | |
138 | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
139 ;;;###autoload |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
140 (defconst shell-prompt-pattern "^[^#$%>]*[#$%>] *" |
252 | 141 "Regexp to match prompts in the inferior shell. |
142 Defaults to \"^[^#$%>]*[#$%>] *\", which works pretty well. | |
143 This variable is used to initialise comint-prompt-regexp in the | |
144 shell buffer. | |
145 | |
146 This is a fine thing to set in your .emacs file.") | |
147 | |
114 | 148 (defvar shell-popd-regexp "popd" |
149 "*Regexp to match subshell commands equivalent to popd.") | |
150 | |
151 (defvar shell-pushd-regexp "pushd" | |
152 "*Regexp to match subshell commands equivalent to pushd.") | |
153 | |
154 (defvar shell-cd-regexp "cd" | |
155 "*Regexp to match subshell commands equivalent to cd.") | |
156 | |
157 (defvar explicit-shell-file-name nil | |
158 "*If non-nil, is file name to use for explicitly requested inferior shell.") | |
159 | |
160 (defvar explicit-csh-args | |
161 (if (eq system-type 'hpux) | |
162 ;; -T persuades HP's csh not to think it is smarter | |
163 ;; than us about what terminal modes to use. | |
164 '("-i" "-T") | |
165 '("-i")) | |
166 "*Args passed to inferior shell by M-x shell, if the shell is csh. | |
167 Value is a list of strings, which may be nil.") | |
168 | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
169 ;;; All the above vars aren't prefixed "cmushell-" to make them |
252 | 170 ;;; backwards compatible w/shell.el and old .emacs files. |
171 | |
114 | 172 (defvar shell-dirstack nil |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
173 "List of directories saved by pushd in this buffer's shell. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
174 Thus, this does not include the shell's current directory.") |
114 | 175 |
176 (defvar shell-dirstack-query "dirs" | |
177 "Command used by shell-resync-dirlist to query shell.") | |
178 | |
252 | 179 (defvar shell-mode-map '()) |
114 | 180 (cond ((not shell-mode-map) |
252 | 181 (setq shell-mode-map (full-copy-sparse-keymap comint-mode-map)) |
114 | 182 (define-key shell-mode-map "\t" 'comint-dynamic-complete) |
183 (define-key shell-mode-map "\M-?" 'comint-dynamic-list-completions))) | |
184 | |
185 (defvar shell-mode-hook '() | |
186 "*Hook for customising shell mode") | |
187 | |
188 | |
189 ;;; Basic Procedures | |
190 ;;; =========================================================================== | |
191 ;;; | |
192 | |
193 (defun shell-mode () | |
194 "Major mode for interacting with an inferior shell. | |
195 Return after the end of the process' output sends the text from the | |
196 end of process to the end of the current line. | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
197 Return before end of process output copies the current line (except |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
198 for the prompt) to the end of the buffer and sends it. |
114 | 199 M-x send-invisible reads a line of text without echoing it, and sends it to |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
200 the shell. This is useful for entering passwords. |
114 | 201 |
202 If you accidentally suspend your process, use \\[comint-continue-subjob] | |
203 to continue it. | |
204 | |
205 cd, pushd and popd commands given to the shell are watched by Emacs to keep | |
206 this buffer's default directory the same as the shell's working directory. | |
207 M-x dirs queries the shell and resyncs Emacs' idea of what the current | |
208 directory stack is. | |
209 M-x dirtrack-toggle turns directory tracking on and off. | |
210 | |
211 \\{shell-mode-map} | |
212 Customisation: Entry to this mode runs the hooks on comint-mode-hook and | |
213 shell-mode-hook (in that order). | |
214 | |
215 Variables shell-cd-regexp, shell-pushd-regexp and shell-popd-regexp are used | |
216 to match their respective commands." | |
217 (interactive) | |
218 (comint-mode) | |
252 | 219 (setq comint-prompt-regexp shell-prompt-pattern) |
220 (setq major-mode 'shell-mode) | |
221 (setq mode-name "shell") | |
114 | 222 (use-local-map shell-mode-map) |
223 (make-local-variable 'shell-dirstack) | |
252 | 224 (setq shell-dirstack nil) |
225 (make-local-variable 'shell-dirtrackp) | |
226 (setq shell-dirtrackp t) | |
227 (setq comint-input-sentinel 'shell-directory-tracker) | |
114 | 228 (run-hooks 'shell-mode-hook)) |
229 | |
230 | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
231 ;;;###autoload |
114 | 232 (defun shell () |
233 "Run an inferior shell, with I/O through buffer *shell*. | |
234 If buffer exists but shell process is not running, make new shell. | |
252 | 235 If buffer exists and shell process is running, |
236 just switch to buffer *shell*. | |
237 Program used comes from variable explicit-shell-file-name, | |
238 or (if that is nil) from the ESHELL environment variable, | |
239 or else from SHELL if there is no ESHELL. | |
240 If a file ~/.emacs_SHELLNAME exists, it is given as initial input | |
241 (Note that this may lose due to a timing error if the shell | |
242 discards input when it starts up.) | |
243 The buffer is put in shell-mode, giving commands for sending input | |
244 and controlling the subjobs of the shell. See shell-mode. | |
245 See also variable shell-prompt-pattern. | |
114 | 246 |
252 | 247 The shell file name (sans directories) is used to make a symbol name |
114 | 248 such as `explicit-csh-arguments'. If that symbol is a variable, |
249 its value is used as a list of arguments when invoking the shell. | |
250 Otherwise, one argument `-i' is passed to the shell. | |
251 | |
252 \(Type \\[describe-mode] in the shell buffer for a list of commands.)" | |
253 (interactive) | |
254 (cond ((not (comint-check-proc "*shell*")) | |
255 (let* ((prog (or explicit-shell-file-name | |
256 (getenv "ESHELL") | |
257 (getenv "SHELL") | |
252 | 258 "/bin/sh")) |
114 | 259 (name (file-name-nondirectory prog)) |
260 (startfile (concat "~/.emacs_" name)) | |
261 (xargs-name (intern-soft (concat "explicit-" name "-args")))) | |
262 (set-buffer (apply 'make-comint "shell" prog | |
263 (if (file-exists-p startfile) startfile) | |
264 (if (and xargs-name (boundp xargs-name)) | |
265 (symbol-value xargs-name) | |
266 '("-i")))) | |
267 (shell-mode)))) | |
268 (switch-to-buffer "*shell*")) | |
269 | |
270 | |
271 ;;; Directory tracking | |
272 ;;; =========================================================================== | |
273 ;;; This code provides the shell mode input sentinel | |
274 ;;; SHELL-DIRECTORY-TRACKER | |
275 ;;; that tracks cd, pushd, and popd commands issued to the shell, and | |
276 ;;; changes the current directory of the shell buffer accordingly. | |
277 ;;; | |
278 ;;; This is basically a fragile hack, although it's more accurate than | |
252 | 279 ;;; the released version in shell.el. It has the following failings: |
114 | 280 ;;; 1. It doesn't know about the cdpath shell variable. |
281 ;;; 2. It only spots the first command in a command sequence. E.g., it will | |
282 ;;; miss the cd in "ls; cd foo" | |
283 ;;; 3. More generally, any complex command (like ";" sequencing) is going to | |
284 ;;; throw it. Otherwise, you'd have to build an entire shell interpreter in | |
285 ;;; emacs lisp. Failing that, there's no way to catch shell commands where | |
286 ;;; cd's are buried inside conditional expressions, aliases, and so forth. | |
287 ;;; | |
288 ;;; The whole approach is a crock. Shell aliases mess it up. File sourcing | |
289 ;;; messes it up. You run other processes under the shell; these each have | |
290 ;;; separate working directories, and some have commands for manipulating | |
291 ;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have | |
252 | 292 ;;; commands that do *not* affect the current w.d. at all, but look like they |
114 | 293 ;;; do (e.g., the cd command in ftp). In shells that allow you job |
294 ;;; control, you can switch between jobs, all having different w.d.'s. So | |
295 ;;; simply saying %3 can shift your w.d.. | |
296 ;;; | |
297 ;;; The solution is to relax, not stress out about it, and settle for | |
298 ;;; a hack that works pretty well in typical circumstances. Remember | |
299 ;;; that a half-assed solution is more in keeping with the spirit of Unix, | |
300 ;;; anyway. Blech. | |
301 ;;; | |
302 ;;; One good hack not implemented here for users of programmable shells | |
303 ;;; is to program up the shell w.d. manipulation commands to output | |
304 ;;; a coded command sequence to the tty. Something like | |
305 ;;; ESC | <cwd> | | |
306 ;;; where <cwd> is the new current working directory. Then trash the | |
307 ;;; directory tracking machinery currently used in this package, and | |
308 ;;; replace it with a process filter that watches for and strips out | |
309 ;;; these messages. | |
310 | |
311 ;;; REGEXP is a regular expression. STR is a string. START is a fixnum. | |
312 ;;; Returns T if REGEXP matches STR where the match is anchored to start | |
313 ;;; at position START in STR. Sort of like LOOKING-AT for strings. | |
314 (defun shell-front-match (regexp str start) | |
315 (eq start (string-match regexp str start))) | |
316 | |
317 (defun shell-directory-tracker (str) | |
318 "Tracks cd, pushd and popd commands issued to the shell. | |
319 This function is called on each input passed to the shell. | |
320 It watches for cd, pushd and popd commands and sets the buffer's | |
321 default directory to track these commands. | |
322 | |
323 You may toggle this tracking on and off with M-x dirtrack-toggle. | |
324 If emacs gets confused, you can resync with the shell with M-x dirs. | |
325 | |
326 See variables shell-cd-regexp, shell-pushd-regexp, and shell-popd-regexp. | |
327 Environment variables are expanded, see function substitute-in-file-name." | |
328 (condition-case err | |
329 (cond (shell-dirtrackp | |
330 (string-match "^\\s *" str) ; skip whitespace | |
331 (let ((bos (match-end 0)) | |
332 (x nil)) | |
333 (cond ((setq x (shell-match-cmd-w/optional-arg shell-popd-regexp | |
334 str bos)) | |
335 (shell-process-popd (substitute-in-file-name x))) | |
336 ((setq x (shell-match-cmd-w/optional-arg shell-pushd-regexp | |
337 str bos)) | |
338 (shell-process-pushd (substitute-in-file-name x))) | |
339 ((setq x (shell-match-cmd-w/optional-arg shell-cd-regexp | |
340 str bos)) | |
341 (shell-process-cd (substitute-in-file-name x))))))) | |
342 (error (message (car (cdr err)))))) | |
343 | |
344 | |
345 ;;; Try to match regexp CMD to string, anchored at position START. | |
346 ;;; CMD may be followed by a single argument. If a match, then return | |
347 ;;; the argument, if there is one, or the empty string if not. If | |
348 ;;; no match, return nil. | |
349 | |
350 (defun shell-match-cmd-w/optional-arg (cmd str start) | |
351 (and (shell-front-match cmd str start) | |
352 (let ((eoc (match-end 0))) ; end of command | |
353 (cond ((shell-front-match "\\s *\\(\;\\|$\\)" str eoc) | |
354 "") ; no arg | |
355 ((shell-front-match "\\s +\\([^ \t\;]+\\)\\s *\\(\;\\|$\\)" | |
356 str eoc) | |
357 (substring str (match-beginning 1) (match-end 1))) ; arg | |
358 (t nil))))) ; something else. | |
359 ;;; The first regexp is [optional whitespace, (";" or the end of string)]. | |
360 ;;; The second regexp is [whitespace, (an arg), optional whitespace, | |
361 ;;; (";" or end of string)]. | |
362 | |
363 | |
364 ;;; popd [+n] | |
365 (defun shell-process-popd (arg) | |
366 (let ((num (if (zerop (length arg)) 0 ; no arg means +0 | |
367 (shell-extract-num arg)))) | |
368 (if (and num (< num (length shell-dirstack))) | |
369 (if (= num 0) ; condition-case because the CD could lose. | |
370 (condition-case nil (progn (cd (car shell-dirstack)) | |
371 (setq shell-dirstack | |
372 (cdr shell-dirstack)) | |
373 (shell-dirstack-message)) | |
374 (error (message "Couldn't cd."))) | |
375 (let* ((ds (cons nil shell-dirstack)) | |
376 (cell (nthcdr (- num 1) ds))) | |
377 (rplacd cell (cdr (cdr cell))) | |
378 (setq shell-dirstack (cdr ds)) | |
379 (shell-dirstack-message))) | |
380 (message "Bad popd.")))) | |
381 | |
382 | |
383 ;;; cd [dir] | |
384 (defun shell-process-cd (arg) | |
385 (condition-case nil (progn (cd (if (zerop (length arg)) (getenv "HOME") | |
386 arg)) | |
387 (shell-dirstack-message)) | |
388 (error (message "Couldn't cd.")))) | |
389 | |
390 | |
391 ;;; pushd [+n | dir] | |
392 (defun shell-process-pushd (arg) | |
393 (if (zerop (length arg)) | |
394 ;; no arg -- swap pwd and car of shell stack | |
395 (condition-case nil (if shell-dirstack | |
396 (let ((old default-directory)) | |
397 (cd (car shell-dirstack)) | |
398 (setq shell-dirstack | |
399 (cons old (cdr shell-dirstack))) | |
400 (shell-dirstack-message)) | |
401 (message "Directory stack empty.")) | |
402 (message "Couldn't cd.")) | |
403 | |
404 (let ((num (shell-extract-num arg))) | |
405 (if num ; pushd +n | |
406 (if (> num (length shell-dirstack)) | |
407 (message "Directory stack not that deep.") | |
408 (let* ((ds (cons default-directory shell-dirstack)) | |
409 (dslen (length ds)) | |
410 (front (nthcdr num ds)) | |
411 (back (reverse (nthcdr (- dslen num) (reverse ds)))) | |
412 (new-ds (append front back))) | |
413 (condition-case nil | |
414 (progn (cd (car new-ds)) | |
415 (setq shell-dirstack (cdr new-ds)) | |
416 (shell-dirstack-message)) | |
417 (error (message "Couldn't cd."))))) | |
418 | |
419 ;; pushd <dir> | |
420 (let ((old-wd default-directory)) | |
421 (condition-case nil | |
422 (progn (cd arg) | |
423 (setq shell-dirstack | |
424 (cons old-wd shell-dirstack)) | |
425 (shell-dirstack-message)) | |
426 (error (message "Couldn't cd.")))))))) | |
427 | |
428 ;; If STR is of the form +n, for n>0, return n. Otherwise, nil. | |
429 (defun shell-extract-num (str) | |
430 (and (string-match "^\\+[1-9][0-9]*$" str) | |
431 (string-to-int str))) | |
432 | |
433 | |
434 (defun shell-dirtrack-toggle () | |
435 "Turn directory tracking on and off in a shell buffer." | |
436 (interactive) | |
437 (setq shell-dirtrackp (not shell-dirtrackp)) | |
438 (message "directory tracking %s." | |
439 (if shell-dirtrackp "ON" "OFF"))) | |
440 | |
441 ;;; For your typing convenience: | |
442 (fset 'dirtrack-toggle 'shell-dirtrack-toggle) | |
443 | |
444 | |
445 (defun shell-resync-dirs () | |
446 "Resync the buffer's idea of the current directory stack. | |
447 This command queries the shell with the command bound to | |
448 shell-dirstack-query (default \"dirs\"), reads the next | |
449 line output and parses it to form the new directory stack. | |
450 DON'T issue this command unless the buffer is at a shell prompt. | |
451 Also, note that if some other subprocess decides to do output | |
452 immediately after the query, its output will be taken as the | |
453 new directory stack -- you lose. If this happens, just do the | |
454 command again." | |
455 (interactive) | |
456 (let* ((proc (get-buffer-process (current-buffer))) | |
457 (pmark (process-mark proc))) | |
458 (goto-char pmark) | |
459 (insert shell-dirstack-query) (insert "\n") | |
460 (sit-for 0) ; force redisplay | |
461 (comint-send-string proc shell-dirstack-query) | |
462 (comint-send-string proc "\n") | |
463 (set-marker pmark (point)) | |
464 (let ((pt (point))) ; wait for 1 line | |
465 ;; This extra newline prevents the user's pending input from spoofing us. | |
466 (insert "\n") (backward-char 1) | |
467 (while (not (looking-at ".+\n")) | |
468 (accept-process-output proc) | |
469 (goto-char pt))) | |
470 (goto-char pmark) (delete-char 1) ; remove the extra newline | |
471 ;; That's the dirlist. grab it & parse it. | |
472 (let* ((dl (buffer-substring (match-beginning 0) (- (match-end 0) 1))) | |
473 (dl-len (length dl)) | |
474 (ds '()) ; new dir stack | |
475 (i 0)) | |
476 (while (< i dl-len) | |
477 ;; regexp = optional whitespace, (non-whitespace), optional whitespace | |
478 (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir | |
479 (setq ds (cons (substring dl (match-beginning 1) (match-end 1)) | |
480 ds)) | |
481 (setq i (match-end 0))) | |
482 (let ((ds (reverse ds))) | |
483 (condition-case nil | |
484 (progn (cd (car ds)) | |
485 (setq shell-dirstack (cdr ds)) | |
486 (shell-dirstack-message)) | |
487 (error (message "Couldn't cd."))))))) | |
488 | |
489 ;;; For your typing convenience: | |
490 (fset 'dirs 'shell-resync-dirs) | |
491 | |
492 | |
493 ;;; Show the current dirstack on the message line. | |
494 ;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo". | |
495 ;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".) | |
496 ;;; All the commands that mung the buffer's dirstack finish by calling | |
497 ;;; this guy. | |
498 (defun shell-dirstack-message () | |
499 (let ((msg "") | |
500 (ds (cons default-directory shell-dirstack))) | |
501 (while ds | |
502 (let ((dir (car ds))) | |
503 (if (string-match (format "^%s\\(/\\|$\\)" (getenv "HOME")) dir) | |
504 (setq dir (concat "~/" (substring dir (match-end 0))))) | |
505 (if (string-equal dir "~/") (setq dir "~")) | |
506 (setq msg (concat msg dir " ")) | |
507 (setq ds (cdr ds)))) | |
508 (message msg))) | |
252 | 509 |
510 | |
511 | |
512 ;;; Interfacing to client packages (and converting them) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
513 ;;; Notes from when this was called cmushell, and was not the standard emacs |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
514 ;;; shell package. Many of the conversions discussed here have been done. |
252 | 515 ;;;============================================================================ |
516 ;;; Several gnu packages (tex-mode, background, dbx, gdb, kermit, prolog, | |
517 ;;; telnet are some) use the shell package as clients. Most of them would | |
518 ;;; be better off using the comint package directly, but they predate it. | |
519 ;;; The catch is that most of these packages (dbx, gdb, prolog, telnet) | |
520 ;;; assume total knowledge of all the local variables that shell mode | |
521 ;;; functions depend on. So they (kill-all-local-variables), then create | |
522 ;;; the few local variables that shell.el functions depend on. Alas, | |
523 ;;; cmushell.el functions depend on a different set of vars (for example, | |
524 ;;; the input history ring is a local variable in cmushell.el's shell mode, | |
525 ;;; whereas there is no input history ring in shell.el's shell mode). | |
526 ;;; So we have a situation where the greater functionality of cmushell.el | |
527 ;;; is biting us -- you can't just replace shell will cmushell. | |
528 ;;; | |
529 ;;; Altering these packages to use comint mode directly should *greatly* | |
530 ;;; improve their functionality, and is actually pretty easy. It's | |
531 ;;; mostly a matter of renaming a few variable names. See comint.el for more. | |
532 ;;; -Olin | |
533 | |
534 | |
535 | |
536 ;;; Do the user's customisation... | |
537 ;;;=============================== | |
538 (defvar shell-load-hook nil | |
539 "This hook is run when shell is loaded in. | |
540 This is a good place to put keybindings.") | |
541 | |
542 (run-hooks 'shell-load-hook) | |
543 | |
544 ;;; Change Log | |
545 ;;; =========================================================================== | |
546 ;;; Olin 8/88 | |
547 ;;; Created. | |
548 ;;; | |
549 ;;; Olin 5/26/90 | |
550 ;;; - Split cmulisp and cmushell modes into separate files. | |
551 ;;; Not only is this a good idea, it's apparently the way it'll be rel 19. | |
552 ;;; - Souped up the directory tracking; it now can handle pushd, pushd +n, | |
553 ;;; and popd +n. | |
554 ;;; - Added cmushell-dirtrack-toggle command to toggle the directory | |
555 ;;; tracking that cmushell tries to do. This is useful, for example, | |
556 ;;; when you are running ftp -- it prevents the ftp "cd" command from | |
557 ;;; spoofing the tracking machinery. This command is also named | |
558 ;;; dirtrack-toggle, so you need only type M-x dirtrack to run it. | |
559 ;;; - Added cmushell-resync-dirs command. This queries the shell | |
560 ;;; for the current directory stack, and resets the buffer's stack | |
561 ;;; accordingly. This command is also named dirs, so you need only type | |
562 ;;; M-x dirs to run it. | |
563 ;;; - Bits of the new directory tracking code were adapted from source | |
564 ;;; contributed by Vince Broman, Jeff Peck, and Barry Warsaw. | |
565 ;;; - See also the improvements made to comint.el at the same time. | |
566 ;;; - Renamed several variables. Mostly this comprised changing "shell" | |
567 ;;; to "cmushell" in the names. The only variables that are not prefixed | |
568 ;;; with "cmushell-" are the ones that are common with shell.el: | |
569 ;;; explicit-shell-file-name shell-prompt-pattern explicit-csh-args | |
570 ;;; and shell-cd/popd/pushd-regexp | |
571 ;;; The variables and functions that were changed to have "cmushell-" | |
572 ;;; prefixes are: | |
573 ;;; shell-directory-stack (v), shell-directory-tracker (f) | |
574 ;;; This should not affect users, only elisp hackers. Hopefully | |
575 ;;; one day shell.el will just go away, and we can drop all this | |
576 ;;; "cmushell" bullshit. | |
577 ;;; - Upgraded process sends to use comint-send-string instead of | |
578 ;;; process-send-string. | |
579 ;;; | |
580 ;;; Olin 6/14/90 | |
581 ;;; - If your shell is named <shellname>, and a variable named | |
582 ;;; explicit-<shellname>-args exists, cmushell is supposed | |
583 ;;; to use its value as the arglist to the shell invocation. | |
584 ;;; E.g., if you define explicit-csh-args to be | |
585 ;;; ("-ifx"), then when cmushell cranks up a csh, it execs it | |
586 ;;; as "csh -ifx". This is what is documented. What has actually | |
587 ;;; been the case is that the variable checked is | |
588 ;;; explicit-<shellname>-arguments, not explicit-<shellname>-args. | |
589 ;;; The documentation has been changed to conform to the code (for | |
590 ;;; backwards compatibility with shell.el). This bug is inherited from | |
591 ;;; the same bug in shell.el. | |
592 ;;; This bug reported by Stephen Anderson. | |
593 ;;; | |
594 ;;; Olin 9/5/90 | |
595 ;;; - Arguments to cd, popd, and pushd now have their env vars expanded | |
596 ;;; out by the tracking machinery. So if you say "cd $SRCDIR/funs", the | |
597 ;;; $SRCDIR var will be replaced by its value *in emacs' process | |
598 ;;; environment*. If this is different from the shell's binding of the | |
599 ;;; variable, you lose. Several users needed this feature, fragile | |
600 ;;; though it may be. The fix was contributed by sk@thp.Uni-Koeln.DE. | |
601 ;;; | |
602 ;;; Olin 3/12/91 | |
603 ;;; - Moved comint-dynamic-complete (filename completion) from M-tab to tab. | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
604 ;;; |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
605 ;;; Jim Blandy 10/30/91 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
606 ;;; - Removed the "cmu" prefix from names, renamed file to shell.el, |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
607 ;;; to become the standard shell package. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
608 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
609 (provide 'shell) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
610 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
252
diff
changeset
|
611 ;;; shell.el ends here |