Function: cape--dynamic-table

cape--dynamic-table is a byte-compiled function defined in cape.el.

Signature

(cape--dynamic-table BEG END FUN)

Documentation

Create dynamic completion table from FUN with caching.

BEG and END are the input bounds. FUN is the function which computes the candidates. FUN must return a pair of a predicate function function and the list of candidates. The predicate is passed new input and must return non-nil if the candidates are still valid.

It is only necessary to use this function if the set of candidates is computed dynamically based on the input and not statically determined. The behavior is similar but slightly different to completion-table-dynamic.

The difference to the builtins completion-table-dynamic and completion-table-with-cache is that this function does not use the prefix argument of the completion table to compute the candidates. Instead it uses the input in the buffer between BEG and END to FUN to compute the candidates. This way the dynamic candidate computation is compatible with non-prefix completion styles like substring or orderless, which pass the empty string as first argument to the completion table.

Source Code

;; Defined in ~/.emacs.d/elpa/cape-20260804.2303/cape.el
(defun cape--dynamic-table (beg end fun)
  "Create dynamic completion table from FUN with caching.
BEG and END are the input bounds.  FUN is the function which
computes the candidates.  FUN must return a pair of a predicate
function function and the list of candidates.  The predicate is
passed new input and must return non-nil if the candidates are
still valid.

It is only necessary to use this function if the set of
candidates is computed dynamically based on the input and not
statically determined.  The behavior is similar but slightly
different to `completion-table-dynamic'.

The difference to the builtins `completion-table-dynamic' and
`completion-table-with-cache' is that this function does not use
the prefix argument of the completion table to compute the
candidates.  Instead it uses the input in the buffer between BEG
and END to FUN to compute the candidates.  This way the dynamic
candidate computation is compatible with non-prefix completion
styles like `substring' or `orderless', which pass the empty
string as first argument to the completion table."
  (let ((beg (copy-marker beg))
        (end (copy-marker end t))
        valid table)
    (lambda (str pred action)
      ;; Bail out early for `metadata' and `boundaries'. This is a pointless
      ;; move because of caching, but we do it anyway in the hope that the
      ;; profiler report looks less confusing, since the weight of the expensive
      ;; FUN computation is moved to the `all-completions' action.  Computing
      ;; `all-completions' must surely be most expensive, so nobody will suspect
      ;; a thing.
      (unless (or (eq action 'metadata) (eq (car-safe action) 'boundaries))
        (let ((input (buffer-substring-no-properties beg end)))
          (unless (and valid
                       (or (cape--separator-p input)
                           (funcall valid input)))
            (let* (;; Reset in case `all-completions' is used inside FUN
                   completion-ignore-case completion-regexp-list
                   ;; Retrieve new state by calling FUN
                   (new (and (< beg end) (funcall fun input)))
                   ;; No interrupt during state update
                   throw-on-input)
              (setq valid (car new) table (cdr new)))))
        (complete-with-action action table str pred)))))