Function: completion--flex-adjust-metadata

completion--flex-adjust-metadata is a byte-compiled function defined in minibuffer.el.gz.

Signature

(completion--flex-adjust-metadata METADATA)

Documentation

If flex is actually doing filtering, adjust sorting.

Source Code

;; Defined in /usr/src/emacs/lisp/minibuffer.el.gz
(defun completion--flex-adjust-metadata (metadata)
  "If `flex' is actually doing filtering, adjust sorting."
  (let ((flex-is-filtering-p completion-pcm--regexp)
        (existing-dsf
         (completion-metadata-get metadata 'display-sort-function))
        (existing-csf
         (completion-metadata-get metadata 'cycle-sort-function)))
    (cl-flet
        ((compose-flex-sort-fn (existing-sort-fn)
           (lambda (completions)
             (let* ((sorted (sort
                             (mapcar
                              (lambda (str)
                                (cons
                                 (- (completion--flex-score
                                     (or (get-text-property
                                          0 'completion--unquoted str)
                                         str)
                                     completion-pcm--regexp))
                                 str))
                              (if existing-sort-fn
                                  (funcall existing-sort-fn completions)
                                completions))
                             #'car-less-than-car))
                    (cell sorted))
               ;; Reuse the list
               (while cell
                 (setcar cell (cdar cell))
                 (pop cell))
               sorted))))
      `(metadata
        ,@(and flex-is-filtering-p
               `((display-sort-function . ,(compose-flex-sort-fn existing-dsf))))
        ,@(and flex-is-filtering-p
               `((cycle-sort-function . ,(compose-flex-sort-fn existing-csf))))
        ,@(cdr metadata)))))