Function: eca-chat--maybe-finalize-context-token

eca-chat--maybe-finalize-context-token is a natively compiled function defined in eca-chat-context.el.

Signature

(eca-chat--maybe-finalize-context-token)

Documentation

Turn a raw @path or #path token before point into a proper item.

Meant to be called right after a space was inserted. Does nothing when the previous word does not resolve to an existing file or directory.

Source Code

;; Defined in /nix/store/jklqzw6pzz6j4jrbv8wy0iwgi7jlrbld-emacs-packages-deps/share/emacs/site-lisp/elpa/eca-20260928.1827/eca-chat-context.el
(defun eca-chat--maybe-finalize-context-token ()
  "Turn a raw @path or #path token before point into a proper item.
Meant to be called right after a space was inserted.  Does
nothing when the previous word does not resolve to an existing
file or directory."
  (let* ((end (1- (point)))
         (start (save-excursion
                  (goto-char end)
                  (if (re-search-backward "[[:space:]]" (line-beginning-position) t)
                      (1+ (point))
                    (line-beginning-position)))))
    (when (< start end)
      (let ((word (buffer-substring-no-properties start end)))
        (when (and (> (length word) 1)
                   (or (string-prefix-p eca-chat-context-prefix word)
                       (string-prefix-p eca-chat-filepath-prefix word))
                   (not (get-text-property start 'eca-chat-item-type)))
          (when-let ((path (eca-chat--resolve-path-token (substring word 1))))
            (let ((context? (string-prefix-p eca-chat-context-prefix word)))
              (cond
               ;; On the context line: add to the context list; the
               ;; refresh wipes the typed text.
               ((and context? (eca-chat--point-at-new-context-p))
                (eca-chat--add-context
                 (list :type (if (f-dir? path) "directory" "file")
                       :path path)))
               ;; In the prompt: replace the raw token with an item.
               ((eca-chat--point-at-prompt-field-p)
                (let ((item-str (if context?
                                    (eca-chat--context->str
                                     (list :type (if (f-dir? path) "directory" "file")
                                           :path path)
                                     'static)
                                  (eca-chat--filepath->str path nil))))
                  (delete-region start (point))
                  (eca-chat--insert item-str)
                  (eca-chat--insert " ")))))))))))