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