Improve responsiveness while in 'replace-buffer-contents'
[emacs.git] / lisp / eshell / esh-ext.el
blob1bfab23c2209d0ef783dbfcb8d647ce5416e4083
1 ;;; esh-ext.el --- commands external to Eshell -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2018 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; To force a command to invoked external, either provide an explicit
25 ;; pathname for the command argument, or prefix the command name with
26 ;; an asterix character. Example:
28 ;; grep ; make invoke `grep' Lisp function, or `eshell/grep'
29 ;; /bin/grep ; will definitely invoke /bin/grep
30 ;; *grep ; will also invoke /bin/grep
32 ;;; Code:
34 (provide 'esh-ext)
36 (require 'esh-util)
38 (eval-when-compile
39 (require 'cl-lib)
40 (require 'esh-io)
41 (require 'esh-cmd))
42 (require 'esh-arg)
43 (require 'esh-opt)
44 (require 'esh-proc)
46 (defgroup eshell-ext nil
47 "External commands are invoked when operating system executables are
48 loaded into memory, thus beginning a new process."
49 :tag "External commands"
50 :group 'eshell)
52 ;;; User Variables:
54 (defcustom eshell-ext-load-hook nil
55 "A hook that gets run when `eshell-ext' is loaded."
56 :version "24.1" ; removed eshell-ext-initialize
57 :type 'hook
58 :group 'eshell-ext)
60 (defcustom eshell-binary-suffixes exec-suffixes
61 "A list of suffixes used when searching for executable files."
62 :type '(repeat string)
63 :group 'eshell-ext)
65 (defcustom eshell-force-execution
66 (not (null (memq system-type '(windows-nt ms-dos))))
67 "If non-nil, try to execute files regardless of execute permissions.
68 This can be useful on systems like Windows, where the operating system
69 doesn't support the execution bit for shell scripts; or in cases where
70 you want to associate an interpreter with a particular kind of script
71 file, but the language won't let you but a `#!' interpreter line in
72 the file, and you don't want to make it executable since nothing else
73 but Eshell will be able to understand
74 `eshell-interpreter-alist'."
75 :type 'boolean
76 :group 'eshell-ext)
78 (defun eshell-search-path (name)
79 "Search the environment path for NAME."
80 (if (file-name-absolute-p name)
81 name
82 (let ((list (eshell-parse-colon-path eshell-path-env))
83 suffixes n1 n2 file)
84 (if (eshell-under-windows-p)
85 (push "." list))
86 (while list
87 (setq n1 (concat (car list) name))
88 (setq suffixes eshell-binary-suffixes)
89 (while suffixes
90 (setq n2 (concat n1 (car suffixes)))
91 (if (and (or (file-executable-p n2)
92 (and eshell-force-execution
93 (file-readable-p n2)))
94 (not (file-directory-p n2)))
95 (setq file n2 suffixes nil list nil))
96 (setq suffixes (cdr suffixes)))
97 (setq list (cdr list)))
98 file)))
100 ;; This file provides itself then eval-when-compile loads files that require it.
101 ;; This causes spurious "might not be defined at runtime" warnings.
102 (declare-function eshell-search-path "esh-ext" (name))
104 (defcustom eshell-windows-shell-file
105 (if (eshell-under-windows-p)
106 (if (string-match "\\(cmdproxy\\|sh\\)\\.\\(com\\|exe\\)"
107 shell-file-name)
108 (or (eshell-search-path "cmd.exe")
109 (eshell-search-path "command.com"))
110 shell-file-name))
111 "The name of the shell command to use for DOS/Windows batch files.
112 This defaults to nil on non-Windows systems, where this variable is
113 wholly ignored."
114 :type '(choice file (const nil))
115 :group 'eshell-ext)
117 (autoload 'eshell-parse-command "esh-cmd")
119 (defsubst eshell-invoke-batch-file (&rest args)
120 "Invoke a .BAT or .CMD file on DOS/Windows systems."
121 ;; since CMD.EXE can't handle forward slashes in the initial
122 ;; argument...
123 (setcar args (subst-char-in-string ?/ ?\\ (car args)))
124 (throw 'eshell-replace-command
125 (eshell-parse-command
126 (eshell-quote-argument eshell-windows-shell-file)
127 (cons "/c" args))))
129 (defcustom eshell-interpreter-alist
130 (if (eshell-under-windows-p)
131 '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file)))
132 "An alist defining interpreter substitutions.
133 Each member is a cons cell of the form:
135 (MATCH . INTERPRETER)
137 MATCH should be a regexp, which is matched against the command
138 name, or a function of arity 2 receiving the COMMAND and its
139 ARGS (a list). If either returns a non-nil value, then
140 INTERPRETER will be used for that command.
142 If INTERPRETER is a string, it will be called as the command name,
143 with the original command name passed as the first argument, with all
144 subsequent arguments following. If INTERPRETER is a function, it will
145 be called with all of those arguments. Note that interpreter
146 functions should throw `eshell-replace-command' with the alternate
147 command form, or they should return a value compatible with the
148 possible return values of `eshell-external-command', which see."
149 :type '(repeat (cons (choice regexp (function :tag "Predicate"))
150 (choice string (function :tag "Interpreter"))))
151 :group 'eshell-ext)
153 (defcustom eshell-alternate-command-hook nil
154 "A hook run whenever external command lookup fails.
155 If a functions wishes to provide an alternate command, they must throw
156 it using the tag `eshell-replace-command'. This is done because the
157 substituted command need not be external at all, and therefore must be
158 passed up to a higher level for re-evaluation.
160 Or, if the function returns a filename, that filename will be invoked
161 with the current command arguments rather than the command specified
162 by the user on the command line."
163 :type 'hook
164 :group 'eshell-ext)
166 (defcustom eshell-command-interpreter-max-length 256
167 "The maximum length of any command interpreter string, plus args."
168 :type 'integer
169 :group 'eshell-ext)
171 (defcustom eshell-explicit-command-char ?*
172 "If this char occurs before a command name, call it externally.
173 That is, although `vi' may be an alias, `\vi' will always call the
174 external version."
175 :type 'character
176 :group 'eshell-ext)
178 ;;; Functions:
180 (defun eshell-ext-initialize ()
181 "Initialize the external command handling code."
182 (add-hook 'eshell-named-command-hook 'eshell-explicit-command nil t))
184 (defun eshell-explicit-command (command args)
185 "If a command name begins with `*', call it externally always.
186 This bypasses all Lisp functions and aliases."
187 (when (and (> (length command) 1)
188 (eq (aref command 0) eshell-explicit-command-char))
189 (let ((cmd (eshell-search-path (substring command 1))))
190 (if cmd
191 (or (eshell-external-command cmd args)
192 (error "%s: external command failed" cmd))
193 (error "%s: external command not found"
194 (substring command 1))))))
196 (autoload 'eshell-close-handles "esh-io")
198 (defun eshell-remote-command (command args)
199 "Insert output from a remote COMMAND, using ARGS.
200 A remote command is something that executes on a different machine.
201 An external command simply means external to Emacs.
203 Note that this function is very crude at the moment. It gathers up
204 all the output from the remote command, and sends it all at once,
205 causing the user to wonder if anything's really going on..."
206 (let ((outbuf (generate-new-buffer " *eshell remote output*"))
207 (errbuf (generate-new-buffer " *eshell remote error*"))
208 (command (file-local-name command))
209 (exitcode 1))
210 (unwind-protect
211 (progn
212 (setq exitcode
213 (shell-command
214 (mapconcat 'shell-quote-argument
215 (append (list command) args) " ")
216 outbuf errbuf))
217 (eshell-print (with-current-buffer outbuf (buffer-string)))
218 (eshell-error (with-current-buffer errbuf (buffer-string))))
219 (eshell-close-handles exitcode 'nil)
220 (kill-buffer outbuf)
221 (kill-buffer errbuf))))
223 (defun eshell-external-command (command args)
224 "Insert output from an external COMMAND, using ARGS."
225 (setq args (eshell-stringify-list (eshell-flatten-list args)))
226 (let ((interp (eshell-find-interpreter
227 command
228 args
229 ;; `eshell-find-interpreter' does not work correctly
230 ;; for Tramp file name syntax. But we don't need to
231 ;; know the interpreter in that case, therefore the
232 ;; check is suppressed.
233 (or (and (stringp command) (file-remote-p command))
234 (file-remote-p default-directory)))))
235 (cl-assert interp)
236 (if (functionp (car interp))
237 (apply (car interp) (append (cdr interp) args))
238 (eshell-gather-process-output
239 (car interp) (append (cdr interp) args)))))
241 (defun eshell/addpath (&rest args)
242 "Add a set of paths to PATH."
243 (eshell-eval-using-options
244 "addpath" args
245 '((?b "begin" nil prepend "add path element at beginning")
246 (?h "help" nil nil "display this usage message")
247 :usage "[-b] PATH
248 Adds the given PATH to $PATH.")
249 (if args
250 (progn
251 (setq eshell-path-env (getenv "PATH")
252 args (mapconcat 'identity args path-separator)
253 eshell-path-env
254 (if prepend
255 (concat args path-separator eshell-path-env)
256 (concat eshell-path-env path-separator args)))
257 (setenv "PATH" eshell-path-env))
258 (dolist (dir (parse-colon-path (getenv "PATH")))
259 (eshell-printn dir)))))
261 (put 'eshell/addpath 'eshell-no-numeric-conversions t)
263 (defun eshell-script-interpreter (file)
264 "Extract the script to run from FILE, if it has #!<interp> in it.
265 Return nil, or a list of the form:
267 (INTERPRETER [ARGS] FILE)"
268 (let ((maxlen eshell-command-interpreter-max-length))
269 (if (and (file-readable-p file)
270 (file-regular-p file))
271 (with-temp-buffer
272 (insert-file-contents-literally file nil 0 maxlen)
273 (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?")
274 (if (match-string 3)
275 (list (match-string 1)
276 (match-string 3)
277 file)
278 (list (match-string 1)
279 file)))))))
281 (defun eshell-find-interpreter (file args &optional no-examine-p)
282 "Find the command interpreter with which to execute FILE.
283 If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script
284 line of the form #!<interp>."
285 (let ((finterp
286 (catch 'found
287 (ignore
288 (dolist (possible eshell-interpreter-alist)
289 (cond
290 ((functionp (car possible))
291 (let ((fn (car possible)))
292 (and (funcall fn file args)
293 (throw 'found (cdr possible)))))
294 ((stringp (car possible))
295 (and (string-match (car possible) file)
296 (throw 'found (cdr possible))))
298 (error "Invalid interpreter-alist test"))))))))
299 (if finterp ; first check
300 (list finterp file)
301 (let ((fullname (if (file-name-directory file) file
302 (eshell-search-path file)))
303 (suffixes eshell-binary-suffixes))
304 (when (and fullname
305 (not (file-remote-p fullname))
306 (file-remote-p default-directory))
307 (setq fullname
308 (if (file-name-absolute-p fullname)
309 (concat (file-remote-p default-directory) fullname)
310 (expand-file-name fullname default-directory))))
311 (if (and fullname (not (or eshell-force-execution
312 (file-executable-p fullname))))
313 (while suffixes
314 (let ((try (concat fullname (car suffixes))))
315 (if (or (file-executable-p try)
316 (and eshell-force-execution
317 (file-readable-p try)))
318 (setq fullname try suffixes nil)
319 (setq suffixes (cdr suffixes))))))
320 (cond ((not (and fullname (file-exists-p fullname)))
321 (let ((name (or fullname file)))
322 (unless (setq fullname
323 (run-hook-with-args-until-success
324 'eshell-alternate-command-hook file))
325 (error "%s: command not found" name))))
326 ((not (or eshell-force-execution
327 (file-executable-p fullname)))
328 (error "%s: Permission denied" fullname)))
329 (let (interp)
330 (unless no-examine-p
331 (setq interp (eshell-script-interpreter fullname))
332 (if interp
333 (setq interp
334 (cons (car (eshell-find-interpreter (car interp) args t))
335 (cdr interp)))))
336 (or interp (list fullname)))))))
338 ;;; esh-ext.el ends here