Mercurial > emacs
annotate lisp/eshell/esh-var.el @ 30025:c5eb8840659f
(make_composition_value_copy): Extern it.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Tue, 04 Jul 2000 07:39:18 +0000 |
parents | 34b1ab9d583d |
children | 3099993cba0f |
rev | line source |
---|---|
29876 | 1 ;;; esh-var --- handling of variables |
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 |
5 ;; This file is part of GNU Emacs. | |
6 | |
7 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 ;; it under the terms of the GNU General Public License as published by | |
9 ;; the Free Software Foundation; either version 2, or (at your option) | |
10 ;; any later version. | |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 ;; GNU General Public License for more details. | |
16 | |
17 ;; You should have received a copy of the GNU General Public License | |
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 ;; Boston, MA 02111-1307, USA. | |
21 | |
22 (provide 'esh-var) | |
23 | |
24 (eval-when-compile (require 'esh-maint)) | |
25 | |
26 (defgroup eshell-var nil | |
27 "Variable interpolation is introduced whenever the '$' character | |
28 appears unquoted in any argument (except when that argument is | |
29 surrounded by single quotes) . It may be used to interpolate a | |
30 variable value, a subcommand, or even the result of a Lisp form." | |
31 :tag "Variable handling" | |
32 :group 'eshell) | |
33 | |
34 ;;; Commentary: | |
35 | |
36 ;; These are the possible variable interpolation syntaxes. Also keep | |
37 ;; in mind that if an argument looks like a number, it will be | |
38 ;; converted to a number. This is not significant when invoking | |
39 ;; external commands, but it's important when calling Lisp functions. | |
40 ;; | |
41 ;; $VARIABLE | |
42 ;; | |
43 ;; Interval the value of an environment variable, or a Lisp variable | |
44 ;; | |
45 ;; $ALSO-VAR | |
46 ;; | |
47 ;; "-" is a legal part of a variable name. | |
48 ;; | |
49 ;; $<MYVAR>-TOO | |
50 ;; | |
51 ;; Only "MYVAR" is part of the variable name in this case. | |
52 ;; | |
53 ;; $#VARIABLE | |
54 ;; | |
55 ;; Returns the length of the value of VARIABLE. This could also be | |
56 ;; done using the `length' Lisp function. | |
57 ;; | |
58 ;; $(lisp) | |
59 ;; | |
60 ;; Returns result of lisp evaluation. Note: Used alone like this, it | |
61 ;; is identical to just saying (lisp); but with the variable expansion | |
62 ;; form, the result may be interpolated a larger string, such as | |
63 ;; '$(lisp)/other'. | |
64 ;; | |
65 ;; ${command} | |
66 ;; | |
67 ;; Returns the value of an eshell subcommand. See the note above | |
68 ;; regarding Lisp evaluations. | |
69 ;; | |
70 ;; $ANYVAR[10] | |
71 ;; | |
72 ;; Return the 10th element of ANYVAR. If ANYVAR's value is a string, | |
73 ;; it will be split in order to make it a list. The splitting will | |
74 ;; occur at whitespace. | |
75 ;; | |
76 ;; $ANYVAR[: 10] | |
77 ;; | |
78 ;; As above, except that splitting occurs at the colon now. | |
79 ;; | |
80 ;; $ANYVAR[: 10 20] | |
81 ;; | |
82 ;; As above, but instead of returning just a string, it now returns a | |
83 ;; list of two strings. If the result is being interpolated into a | |
84 ;; larger string, this list will be flattened into one big string, | |
85 ;; with each element separated by a space. | |
86 ;; | |
87 ;; $ANYVAR["\\\\" 10] | |
88 ;; | |
89 ;; Separate on backslash characters. Actually, the first argument -- | |
90 ;; if it doesn't have the form of a number, or a plain variable name | |
91 ;; -- can be any regular expression. So to split on numbers, use | |
92 ;; '$ANYVAR["[0-9]+" 10 20]'. | |
93 ;; | |
94 ;; $ANYVAR[hello] | |
95 ;; | |
96 ;; Calls `assoc' on ANYVAR with 'hello', expecting it to be an alist. | |
97 ;; | |
98 ;; $#ANYVAR[hello] | |
99 ;; | |
100 ;; Returns the length of the cdr of the element of ANYVAR who car is | |
101 ;; equal to "hello". | |
102 ;; | |
103 ;; There are also a few special variables defined by Eshell. '$$' is | |
104 ;; the value of the last command (t or nil, in the case of an external | |
105 ;; command). This makes it possible to chain results: | |
106 ;; | |
107 ;; /tmp $ echo /var/spool/mail/johnw | |
108 ;; /var/spool/mail/johnw | |
109 ;; /tmp $ dirname $$ | |
110 ;; /var/spool/mail/ | |
111 ;; /tmp $ cd $$ | |
112 ;; /var/spool/mail $ | |
113 ;; | |
114 ;; '$_' refers to the last argument of the last command. And $? | |
115 ;; contains the exit code of the last command (0 or 1 for Lisp | |
116 ;; functions, based on successful completion). | |
117 | |
118 (require 'env) | |
119 (require 'ring) | |
120 | |
121 ;;; User Variables: | |
122 | |
123 (defcustom eshell-var-load-hook '(eshell-var-initialize) | |
124 "*A list of functions to call when loading `eshell-var'." | |
125 :type 'hook | |
126 :group 'eshell-var) | |
127 | |
128 (defcustom eshell-prefer-lisp-variables nil | |
129 "*If non-nil, prefer Lisp variables to environment variables." | |
130 :type 'boolean | |
131 :group 'eshell-var) | |
132 | |
133 (defcustom eshell-complete-export-definition t | |
134 "*If non-nil, completing names for `export' shows current definition." | |
135 :type 'boolean | |
136 :group 'eshell-var) | |
137 | |
138 (defcustom eshell-variable-name-regexp "[A-Za-z0-9_-]+" | |
139 "*A regexp identifying what constitutes a variable name reference. | |
140 Note that this only applies for '$NAME'. If the syntax '$<NAME>' is | |
141 used, then NAME can contain any character, including angle brackets, | |
142 if they are quoted with a backslash." | |
143 :type 'regexp | |
144 :group 'eshell-var) | |
145 | |
146 (defcustom eshell-variable-aliases-list | |
147 '(;; for eshell.el | |
148 ("COLUMNS" (lambda (indices) (window-width)) t) | |
149 ("LINES" (lambda (indices) (window-height)) t) | |
150 | |
151 ;; for eshell-cmd.el | |
152 ("_" (lambda (indices) | |
153 (if (not indices) | |
154 (car (last eshell-last-arguments)) | |
155 (eshell-apply-indices eshell-last-arguments | |
156 indices)))) | |
157 ("?" eshell-last-command-status) | |
158 ("$" eshell-last-command-result) | |
159 ("0" eshell-command-name) | |
160 ("1" (lambda (indices) (nth 0 eshell-command-arguments))) | |
161 ("2" (lambda (indices) (nth 1 eshell-command-arguments))) | |
162 ("3" (lambda (indices) (nth 2 eshell-command-arguments))) | |
163 ("4" (lambda (indices) (nth 3 eshell-command-arguments))) | |
164 ("5" (lambda (indices) (nth 4 eshell-command-arguments))) | |
165 ("6" (lambda (indices) (nth 5 eshell-command-arguments))) | |
166 ("7" (lambda (indices) (nth 6 eshell-command-arguments))) | |
167 ("8" (lambda (indices) (nth 7 eshell-command-arguments))) | |
168 ("9" (lambda (indices) (nth 8 eshell-command-arguments))) | |
169 ("*" (lambda (indices) | |
170 (if (not indices) | |
171 eshell-command-arguments | |
172 (eshell-apply-indices eshell-command-arguments | |
173 indices))))) | |
174 "*This list provides aliasing for variable references. | |
175 It is very similar in concept to what `eshell-user-aliases-list' does | |
176 for commands. Each member of this defines defines the name of a | |
177 command, and the Lisp value to return for that variable if it is | |
178 accessed via the syntax '$NAME'. | |
179 | |
180 If the value is a function, that function will be called with two | |
181 arguments: the list of the indices that was used in the reference, and | |
182 whether the user is requesting the length of the ultimate element. | |
183 For example, a reference of '$NAME[10][20]' would result in the | |
184 function for alias `NAME' being called (assuming it were aliased to a | |
185 function), and the arguments passed to this function would be the list | |
186 '(10 20)', and nil." | |
187 :type '(repeat (list string sexp | |
188 (choice (const :tag "Copy to environment" t) | |
189 (const :tag "Use only in Eshell" nil)))) | |
190 :group 'eshell-var) | |
191 | |
192 (put 'eshell-variable-aliases-list 'risky-local-variable t) | |
193 | |
194 ;;; Functions: | |
195 | |
196 (defun eshell-var-initialize () | |
197 "Initialize the variable handle code." | |
198 ;; Break the association with our parent's environment. Otherwise, | |
199 ;; changing a variable will affect all of Emacs. | |
200 (set (make-local-variable 'process-environment) (eshell-copy-environment)) | |
201 | |
202 (define-key eshell-command-map [(meta ?v)] 'eshell-insert-envvar) | |
203 | |
204 (set (make-local-variable 'eshell-special-chars-inside-quoting) | |
205 (append eshell-special-chars-inside-quoting '(?$))) | |
206 (set (make-local-variable 'eshell-special-chars-outside-quoting) | |
207 (append eshell-special-chars-outside-quoting '(?$))) | |
208 | |
209 (make-local-hook 'eshell-parse-argument-hook) | |
210 (add-hook 'eshell-parse-argument-hook 'eshell-interpolate-variable t t) | |
211 | |
212 (make-local-hook 'eshell-prepare-command-hook) | |
213 (add-hook 'eshell-prepare-command-hook | |
214 'eshell-handle-local-variables nil t) | |
215 | |
216 (when (eshell-using-module 'eshell-cmpl) | |
217 (make-local-hook 'pcomplete-try-first-hook) | |
218 (add-hook 'pcomplete-try-first-hook | |
219 'eshell-complete-variable-reference nil t) | |
220 (add-hook 'pcomplete-try-first-hook | |
221 'eshell-complete-variable-assignment nil t))) | |
222 | |
223 (defun eshell-handle-local-variables () | |
224 "Allow for the syntax 'VAR=val <command> <args>'." | |
225 ;; strip off any null commands, which can only happen if a variable | |
226 ;; evaluates to nil, such as "$var x", where `var' is nil. The | |
227 ;; command name in that case becomes `x', for compatibility with | |
228 ;; most regular shells (the difference is that they do an | |
229 ;; interpolation pass before the argument parsing pass, but Eshell | |
230 ;; does both at the same time). | |
231 (while (and (not eshell-last-command-name) | |
232 eshell-last-arguments) | |
233 (setq eshell-last-command-name (car eshell-last-arguments) | |
234 eshell-last-arguments (cdr eshell-last-arguments))) | |
235 (let ((setvar "\\`\\([A-Za-z_][A-Za-z0-9_]*\\)=\\(.*\\)\\'") | |
236 (command (eshell-stringify eshell-last-command-name)) | |
237 (args eshell-last-arguments)) | |
238 ;; local variable settings (such as 'CFLAGS=-O2 make') are handled | |
239 ;; by making the whole command into a subcommand, and calling | |
240 ;; setenv immediately before the command is invoked. This means | |
241 ;; that 'BLAH=x cd blah' won't work exactly as expected, but that | |
242 ;; is by no means a typical use of local environment variables. | |
243 (if (and command (string-match setvar command)) | |
244 (throw | |
245 'eshell-replace-command | |
246 (list | |
247 'eshell-as-subcommand | |
248 (append | |
249 (list 'progn) | |
250 (let ((l (list t))) | |
251 (while (string-match setvar command) | |
252 (nconc | |
253 l (list | |
254 (list 'setenv (match-string 1 command) | |
255 (match-string 2 command) | |
256 (= (length (match-string 2 command)) 0)))) | |
257 (setq command (eshell-stringify (car args)) | |
258 args (cdr args))) | |
259 (cdr l)) | |
260 (list (list 'eshell-named-command | |
261 command (list 'quote args))))))))) | |
262 | |
263 (defun eshell-interpolate-variable () | |
264 "Parse a variable interpolation. | |
265 This function is explicit for adding to `eshell-parse-argument-hook'." | |
266 (when (and (eq (char-after) ?$) | |
267 (not (= (1+ (point)) (point-max)))) | |
268 (forward-char) | |
269 (list 'eshell-escape-arg | |
270 (eshell-parse-variable)))) | |
271 | |
272 (defun eshell/define (var-alias definition) | |
273 "Define an VAR-ALIAS using DEFINITION." | |
274 (if (not definition) | |
275 (setq eshell-variable-aliases-list | |
276 (delq (assoc var-alias eshell-variable-aliases-list) | |
277 eshell-variable-aliases-list)) | |
278 (let ((def (assoc var-alias eshell-variable-aliases-list)) | |
279 (alias-def | |
280 (list var-alias | |
281 (list 'quote (if (= (length definition) 1) | |
282 (car definition) | |
283 definition))))) | |
284 (if def | |
285 (setq eshell-variable-aliases-list | |
286 (delq (assoc var-alias eshell-variable-aliases-list) | |
287 eshell-variable-aliases-list))) | |
288 (setq eshell-variable-aliases-list | |
289 (cons alias-def | |
290 eshell-variable-aliases-list)))) | |
291 nil) | |
292 | |
293 (defun eshell/export (&rest sets) | |
294 "This alias allows the 'export' command to act as bash users expect." | |
295 (while sets | |
296 (if (string-match "^\\([^=]+\\)=\\(.*\\)" (car sets)) | |
297 (setenv (match-string 1 (car sets)) | |
298 (match-string 2 (car sets)))) | |
299 (setq sets (cdr sets)))) | |
300 | |
301 (defun pcomplete/eshell-mode/export () | |
302 "Completion function for Eshell's `export'." | |
303 (while (pcomplete-here | |
304 (if eshell-complete-export-definition | |
305 process-environment | |
306 (eshell-envvar-names))))) | |
307 | |
308 (defun eshell/setq (&rest args) | |
309 "Allow command-ish use of `setq'." | |
310 (let (last-value) | |
311 (while args | |
312 (let ((sym (intern (car args))) | |
313 (val (cadr args))) | |
314 (setq last-value (set sym val) | |
315 args (cddr args)))) | |
316 last-value)) | |
317 | |
318 (defun pcomplete/eshell-mode/setq () | |
319 "Completion function for Eshell's `setq'." | |
320 (while (and (pcomplete-here (all-completions pcomplete-stub | |
321 obarray 'boundp)) | |
322 (pcomplete-here)))) | |
323 | |
324 (defun eshell/env (&rest args) | |
325 "Implemention of `env' in Lisp." | |
326 (eshell-init-print-buffer) | |
327 (eshell-eval-using-options | |
328 "env" args | |
329 '((?h "help" nil nil "show this usage screen") | |
330 :external "env" | |
331 :usage "<no arguments>") | |
332 (eshell-for setting (sort (eshell-environment-variables) | |
333 'string-lessp) | |
334 (eshell-buffered-print setting "\n")) | |
335 (eshell-flush))) | |
336 | |
337 (defun eshell-insert-envvar (envvar-name) | |
338 "Insert ENVVAR-NAME into the current buffer at point." | |
339 (interactive | |
340 (list (read-envvar-name "Name of environment variable: " t))) | |
341 (insert-and-inherit "$" envvar-name)) | |
342 | |
343 (defun eshell-envvar-names (&optional environment) | |
344 "Return a list of currently visible environment variable names." | |
345 (mapcar (function | |
346 (lambda (x) | |
347 (substring x 0 (string-match "=" x)))) | |
348 (or environment process-environment))) | |
349 | |
350 (defun eshell-environment-variables () | |
351 "Return a `process-environment', fully updated. | |
352 This involves setting any variable aliases which affect the | |
353 environment, as specified in `eshell-variable-aliases-list'." | |
354 (let ((process-environment (eshell-copy-environment))) | |
355 (eshell-for var-alias eshell-variable-aliases-list | |
356 (if (nth 2 var-alias) | |
357 (setenv (car var-alias) | |
358 (eshell-stringify | |
359 (or (eshell-get-variable (car var-alias)) ""))))) | |
360 process-environment)) | |
361 | |
362 (defun eshell-parse-variable () | |
363 "Parse the next variable reference at point. | |
364 The variable name could refer to either an environment variable, or a | |
365 Lisp variable. The priority order depends on the setting of | |
366 `eshell-prefer-lisp-variables'. | |
367 | |
368 Its purpose is to call `eshell-parse-variable-ref', and then to | |
369 process any indices that come after the variable reference." | |
370 (let* ((get-len (when (eq (char-after) ?#) | |
371 (forward-char) t)) | |
372 value indices) | |
373 (setq value (eshell-parse-variable-ref) | |
374 indices (and (not (eobp)) | |
375 (eq (char-after) ?\[) | |
376 (eshell-parse-indices)) | |
377 value (list 'let | |
378 (list (list 'indices | |
379 (list 'quote indices))) | |
380 value)) | |
381 (if get-len | |
382 (list 'length value) | |
383 value))) | |
384 | |
385 (defun eshell-parse-variable-ref () | |
386 "Eval a variable reference. | |
387 Returns a Lisp form which, if evaluated, will return the value of the | |
388 variable. | |
389 | |
390 Possible options are: | |
391 | |
392 NAME an environment or Lisp variable value | |
393 <LONG-NAME> disambiguates the length of the name | |
394 {COMMAND} result of command is variable's value | |
395 (LISP-FORM) result of Lisp form is variable's value" | |
396 (let (end) | |
397 (cond | |
398 ((eq (char-after) ?{) | |
399 (let ((end (eshell-find-delimiter ?\{ ?\}))) | |
400 (if (not end) | |
401 (throw 'eshell-incomplete ?\{) | |
402 (prog1 | |
403 (list 'eshell-convert | |
404 (list 'eshell-command-to-value | |
405 (list 'eshell-as-subcommand | |
406 (eshell-parse-command | |
407 (cons (1+ (point)) end))))) | |
408 (goto-char (1+ end)))))) | |
409 ((memq (char-after) '(?\' ?\")) | |
410 (let ((name (if (eq (char-after) ?\') | |
411 (eshell-parse-literal-quote) | |
412 (eshell-parse-double-quote)))) | |
413 (if name | |
414 (list 'eshell-get-variable (eval name) 'indices)))) | |
415 ((eq (char-after) ?<) | |
416 (let ((end (eshell-find-delimiter ?\< ?\>))) | |
417 (if (not end) | |
418 (throw 'eshell-incomplete ?\<) | |
419 (let* ((temp (make-temp-name temporary-file-directory)) | |
420 (cmd (concat (buffer-substring (1+ (point)) end) | |
421 " > " temp))) | |
422 (prog1 | |
423 (list | |
424 'let (list (list 'eshell-current-handles | |
425 (list 'eshell-create-handles temp | |
426 (list 'quote 'overwrite)))) | |
427 (list | |
428 'progn | |
429 (list 'eshell-as-subcommand | |
430 (eshell-parse-command cmd)) | |
431 (list 'ignore | |
432 (list 'nconc 'eshell-this-command-hook | |
433 (list 'list | |
434 (list 'function | |
435 (list 'lambda nil | |
436 (list 'delete-file temp)))))) | |
437 (list 'quote temp))) | |
438 (goto-char (1+ end))))))) | |
439 ((eq (char-after) ?\() | |
440 (condition-case err | |
441 (list 'eshell-command-to-value | |
442 (list 'eshell-lisp-command | |
443 (list 'quote (read (current-buffer))))) | |
444 (end-of-file | |
445 (throw 'eshell-incomplete ?\()))) | |
446 ((assoc (char-to-string (char-after)) | |
447 eshell-variable-aliases-list) | |
448 (forward-char) | |
449 (list 'eshell-get-variable | |
450 (char-to-string (char-before)) 'indices)) | |
451 ((looking-at eshell-variable-name-regexp) | |
452 (prog1 | |
453 (list 'eshell-get-variable (match-string 0) 'indices) | |
454 (goto-char (match-end 0)))) | |
455 (t | |
456 (error "Invalid variable reference"))))) | |
457 | |
458 (eshell-deftest var interp-cmd | |
459 "Interpolate command result" | |
460 (eshell-command-result-p "+ ${+ 1 2} 3" "6\n")) | |
461 | |
462 (eshell-deftest var interp-lisp | |
463 "Interpolate Lisp form evalution" | |
464 (eshell-command-result-p "+ $(+ 1 2) 3" "6\n")) | |
465 | |
466 (eshell-deftest var interp-concat | |
467 "Interpolate and concat command" | |
468 (eshell-command-result-p "+ ${+ 1 2}3 3" "36\n")) | |
469 | |
470 (eshell-deftest var interp-concat-lisp | |
471 "Interpolate and concat Lisp form" | |
472 (eshell-command-result-p "+ $(+ 1 2)3 3" "36\n")) | |
473 | |
474 (eshell-deftest var interp-concat2 | |
475 "Interpolate and concat two commands" | |
476 (eshell-command-result-p "+ ${+ 1 2}${+ 1 2} 3" "36\n")) | |
477 | |
478 (eshell-deftest var interp-concat-lisp2 | |
479 "Interpolate and concat two Lisp forms" | |
480 (eshell-command-result-p "+ $(+ 1 2)$(+ 1 2) 3" "36\n")) | |
481 | |
482 (defun eshell-parse-indices () | |
483 "Parse and return a list of list of indices." | |
484 (let (indices) | |
485 (while (eq (char-after) ?\[) | |
486 (let ((end (eshell-find-delimiter ?\[ ?\]))) | |
487 (if (not end) | |
488 (throw 'eshell-incomplete ?\[) | |
489 (forward-char) | |
490 (let (eshell-glob-function) | |
491 (setq indices (cons (eshell-parse-arguments (point) end) | |
492 indices))) | |
493 (goto-char (1+ end))))) | |
494 (nreverse indices))) | |
495 | |
496 (defun eshell-get-variable (name &optional indices) | |
497 "Get the value for the variable NAME." | |
498 (let* ((alias (assoc name eshell-variable-aliases-list)) | |
499 (var (if alias | |
500 (cadr alias) | |
501 name))) | |
502 (if (and alias (functionp var)) | |
503 (funcall var indices) | |
504 (eshell-apply-indices | |
505 (cond | |
506 ((stringp var) | |
507 (let ((sym (intern-soft var))) | |
508 (if (and sym (boundp sym) | |
509 (or eshell-prefer-lisp-variables | |
510 (not (getenv var)))) | |
511 (symbol-value sym) | |
512 (getenv var)))) | |
513 ((symbolp var) | |
514 (symbol-value var)) | |
515 (t | |
516 (error "Unknown variable `%s'" (eshell-stringify var)))) | |
517 indices)))) | |
518 | |
519 (defun eshell-apply-indices (value indices) | |
520 "Apply to VALUE all of the given INDICES, returning the sub-result. | |
521 The format of INDICES is: | |
522 | |
523 ((INT-OR-NAME-OR-OTHER INT-OR-NAME INT-OR-NAME ...) | |
524 ...) | |
525 | |
526 Each member of INDICES represents a level of nesting. If the first | |
527 member of a sublist is not an integer or name, and the value it's | |
528 reference is a string, that will be used as the regexp with which is | |
529 to divide the string into sub-parts. The default is whitespace. | |
530 Otherwise, each INT-OR-NAME refers to an element of the list value. | |
531 Integers imply a direct index, and names, an associate lookup using | |
532 `assoc'. | |
533 | |
534 For example, to retrieve the second element of a user's record in | |
535 '/etc/passwd', the variable reference would look like: | |
536 | |
537 ${egrep johnw /etc/passwd}[: 2]" | |
538 (while indices | |
539 (let ((refs (car indices))) | |
540 (when (stringp value) | |
541 (let (separator) | |
542 (if (not (or (not (stringp (caar indices))) | |
543 (string-match | |
544 (concat "^" eshell-variable-name-regexp "$") | |
545 (caar indices)))) | |
546 (setq separator (caar indices) | |
547 refs (cdr refs))) | |
548 (setq value | |
549 (mapcar 'eshell-convert | |
550 (split-string value separator))))) | |
551 (cond | |
552 ((< (length refs) 0) | |
553 (error "Illegal array variable index: %s" | |
554 (eshell-stringify refs))) | |
555 ((= (length refs) 1) | |
556 (setq value (eshell-index-value value (car refs)))) | |
557 (t | |
558 (let ((new-value (list t))) | |
559 (while refs | |
560 (nconc new-value | |
561 (list (eshell-index-value value | |
562 (car refs)))) | |
563 (setq refs (cdr refs))) | |
564 (setq value (cdr new-value)))))) | |
565 (setq indices (cdr indices))) | |
566 value) | |
567 | |
568 (defun eshell-index-value (value index) | |
569 "Reference VALUE using the given INDEX." | |
570 (if (stringp index) | |
571 (cdr (assoc index value)) | |
572 (cond | |
573 ((ring-p value) | |
574 (if (> index (ring-length value)) | |
575 (error "Index exceeds length of ring") | |
576 (ring-ref value index))) | |
577 ((listp value) | |
578 (if (> index (length value)) | |
579 (error "Index exceeds length of list") | |
580 (nth index value))) | |
581 ((vectorp value) | |
582 (if (> index (length value)) | |
583 (error "Index exceeds length of vector") | |
584 (aref value index))) | |
585 (t | |
586 (error "Invalid data type for indexing"))))) | |
587 | |
588 ;;;_* Variable name completion | |
589 | |
590 (defun eshell-complete-variable-reference () | |
591 "If there is a variable reference, complete it." | |
592 (let ((arg (pcomplete-actual-arg)) index) | |
593 (when (setq index | |
594 (string-match | |
595 (concat "\\$\\(" eshell-variable-name-regexp | |
596 "\\)?\\'") arg)) | |
597 (setq pcomplete-stub (substring arg (1+ index))) | |
598 (throw 'pcomplete-completions (eshell-variables-list))))) | |
599 | |
600 (defun eshell-variables-list () | |
601 "Generate list of applicable variables." | |
602 (let ((argname pcomplete-stub) | |
603 completions) | |
604 (eshell-for alias eshell-variable-aliases-list | |
605 (if (string-match (concat "^" argname) (car alias)) | |
606 (setq completions (cons (car alias) completions)))) | |
607 (sort | |
608 (append | |
609 (mapcar | |
610 (function | |
611 (lambda (varname) | |
612 (let ((value (eshell-get-variable varname))) | |
613 (if (and value | |
614 (stringp value) | |
615 (file-directory-p value)) | |
616 (concat varname (char-to-string directory-sep-char)) | |
617 varname)))) | |
618 (eshell-envvar-names (eshell-environment-variables))) | |
619 (all-completions argname obarray 'boundp) | |
620 completions) | |
621 'string-lessp))) | |
622 | |
623 (defun eshell-complete-variable-assignment () | |
624 "If there is a variable assignment, allow completion of entries." | |
625 (let ((arg (pcomplete-actual-arg)) pos) | |
626 (when (string-match (concat "\\`" eshell-variable-name-regexp "=") arg) | |
627 (setq pos (match-end 0)) | |
628 (if (string-match "\\(:\\)[^:]*\\'" arg) | |
629 (setq pos (match-end 1))) | |
630 (setq pcomplete-stub (substring arg pos)) | |
631 (throw 'pcomplete-completions (pcomplete-entries))))) | |
632 | |
633 ;;; Code: | |
634 | |
635 ;;; esh-var.el ends here |