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