Function: cape-company-to-capf

cape-company-to-capf is an autoloaded and byte-compiled function defined in cape.el.

Signature

(cape-company-to-capf BACKEND &optional VALID)

Documentation

Convert Company BACKEND function to Capf.

VALID is a function taking the old and new input string. It should return nil if the cached candidates became invalid. The default value for VALID is string-prefix-p such that the candidates are only fetched again if the input prefix changed.

Source Code

;; Defined in ~/.emacs.d/elpa/cape-20260804.2303/cape.el
;;;###autoload
(defun cape-company-to-capf (backend &optional valid)
  "Convert Company BACKEND function to Capf.
VALID is a function taking the old and new input string.  It should
return nil if the cached candidates became invalid.  The default value
for VALID is `string-prefix-p' such that the candidates are only fetched
again if the input prefix changed."
  (lambda ()
    (when (and (symbolp backend) (not (fboundp backend)))
      (ignore-errors (require backend nil t)))
    (when (bound-and-true-p company-mode)
      (error "`cape-company-to-capf' should not be used with `company-mode', use the Company backend directly instead"))
    (when (and (symbolp backend) (not (alist-get backend cape--company-init)))
      (funcall backend 'init)
      (put backend 'company-init t)
      (setf (alist-get backend cape--company-init) t))
    (when-let* ((pre (pcase (cape--company-call backend 'prefix)
                       ((or `(,p ,_s) (and (pred stringp) p)) (cons p (length p)))
                       ((or `(,p ,_s ,l) `(,p . ,l)) (cons p l)))))
      (let* ((end (point)) (beg (- end (length (car pre))))
             (valid (if (cape--company-call backend 'no-cache (car pre))
                        #'equal (or valid #'string-prefix-p)))
             (sort-fun (and (cape--company-call backend 'sorted) #'identity))
             restore-props)
        (list beg end
              (funcall
               (if (cape--company-call backend 'ignore-case)
                   #'completion-table-case-fold
                 #'identity)
               (cape--dynamic-table
                beg end
                (lambda (input)
                  (let ((cands (cape--company-call backend 'candidates input)))
                    ;; The candidate string including text properties should be
                    ;; restored in the :exit-function, unless the UI guarantees
                    ;; this itself, like Corfu.
                    (unless (bound-and-true-p corfu-mode)
                      (setq restore-props cands))
                    (cons (apply-partially valid input) cands)))))
              :category backend
              :exclusive 'no
              :company-prefix-length (cdr pre)
              :company-doc-buffer (lambda (x) (cape--company-call backend 'doc-buffer x))
              :company-location (lambda (x) (cape--company-call backend 'location x))
              :company-docsig (lambda (x) (cape--company-call backend 'meta x))
              :company-deprecated (lambda (x) (cape--company-call backend 'deprecated x))
              :company-kind (lambda (x) (cape--company-call backend 'kind x))
              :display-sort-function sort-fun
              :cycle-sort-function sort-fun
              :annotation-function (lambda (x)
                                     (when-let* ((ann (cape--company-call backend 'annotation x)))
                                       (concat " " (string-trim ann))))
              :exit-function (lambda (x _status)
                               ;; Restore the candidate string including
                               ;; properties if restore-props is non-nil.  See
                               ;; the comment above.
                               (setq x (or (car (member x restore-props)) x))
                               (cape--company-call backend 'post-completion x)))))))