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