1 ;;; esh-var.el --- handling of variables
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
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 2, or (at your option)
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; 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.
26 (eval-when-compile (require 'esh-maint
))
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"
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.
45 ;; Interval the value of an environment variable, or a Lisp variable
49 ;; "-" is a legal part of a variable name.
53 ;; Only "MYVAR" is part of the variable name in this case.
57 ;; Returns the length of the value of VARIABLE. This could also be
58 ;; done using the `length' Lisp function.
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
69 ;; Returns the value of an eshell subcommand. See the note above
70 ;; regarding Lisp evaluations.
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.
80 ;; As above, except that splitting occurs at the colon now.
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.
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]'.
98 ;; Calls `assoc' on ANYVAR with 'hello', expecting it to be an alist.
102 ;; Returns the length of the cdr of the element of ANYVAR who car is
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:
109 ;; /tmp $ echo /var/spool/mail/johnw
110 ;; /var/spool/mail/johnw
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).
125 (defcustom eshell-var-load-hook
'(eshell-var-initialize)
126 "*A list of functions to call when loading `eshell-var'."
130 (defcustom eshell-prefer-lisp-variables nil
131 "*If non-nil, prefer Lisp variables to environment variables."
135 (defcustom eshell-complete-export-definition t
136 "*If non-nil, completing names for `export' shows current definition."
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."
148 (defcustom eshell-variable-aliases-list
150 ("COLUMNS" (lambda (indices) (window-width)) t
)
151 ("LINES" (lambda (indices) (window-height)) t
)
154 ("_" (lambda (indices)
156 (car (last eshell-last-arguments
))
157 (eshell-apply-indices eshell-last-arguments
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)
173 eshell-command-arguments
174 (eshell-apply-indices eshell-command-arguments
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'.
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
189 :type
'(repeat (list string sexp
190 (choice (const :tag
"Copy to environment" t
)
191 (const :tag
"Use only in Eshell" nil
))))
194 (put 'eshell-variable-aliases-list
'risky-local-variable t
)
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))
204 (define-key eshell-command-map
[(meta ?v
)] 'eshell-insert-envvar
)
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
'(?$
)))
211 (add-hook 'eshell-parse-argument-hook
'eshell-interpolate-variable t t
)
213 (add-hook 'eshell-prepare-command-hook
214 'eshell-handle-local-variables nil t
)
216 (when (eshell-using-module 'eshell-cmpl
)
217 (add-hook 'pcomplete-try-first-hook
218 'eshell-complete-variable-reference nil t
)
219 (add-hook 'pcomplete-try-first-hook
220 'eshell-complete-variable-assignment nil t
)))
222 (defun eshell-handle-local-variables ()
223 "Allow for the syntax 'VAR=val <command> <args>'."
224 ;; strip off any null commands, which can only happen if a variable
225 ;; evaluates to nil, such as "$var x", where `var' is nil. The
226 ;; command name in that case becomes `x', for compatibility with
227 ;; most regular shells (the difference is that they do an
228 ;; interpolation pass before the argument parsing pass, but Eshell
229 ;; does both at the same time).
230 (while (and (not eshell-last-command-name
)
231 eshell-last-arguments
)
232 (setq eshell-last-command-name
(car eshell-last-arguments
)
233 eshell-last-arguments
(cdr eshell-last-arguments
)))
234 (let ((setvar "\\`\\([A-Za-z_][A-Za-z0-9_]*\\)=\\(.*\\)\\'")
235 (command (eshell-stringify eshell-last-command-name
))
236 (args eshell-last-arguments
))
237 ;; local variable settings (such as 'CFLAGS=-O2 make') are handled
238 ;; by making the whole command into a subcommand, and calling
239 ;; setenv immediately before the command is invoked. This means
240 ;; that 'BLAH=x cd blah' won't work exactly as expected, but that
241 ;; is by no means a typical use of local environment variables.
242 (if (and command
(string-match setvar command
))
244 'eshell-replace-command
246 'eshell-as-subcommand
250 (while (string-match setvar command
)
253 (list 'setenv
(match-string 1 command
)
254 (match-string 2 command
)
255 (= (length (match-string 2 command
)) 0))))
256 (setq command
(eshell-stringify (car args
))
259 (list (list 'eshell-named-command
260 command
(list 'quote args
)))))))))
262 (defun eshell-interpolate-variable ()
263 "Parse a variable interpolation.
264 This function is explicit for adding to `eshell-parse-argument-hook'."
265 (when (and (eq (char-after) ?$
)
266 (/= (1+ (point)) (point-max)))
268 (list 'eshell-escape-arg
269 (eshell-parse-variable))))
271 (defun eshell/define
(var-alias definition
)
272 "Define an VAR-ALIAS using DEFINITION."
274 (setq eshell-variable-aliases-list
275 (delq (assoc var-alias eshell-variable-aliases-list
)
276 eshell-variable-aliases-list
))
277 (let ((def (assoc var-alias eshell-variable-aliases-list
))
280 (list 'quote
(if (= (length definition
) 1)
284 (setq eshell-variable-aliases-list
285 (delq (assoc var-alias eshell-variable-aliases-list
)
286 eshell-variable-aliases-list
)))
287 (setq eshell-variable-aliases-list
289 eshell-variable-aliases-list
))))
292 (defun eshell/export
(&rest sets
)
293 "This alias allows the 'export' command to act as bash users expect."
295 (if (and (stringp (car sets
))
296 (string-match "^\\([^=]+\\)=\\(.*\\)" (car sets
)))
297 (setenv (match-string 1 (car sets
))
298 (match-string 2 (car sets
))))
299 (setq sets
(cdr sets
))))
301 (defun pcomplete/eshell-mode
/export
()
302 "Completion function for Eshell's `export'."
303 (while (pcomplete-here
304 (if eshell-complete-export-definition
306 (eshell-envvar-names)))))
308 (defun eshell/unset
(&rest args
)
309 "Unset an environment variable."
311 (if (stringp (car args
))
312 (setenv (car args
) nil t
))
313 (setq args
(cdr args
))))
315 (defun pcomplete/eshell-mode
/unset
()
316 "Completion function for Eshell's `unset'."
317 (while (pcomplete-here (eshell-envvar-names))))
319 (defun eshell/setq
(&rest args
)
320 "Allow command-ish use of `setq'."
323 (let ((sym (intern (car args
)))
325 (setq last-value
(set sym val
)
329 (defun pcomplete/eshell-mode
/setq
()
330 "Completion function for Eshell's `setq'."
331 (while (and (pcomplete-here (all-completions pcomplete-stub
335 (defun eshell/env
(&rest args
)
336 "Implemention of `env' in Lisp."
337 (eshell-init-print-buffer)
338 (eshell-eval-using-options
340 '((?h
"help" nil nil
"show this usage screen")
342 :usage
"<no arguments>")
343 (eshell-for setting
(sort (eshell-environment-variables)
345 (eshell-buffered-print setting
"\n"))
348 (defun eshell-insert-envvar (envvar-name)
349 "Insert ENVVAR-NAME into the current buffer at point."
351 (list (read-envvar-name "Name of environment variable: " t
)))
352 (insert-and-inherit "$" envvar-name
))
354 (defun eshell-envvar-names (&optional environment
)
355 "Return a list of currently visible environment variable names."
358 (substring x
0 (string-match "=" x
))))
359 (or environment process-environment
)))
361 (defun eshell-environment-variables ()
362 "Return a `process-environment', fully updated.
363 This involves setting any variable aliases which affect the
364 environment, as specified in `eshell-variable-aliases-list'."
365 (let ((process-environment (eshell-copy-environment)))
366 (eshell-for var-alias eshell-variable-aliases-list
367 (if (nth 2 var-alias
)
368 (setenv (car var-alias
)
370 (or (eshell-get-variable (car var-alias
)) "")))))
371 process-environment
))
373 (defun eshell-parse-variable ()
374 "Parse the next variable reference at point.
375 The variable name could refer to either an environment variable, or a
376 Lisp variable. The priority order depends on the setting of
377 `eshell-prefer-lisp-variables'.
379 Its purpose is to call `eshell-parse-variable-ref', and then to
380 process any indices that come after the variable reference."
381 (let* ((get-len (when (eq (char-after) ?
#)
384 (setq value
(eshell-parse-variable-ref)
385 indices
(and (not (eobp))
386 (eq (char-after) ?\
[)
387 (eshell-parse-indices))
390 (list 'quote indices
)))
396 (defun eshell-parse-variable-ref ()
397 "Eval a variable reference.
398 Returns a Lisp form which, if evaluated, will return the value of the
401 Possible options are:
403 NAME an environment or Lisp variable value
404 <LONG-NAME> disambiguates the length of the name
405 {COMMAND} result of command is variable's value
406 (LISP-FORM) result of Lisp form is variable's value"
409 ((eq (char-after) ?
{)
410 (let ((end (eshell-find-delimiter ?\
{ ?\
})))
412 (throw 'eshell-incomplete ?\
{)
414 (list 'eshell-convert
415 (list 'eshell-command-to-value
416 (list 'eshell-as-subcommand
417 (eshell-parse-command
418 (cons (1+ (point)) end
)))))
419 (goto-char (1+ end
))))))
420 ((memq (char-after) '(?
\' ?
\"))
421 (let ((name (if (eq (char-after) ?
\')
422 (eshell-parse-literal-quote)
423 (eshell-parse-double-quote))))
425 (list 'eshell-get-variable
(eval name
) 'indices
))))
426 ((eq (char-after) ?
<)
427 (let ((end (eshell-find-delimiter ?\
< ?\
>)))
429 (throw 'eshell-incomplete ?\
<)
430 (let* ((temp (make-temp-file temporary-file-directory
))
431 (cmd (concat (buffer-substring (1+ (point)) end
)
435 'let
(list (list 'eshell-current-handles
436 (list 'eshell-create-handles temp
437 (list 'quote
'overwrite
))))
440 (list 'eshell-as-subcommand
441 (eshell-parse-command cmd
))
443 (list 'nconc
'eshell-this-command-hook
447 (list 'delete-file temp
))))))
449 (goto-char (1+ end
)))))))
450 ((eq (char-after) ?\
()
452 (list 'eshell-command-to-value
453 (list 'eshell-lisp-command
454 (list 'quote
(read (current-buffer)))))
456 (throw 'eshell-incomplete ?\
())))
457 ((assoc (char-to-string (char-after))
458 eshell-variable-aliases-list
)
460 (list 'eshell-get-variable
461 (char-to-string (char-before)) 'indices
))
462 ((looking-at eshell-variable-name-regexp
)
464 (list 'eshell-get-variable
(match-string 0) 'indices
)
465 (goto-char (match-end 0))))
467 (error "Invalid variable reference")))))
469 (eshell-deftest var interp-cmd
470 "Interpolate command result"
471 (eshell-command-result-p "+ ${+ 1 2} 3" "6\n"))
473 (eshell-deftest var interp-lisp
474 "Interpolate Lisp form evalution"
475 (eshell-command-result-p "+ $(+ 1 2) 3" "6\n"))
477 (eshell-deftest var interp-concat
478 "Interpolate and concat command"
479 (eshell-command-result-p "+ ${+ 1 2}3 3" "36\n"))
481 (eshell-deftest var interp-concat-lisp
482 "Interpolate and concat Lisp form"
483 (eshell-command-result-p "+ $(+ 1 2)3 3" "36\n"))
485 (eshell-deftest var interp-concat2
486 "Interpolate and concat two commands"
487 (eshell-command-result-p "+ ${+ 1 2}${+ 1 2} 3" "36\n"))
489 (eshell-deftest var interp-concat-lisp2
490 "Interpolate and concat two Lisp forms"
491 (eshell-command-result-p "+ $(+ 1 2)$(+ 1 2) 3" "36\n"))
493 (defun eshell-parse-indices ()
494 "Parse and return a list of list of indices."
496 (while (eq (char-after) ?\
[)
497 (let ((end (eshell-find-delimiter ?\
[ ?\
])))
499 (throw 'eshell-incomplete ?\
[)
501 (let (eshell-glob-function)
502 (setq indices
(cons (eshell-parse-arguments (point) end
)
504 (goto-char (1+ end
)))))
507 (defun eshell-get-variable (name &optional indices
)
508 "Get the value for the variable NAME."
509 (let* ((alias (assoc name eshell-variable-aliases-list
))
513 (if (and alias
(functionp var
))
514 (funcall var indices
)
515 (eshell-apply-indices
518 (let ((sym (intern-soft var
)))
519 (if (and sym
(boundp sym
)
520 (or eshell-prefer-lisp-variables
527 (error "Unknown variable `%s'" (eshell-stringify var
))))
530 (defun eshell-apply-indices (value indices
)
531 "Apply to VALUE all of the given INDICES, returning the sub-result.
532 The format of INDICES is:
534 ((INT-OR-NAME-OR-OTHER INT-OR-NAME INT-OR-NAME ...)
537 Each member of INDICES represents a level of nesting. If the first
538 member of a sublist is not an integer or name, and the value it's
539 reference is a string, that will be used as the regexp with which is
540 to divide the string into sub-parts. The default is whitespace.
541 Otherwise, each INT-OR-NAME refers to an element of the list value.
542 Integers imply a direct index, and names, an associate lookup using
545 For example, to retrieve the second element of a user's record in
546 '/etc/passwd', the variable reference would look like:
548 ${egrep johnw /etc/passwd}[: 2]"
550 (let ((refs (car indices
)))
551 (when (stringp value
)
553 (if (not (or (not (stringp (caar indices
)))
555 (concat "^" eshell-variable-name-regexp
"$")
557 (setq separator
(caar indices
)
560 (mapcar 'eshell-convert
561 (split-string value separator
)))))
564 (error "Illegal array variable index: %s"
565 (eshell-stringify refs
)))
567 (setq value
(eshell-index-value value
(car refs
))))
569 (let ((new-value (list t
)))
572 (list (eshell-index-value value
574 (setq refs
(cdr refs
)))
575 (setq value
(cdr new-value
))))))
576 (setq indices
(cdr indices
)))
579 (defun eshell-index-value (value index
)
580 "Reference VALUE using the given INDEX."
582 (cdr (assoc index value
))
585 (if (> index
(ring-length value
))
586 (error "Index exceeds length of ring")
587 (ring-ref value index
)))
589 (if (> index
(length value
))
590 (error "Index exceeds length of list")
593 (if (> index
(length value
))
594 (error "Index exceeds length of vector")
597 (error "Invalid data type for indexing")))))
599 ;;;_* Variable name completion
601 (defun eshell-complete-variable-reference ()
602 "If there is a variable reference, complete it."
603 (let ((arg (pcomplete-actual-arg)) index
)
606 (concat "\\$\\(" eshell-variable-name-regexp
608 (setq pcomplete-stub
(substring arg
(1+ index
)))
609 (throw 'pcomplete-completions
(eshell-variables-list)))))
611 (defun eshell-variables-list ()
612 "Generate list of applicable variables."
613 (let ((argname pcomplete-stub
)
615 (eshell-for alias eshell-variable-aliases-list
616 (if (string-match (concat "^" argname
) (car alias
))
617 (setq completions
(cons (car alias
) completions
))))
623 (let ((value (eshell-get-variable varname
)))
626 (file-directory-p value
))
627 (concat varname
(char-to-string directory-sep-char
))
629 (eshell-envvar-names (eshell-environment-variables)))
630 (all-completions argname obarray
'boundp
)
634 (defun eshell-complete-variable-assignment ()
635 "If there is a variable assignment, allow completion of entries."
636 (let ((arg (pcomplete-actual-arg)) pos
)
637 (when (string-match (concat "\\`" eshell-variable-name-regexp
"=") arg
)
638 (setq pos
(match-end 0))
639 (if (string-match "\\(:\\)[^:]*\\'" arg
)
640 (setq pos
(match-end 1)))
641 (setq pcomplete-stub
(substring arg pos
))
642 (throw 'pcomplete-completions
(pcomplete-entries)))))
646 ;;; esh-var.el ends here