changeset 29674:611062a8c71e

(PC-env-vars-alist): New variable. (PC-complete-as-file-name): New function. (partial-completion-mode): Initialize PC-env-vars-alist from process-environment. (PC-do-completion): Handle completion of env vars.
author Gerd Moellmann <gerd@gnu.org>
date Thu, 15 Jun 2000 14:42:19 +0000
parents 30b29d26cd76
children 4eb75bd04c1b
files lisp/complete.el
diffstat 1 files changed, 55 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/complete.el	Thu Jun 15 13:01:14 2000 +0000
+++ b/lisp/complete.el	Thu Jun 15 14:42:19 2000 +0000
@@ -1,6 +1,7 @@
 ;;; complete.el --- partial completion mechanism plus other goodies
 
-;; Copyright (C) 1990, 1991, 1992, 1993, 1999 Free Software Foundation, Inc.
+;; Copyright (C) 1990, 1991, 1992, 1993, 1999, 2000
+;;  Free Software Foundation, Inc.
 
 ;; Author: Dave Gillespie <daveg@synaptics.com>
 ;; Keywords: abbrev convenience
@@ -156,6 +157,10 @@
 
 (defvar PC-default-bindings t
   "If non-nil, default partial completion key bindings are suppressed.")
+
+(defvar PC-env-vars-alist nil
+  "A list of the environment variable names and values.")
+
 
 (defvar PC-old-read-file-name-internal nil)
 
@@ -197,6 +202,13 @@
 		 (symbol-function 'read-file-name-internal))
 	   (fset 'read-file-name-internal
 		 'PC-read-include-file-name-internal)))
+    (when (and on-p (null PC-env-vars-alist))
+      (setq PC-env-vars-alist
+	    (mapcar (lambda (string)
+		      (let ((d (string-match "=" string)))
+			(cons (concat "$" (substring string 0 d))
+			      (and d (substring string (1+ d))))))
+		    process-environment)))
     ;; Finally set the mode variable.
     (setq partial-completion-mode on-p)))
 
@@ -377,11 +389,12 @@
 	 (pred minibuffer-completion-predicate)
 	 (filename (funcall PC-completion-as-file-name-predicate))
 	 (dirname nil)
-	 dirlength
+	 (dirlength 0)
 	 (str (buffer-substring beg end))
 	 (incname (and filename (string-match "<\\([^\"<>]*\\)>?$" str)))
 	 (ambig nil)
 	 basestr
+	 env-on
 	 regex
 	 p offset
 	 (poss nil)
@@ -393,21 +406,19 @@
 	     (PC-is-complete-p str table pred))
 	'complete
 
-      ;; Record how many characters at the beginning are not included
-      ;; in completion.
-      (setq dirlength
-	    (if filename
-		(length (file-name-directory str))
-	      0))
-
       ;; Do substitutions in directory names
       (and filename
-	   (not (equal str (setq p (substitute-in-file-name str))))
-	   (progn
+           (setq basestr (or (file-name-directory str) ""))
+           (setq dirlength (length basestr))
+	   ;; Do substitutions in directory names
+           (setq p (substitute-in-file-name basestr))
+           (not (string-equal basestr p))
+           (setq str (concat p (file-name-nondirectory str)))
+           (progn
 	     (delete-region beg end)
-	     (insert p)
-	     (setq str p end (+ beg (length str)))))
-
+	     (insert str)
+	     (setq end (+ beg (length str)))))
+      
       ;; Prepare various delimiter strings
       (or (equal PC-word-delimiters PC-delims)
 	  (setq PC-delims PC-word-delimiters
@@ -484,6 +495,12 @@
       ;;(setq the-regex regex)
       (setq regex (concat "\\`" regex))
 
+      (and (> (length basestr) 0)
+           (= (aref basestr 0) ?$)
+           (setq env-on t
+                 table PC-env-vars-alist
+                 pred nil))
+
       ;; Find an initial list of possible completions
       (if (not (setq p (string-match (concat PC-delim-regex
 					     (if filename "\\|\\*" ""))
@@ -491,15 +508,18 @@
 				     (+ (length dirname) offset))))
 
 	  ;; Minibuffer contains no hyphens -- simple case!
-	  (setq poss (all-completions str
+	  (setq poss (all-completions (if env-on
+					  basestr str)
 				      table
 				      pred))
 
 	;; Use all-completions to do an initial cull.  This is a big win,
 	;; since all-completions is written in C!
-	(let ((compl (all-completions (substring str 0 p)
-				      table
-				      pred)))
+	(let ((compl (all-completions (if env-on
+					  (file-name-nondirectory (substring str 0 p))
+					(substring str 0 p))
+                                        table
+                                        pred)))
 	  (setq p compl)
 	  (while p
 	    (and (string-match regex (car p))
@@ -665,7 +685,8 @@
 
        ;; Only one possible completion
        (t
-	(if (equal basestr (car poss))
+	(if (and (equal basestr (car poss))
+		 (not (and env-on filename)))
 	    (if (null mode)
 		(PC-temp-minibuffer-message " [Sole completion]"))
 	  (delete-region beg end)
@@ -754,6 +775,21 @@
 	 (PC-not-minibuffer t))
     (PC-do-completion nil beg end)))
 
+(defun PC-complete-as-file-name ()
+   "Perform completion on file names preceding point.
+ Environment vars are converted to their values."
+   (interactive)
+   (let* ((end (point))
+          (beg (if (re-search-backward "[^\\][ \t\n\"\`\'][^ \t\n\"\`\']"
+				       (point-min) t)
+                   (+ (point) 2)
+                   (point-min)))
+          (minibuffer-completion-table 'read-file-name-internal)
+          (minibuffer-completion-predicate "")
+          (PC-not-minibuffer t))
+     (goto-char end)
+     (PC-do-completion nil beg end)))
+
 ;;; Use the shell to do globbing.
 ;;; This could now use file-expand-wildcards instead.