* lisp/shell.el (shell-dir-cookie-re): New custom variable.
[emacs.git] / lisp / shell.el
blob7771c0d1c4fbb898f9b216e12a1fc57a6427195d
1 ;;; shell.el --- specialized comint.el for running the shell
3 ;; Copyright (C) 1988, 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: Olin Shivers <shivers@cs.cmu.edu>
8 ;; Simon Marshall <simon@gnu.org>
9 ;; Maintainer: FSF <emacs-devel@gnu.org>
10 ;; Keywords: processes
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; This file defines a shell-in-a-buffer package (shell mode) built on
30 ;; top of comint mode. This is actually cmushell with things renamed
31 ;; to replace its counterpart in Emacs 18. cmushell is more
32 ;; featureful, robust, and uniform than the Emacs 18 version.
34 ;; Since this mode is built on top of the general command-interpreter-in-
35 ;; a-buffer mode (comint mode), it shares a common base functionality,
36 ;; and a common set of bindings, with all modes derived from comint mode.
37 ;; This makes these modes easier to use.
39 ;; For documentation on the functionality provided by comint mode, and
40 ;; the hooks available for customising it, see the file comint.el.
41 ;; For further information on shell mode, see the comments below.
43 ;; Needs fixin:
44 ;; When sending text from a source file to a subprocess, the process-mark can
45 ;; move off the window, so you can lose sight of the process interactions.
46 ;; Maybe I should ensure the process mark is in the window when I send
47 ;; text to the process? Switch selectable?
49 ;; YOUR .EMACS FILE
50 ;;=============================================================================
51 ;; Some suggestions for your .emacs file.
53 ;; ;; Define M-# to run some strange command:
54 ;; (eval-after-load "shell"
55 ;; '(define-key shell-mode-map "\M-#" 'shells-dynamic-spell))
57 ;; Brief Command Documentation:
58 ;;============================================================================
59 ;; Comint Mode Commands: (common to shell and all comint-derived modes)
61 ;; m-p comint-previous-input Cycle backwards in input history
62 ;; m-n comint-next-input Cycle forwards
63 ;; m-r comint-previous-matching-input Previous input matching a regexp
64 ;; m-s comint-next-matching-input Next input that matches
65 ;; m-c-l comint-show-output Show last batch of process output
66 ;; return comint-send-input
67 ;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff.
68 ;; c-c c-a comint-bol Beginning of line; skip prompt
69 ;; c-c c-u comint-kill-input ^u
70 ;; c-c c-w backward-kill-word ^w
71 ;; c-c c-c comint-interrupt-subjob ^c
72 ;; c-c c-z comint-stop-subjob ^z
73 ;; c-c c-\ comint-quit-subjob ^\
74 ;; c-c c-o comint-delete-output Delete last batch of process output
75 ;; c-c c-r comint-show-output Show last batch of process output
76 ;; c-c c-l comint-dynamic-list-input-ring List input history
77 ;; send-invisible Read line w/o echo & send to proc
78 ;; comint-continue-subjob Useful if you accidentally suspend
79 ;; top-level job
80 ;; comint-mode-hook is the comint mode hook.
82 ;; Shell Mode Commands:
83 ;; shell Fires up the shell process
84 ;; tab comint-dynamic-complete Complete filename/command/history
85 ;; m-? comint-dynamic-list-filename-completions
86 ;; List completions in help buffer
87 ;; m-c-f shell-forward-command Forward a shell command
88 ;; m-c-b shell-backward-command Backward a shell command
89 ;; dirs Resync the buffer's dir stack
90 ;; shell-dirtrack-mode Turn dir tracking on/off
91 ;; comint-strip-ctrl-m Remove trailing ^Ms from output
93 ;; The shell mode hook is shell-mode-hook
94 ;; comint-prompt-regexp is initialised to shell-prompt-pattern, for backwards
95 ;; compatibility.
97 ;; Read the rest of this file for more information.
99 ;;; Code:
101 (require 'comint)
103 ;;; Customization and Buffer Variables
105 (defgroup shell nil
106 "Running shell from within Emacs buffers."
107 :group 'processes
108 :group 'unix)
110 (defgroup shell-directories nil
111 "Directory support in shell mode."
112 :group 'shell)
114 (defgroup shell-faces nil
115 "Faces in shell buffers."
116 :group 'shell)
118 ;;;###autoload
119 (defcustom shell-dumb-shell-regexp (purecopy "cmd\\(proxy\\)?\\.exe")
120 "Regexp to match shells that don't save their command history, and
121 don't handle the backslash as a quote character. For shells that
122 match this regexp, Emacs will write out the command history when the
123 shell finishes, and won't remove backslashes when it unquotes shell
124 arguments."
125 :type 'regexp
126 :group 'shell)
128 (defcustom shell-prompt-pattern "^[^#$%>\n]*[#$%>] *"
129 "Regexp to match prompts in the inferior shell.
130 Defaults to \"^[^#$%>\\n]*[#$%>] *\", which works pretty well.
131 This variable is used to initialize `comint-prompt-regexp' in the
132 shell buffer.
134 If `comint-use-prompt-regexp' is nil, then this variable is only used
135 to determine paragraph boundaries. See Info node `Shell Prompts' for
136 how Shell mode treats paragraphs.
138 The pattern should probably not match more than one line. If it does,
139 Shell mode may become confused trying to distinguish prompt from input
140 on lines which don't start with a prompt.
142 This is a fine thing to set in your `.emacs' file."
143 :type 'regexp
144 :group 'shell)
146 (defcustom shell-completion-fignore nil
147 "List of suffixes to be disregarded during file/command completion.
148 This variable is used to initialize `comint-completion-fignore' in the shell
149 buffer. The default is nil, for compatibility with most shells.
150 Some people like (\"~\" \"#\" \"%\").
152 This is a fine thing to set in your `.emacs' file."
153 :type '(repeat (string :tag "Suffix"))
154 :group 'shell)
156 (defvar shell-delimiter-argument-list '(?\| ?& ?< ?> ?\( ?\) ?\;)
157 "List of characters to recognize as separate arguments.
158 This variable is used to initialize `comint-delimiter-argument-list' in the
159 shell buffer. The value may depend on the operating system or shell.
161 This is a fine thing to set in your `.emacs' file.")
163 (defvar shell-file-name-chars
164 (if (memq system-type '(ms-dos windows-nt cygwin))
165 "~/A-Za-z0-9_^$!#%&{}@`'.,:()-"
166 "[]~/A-Za-z0-9+@:_.$#%,={}-")
167 "String of characters valid in a file name.
168 This variable is used to initialize `comint-file-name-chars' in the
169 shell buffer. The value may depend on the operating system or shell.
171 This is a fine thing to set in your `.emacs' file.")
173 (defvar shell-file-name-quote-list
174 (if (memq system-type '(ms-dos windows-nt))
176 (append shell-delimiter-argument-list '(?\s ?$ ?\* ?\! ?\" ?\' ?\` ?\# ?\\)))
177 "List of characters to quote when in a file name.
178 This variable is used to initialize `comint-file-name-quote-list' in the
179 shell buffer. The value may depend on the operating system or shell.
181 This is a fine thing to set in your `.emacs' file.")
183 (defvar shell-dynamic-complete-functions
184 '(comint-replace-by-expanded-history
185 shell-dynamic-complete-environment-variable
186 shell-dynamic-complete-command
187 shell-replace-by-expanded-directory
188 shell-dynamic-complete-filename
189 comint-dynamic-complete-filename)
190 "List of functions called to perform completion.
191 This variable is used to initialize `comint-dynamic-complete-functions' in the
192 shell buffer.
194 This is a fine thing to set in your `.emacs' file.")
196 (defcustom shell-command-regexp "[^;&|\n]+"
197 "Regexp to match a single command within a pipeline.
198 This is used for directory tracking and does not do a perfect job."
199 :type 'regexp
200 :group 'shell)
202 (defcustom shell-command-separator-regexp "[;&|\n \t]*"
203 "Regexp to match a single command within a pipeline.
204 This is used for directory tracking and does not do a perfect job."
205 :type 'regexp
206 :group 'shell)
208 (defcustom shell-completion-execonly t
209 "If non-nil, use executable files only for completion candidates.
210 This mirrors the optional behavior of tcsh.
212 Detecting executability of files may slow command completion considerably."
213 :type 'boolean
214 :group 'shell)
216 (defcustom shell-popd-regexp "popd"
217 "Regexp to match subshell commands equivalent to popd."
218 :type 'regexp
219 :group 'shell-directories)
221 (defcustom shell-pushd-regexp "pushd"
222 "Regexp to match subshell commands equivalent to pushd."
223 :type 'regexp
224 :group 'shell-directories)
226 (defcustom shell-pushd-tohome nil
227 "If non-nil, make pushd with no arg behave as \"pushd ~\" (like cd).
228 This mirrors the optional behavior of tcsh."
229 :type 'boolean
230 :group 'shell-directories)
232 (defcustom shell-pushd-dextract nil
233 "If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
234 This mirrors the optional behavior of tcsh."
235 :type 'boolean
236 :group 'shell-directories)
238 (defcustom shell-pushd-dunique nil
239 "If non-nil, make pushd only add unique directories to the stack.
240 This mirrors the optional behavior of tcsh."
241 :type 'boolean
242 :group 'shell-directories)
244 (defcustom shell-cd-regexp "cd"
245 "Regexp to match subshell commands equivalent to cd."
246 :type 'regexp
247 :group 'shell-directories)
249 (defcustom shell-chdrive-regexp
250 (if (memq system-type '(ms-dos windows-nt))
251 ; NetWare allows the five chars between upper and lower alphabetics.
252 "[]a-zA-Z^_`\\[\\\\]:"
253 nil)
254 "If non-nil, is regexp used to track drive changes."
255 :type '(choice regexp
256 (const nil))
257 :group 'shell-directories)
259 (defcustom shell-dirtrack-verbose t
260 "If non-nil, show the directory stack following directory change.
261 This is effective only if directory tracking is enabled.
262 The `dirtrack' package provides an alternative implementation of this feature -
263 see the function `dirtrack-mode'."
264 :type 'boolean
265 :group 'shell-directories)
267 (defcustom explicit-shell-file-name nil
268 "If non-nil, is file name to use for explicitly requested inferior shell."
269 :type '(choice (const :tag "None" nil) file)
270 :group 'shell)
272 ;; Note: There are no explicit references to the variable `explicit-csh-args'.
273 ;; It is used implicitly by M-x shell when the shell is `csh'.
274 (defcustom explicit-csh-args
275 (if (eq system-type 'hpux)
276 ;; -T persuades HP's csh not to think it is smarter
277 ;; than us about what terminal modes to use.
278 '("-i" "-T")
279 '("-i"))
280 "Args passed to inferior shell by \\[shell], if the shell is csh.
281 Value is a list of strings, which may be nil."
282 :type '(repeat (string :tag "Argument"))
283 :group 'shell)
285 ;; Note: There are no explicit references to the variable `explicit-bash-args'.
286 ;; It is used implicitly by M-x shell when the interactive shell is `bash'.
287 (defcustom explicit-bash-args
288 (let* ((prog (or (and (boundp 'explicit-shell-file-name) explicit-shell-file-name)
289 (getenv "ESHELL") shell-file-name))
290 (name (file-name-nondirectory prog)))
291 ;; Tell bash not to use readline, except for bash 1.x which
292 ;; doesn't grook --noediting. Bash 1.x has -nolineediting, but
293 ;; process-send-eof cannot terminate bash if we use it.
294 (if (and (not purify-flag)
295 (equal name "bash")
296 (file-executable-p prog)
297 (string-match "bad option"
298 (shell-command-to-string
299 (concat (shell-quote-argument prog)
300 " --noediting"))))
301 '("-i")
302 '("--noediting" "-i")))
303 "Args passed to inferior shell by \\[shell], if the shell is bash.
304 Value is a list of strings, which may be nil."
305 :type '(repeat (string :tag "Argument"))
306 :group 'shell)
308 (defcustom shell-input-autoexpand 'history
309 "If non-nil, expand input command history references on completion.
310 This mirrors the optional behavior of tcsh (its autoexpand and histlit).
312 If the value is `input', then the expansion is seen on input.
313 If the value is `history', then the expansion is only when inserting
314 into the buffer's input ring. See also `comint-magic-space' and
315 `comint-dynamic-complete'.
317 This variable supplies a default for `comint-input-autoexpand',
318 for Shell mode only."
319 :type '(choice (const :tag "off" nil)
320 (const input)
321 (const history)
322 (const :tag "on" t))
323 :group 'shell)
325 (defvar shell-dirstack nil
326 "List of directories saved by pushd in this buffer's shell.
327 Thus, this does not include the shell's current directory.")
329 (defvar shell-dirtrackp t
330 "Non-nil in a shell buffer means directory tracking is enabled.")
332 (defvar shell-last-dir nil
333 "Keep track of last directory for ksh `cd -' command.")
335 (defvar shell-dirstack-query nil
336 "Command used by `shell-resync-dirs' to query the shell.")
338 (defvar shell-mode-map
339 (let ((map (nconc (make-sparse-keymap) comint-mode-map)))
340 (define-key map "\C-c\C-f" 'shell-forward-command)
341 (define-key map "\C-c\C-b" 'shell-backward-command)
342 (define-key map "\t" 'comint-dynamic-complete)
343 (define-key map (kbd "M-RET") 'shell-resync-dirs)
344 (define-key map "\M-?" 'comint-dynamic-list-filename-completions)
345 (define-key map [menu-bar completion]
346 (cons "Complete"
347 (copy-keymap (lookup-key comint-mode-map [menu-bar completion]))))
348 (define-key-after (lookup-key map [menu-bar completion])
349 [complete-env-variable] '("Complete Env. Variable Name" .
350 shell-dynamic-complete-environment-variable)
351 'complete-file)
352 (define-key-after (lookup-key map [menu-bar completion])
353 [expand-directory] '("Expand Directory Reference" .
354 shell-replace-by-expanded-directory)
355 'complete-expand)
356 map))
358 (defcustom shell-mode-hook '()
359 "Hook for customizing Shell mode."
360 :type 'hook
361 :group 'shell)
363 (defvar shell-font-lock-keywords
364 '(("[ \t]\\([+-][^ \t\n]+\\)" 1 font-lock-comment-face)
365 ("^[^ \t\n]+:.*" . font-lock-string-face)
366 ("^\\[[1-9][0-9]*\\]" . font-lock-string-face))
367 "Additional expressions to highlight in Shell mode.")
369 ;;; Basic Procedures
371 (put 'shell-mode 'mode-class 'special)
373 (define-derived-mode shell-mode comint-mode "Shell"
374 "Major mode for interacting with an inferior shell.\\<shell-mode-map>
375 \\[comint-send-input] after the end of the process' output sends the text from
376 the end of process to the end of the current line.
377 \\[comint-send-input] before end of process output copies the current line minus the prompt to
378 the end of the buffer and sends it (\\[comint-copy-old-input] just copies the current line).
379 \\[send-invisible] reads a line of text without echoing it, and sends it to
380 the shell. This is useful for entering passwords. Or, add the function
381 `comint-watch-for-password-prompt' to `comint-output-filter-functions'.
383 If you want to make multiple shell buffers, rename the `*shell*' buffer
384 using \\[rename-buffer] or \\[rename-uniquely] and start a new shell.
386 If you want to make shell buffers limited in length, add the function
387 `comint-truncate-buffer' to `comint-output-filter-functions'.
389 If you accidentally suspend your process, use \\[comint-continue-subjob]
390 to continue it.
392 `cd', `pushd' and `popd' commands given to the shell are watched by Emacs to
393 keep this buffer's default directory the same as the shell's working directory.
394 While directory tracking is enabled, the shell's working directory is displayed
395 by \\[list-buffers] or \\[mouse-buffer-menu] in the `File' field.
396 \\[dirs] queries the shell and resyncs Emacs' idea of what the current
397 directory stack is.
398 \\[shell-dirtrack-mode] turns directory tracking on and off.
399 \(The `dirtrack' package provides an alternative implementation of this
400 feature - see the function `dirtrack-mode'.)
402 \\{shell-mode-map}
403 Customization: Entry to this mode runs the hooks on `comint-mode-hook' and
404 `shell-mode-hook' (in that order). Before each input, the hooks on
405 `comint-input-filter-functions' are run. After each shell output, the hooks
406 on `comint-output-filter-functions' are run.
408 Variables `shell-cd-regexp', `shell-chdrive-regexp', `shell-pushd-regexp'
409 and `shell-popd-regexp' are used to match their respective commands,
410 while `shell-pushd-tohome', `shell-pushd-dextract' and `shell-pushd-dunique'
411 control the behavior of the relevant command.
413 Variables `comint-completion-autolist', `comint-completion-addsuffix',
414 `comint-completion-recexact' and `comint-completion-fignore' control the
415 behavior of file name, command name and variable name completion. Variable
416 `shell-completion-execonly' controls the behavior of command name completion.
417 Variable `shell-completion-fignore' is used to initialize the value of
418 `comint-completion-fignore'.
420 Variables `comint-input-ring-file-name' and `comint-input-autoexpand' control
421 the initialization of the input ring history, and history expansion.
423 Variables `comint-output-filter-functions', a hook, and
424 `comint-scroll-to-bottom-on-input' and `comint-scroll-to-bottom-on-output'
425 control whether input and output cause the window to scroll to the end of the
426 buffer."
427 (setq comint-prompt-regexp shell-prompt-pattern)
428 (setq comint-completion-fignore shell-completion-fignore)
429 (setq comint-delimiter-argument-list shell-delimiter-argument-list)
430 (setq comint-file-name-chars shell-file-name-chars)
431 (setq comint-file-name-quote-list shell-file-name-quote-list)
432 (set (make-local-variable 'comint-dynamic-complete-functions)
433 shell-dynamic-complete-functions)
434 (set (make-local-variable 'paragraph-separate) "\\'")
435 (make-local-variable 'paragraph-start)
436 (setq paragraph-start comint-prompt-regexp)
437 (make-local-variable 'font-lock-defaults)
438 (setq font-lock-defaults '(shell-font-lock-keywords t))
439 (make-local-variable 'shell-dirstack)
440 (setq shell-dirstack nil)
441 (make-local-variable 'shell-last-dir)
442 (setq shell-last-dir nil)
443 (setq comint-input-autoexpand shell-input-autoexpand)
444 (shell-dirtrack-mode 1)
445 ;; This is not really correct, since the shell buffer does not really
446 ;; edit this directory. But it is useful in the buffer list and menus.
447 (setq list-buffers-directory (expand-file-name default-directory))
448 ;; shell-dependent assignments.
449 (when (ring-empty-p comint-input-ring)
450 (let ((shell (file-name-nondirectory (car
451 (process-command (get-buffer-process (current-buffer)))))))
452 (setq comint-input-ring-file-name
453 (or (getenv "HISTFILE")
454 (cond ((string-equal shell "bash") "~/.bash_history")
455 ((string-equal shell "ksh") "~/.sh_history")
456 (t "~/.history"))))
457 (if (or (equal comint-input-ring-file-name "")
458 (equal (file-truename comint-input-ring-file-name)
459 (file-truename "/dev/null")))
460 (setq comint-input-ring-file-name nil))
461 ;; Arrange to write out the input ring on exit, if the shell doesn't
462 ;; do this itself.
463 (if (and comint-input-ring-file-name
464 (string-match shell-dumb-shell-regexp shell))
465 (set-process-sentinel (get-buffer-process (current-buffer))
466 #'shell-write-history-on-exit))
467 (setq shell-dirstack-query
468 (cond ((string-equal shell "sh") "pwd")
469 ((string-equal shell "ksh") "echo $PWD ~-")
470 (t "dirs")))
471 ;; Bypass a bug in certain versions of bash.
472 (when (string-equal shell "bash")
473 (add-hook 'comint-output-filter-functions
474 'shell-filter-ctrl-a-ctrl-b nil t)))
475 (when shell-dir-cookie-re
476 ;; Watch for magic cookies in the output to track the current dir.
477 (add-hook 'comint-output-filter-functions
478 'shell-dir-cookie-watcher nil t))
479 (comint-read-input-ring t)))
481 (defun shell-filter-ctrl-a-ctrl-b (string)
482 "Remove `^A' and `^B' characters from comint output.
484 Bash uses these characters as internal quoting characters in its
485 prompt. Due to a bug in some bash versions (including 2.03,
486 2.04, and 2.05b), they may erroneously show up when bash is
487 started with the `--noediting' option and Select Graphic
488 Rendition (SGR) control sequences (formerly known as ANSI escape
489 sequences) are used to color the prompt.
491 This function can be put on `comint-output-filter-functions'.
492 The argument STRING is ignored."
493 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
494 (save-excursion
495 (goto-char (or (and (markerp comint-last-output-start)
496 (marker-position comint-last-output-start))
497 (point-min)))
498 (while (re-search-forward "[\C-a\C-b]" pmark t)
499 (replace-match "")))))
501 (defun shell-write-history-on-exit (process event)
502 "Called when the shell process is stopped.
504 Writes the input history to a history file
505 `comint-input-ring-file-name' using `comint-write-input-ring'
506 and inserts a short message in the shell buffer.
508 This function is a sentinel watching the shell interpreter process.
509 Sentinels will always get the two parameters PROCESS and EVENT."
510 ;; Write history.
511 (comint-write-input-ring)
512 (let ((buf (process-buffer process)))
513 (when (buffer-live-p buf)
514 (with-current-buffer buf
515 (insert (format "\nProcess %s %s\n" process event))))))
517 ;;;###autoload
518 (defun shell (&optional buffer)
519 "Run an inferior shell, with I/O through BUFFER (which defaults to `*shell*').
520 Interactively, a prefix arg means to prompt for BUFFER.
521 If `default-directory' is a remote file name, it is also prompted
522 to change if called with a prefix arg.
524 If BUFFER exists but shell process is not running, make new shell.
525 If BUFFER exists and shell process is running, just switch to BUFFER.
526 Program used comes from variable `explicit-shell-file-name',
527 or (if that is nil) from the ESHELL environment variable,
528 or (if that is nil) from `shell-file-name'.
529 If a file `~/.emacs_SHELLNAME' exists, or `~/.emacs.d/init_SHELLNAME.sh',
530 it is given as initial input (but this may be lost, due to a timing
531 error, if the shell discards input when it starts up).
532 The buffer is put in Shell mode, giving commands for sending input
533 and controlling the subjobs of the shell. See `shell-mode'.
534 See also the variable `shell-prompt-pattern'.
536 To specify a coding system for converting non-ASCII characters
537 in the input and output to the shell, use \\[universal-coding-system-argument]
538 before \\[shell]. You can also specify this with \\[set-buffer-process-coding-system]
539 in the shell buffer, after you start the shell.
540 The default comes from `process-coding-system-alist' and
541 `default-process-coding-system'.
543 The shell file name (sans directories) is used to make a symbol name
544 such as `explicit-csh-args'. If that symbol is a variable,
545 its value is used as a list of arguments when invoking the shell.
546 Otherwise, one argument `-i' is passed to the shell.
548 \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
549 (interactive
550 (list
551 (and current-prefix-arg
552 (prog1
553 (read-buffer "Shell buffer: "
554 (generate-new-buffer-name "*shell*"))
555 (if (file-remote-p default-directory)
556 ;; It must be possible to declare a local default-directory.
557 (setq default-directory
558 (expand-file-name
559 (read-file-name
560 "Default directory: " default-directory default-directory
561 t nil 'file-directory-p))))))))
562 (require 'ansi-color)
563 (setq buffer (get-buffer-create (or buffer "*shell*")))
564 ;; Pop to buffer, so that the buffer's window will be correctly set
565 ;; when we call comint (so that comint sets the COLUMNS env var properly).
566 (pop-to-buffer buffer)
567 (unless (comint-check-proc buffer)
568 (let* ((prog (or explicit-shell-file-name
569 (getenv "ESHELL") shell-file-name))
570 (name (file-name-nondirectory prog))
571 (startfile (concat "~/.emacs_" name))
572 (xargs-name (intern-soft (concat "explicit-" name "-args"))))
573 (unless (file-exists-p startfile)
574 (setq startfile (concat user-emacs-directory "init_" name ".sh")))
575 (apply 'make-comint-in-buffer "shell" buffer prog
576 (if (file-exists-p startfile) startfile)
577 (if (and xargs-name (boundp xargs-name))
578 (symbol-value xargs-name)
579 '("-i")))
580 (shell-mode)))
581 buffer)
583 ;; Don't do this when shell.el is loaded, only while dumping.
584 ;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*shell*"))
586 ;;; Directory tracking
588 ;; This code provides the shell mode input sentinel
589 ;; SHELL-DIRECTORY-TRACKER
590 ;; that tracks cd, pushd, and popd commands issued to the shell, and
591 ;; changes the current directory of the shell buffer accordingly.
593 ;; This is basically a fragile hack, although it's more accurate than
594 ;; the version in Emacs 18's shell.el. It has the following failings:
595 ;; 1. It doesn't know about the cdpath shell variable.
596 ;; 2. It cannot infallibly deal with command sequences, though it does well
597 ;; with these and with ignoring commands forked in another shell with ()s.
598 ;; 3. More generally, any complex command is going to throw it. Otherwise,
599 ;; you'd have to build an entire shell interpreter in Emacs Lisp. Failing
600 ;; that, there's no way to catch shell commands where cd's are buried
601 ;; inside conditional expressions, aliases, and so forth.
603 ;; The whole approach is a crock. Shell aliases mess it up. File sourcing
604 ;; messes it up. You run other processes under the shell; these each have
605 ;; separate working directories, and some have commands for manipulating
606 ;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have
607 ;; commands that do *not* affect the current w.d. at all, but look like they
608 ;; do (e.g., the cd command in ftp). In shells that allow you job
609 ;; control, you can switch between jobs, all having different w.d.'s. So
610 ;; simply saying %3 can shift your w.d..
612 ;; The solution is to relax, not stress out about it, and settle for
613 ;; a hack that works pretty well in typical circumstances. Remember
614 ;; that a half-assed solution is more in keeping with the spirit of Unix,
615 ;; anyway. Blech.
617 ;; One good hack not implemented here for users of programmable shells
618 ;; is to program up the shell w.d. manipulation commands to output
619 ;; a coded command sequence to the tty. Something like
620 ;; ESC | <cwd> |
621 ;; where <cwd> is the new current working directory. Then trash the
622 ;; directory tracking machinery currently used in this package, and
623 ;; replace it with a process filter that watches for and strips out
624 ;; these messages.
626 (defcustom shell-dir-cookie-re nil
627 "Regexp matching your prompt, including some part of the current directory.
628 If your prompt includes the current directory or the last few elements of it,
629 set this to a pattern that matches your prompt and whose subgroup 1 matches
630 the directory part of it.
631 This is used by `shell-dir-cookie-watcher' to try and use this info
632 to track your current directory. It can be used instead of or in addition
633 to `dirtrack-mode'."
634 :type '(choice (const nil) regexp))
636 (defun shell-dir-cookie-watcher (text)
637 ;; This is fragile: the TEXT could be split into several chunks and we'd
638 ;; miss it. Oh well. It's a best effort anyway. I'd expect that it's
639 ;; rather unusual to have the prompt split into several packets, but
640 ;; I'm sure Murphy will prove me wrong.
641 (when (and shell-dir-cookie-re (string-match shell-dir-cookie-re text))
642 (let ((dir (match-string 1 text)))
643 (cond
644 ((file-name-absolute-p dir) (shell-cd dir))
645 ;; Let's try and see if it seems to be up or down from where we were.
646 ((string-match "\\`\\(.*\\)\\(?:/.*\\)?\n\\(.*/\\)\\1\\(?:/.*\\)?\\'"
647 (setq text (concat dir "\n" default-directory)))
648 (shell-cd (concat (match-string 2 text) dir)))))))
651 (defun shell-directory-tracker (str)
652 "Tracks cd, pushd and popd commands issued to the shell.
653 This function is called on each input passed to the shell.
654 It watches for cd, pushd and popd commands and sets the buffer's
655 default directory to track these commands.
657 You may toggle this tracking on and off with \\[shell-dirtrack-mode].
658 If Emacs gets confused, you can resync with the shell with \\[dirs].
659 \(The `dirtrack' package provides an alternative implementation of this
660 feature - see the function `dirtrack-mode'.)
662 See variables `shell-cd-regexp', `shell-chdrive-regexp', `shell-pushd-regexp',
663 and `shell-popd-regexp', while `shell-pushd-tohome', `shell-pushd-dextract',
664 and `shell-pushd-dunique' control the behavior of the relevant command.
666 Environment variables are expanded, see function `substitute-in-file-name'."
667 (if shell-dirtrackp
668 ;; We fail gracefully if we think the command will fail in the shell.
669 (condition-case chdir-failure
670 (let ((start (progn (string-match
671 (concat "^" shell-command-separator-regexp)
672 str) ; skip whitespace
673 (match-end 0)))
674 end cmd arg1)
675 (while (string-match shell-command-regexp str start)
676 (setq end (match-end 0)
677 cmd (comint-arguments (substring str start end) 0 0)
678 arg1 (comint-arguments (substring str start end) 1 1))
679 (if arg1
680 (setq arg1 (shell-unquote-argument arg1)))
681 (cond ((string-match (concat "\\`\\(" shell-popd-regexp
682 "\\)\\($\\|[ \t]\\)")
683 cmd)
684 (shell-process-popd (comint-substitute-in-file-name arg1)))
685 ((string-match (concat "\\`\\(" shell-pushd-regexp
686 "\\)\\($\\|[ \t]\\)")
687 cmd)
688 (shell-process-pushd (comint-substitute-in-file-name arg1)))
689 ((string-match (concat "\\`\\(" shell-cd-regexp
690 "\\)\\($\\|[ \t]\\)")
691 cmd)
692 (shell-process-cd (comint-substitute-in-file-name arg1)))
693 ((and shell-chdrive-regexp
694 (string-match (concat "\\`\\(" shell-chdrive-regexp
695 "\\)\\($\\|[ \t]\\)")
696 cmd))
697 (shell-process-cd (comint-substitute-in-file-name cmd))))
698 (setq start (progn (string-match shell-command-separator-regexp
699 str end)
700 ;; skip again
701 (match-end 0)))))
702 (error "Couldn't cd"))))
704 (defun shell-unquote-argument (string)
705 "Remove all kinds of shell quoting from STRING."
706 (save-match-data
707 (let ((idx 0) next inside
708 (quote-chars
709 (if (string-match shell-dumb-shell-regexp
710 (file-name-nondirectory
711 (car (process-command (get-buffer-process (current-buffer))))))
712 "['`\"]"
713 "[\\'`\"]")))
714 (while (and (< idx (length string))
715 (setq next (string-match quote-chars string next)))
716 (cond ((= (aref string next) ?\\)
717 (setq string (replace-match "" nil nil string))
718 (setq next (1+ next)))
719 ((and inside (= (aref string next) inside))
720 (setq string (replace-match "" nil nil string))
721 (setq inside nil))
722 (inside
723 (setq next (1+ next)))
725 (setq inside (aref string next))
726 (setq string (replace-match "" nil nil string)))))
727 string)))
729 ;; popd [+n]
730 (defun shell-process-popd (arg)
731 (let ((num (or (shell-extract-num arg) 0)))
732 (cond ((and num (= num 0) shell-dirstack)
733 (shell-cd (shell-prefixed-directory-name (car shell-dirstack)))
734 (setq shell-dirstack (cdr shell-dirstack))
735 (shell-dirstack-message))
736 ((and num (> num 0) (<= num (length shell-dirstack)))
737 (let* ((ds (cons nil shell-dirstack))
738 (cell (nthcdr (1- num) ds)))
739 (rplacd cell (cdr (cdr cell)))
740 (setq shell-dirstack (cdr ds))
741 (shell-dirstack-message)))
743 (error "Couldn't popd")))))
745 ;; Return DIR prefixed with comint-file-name-prefix as appropriate.
746 (defun shell-prefixed-directory-name (dir)
747 (if (= (length comint-file-name-prefix) 0)
749 (if (file-name-absolute-p dir)
750 ;; The name is absolute, so prepend the prefix.
751 (concat comint-file-name-prefix dir)
752 ;; For relative name we assume default-directory already has the prefix.
753 (expand-file-name dir))))
755 ;; cd [dir]
756 (defun shell-process-cd (arg)
757 (let ((new-dir (cond ((zerop (length arg)) (concat comint-file-name-prefix
758 "~"))
759 ((string-equal "-" arg) shell-last-dir)
760 (t (shell-prefixed-directory-name arg)))))
761 (setq shell-last-dir default-directory)
762 (shell-cd new-dir)
763 (shell-dirstack-message)))
765 ;; pushd [+n | dir]
766 (defun shell-process-pushd (arg)
767 (let ((num (shell-extract-num arg)))
768 (cond ((zerop (length arg))
769 ;; no arg -- swap pwd and car of stack unless shell-pushd-tohome
770 (cond (shell-pushd-tohome
771 (shell-process-pushd (concat comint-file-name-prefix "~")))
772 (shell-dirstack
773 (let ((old default-directory))
774 (shell-cd (car shell-dirstack))
775 (setq shell-dirstack (cons old (cdr shell-dirstack)))
776 (shell-dirstack-message)))
778 (message "Directory stack empty."))))
779 ((numberp num)
780 ;; pushd +n
781 (cond ((> num (length shell-dirstack))
782 (message "Directory stack not that deep."))
783 ((= num 0)
784 (error (message "Couldn't cd")))
785 (shell-pushd-dextract
786 (let ((dir (nth (1- num) shell-dirstack)))
787 (shell-process-popd arg)
788 (shell-process-pushd default-directory)
789 (shell-cd dir)
790 (shell-dirstack-message)))
792 (let* ((ds (cons default-directory shell-dirstack))
793 (dslen (length ds))
794 (front (nthcdr num ds))
795 (back (reverse (nthcdr (- dslen num) (reverse ds))))
796 (new-ds (append front back)))
797 (shell-cd (car new-ds))
798 (setq shell-dirstack (cdr new-ds))
799 (shell-dirstack-message)))))
801 ;; pushd <dir>
802 (let ((old-wd default-directory))
803 (shell-cd (shell-prefixed-directory-name arg))
804 (if (or (null shell-pushd-dunique)
805 (not (member old-wd shell-dirstack)))
806 (setq shell-dirstack (cons old-wd shell-dirstack)))
807 (shell-dirstack-message))))))
809 ;; If STR is of the form +n, for n>0, return n. Otherwise, nil.
810 (defun shell-extract-num (str)
811 (and (string-match "^\\+[1-9][0-9]*$" str)
812 (string-to-number str)))
814 (defvaralias 'shell-dirtrack-mode 'shell-dirtrackp)
815 (define-minor-mode shell-dirtrack-mode
816 "Turn directory tracking on and off in a shell buffer.
817 The `dirtrack' package provides an alternative implementation of this
818 feature - see the function `dirtrack-mode'."
819 nil nil nil
820 (setq list-buffers-directory (if shell-dirtrack-mode default-directory))
821 (if shell-dirtrack-mode
822 (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t)
823 (remove-hook 'comint-input-filter-functions 'shell-directory-tracker t)))
825 (define-obsolete-function-alias 'shell-dirtrack-toggle 'shell-dirtrack-mode
826 "23.1")
828 (defun shell-cd (dir)
829 "Do normal `cd' to DIR, and set `list-buffers-directory'."
830 (cd dir)
831 (if shell-dirtrackp
832 (setq list-buffers-directory default-directory)))
834 (defun shell-resync-dirs ()
835 "Resync the buffer's idea of the current directory stack.
836 This command queries the shell with the command bound to
837 `shell-dirstack-query' (default \"dirs\"), reads the next
838 line output and parses it to form the new directory stack.
839 DON'T issue this command unless the buffer is at a shell prompt.
840 Also, note that if some other subprocess decides to do output
841 immediately after the query, its output will be taken as the
842 new directory stack -- you lose. If this happens, just do the
843 command again."
844 (interactive)
845 (let* ((proc (get-buffer-process (current-buffer)))
846 (pmark (process-mark proc))
847 (started-at-pmark (= (point) (marker-position pmark))))
848 (save-excursion
849 (goto-char pmark)
850 ;; If the process echoes commands, don't insert a fake command in
851 ;; the buffer or it will appear twice.
852 (unless comint-process-echoes
853 (insert shell-dirstack-query) (insert "\n"))
854 (sit-for 0) ; force redisplay
855 (comint-send-string proc shell-dirstack-query)
856 (comint-send-string proc "\n")
857 (set-marker pmark (point))
858 (let ((pt (point))
859 (regexp
860 (concat
861 (if comint-process-echoes
862 ;; Skip command echo if the process echoes
863 (concat "\\(" (regexp-quote shell-dirstack-query) "\n\\)")
864 "\\(\\)")
865 "\\(.+\n\\)")))
866 ;; This extra newline prevents the user's pending input from spoofing us.
867 (insert "\n") (backward-char 1)
868 ;; Wait for one line.
869 (while (not (looking-at regexp))
870 (accept-process-output proc)
871 (goto-char pt)))
872 (goto-char pmark) (delete-char 1) ; remove the extra newline
873 ;; That's the dirlist. grab it & parse it.
874 (let* ((dl (buffer-substring (match-beginning 2) (1- (match-end 2))))
875 (dl-len (length dl))
876 (ds '()) ; new dir stack
877 (i 0))
878 (while (< i dl-len)
879 ;; regexp = optional whitespace, (non-whitespace), optional whitespace
880 (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir
881 (setq ds (cons (concat comint-file-name-prefix
882 (substring dl (match-beginning 1)
883 (match-end 1)))
884 ds))
885 (setq i (match-end 0)))
886 (let ((ds (nreverse ds)))
887 (condition-case nil
888 (progn (shell-cd (car ds))
889 (setq shell-dirstack (cdr ds)
890 shell-last-dir (car shell-dirstack))
891 (shell-dirstack-message))
892 (error (message "Couldn't cd"))))))
893 (if started-at-pmark (goto-char (marker-position pmark)))))
895 ;; For your typing convenience:
896 (defalias 'dirs 'shell-resync-dirs)
899 ;; Show the current dirstack on the message line.
900 ;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo".
901 ;; (This isn't necessary if the dirlisting is generated with a simple "dirs".)
902 ;; All the commands that mung the buffer's dirstack finish by calling
903 ;; this guy.
904 (defun shell-dirstack-message ()
905 (when shell-dirtrack-verbose
906 (let* ((msg "")
907 (ds (cons default-directory shell-dirstack))
908 (home (expand-file-name (concat comint-file-name-prefix "~/")))
909 (homelen (length home)))
910 (while ds
911 (let ((dir (car ds)))
912 (and (>= (length dir) homelen)
913 (string= home (substring dir 0 homelen))
914 (setq dir (concat "~/" (substring dir homelen))))
915 ;; Strip off comint-file-name-prefix if present.
916 (and comint-file-name-prefix
917 (>= (length dir) (length comint-file-name-prefix))
918 (string= comint-file-name-prefix
919 (substring dir 0 (length comint-file-name-prefix)))
920 (setq dir (substring dir (length comint-file-name-prefix)))
921 (setcar ds dir))
922 (setq msg (concat msg (directory-file-name dir) " "))
923 (setq ds (cdr ds))))
924 (message "%s" msg))))
926 ;; This was mostly copied from shell-resync-dirs.
927 (defun shell-snarf-envar (var)
928 "Return as a string the shell's value of environment variable VAR."
929 (let* ((cmd (format "printenv '%s'\n" var))
930 (proc (get-buffer-process (current-buffer)))
931 (pmark (process-mark proc)))
932 (goto-char pmark)
933 (insert cmd)
934 (sit-for 0) ; force redisplay
935 (comint-send-string proc cmd)
936 (set-marker pmark (point))
937 (let ((pt (point))) ; wait for 1 line
938 ;; This extra newline prevents the user's pending input from spoofing us.
939 (insert "\n") (backward-char 1)
940 (while (not (looking-at ".+\n"))
941 (accept-process-output proc)
942 (goto-char pt)))
943 (goto-char pmark) (delete-char 1) ; remove the extra newline
944 (buffer-substring (match-beginning 0) (1- (match-end 0)))))
946 (defun shell-copy-environment-variable (variable)
947 "Copy the environment variable VARIABLE from the subshell to Emacs.
948 This command reads the value of the specified environment variable
949 in the shell, and sets the same environment variable in Emacs
950 \(what `getenv' in Emacs would return) to that value.
951 That value will affect any new subprocesses that you subsequently start
952 from Emacs."
953 (interactive (list (read-envvar-name "\
954 Copy Shell environment variable to Emacs: ")))
955 (setenv variable (shell-snarf-envar variable)))
957 (defun shell-forward-command (&optional arg)
958 "Move forward across ARG shell command(s). Does not cross lines.
959 See `shell-command-regexp'."
960 (interactive "p")
961 (let ((limit (line-end-position)))
962 (if (re-search-forward (concat shell-command-regexp "\\([;&|][\t ]*\\)+")
963 limit 'move arg)
964 (skip-syntax-backward " "))))
967 (defun shell-backward-command (&optional arg)
968 "Move backward across ARG shell command(s). Does not cross lines.
969 See `shell-command-regexp'."
970 (interactive "p")
971 (let ((limit (save-excursion (comint-bol nil) (point))))
972 (when (> limit (point))
973 (setq limit (line-beginning-position)))
974 (skip-syntax-backward " " limit)
975 (if (re-search-backward
976 (format "[;&|]+[\t ]*\\(%s\\)" shell-command-regexp) limit 'move arg)
977 (progn (goto-char (match-beginning 1))
978 (skip-chars-forward ";&|")))))
980 (defun shell-dynamic-complete-command ()
981 "Dynamically complete the command at point.
982 This function is similar to `comint-dynamic-complete-filename', except that it
983 searches `exec-path' (minus the trailing Emacs library path) for completion
984 candidates. Note that this may not be the same as the shell's idea of the
985 path.
987 Completion is dependent on the value of `shell-completion-execonly', plus
988 those that effect file completion. See `shell-dynamic-complete-as-command'.
990 Returns t if successful."
991 (interactive)
992 (let ((filename (comint-match-partial-filename)))
993 (if (and filename
994 (save-match-data (not (string-match "[~/]" filename)))
995 (eq (match-beginning 0)
996 (save-excursion (shell-backward-command 1) (point))))
997 (prog2 (unless (window-minibuffer-p (selected-window))
998 (message "Completing command name..."))
999 (shell-dynamic-complete-as-command)))))
1002 (defun shell-dynamic-complete-as-command ()
1003 "Dynamically complete at point as a command.
1004 See `shell-dynamic-complete-filename'. Returns t if successful."
1005 (let* ((filename (or (comint-match-partial-filename) ""))
1006 (filenondir (file-name-nondirectory filename))
1007 (path-dirs (cdr (reverse exec-path)))
1008 (cwd (file-name-as-directory (expand-file-name default-directory)))
1009 (ignored-extensions
1010 (and comint-completion-fignore
1011 (mapconcat (function (lambda (x) (concat (regexp-quote x) "$")))
1012 comint-completion-fignore "\\|")))
1013 (dir "") (comps-in-dir ())
1014 (file "") (abs-file-name "") (completions ()))
1015 ;; Go thru each dir in the search path, finding completions.
1016 (while path-dirs
1017 (setq dir (file-name-as-directory (comint-directory (or (car path-dirs) ".")))
1018 comps-in-dir (and (file-accessible-directory-p dir)
1019 (file-name-all-completions filenondir dir)))
1020 ;; Go thru each completion found, to see whether it should be used.
1021 (while comps-in-dir
1022 (setq file (car comps-in-dir)
1023 abs-file-name (concat dir file))
1024 (if (and (not (member file completions))
1025 (not (and ignored-extensions
1026 (string-match ignored-extensions file)))
1027 (or (string-equal dir cwd)
1028 (not (file-directory-p abs-file-name)))
1029 (or (null shell-completion-execonly)
1030 (file-executable-p abs-file-name)))
1031 (setq completions (cons file completions)))
1032 (setq comps-in-dir (cdr comps-in-dir)))
1033 (setq path-dirs (cdr path-dirs)))
1034 ;; OK, we've got a list of completions.
1035 (let ((success (let ((comint-completion-addsuffix nil))
1036 (comint-dynamic-simple-complete filenondir completions))))
1037 (if (and (memq success '(sole shortest)) comint-completion-addsuffix
1038 (not (file-directory-p (comint-match-partial-filename))))
1039 (insert " "))
1040 success)))
1042 (defun shell-dynamic-complete-filename ()
1043 "Dynamically complete the filename at point.
1044 This completes only if point is at a suitable position for a
1045 filename argument."
1046 (interactive)
1047 (let ((opoint (point))
1048 (beg (comint-line-beginning-position)))
1049 (when (save-excursion
1050 (goto-char (if (re-search-backward "[;|&]" beg t)
1051 (match-end 0)
1052 beg))
1053 (re-search-forward "[^ \t][ \t]" opoint t))
1054 (comint-dynamic-complete-as-filename))))
1056 (defun shell-match-partial-variable ()
1057 "Return the shell variable at point, or nil if none is found."
1058 (save-excursion
1059 (let ((limit (point)))
1060 (if (re-search-backward "[^A-Za-z0-9_{}]" nil 'move)
1061 (or (looking-at "\\$") (forward-char 1)))
1062 ;; Anchor the search forwards.
1063 (if (or (eolp) (looking-at "[^A-Za-z0-9_{}$]"))
1065 (re-search-forward "\\$?{?[A-Za-z0-9_]*}?" limit)
1066 (buffer-substring (match-beginning 0) (match-end 0))))))
1068 (defun shell-dynamic-complete-environment-variable ()
1069 "Dynamically complete the environment variable at point.
1070 Completes if after a variable, i.e., if it starts with a \"$\".
1071 See `shell-dynamic-complete-as-environment-variable'.
1073 This function is similar to `comint-dynamic-complete-filename', except that it
1074 searches `process-environment' for completion candidates. Note that this may
1075 not be the same as the interpreter's idea of variable names. The main problem
1076 with this type of completion is that `process-environment' is the environment
1077 which Emacs started with. Emacs does not track changes to the environment made
1078 by the interpreter. Perhaps it would be more accurate if this function was
1079 called `shell-dynamic-complete-process-environment-variable'.
1081 Returns non-nil if successful."
1082 (interactive)
1083 (let ((variable (shell-match-partial-variable)))
1084 (if (and variable (string-match "^\\$" variable))
1085 (prog2 (unless (window-minibuffer-p (selected-window))
1086 (message "Completing variable name..."))
1087 (shell-dynamic-complete-as-environment-variable)))))
1090 (defun shell-dynamic-complete-as-environment-variable ()
1091 "Dynamically complete at point as an environment variable.
1092 Used by `shell-dynamic-complete-environment-variable'.
1093 Uses `comint-dynamic-simple-complete'."
1094 (let* ((var (or (shell-match-partial-variable) ""))
1095 (variable (substring var (or (string-match "[^$({]\\|$" var) 0)))
1096 (variables (mapcar (function (lambda (x)
1097 (substring x 0 (string-match "=" x))))
1098 process-environment))
1099 (addsuffix comint-completion-addsuffix)
1100 (comint-completion-addsuffix nil)
1101 (success (comint-dynamic-simple-complete variable variables)))
1102 (if (memq success '(sole shortest))
1103 (let* ((var (shell-match-partial-variable))
1104 (variable (substring var (string-match "[^$({]" var)))
1105 (protection (cond ((string-match "{" var) "}")
1106 ((string-match "(" var) ")")
1107 (t "")))
1108 (suffix (cond ((null addsuffix) "")
1109 ((file-directory-p
1110 (comint-directory (getenv variable))) "/")
1111 (t " "))))
1112 (insert protection suffix)))
1113 success))
1116 (defun shell-replace-by-expanded-directory ()
1117 "Expand directory stack reference before point.
1118 Directory stack references are of the form \"=digit\" or \"=-\".
1119 See `default-directory' and `shell-dirstack'.
1121 Returns t if successful."
1122 (interactive)
1123 (if (comint-match-partial-filename)
1124 (save-excursion
1125 (goto-char (match-beginning 0))
1126 (let ((stack (cons default-directory shell-dirstack))
1127 (index (cond ((looking-at "=-/?")
1128 (length shell-dirstack))
1129 ((looking-at "=\\([0-9]+\\)/?")
1130 (string-to-number
1131 (buffer-substring
1132 (match-beginning 1) (match-end 1)))))))
1133 (cond ((null index)
1134 nil)
1135 ((>= index (length stack))
1136 (error "Directory stack not that deep"))
1138 (replace-match (file-name-as-directory (nth index stack)) t t)
1139 (message "Directory item: %d" index)
1140 t))))))
1142 (provide 'shell)
1144 ;;; shell.el ends here