Commit | Line | Data |
---|---|---|
5336e829 | 1 | ;;; shell.el --- specialized comint.el for running the shell. |
24fdffaa | 2 | ;;; Copyright (C) 1988, 1993 Free Software Foundation, Inc. |
6d74b528 | 3 | |
630cc463 | 4 | ;; Author: Olin Shivers <shivers@cs.cmu.edu> |
d7b4d18f | 5 | ;; Keywords: processes |
630cc463 | 6 | |
c88ab9ce ER |
7 | ;;; This file is part of GNU Emacs. |
8 | ||
9 | ;;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 | ;;; it under the terms of the GNU General Public License as published by | |
e5167999 | 11 | ;;; the Free Software Foundation; either version 2, or (at your option) |
c88ab9ce ER |
12 | ;;; any later version. |
13 | ||
14 | ;;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;;; GNU General Public License for more details. | |
18 | ||
19 | ;;; You should have received a copy of the GNU General Public License | |
20 | ;;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 | ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
be9b65ac | 22 | |
630cc463 ER |
23 | ;;; Commentary: |
24 | ||
a9ec2adb | 25 | ;;; The changelog is at the end of file. |
be9b65ac | 26 | |
a9ec2adb JB |
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) | |
be9b65ac | 30 | |
a9ec2adb | 31 | ;;; This file defines a a shell-in-a-buffer package (shell mode) built |
c88ab9ce ER |
32 | ;;; on top of comint mode. This is actually cmushell with things |
33 | ;;; renamed to replace its counterpart in Emacs 18. cmushell is more | |
34 | ;;; featureful, robust, and uniform than the Emacs 18 version. | |
be9b65ac DL |
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. | |
a9ec2adb | 39 | ;;; This makes these modes easier to use. |
be9b65ac DL |
40 | |
41 | ;;; For documentation on the functionality provided by comint mode, and | |
42 | ;;; the hooks available for customising it, see the file comint.el. | |
a9ec2adb | 43 | ;;; For further information on shell mode, see the comments below. |
be9b65ac DL |
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 | ||
a9ec2adb JB |
51 | ;; YOUR .EMACS FILE |
52 | ;;============================================================================= | |
53 | ;; Some suggestions for your .emacs file. | |
54 | ;; | |
fa8f1b25 | 55 | ;; ; If shell lives in some non-standard directory, you must tell emacs |
a9ec2adb JB |
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 | ;; | |
fa8f1b25 ER |
59 | ;; ; Autoload shell from file shell.el |
60 | ;; (autoload 'shell "shell" | |
a9ec2adb JB |
61 | ;; "Run an inferior shell process." |
62 | ;; t) | |
63 | ;; | |
fa8f1b25 ER |
64 | ;; ; Define C-c t to run my favorite command in shell mode: |
65 | ;; (setq shell-load-hook | |
a9ec2adb | 66 | ;; '((lambda () |
fa8f1b25 | 67 | ;; (define-key shell-mode-map "\C-ct" 'favorite-cmd)))) |
a9ec2adb JB |
68 | |
69 | \f | |
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 | \f | |
c88ab9ce ER |
106 | ;;; SHELL.EL COMPATIBILITY |
107 | ;;; Notes from when this was called cmushell, and was not the standard emacs | |
108 | ;;; shell package. | |
a9ec2adb JB |
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 | |
c88ab9ce | 114 | ;;; names, however, so you can customise shell mode differently from cmushell |
a9ec2adb JB |
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 | \f | |
8f95f0ce | 138 | ;;; Customization and Buffer Variables |
a9ec2adb JB |
139 | ;;; =========================================================================== |
140 | ;;; | |
141 | ||
630cc463 ER |
142 | ;;; Code: |
143 | ||
144 | (require 'comint) | |
145 | ||
c88ab9ce | 146 | ;;;###autoload |
24fdffaa | 147 | (defconst shell-prompt-pattern "^[^#$%>]*[#$%>] *" |
a9ec2adb JB |
148 | "Regexp to match prompts in the inferior shell. |
149 | Defaults to \"^[^#$%>]*[#$%>] *\", which works pretty well. | |
24fdffaa | 150 | This variable is used to initialise `comint-prompt-regexp' in the |
a9ec2adb JB |
151 | shell buffer. |
152 | ||
24fdffaa | 153 | This is a fine thing to set in your `.emacs' file.") |
a9ec2adb | 154 | |
be9b65ac DL |
155 | (defvar shell-popd-regexp "popd" |
156 | "*Regexp to match subshell commands equivalent to popd.") | |
157 | ||
158 | (defvar shell-pushd-regexp "pushd" | |
159 | "*Regexp to match subshell commands equivalent to pushd.") | |
160 | ||
161 | (defvar shell-cd-regexp "cd" | |
162 | "*Regexp to match subshell commands equivalent to cd.") | |
163 | ||
164 | (defvar explicit-shell-file-name nil | |
165 | "*If non-nil, is file name to use for explicitly requested inferior shell.") | |
166 | ||
167 | (defvar explicit-csh-args | |
168 | (if (eq system-type 'hpux) | |
169 | ;; -T persuades HP's csh not to think it is smarter | |
170 | ;; than us about what terminal modes to use. | |
171 | '("-i" "-T") | |
172 | '("-i")) | |
173 | "*Args passed to inferior shell by M-x shell, if the shell is csh. | |
174 | Value is a list of strings, which may be nil.") | |
175 | ||
c88ab9ce | 176 | ;;; All the above vars aren't prefixed "cmushell-" to make them |
a9ec2adb JB |
177 | ;;; backwards compatible w/shell.el and old .emacs files. |
178 | ||
be9b65ac | 179 | (defvar shell-dirstack nil |
c88ab9ce ER |
180 | "List of directories saved by pushd in this buffer's shell. |
181 | Thus, this does not include the shell's current directory.") | |
be9b65ac | 182 | |
fa8f1b25 ER |
183 | (defvar shell-last-dir nil |
184 | "Keep track of last directory for ksh `cd -' command.") | |
185 | ||
be9b65ac | 186 | (defvar shell-dirstack-query "dirs" |
24fdffaa | 187 | "Command used by `shell-resync-dir' to query the shell.") |
be9b65ac | 188 | |
a9ec2adb | 189 | (defvar shell-mode-map '()) |
be9b65ac | 190 | (cond ((not shell-mode-map) |
a9ec2adb | 191 | (setq shell-mode-map (full-copy-sparse-keymap comint-mode-map)) |
be9b65ac DL |
192 | (define-key shell-mode-map "\t" 'comint-dynamic-complete) |
193 | (define-key shell-mode-map "\M-?" 'comint-dynamic-list-completions))) | |
194 | ||
195 | (defvar shell-mode-hook '() | |
24fdffaa | 196 | "*Hook for customising Shell mode.") |
be9b65ac DL |
197 | |
198 | \f | |
199 | ;;; Basic Procedures | |
200 | ;;; =========================================================================== | |
201 | ;;; | |
202 | ||
203 | (defun shell-mode () | |
204 | "Major mode for interacting with an inferior shell. | |
205 | Return after the end of the process' output sends the text from the | |
206 | end of process to the end of the current line. | |
c88ab9ce ER |
207 | Return before end of process output copies the current line (except |
208 | for the prompt) to the end of the buffer and sends it. | |
be9b65ac | 209 | M-x send-invisible reads a line of text without echoing it, and sends it to |
c88ab9ce | 210 | the shell. This is useful for entering passwords. |
be9b65ac DL |
211 | |
212 | If you accidentally suspend your process, use \\[comint-continue-subjob] | |
213 | to continue it. | |
214 | ||
215 | cd, pushd and popd commands given to the shell are watched by Emacs to keep | |
216 | this buffer's default directory the same as the shell's working directory. | |
217 | M-x dirs queries the shell and resyncs Emacs' idea of what the current | |
218 | directory stack is. | |
219 | M-x dirtrack-toggle turns directory tracking on and off. | |
220 | ||
221 | \\{shell-mode-map} | |
24fdffaa RS |
222 | Customization: Entry to this mode runs the hooks on `comint-mode-hook' and |
223 | `shell-mode-hook' (in that order). | |
be9b65ac | 224 | |
24fdffaa RS |
225 | Variables `shell-cd-regexp', `shell-pushd-regexp' and `shell-popd-regexp' |
226 | are used to match their respective commands." | |
be9b65ac DL |
227 | (interactive) |
228 | (comint-mode) | |
a9ec2adb JB |
229 | (setq comint-prompt-regexp shell-prompt-pattern) |
230 | (setq major-mode 'shell-mode) | |
bd5201e3 | 231 | (setq mode-name "Shell") |
be9b65ac DL |
232 | (use-local-map shell-mode-map) |
233 | (make-local-variable 'shell-dirstack) | |
a9ec2adb | 234 | (setq shell-dirstack nil) |
fa8f1b25 | 235 | (setq shell-last-dir nil) |
a9ec2adb JB |
236 | (make-local-variable 'shell-dirtrackp) |
237 | (setq shell-dirtrackp t) | |
238 | (setq comint-input-sentinel 'shell-directory-tracker) | |
be9b65ac DL |
239 | (run-hooks 'shell-mode-hook)) |
240 | ||
241 | \f | |
c88ab9ce | 242 | ;;;###autoload |
be9b65ac DL |
243 | (defun shell () |
244 | "Run an inferior shell, with I/O through buffer *shell*. | |
245 | If buffer exists but shell process is not running, make new shell. | |
a9ec2adb | 246 | If buffer exists and shell process is running, |
fbc270e7 | 247 | just switch to buffer `*shell*'. |
24fdffaa | 248 | Program used comes from variable `explicit-shell-file-name', |
a9ec2adb JB |
249 | or (if that is nil) from the ESHELL environment variable, |
250 | or else from SHELL if there is no ESHELL. | |
fbc270e7 | 251 | If a file `~/.emacs_SHELLNAME' exists, it is given as initial input |
a9ec2adb JB |
252 | (Note that this may lose due to a timing error if the shell |
253 | discards input when it starts up.) | |
fbc270e7 RS |
254 | The buffer is put in Shell mode, giving commands for sending input |
255 | and controlling the subjobs of the shell. See `shell-mode'. | |
256 | See also the variable `shell-prompt-pattern'. | |
be9b65ac | 257 | |
a9ec2adb | 258 | The shell file name (sans directories) is used to make a symbol name |
62c9fad7 | 259 | such as `explicit-csh-args'. If that symbol is a variable, |
be9b65ac DL |
260 | its value is used as a list of arguments when invoking the shell. |
261 | Otherwise, one argument `-i' is passed to the shell. | |
262 | ||
263 | \(Type \\[describe-mode] in the shell buffer for a list of commands.)" | |
264 | (interactive) | |
265 | (cond ((not (comint-check-proc "*shell*")) | |
266 | (let* ((prog (or explicit-shell-file-name | |
267 | (getenv "ESHELL") | |
268 | (getenv "SHELL") | |
a9ec2adb | 269 | "/bin/sh")) |
be9b65ac DL |
270 | (name (file-name-nondirectory prog)) |
271 | (startfile (concat "~/.emacs_" name)) | |
272 | (xargs-name (intern-soft (concat "explicit-" name "-args")))) | |
273 | (set-buffer (apply 'make-comint "shell" prog | |
274 | (if (file-exists-p startfile) startfile) | |
275 | (if (and xargs-name (boundp xargs-name)) | |
276 | (symbol-value xargs-name) | |
277 | '("-i")))) | |
278 | (shell-mode)))) | |
279 | (switch-to-buffer "*shell*")) | |
280 | ||
281 | \f | |
282 | ;;; Directory tracking | |
283 | ;;; =========================================================================== | |
284 | ;;; This code provides the shell mode input sentinel | |
285 | ;;; SHELL-DIRECTORY-TRACKER | |
286 | ;;; that tracks cd, pushd, and popd commands issued to the shell, and | |
287 | ;;; changes the current directory of the shell buffer accordingly. | |
288 | ;;; | |
289 | ;;; This is basically a fragile hack, although it's more accurate than | |
b7fc702e | 290 | ;;; the version in Emacs 18's shell.el. It has the following failings: |
be9b65ac DL |
291 | ;;; 1. It doesn't know about the cdpath shell variable. |
292 | ;;; 2. It only spots the first command in a command sequence. E.g., it will | |
293 | ;;; miss the cd in "ls; cd foo" | |
294 | ;;; 3. More generally, any complex command (like ";" sequencing) is going to | |
295 | ;;; throw it. Otherwise, you'd have to build an entire shell interpreter in | |
296 | ;;; emacs lisp. Failing that, there's no way to catch shell commands where | |
297 | ;;; cd's are buried inside conditional expressions, aliases, and so forth. | |
298 | ;;; | |
299 | ;;; The whole approach is a crock. Shell aliases mess it up. File sourcing | |
300 | ;;; messes it up. You run other processes under the shell; these each have | |
301 | ;;; separate working directories, and some have commands for manipulating | |
302 | ;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have | |
a9ec2adb | 303 | ;;; commands that do *not* affect the current w.d. at all, but look like they |
be9b65ac DL |
304 | ;;; do (e.g., the cd command in ftp). In shells that allow you job |
305 | ;;; control, you can switch between jobs, all having different w.d.'s. So | |
306 | ;;; simply saying %3 can shift your w.d.. | |
307 | ;;; | |
308 | ;;; The solution is to relax, not stress out about it, and settle for | |
309 | ;;; a hack that works pretty well in typical circumstances. Remember | |
310 | ;;; that a half-assed solution is more in keeping with the spirit of Unix, | |
311 | ;;; anyway. Blech. | |
312 | ;;; | |
313 | ;;; One good hack not implemented here for users of programmable shells | |
314 | ;;; is to program up the shell w.d. manipulation commands to output | |
315 | ;;; a coded command sequence to the tty. Something like | |
316 | ;;; ESC | <cwd> | | |
317 | ;;; where <cwd> is the new current working directory. Then trash the | |
318 | ;;; directory tracking machinery currently used in this package, and | |
319 | ;;; replace it with a process filter that watches for and strips out | |
320 | ;;; these messages. | |
321 | ||
322 | ;;; REGEXP is a regular expression. STR is a string. START is a fixnum. | |
323 | ;;; Returns T if REGEXP matches STR where the match is anchored to start | |
324 | ;;; at position START in STR. Sort of like LOOKING-AT for strings. | |
325 | (defun shell-front-match (regexp str start) | |
326 | (eq start (string-match regexp str start))) | |
327 | ||
328 | (defun shell-directory-tracker (str) | |
329 | "Tracks cd, pushd and popd commands issued to the shell. | |
330 | This function is called on each input passed to the shell. | |
331 | It watches for cd, pushd and popd commands and sets the buffer's | |
332 | default directory to track these commands. | |
333 | ||
334 | You may toggle this tracking on and off with M-x dirtrack-toggle. | |
335 | If emacs gets confused, you can resync with the shell with M-x dirs. | |
336 | ||
24fdffaa RS |
337 | See variables `shell-cd-regexp', `shell-pushd-regexp', and `shell-popd-regexp'. |
338 | Environment variables are expanded, see function `substitute-in-file-name'." | |
be9b65ac DL |
339 | (condition-case err |
340 | (cond (shell-dirtrackp | |
341 | (string-match "^\\s *" str) ; skip whitespace | |
342 | (let ((bos (match-end 0)) | |
343 | (x nil)) | |
344 | (cond ((setq x (shell-match-cmd-w/optional-arg shell-popd-regexp | |
345 | str bos)) | |
346 | (shell-process-popd (substitute-in-file-name x))) | |
347 | ((setq x (shell-match-cmd-w/optional-arg shell-pushd-regexp | |
348 | str bos)) | |
349 | (shell-process-pushd (substitute-in-file-name x))) | |
350 | ((setq x (shell-match-cmd-w/optional-arg shell-cd-regexp | |
351 | str bos)) | |
352 | (shell-process-cd (substitute-in-file-name x))))))) | |
353 | (error (message (car (cdr err)))))) | |
354 | ||
355 | ||
356 | ;;; Try to match regexp CMD to string, anchored at position START. | |
357 | ;;; CMD may be followed by a single argument. If a match, then return | |
358 | ;;; the argument, if there is one, or the empty string if not. If | |
359 | ;;; no match, return nil. | |
360 | ||
361 | (defun shell-match-cmd-w/optional-arg (cmd str start) | |
362 | (and (shell-front-match cmd str start) | |
363 | (let ((eoc (match-end 0))) ; end of command | |
364 | (cond ((shell-front-match "\\s *\\(\;\\|$\\)" str eoc) | |
365 | "") ; no arg | |
366 | ((shell-front-match "\\s +\\([^ \t\;]+\\)\\s *\\(\;\\|$\\)" | |
367 | str eoc) | |
368 | (substring str (match-beginning 1) (match-end 1))) ; arg | |
369 | (t nil))))) ; something else. | |
370 | ;;; The first regexp is [optional whitespace, (";" or the end of string)]. | |
371 | ;;; The second regexp is [whitespace, (an arg), optional whitespace, | |
372 | ;;; (";" or end of string)]. | |
373 | ||
374 | ||
375 | ;;; popd [+n] | |
376 | (defun shell-process-popd (arg) | |
377 | (let ((num (if (zerop (length arg)) 0 ; no arg means +0 | |
378 | (shell-extract-num arg)))) | |
379 | (if (and num (< num (length shell-dirstack))) | |
380 | (if (= num 0) ; condition-case because the CD could lose. | |
381 | (condition-case nil (progn (cd (car shell-dirstack)) | |
382 | (setq shell-dirstack | |
383 | (cdr shell-dirstack)) | |
384 | (shell-dirstack-message)) | |
385 | (error (message "Couldn't cd."))) | |
386 | (let* ((ds (cons nil shell-dirstack)) | |
387 | (cell (nthcdr (- num 1) ds))) | |
388 | (rplacd cell (cdr (cdr cell))) | |
389 | (setq shell-dirstack (cdr ds)) | |
390 | (shell-dirstack-message))) | |
391 | (message "Bad popd.")))) | |
392 | ||
393 | ||
394 | ;;; cd [dir] | |
395 | (defun shell-process-cd (arg) | |
fa8f1b25 ER |
396 | (condition-case nil |
397 | (let ((new-dir (cond | |
398 | ((zerop (length arg)) (getenv "HOME")) | |
399 | ((string-equal "-" arg) shell-last-dir) | |
400 | (t arg)))) | |
401 | (setq shell-last-dir default-directory) | |
402 | (cd new-dir) | |
403 | (shell-dirstack-message)) | |
404 | (error (message "Couldn't cd.")))) | |
be9b65ac DL |
405 | |
406 | ;;; pushd [+n | dir] | |
407 | (defun shell-process-pushd (arg) | |
408 | (if (zerop (length arg)) | |
409 | ;; no arg -- swap pwd and car of shell stack | |
410 | (condition-case nil (if shell-dirstack | |
411 | (let ((old default-directory)) | |
412 | (cd (car shell-dirstack)) | |
413 | (setq shell-dirstack | |
414 | (cons old (cdr shell-dirstack))) | |
415 | (shell-dirstack-message)) | |
416 | (message "Directory stack empty.")) | |
417 | (message "Couldn't cd.")) | |
418 | ||
419 | (let ((num (shell-extract-num arg))) | |
420 | (if num ; pushd +n | |
421 | (if (> num (length shell-dirstack)) | |
422 | (message "Directory stack not that deep.") | |
423 | (let* ((ds (cons default-directory shell-dirstack)) | |
424 | (dslen (length ds)) | |
425 | (front (nthcdr num ds)) | |
426 | (back (reverse (nthcdr (- dslen num) (reverse ds)))) | |
427 | (new-ds (append front back))) | |
428 | (condition-case nil | |
429 | (progn (cd (car new-ds)) | |
430 | (setq shell-dirstack (cdr new-ds)) | |
431 | (shell-dirstack-message)) | |
432 | (error (message "Couldn't cd."))))) | |
433 | ||
434 | ;; pushd <dir> | |
435 | (let ((old-wd default-directory)) | |
436 | (condition-case nil | |
437 | (progn (cd arg) | |
438 | (setq shell-dirstack | |
439 | (cons old-wd shell-dirstack)) | |
440 | (shell-dirstack-message)) | |
441 | (error (message "Couldn't cd.")))))))) | |
442 | ||
443 | ;; If STR is of the form +n, for n>0, return n. Otherwise, nil. | |
444 | (defun shell-extract-num (str) | |
445 | (and (string-match "^\\+[1-9][0-9]*$" str) | |
446 | (string-to-int str))) | |
447 | ||
448 | ||
449 | (defun shell-dirtrack-toggle () | |
450 | "Turn directory tracking on and off in a shell buffer." | |
451 | (interactive) | |
452 | (setq shell-dirtrackp (not shell-dirtrackp)) | |
453 | (message "directory tracking %s." | |
454 | (if shell-dirtrackp "ON" "OFF"))) | |
455 | ||
456 | ;;; For your typing convenience: | |
31e1d920 | 457 | (defalias 'dirtrack-toggle 'shell-dirtrack-toggle) |
be9b65ac DL |
458 | |
459 | ||
460 | (defun shell-resync-dirs () | |
461 | "Resync the buffer's idea of the current directory stack. | |
462 | This command queries the shell with the command bound to | |
24fdffaa | 463 | `shell-dirstack-query' (default \"dirs\"), reads the next |
be9b65ac DL |
464 | line output and parses it to form the new directory stack. |
465 | DON'T issue this command unless the buffer is at a shell prompt. | |
466 | Also, note that if some other subprocess decides to do output | |
467 | immediately after the query, its output will be taken as the | |
468 | new directory stack -- you lose. If this happens, just do the | |
469 | command again." | |
470 | (interactive) | |
471 | (let* ((proc (get-buffer-process (current-buffer))) | |
472 | (pmark (process-mark proc))) | |
473 | (goto-char pmark) | |
474 | (insert shell-dirstack-query) (insert "\n") | |
475 | (sit-for 0) ; force redisplay | |
476 | (comint-send-string proc shell-dirstack-query) | |
477 | (comint-send-string proc "\n") | |
478 | (set-marker pmark (point)) | |
479 | (let ((pt (point))) ; wait for 1 line | |
480 | ;; This extra newline prevents the user's pending input from spoofing us. | |
481 | (insert "\n") (backward-char 1) | |
482 | (while (not (looking-at ".+\n")) | |
483 | (accept-process-output proc) | |
484 | (goto-char pt))) | |
485 | (goto-char pmark) (delete-char 1) ; remove the extra newline | |
486 | ;; That's the dirlist. grab it & parse it. | |
487 | (let* ((dl (buffer-substring (match-beginning 0) (- (match-end 0) 1))) | |
488 | (dl-len (length dl)) | |
489 | (ds '()) ; new dir stack | |
490 | (i 0)) | |
491 | (while (< i dl-len) | |
492 | ;; regexp = optional whitespace, (non-whitespace), optional whitespace | |
493 | (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir | |
494 | (setq ds (cons (substring dl (match-beginning 1) (match-end 1)) | |
495 | ds)) | |
496 | (setq i (match-end 0))) | |
497 | (let ((ds (reverse ds))) | |
498 | (condition-case nil | |
499 | (progn (cd (car ds)) | |
500 | (setq shell-dirstack (cdr ds)) | |
501 | (shell-dirstack-message)) | |
502 | (error (message "Couldn't cd."))))))) | |
503 | ||
504 | ;;; For your typing convenience: | |
31e1d920 | 505 | (defalias 'dirs 'shell-resync-dirs) |
be9b65ac DL |
506 | |
507 | ||
508 | ;;; Show the current dirstack on the message line. | |
509 | ;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo". | |
510 | ;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".) | |
511 | ;;; All the commands that mung the buffer's dirstack finish by calling | |
512 | ;;; this guy. | |
513 | (defun shell-dirstack-message () | |
514 | (let ((msg "") | |
515 | (ds (cons default-directory shell-dirstack))) | |
516 | (while ds | |
517 | (let ((dir (car ds))) | |
518 | (if (string-match (format "^%s\\(/\\|$\\)" (getenv "HOME")) dir) | |
519 | (setq dir (concat "~/" (substring dir (match-end 0))))) | |
520 | (if (string-equal dir "~/") (setq dir "~")) | |
521 | (setq msg (concat msg dir " ")) | |
522 | (setq ds (cdr ds)))) | |
523 | (message msg))) | |
a9ec2adb JB |
524 | |
525 | ||
526 | \f | |
527 | ;;; Interfacing to client packages (and converting them) | |
c88ab9ce ER |
528 | ;;; Notes from when this was called cmushell, and was not the standard emacs |
529 | ;;; shell package. Many of the conversions discussed here have been done. | |
a9ec2adb JB |
530 | ;;;============================================================================ |
531 | ;;; Several gnu packages (tex-mode, background, dbx, gdb, kermit, prolog, | |
532 | ;;; telnet are some) use the shell package as clients. Most of them would | |
533 | ;;; be better off using the comint package directly, but they predate it. | |
534 | ;;; The catch is that most of these packages (dbx, gdb, prolog, telnet) | |
535 | ;;; assume total knowledge of all the local variables that shell mode | |
536 | ;;; functions depend on. So they (kill-all-local-variables), then create | |
537 | ;;; the few local variables that shell.el functions depend on. Alas, | |
538 | ;;; cmushell.el functions depend on a different set of vars (for example, | |
539 | ;;; the input history ring is a local variable in cmushell.el's shell mode, | |
540 | ;;; whereas there is no input history ring in shell.el's shell mode). | |
541 | ;;; So we have a situation where the greater functionality of cmushell.el | |
542 | ;;; is biting us -- you can't just replace shell will cmushell. | |
543 | ;;; | |
544 | ;;; Altering these packages to use comint mode directly should *greatly* | |
545 | ;;; improve their functionality, and is actually pretty easy. It's | |
546 | ;;; mostly a matter of renaming a few variable names. See comint.el for more. | |
547 | ;;; -Olin | |
548 | ||
549 | ||
550 | ||
8f95f0ce | 551 | ;;; Do the user's customization... |
a9ec2adb JB |
552 | ;;;=============================== |
553 | (defvar shell-load-hook nil | |
554 | "This hook is run when shell is loaded in. | |
555 | This is a good place to put keybindings.") | |
556 | ||
557 | (run-hooks 'shell-load-hook) | |
558 | ||
c88ab9ce ER |
559 | (provide 'shell) |
560 | ||
561 | ;;; shell.el ends here |