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