Function: comint-dynamic-list-completions

comint-dynamic-list-completions is a byte-compiled function defined in comint.el.gz.

Signature

(comint-dynamic-list-completions COMPLETIONS &optional COMMON-SUBSTRING)

Documentation

Display a list of sorted COMPLETIONS.

Typing SPC flushes the completions buffer.

The optional argument COMMON-SUBSTRING, if non-nil, should be a string specifying a common substring for adding the faces completions-first-difference and completions-common-part to the completions.

Source Code

;; Defined in /usr/src/emacs/lisp/comint.el.gz
(defun comint-dynamic-list-completions (completions &optional common-substring)
  "Display a list of sorted COMPLETIONS.
Typing SPC flushes the completions buffer.

The optional argument COMMON-SUBSTRING, if non-nil, should be a string
specifying a common substring for adding the faces
`completions-first-difference' and `completions-common-part' to
the completions."
  (let ((window (get-buffer-window "*Completions*" 0)))
    (setq completions (sort completions 'string-lessp))
    (if (and (eq last-command this-command)
	     window (window-live-p window) (window-buffer window)
	     (buffer-name (window-buffer window))
	     ;; The above tests are not sufficient to detect the case where we
	     ;; should scroll, because the top-level interactive command may
	     ;; not have displayed a completions window the last time it was
	     ;; invoked, and there may be such a window left over from a
	     ;; previous completion command with a different set of
	     ;; completions.  To detect that case, we also test that the set
	     ;; of displayed completions is in fact the same as the previously
	     ;; displayed set.
	     (equal completions
		    (buffer-local-value 'comint-displayed-dynamic-completions
					(window-buffer window))))
	;; If this command was repeated, and
	;; there's a fresh completion window with a live buffer,
	;; and this command is repeated, scroll that window.
	(with-current-buffer (window-buffer window)
	  (if (pos-visible-in-window-p (point-max) window)
	      (set-window-start window (point-min))
	    (save-selected-window
	      (select-window window)
	      (scroll-up))))

      ;; Display a completion list for the first time.
      (setq comint-dynamic-list-completions-config
	    (current-window-configuration))
      (with-output-to-temp-buffer "*Completions*"
        (display-completion-list
         (completion-hilit-commonality completions (length common-substring))))
      (if (window-minibuffer-p)
	  (minibuffer-message "Type space to flush; repeat completion command to scroll")
	(message "Type space to flush; repeat completion command to scroll")))

    ;; Read the next key, to process SPC.
    (let (key first)
      (if (with-current-buffer "*Completions*"
	    (setq-local comint-displayed-dynamic-completions
                        completions)
	    (setq key (read-key-sequence nil)
		  first (aref key 0))
	    (and (consp first) (consp (event-start first))
		 (eq (window-buffer (posn-window (event-start first)))
		     (get-buffer "*Completions*"))
		 (memq (key-binding key)
                       '(choose-completion))))
	  ;; If the user does choose-completion with the mouse,
	  ;; execute the command, then delete the completion window.
	  (progn
	    (choose-completion first)
	    (set-window-configuration comint-dynamic-list-completions-config))
	(if (eq first ?\s)
	    (set-window-configuration comint-dynamic-list-completions-config)
	  (setq unread-command-events
                (nconc (listify-key-sequence key) unread-command-events)))))))