Mercurial > emacs
annotate lisp/eshell/esh-ext.el @ 47149:facc9fbc1777
(struct w32_display_info) <x_highlight_frame>: Renamed from
w32_highlight_frame.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Fri, 30 Aug 2002 13:20:36 +0000 |
parents | acd6b7b35363 |
children | 10b09165e86f |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
37818
diff
changeset
|
1 ;;; esh-ext.el --- commands external to Eshell |
29876 | 2 |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29876
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29876 | 4 |
32526 | 5 ;; Author: John Wiegley <johnw@gnu.org> |
6 | |
29876 | 7 ;; This file is part of GNU Emacs. |
8 | |
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 2, or (at your option) | |
12 ;; any later version. | |
13 | |
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. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 (provide 'esh-ext) | |
25 | |
26 (eval-when-compile (require 'esh-maint)) | |
27 | |
28 (defgroup eshell-ext nil | |
29 "External commands are invoked when operating system executables are | |
30 loaded into memory, thus beginning a new process." | |
31 :tag "External commands" | |
32 :group 'eshell) | |
33 | |
34 ;;; Commentary: | |
35 | |
36 ;; To force a command to invoked external, either provide an explicit | |
37 ;; pathname for the command argument, or prefix the command name with | |
38 ;; an asterix character. Example: | |
39 ;; | |
40 ;; grep ; make invoke `grep' Lisp function, or `eshell/grep' | |
41 ;; /bin/grep ; will definitely invoke /bin/grep | |
42 ;; *grep ; will also invoke /bin/grep | |
43 | |
44 ;;; User Variables: | |
45 | |
46 (defcustom eshell-ext-load-hook '(eshell-ext-initialize) | |
47 "*A hook that gets run when `eshell-ext' is loaded." | |
48 :type 'hook | |
49 :group 'eshell-ext) | |
50 | |
44002
a27b6cb6448d
(eshell-binary-suffixes): Use exec-suffixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43336
diff
changeset
|
51 (defcustom eshell-binary-suffixes exec-suffixes |
29876 | 52 "*A list of suffixes used when searching for executable files." |
53 :type '(repeat string) | |
54 :group 'eshell-ext) | |
55 | |
56 (defcustom eshell-force-execution nil | |
57 "*If non-nil, try to execute binary files regardless of permissions. | |
58 This can be useful on systems like Windows, where the operating system | |
59 doesn't happen to honor the permission bits in certain cases; or in | |
60 cases where you want to associate an interpreter with a particular | |
61 kind of script file, but the language won't let you but a '#!' | |
62 interpreter line in the file, and you don't want to make it executable | |
63 since nothing else but Eshell will be able to understand | |
64 `eshell-interpreter-alist'." | |
65 :type 'boolean | |
66 :group 'eshell-ext) | |
67 | |
68 (defun eshell-search-path (name) | |
69 "Search the environment path for NAME." | |
70 (if (file-name-absolute-p name) | |
71 name | |
72 (let ((list (parse-colon-path (getenv "PATH"))) | |
73 suffixes n1 n2 file) | |
74 (while list | |
75 (setq n1 (concat (car list) name)) | |
76 (setq suffixes eshell-binary-suffixes) | |
77 (while suffixes | |
78 (setq n2 (concat n1 (car suffixes))) | |
79 (if (and (or (file-executable-p n2) | |
80 (and eshell-force-execution | |
81 (file-readable-p n2))) | |
82 (not (file-directory-p n2))) | |
83 (setq file n2 suffixes nil list nil)) | |
84 (setq suffixes (cdr suffixes))) | |
85 (setq list (cdr list))) | |
86 file))) | |
87 | |
88 (defcustom eshell-windows-shell-file | |
89 (if (eshell-under-windows-p) | |
90 (if (string-match "\\(\\`cmdproxy\\|sh\\)\\.\\(com\\|exe\\)" | |
91 shell-file-name) | |
92 (or (eshell-search-path "cmd.exe") | |
93 (eshell-search-path "command.exe")) | |
94 shell-file-name)) | |
95 "*The name of the shell command to use for DOS/Windows batch files. | |
96 This defaults to nil on non-Windows systems, where this variable is | |
97 wholly ignored." | |
35717
a16b7aced16a
(eshell-windows-shell-file): Fix :type.
Dave Love <fx@gnu.org>
parents:
32526
diff
changeset
|
98 :type '(choice file (const nil)) |
29876 | 99 :group 'eshell-ext) |
100 | |
101 (defsubst eshell-invoke-batch-file (&rest args) | |
102 "Invoke a .BAT or .CMD file on DOS/Windows systems." | |
103 ;; since CMD.EXE can't handle forward slashes in the initial | |
104 ;; argument... | |
31241 | 105 (setcar args (subst-char-in-string directory-sep-char ?\\ (car args))) |
29876 | 106 (throw 'eshell-replace-command |
31241 | 107 (eshell-parse-command eshell-windows-shell-file (cons "/c" args)))) |
29876 | 108 |
109 (defcustom eshell-interpreter-alist | |
110 (if (eshell-under-windows-p) | |
111 '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file))) | |
112 "*An alist defining interpreter substitutions. | |
113 Each member is a cons cell of the form: | |
114 | |
115 (MATCH . INTERPRETER) | |
116 | |
117 MATCH should be a regexp, which is matched against the command name, | |
118 or a function. If either returns a non-nil value, then INTERPRETER | |
119 will be used for that command. | |
120 | |
121 If INTERPRETER is a string, it will be called as the command name, | |
122 with the original command name passed as the first argument, with all | |
123 subsequent arguments following. If INTERPRETER is a function, it will | |
124 be called with all of those arguments. Note that interpreter | |
125 functions should throw `eshell-replace-command' with the alternate | |
126 command form, or they should return a value compatible with the | |
127 possible return values of `eshell-external-command', which see." | |
128 :type '(repeat (cons (choice regexp (function :tag "Predicate")) | |
129 (choice string (function :tag "Interpreter")))) | |
130 :group 'eshell-ext) | |
131 | |
132 (defcustom eshell-alternate-command-hook nil | |
133 "*A hook run whenever external command lookup fails. | |
134 If a functions wishes to provide an alternate command, they must throw | |
135 it using the tag `eshell-replace-command'. This is done because the | |
136 substituted command need not be external at all, and therefore must be | |
137 passed up to a higher level for re-evaluation. | |
138 | |
139 Or, if the function returns a filename, that filename will be invoked | |
140 with the current command arguments rather than the command specified | |
141 by the user on the command line." | |
142 :type 'hook | |
143 :group 'eshell-ext) | |
144 | |
145 (defcustom eshell-command-interpreter-max-length 256 | |
146 "*The maximum length of any command interpreter string, plus args." | |
147 :type 'integer | |
148 :group 'eshell-ext) | |
149 | |
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
150 (defcustom eshell-explicit-command-char ?* |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
151 "*If this char occurs before a command name, call it externally. |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
152 That is, although vi may be an alias, *vi will always call the |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
153 external version. UNIX users may prefer this variable to be \." |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
154 :type 'character |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
155 :group 'eshell-ext) |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
156 |
29876 | 157 ;;; Functions: |
158 | |
159 (defun eshell-ext-initialize () | |
160 "Initialize the external command handling code." | |
161 (add-hook 'eshell-named-command-hook 'eshell-explicit-command nil t)) | |
162 | |
163 (defun eshell-explicit-command (command args) | |
164 "If a command name begins with `*', call it externally always. | |
165 This bypasses all Lisp functions and aliases." | |
166 (when (and (> (length command) 1) | |
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
167 (eq (aref command 0) eshell-explicit-command-char)) |
29876 | 168 (let ((cmd (eshell-search-path (substring command 1)))) |
169 (if cmd | |
170 (or (eshell-external-command cmd args) | |
171 (error "%s: external command failed" cmd)) | |
172 (error "%s: external command not found" | |
173 (substring command 1)))))) | |
174 | |
175 (defun eshell-remote-command (handler command args) | |
176 "Insert output from a remote COMMAND, using ARGS. | |
177 A remote command is something that executes on a different machine. | |
178 An external command simply means external to Emacs. | |
179 | |
180 Note that this function is very crude at the moment. It gathers up | |
181 all the output from the remote command, and sends it all at once, | |
182 causing the user to wonder if anything's really going on..." | |
183 (let ((outbuf (generate-new-buffer " *eshell remote output*")) | |
184 (errbuf (generate-new-buffer " *eshell remote error*")) | |
185 (exitcode 1)) | |
186 (unwind-protect | |
187 (progn | |
188 (setq exitcode | |
189 (funcall handler 'shell-command | |
190 (mapconcat 'shell-quote-argument | |
191 (append (list command) args) " ") | |
192 outbuf errbuf)) | |
193 (eshell-print (save-excursion (set-buffer outbuf) | |
194 (buffer-string))) | |
195 (eshell-error (save-excursion (set-buffer errbuf) | |
196 (buffer-string)))) | |
197 (eshell-close-handles exitcode 'nil) | |
198 (kill-buffer outbuf) | |
199 (kill-buffer errbuf)))) | |
200 | |
201 (defun eshell-external-command (command args) | |
202 "Insert output from an external COMMAND, using ARGS." | |
203 (setq args (eshell-stringify-list (eshell-flatten-list args))) | |
204 (let ((handler | |
205 (unless (or (equal default-directory "/") | |
206 (and (eshell-under-windows-p) | |
207 (string-match "\\`[A-Za-z]:[/\\\\]\\'" | |
208 default-directory))) | |
209 (find-file-name-handler default-directory | |
210 'shell-command)))) | |
43319
539d3657c52c
(eshell-external-command): Added a fix for XEmacs' new dired.el, which
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
211 (if (and handler |
539d3657c52c
(eshell-external-command): Added a fix for XEmacs' new dired.el, which
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
212 (not (and (eshell-under-xemacs-p) |
539d3657c52c
(eshell-external-command): Added a fix for XEmacs' new dired.el, which
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
213 (eq handler 'dired-handler-fn)))) |
29876 | 214 (eshell-remote-command handler command args)) |
215 (let ((interp (eshell-find-interpreter command))) | |
216 (assert interp) | |
217 (if (functionp (car interp)) | |
218 (apply (car interp) (append (cdr interp) args)) | |
219 (eshell-gather-process-output | |
220 (car interp) (append (cdr interp) args)))))) | |
221 | |
222 (defun eshell/addpath (&rest args) | |
223 "Add a set of paths to PATH." | |
224 (eshell-eval-using-options | |
225 "addpath" args | |
226 '((?b "begin" nil prepend "add path element at beginning") | |
227 (?h "help" nil nil "display this usage message") | |
228 :usage "[-b] PATH | |
229 Adds the given PATH to $PATH.") | |
230 (if args | |
231 (progn | |
232 (if prepend | |
233 (setq args (nreverse args))) | |
234 (while args | |
235 (setenv "PATH" | |
236 (if prepend | |
237 (concat (car args) path-separator | |
238 (getenv "PATH")) | |
239 (concat (getenv "PATH") path-separator | |
240 (car args)))) | |
241 (setq args (cdr args)))) | |
242 (let ((paths (parse-colon-path (getenv "PATH")))) | |
243 (while paths | |
244 (eshell-printn (car paths)) | |
245 (setq paths (cdr paths))))))) | |
246 | |
37661
6d7c89c79996
Set the property `eshell-no-numeric-conversions' on the following
John Wiegley <johnw@newartisans.com>
parents:
35717
diff
changeset
|
247 (put 'eshell/addpath 'eshell-no-numeric-conversions t) |
6d7c89c79996
Set the property `eshell-no-numeric-conversions' on the following
John Wiegley <johnw@newartisans.com>
parents:
35717
diff
changeset
|
248 |
29876 | 249 (defun eshell-script-interpreter (file) |
250 "Extract the script to run from FILE, if it has #!<interp> in it. | |
251 Return nil, or a list of the form: | |
252 | |
253 (INTERPRETER [ARGS] FILE)" | |
254 (let ((maxlen eshell-command-interpreter-max-length)) | |
255 (if (and (file-readable-p file) | |
256 (file-regular-p file)) | |
257 (with-temp-buffer | |
258 (insert-file-contents-literally file nil 0 maxlen) | |
44553
536e5590a586
(eshell-script-interpreter): Fix for CRLF operating systems to the
John Wiegley <johnw@newartisans.com>
parents:
44002
diff
changeset
|
259 (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?") |
29876 | 260 (if (match-string 3) |
261 (list (match-string 1) | |
262 (match-string 3) | |
263 file) | |
264 (list (match-string 1) | |
265 file))))))) | |
266 | |
267 (defun eshell-find-interpreter (file &optional no-examine-p) | |
268 "Find the command interpreter with which to execute FILE. | |
269 If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script | |
270 line of the form #!<interp>." | |
271 (let ((finterp | |
272 (catch 'found | |
273 (ignore | |
274 (eshell-for possible eshell-interpreter-alist | |
275 (cond | |
276 ((functionp (car possible)) | |
277 (and (funcall (car possible) file) | |
278 (throw 'found (cdr possible)))) | |
279 ((stringp (car possible)) | |
280 (and (string-match (car possible) file) | |
281 (throw 'found (cdr possible)))) | |
282 (t | |
283 (error "Invalid interpreter-alist test")))))))) | |
284 (if finterp ; first check | |
285 (list finterp file) | |
286 (let ((fullname (if (file-name-directory file) file | |
287 (eshell-search-path file))) | |
288 (suffixes eshell-binary-suffixes)) | |
289 (if (and fullname (not (or eshell-force-execution | |
290 (file-executable-p fullname)))) | |
291 (while suffixes | |
292 (let ((try (concat fullname (car suffixes)))) | |
293 (if (or (file-executable-p try) | |
294 (and eshell-force-execution | |
295 (file-readable-p try))) | |
296 (setq fullname try suffixes nil) | |
297 (setq suffixes (cdr suffixes)))))) | |
298 (cond ((not (and fullname (file-exists-p fullname))) | |
299 (let ((name (or fullname file))) | |
300 (unless (setq fullname | |
301 (run-hook-with-args-until-success | |
302 'eshell-alternate-command-hook file)) | |
303 (error "%s: command not found" name)))) | |
304 ((not (or eshell-force-execution | |
305 (file-executable-p fullname))) | |
306 (error "%s: Permission denied" fullname))) | |
307 (let (interp) | |
308 (unless no-examine-p | |
309 (setq interp (eshell-script-interpreter fullname)) | |
310 (if interp | |
311 (setq interp | |
312 (cons (car (eshell-find-interpreter (car interp) t)) | |
313 (cdr interp))))) | |
314 (or interp (list fullname))))))) | |
315 | |
316 ;;; Code: | |
317 | |
318 ;;; esh-ext.el ends here |