esh-var.el 21 KB

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