Function: shell--command-completion-data

shell--command-completion-data is a byte-compiled function defined in shell.el.gz.

Signature

(shell--command-completion-data)

Documentation

Return the completion data for the command at point.

Source Code

;; Defined in /usr/src/emacs/lisp/shell.el.gz
(defun shell--command-completion-data ()
  "Return the completion data for the command at point."
  (let* ((filename (or (comint-match-partial-filename) ""))
         (start (if (zerop (length filename)) (point) (match-beginning 0)))
         (end (if (zerop (length filename)) (point) (match-end 0)))
	 (filenondir (file-name-nondirectory filename))
	 (path-dirs
	  ;; Ignore `exec-directory', the last entry in `exec-path'.
          (append (cdr (reverse (exec-path)))
                  (if (and (memq system-type '(windows-nt ms-dos))
                           (not (file-remote-p default-directory)))
                      '("."))))
	 (cwd (file-name-as-directory (expand-file-name default-directory)))
	 (ignored-extensions
	  (and comint-completion-fignore
               (mapconcat (lambda (x) (concat (regexp-quote x) "\\'"))
			  comint-completion-fignore "\\|")))
	 (dir "") (comps-in-dir ())
	 (file "") (abs-file-name "") (completions ()))
    ;; Go thru each dir in the search path, finding completions.
    (while path-dirs
      (setq dir (file-name-as-directory (comint-directory (or (car path-dirs) ".")))
	    comps-in-dir (and (file-accessible-directory-p dir)
			      (file-name-all-completions filenondir dir)))
      ;; Go thru each completion found, to see whether it should be used.
      (while comps-in-dir
	(setq file (car comps-in-dir)
	      abs-file-name (concat dir file))
	(if (and (not (member file completions))
		 (not (and ignored-extensions
			   (string-match ignored-extensions file)))
		 (or (string-equal dir cwd)
		     (not (file-directory-p abs-file-name)))
		 (or (null shell-completion-execonly)
		     (file-executable-p abs-file-name)))
	    (setq completions (cons file completions)))
	(setq comps-in-dir (cdr comps-in-dir)))
      (setq path-dirs (cdr path-dirs)))
    ;; OK, we've got a list of completions.
    (list
     start end
     (lambda (string pred action)
       (if (string-search "/" string)
           (completion-file-name-table string pred action)
         (complete-with-action action completions string pred)))
     :exit-function
     (lambda (_string finished)
       (when (memq finished '(sole finished))
         (if (looking-at " ")
             (goto-char (match-end 0))
           (insert " ")))))))