Function: ghostel--event-key-spec

ghostel--event-key-spec is a natively compiled function defined in ghostel.el.

Signature

(ghostel--event-key-spec EVENT &optional META)

Documentation

Decode EVENT into a (KEY-NAME . MOD-STRING) cons for the key encoder.

Non-nil META adds the meta modifier unless EVENT already carries it. Returns nil when EVENT has no encoder representation.

Source Code

;; Defined in /nix/store/1252krff0gb2kisjcpp45m50cpdskfrl-emacs-packages-deps/share/emacs/site-lisp/ghostel.el
(defun ghostel--event-key-spec (event &optional meta)
  "Decode EVENT into a (KEY-NAME . MOD-STRING) cons for the key encoder.
Non-nil META adds the meta modifier unless EVENT already carries it.
Returns nil when EVENT has no encoder representation."
  (let* ((base (event-basic-type event))
         (mods (event-modifiers event))
         (mods (if (and meta (not (memq 'meta mods)))
                   (cons 'meta mods)
                 mods))
         ;; Raw C0 bytes for RET/TAB/ESC are ambiguous with C-m/C-i/C-[
         ;; and Emacs decodes them as ctrl+letter.  Treat them as the
         ;; functional key and drop the artifact ctrl modifier so Enter
         ;; encodes as CR, not as a fixterms CSI-u sequence.
         (c0-key (and (memq event '(?\r ?\t ?\e))
                      (cdr (assq event '((?\r . "return")
                                         (?\t . "tab")
                                         (?\e . "escape"))))))
         (mods (if c0-key (remq 'control mods) mods))
         (key-name (cond
                    (c0-key c0-key)
                    ;; backtab is Emacs's name for S-TAB
                    ((eq base 'backtab) "tab")
                    ;; Terminal mode sends ASCII 127 for the backspace key
                    ((and (integerp base) (= base 127)) "backspace")
                    ;; Integer base (character key).  Uppercase chords
                    ;; arrive as lowercase base + shift; restore the case
                    ;; so the encoder emits the shifted character.
                    ((integerp base)
                     (and (< base 128)
                          (string (if (and (memq 'shift mods)
                                           (<= ?a base ?z))
                                      (upcase base)
                                    base))))
                    ((eq base 'deletechar) "delete")
                    ;; Normal function key symbol
                    ((and base (symbolp base)) (symbol-name base))
                    ;; Modified return/tab/backspace/escape: event-basic-type
                    ;; returns nil but modifiers are extracted correctly.
                    ;; Strip modifier prefixes from the symbol name.
                    ((and (null base) (symbolp event))
                     (replace-regexp-in-string
                      "\\`\\(?:[CMSHs]-\\)*" "" (symbol-name event)))
                    (t nil)))
         ;; backtab needs shift added back since it's baked into the name
         (mods (if (eq base 'backtab) (cons 'shift mods) mods))
         (mod-str (mapconcat
                   #'identity
                   (delq nil
                         (mapcar (lambda (m)
                                   (pcase m
                                     ('shift "shift") ('control "ctrl")
                                     ('meta "meta") ('alt "alt")
                                     ('hyper "hyper") ('super "super")))
                                 mods))
                   ",")))
    (when key-name (cons key-name mod-str))))