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