Function: kkp--terminal-setup
kkp--terminal-setup is a natively compiled function defined in kkp.el.
Signature
(kkp--terminal-setup)
Documentation
Run setup to enable KKP support in current terminal.
This does not work well if checking for another terminal which does not have focus, as input from this terminal cannot be reliably read.
Source Code
;; Defined in /nix/store/0igl9q4k4n87vg4h2gnnrlha21kx9biv-emacs-packages-deps/share/emacs/site-lisp/elpa/kkp-20260706.858/kkp.el
(defun kkp--terminal-setup ()
"Run setup to enable KKP support in current terminal.
This does not work well if checking for another terminal which
does not have focus, as input from this terminal cannot be reliably read."
(let ((terminal-input "")
(terminal (kkp--selected-terminal))
chr)
(while (and (setq chr (read-event nil nil kkp-terminal-query-timeout)) (not (equal chr ?c)))
(setq terminal-input (concat terminal-input (string chr))))
;; clear the setup-started flag as soon as possible
;; to enable another try if somehow the string-match-p evaluates to nil
(setf (kkp--state-setup-started (kkp--ensure-state terminal)) nil)
;; Condition: CSI?<flags>u CSI?...c must be in response
;; CSI? is already in response as it was registered as handler for the async request
;; thus it is not in terminal-input.
(if (string-match-p (rx line-start
(+ digit) ;; <flags>
"u\e[?"
(+ anychar) ;; primary device attributes
eol) terminal-input)
(progn
(kkp--verbose "setup response matched (terminal supports KKP)")
(unless (kkp--active-p terminal)
(let ((enhancement-flag (kkp--calculate-flags-integer)))
(if (eq enhancement-flag 0)
(kkp--verbose "no enhancements to enable (flag=0); skipping")
(kkp--verbose "enabling KKP: sending >%su, setting keymaps" enhancement-flag)
(send-string-to-terminal (kkp--csi-escape (format ">%su" enhancement-flag)) terminal)
;; Record the flags the terminal actually enabled, not those we
;; requested: some terminals (e.g. zellij) implement only a
;; subset of the enhancements and enable fewer flags than asked.
(setf (kkp--state-enhancements (kkp--ensure-state terminal))
(kkp--reply-flags (kkp--query-terminal-sync "?u" ?u)))
(kkp-setup-function-keys terminal)
(setf (kkp--state-previous-normal-erase (kkp--ensure-state terminal)) (terminal-parameter terminal 'normal-erase-is-backspace))
(dolist (frame (frames-on-display-list terminal))
(with-selected-frame frame
(normal-erase-is-backspace-mode 1)
;; we register functions for each prefix to not interfere with e.g., M-[ I
(dolist (prefix kkp--key-prefixes)
(define-key input-decode-map (kkp--csi-escape (string prefix))
(lambda (_prompt) (kkp--process-keys prefix))))
(run-hooks 'kkp-terminal-setup-complete-hook)))
(kkp--verbose "setup complete; enhancements active")))))
(kkp--verbose "setup response did not match (slow SSH? non-Kitty?); %s"
(kkp--format-bytes-and-readable terminal-input)))))