Commit | Line | Data |
---|---|---|
c0a193ea | 1 | ;;; shell.el --- specialized comint.el for running the shell -*- lexical-binding: t -*- |
5109bfb0 | 2 | |
44e97401 | 3 | ;; Copyright (C) 1988, 1993-1997, 2000-2012 Free Software Foundation, Inc. |
6d74b528 | 4 | |
1cd7adc6 | 5 | ;; Author: Olin Shivers <shivers@cs.cmu.edu> |
5762abec | 6 | ;; Simon Marshall <simon@gnu.org> |
b71f9bdb | 7 | ;; Maintainer: FSF <emacs-devel@gnu.org> |
d7b4d18f | 8 | ;; Keywords: processes |
630cc463 | 9 | |
5109bfb0 | 10 | ;; This file is part of GNU Emacs. |
c88ab9ce | 11 | |
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
5109bfb0 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) 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 |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
be9b65ac | 24 | |
630cc463 ER |
25 | ;;; Commentary: |
26 | ||
4696802b JB |
27 | ;; This file defines a shell-in-a-buffer package (shell mode) built on |
28 | ;; top of comint mode. This is actually cmushell with things renamed | |
29 | ;; to replace its counterpart in Emacs 18. cmushell is more | |
b578f267 | 30 | ;; featureful, robust, and uniform than the Emacs 18 version. |
be9b65ac | 31 | |
b578f267 | 32 | ;; Since this mode is built on top of the general command-interpreter-in- |
84728570 | 33 | ;; a-buffer mode (comint mode), it shares a common base functionality, |
b578f267 EN |
34 | ;; and a common set of bindings, with all modes derived from comint mode. |
35 | ;; This makes these modes easier to use. | |
be9b65ac | 36 | |
b578f267 | 37 | ;; For documentation on the functionality provided by comint mode, and |
333f9019 | 38 | ;; the hooks available for customizing it, see the file comint.el. |
b578f267 | 39 | ;; For further information on shell mode, see the comments below. |
be9b65ac | 40 | |
b578f267 | 41 | ;; Needs fixin: |
84728570 | 42 | ;; When sending text from a source file to a subprocess, the process-mark can |
b578f267 EN |
43 | ;; move off the window, so you can lose sight of the process interactions. |
44 | ;; Maybe I should ensure the process mark is in the window when I send | |
45 | ;; text to the process? Switch selectable? | |
be9b65ac | 46 | |
a9ec2adb JB |
47 | ;; YOUR .EMACS FILE |
48 | ;;============================================================================= | |
865fe16f | 49 | ;; Some suggestions for your init file. |
a9ec2adb | 50 | ;; |
0b3c9cf1 SM |
51 | ;; ;; Define M-# to run some strange command: |
52 | ;; (eval-after-load "shell" | |
53 | ;; '(define-key shell-mode-map "\M-#" 'shells-dynamic-spell)) | |
84728570 | 54 | |
b578f267 EN |
55 | ;; Brief Command Documentation: |
56 | ;;============================================================================ | |
57 | ;; Comint Mode Commands: (common to shell and all comint-derived modes) | |
58 | ;; | |
a9e73449 RS |
59 | ;; m-p comint-previous-input Cycle backwards in input history |
60 | ;; m-n comint-next-input Cycle forwards | |
b578f267 EN |
61 | ;; m-r comint-previous-matching-input Previous input matching a regexp |
62 | ;; m-s comint-next-matching-input Next input that matches | |
a9e73449 | 63 | ;; m-c-l comint-show-output Show last batch of process output |
b578f267 | 64 | ;; return comint-send-input |
a9e73449 | 65 | ;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff. |
b578f267 | 66 | ;; c-c c-a comint-bol Beginning of line; skip prompt |
a9e73449 RS |
67 | ;; c-c c-u comint-kill-input ^u |
68 | ;; c-c c-w backward-kill-word ^w | |
69 | ;; c-c c-c comint-interrupt-subjob ^c | |
70 | ;; c-c c-z comint-stop-subjob ^z | |
71 | ;; c-c c-\ comint-quit-subjob ^\ | |
3226d6ca | 72 | ;; c-c c-o comint-delete-output Delete last batch of process output |
a9e73449 | 73 | ;; c-c c-r comint-show-output Show last batch of process output |
94c34f7a | 74 | ;; c-c c-l comint-dynamic-list-input-ring List input history |
b578f267 | 75 | ;; send-invisible Read line w/o echo & send to proc |
a9e73449 | 76 | ;; comint-continue-subjob Useful if you accidentally suspend |
b578f267 EN |
77 | ;; top-level job |
78 | ;; comint-mode-hook is the comint mode hook. | |
79 | ||
80 | ;; Shell Mode Commands: | |
81 | ;; shell Fires up the shell process | |
c0a193ea | 82 | ;; tab completion-at-point Complete filename/command/history |
b578f267 EN |
83 | ;; m-? comint-dynamic-list-filename-completions |
84 | ;; List completions in help buffer | |
85 | ;; m-c-f shell-forward-command Forward a shell command | |
86 | ;; m-c-b shell-backward-command Backward a shell command | |
a9e73449 | 87 | ;; dirs Resync the buffer's dir stack |
a034393c | 88 | ;; shell-dirtrack-mode Turn dir tracking on/off |
b578f267 EN |
89 | ;; comint-strip-ctrl-m Remove trailing ^Ms from output |
90 | ;; | |
91 | ;; The shell mode hook is shell-mode-hook | |
e1dbe924 | 92 | ;; comint-prompt-regexp is initialized to shell-prompt-pattern, for backwards |
b578f267 | 93 | ;; compatibility. |
a9ec2adb | 94 | |
b578f267 | 95 | ;; Read the rest of this file for more information. |
84728570 | 96 | |
630cc463 ER |
97 | ;;; Code: |
98 | ||
99 | (require 'comint) | |
e17d9003 | 100 | (require 'pcomplete) |
630cc463 | 101 | |
b578f267 EN |
102 | ;;; Customization and Buffer Variables |
103 | ||
ed366e87 | 104 | (defgroup shell nil |
edf959f9 | 105 | "Running shell from within Emacs buffers." |
ed366e87 RS |
106 | :group 'processes |
107 | :group 'unix) | |
108 | ||
109 | (defgroup shell-directories nil | |
edf959f9 | 110 | "Directory support in shell mode." |
ed366e87 RS |
111 | :group 'shell) |
112 | ||
113 | (defgroup shell-faces nil | |
edf959f9 | 114 | "Faces in shell buffers." |
ed366e87 RS |
115 | :group 'shell) |
116 | ||
c88ab9ce | 117 | ;;;###autoload |
1e8780b1 | 118 | (defcustom shell-dumb-shell-regexp (purecopy "cmd\\(proxy\\)?\\.exe") |
f5058b96 EZ |
119 | "Regexp to match shells that don't save their command history, and |
120 | don't handle the backslash as a quote character. For shells that | |
121 | match this regexp, Emacs will write out the command history when the | |
122 | shell finishes, and won't remove backslashes when it unquotes shell | |
123 | arguments." | |
4dced927 AI |
124 | :type 'regexp |
125 | :group 'shell) | |
126 | ||
fad22e2e | 127 | (defcustom shell-prompt-pattern "^[^#$%>\n]*[#$%>] *" |
a9ec2adb | 128 | "Regexp to match prompts in the inferior shell. |
ac75ef20 | 129 | Defaults to \"^[^#$%>\\n]*[#$%>] *\", which works pretty well. |
2df2f980 | 130 | This variable is used to initialize `comint-prompt-regexp' in the |
a9ec2adb JB |
131 | shell buffer. |
132 | ||
e03035e3 LT |
133 | If `comint-use-prompt-regexp' is nil, then this variable is only used |
134 | to determine paragraph boundaries. See Info node `Shell Prompts' for | |
135 | how Shell mode treats paragraphs. | |
28bc09d6 | 136 | |
ac75ef20 | 137 | The pattern should probably not match more than one line. If it does, |
221ca406 | 138 | Shell mode may become confused trying to distinguish prompt from input |
d079ee5f | 139 | on lines which don't start with a prompt." |
fad22e2e RS |
140 | :type 'regexp |
141 | :group 'shell) | |
a9ec2adb | 142 | |
ed366e87 | 143 | (defcustom shell-completion-fignore nil |
05327ca9 | 144 | "List of suffixes to be disregarded during file/command completion. |
e11284d5 RS |
145 | This variable is used to initialize `comint-completion-fignore' in the shell |
146 | buffer. The default is nil, for compatibility with most shells. | |
d079ee5f | 147 | Some people like (\"~\" \"#\" \"%\")." |
ed366e87 RS |
148 | :type '(repeat (string :tag "Suffix")) |
149 | :group 'shell) | |
e11284d5 | 150 | |
13932042 | 151 | (defcustom shell-delimiter-argument-list '(?\| ?& ?< ?> ?\( ?\) ?\;) |
2df2f980 | 152 | "List of characters to recognize as separate arguments. |
56eb1060 | 153 | This variable is used to initialize `comint-delimiter-argument-list' in the |
64a14c74 GM |
154 | shell buffer. The value may depend on the operating system or shell." |
155 | :type '(choice (const nil) | |
156 | (repeat :tag "List of characters" character)) | |
64a14c74 | 157 | :group 'shell) |
39d1a2ce | 158 | |
d079ee5f | 159 | (defcustom shell-file-name-chars |
c60ee5e7 | 160 | (if (memq system-type '(ms-dos windows-nt cygwin)) |
4aed0379 | 161 | "~/A-Za-z0-9_^$!#%&{}@`'.,:()-" |
f0d5dc24 | 162 | "[]~/A-Za-z0-9+@:_.$#%,={}-") |
76f63ea0 RS |
163 | "String of characters valid in a file name. |
164 | This variable is used to initialize `comint-file-name-chars' in the | |
d079ee5f CY |
165 | shell buffer. The value may depend on the operating system or shell." |
166 | :type 'string | |
167 | :group 'shell) | |
d76b71af | 168 | |
d079ee5f | 169 | (defcustom shell-file-name-quote-list |
39d1a2ce SM |
170 | (if (memq system-type '(ms-dos windows-nt)) |
171 | nil | |
3ca5dc5b | 172 | (append shell-delimiter-argument-list '(?\s ?$ ?\* ?\! ?\" ?\' ?\` ?\# ?\\))) |
7b4c6503 SM |
173 | "List of characters to quote when in a file name. |
174 | This variable is used to initialize `comint-file-name-quote-list' in the | |
d079ee5f CY |
175 | shell buffer. The value may depend on the operating system or shell." |
176 | :type '(repeat character) | |
177 | :group 'shell) | |
7b4c6503 | 178 | |
d079ee5f | 179 | (defcustom shell-dynamic-complete-functions |
c0a193ea SM |
180 | '(comint-c-a-p-replace-by-expanded-history |
181 | shell-environment-variable-completion | |
182 | shell-command-completion | |
183 | shell-c-a-p-replace-by-expanded-directory | |
7baca3bc | 184 | pcomplete-completions-at-point |
c0a193ea | 185 | shell-filename-completion |
7baca3bc | 186 | comint-filename-completion) |
988a4d60 | 187 | "List of functions called to perform completion. |
2df2f980 | 188 | This variable is used to initialize `comint-dynamic-complete-functions' in the |
d079ee5f CY |
189 | shell buffer." |
190 | :type '(repeat function) | |
191 | :group 'shell) | |
988a4d60 | 192 | |
ed366e87 | 193 | (defcustom shell-command-regexp "[^;&|\n]+" |
05327ca9 | 194 | "Regexp to match a single command within a pipeline. |
ed366e87 RS |
195 | This is used for directory tracking and does not do a perfect job." |
196 | :type 'regexp | |
197 | :group 'shell) | |
d76b71af | 198 | |
6ed020c8 | 199 | (defcustom shell-command-separator-regexp "[;&|\n \t]*" |
05327ca9 | 200 | "Regexp to match a single command within a pipeline. |
6ed020c8 JB |
201 | This is used for directory tracking and does not do a perfect job." |
202 | :type 'regexp | |
203 | :group 'shell) | |
204 | ||
ed366e87 | 205 | (defcustom shell-completion-execonly t |
05327ca9 | 206 | "If non-nil, use executable files only for completion candidates. |
5394cb44 | 207 | This mirrors the optional behavior of tcsh. |
d76b71af | 208 | |
ed366e87 RS |
209 | Detecting executability of files may slow command completion considerably." |
210 | :type 'boolean | |
211 | :group 'shell) | |
d76b71af | 212 | |
ed366e87 | 213 | (defcustom shell-popd-regexp "popd" |
05327ca9 | 214 | "Regexp to match subshell commands equivalent to popd." |
ed366e87 RS |
215 | :type 'regexp |
216 | :group 'shell-directories) | |
be9b65ac | 217 | |
ed366e87 | 218 | (defcustom shell-pushd-regexp "pushd" |
05327ca9 | 219 | "Regexp to match subshell commands equivalent to pushd." |
ed366e87 RS |
220 | :type 'regexp |
221 | :group 'shell-directories) | |
be9b65ac | 222 | |
ed366e87 | 223 | (defcustom shell-pushd-tohome nil |
05327ca9 | 224 | "If non-nil, make pushd with no arg behave as \"pushd ~\" (like cd). |
ed366e87 RS |
225 | This mirrors the optional behavior of tcsh." |
226 | :type 'boolean | |
227 | :group 'shell-directories) | |
d76b71af | 228 | |
ed366e87 | 229 | (defcustom shell-pushd-dextract nil |
05327ca9 | 230 | "If non-nil, make \"pushd +n\" pop the nth dir to the stack top. |
ed366e87 RS |
231 | This mirrors the optional behavior of tcsh." |
232 | :type 'boolean | |
233 | :group 'shell-directories) | |
d76b71af | 234 | |
ed366e87 | 235 | (defcustom shell-pushd-dunique nil |
05327ca9 | 236 | "If non-nil, make pushd only add unique directories to the stack. |
ed366e87 RS |
237 | This mirrors the optional behavior of tcsh." |
238 | :type 'boolean | |
239 | :group 'shell-directories) | |
d76b71af | 240 | |
ed366e87 | 241 | (defcustom shell-cd-regexp "cd" |
05327ca9 | 242 | "Regexp to match subshell commands equivalent to cd." |
ed366e87 RS |
243 | :type 'regexp |
244 | :group 'shell-directories) | |
be9b65ac | 245 | |
ed366e87 | 246 | (defcustom shell-chdrive-regexp |
84728570 | 247 | (if (memq system-type '(ms-dos windows-nt)) |
32d01212 RS |
248 | ; NetWare allows the five chars between upper and lower alphabetics. |
249 | "[]a-zA-Z^_`\\[\\\\]:" | |
250 | nil) | |
05327ca9 | 251 | "If non-nil, is regexp used to track drive changes." |
233f3fb8 RS |
252 | :type '(choice regexp |
253 | (const nil)) | |
ed366e87 | 254 | :group 'shell-directories) |
32d01212 | 255 | |
8a6387ed | 256 | (defcustom shell-dirtrack-verbose t |
05327ca9 | 257 | "If non-nil, show the directory stack following directory change. |
a034393c | 258 | This is effective only if directory tracking is enabled. |
c004e539 GM |
259 | The `dirtrack' package provides an alternative implementation of this feature - |
260 | see the function `dirtrack-mode'." | |
8a6387ed SM |
261 | :type 'boolean |
262 | :group 'shell-directories) | |
263 | ||
ed366e87 | 264 | (defcustom explicit-shell-file-name nil |
05327ca9 | 265 | "If non-nil, is file name to use for explicitly requested inferior shell." |
ed366e87 RS |
266 | :type '(choice (const :tag "None" nil) file) |
267 | :group 'shell) | |
be9b65ac | 268 | |
400b0022 KS |
269 | ;; Note: There are no explicit references to the variable `explicit-csh-args'. |
270 | ;; It is used implicitly by M-x shell when the shell is `csh'. | |
ed366e87 | 271 | (defcustom explicit-csh-args |
be9b65ac DL |
272 | (if (eq system-type 'hpux) |
273 | ;; -T persuades HP's csh not to think it is smarter | |
274 | ;; than us about what terminal modes to use. | |
275 | '("-i" "-T") | |
276 | '("-i")) | |
05327ca9 | 277 | "Args passed to inferior shell by \\[shell], if the shell is csh. |
ed366e87 RS |
278 | Value is a list of strings, which may be nil." |
279 | :type '(repeat (string :tag "Argument")) | |
280 | :group 'shell) | |
be9b65ac | 281 | |
400b0022 KS |
282 | ;; Note: There are no explicit references to the variable `explicit-bash-args'. |
283 | ;; It is used implicitly by M-x shell when the interactive shell is `bash'. | |
6674676b | 284 | (defcustom explicit-bash-args |
969a0631 KS |
285 | (let* ((prog (or (and (boundp 'explicit-shell-file-name) explicit-shell-file-name) |
286 | (getenv "ESHELL") shell-file-name)) | |
287 | (name (file-name-nondirectory prog))) | |
491bf4bc | 288 | ;; Tell bash not to use readline, except for bash 1.x which |
3ed8598c | 289 | ;; doesn't grok --noediting. Bash 1.x has -nolineediting, but |
491bf4bc | 290 | ;; process-send-eof cannot terminate bash if we use it. |
969a0631 KS |
291 | (if (and (not purify-flag) |
292 | (equal name "bash") | |
293 | (file-executable-p prog) | |
294 | (string-match "bad option" | |
88b7c0e4 KS |
295 | (shell-command-to-string |
296 | (concat (shell-quote-argument prog) | |
297 | " --noediting")))) | |
969a0631 KS |
298 | '("-i") |
299 | '("--noediting" "-i"))) | |
05327ca9 | 300 | "Args passed to inferior shell by \\[shell], if the shell is bash. |
6674676b AS |
301 | Value is a list of strings, which may be nil." |
302 | :type '(repeat (string :tag "Argument")) | |
303 | :group 'shell) | |
304 | ||
ed366e87 | 305 | (defcustom shell-input-autoexpand 'history |
05327ca9 | 306 | "If non-nil, expand input command history references on completion. |
4f99443b RS |
307 | This mirrors the optional behavior of tcsh (its autoexpand and histlit). |
308 | ||
309 | If the value is `input', then the expansion is seen on input. | |
310 | If the value is `history', then the expansion is only when inserting | |
311 | into the buffer's input ring. See also `comint-magic-space' and | |
c0a193ea | 312 | `comint-dynamic-complete-functions'. |
4f99443b RS |
313 | |
314 | This variable supplies a default for `comint-input-autoexpand', | |
ed366e87 | 315 | for Shell mode only." |
6f0984e5 AS |
316 | :type '(choice (const :tag "off" nil) |
317 | (const input) | |
318 | (const history) | |
319 | (const :tag "on" t)) | |
362ae0b3 | 320 | :group 'shell) |
4f99443b | 321 | |
be9b65ac | 322 | (defvar shell-dirstack nil |
c88ab9ce ER |
323 | "List of directories saved by pushd in this buffer's shell. |
324 | Thus, this does not include the shell's current directory.") | |
be9b65ac | 325 | |
b493a9b2 RS |
326 | (defvar shell-dirtrackp t |
327 | "Non-nil in a shell buffer means directory tracking is enabled.") | |
328 | ||
fa8f1b25 ER |
329 | (defvar shell-last-dir nil |
330 | "Keep track of last directory for ksh `cd -' command.") | |
331 | ||
c63e83a4 | 332 | (defvar shell-dirstack-query nil |
4dea1735 | 333 | "Command used by `shell-resync-dirs' to query the shell.") |
be9b65ac | 334 | |
a0310a6c DN |
335 | (defvar shell-mode-map |
336 | (let ((map (nconc (make-sparse-keymap) comint-mode-map))) | |
337 | (define-key map "\C-c\C-f" 'shell-forward-command) | |
338 | (define-key map "\C-c\C-b" 'shell-backward-command) | |
c0a193ea | 339 | (define-key map "\t" 'completion-at-point) |
a0310a6c DN |
340 | (define-key map (kbd "M-RET") 'shell-resync-dirs) |
341 | (define-key map "\M-?" 'comint-dynamic-list-filename-completions) | |
342 | (define-key map [menu-bar completion] | |
343 | (cons "Complete" | |
344 | (copy-keymap (lookup-key comint-mode-map [menu-bar completion])))) | |
345 | (define-key-after (lookup-key map [menu-bar completion]) | |
346 | [complete-env-variable] '("Complete Env. Variable Name" . | |
347 | shell-dynamic-complete-environment-variable) | |
348 | 'complete-file) | |
349 | (define-key-after (lookup-key map [menu-bar completion]) | |
350 | [expand-directory] '("Expand Directory Reference" . | |
351 | shell-replace-by-expanded-directory) | |
352 | 'complete-expand) | |
353 | map)) | |
be9b65ac | 354 | |
ed366e87 | 355 | (defcustom shell-mode-hook '() |
5a0c3f56 | 356 | "Hook for customizing Shell mode." |
ed366e87 RS |
357 | :type 'hook |
358 | :group 'shell) | |
be9b65ac | 359 | |
5945a1f7 | 360 | (defvar shell-font-lock-keywords |
cb6e07b1 | 361 | '(("[ \t]\\([+-][^ \t\n]+\\)" 1 font-lock-comment-face) |
473fbd89 SM |
362 | ("^[^ \t\n]+:.*" . font-lock-string-face) |
363 | ("^\\[[1-9][0-9]*\\]" . font-lock-string-face)) | |
5945a1f7 | 364 | "Additional expressions to highlight in Shell mode.") |
84728570 | 365 | |
be9b65ac | 366 | ;;; Basic Procedures |
be9b65ac | 367 | |
2473256d SM |
368 | (defun shell--unquote&requote-argument (qstr &optional upos) |
369 | (unless upos (setq upos 0)) | |
370 | (let* ((qpos 0) | |
371 | (dquotes nil) | |
372 | (ustrs '()) | |
373 | (re (concat | |
374 | "[\"']" | |
375 | "\\|\\$\\(?:\\([[:alpha:]][[:alnum:]]*\\)" | |
376 | "\\|{\\(?1:[^{}]+\\)}\\)" | |
377 | (when (memq system-type '(ms-dos windows-nt)) | |
378 | "\\|%\\(?1:[^\\\\/]*\\)%") | |
379 | (when comint-file-name-quote-list | |
380 | "\\|\\\\\\(.\\)"))) | |
381 | (qupos nil) | |
382 | (push (lambda (str end) | |
383 | (push str ustrs) | |
384 | (setq upos (- upos (length str))) | |
385 | (unless (or qupos (> upos 0)) | |
386 | (setq qupos (if (< end 0) (- end) (+ upos end)))))) | |
387 | match) | |
388 | (while (setq match (string-match re qstr qpos)) | |
389 | (funcall push (substring qstr qpos match) match) | |
390 | (cond | |
391 | ((match-beginning 2) (funcall push (match-string 2 qstr) (match-end 0))) | |
392 | ((match-beginning 1) (funcall push (getenv (match-string 1 qstr)) | |
393 | (- (match-end 0)))) | |
394 | ((eq (aref qstr match) ?\") (setq dquotes (not dquotes))) | |
395 | ((eq (aref qstr match) ?\') | |
396 | (cond | |
397 | (dquotes (funcall push "'" (match-end 0))) | |
398 | ((< match (1+ (length qstr))) | |
399 | (let ((end (string-match "'" qstr (1+ match)))) | |
400 | (funcall push (substring qstr (1+ match) end) | |
401 | (or end (length qstr))))) | |
402 | (t nil))) | |
403 | (t (error "Unexpected case in shell--unquote&requote-argument!"))) | |
404 | (setq qpos (match-end 0))) | |
405 | (funcall push (substring qstr qpos) (length qstr)) | |
406 | (list (mapconcat #'identity (nreverse ustrs) "") | |
407 | qupos #'comint-quote-filename))) | |
408 | ||
409 | (defun shell--unquote-argument (str) | |
410 | (car (shell--unquote&requote-argument str))) | |
411 | (defun shell--requote-argument (upos qstr) | |
412 | ;; See `completion-table-with-quoting'. | |
413 | (let ((res (shell--unquote&requote-argument qstr upos))) | |
414 | (cons (nth 1 res) (nth 2 res)))) | |
415 | ||
416 | (defun shell--parse-pcomplete-arguments () | |
b3fd59bd | 417 | "Parse whitespace separated arguments in the current region." |
2473256d | 418 | ;; FIXME: share code with shell--unquote&requote-argument. |
b3fd59bd SM |
419 | (let ((begin (save-excursion (shell-backward-command 1) (point))) |
420 | (end (point)) | |
421 | begins args) | |
422 | (save-excursion | |
423 | (goto-char begin) | |
424 | (while (< (point) end) | |
425 | (skip-chars-forward " \t\n") | |
426 | (push (point) begins) | |
356a3681 SM |
427 | (let ((arg ())) |
428 | (while (looking-at | |
429 | (eval-when-compile | |
430 | (concat | |
431 | "\\(?:[^\s\t\n\\\"']+" | |
432 | "\\|'\\([^']*\\)'?" | |
433 | "\\|\"\\(\\(?:[^\"\\]\\|\\\\.\\)*\\)\"?" | |
434 | "\\|\\\\\\(\\(?:.\\|\n\\)?\\)\\)"))) | |
435 | (goto-char (match-end 0)) | |
436 | (cond | |
437 | ((match-beginning 3) ;Backslash escape. | |
3d53ee1b | 438 | (push (cond |
2473256d | 439 | ((null comint-file-name-quote-list) |
3d53ee1b SM |
440 | (goto-char (match-beginning 3)) "\\") |
441 | ((= (match-beginning 3) (match-end 3)) "\\") | |
442 | (t (match-string 3))) | |
356a3681 SM |
443 | arg)) |
444 | ((match-beginning 2) ;Double quote. | |
2473256d | 445 | (push (if (null comint-file-name-quote-list) (match-string 2) |
fe263b8f SM |
446 | (replace-regexp-in-string |
447 | "\\\\\\(.\\)" "\\1" (match-string 2))) | |
356a3681 SM |
448 | arg)) |
449 | ((match-beginning 1) ;Single quote. | |
450 | (push (match-string 1) arg)) | |
451 | (t (push (match-string 0) arg)))) | |
452 | (push (mapconcat #'identity (nreverse arg) "") args))) | |
b3fd59bd | 453 | (cons (nreverse args) (nreverse begins))))) |
8e249bbd | 454 | |
7baca3bc CY |
455 | (defun shell-command-completion-function () |
456 | "Completion function for shell command names. | |
457 | This is the value of `pcomplete-command-completion-function' for | |
458 | Shell buffers. It implements `shell-completion-execonly' for | |
459 | `pcomplete' completion." | |
460 | (pcomplete-here (pcomplete-entries nil | |
461 | (if shell-completion-execonly | |
462 | 'file-executable-p)))) | |
463 | ||
914a0ae1 SM |
464 | (defun shell-completion-vars () |
465 | "Setup completion vars for `shell-mode' and `read-shell-command'." | |
466 | (set (make-local-variable 'comint-completion-fignore) | |
467 | shell-completion-fignore) | |
468 | (set (make-local-variable 'comint-delimiter-argument-list) | |
469 | shell-delimiter-argument-list) | |
470 | (set (make-local-variable 'comint-file-name-chars) shell-file-name-chars) | |
471 | (set (make-local-variable 'comint-file-name-quote-list) | |
472 | shell-file-name-quote-list) | |
473 | (set (make-local-variable 'comint-dynamic-complete-functions) | |
474 | shell-dynamic-complete-functions) | |
2473256d SM |
475 | (setq-local comint-unquote-function #'shell--unquote-argument) |
476 | (setq-local comint-requote-function #'shell--requote-argument) | |
914a0ae1 | 477 | (set (make-local-variable 'pcomplete-parse-arguments-function) |
2473256d | 478 | #'shell--parse-pcomplete-arguments) |
1c6d8c76 SM |
479 | (set (make-local-variable 'pcomplete-termination-string) |
480 | (cond ((not comint-completion-addsuffix) "") | |
481 | ((stringp comint-completion-addsuffix) | |
482 | comint-completion-addsuffix) | |
483 | ((not (consp comint-completion-addsuffix)) " ") | |
484 | (t (cdr comint-completion-addsuffix)))) | |
7baca3bc CY |
485 | (set (make-local-variable 'pcomplete-command-completion-function) |
486 | #'shell-command-completion-function) | |
e17d9003 SM |
487 | ;; Don't use pcomplete's defaulting mechanism, rely on |
488 | ;; shell-dynamic-complete-functions instead. | |
489 | (set (make-local-variable 'pcomplete-default-completion-function) #'ignore) | |
914a0ae1 SM |
490 | (setq comint-input-autoexpand shell-input-autoexpand) |
491 | ;; Not needed in shell-mode because it's inherited from comint-mode, but | |
492 | ;; placed here for read-shell-command. | |
493 | (add-hook 'completion-at-point-functions 'comint-completion-at-point nil t)) | |
494 | ||
e9904b59 RS |
495 | (put 'shell-mode 'mode-class 'special) |
496 | ||
9f299ecb | 497 | (define-derived-mode shell-mode comint-mode "Shell" |
933907e9 | 498 | "Major mode for interacting with an inferior shell.\\<shell-mode-map> |
8079b590 SM |
499 | \\[comint-send-input] after the end of the process' output sends the text from |
500 | the end of process to the end of the current line. | |
501 | \\[comint-send-input] before end of process output copies the current line minus the prompt to | |
502 | the end of the buffer and sends it (\\[comint-copy-old-input] just copies the current line). | |
e9933cb1 | 503 | \\[send-invisible] reads a line of text without echoing it, and sends it to |
56783585 RS |
504 | the shell. This is useful for entering passwords. Or, add the function |
505 | `comint-watch-for-password-prompt' to `comint-output-filter-functions'. | |
be9b65ac | 506 | |
e9933cb1 SM |
507 | If you want to make multiple shell buffers, rename the `*shell*' buffer |
508 | using \\[rename-buffer] or \\[rename-uniquely] and start a new shell. | |
509 | ||
8079b590 | 510 | If you want to make shell buffers limited in length, add the function |
4841acc4 | 511 | `comint-truncate-buffer' to `comint-output-filter-functions'. |
8079b590 | 512 | |
be9b65ac DL |
513 | If you accidentally suspend your process, use \\[comint-continue-subjob] |
514 | to continue it. | |
515 | ||
e9933cb1 SM |
516 | `cd', `pushd' and `popd' commands given to the shell are watched by Emacs to |
517 | keep this buffer's default directory the same as the shell's working directory. | |
0b3c9cf1 SM |
518 | While directory tracking is enabled, the shell's working directory is displayed |
519 | by \\[list-buffers] or \\[mouse-buffer-menu] in the `File' field. | |
44e97401 | 520 | \\[dirs] queries the shell and resyncs Emacs's idea of what the current |
be9b65ac | 521 | directory stack is. |
a034393c GM |
522 | \\[shell-dirtrack-mode] turns directory tracking on and off. |
523 | \(The `dirtrack' package provides an alternative implementation of this | |
c004e539 | 524 | feature - see the function `dirtrack-mode'.) |
be9b65ac DL |
525 | |
526 | \\{shell-mode-map} | |
24fdffaa | 527 | Customization: Entry to this mode runs the hooks on `comint-mode-hook' and |
988a4d60 | 528 | `shell-mode-hook' (in that order). Before each input, the hooks on |
ba02c167 RS |
529 | `comint-input-filter-functions' are run. After each shell output, the hooks |
530 | on `comint-output-filter-functions' are run. | |
be9b65ac | 531 | |
84728570 SS |
532 | Variables `shell-cd-regexp', `shell-chdrive-regexp', `shell-pushd-regexp' |
533 | and `shell-popd-regexp' are used to match their respective commands, | |
534 | while `shell-pushd-tohome', `shell-pushd-dextract' and `shell-pushd-dunique' | |
32d01212 | 535 | control the behavior of the relevant command. |
d76b71af | 536 | |
e11284d5 RS |
537 | Variables `comint-completion-autolist', `comint-completion-addsuffix', |
538 | `comint-completion-recexact' and `comint-completion-fignore' control the | |
539 | behavior of file name, command name and variable name completion. Variable | |
540 | `shell-completion-execonly' controls the behavior of command name completion. | |
2df2f980 | 541 | Variable `shell-completion-fignore' is used to initialize the value of |
e11284d5 | 542 | `comint-completion-fignore'. |
d76b71af RS |
543 | |
544 | Variables `comint-input-ring-file-name' and `comint-input-autoexpand' control | |
ab646e49 | 545 | the initialization of the input ring history, and history expansion. |
d76b71af | 546 | |
ba02c167 | 547 | Variables `comint-output-filter-functions', a hook, and |
e11284d5 | 548 | `comint-scroll-to-bottom-on-input' and `comint-scroll-to-bottom-on-output' |
988a4d60 RS |
549 | control whether input and output cause the window to scroll to the end of the |
550 | buffer." | |
d76b71af | 551 | (setq comint-prompt-regexp shell-prompt-pattern) |
914a0ae1 | 552 | (shell-completion-vars) |
e03035e3 | 553 | (set (make-local-variable 'paragraph-separate) "\\'") |
914a0ae1 SM |
554 | (set (make-local-variable 'paragraph-start) comint-prompt-regexp) |
555 | (set (make-local-variable 'font-lock-defaults) '(shell-font-lock-keywords t)) | |
556 | (set (make-local-variable 'shell-dirstack) nil) | |
557 | (set (make-local-variable 'shell-last-dir) nil) | |
11ee8d90 | 558 | (shell-dirtrack-mode 1) |
0fd40f89 CY |
559 | |
560 | ;; By default, ansi-color applies faces using overlays. This is | |
561 | ;; very inefficient in Shell buffers (e.g. Bug#10835). We use a | |
562 | ;; custom `ansi-color-apply-face-function' to convert color escape | |
563 | ;; sequences into `font-lock-face' properties. | |
564 | (set (make-local-variable 'ansi-color-apply-face-function) | |
565 | (lambda (beg end face) | |
566 | (when face | |
567 | (put-text-property beg end 'font-lock-face face)))) | |
568 | ||
5d9719e6 RS |
569 | ;; This is not really correct, since the shell buffer does not really |
570 | ;; edit this directory. But it is useful in the buffer list and menus. | |
5d9719e6 | 571 | (setq list-buffers-directory (expand-file-name default-directory)) |
d76b71af | 572 | ;; shell-dependent assignments. |
6ba0fd3e | 573 | (when (ring-empty-p comint-input-ring) |
ee8107c0 | 574 | (let ((shell (file-name-nondirectory (car |
0be6f4f1 GM |
575 | (process-command (get-buffer-process (current-buffer)))))) |
576 | (hsize (getenv "HISTSIZE"))) | |
577 | (and (stringp hsize) | |
578 | (integerp (setq hsize (string-to-number hsize))) | |
579 | (> hsize 0) | |
580 | (set (make-local-variable 'comint-input-ring-size) hsize)) | |
ee8107c0 RS |
581 | (setq comint-input-ring-file-name |
582 | (or (getenv "HISTFILE") | |
583 | (cond ((string-equal shell "bash") "~/.bash_history") | |
584 | ((string-equal shell "ksh") "~/.sh_history") | |
585 | (t "~/.history")))) | |
586 | (if (or (equal comint-input-ring-file-name "") | |
587 | (equal (file-truename comint-input-ring-file-name) | |
588 | (file-truename "/dev/null"))) | |
589 | (setq comint-input-ring-file-name nil)) | |
590 | ;; Arrange to write out the input ring on exit, if the shell doesn't | |
591 | ;; do this itself. | |
592 | (if (and comint-input-ring-file-name | |
593 | (string-match shell-dumb-shell-regexp shell)) | |
594 | (set-process-sentinel (get-buffer-process (current-buffer)) | |
595 | #'shell-write-history-on-exit)) | |
596 | (setq shell-dirstack-query | |
597 | (cond ((string-equal shell "sh") "pwd") | |
598 | ((string-equal shell "ksh") "echo $PWD ~-") | |
3811bec8 LK |
599 | (t "dirs"))) |
600 | ;; Bypass a bug in certain versions of bash. | |
601 | (when (string-equal shell "bash") | |
c0a193ea | 602 | (add-hook 'comint-preoutput-filter-functions |
3811bec8 | 603 | 'shell-filter-ctrl-a-ctrl-b nil t))) |
ee8107c0 | 604 | (comint-read-input-ring t))) |
4dced927 | 605 | |
c0a193ea | 606 | (defun shell-filter-ctrl-a-ctrl-b (string) |
3811bec8 LK |
607 | "Remove `^A' and `^B' characters from comint output. |
608 | ||
609 | Bash uses these characters as internal quoting characters in its | |
610 | prompt. Due to a bug in some bash versions (including 2.03, | |
611 | 2.04, and 2.05b), they may erroneously show up when bash is | |
612 | started with the `--noediting' option and Select Graphic | |
613 | Rendition (SGR) control sequences (formerly known as ANSI escape | |
614 | sequences) are used to color the prompt. | |
615 | ||
c0a193ea SM |
616 | This function can be put on `comint-preoutput-filter-functions'." |
617 | (if (string-match "[\C-a\C-b]" string) | |
618 | (replace-regexp-in-string "[\C-a\C-b]" "" string t t) | |
619 | string)) | |
3811bec8 | 620 | |
4dced927 AI |
621 | (defun shell-write-history-on-exit (process event) |
622 | "Called when the shell process is stopped. | |
623 | ||
624 | Writes the input history to a history file | |
84728570 | 625 | `comint-input-ring-file-name' using `comint-write-input-ring' |
4dced927 AI |
626 | and inserts a short message in the shell buffer. |
627 | ||
628 | This function is a sentinel watching the shell interpreter process. | |
629 | Sentinels will always get the two parameters PROCESS and EVENT." | |
630 | ;; Write history. | |
631 | (comint-write-input-ring) | |
84728570 SS |
632 | (let ((buf (process-buffer process))) |
633 | (when (buffer-live-p buf) | |
634 | (with-current-buffer buf | |
635 | (insert (format "\nProcess %s %s\n" process event)))))) | |
636 | ||
c88ab9ce | 637 | ;;;###autoload |
fd1035aa MB |
638 | (defun shell (&optional buffer) |
639 | "Run an inferior shell, with I/O through BUFFER (which defaults to `*shell*'). | |
640 | Interactively, a prefix arg means to prompt for BUFFER. | |
66a100be MA |
641 | If `default-directory' is a remote file name, it is also prompted |
642 | to change if called with a prefix arg. | |
643 | ||
fd1035aa MB |
644 | If BUFFER exists but shell process is not running, make new shell. |
645 | If BUFFER exists and shell process is running, just switch to BUFFER. | |
24fdffaa | 646 | Program used comes from variable `explicit-shell-file-name', |
a9ec2adb | 647 | or (if that is nil) from the ESHELL environment variable, |
c3342e8e | 648 | or (if that is nil) from `shell-file-name'. |
ec1d7c6e | 649 | If a file `~/.emacs_SHELLNAME' exists, or `~/.emacs.d/init_SHELLNAME.sh', |
71af6fc4 RS |
650 | it is given as initial input (but this may be lost, due to a timing |
651 | error, if the shell discards input when it starts up). | |
fbc270e7 RS |
652 | The buffer is put in Shell mode, giving commands for sending input |
653 | and controlling the subjobs of the shell. See `shell-mode'. | |
654 | See also the variable `shell-prompt-pattern'. | |
be9b65ac | 655 | |
ebe4d555 RS |
656 | To specify a coding system for converting non-ASCII characters |
657 | in the input and output to the shell, use \\[universal-coding-system-argument] | |
658 | before \\[shell]. You can also specify this with \\[set-buffer-process-coding-system] | |
659 | in the shell buffer, after you start the shell. | |
660 | The default comes from `process-coding-system-alist' and | |
661 | `default-process-coding-system'. | |
662 | ||
a9ec2adb | 663 | The shell file name (sans directories) is used to make a symbol name |
62c9fad7 | 664 | such as `explicit-csh-args'. If that symbol is a variable, |
be9b65ac DL |
665 | its value is used as a list of arguments when invoking the shell. |
666 | Otherwise, one argument `-i' is passed to the shell. | |
667 | ||
668 | \(Type \\[describe-mode] in the shell buffer for a list of commands.)" | |
fd1035aa MB |
669 | (interactive |
670 | (list | |
671 | (and current-prefix-arg | |
d9af6951 AS |
672 | (prog1 |
673 | (read-buffer "Shell buffer: " | |
a813fe3f CY |
674 | ;; If the current buffer is an inactive |
675 | ;; shell buffer, use it as the default. | |
676 | (if (and (eq major-mode 'shell-mode) | |
677 | (null (get-buffer-process (current-buffer)))) | |
678 | (buffer-name) | |
679 | (generate-new-buffer-name "*shell*"))) | |
d9af6951 AS |
680 | (if (file-remote-p default-directory) |
681 | ;; It must be possible to declare a local default-directory. | |
8f5f1e68 SM |
682 | ;; FIXME: This can't be right: it changes the default-directory |
683 | ;; of the current-buffer rather than of the *shell* buffer. | |
d9af6951 AS |
684 | (setq default-directory |
685 | (expand-file-name | |
7e27ce9c | 686 | (read-directory-name |
d9af6951 | 687 | "Default directory: " default-directory default-directory |
7e27ce9c | 688 | t nil)))))))) |
8f5f1e68 SM |
689 | (setq buffer (if (or buffer (not (derived-mode-p 'shell-mode)) |
690 | (comint-check-proc (current-buffer))) | |
691 | (get-buffer-create (or buffer "*shell*")) | |
692 | ;; If the current buffer is a dead shell buffer, use it. | |
693 | (current-buffer))) | |
77c992bc MA |
694 | |
695 | ;; On remote hosts, the local `shell-file-name' might be useless. | |
06b60517 | 696 | (if (and (called-interactively-p 'any) |
77c992bc MA |
697 | (file-remote-p default-directory) |
698 | (null explicit-shell-file-name) | |
699 | (null (getenv "ESHELL"))) | |
700 | (with-current-buffer buffer | |
701 | (set (make-local-variable 'explicit-shell-file-name) | |
702 | (file-remote-p | |
703 | (expand-file-name | |
704 | (read-file-name | |
705 | "Remote shell path: " default-directory shell-file-name | |
706 | t shell-file-name)) | |
707 | 'localname)))) | |
708 | ||
37ac18a3 CY |
709 | ;; The buffer's window must be correctly set when we call comint (so |
710 | ;; that comint sets the COLUMNS env var properly). | |
72258fe5 | 711 | (pop-to-buffer-same-window buffer) |
5a45735a SM |
712 | (unless (comint-check-proc buffer) |
713 | (let* ((prog (or explicit-shell-file-name | |
714 | (getenv "ESHELL") shell-file-name)) | |
715 | (name (file-name-nondirectory prog)) | |
716 | (startfile (concat "~/.emacs_" name)) | |
717 | (xargs-name (intern-soft (concat "explicit-" name "-args")))) | |
ec1d7c6e | 718 | (unless (file-exists-p startfile) |
f7c94f43 | 719 | (setq startfile (concat user-emacs-directory "init_" name ".sh"))) |
5a45735a SM |
720 | (apply 'make-comint-in-buffer "shell" buffer prog |
721 | (if (file-exists-p startfile) startfile) | |
722 | (if (and xargs-name (boundp xargs-name)) | |
723 | (symbol-value xargs-name) | |
724 | '("-i"))) | |
725 | (shell-mode))) | |
726 | buffer) | |
519bfec4 | 727 | |
be9b65ac | 728 | ;;; Directory tracking |
05327ca9 SM |
729 | ;; |
730 | ;; This code provides the shell mode input sentinel | |
731 | ;; SHELL-DIRECTORY-TRACKER | |
732 | ;; that tracks cd, pushd, and popd commands issued to the shell, and | |
733 | ;; changes the current directory of the shell buffer accordingly. | |
734 | ;; | |
735 | ;; This is basically a fragile hack, although it's more accurate than | |
736 | ;; the version in Emacs 18's shell.el. It has the following failings: | |
737 | ;; 1. It doesn't know about the cdpath shell variable. | |
738 | ;; 2. It cannot infallibly deal with command sequences, though it does well | |
739 | ;; with these and with ignoring commands forked in another shell with ()s. | |
740 | ;; 3. More generally, any complex command is going to throw it. Otherwise, | |
741 | ;; you'd have to build an entire shell interpreter in Emacs Lisp. Failing | |
742 | ;; that, there's no way to catch shell commands where cd's are buried | |
743 | ;; inside conditional expressions, aliases, and so forth. | |
744 | ;; | |
745 | ;; The whole approach is a crock. Shell aliases mess it up. File sourcing | |
746 | ;; messes it up. You run other processes under the shell; these each have | |
747 | ;; separate working directories, and some have commands for manipulating | |
748 | ;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have | |
749 | ;; commands that do *not* affect the current w.d. at all, but look like they | |
750 | ;; do (e.g., the cd command in ftp). In shells that allow you job | |
751 | ;; control, you can switch between jobs, all having different w.d.'s. So | |
752 | ;; simply saying %3 can shift your w.d.. | |
753 | ;; | |
754 | ;; The solution is to relax, not stress out about it, and settle for | |
755 | ;; a hack that works pretty well in typical circumstances. Remember | |
756 | ;; that a half-assed solution is more in keeping with the spirit of Unix, | |
757 | ;; anyway. Blech. | |
758 | ;; | |
759 | ;; One good hack not implemented here for users of programmable shells | |
760 | ;; is to program up the shell w.d. manipulation commands to output | |
761 | ;; a coded command sequence to the tty. Something like | |
762 | ;; ESC | <cwd> | | |
763 | ;; where <cwd> is the new current working directory. Then trash the | |
764 | ;; directory tracking machinery currently used in this package, and | |
765 | ;; replace it with a process filter that watches for and strips out | |
766 | ;; these messages. | |
be9b65ac | 767 | |
be9b65ac DL |
768 | (defun shell-directory-tracker (str) |
769 | "Tracks cd, pushd and popd commands issued to the shell. | |
770 | This function is called on each input passed to the shell. | |
771 | It watches for cd, pushd and popd commands and sets the buffer's | |
772 | default directory to track these commands. | |
773 | ||
a034393c | 774 | You may toggle this tracking on and off with \\[shell-dirtrack-mode]. |
05327ca9 | 775 | If Emacs gets confused, you can resync with the shell with \\[dirs]. |
c004e539 GM |
776 | \(The `dirtrack' package provides an alternative implementation of this |
777 | feature - see the function `dirtrack-mode'.) | |
be9b65ac | 778 | |
32d01212 | 779 | See variables `shell-cd-regexp', `shell-chdrive-regexp', `shell-pushd-regexp', |
84728570 | 780 | and `shell-popd-regexp', while `shell-pushd-tohome', `shell-pushd-dextract', |
32d01212 | 781 | and `shell-pushd-dunique' control the behavior of the relevant command. |
d76b71af | 782 | |
24fdffaa | 783 | Environment variables are expanded, see function `substitute-in-file-name'." |
d76b71af RS |
784 | (if shell-dirtrackp |
785 | ;; We fail gracefully if we think the command will fail in the shell. | |
06b60517 | 786 | (condition-case nil |
6ed020c8 JB |
787 | (let ((start (progn (string-match |
788 | (concat "^" shell-command-separator-regexp) | |
789 | str) ; skip whitespace | |
d76b71af | 790 | (match-end 0))) |
4b29d9fb | 791 | (case-fold-search) |
d76b71af RS |
792 | end cmd arg1) |
793 | (while (string-match shell-command-regexp str start) | |
794 | (setq end (match-end 0) | |
795 | cmd (comint-arguments (substring str start end) 0 0) | |
796 | arg1 (comint-arguments (substring str start end) 1 1)) | |
5f49d5e8 KH |
797 | (if arg1 |
798 | (setq arg1 (shell-unquote-argument arg1))) | |
54021695 KH |
799 | (cond ((string-match (concat "\\`\\(" shell-popd-regexp |
800 | "\\)\\($\\|[ \t]\\)") | |
801 | cmd) | |
32d01212 | 802 | (shell-process-popd (comint-substitute-in-file-name arg1))) |
54021695 KH |
803 | ((string-match (concat "\\`\\(" shell-pushd-regexp |
804 | "\\)\\($\\|[ \t]\\)") | |
805 | cmd) | |
32d01212 | 806 | (shell-process-pushd (comint-substitute-in-file-name arg1))) |
54021695 KH |
807 | ((string-match (concat "\\`\\(" shell-cd-regexp |
808 | "\\)\\($\\|[ \t]\\)") | |
809 | cmd) | |
32d01212 RS |
810 | (shell-process-cd (comint-substitute-in-file-name arg1))) |
811 | ((and shell-chdrive-regexp | |
812 | (string-match (concat "\\`\\(" shell-chdrive-regexp | |
813 | "\\)\\($\\|[ \t]\\)") | |
814 | cmd)) | |
815 | (shell-process-cd (comint-substitute-in-file-name cmd)))) | |
6ed020c8 JB |
816 | (setq start (progn (string-match shell-command-separator-regexp |
817 | str end) | |
818 | ;; skip again | |
d76b71af | 819 | (match-end 0))))) |
ba02c167 | 820 | (error "Couldn't cd")))) |
d76b71af | 821 | |
5f49d5e8 KH |
822 | (defun shell-unquote-argument (string) |
823 | "Remove all kinds of shell quoting from STRING." | |
824 | (save-match-data | |
f5058b96 EZ |
825 | (let ((idx 0) next inside |
826 | (quote-chars | |
827 | (if (string-match shell-dumb-shell-regexp | |
828 | (file-name-nondirectory | |
829 | (car (process-command (get-buffer-process (current-buffer)))))) | |
830 | "['`\"]" | |
831 | "[\\'`\"]"))) | |
5f49d5e8 | 832 | (while (and (< idx (length string)) |
f5058b96 | 833 | (setq next (string-match quote-chars string next))) |
5f49d5e8 KH |
834 | (cond ((= (aref string next) ?\\) |
835 | (setq string (replace-match "" nil nil string)) | |
836 | (setq next (1+ next))) | |
837 | ((and inside (= (aref string next) inside)) | |
838 | (setq string (replace-match "" nil nil string)) | |
839 | (setq inside nil)) | |
840 | (inside | |
841 | (setq next (1+ next))) | |
842 | (t | |
843 | (setq inside (aref string next)) | |
844 | (setq string (replace-match "" nil nil string))))) | |
845 | string))) | |
846 | ||
05327ca9 | 847 | ;; popd [+n] |
be9b65ac | 848 | (defun shell-process-popd (arg) |
d76b71af RS |
849 | (let ((num (or (shell-extract-num arg) 0))) |
850 | (cond ((and num (= num 0) shell-dirstack) | |
f8aefe82 | 851 | (shell-cd (shell-prefixed-directory-name (car shell-dirstack))) |
d76b71af RS |
852 | (setq shell-dirstack (cdr shell-dirstack)) |
853 | (shell-dirstack-message)) | |
854 | ((and num (> num 0) (<= num (length shell-dirstack))) | |
855 | (let* ((ds (cons nil shell-dirstack)) | |
856 | (cell (nthcdr (1- num) ds))) | |
857 | (rplacd cell (cdr (cdr cell))) | |
858 | (setq shell-dirstack (cdr ds)) | |
859 | (shell-dirstack-message))) | |
860 | (t | |
ba02c167 | 861 | (error "Couldn't popd"))))) |
be9b65ac | 862 | |
3db9a4eb | 863 | ;; Return DIR prefixed with comint-file-name-prefix as appropriate. |
4a02b552 RS |
864 | (defun shell-prefixed-directory-name (dir) |
865 | (if (= (length comint-file-name-prefix) 0) | |
866 | dir | |
867 | (if (file-name-absolute-p dir) | |
868 | ;; The name is absolute, so prepend the prefix. | |
869 | (concat comint-file-name-prefix dir) | |
0b3c9cf1 | 870 | ;; For relative name we assume default-directory already has the prefix. |
4a02b552 | 871 | (expand-file-name dir)))) |
3db9a4eb | 872 | |
05327ca9 | 873 | ;; cd [dir] |
be9b65ac | 874 | (defun shell-process-cd (arg) |
3db9a4eb RM |
875 | (let ((new-dir (cond ((zerop (length arg)) (concat comint-file-name-prefix |
876 | "~")) | |
d76b71af | 877 | ((string-equal "-" arg) shell-last-dir) |
3db9a4eb | 878 | (t (shell-prefixed-directory-name arg))))) |
d76b71af | 879 | (setq shell-last-dir default-directory) |
0b3c9cf1 | 880 | (shell-cd new-dir) |
d76b71af | 881 | (shell-dirstack-message))) |
be9b65ac | 882 | |
05327ca9 | 883 | ;; pushd [+n | dir] |
be9b65ac | 884 | (defun shell-process-pushd (arg) |
d76b71af RS |
885 | (let ((num (shell-extract-num arg))) |
886 | (cond ((zerop (length arg)) | |
887 | ;; no arg -- swap pwd and car of stack unless shell-pushd-tohome | |
888 | (cond (shell-pushd-tohome | |
3db9a4eb | 889 | (shell-process-pushd (concat comint-file-name-prefix "~"))) |
d76b71af RS |
890 | (shell-dirstack |
891 | (let ((old default-directory)) | |
0b3c9cf1 | 892 | (shell-cd (car shell-dirstack)) |
221ca406 | 893 | (setq shell-dirstack (cons old (cdr shell-dirstack))) |
d76b71af RS |
894 | (shell-dirstack-message))) |
895 | (t | |
896 | (message "Directory stack empty.")))) | |
897 | ((numberp num) | |
898 | ;; pushd +n | |
899 | (cond ((> num (length shell-dirstack)) | |
900 | (message "Directory stack not that deep.")) | |
901 | ((= num 0) | |
1cd7adc6 | 902 | (error (message "Couldn't cd"))) |
d76b71af RS |
903 | (shell-pushd-dextract |
904 | (let ((dir (nth (1- num) shell-dirstack))) | |
905 | (shell-process-popd arg) | |
906 | (shell-process-pushd default-directory) | |
0b3c9cf1 | 907 | (shell-cd dir) |
d76b71af RS |
908 | (shell-dirstack-message))) |
909 | (t | |
910 | (let* ((ds (cons default-directory shell-dirstack)) | |
911 | (dslen (length ds)) | |
912 | (front (nthcdr num ds)) | |
913 | (back (reverse (nthcdr (- dslen num) (reverse ds)))) | |
914 | (new-ds (append front back))) | |
0b3c9cf1 | 915 | (shell-cd (car new-ds)) |
d76b71af RS |
916 | (setq shell-dirstack (cdr new-ds)) |
917 | (shell-dirstack-message))))) | |
918 | (t | |
919 | ;; pushd <dir> | |
920 | (let ((old-wd default-directory)) | |
0b3c9cf1 | 921 | (shell-cd (shell-prefixed-directory-name arg)) |
d76b71af RS |
922 | (if (or (null shell-pushd-dunique) |
923 | (not (member old-wd shell-dirstack))) | |
924 | (setq shell-dirstack (cons old-wd shell-dirstack))) | |
925 | (shell-dirstack-message)))))) | |
be9b65ac DL |
926 | |
927 | ;; If STR is of the form +n, for n>0, return n. Otherwise, nil. | |
928 | (defun shell-extract-num (str) | |
929 | (and (string-match "^\\+[1-9][0-9]*$" str) | |
027a4b6b | 930 | (string-to-number str))) |
be9b65ac | 931 | |
05327ca9 SM |
932 | (defvaralias 'shell-dirtrack-mode 'shell-dirtrackp) |
933 | (define-minor-mode shell-dirtrack-mode | |
06e21633 CY |
934 | "Toggle directory tracking in this shell buffer (Shell Dirtrack mode). |
935 | With a prefix argument ARG, enable Shell Dirtrack mode if ARG is | |
936 | positive, and disable it otherwise. If called from Lisp, enable | |
937 | the mode if ARG is omitted or nil. | |
938 | ||
939 | The `dirtrack' package provides an alternative implementation of | |
940 | this feature; see the function `dirtrack-mode'." | |
05327ca9 SM |
941 | nil nil nil |
942 | (setq list-buffers-directory (if shell-dirtrack-mode default-directory)) | |
943 | (if shell-dirtrack-mode | |
944 | (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t) | |
945 | (remove-hook 'comint-input-filter-functions 'shell-directory-tracker t))) | |
946 | ||
a034393c GM |
947 | (define-obsolete-function-alias 'shell-dirtrack-toggle 'shell-dirtrack-mode |
948 | "23.1") | |
be9b65ac | 949 | |
0b3c9cf1 SM |
950 | (defun shell-cd (dir) |
951 | "Do normal `cd' to DIR, and set `list-buffers-directory'." | |
05327ca9 | 952 | (cd dir) |
b7e97ed5 | 953 | (if shell-dirtrackp |
05327ca9 | 954 | (setq list-buffers-directory default-directory))) |
be9b65ac DL |
955 | |
956 | (defun shell-resync-dirs () | |
957 | "Resync the buffer's idea of the current directory stack. | |
84728570 | 958 | This command queries the shell with the command bound to |
24fdffaa | 959 | `shell-dirstack-query' (default \"dirs\"), reads the next |
be9b65ac DL |
960 | line output and parses it to form the new directory stack. |
961 | DON'T issue this command unless the buffer is at a shell prompt. | |
962 | Also, note that if some other subprocess decides to do output | |
963 | immediately after the query, its output will be taken as the | |
ab646e49 | 964 | new directory stack -- you lose. If this happens, just do the |
be9b65ac DL |
965 | command again." |
966 | (interactive) | |
967 | (let* ((proc (get-buffer-process (current-buffer))) | |
45a4af63 GM |
968 | (pmark (process-mark proc)) |
969 | (started-at-pmark (= (point) (marker-position pmark)))) | |
970 | (save-excursion | |
971 | (goto-char pmark) | |
972 | ;; If the process echoes commands, don't insert a fake command in | |
973 | ;; the buffer or it will appear twice. | |
974 | (unless comint-process-echoes | |
975 | (insert shell-dirstack-query) (insert "\n")) | |
976 | (sit-for 0) ; force redisplay | |
977 | (comint-send-string proc shell-dirstack-query) | |
978 | (comint-send-string proc "\n") | |
979 | (set-marker pmark (point)) | |
980 | (let ((pt (point)) | |
981 | (regexp | |
982 | (concat | |
983 | (if comint-process-echoes | |
984 | ;; Skip command echo if the process echoes | |
985 | (concat "\\(" (regexp-quote shell-dirstack-query) "\n\\)") | |
986 | "\\(\\)") | |
987 | "\\(.+\n\\)"))) | |
988 | ;; This extra newline prevents the user's pending input from spoofing us. | |
989 | (insert "\n") (backward-char 1) | |
990 | ;; Wait for one line. | |
991 | (while (not (looking-at regexp)) | |
992 | (accept-process-output proc) | |
993 | (goto-char pt))) | |
994 | (goto-char pmark) (delete-char 1) ; remove the extra newline | |
995 | ;; That's the dirlist. grab it & parse it. | |
996 | (let* ((dl (buffer-substring (match-beginning 2) (1- (match-end 2)))) | |
997 | (dl-len (length dl)) | |
998 | (ds '()) ; new dir stack | |
999 | (i 0)) | |
1000 | (while (< i dl-len) | |
1001 | ;; regexp = optional whitespace, (non-whitespace), optional whitespace | |
1002 | (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir | |
1003 | (setq ds (cons (concat comint-file-name-prefix | |
1004 | (substring dl (match-beginning 1) | |
1005 | (match-end 1))) | |
1006 | ds)) | |
1007 | (setq i (match-end 0))) | |
1008 | (let ((ds (nreverse ds))) | |
1009 | (condition-case nil | |
1010 | (progn (shell-cd (car ds)) | |
1011 | (setq shell-dirstack (cdr ds) | |
1012 | shell-last-dir (car shell-dirstack)) | |
1013 | (shell-dirstack-message)) | |
1014 | (error (message "Couldn't cd")))))) | |
1015 | (if started-at-pmark (goto-char (marker-position pmark))))) | |
be9b65ac | 1016 | |
05327ca9 | 1017 | ;; For your typing convenience: |
31e1d920 | 1018 | (defalias 'dirs 'shell-resync-dirs) |
be9b65ac DL |
1019 | |
1020 | ||
05327ca9 SM |
1021 | ;; Show the current dirstack on the message line. |
1022 | ;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo". | |
1023 | ;; (This isn't necessary if the dirlisting is generated with a simple "dirs".) | |
1024 | ;; All the commands that mung the buffer's dirstack finish by calling | |
1025 | ;; this guy. | |
be9b65ac | 1026 | (defun shell-dirstack-message () |
8a6387ed SM |
1027 | (when shell-dirtrack-verbose |
1028 | (let* ((msg "") | |
1029 | (ds (cons default-directory shell-dirstack)) | |
1030 | (home (expand-file-name (concat comint-file-name-prefix "~/"))) | |
1031 | (homelen (length home))) | |
1032 | (while ds | |
1033 | (let ((dir (car ds))) | |
1034 | (and (>= (length dir) homelen) | |
1035 | (string= home (substring dir 0 homelen)) | |
1036 | (setq dir (concat "~/" (substring dir homelen)))) | |
1037 | ;; Strip off comint-file-name-prefix if present. | |
1038 | (and comint-file-name-prefix | |
1039 | (>= (length dir) (length comint-file-name-prefix)) | |
1040 | (string= comint-file-name-prefix | |
1041 | (substring dir 0 (length comint-file-name-prefix))) | |
1042 | (setq dir (substring dir (length comint-file-name-prefix))) | |
1043 | (setcar ds dir)) | |
1044 | (setq msg (concat msg (directory-file-name dir) " ")) | |
1045 | (setq ds (cdr ds)))) | |
1046 | (message "%s" msg)))) | |
84728570 | 1047 | |
c64d8c55 RS |
1048 | ;; This was mostly copied from shell-resync-dirs. |
1049 | (defun shell-snarf-envar (var) | |
1050 | "Return as a string the shell's value of environment variable VAR." | |
1051 | (let* ((cmd (format "printenv '%s'\n" var)) | |
1052 | (proc (get-buffer-process (current-buffer))) | |
1053 | (pmark (process-mark proc))) | |
1054 | (goto-char pmark) | |
1055 | (insert cmd) | |
1056 | (sit-for 0) ; force redisplay | |
1057 | (comint-send-string proc cmd) | |
1058 | (set-marker pmark (point)) | |
1059 | (let ((pt (point))) ; wait for 1 line | |
1060 | ;; This extra newline prevents the user's pending input from spoofing us. | |
1061 | (insert "\n") (backward-char 1) | |
1062 | (while (not (looking-at ".+\n")) | |
1063 | (accept-process-output proc) | |
1064 | (goto-char pt))) | |
1065 | (goto-char pmark) (delete-char 1) ; remove the extra newline | |
1066 | (buffer-substring (match-beginning 0) (1- (match-end 0))))) | |
1067 | ||
1068 | (defun shell-copy-environment-variable (variable) | |
1069 | "Copy the environment variable VARIABLE from the subshell to Emacs. | |
1070 | This command reads the value of the specified environment variable | |
1071 | in the shell, and sets the same environment variable in Emacs | |
a9e73449 | 1072 | \(what `getenv' in Emacs would return) to that value. |
c64d8c55 RS |
1073 | That value will affect any new subprocesses that you subsequently start |
1074 | from Emacs." | |
1075 | (interactive (list (read-envvar-name "\ | |
1076 | Copy Shell environment variable to Emacs: "))) | |
1077 | (setenv variable (shell-snarf-envar variable))) | |
84728570 | 1078 | |
d76b71af RS |
1079 | (defun shell-forward-command (&optional arg) |
1080 | "Move forward across ARG shell command(s). Does not cross lines. | |
1081 | See `shell-command-regexp'." | |
1082 | (interactive "p") | |
5ed619e0 | 1083 | (let ((limit (line-end-position))) |
19134c18 | 1084 | (if (re-search-forward (concat shell-command-regexp "\\([;&|][\t ]*\\)+") |
d76b71af | 1085 | limit 'move arg) |
b0f86743 | 1086 | (skip-syntax-backward " ")))) |
d76b71af RS |
1087 | |
1088 | ||
1089 | (defun shell-backward-command (&optional arg) | |
1090 | "Move backward across ARG shell command(s). Does not cross lines. | |
1091 | See `shell-command-regexp'." | |
1092 | (interactive "p") | |
1093 | (let ((limit (save-excursion (comint-bol nil) (point)))) | |
28bc09d6 MB |
1094 | (when (> limit (point)) |
1095 | (setq limit (line-beginning-position))) | |
b0f86743 | 1096 | (skip-syntax-backward " " limit) |
d76b71af | 1097 | (if (re-search-backward |
19134c18 | 1098 | (format "[;&|]+[\t ]*\\(%s\\)" shell-command-regexp) limit 'move arg) |
d76b71af | 1099 | (progn (goto-char (match-beginning 1)) |
b0f86743 | 1100 | (skip-chars-forward ";&|"))))) |
d76b71af | 1101 | |
d76b71af RS |
1102 | (defun shell-dynamic-complete-command () |
1103 | "Dynamically complete the command at point. | |
1104 | This function is similar to `comint-dynamic-complete-filename', except that it | |
5113b294 | 1105 | searches `exec-path' (minus the trailing Emacs library path) for completion |
d76b71af RS |
1106 | candidates. Note that this may not be the same as the shell's idea of the |
1107 | path. | |
1108 | ||
1109 | Completion is dependent on the value of `shell-completion-execonly', plus | |
c0a193ea | 1110 | those that effect file completion. |
988a4d60 RS |
1111 | |
1112 | Returns t if successful." | |
d76b71af | 1113 | (interactive) |
c0a193ea SM |
1114 | (let ((data (shell-command-completion))) |
1115 | (if data | |
1116 | (prog2 (unless (window-minibuffer-p (selected-window)) | |
1117 | (message "Completing command name...")) | |
1118 | (apply #'completion-in-region data))))) | |
1119 | ||
1120 | (defun shell-command-completion () | |
1121 | "Return the completion data for the command at point, if any." | |
988a4d60 RS |
1122 | (let ((filename (comint-match-partial-filename))) |
1123 | (if (and filename | |
1124 | (save-match-data (not (string-match "[~/]" filename))) | |
1125 | (eq (match-beginning 0) | |
1126 | (save-excursion (shell-backward-command 1) (point)))) | |
c0a193ea | 1127 | (shell--command-completion-data)))) |
988a4d60 | 1128 | |
c0a193ea SM |
1129 | (defun shell--command-completion-data () |
1130 | "Return the completion data for the command at point." | |
56783585 | 1131 | (let* ((filename (or (comint-match-partial-filename) "")) |
c0a193ea SM |
1132 | (start (if (zerop (length filename)) (point) (match-beginning 0))) |
1133 | (end (if (zerop (length filename)) (point) (match-end 0))) | |
f0d5dc24 | 1134 | (filenondir (file-name-nondirectory filename)) |
c0a193ea | 1135 | (path-dirs (cdr (reverse exec-path))) ;FIXME: Why `cdr'? |
d76b71af RS |
1136 | (cwd (file-name-as-directory (expand-file-name default-directory))) |
1137 | (ignored-extensions | |
a867a90a | 1138 | (and comint-completion-fignore |
c0a193ea | 1139 | (mapconcat (function (lambda (x) (concat (regexp-quote x) "\\'"))) |
a867a90a | 1140 | comint-completion-fignore "\\|"))) |
2df2f980 | 1141 | (dir "") (comps-in-dir ()) |
f0d5dc24 RS |
1142 | (file "") (abs-file-name "") (completions ())) |
1143 | ;; Go thru each dir in the search path, finding completions. | |
1144 | (while path-dirs | |
1145 | (setq dir (file-name-as-directory (comint-directory (or (car path-dirs) "."))) | |
1146 | comps-in-dir (and (file-accessible-directory-p dir) | |
1147 | (file-name-all-completions filenondir dir))) | |
d76b71af | 1148 | ;; Go thru each completion found, to see whether it should be used. |
f0d5dc24 RS |
1149 | (while comps-in-dir |
1150 | (setq file (car comps-in-dir) | |
1151 | abs-file-name (concat dir file)) | |
d76b71af | 1152 | (if (and (not (member file completions)) |
7b2db0a0 KH |
1153 | (not (and ignored-extensions |
1154 | (string-match ignored-extensions file))) | |
f0d5dc24 RS |
1155 | (or (string-equal dir cwd) |
1156 | (not (file-directory-p abs-file-name))) | |
d76b71af | 1157 | (or (null shell-completion-execonly) |
f0d5dc24 | 1158 | (file-executable-p abs-file-name))) |
d76b71af | 1159 | (setq completions (cons file completions))) |
f0d5dc24 RS |
1160 | (setq comps-in-dir (cdr comps-in-dir))) |
1161 | (setq path-dirs (cdr path-dirs))) | |
d76b71af | 1162 | ;; OK, we've got a list of completions. |
c0a193ea SM |
1163 | (list |
1164 | start end | |
1165 | (lambda (string pred action) | |
a2a25d24 SM |
1166 | (if (string-match "/" string) |
1167 | (completion-file-name-table string pred action) | |
1168 | (complete-with-action action completions string pred))) | |
1169 | :exit-function | |
1170 | (lambda (_string finished) | |
1171 | (when (memq finished '(sole finished)) | |
1172 | (if (looking-at " ") | |
1173 | (goto-char (match-end 0)) | |
1174 | (insert " "))))))) | |
c0a193ea SM |
1175 | |
1176 | ;; (defun shell-dynamic-complete-as-command () | |
1177 | ;; "Dynamically complete at point as a command. | |
1178 | ;; See `shell-dynamic-complete-filename'. Returns t if successful." | |
1179 | ;; (apply #'completion-in-region shell--command-completion-data)) | |
988a4d60 | 1180 | |
a086d081 CY |
1181 | (defun shell-dynamic-complete-filename () |
1182 | "Dynamically complete the filename at point. | |
1183 | This completes only if point is at a suitable position for a | |
1184 | filename argument." | |
1185 | (interactive) | |
c0a193ea SM |
1186 | (let ((data (shell-filename-completion))) |
1187 | (if data (apply #'completion-in-region data)))) | |
1188 | ||
1189 | (defun shell-filename-completion () | |
1190 | "Return the completion data for file name at point, if any." | |
a086d081 CY |
1191 | (let ((opoint (point)) |
1192 | (beg (comint-line-beginning-position))) | |
1193 | (when (save-excursion | |
1194 | (goto-char (if (re-search-backward "[;|&]" beg t) | |
1195 | (match-end 0) | |
1196 | beg)) | |
1197 | (re-search-forward "[^ \t][ \t]" opoint t)) | |
c0a193ea | 1198 | (comint-filename-completion)))) |
988a4d60 RS |
1199 | |
1200 | (defun shell-match-partial-variable () | |
da6a48c9 | 1201 | "Return the shell variable at point, or nil if none is found." |
988a4d60 | 1202 | (save-excursion |
c0a193ea SM |
1203 | (if (re-search-backward "[^A-Za-z0-9_{(]" nil 'move) |
1204 | (or (looking-at "\\$") (forward-char 1))) | |
1205 | (if (or (eolp) (looking-at "[^A-Za-z0-9_{($]")) | |
1206 | nil | |
1207 | (looking-at "\\$?[{(]?[A-Za-z0-9_]*[})]?") | |
1208 | (buffer-substring (match-beginning 0) (match-end 0))))) | |
a9ec2adb | 1209 | |
988a4d60 RS |
1210 | (defun shell-dynamic-complete-environment-variable () |
1211 | "Dynamically complete the environment variable at point. | |
1212 | Completes if after a variable, i.e., if it starts with a \"$\". | |
988a4d60 RS |
1213 | |
1214 | This function is similar to `comint-dynamic-complete-filename', except that it | |
1215 | searches `process-environment' for completion candidates. Note that this may | |
1216 | not be the same as the interpreter's idea of variable names. The main problem | |
1217 | with this type of completion is that `process-environment' is the environment | |
1218 | which Emacs started with. Emacs does not track changes to the environment made | |
1219 | by the interpreter. Perhaps it would be more accurate if this function was | |
1220 | called `shell-dynamic-complete-process-environment-variable'. | |
1221 | ||
1222 | Returns non-nil if successful." | |
1223 | (interactive) | |
c0a193ea SM |
1224 | (let ((data (shell-environment-variable-completion))) |
1225 | (if data | |
c35be7ec JL |
1226 | (prog2 (unless (window-minibuffer-p (selected-window)) |
1227 | (message "Completing variable name...")) | |
c0a193ea SM |
1228 | (apply #'completion-in-region data))))) |
1229 | ||
1230 | ||
1231 | (defun shell-environment-variable-completion () | |
1232 | "Completion data for an environment variable at point, if any." | |
1233 | (let* ((var (shell-match-partial-variable)) | |
1234 | (end (match-end 0))) | |
1235 | (when (and (not (zerop (length var))) (eq (aref var 0) ?$)) | |
1236 | (let* ((start | |
1237 | (save-excursion | |
1238 | (goto-char (match-beginning 0)) | |
1239 | (looking-at "\\$?[({]*") | |
1240 | (match-end 0))) | |
1241 | (variables (mapcar (lambda (x) | |
1242 | (substring x 0 (string-match "=" x))) | |
1243 | process-environment)) | |
a464a6c7 | 1244 | (suffix (pcase (char-before start) (?\{ "}") (?\( ")") (_ "")))) |
a2a25d24 SM |
1245 | (list start end variables |
1246 | :exit-function | |
1247 | (lambda (s finished) | |
1248 | (when (memq finished '(sole finished)) | |
1249 | (let ((suf (concat suffix | |
1250 | (if (file-directory-p | |
1251 | (comint-directory (getenv s))) | |
1252 | "/")))) | |
1253 | (if (looking-at (regexp-quote suf)) | |
1254 | (goto-char (match-end 0)) | |
1255 | (insert suf)))))))))) | |
c0a193ea SM |
1256 | |
1257 | ||
1258 | (defun shell-c-a-p-replace-by-expanded-directory () | |
1259 | "Expand directory stack reference before point. | |
1260 | For use on `completion-at-point-functions'." | |
1261 | (when (comint-match-partial-filename) | |
1262 | (save-excursion | |
1263 | (goto-char (match-beginning 0)) | |
1264 | (let ((stack (cons default-directory shell-dirstack)) | |
1265 | (index (cond ((looking-at "=-/?") | |
1266 | (length shell-dirstack)) | |
1267 | ((looking-at "=\\([0-9]+\\)/?") | |
1268 | (string-to-number | |
1269 | (buffer-substring | |
1270 | (match-beginning 1) (match-end 1))))))) | |
1271 | (when index | |
1272 | (let ((start (match-beginning 0)) | |
1273 | (end (match-end 0)) | |
1274 | (replacement (file-name-as-directory (nth index stack)))) | |
1275 | (lambda () | |
1276 | (cond | |
1277 | ((>= index (length stack)) | |
1278 | (error "Directory stack not that deep")) | |
1279 | (t | |
1280 | (save-excursion | |
1281 | (goto-char start) | |
1282 | (insert replacement) | |
1283 | (delete-char (- end start))) | |
1284 | (message "Directory item: %d" index) | |
1285 | t))))))))) | |
988a4d60 RS |
1286 | |
1287 | (defun shell-replace-by-expanded-directory () | |
1288 | "Expand directory stack reference before point. | |
1289 | Directory stack references are of the form \"=digit\" or \"=-\". | |
1290 | See `default-directory' and `shell-dirstack'. | |
1291 | ||
1292 | Returns t if successful." | |
1293 | (interactive) | |
c0a193ea SM |
1294 | (let ((f (shell-c-a-p-replace-by-expanded-directory))) |
1295 | (if f (funcall f)))) | |
84728570 | 1296 | |
c88ab9ce ER |
1297 | (provide 'shell) |
1298 | ||
1299 | ;;; shell.el ends here |