emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] /srv/bzr/emacs/trunk r110834: * lisp/env.el (env--substitu


From: Stefan Monnier
Subject: [Emacs-diffs] /srv/bzr/emacs/trunk r110834: * lisp/env.el (env--substitute-vars-regexp): New const.
Date: Thu, 08 Nov 2012 10:10:08 -0500
User-agent: Bazaar (2.5.0)

------------------------------------------------------------
revno: 110834
committer: Stefan Monnier <address@hidden>
branch nick: trunk
timestamp: Thu 2012-11-08 10:10:08 -0500
message:
  * lisp/env.el (env--substitute-vars-regexp): New const.
  (substitute-env-vars): Use it.  Add `only-defined' arg.
  * lisp/net/tramp.el (tramp-replace-environment-variables): Use it.
modified:
  lisp/ChangeLog
  lisp/env.el
  lisp/minibuffer.el
  lisp/net/tramp.el
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2012-11-08 14:21:21 +0000
+++ b/lisp/ChangeLog    2012-11-08 15:10:08 +0000
@@ -1,5 +1,9 @@
 2012-11-08  Stefan Monnier  <address@hidden>
 
+       * env.el (env--substitute-vars-regexp): New const.
+       (substitute-env-vars): Use it.  Add `only-defined' arg.
+       * net/tramp.el (tramp-replace-environment-variables): Use it.
+
        * emacs-lisp/bytecomp.el (byte-compile-initial-macro-environment):
        Byte-compile *before* eval in eval-and-compile.
        (byte-compile-log-warning): Remove redundant inhibit-read-only.

=== modified file 'lisp/env.el'
--- a/lisp/env.el       2012-06-22 21:24:54 +0000
+++ b/lisp/env.el       2012-11-08 15:10:08 +0000
@@ -57,31 +57,31 @@
 ;; History list for VALUE argument to setenv.
 (defvar setenv-history nil)
 
+(defconst env--substitute-vars-regexp
+  (rx "$"
+      (or (submatch-n 1 (1+ (regexp "[[:alnum:]_]")))
+          (and "{" (submatch-n 1 (minimal-match (0+ anything))) "}")
+          "$")))
 
-(defun substitute-env-vars (string)
+(defun substitute-env-vars (string &optional only-defined)
   "Substitute environment variables referred to in STRING.
 `$FOO' where FOO is an environment variable name means to substitute
 the value of that variable.  The variable name should be terminated
 with a character not a letter, digit or underscore; otherwise, enclose
 the entire variable name in braces.  For instance, in `ab$cd-x',
 `$cd' is treated as an environment variable.
+If ONLY-DEFINED is nil, references to undefined environment variables
+are replaced by the empty string; if it is non-nil, they are left unchanged.
 
 Use `$$' to insert a single dollar sign."
   (let ((start 0))
-    (while (string-match
-           (eval-when-compile
-             (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
-                     (and "${" (submatch (minimal-match (0+ anything))) "}")
-                     "$$")))
-           string start)
+    (while (string-match env--substitute-vars-regexp string start)
       (cond ((match-beginning 1)
             (let ((value (getenv (match-string 1 string))))
-              (setq string (replace-match (or value "") t t string)
-                    start (+ (match-beginning 0) (length value)))))
-           ((match-beginning 2)
-            (let ((value (getenv (match-string 2 string))))
-              (setq string (replace-match (or value "") t t string)
-                    start (+ (match-beginning 0) (length value)))))
+               (if (and (null value) only-defined)
+                   (setq start (match-end 0))
+              (setq string (replace-match (or value "") t t string)
+                       start (+ (match-beginning 0) (length value))))))
            (t
             (setq string (replace-match "$" t t string)
                   start (+ (match-beginning 0) 1)))))
@@ -185,7 +185,7 @@
 the environment.  Otherwise, value is a string.
 
 If optional parameter FRAME is non-nil, then it should be a
-frame.  This function will look up VARIABLE in its 'environment
+frame.  This function will look up VARIABLE in its `environment'
 parameter.
 
 Otherwise, this function searches `process-environment' for

=== modified file 'lisp/minibuffer.el'
--- a/lisp/minibuffer.el        2012-10-28 19:07:52 +0000
+++ b/lisp/minibuffer.el        2012-11-08 15:10:08 +0000
@@ -51,6 +51,9 @@
 
 ;;; Todo:
 
+;; - Make *Completions* readable even if some of the completion
+;;   entries have LF chars or spaces in them (including at
+;;   beginning/end) or are very long.
 ;; - for M-x, cycle-sort commands that have no key binding first.
 ;; - Make things like icomplete-mode or lightning-completion work with
 ;;   completion-in-region-mode.
@@ -74,6 +77,9 @@
 ;;   - whether the user wants completion to pay attention to case.
 ;;   e.g. we may want to make it possible for the user to say "first try
 ;;   completion case-sensitively, and if that fails, try to ignore case".
+;;   Maybe the trick is that we should distinguish completion-ignore-case in
+;;   try/all-completions (obey user's preference) from its use in
+;;   test-completion (obey the underlying object's semantics).
 
 ;; - add support for ** to pcm.
 ;; - Add vc-file-name-completion-table to read-file-name-internal.
@@ -2048,6 +2054,8 @@
           process-environment))
 
 (defconst completion--embedded-envvar-re
+  ;; We can't reuse env--substitute-vars-regexp because we need to match only
+  ;; potentially-unfinished envvars at end of string.
   (concat "\\(?:^\\|[^$]\\(?:\\$\\$\\)*\\)"
           "$\\([[:alnum:]_]*\\|{\\([^}]*\\)\\)\\'"))
 

=== modified file 'lisp/net/tramp.el'
--- a/lisp/net/tramp.el 2012-10-09 12:49:02 +0000
+++ b/lisp/net/tramp.el 2012-11-08 15:10:08 +0000
@@ -1748,20 +1748,26 @@
   (or (file-modes filename)
       (logand (default-file-modes) (tramp-compat-octal-to-decimal "0666"))))
 
-(defun tramp-replace-environment-variables (filename)
-  "Replace environment variables in FILENAME.
+(defalias 'tramp-replace-environment-variables
+  (if (ignore-errors
+        (equal "${ tramp?}" (substitute-env-vars "${ tramp?}" 'only-defined)))
+      (lambda (filename)
+        "Like `substitute-env-vars' with `only-defined' non-nil."
+        (substitute-env-vars filename 'only-defined))
+    (lambda (filename)
+      "Replace environment variables in FILENAME.
 Return the string with the replaced variables."
-  (save-match-data
-    (let ((idx (string-match "$\\(\\w+\\)" filename)))
-      ;; `$' is coded as `$$'.
-      (when (and idx
-                (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
-                (getenv (match-string 1 filename)))
-       (setq filename
-             (replace-match
-              (substitute-in-file-name (match-string 0 filename))
-              t nil filename)))
-      filename)))
+      (save-match-data
+        (let ((idx (string-match "$\\(\\w+\\)" filename)))
+          ;; `$' is coded as `$$'.
+          (when (and idx
+                     (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
+                     (getenv (match-string 1 filename)))
+            (setq filename
+                  (replace-match
+                   (substitute-in-file-name (match-string 0 filename))
+                   t nil filename)))
+          filename)))))
 
 ;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
 ;; which calls corresponding functions (see minibuf.el).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]