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