Function: completion--nth-completion

completion--nth-completion is a byte-compiled function defined in minibuffer.el.gz.

Signature

(completion--nth-completion N STRING TABLE PRED POINT METADATA)

Documentation

Call the Nth method of completion styles.

Source Code

;; Defined in /usr/src/emacs/lisp/minibuffer.el.gz
(defun completion--nth-completion (n string table pred point metadata)
  "Call the Nth method of completion styles."
  ;; We provide special support for quoting/unquoting here because it cannot
  ;; reliably be done within the normal completion-table routines: Completion
  ;; styles such as `substring' or `partial-completion' need to match the
  ;; output of all-completions with the user's input, and since most/all
  ;; quoting mechanisms allow several equivalent quoted forms, the
  ;; completion-style can't do this matching (e.g. `substring' doesn't know
  ;; that "\a\b\e" is a valid (quoted) substring of "label").
  ;; The quote/unquote function needs to come from the completion table (rather
  ;; than from completion-extra-properties) because it may apply only to some
  ;; part of the string (e.g. substitute-in-file-name).
  (let* ((md (or metadata
                 (completion-metadata (substring string 0 point) table pred)))
         (requote
          (when (and
                 (completion-metadata-get md 'completion--unquote-requote)
                 ;; Sometimes a table's metadata is used on another
                 ;; table (typically that other table is just a list taken
                 ;; from the output of `all-completions' or something
                 ;; equivalent, for progressive refinement).
                 ;; See bug#28898 and bug#16274.
                 ;; FIXME: Rather than do nothing, we should somehow call
                 ;; the original table, in that case!
                 (functionp table))
            (let ((new (funcall table string point 'completion--unquote)))
              (setq string (pop new))
              (setq table (pop new))
              (setq point (pop new))
              (cl-assert (<= point (length string)))
              (pop new))))
         (result-and-style
          (seq-some
           (lambda (style)
             (let (symbols values)
               (when (consp style)
                 (dolist (binding (cadr style))
                   (push (car binding) symbols)
                   (push (cadr binding) values))
                 (setq style (car style)))
               (cl-progv symbols values
                 (let ((probe (funcall
                               (or (nth n (assq style completion-styles-alist))
                                   (error "Invalid completion style %s" style))
                               string table pred point)))
                   (and probe (cons probe style))))))
           (completion--styles md)))
         (adjust-fn (get (cdr result-and-style) 'completion--adjust-metadata))
         (adjusted (completion-metadata-get
                    metadata 'completion--adjusted-metadata)))
    (when (and adjust-fn metadata
               ;; Avoid re-applying the same adjustment (bug#74718).
               (not (memq (cdr result-and-style) adjusted)))
      (setcdr metadata `((completion--adjusted-metadata
                          ,(cdr result-and-style) . ,adjusted)
                         . ,(cdr (funcall adjust-fn metadata)))))
    (if requote
        (funcall requote (car result-and-style) n)
      (car result-and-style))))