Function: add-completion-to-head

add-completion-to-head is a byte-compiled function defined in completion.el.gz.

Signature

(add-completion-to-head COMPLETION-STRING)

Documentation

If COMPLETION-STRING is not in the database, add it to prefix list.

We add COMPLETION-STRING to the head of the appropriate prefix list, or to the head of the list. COMPLETION-STRING must be longer than completion-prefix-min-length. Updates the saved string with the supplied string. This must be very fast. Returns the completion entry.

Source Code

;; Defined in /usr/src/emacs/lisp/completion.el.gz
(defun add-completion-to-head (completion-string)
  "If COMPLETION-STRING is not in the database, add it to prefix list.
We add COMPLETION-STRING to the head of the appropriate prefix list,
or to the head of the list.
COMPLETION-STRING must be longer than `completion-prefix-min-length'.
Updates the saved string with the supplied string.
This must be very fast.
Returns the completion entry."
  (let ((cmpl--completion-string completion-string))
    ;; Handle pending acceptance
    (if completion-to-accept (accept-completion))
    ;; test if already in database
    (if (setq cmpl-db-entry (find-exact-completion completion-string))
        ;; found
        (let* ((prefix-entry (find-cmpl-prefix-entry
                              (substring cmpl-db-downcase-string 0
                                         completion-prefix-min-length)))
               (splice-ptr (completion-locate-entry cmpl-db-entry prefix-entry))
               (cmpl-ptr (cdr splice-ptr)))
          ;; update entry
          (set-completion-string cmpl-db-entry completion-string)
          ;; move to head (if necessary)
          (cond (splice-ptr
                 ;; These should all execute atomically but it is not fatal if
                 ;; they don't.
                 ;; splice it out
                 (or (setcdr splice-ptr (cdr cmpl-ptr))
                     ;; fix up tail if necessary
                     (set-cmpl-prefix-entry-tail prefix-entry splice-ptr))
                 ;; splice in at head
                 (setcdr cmpl-ptr (cmpl-prefix-entry-head prefix-entry))
                 (set-cmpl-prefix-entry-head prefix-entry cmpl-ptr)))
          cmpl-db-entry)
      ;; not there
      (let ( ;; create an entry
            (entry (list (make-completion completion-string)))
            ;; setup the prefix
            (prefix-entry (find-cmpl-prefix-entry
                           (substring cmpl-db-downcase-string 0
                                      completion-prefix-min-length))))
        (cond (prefix-entry
               ;; Splice in at head
               (setcdr entry (cmpl-prefix-entry-head prefix-entry))
               (set-cmpl-prefix-entry-head prefix-entry entry))
              (t
               ;; Start new prefix entry
               (set cmpl-db-prefix-symbol (make-cmpl-prefix-entry entry))))
        ;; Add it to the symbol
        (set cmpl-db-symbol (car entry))))))