diff options
| -rw-r--r-- | lisp/xterm-kitty.el | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/lisp/xterm-kitty.el b/lisp/xterm-kitty.el index 809bff3..906c032 100644 --- a/lisp/xterm-kitty.el +++ b/lisp/xterm-kitty.el @@ -29,6 +29,8 @@ ;;; Code: +(require 'term/xterm) + (defvar xterm-kitty-in-use (and (string-match "^xterm-kitty" (getenv-internal "TERM" initial-environment)) t) "Currently running under xterm-kitty.") @@ -44,13 +46,6 @@ would swap meta and super.") (defvar xterm-kitty-escape-prefix "\e[" "CSI escape sequence generated by kitty.") -(defvar xterm-kitty--prefix-alist - '((shift . "S-") (alt . "A-") (control . "C-") (super . "s-") (hyper . "H-") (meta . "M-")) - "Modifier prefixes.") -(defvar xterm-kitty--bitset-alist - `((shift . ,(ash 1 25)) (alt . ,(ash 1 22)) (control . ,(ash 1 26)) (super . ,(ash 1 23)) (hyper . ,(ash 1 24)) (meta . ,(ash 1 27))) - "Modifier bits set.") - (defvar xterm-kitty-shift-alist `(,@(mapcar (lambda (p) (cons (aref p 0) (aref p 1))) (split-string "`~ 1! 2@ 3# 4$ 5% 6^ 7& 8* 9( 0) -_ =+ [{ ]} \\| ;: '\" ,< .> /?")) @@ -58,6 +53,20 @@ would swap meta and super.") (number-sequence ?a ?z))) "Characters produced by shifted keys; used to convert shifted keybindings.") +;; ------------------------------------------------------------------------------------ +;; Implementation + +(defconst xterm-kitty--prefix-alist + '((shift . "S-") (alt . "A-") (control . "C-") (super . "s-") (hyper . "H-") (meta . "M-")) + "Modifier prefixes.") +(defconst xterm-kitty--bitset-alist + (mapcar (lambda (a) (cons (car a) (ash 1 (cdr a)))) + '((shift . 25) (alt . 22) (control . 26) (super . 23) (hyper . 24) (meta . 27))) + "Modifier bits set.") +(defconst xterm-kitty--modifier-combinations + (number-sequence 0 (1- (ash 1 (length xterm-kitty-modifiers-alist)))) + "Numerical representation of all combinations") + (defun xterm-kitty--make-modifiers-from-num (num &rest others) "Make a list of modifiers from NUM along with additional modifiers OTHERS." (let* ((bits (mapcar (lambda (idx) (logand num (ash 1 idx))) (number-sequence 0 (1- (length xterm-kitty-modifiers-alist))))) @@ -68,16 +77,16 @@ would swap meta and super.") (apply folder (mapcar (lambda (mod) (alist-get mod list-map)) (xterm-kitty--make-modifiers-from-num num)))) -(defvar xterm-kitty--numeric-modifiers +(defconst xterm-kitty--numeric-modifiers (apply #'vector (mapcar (lambda (num) (xterm-kitty--from-numeric-modifer num xterm-kitty--bitset-alist #'logior)) - (number-sequence 0 (1- (ash 1 (length xterm-kitty-modifiers-alist)))))) + xterm-kitty--modifier-combinations)) "Numeric modifiers to apply to each printable character for kitty modifier.") -(defvar xterm-kitty--prefix-modifiers +(defconst xterm-kitty--prefix-modifiers (apply #'vector (mapcar (lambda (num) (if (> num 0) (xterm-kitty--from-numeric-modifer num xterm-kitty--prefix-alist #'concat) "")) - (number-sequence 0 (1- (ash 1 (length xterm-kitty-modifiers-alist)))))) + xterm-kitty--modifier-combinations)) "Symbolic prefix to apply to each printable character for kitty modifier.") ;; (message (apply #'concat (mapcar (lambda (p) (format "%x " (ash p -22))) (sort xterm-kitty--numeric-modifiers '<)))) @@ -151,7 +160,7 @@ would swap meta and super.") (defun xterm-kitty--insert-decode-table (keymap) "Insert decoding table into KEYMAP" - (let* ((all-modifiers (number-sequence 0 (1- (ash 1 (length xterm-kitty-modifiers-alist))))) + (let* ((all-modifiers xterm-kitty--modifier-combinations) (all-mod-suffixes (apply #'vector (mapcar #'xterm-kitty--make-suffix all-modifiers)))) (mapc (lambda (key) @@ -194,7 +203,7 @@ would swap meta and super.") ;; (xterm-kitty--add-modifier-list '(control) ?i) (defun xterm-kitty-decode-key-stroke (keycode modifiers suffix) - "Take KEYCODE MODIFIERS SUFFIX of the form 105;5u and construct key." + "Take KEYCODE MODIFIERS SUFFIX of the form (105,5,u) and construct key." ;; Ignore modifiers that we cannot understand (CapsLock and NumLock status) (let ((mods (logand modifiers (1- (length xterm-kitty--numeric-modifiers))))) (if (eql suffix ?u) @@ -221,6 +230,7 @@ would swap meta and super.") (aref (aref xterm-kitty--suffix-alpha-precomputed (- suffix ?A)) mods))))))) (defun xterm-kitty-handle-non-printable (keystr) + "Split kitty non-printable keystring KEYSTR (e.g., 105;5u) and construct key" (let* ((suffix (aref keystr (1- (length keystr)))) (parts (split-string keystr ";")) (num-parts (length parts)) @@ -229,12 +239,13 @@ would swap meta and super.") ;; (send-string-to-terminal (format "%s %s %s" keystr code modifiers)) (xterm-kitty-decode-key-stroke code modifiers suffix))) +;; The following function is semi-obsolete, and is intended to be used solely for testing. (defun xterm-kitty--handle-escape-code1 (prompt) "Handle escape code by reading rest of keycode as string; PROMPT is ignored." (let* ((e (read-char)) (complete-string (string e)) (count 0)) - ;; There must be a faster way to create this string one character at a time + ;; There must be a faster way to create this string than one character at a time (while (and (or (<= ?0 e ?9) (eql e ?\;)) (< count 25)) @@ -246,7 +257,7 @@ would swap meta and super.") (fset 'xterm-kitty--original-read-char-exclusive (symbol-function 'read-char-exclusive)) (defun xterm-kitty--handle-escape-code (prompt) - "Try to optimize handling keycode using integer math; PROMPT is ignored." + "Handle keycode using integer math; PROMPT is ignored." (let ((keycode 0) (modifiers 0) (suffix nil) |
