-(defun comint-dynamic-complete-variable ()
- "Dynamically complete the environment variable at point.
-This function is similar to `comint-dynamic-complete-filename', except that it
-searches `process-environment' for completion candidates. Note that this may
-not be the same as the interpreter's idea of variable names. The main
-problem with this type of completion is that `process-environment' is the
-environment which Emacs started with. Emacs does not track changes to the
-environment made by the interpreter. Perhaps it would be more accurate if this
-function was called `comint-dynamic-complete-process-environment-variable'.
-
-See also `comint-dynamic-complete-filename'."
- (interactive)
- (let* ((completion-ignore-case nil)
- (variable (comint-match-partial-variable))
- (varname (substring variable (string-match "[^$({]" variable)))
- (protection (cond ((string-match "{" variable) "}")
- ((string-match "(" variable) ")")
- (t "")))
- (variables (mapcar (function (lambda (x)
- (list (substring x 0 (string-match "=" x)))))
- process-environment))
- (var-directory-p
- (function (lambda (var)
- (file-directory-p
- (comint-directory (substitute-in-file-name (concat "$" var)))))))
- (completions (all-completions varname variables)))
- ;; Complete variable as if its value were a filename (which it might be).
- (cond ((null completions)
- (message "No completions of %s" varname)
- (ding))
- ((= 1 (length completions)) ; Gotcha!
- (let ((completion (car completions)))
- (if (string-equal completion varname)
- (message "Sole completion")
- (insert (substring (directory-file-name completion)
- (length varname)))
- (message "Completed"))
- (insert protection)
- (if comint-completion-addsuffix
- (insert (if (funcall var-directory-p completion) "/" " ")))))
- (t ; There's no unique completion.
- (let ((completion (try-completion varname variables)))
- ;; Insert the longest substring.
- (insert (substring (directory-file-name completion)
- (length varname)))
- (cond ((and comint-completion-recexact comint-completion-addsuffix
- (string-equal varname completion)
- (member completion completions))
- ;; It's not unique, but user wants shortest match.
- (insert protection
- (if (funcall var-directory-p completion) "/" " "))
- (message "Completed shortest"))
- ((or comint-completion-autolist
- (string-equal varname completion))
- ;; It's not unique, list possible completions.
- (comint-dynamic-list-completions completions))
- (t
- (message "Partially completed"))))))))
-
-