Add with-file-modes macro, and use it
[bpt/emacs.git] / lisp / eshell / esh-var.el
index a0f0f88..689eb66 100644 (file)
@@ -1,7 +1,6 @@
-;;; esh-var.el --- handling of variables
+;;; esh-var.el --- handling of variables  -*- lexical-binding:t -*-
 
-;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
+;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
 
 ;; Author: John Wiegley <johnw@gnu.org>
 
 
 (provide 'esh-var)
 
-(eval-when-compile
-  (require 'pcomplete)
-  (require 'esh-test)
-  (require 'esh-util)
-  (require 'esh-opt)
-  (require 'esh-mode))
+(require 'esh-util)
+(require 'esh-cmd)
+(require 'esh-opt)
+
+(require 'pcomplete)
 (require 'env)
 (require 'ring)
 
@@ -127,28 +125,29 @@ variable value, a subcommand, or even the result of a Lisp form."
 
 ;;; User Variables:
 
-(defcustom eshell-var-load-hook '(eshell-var-initialize)
-  "*A list of functions to call when loading `eshell-var'."
+(defcustom eshell-var-load-hook nil
+  "A list of functions to call when loading `eshell-var'."
+  :version "24.1"                      ; removed eshell-var-initialize
   :type 'hook
   :group 'eshell-var)
 
 (defcustom eshell-prefer-lisp-variables nil
-  "*If non-nil, prefer Lisp variables to environment variables."
+  "If non-nil, prefer Lisp variables to environment variables."
   :type 'boolean
   :group 'eshell-var)
 
 (defcustom eshell-complete-export-definition t
-  "*If non-nil, completing names for `export' shows current definition."
+  "If non-nil, completing names for `export' shows current definition."
   :type 'boolean
   :group 'eshell-var)
 
 (defcustom eshell-modify-global-environment nil
-  "*If non-nil, using `export' changes Emacs's global environment."
+  "If non-nil, using `export' changes Emacs's global environment."
   :type 'boolean
   :group 'eshell-var)
 
 (defcustom eshell-variable-name-regexp "[A-Za-z0-9_-]+"
-  "*A regexp identifying what constitutes a variable name reference.
+  "A regexp identifying what constitutes a variable name reference.
 Note that this only applies for '$NAME'.  If the syntax '$<NAME>' is
 used, then NAME can contain any character, including angle brackets,
 if they are quoted with a backslash."
@@ -183,11 +182,11 @@ if they are quoted with a backslash."
               eshell-command-arguments
             (eshell-apply-indices eshell-command-arguments
                                   indices)))))
-  "*This list provides aliasing for variable references.
+  "This list provides aliasing for variable references.
 It is very similar in concept to what `eshell-user-aliases-list' does
-for commands.  Each member of this defines defines the name of a
-command, and the Lisp value to return for that variable if it is
-accessed via the syntax '$NAME'.
+for commands.  Each member of this defines the name of a command,
+and the Lisp value to return for that variable if it is accessed
+via the syntax '$NAME'.
 
 If the value is a function, that function will be called with two
 arguments: the list of the indices that was used in the reference, and
@@ -345,15 +344,14 @@ This function is explicit for adding to `eshell-parse-argument-hook'."
              (pcomplete-here))))
 
 (defun eshell/env (&rest args)
-  "Implemention of `env' in Lisp."
+  "Implementation of `env' in Lisp."
   (eshell-init-print-buffer)
   (eshell-eval-using-options
    "env" args
    '((?h "help" nil nil "show this usage screen")
      :external "env"
      :usage "<no arguments>")
-   (eshell-for setting (sort (eshell-environment-variables)
-                            'string-lessp)
+   (dolist (setting (sort (eshell-environment-variables) 'string-lessp))
      (eshell-buffered-print setting "\n"))
    (eshell-flush)))
 
@@ -375,7 +373,7 @@ This function is explicit for adding to `eshell-parse-argument-hook'."
 This involves setting any variable aliases which affect the
 environment, as specified in `eshell-variable-aliases-list'."
   (let ((process-environment (eshell-copy-environment)))
-    (eshell-for var-alias eshell-variable-aliases-list
+    (dolist (var-alias eshell-variable-aliases-list)
       (if (nth 2 var-alias)
          (setenv (car var-alias)
                  (eshell-stringify
@@ -397,12 +395,9 @@ process any indices that come after the variable reference."
          indices (and (not (eobp))
                       (eq (char-after) ?\[)
                       (eshell-parse-indices))
-         value (list 'let
-                     (list (list 'indices
-                                 (list 'quote indices)))
-                     value))
+         value `(let ((indices ',indices)) ,value))
     (if get-len
-       (list 'length value)
+       `(length ,value)
       value)))
 
 (defun eshell-parse-variable-ref ()
@@ -416,91 +411,68 @@ Possible options are:
   <LONG-NAME>   disambiguates the length of the name
   {COMMAND}     result of command is variable's value
   (LISP-FORM)   result of Lisp form is variable's value"
-  (let (end)
-    (cond
-     ((eq (char-after) ?{)
-      (let ((end (eshell-find-delimiter ?\{ ?\})))
-       (if (not end)
-           (throw 'eshell-incomplete ?\{)
-         (prog1
-             (list 'eshell-convert
-                   (list 'eshell-command-to-value
-                         (list 'eshell-as-subcommand
-                               (eshell-parse-command
-                                (cons (1+ (point)) end)))))
-           (goto-char (1+ end))))))
-     ((memq (char-after) '(?\' ?\"))
-      (let ((name (if (eq (char-after) ?\')
-                     (eshell-parse-literal-quote)
-                   (eshell-parse-double-quote))))
-       (if name
+  (cond
+   ((eq (char-after) ?{)
+    (let ((end (eshell-find-delimiter ?\{ ?\})))
+      (if (not end)
+          (throw 'eshell-incomplete ?\{)
+        (prog1
+            (list 'eshell-convert
+                  (list 'eshell-command-to-value
+                        (list 'eshell-as-subcommand
+                              (eshell-parse-command
+                               (cons (1+ (point)) end)))))
+          (goto-char (1+ end))))))
+   ((memq (char-after) '(?\' ?\"))
+    (let ((name (if (eq (char-after) ?\')
+                    (eshell-parse-literal-quote)
+                  (eshell-parse-double-quote))))
+      (if name
          (list 'eshell-get-variable (eval name) 'indices))))
-     ((eq (char-after) ?\<)
-      (let ((end (eshell-find-delimiter ?\< ?\>)))
-       (if (not end)
-           (throw 'eshell-incomplete ?\<)
-         (let* ((temp (make-temp-file temporary-file-directory))
-                (cmd (concat (buffer-substring (1+ (point)) end)
-                             " > " temp)))
-           (prog1
-               (list
-                'let (list (list 'eshell-current-handles
-                                 (list 'eshell-create-handles temp
-                                       (list 'quote 'overwrite))))
-                (list
-                 'progn
-                 (list 'eshell-as-subcommand
-                       (eshell-parse-command cmd))
-                 (list 'ignore
-                       (list 'nconc 'eshell-this-command-hook
-                             (list 'list
-                                   (list 'function
-                                         (list 'lambda nil
-                                               (list 'delete-file temp))))))
-                 (list 'quote temp)))
-             (goto-char (1+ end)))))))
-     ((eq (char-after) ?\()
-      (condition-case err
-         (list 'eshell-command-to-value
-               (list 'eshell-lisp-command
-                     (list 'quote (read (current-buffer)))))
-       (end-of-file
-        (throw 'eshell-incomplete ?\())))
-     ((assoc (char-to-string (char-after))
-            eshell-variable-aliases-list)
-      (forward-char)
-      (list 'eshell-get-variable
-           (char-to-string (char-before)) 'indices))
-     ((looking-at eshell-variable-name-regexp)
-      (prog1
-         (list 'eshell-get-variable (match-string 0) 'indices)
-       (goto-char (match-end 0))))
-     (t
-      (error "Invalid variable reference")))))
-
-(eshell-deftest var interp-cmd
-  "Interpolate command result"
-  (eshell-command-result-p "+ ${+ 1 2} 3" "6\n"))
-
-(eshell-deftest var interp-lisp
-  "Interpolate Lisp form evalution"
-  (eshell-command-result-p "+ $(+ 1 2) 3" "6\n"))
-
-(eshell-deftest var interp-concat
-  "Interpolate and concat command"
-  (eshell-command-result-p "+ ${+ 1 2}3 3" "36\n"))
-
-(eshell-deftest var interp-concat-lisp
-  "Interpolate and concat Lisp form"
-  (eshell-command-result-p "+ $(+ 1 2)3 3" "36\n"))
-
-(eshell-deftest var interp-concat2
-  "Interpolate and concat two commands"
-  (eshell-command-result-p "+ ${+ 1 2}${+ 1 2} 3" "36\n"))
+   ((eq (char-after) ?\<)
+    (let ((end (eshell-find-delimiter ?\< ?\>)))
+      (if (not end)
+          (throw 'eshell-incomplete ?\<)
+        (let* ((temp (make-temp-file temporary-file-directory))
+               (cmd (concat (buffer-substring (1+ (point)) end)
+                            " > " temp)))
+          (prog1
+              (list
+               'let (list (list 'eshell-current-handles
+                                (list 'eshell-create-handles temp
+                                      (list 'quote 'overwrite))))
+               (list
+                'progn
+                (list 'eshell-as-subcommand
+                      (eshell-parse-command cmd))
+                (list 'ignore
+                      (list 'nconc 'eshell-this-command-hook
+                            (list 'list
+                                  (list 'function
+                                        (list 'lambda nil
+                                              (list 'delete-file temp))))))
+                (list 'quote temp)))
+            (goto-char (1+ end)))))))
+   ((eq (char-after) ?\()
+    (condition-case nil
+        (list 'eshell-command-to-value
+              (list 'eshell-lisp-command
+                    (list 'quote (read (current-buffer)))))
+      (end-of-file
+       (throw 'eshell-incomplete ?\())))
+   ((assoc (char-to-string (char-after))
+           eshell-variable-aliases-list)
+    (forward-char)
+    (list 'eshell-get-variable
+          (char-to-string (char-before)) 'indices))
+   ((looking-at eshell-variable-name-regexp)
+    (prog1
+        (list 'eshell-get-variable (match-string 0) 'indices)
+      (goto-char (match-end 0))))
+   (t
+    (error "Invalid variable reference"))))
 
-(eshell-deftest var interp-concat-lisp2
-  "Interpolate and concat two Lisp forms"
-  (eshell-command-result-p "+ $(+ 1 2)$(+ 1 2) 3" "36\n"))
+(defvar eshell-glob-function)
 
 (defun eshell-parse-indices ()
   "Parse and return a list of list of indices."
@@ -530,6 +502,7 @@ Possible options are:
         (let ((sym (intern-soft var)))
           (if (and sym (boundp sym)
                    (or eshell-prefer-lisp-variables
+                       (memq sym eshell--local-vars) ; bug#15372
                        (not (getenv var))))
               (symbol-value sym)
             (getenv var))))
@@ -624,7 +597,7 @@ For example, to retrieve the second element of a user's record in
   "Generate list of applicable variables."
   (let ((argname pcomplete-stub)
        completions)
-    (eshell-for alias eshell-variable-aliases-list
+    (dolist (alias eshell-variable-aliases-list)
       (if (string-match (concat "^" argname) (car alias))
          (setq completions (cons (car alias) completions))))
     (sort
@@ -653,5 +626,4 @@ For example, to retrieve the second element of a user's record in
       (setq pcomplete-stub (substring arg pos))
       (throw 'pcomplete-completions (pcomplete-entries)))))
 
-;; arch-tag: 393654fe-bdad-4f27-9a10-b1472ded14cf
 ;;; esh-var.el ends here