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