(Info-build-node-completions): Make Info-current-file-completions buffer local.
[bpt/emacs.git] / lisp / shell.el
CommitLineData
5336e829 1;;; shell.el --- specialized comint.el for running the shell.
5109bfb0 2
fd1035aa 3;; Copyright (C) 1988, 93, 94, 95, 96, 1997, 2000 Free Software Foundation, Inc.
6d74b528 4
f8604035 5;; Author: Olin Shivers <shivers@cs.cmu.edu> then
5762abec 6;; Simon Marshall <simon@gnu.org>
f8604035 7;; Maintainer: FSF
d7b4d18f 8;; Keywords: processes
630cc463 9
5109bfb0 10;; This file is part of GNU Emacs.
c88ab9ce 11
5109bfb0
KH
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
c88ab9ce 16
5109bfb0
KH
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
c88ab9ce 21
5109bfb0 22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
be9b65ac 26
630cc463
ER
27;;; Commentary:
28
b578f267
EN
29;; Please send me bug reports, bug fixes, and extensions, so that I can
30;; merge them into the master source.
31;; - Olin Shivers (shivers@cs.cmu.edu)
5762abec 32;; - Simon Marshall (simon@gnu.org)
be9b65ac 33
b578f267
EN
34;; This file defines a a shell-in-a-buffer package (shell mode) built
35;; on top of comint mode. This is actually cmushell with things
36;; renamed to replace its counterpart in Emacs 18. cmushell is more
37;; featureful, robust, and uniform than the Emacs 18 version.
be9b65ac 38
b578f267 39;; Since this mode is built on top of the general command-interpreter-in-
84728570 40;; a-buffer mode (comint mode), it shares a common base functionality,
b578f267
EN
41;; and a common set of bindings, with all modes derived from comint mode.
42;; This makes these modes easier to use.
be9b65ac 43
b578f267
EN
44;; For documentation on the functionality provided by comint mode, and
45;; the hooks available for customising it, see the file comint.el.
46;; For further information on shell mode, see the comments below.
be9b65ac 47
b578f267 48;; Needs fixin:
84728570 49;; When sending text from a source file to a subprocess, the process-mark can
b578f267
EN
50;; move off the window, so you can lose sight of the process interactions.
51;; Maybe I should ensure the process mark is in the window when I send
52;; text to the process? Switch selectable?
be9b65ac 53
a9ec2adb
JB
54;; YOUR .EMACS FILE
55;;=============================================================================
56;; Some suggestions for your .emacs file.
57;;
0b3c9cf1
SM
58;; ;; Define M-# to run some strange command:
59;; (eval-after-load "shell"
60;; '(define-key shell-mode-map "\M-#" 'shells-dynamic-spell))
84728570 61
b578f267
EN
62;; Brief Command Documentation:
63;;============================================================================
64;; Comint Mode Commands: (common to shell and all comint-derived modes)
65;;
a9e73449
RS
66;; m-p comint-previous-input Cycle backwards in input history
67;; m-n comint-next-input Cycle forwards
b578f267
EN
68;; m-r comint-previous-matching-input Previous input matching a regexp
69;; m-s comint-next-matching-input Next input that matches
a9e73449 70;; m-c-l comint-show-output Show last batch of process output
b578f267 71;; return comint-send-input
a9e73449 72;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff.
b578f267 73;; c-c c-a comint-bol Beginning of line; skip prompt
a9e73449
RS
74;; c-c c-u comint-kill-input ^u
75;; c-c c-w backward-kill-word ^w
76;; c-c c-c comint-interrupt-subjob ^c
77;; c-c c-z comint-stop-subjob ^z
78;; c-c c-\ comint-quit-subjob ^\
79;; c-c c-o comint-kill-output Delete last batch of process output
80;; c-c c-r comint-show-output Show last batch of process output
94c34f7a 81;; c-c c-l comint-dynamic-list-input-ring List input history
b578f267 82;; send-invisible Read line w/o echo & send to proc
a9e73449 83;; comint-continue-subjob Useful if you accidentally suspend
b578f267
EN
84;; top-level job
85;; comint-mode-hook is the comint mode hook.
86
87;; Shell Mode Commands:
88;; shell Fires up the shell process
89;; tab comint-dynamic-complete Complete filename/command/history
90;; m-? comint-dynamic-list-filename-completions
91;; List completions in help buffer
92;; m-c-f shell-forward-command Forward a shell command
93;; m-c-b shell-backward-command Backward a shell command
a9e73449
RS
94;; dirs Resync the buffer's dir stack
95;; dirtrack-mode Turn dir tracking on/off
b578f267
EN
96;; comint-strip-ctrl-m Remove trailing ^Ms from output
97;;
98;; The shell mode hook is shell-mode-hook
99;; comint-prompt-regexp is initialised to shell-prompt-pattern, for backwards
100;; compatibility.
a9ec2adb 101
b578f267 102;; Read the rest of this file for more information.
84728570 103
630cc463
ER
104;;; Code:
105
106(require 'comint)
107
b578f267
EN
108;;; Customization and Buffer Variables
109
ed366e87
RS
110(defgroup shell nil
111 "Running shell from within Emacs buffers"
112 :group 'processes
113 :group 'unix)
114
115(defgroup shell-directories nil
116 "Directory support in shell mode"
117 :group 'shell)
118
119(defgroup shell-faces nil
120 "Faces in shell buffers"
121 :group 'shell)
122
c88ab9ce 123;;;###autoload
4dced927 124(defcustom shell-dumb-shell-regexp "cmd\\(proxy\\)?\\.exe"
f5058b96
EZ
125 "Regexp to match shells that don't save their command history, and
126don't handle the backslash as a quote character. For shells that
127match this regexp, Emacs will write out the command history when the
128shell finishes, and won't remove backslashes when it unquotes shell
129arguments."
4dced927
AI
130 :type 'regexp
131 :group 'shell)
132
fad22e2e 133(defcustom shell-prompt-pattern "^[^#$%>\n]*[#$%>] *"
a9ec2adb 134 "Regexp to match prompts in the inferior shell.
ac75ef20 135Defaults to \"^[^#$%>\\n]*[#$%>] *\", which works pretty well.
84728570 136This variable is used to initialise `comint-prompt-regexp' in the
a9ec2adb
JB
137shell buffer.
138
28bc09d6
MB
139This variable is only used if the variable
140`comint-use-prompt-regexp-instead-of-fields' is non-nil.
141
ac75ef20 142The pattern should probably not match more than one line. If it does,
221ca406 143Shell mode may become confused trying to distinguish prompt from input
ac75ef20
JB
144on lines which don't start with a prompt.
145
fad22e2e
RS
146This is a fine thing to set in your `.emacs' file."
147 :type 'regexp
148 :group 'shell)
a9ec2adb 149
ed366e87 150(defcustom shell-completion-fignore nil
e11284d5
RS
151 "*List of suffixes to be disregarded during file/command completion.
152This variable is used to initialize `comint-completion-fignore' in the shell
153buffer. The default is nil, for compatibility with most shells.
154Some people like (\"~\" \"#\" \"%\").
155
ed366e87
RS
156This is a fine thing to set in your `.emacs' file."
157 :type '(repeat (string :tag "Suffix"))
158 :group 'shell)
e11284d5 159
56eb1060 160(defvar shell-delimiter-argument-list '(?\| ?& ?< ?> ?\( ?\) ?\;)
d76b71af 161 "List of characters to recognise as separate arguments.
56eb1060 162This variable is used to initialize `comint-delimiter-argument-list' in the
39d1a2ce
SM
163shell buffer. The value may depend on the operating system or shell.
164
165This is a fine thing to set in your `.emacs' file.")
166
76f63ea0 167(defvar shell-file-name-chars
39d1a2ce 168 (if (memq system-type '(ms-dos windows-nt))
4aed0379 169 "~/A-Za-z0-9_^$!#%&{}@`'.,:()-"
39d1a2ce 170 "~/A-Za-z0-9+@:_.$#%,={}-")
76f63ea0
RS
171 "String of characters valid in a file name.
172This variable is used to initialize `comint-file-name-chars' in the
39d1a2ce 173shell buffer. The value may depend on the operating system or shell.
d76b71af
RS
174
175This is a fine thing to set in your `.emacs' file.")
176
7b4c6503 177(defvar shell-file-name-quote-list
39d1a2ce
SM
178 (if (memq system-type '(ms-dos windows-nt))
179 nil
ad3f2354 180 (append shell-delimiter-argument-list '(?\ ?\* ?\! ?\" ?\' ?\` ?\#)))
7b4c6503
SM
181 "List of characters to quote when in a file name.
182This variable is used to initialize `comint-file-name-quote-list' in the
39d1a2ce 183shell buffer. The value may depend on the operating system or shell.
7b4c6503
SM
184
185This is a fine thing to set in your `.emacs' file.")
186
988a4d60
RS
187(defvar shell-dynamic-complete-functions
188 '(comint-replace-by-expanded-history
189 shell-dynamic-complete-environment-variable
190 shell-dynamic-complete-command
191 shell-replace-by-expanded-directory
192 comint-dynamic-complete-filename)
193 "List of functions called to perform completion.
194This variable is used to initialise `comint-dynamic-complete-functions' in the
195shell buffer.
196
197This is a fine thing to set in your `.emacs' file.")
198
ed366e87 199(defcustom shell-command-regexp "[^;&|\n]+"
a8e0effb 200 "*Regexp to match a single command within a pipeline.
ed366e87
RS
201This is used for directory tracking and does not do a perfect job."
202 :type 'regexp
203 :group 'shell)
d76b71af 204
ed366e87 205(defcustom shell-completion-execonly t
d76b71af 206 "*If non-nil, use executable files only for completion candidates.
5394cb44 207This mirrors the optional behavior of tcsh.
d76b71af 208
ed366e87
RS
209Detecting executability of files may slow command completion considerably."
210 :type 'boolean
211 :group 'shell)
d76b71af 212
ed366e87
RS
213(defcustom shell-popd-regexp "popd"
214 "*Regexp to match subshell commands equivalent to popd."
215 :type 'regexp
216 :group 'shell-directories)
be9b65ac 217
ed366e87
RS
218(defcustom shell-pushd-regexp "pushd"
219 "*Regexp to match subshell commands equivalent to pushd."
220 :type 'regexp
221 :group 'shell-directories)
be9b65ac 222
ed366e87 223(defcustom shell-pushd-tohome nil
d76b71af 224 "*If non-nil, make pushd with no arg behave as \"pushd ~\" (like cd).
ed366e87
RS
225This mirrors the optional behavior of tcsh."
226 :type 'boolean
227 :group 'shell-directories)
d76b71af 228
ed366e87 229(defcustom shell-pushd-dextract nil
d76b71af 230 "*If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
ed366e87
RS
231This mirrors the optional behavior of tcsh."
232 :type 'boolean
233 :group 'shell-directories)
d76b71af 234
ed366e87 235(defcustom shell-pushd-dunique nil
d76b71af 236 "*If non-nil, make pushd only add unique directories to the stack.
ed366e87
RS
237This mirrors the optional behavior of tcsh."
238 :type 'boolean
239 :group 'shell-directories)
d76b71af 240
ed366e87
RS
241(defcustom shell-cd-regexp "cd"
242 "*Regexp to match subshell commands equivalent to cd."
243 :type 'regexp
244 :group 'shell-directories)
be9b65ac 245
ed366e87 246(defcustom shell-chdrive-regexp
84728570 247 (if (memq system-type '(ms-dos windows-nt))
32d01212
RS
248 ; NetWare allows the five chars between upper and lower alphabetics.
249 "[]a-zA-Z^_`\\[\\\\]:"
250 nil)
ed366e87 251 "*If non-nil, is regexp used to track drive changes."
233f3fb8
RS
252 :type '(choice regexp
253 (const nil))
ed366e87 254 :group 'shell-directories)
32d01212 255
8a6387ed
SM
256(defcustom shell-dirtrack-verbose t
257 "*If non-nil, show the directory stack following directory change.
258This is effective only if directory tracking is enabled."
259 :type 'boolean
260 :group 'shell-directories)
261
ed366e87
RS
262(defcustom explicit-shell-file-name nil
263 "*If non-nil, is file name to use for explicitly requested inferior shell."
264 :type '(choice (const :tag "None" nil) file)
265 :group 'shell)
be9b65ac 266
ed366e87 267(defcustom explicit-csh-args
be9b65ac
DL
268 (if (eq system-type 'hpux)
269 ;; -T persuades HP's csh not to think it is smarter
270 ;; than us about what terminal modes to use.
271 '("-i" "-T")
272 '("-i"))
273 "*Args passed to inferior shell by M-x shell, if the shell is csh.
ed366e87
RS
274Value is a list of strings, which may be nil."
275 :type '(repeat (string :tag "Argument"))
276 :group 'shell)
be9b65ac 277
ed366e87 278(defcustom shell-input-autoexpand 'history
4f99443b
RS
279 "*If non-nil, expand input command history references on completion.
280This mirrors the optional behavior of tcsh (its autoexpand and histlit).
281
282If the value is `input', then the expansion is seen on input.
283If the value is `history', then the expansion is only when inserting
284into the buffer's input ring. See also `comint-magic-space' and
285`comint-dynamic-complete'.
286
287This variable supplies a default for `comint-input-autoexpand',
ed366e87 288for Shell mode only."
6f0984e5
AS
289 :type '(choice (const :tag "off" nil)
290 (const input)
291 (const history)
292 (const :tag "on" t))
362ae0b3 293 :group 'shell)
4f99443b 294
be9b65ac 295(defvar shell-dirstack nil
c88ab9ce
ER
296 "List of directories saved by pushd in this buffer's shell.
297Thus, this does not include the shell's current directory.")
be9b65ac 298
b493a9b2
RS
299(defvar shell-dirtrackp t
300 "Non-nil in a shell buffer means directory tracking is enabled.")
301
fa8f1b25
ER
302(defvar shell-last-dir nil
303 "Keep track of last directory for ksh `cd -' command.")
304
c63e83a4 305(defvar shell-dirstack-query nil
24fdffaa 306 "Command used by `shell-resync-dir' to query the shell.")
be9b65ac 307
9376442d 308(defvar shell-mode-map nil)
be9b65ac 309(cond ((not shell-mode-map)
1ed30e75 310 (setq shell-mode-map (nconc (make-sparse-keymap) comint-mode-map))
d76b71af
RS
311 (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
312 (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
be9b65ac 313 (define-key shell-mode-map "\t" 'comint-dynamic-complete)
d76b71af 314 (define-key shell-mode-map "\M-?"
988a4d60 315 'comint-dynamic-list-filename-completions)
1ed30e75
RS
316 (define-key shell-mode-map [menu-bar completion]
317 (copy-keymap (lookup-key comint-mode-map [menu-bar completion])))
5109bfb0
KH
318 (define-key-after (lookup-key shell-mode-map [menu-bar completion])
319 [complete-env-variable] '("Complete Env. Variable Name" .
320 shell-dynamic-complete-environment-variable)
321 'complete-file)
322 (define-key-after (lookup-key shell-mode-map [menu-bar completion])
323 [expand-directory] '("Expand Directory Reference" .
324 shell-replace-by-expanded-directory)
325 'complete-expand)))
be9b65ac 326
ed366e87
RS
327(defcustom shell-mode-hook '()
328 "*Hook for customising Shell mode."
329 :type 'hook
330 :group 'shell)
be9b65ac 331
5945a1f7 332(defvar shell-font-lock-keywords
cb6e07b1 333 '(("[ \t]\\([+-][^ \t\n]+\\)" 1 font-lock-comment-face)
473fbd89
SM
334 ("^[^ \t\n]+:.*" . font-lock-string-face)
335 ("^\\[[1-9][0-9]*\\]" . font-lock-string-face))
5945a1f7 336 "Additional expressions to highlight in Shell mode.")
84728570 337
be9b65ac 338;;; Basic Procedures
be9b65ac 339
e9904b59
RS
340(put 'shell-mode 'mode-class 'special)
341
9f299ecb 342(define-derived-mode shell-mode comint-mode "Shell"
be9b65ac 343 "Major mode for interacting with an inferior shell.
8079b590
SM
344\\[comint-send-input] after the end of the process' output sends the text from
345 the end of process to the end of the current line.
346\\[comint-send-input] before end of process output copies the current line minus the prompt to
347 the end of the buffer and sends it (\\[comint-copy-old-input] just copies the current line).
e9933cb1 348\\[send-invisible] reads a line of text without echoing it, and sends it to
56783585
RS
349 the shell. This is useful for entering passwords. Or, add the function
350 `comint-watch-for-password-prompt' to `comint-output-filter-functions'.
be9b65ac 351
e9933cb1
SM
352If you want to make multiple shell buffers, rename the `*shell*' buffer
353using \\[rename-buffer] or \\[rename-uniquely] and start a new shell.
354
8079b590 355If you want to make shell buffers limited in length, add the function
4841acc4 356`comint-truncate-buffer' to `comint-output-filter-functions'.
8079b590 357
be9b65ac
DL
358If you accidentally suspend your process, use \\[comint-continue-subjob]
359to continue it.
360
e9933cb1
SM
361`cd', `pushd' and `popd' commands given to the shell are watched by Emacs to
362keep this buffer's default directory the same as the shell's working directory.
0b3c9cf1
SM
363While directory tracking is enabled, the shell's working directory is displayed
364by \\[list-buffers] or \\[mouse-buffer-menu] in the `File' field.
84728570 365\\[dirs] queries the shell and resyncs Emacs' idea of what the current
be9b65ac 366 directory stack is.
2154db49 367\\[dirtrack-mode] turns directory tracking on and off.
be9b65ac
DL
368
369\\{shell-mode-map}
24fdffaa 370Customization: Entry to this mode runs the hooks on `comint-mode-hook' and
988a4d60 371`shell-mode-hook' (in that order). Before each input, the hooks on
ba02c167
RS
372`comint-input-filter-functions' are run. After each shell output, the hooks
373on `comint-output-filter-functions' are run.
be9b65ac 374
84728570
SS
375Variables `shell-cd-regexp', `shell-chdrive-regexp', `shell-pushd-regexp'
376and `shell-popd-regexp' are used to match their respective commands,
377while `shell-pushd-tohome', `shell-pushd-dextract' and `shell-pushd-dunique'
32d01212 378control the behavior of the relevant command.
d76b71af 379
e11284d5
RS
380Variables `comint-completion-autolist', `comint-completion-addsuffix',
381`comint-completion-recexact' and `comint-completion-fignore' control the
382behavior of file name, command name and variable name completion. Variable
383`shell-completion-execonly' controls the behavior of command name completion.
384Variable `shell-completion-fignore' is used to initialise the value of
385`comint-completion-fignore'.
d76b71af
RS
386
387Variables `comint-input-ring-file-name' and `comint-input-autoexpand' control
388the initialisation of the input ring history, and history expansion.
389
ba02c167 390Variables `comint-output-filter-functions', a hook, and
e11284d5 391`comint-scroll-to-bottom-on-input' and `comint-scroll-to-bottom-on-output'
988a4d60
RS
392control whether input and output cause the window to scroll to the end of the
393buffer."
d76b71af 394 (setq comint-prompt-regexp shell-prompt-pattern)
e11284d5 395 (setq comint-completion-fignore shell-completion-fignore)
d76b71af 396 (setq comint-delimiter-argument-list shell-delimiter-argument-list)
76f63ea0 397 (setq comint-file-name-chars shell-file-name-chars)
7b4c6503 398 (setq comint-file-name-quote-list shell-file-name-quote-list)
988a4d60 399 (setq comint-dynamic-complete-functions shell-dynamic-complete-functions)
d76b71af
RS
400 (make-local-variable 'paragraph-start)
401 (setq paragraph-start comint-prompt-regexp)
c156572f
SM
402 (make-local-variable 'font-lock-defaults)
403 (setq font-lock-defaults '(shell-font-lock-keywords t))
be9b65ac 404 (make-local-variable 'shell-dirstack)
a9ec2adb 405 (setq shell-dirstack nil)
fdcc1ef7 406 (make-local-variable 'shell-last-dir)
fa8f1b25 407 (setq shell-last-dir nil)
a9ec2adb
JB
408 (make-local-variable 'shell-dirtrackp)
409 (setq shell-dirtrackp t)
1458efa0 410 (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t)
4f99443b 411 (setq comint-input-autoexpand shell-input-autoexpand)
5d9719e6
RS
412 ;; This is not really correct, since the shell buffer does not really
413 ;; edit this directory. But it is useful in the buffer list and menus.
414 (make-local-variable 'list-buffers-directory)
415 (setq list-buffers-directory (expand-file-name default-directory))
d76b71af 416 ;; shell-dependent assignments.
c63e83a4
RS
417 (let ((shell (file-name-nondirectory (car
418 (process-command (get-buffer-process (current-buffer)))))))
d76b71af
RS
419 (setq comint-input-ring-file-name
420 (or (getenv "HISTFILE")
c63e83a4
RS
421 (cond ((string-equal shell "bash") "~/.bash_history")
422 ((string-equal shell "ksh") "~/.sh_history")
423 (t "~/.history"))))
525c5be7 424 (if (or (equal comint-input-ring-file-name "")
fbcb3055
RS
425 (equal (file-truename comint-input-ring-file-name)
426 (file-truename "/dev/null")))
3c24374d 427 (setq comint-input-ring-file-name nil))
4dced927
AI
428 ;; Arrange to write out the input ring on exit, if the shell doesn't
429 ;; do this itself.
430 (if (and comint-input-ring-file-name
431 (string-match shell-dumb-shell-regexp shell))
432 (set-process-sentinel (get-buffer-process (current-buffer))
433 #'shell-write-history-on-exit))
c63e83a4 434 (setq shell-dirstack-query
221ca406
SM
435 (cond ((string-equal shell "sh") "pwd")
436 ((string-equal shell "ksh") "echo $PWD ~-")
437 (t "dirs"))))
4f99443b 438 (comint-read-input-ring t))
4dced927
AI
439
440(defun shell-write-history-on-exit (process event)
441 "Called when the shell process is stopped.
442
443Writes the input history to a history file
84728570 444`comint-input-ring-file-name' using `comint-write-input-ring'
4dced927
AI
445and inserts a short message in the shell buffer.
446
447This function is a sentinel watching the shell interpreter process.
448Sentinels will always get the two parameters PROCESS and EVENT."
449 ;; Write history.
450 (comint-write-input-ring)
84728570
SS
451 (let ((buf (process-buffer process)))
452 (when (buffer-live-p buf)
453 (with-current-buffer buf
454 (insert (format "\nProcess %s %s\n" process event))))))
455
c88ab9ce 456;;;###autoload
fd1035aa
MB
457(defun shell (&optional buffer)
458 "Run an inferior shell, with I/O through BUFFER (which defaults to `*shell*').
459Interactively, a prefix arg means to prompt for BUFFER.
460If BUFFER exists but shell process is not running, make new shell.
461If BUFFER exists and shell process is running, just switch to BUFFER.
24fdffaa 462Program used comes from variable `explicit-shell-file-name',
a9ec2adb
JB
463 or (if that is nil) from the ESHELL environment variable,
464 or else from SHELL if there is no ESHELL.
fbc270e7 465If a file `~/.emacs_SHELLNAME' exists, it is given as initial input
a9ec2adb
JB
466 (Note that this may lose due to a timing error if the shell
467 discards input when it starts up.)
fbc270e7
RS
468The buffer is put in Shell mode, giving commands for sending input
469and controlling the subjobs of the shell. See `shell-mode'.
470See also the variable `shell-prompt-pattern'.
be9b65ac 471
ebe4d555
RS
472To specify a coding system for converting non-ASCII characters
473in the input and output to the shell, use \\[universal-coding-system-argument]
474before \\[shell]. You can also specify this with \\[set-buffer-process-coding-system]
475in the shell buffer, after you start the shell.
476The default comes from `process-coding-system-alist' and
477`default-process-coding-system'.
478
a9ec2adb 479The shell file name (sans directories) is used to make a symbol name
62c9fad7 480such as `explicit-csh-args'. If that symbol is a variable,
be9b65ac
DL
481its value is used as a list of arguments when invoking the shell.
482Otherwise, one argument `-i' is passed to the shell.
483
484\(Type \\[describe-mode] in the shell buffer for a list of commands.)"
fd1035aa
MB
485 (interactive
486 (list
487 (and current-prefix-arg
488 (read-buffer "Shell buffer: " "*shell*"))))
489 (when (null buffer)
490 (setq buffer "*shell*"))
491 (if (not (comint-check-proc buffer))
d76b71af
RS
492 (let* ((prog (or explicit-shell-file-name
493 (getenv "ESHELL")
494 (getenv "SHELL")
84728570 495 "/bin/sh"))
d76b71af
RS
496 (name (file-name-nondirectory prog))
497 (startfile (concat "~/.emacs_" name))
519bfec4
RS
498 (xargs-name (intern-soft (concat "explicit-" name "-args")))
499 shell-buffer)
500 (save-excursion
fd1035aa 501 (set-buffer (apply 'make-comint-in-buffer "shell" buffer prog
519bfec4
RS
502 (if (file-exists-p startfile) startfile)
503 (if (and xargs-name (boundp xargs-name))
504 (symbol-value xargs-name)
505 '("-i"))))
506 (setq shell-buffer (current-buffer))
507 (shell-mode))
508 (pop-to-buffer shell-buffer))
fd1035aa 509 (pop-to-buffer buffer)))
519bfec4
RS
510
511;;; Don't do this when shell.el is loaded, only while dumping.
512;;;###autoload (add-hook 'same-window-buffer-names "*shell*")
84728570 513
be9b65ac 514;;; Directory tracking
b578f267 515;;;
be9b65ac
DL
516;;; This code provides the shell mode input sentinel
517;;; SHELL-DIRECTORY-TRACKER
518;;; that tracks cd, pushd, and popd commands issued to the shell, and
519;;; changes the current directory of the shell buffer accordingly.
520;;;
521;;; This is basically a fragile hack, although it's more accurate than
b7fc702e 522;;; the version in Emacs 18's shell.el. It has the following failings:
be9b65ac 523;;; 1. It doesn't know about the cdpath shell variable.
d76b71af
RS
524;;; 2. It cannot infallibly deal with command sequences, though it does well
525;;; with these and with ignoring commands forked in another shell with ()s.
526;;; 3. More generally, any complex command is going to throw it. Otherwise,
527;;; you'd have to build an entire shell interpreter in emacs lisp. Failing
528;;; that, there's no way to catch shell commands where cd's are buried
529;;; inside conditional expressions, aliases, and so forth.
be9b65ac
DL
530;;;
531;;; The whole approach is a crock. Shell aliases mess it up. File sourcing
532;;; messes it up. You run other processes under the shell; these each have
533;;; separate working directories, and some have commands for manipulating
534;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have
a9ec2adb 535;;; commands that do *not* affect the current w.d. at all, but look like they
be9b65ac
DL
536;;; do (e.g., the cd command in ftp). In shells that allow you job
537;;; control, you can switch between jobs, all having different w.d.'s. So
538;;; simply saying %3 can shift your w.d..
539;;;
540;;; The solution is to relax, not stress out about it, and settle for
541;;; a hack that works pretty well in typical circumstances. Remember
84728570 542;;; that a half-assed solution is more in keeping with the spirit of Unix,
be9b65ac
DL
543;;; anyway. Blech.
544;;;
545;;; One good hack not implemented here for users of programmable shells
546;;; is to program up the shell w.d. manipulation commands to output
547;;; a coded command sequence to the tty. Something like
548;;; ESC | <cwd> |
549;;; where <cwd> is the new current working directory. Then trash the
550;;; directory tracking machinery currently used in this package, and
551;;; replace it with a process filter that watches for and strips out
552;;; these messages.
553
be9b65ac
DL
554(defun shell-directory-tracker (str)
555 "Tracks cd, pushd and popd commands issued to the shell.
556This function is called on each input passed to the shell.
557It watches for cd, pushd and popd commands and sets the buffer's
558default directory to track these commands.
559
2154db49 560You may toggle this tracking on and off with M-x dirtrack-mode.
be9b65ac
DL
561If emacs gets confused, you can resync with the shell with M-x dirs.
562
32d01212 563See variables `shell-cd-regexp', `shell-chdrive-regexp', `shell-pushd-regexp',
84728570 564and `shell-popd-regexp', while `shell-pushd-tohome', `shell-pushd-dextract',
32d01212 565and `shell-pushd-dunique' control the behavior of the relevant command.
d76b71af 566
24fdffaa 567Environment variables are expanded, see function `substitute-in-file-name'."
d76b71af
RS
568 (if shell-dirtrackp
569 ;; We fail gracefully if we think the command will fail in the shell.
570 (condition-case chdir-failure
54021695 571 (let ((start (progn (string-match "^[; \t]*" str) ; skip whitespace
d76b71af
RS
572 (match-end 0)))
573 end cmd arg1)
574 (while (string-match shell-command-regexp str start)
575 (setq end (match-end 0)
576 cmd (comint-arguments (substring str start end) 0 0)
577 arg1 (comint-arguments (substring str start end) 1 1))
5f49d5e8
KH
578 (if arg1
579 (setq arg1 (shell-unquote-argument arg1)))
54021695
KH
580 (cond ((string-match (concat "\\`\\(" shell-popd-regexp
581 "\\)\\($\\|[ \t]\\)")
582 cmd)
32d01212 583 (shell-process-popd (comint-substitute-in-file-name arg1)))
54021695
KH
584 ((string-match (concat "\\`\\(" shell-pushd-regexp
585 "\\)\\($\\|[ \t]\\)")
586 cmd)
32d01212 587 (shell-process-pushd (comint-substitute-in-file-name arg1)))
54021695
KH
588 ((string-match (concat "\\`\\(" shell-cd-regexp
589 "\\)\\($\\|[ \t]\\)")
590 cmd)
32d01212
RS
591 (shell-process-cd (comint-substitute-in-file-name arg1)))
592 ((and shell-chdrive-regexp
593 (string-match (concat "\\`\\(" shell-chdrive-regexp
594 "\\)\\($\\|[ \t]\\)")
595 cmd))
596 (shell-process-cd (comint-substitute-in-file-name cmd))))
54021695 597 (setq start (progn (string-match "[; \t]*" str end) ; skip again
d76b71af 598 (match-end 0)))))
ba02c167 599 (error "Couldn't cd"))))
d76b71af 600
5f49d5e8
KH
601(defun shell-unquote-argument (string)
602 "Remove all kinds of shell quoting from STRING."
603 (save-match-data
f5058b96
EZ
604 (let ((idx 0) next inside
605 (quote-chars
606 (if (string-match shell-dumb-shell-regexp
607 (file-name-nondirectory
608 (car (process-command (get-buffer-process (current-buffer))))))
609 "['`\"]"
610 "[\\'`\"]")))
5f49d5e8 611 (while (and (< idx (length string))
f5058b96 612 (setq next (string-match quote-chars string next)))
5f49d5e8
KH
613 (cond ((= (aref string next) ?\\)
614 (setq string (replace-match "" nil nil string))
615 (setq next (1+ next)))
616 ((and inside (= (aref string next) inside))
617 (setq string (replace-match "" nil nil string))
618 (setq inside nil))
619 (inside
620 (setq next (1+ next)))
621 (t
622 (setq inside (aref string next))
623 (setq string (replace-match "" nil nil string)))))
624 string)))
625
be9b65ac
DL
626;;; popd [+n]
627(defun shell-process-popd (arg)
d76b71af
RS
628 (let ((num (or (shell-extract-num arg) 0)))
629 (cond ((and num (= num 0) shell-dirstack)
0b3c9cf1 630 (shell-cd (car shell-dirstack))
d76b71af
RS
631 (setq shell-dirstack (cdr shell-dirstack))
632 (shell-dirstack-message))
633 ((and num (> num 0) (<= num (length shell-dirstack)))
634 (let* ((ds (cons nil shell-dirstack))
635 (cell (nthcdr (1- num) ds)))
636 (rplacd cell (cdr (cdr cell)))
637 (setq shell-dirstack (cdr ds))
638 (shell-dirstack-message)))
639 (t
ba02c167 640 (error "Couldn't popd")))))
be9b65ac 641
3db9a4eb 642;; Return DIR prefixed with comint-file-name-prefix as appropriate.
4a02b552
RS
643(defun shell-prefixed-directory-name (dir)
644 (if (= (length comint-file-name-prefix) 0)
645 dir
646 (if (file-name-absolute-p dir)
647 ;; The name is absolute, so prepend the prefix.
648 (concat comint-file-name-prefix dir)
0b3c9cf1 649 ;; For relative name we assume default-directory already has the prefix.
4a02b552 650 (expand-file-name dir))))
3db9a4eb 651
be9b65ac
DL
652;;; cd [dir]
653(defun shell-process-cd (arg)
3db9a4eb
RM
654 (let ((new-dir (cond ((zerop (length arg)) (concat comint-file-name-prefix
655 "~"))
d76b71af 656 ((string-equal "-" arg) shell-last-dir)
3db9a4eb 657 (t (shell-prefixed-directory-name arg)))))
d76b71af 658 (setq shell-last-dir default-directory)
0b3c9cf1 659 (shell-cd new-dir)
d76b71af 660 (shell-dirstack-message)))
be9b65ac
DL
661
662;;; pushd [+n | dir]
663(defun shell-process-pushd (arg)
d76b71af
RS
664 (let ((num (shell-extract-num arg)))
665 (cond ((zerop (length arg))
666 ;; no arg -- swap pwd and car of stack unless shell-pushd-tohome
667 (cond (shell-pushd-tohome
3db9a4eb 668 (shell-process-pushd (concat comint-file-name-prefix "~")))
d76b71af
RS
669 (shell-dirstack
670 (let ((old default-directory))
0b3c9cf1 671 (shell-cd (car shell-dirstack))
221ca406 672 (setq shell-dirstack (cons old (cdr shell-dirstack)))
d76b71af
RS
673 (shell-dirstack-message)))
674 (t
675 (message "Directory stack empty."))))
676 ((numberp num)
677 ;; pushd +n
678 (cond ((> num (length shell-dirstack))
679 (message "Directory stack not that deep."))
680 ((= num 0)
681 (error (message "Couldn't cd.")))
682 (shell-pushd-dextract
683 (let ((dir (nth (1- num) shell-dirstack)))
684 (shell-process-popd arg)
685 (shell-process-pushd default-directory)
0b3c9cf1 686 (shell-cd dir)
d76b71af
RS
687 (shell-dirstack-message)))
688 (t
689 (let* ((ds (cons default-directory shell-dirstack))
690 (dslen (length ds))
691 (front (nthcdr num ds))
692 (back (reverse (nthcdr (- dslen num) (reverse ds))))
693 (new-ds (append front back)))
0b3c9cf1 694 (shell-cd (car new-ds))
d76b71af
RS
695 (setq shell-dirstack (cdr new-ds))
696 (shell-dirstack-message)))))
697 (t
698 ;; pushd <dir>
699 (let ((old-wd default-directory))
0b3c9cf1 700 (shell-cd (shell-prefixed-directory-name arg))
d76b71af
RS
701 (if (or (null shell-pushd-dunique)
702 (not (member old-wd shell-dirstack)))
703 (setq shell-dirstack (cons old-wd shell-dirstack)))
704 (shell-dirstack-message))))))
be9b65ac
DL
705
706;; If STR is of the form +n, for n>0, return n. Otherwise, nil.
707(defun shell-extract-num (str)
708 (and (string-match "^\\+[1-9][0-9]*$" str)
709 (string-to-int str)))
710
711
2154db49 712(defun shell-dirtrack-mode ()
be9b65ac
DL
713 "Turn directory tracking on and off in a shell buffer."
714 (interactive)
0b3c9cf1
SM
715 (if (setq shell-dirtrackp (not shell-dirtrackp))
716 (setq list-buffers-directory default-directory)
717 (setq list-buffers-directory nil))
d76b71af 718 (message "Directory tracking %s" (if shell-dirtrackp "ON" "OFF")))
be9b65ac
DL
719
720;;; For your typing convenience:
2154db49
KH
721(defalias 'shell-dirtrack-toggle 'shell-dirtrack-mode)
722(defalias 'dirtrack-toggle 'shell-dirtrack-mode)
723(defalias 'dirtrack-mode 'shell-dirtrack-mode)
be9b65ac 724
0b3c9cf1
SM
725(defun shell-cd (dir)
726 "Do normal `cd' to DIR, and set `list-buffers-directory'."
b7e97ed5
SM
727 (if shell-dirtrackp
728 (setq list-buffers-directory (file-name-as-directory
729 (expand-file-name dir))))
0b3c9cf1 730 (cd dir))
be9b65ac
DL
731
732(defun shell-resync-dirs ()
733 "Resync the buffer's idea of the current directory stack.
84728570 734This command queries the shell with the command bound to
24fdffaa 735`shell-dirstack-query' (default \"dirs\"), reads the next
be9b65ac
DL
736line output and parses it to form the new directory stack.
737DON'T issue this command unless the buffer is at a shell prompt.
738Also, note that if some other subprocess decides to do output
739immediately after the query, its output will be taken as the
740new directory stack -- you lose. If this happens, just do the
741command again."
742 (interactive)
743 (let* ((proc (get-buffer-process (current-buffer)))
744 (pmark (process-mark proc)))
745 (goto-char pmark)
746 (insert shell-dirstack-query) (insert "\n")
747 (sit-for 0) ; force redisplay
84728570 748 (comint-send-string proc shell-dirstack-query)
be9b65ac
DL
749 (comint-send-string proc "\n")
750 (set-marker pmark (point))
751 (let ((pt (point))) ; wait for 1 line
752 ;; This extra newline prevents the user's pending input from spoofing us.
753 (insert "\n") (backward-char 1)
754 (while (not (looking-at ".+\n"))
755 (accept-process-output proc)
756 (goto-char pt)))
757 (goto-char pmark) (delete-char 1) ; remove the extra newline
758 ;; That's the dirlist. grab it & parse it.
d76b71af 759 (let* ((dl (buffer-substring (match-beginning 0) (1- (match-end 0))))
be9b65ac
DL
760 (dl-len (length dl))
761 (ds '()) ; new dir stack
762 (i 0))
763 (while (< i dl-len)
764 ;; regexp = optional whitespace, (non-whitespace), optional whitespace
765 (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir
3db9a4eb
RM
766 (setq ds (cons (concat comint-file-name-prefix
767 (substring dl (match-beginning 1)
768 (match-end 1)))
be9b65ac
DL
769 ds))
770 (setq i (match-end 0)))
3db9a4eb 771 (let ((ds (nreverse ds)))
be9b65ac 772 (condition-case nil
0b3c9cf1 773 (progn (shell-cd (car ds))
221ca406
SM
774 (setq shell-dirstack (cdr ds)
775 shell-last-dir (car shell-dirstack))
be9b65ac
DL
776 (shell-dirstack-message))
777 (error (message "Couldn't cd.")))))))
778
779;;; For your typing convenience:
31e1d920 780(defalias 'dirs 'shell-resync-dirs)
be9b65ac
DL
781
782
783;;; Show the current dirstack on the message line.
784;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo".
785;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".)
786;;; All the commands that mung the buffer's dirstack finish by calling
787;;; this guy.
788(defun shell-dirstack-message ()
8a6387ed
SM
789 (when shell-dirtrack-verbose
790 (let* ((msg "")
791 (ds (cons default-directory shell-dirstack))
792 (home (expand-file-name (concat comint-file-name-prefix "~/")))
793 (homelen (length home)))
794 (while ds
795 (let ((dir (car ds)))
796 (and (>= (length dir) homelen)
797 (string= home (substring dir 0 homelen))
798 (setq dir (concat "~/" (substring dir homelen))))
799 ;; Strip off comint-file-name-prefix if present.
800 (and comint-file-name-prefix
801 (>= (length dir) (length comint-file-name-prefix))
802 (string= comint-file-name-prefix
803 (substring dir 0 (length comint-file-name-prefix)))
804 (setq dir (substring dir (length comint-file-name-prefix)))
805 (setcar ds dir))
806 (setq msg (concat msg (directory-file-name dir) " "))
807 (setq ds (cdr ds))))
808 (message "%s" msg))))
84728570 809
c64d8c55
RS
810;; This was mostly copied from shell-resync-dirs.
811(defun shell-snarf-envar (var)
812 "Return as a string the shell's value of environment variable VAR."
813 (let* ((cmd (format "printenv '%s'\n" var))
814 (proc (get-buffer-process (current-buffer)))
815 (pmark (process-mark proc)))
816 (goto-char pmark)
817 (insert cmd)
818 (sit-for 0) ; force redisplay
819 (comint-send-string proc cmd)
820 (set-marker pmark (point))
821 (let ((pt (point))) ; wait for 1 line
822 ;; This extra newline prevents the user's pending input from spoofing us.
823 (insert "\n") (backward-char 1)
824 (while (not (looking-at ".+\n"))
825 (accept-process-output proc)
826 (goto-char pt)))
827 (goto-char pmark) (delete-char 1) ; remove the extra newline
828 (buffer-substring (match-beginning 0) (1- (match-end 0)))))
829
830(defun shell-copy-environment-variable (variable)
831 "Copy the environment variable VARIABLE from the subshell to Emacs.
832This command reads the value of the specified environment variable
833in the shell, and sets the same environment variable in Emacs
a9e73449 834\(what `getenv' in Emacs would return) to that value.
c64d8c55
RS
835That value will affect any new subprocesses that you subsequently start
836from Emacs."
837 (interactive (list (read-envvar-name "\
838Copy Shell environment variable to Emacs: ")))
839 (setenv variable (shell-snarf-envar variable)))
84728570 840
d76b71af
RS
841(defun shell-forward-command (&optional arg)
842 "Move forward across ARG shell command(s). Does not cross lines.
843See `shell-command-regexp'."
844 (interactive "p")
845 (let ((limit (save-excursion (end-of-line nil) (point))))
19134c18 846 (if (re-search-forward (concat shell-command-regexp "\\([;&|][\t ]*\\)+")
d76b71af 847 limit 'move arg)
b0f86743 848 (skip-syntax-backward " "))))
d76b71af
RS
849
850
851(defun shell-backward-command (&optional arg)
852 "Move backward across ARG shell command(s). Does not cross lines.
853See `shell-command-regexp'."
854 (interactive "p")
855 (let ((limit (save-excursion (comint-bol nil) (point))))
28bc09d6
MB
856 (when (> limit (point))
857 (setq limit (line-beginning-position)))
b0f86743 858 (skip-syntax-backward " " limit)
d76b71af 859 (if (re-search-backward
19134c18 860 (format "[;&|]+[\t ]*\\(%s\\)" shell-command-regexp) limit 'move arg)
d76b71af 861 (progn (goto-char (match-beginning 1))
b0f86743 862 (skip-chars-forward ";&|")))))
d76b71af
RS
863
864
d76b71af
RS
865(defun shell-dynamic-complete-command ()
866 "Dynamically complete the command at point.
867This function is similar to `comint-dynamic-complete-filename', except that it
868searches `exec-path' (minus the trailing emacs library path) for completion
869candidates. Note that this may not be the same as the shell's idea of the
870path.
871
872Completion is dependent on the value of `shell-completion-execonly', plus
988a4d60
RS
873those that effect file completion. See `shell-dynamic-complete-as-command'.
874
875Returns t if successful."
d76b71af 876 (interactive)
988a4d60
RS
877 (let ((filename (comint-match-partial-filename)))
878 (if (and filename
879 (save-match-data (not (string-match "[~/]" filename)))
880 (eq (match-beginning 0)
881 (save-excursion (shell-backward-command 1) (point))))
882 (prog2 (message "Completing command name...")
883 (shell-dynamic-complete-as-command)))))
884
885
886(defun shell-dynamic-complete-as-command ()
887 "Dynamically complete at point as a command.
888See `shell-dynamic-complete-filename'. Returns t if successful."
56783585 889 (let* ((filename (or (comint-match-partial-filename) ""))
d76b71af
RS
890 (pathnondir (file-name-nondirectory filename))
891 (paths (cdr (reverse exec-path)))
892 (cwd (file-name-as-directory (expand-file-name default-directory)))
893 (ignored-extensions
a867a90a
RS
894 (and comint-completion-fignore
895 (mapconcat (function (lambda (x) (concat (regexp-quote x) "$")))
896 comint-completion-fignore "\\|")))
d76b71af
RS
897 (path "") (comps-in-path ()) (file "") (filepath "") (completions ()))
898 ;; Go thru each path in the search path, finding completions.
899 (while paths
900 (setq path (file-name-as-directory (comint-directory (or (car paths) ".")))
901 comps-in-path (and (file-accessible-directory-p path)
902 (file-name-all-completions pathnondir path)))
903 ;; Go thru each completion found, to see whether it should be used.
904 (while comps-in-path
905 (setq file (car comps-in-path)
906 filepath (concat path file))
907 (if (and (not (member file completions))
7b2db0a0
KH
908 (not (and ignored-extensions
909 (string-match ignored-extensions file)))
d76b71af
RS
910 (or (string-equal path cwd)
911 (not (file-directory-p filepath)))
912 (or (null shell-completion-execonly)
913 (file-executable-p filepath)))
914 (setq completions (cons file completions)))
915 (setq comps-in-path (cdr comps-in-path)))
916 (setq paths (cdr paths)))
917 ;; OK, we've got a list of completions.
56783585
RS
918 (let ((success (let ((comint-completion-addsuffix nil))
919 (comint-dynamic-simple-complete pathnondir completions))))
920 (if (and (memq success '(sole shortest)) comint-completion-addsuffix
921 (not (file-directory-p (comint-match-partial-filename))))
922 (insert " "))
923 success)))
988a4d60
RS
924
925
926(defun shell-match-partial-variable ()
da6a48c9 927 "Return the shell variable at point, or nil if none is found."
988a4d60
RS
928 (save-excursion
929 (let ((limit (point)))
930 (if (re-search-backward "[^A-Za-z0-9_{}]" nil 'move)
931 (or (looking-at "\\$") (forward-char 1)))
932 ;; Anchor the search forwards.
933 (if (or (eolp) (looking-at "[^A-Za-z0-9_{}$]"))
934 nil
935 (re-search-forward "\\$?{?[A-Za-z0-9_]*}?" limit)
936 (buffer-substring (match-beginning 0) (match-end 0))))))
a9ec2adb 937
988a4d60
RS
938
939(defun shell-dynamic-complete-environment-variable ()
940 "Dynamically complete the environment variable at point.
941Completes if after a variable, i.e., if it starts with a \"$\".
942See `shell-dynamic-complete-as-environment-variable'.
943
944This function is similar to `comint-dynamic-complete-filename', except that it
945searches `process-environment' for completion candidates. Note that this may
946not be the same as the interpreter's idea of variable names. The main problem
947with this type of completion is that `process-environment' is the environment
948which Emacs started with. Emacs does not track changes to the environment made
949by the interpreter. Perhaps it would be more accurate if this function was
950called `shell-dynamic-complete-process-environment-variable'.
951
952Returns non-nil if successful."
953 (interactive)
954 (let ((variable (shell-match-partial-variable)))
955 (if (and variable (string-match "^\\$" variable))
956 (prog2 (message "Completing variable name...")
957 (shell-dynamic-complete-as-environment-variable)))))
958
959
960(defun shell-dynamic-complete-as-environment-variable ()
961 "Dynamically complete at point as an environment variable.
962Used by `shell-dynamic-complete-environment-variable'.
963Uses `comint-dynamic-simple-complete'."
964 (let* ((var (or (shell-match-partial-variable) ""))
965 (variable (substring var (or (string-match "[^$({]\\|$" var) 0)))
966 (variables (mapcar (function (lambda (x)
967 (substring x 0 (string-match "=" x))))
968 process-environment))
969 (addsuffix comint-completion-addsuffix)
970 (comint-completion-addsuffix nil)
971 (success (comint-dynamic-simple-complete variable variables)))
972 (if (memq success '(sole shortest))
973 (let* ((var (shell-match-partial-variable))
974 (variable (substring var (string-match "[^$({]" var)))
975 (protection (cond ((string-match "{" var) "}")
976 ((string-match "(" var) ")")
977 (t "")))
978 (suffix (cond ((null addsuffix) "")
979 ((file-directory-p
980 (comint-directory (getenv variable))) "/")
981 (t " "))))
982 (insert protection suffix)))
983 success))
984
985
986(defun shell-replace-by-expanded-directory ()
987 "Expand directory stack reference before point.
988Directory stack references are of the form \"=digit\" or \"=-\".
989See `default-directory' and `shell-dirstack'.
990
991Returns t if successful."
992 (interactive)
993 (if (comint-match-partial-filename)
994 (save-excursion
988a4d60
RS
995 (goto-char (match-beginning 0))
996 (let ((stack (cons default-directory shell-dirstack))
997 (index (cond ((looking-at "=-/?")
998 (length shell-dirstack))
999 ((looking-at "=\\([0-9]+\\)")
1000 (string-to-number
1001 (buffer-substring
1002 (match-beginning 1) (match-end 1)))))))
1003 (cond ((null index)
1004 nil)
1005 ((>= index (length stack))
1006 (error "Directory stack not that deep."))
1007 (t
1008 (replace-match (file-name-as-directory (nth index stack)) t t)
1009 (message "Directory item: %d" index)
1010 t))))))
84728570 1011
c88ab9ce
ER
1012(provide 'shell)
1013
1014;;; shell.el ends here