benjaminor / kkp

Emacs support for the Kitty Keyboard Protocol
GNU General Public License v3.0
39 stars 6 forks source link

Can not enable kkp enhancement in emacs running in ssh terminal mode #9

Open zhishi opened 1 year ago

zhishi commented 1 year ago

Hi,

I'm running emacs in a remote server, the emacs is running as daemon server, and I use wezterm terminal with ssh to login the server, then use emacsclient to start a new frame. the wezterm already set "config.enable_kitty_keyboard = true", but when I run the command kkp-status in the emacs, I got below output: KKP supported in this terminal. KKP not active in this terminal.

So looks like wezterm is recognized as support kpp, but I can't enable emacs use the enhancement features. I wonder what could be wrong here? the config I used to enable kpp: (use-package kkp :ensure t :config (global-kkp-mode +1)))

zhishi commented 1 year ago

Btw, I don't have enough knowledge about elisp details, but when I try to execute the partial code in (defun kkp--terminal-setup (), it actually works now. the part I tried run is from here:

     (let ((terminal (kkp--selected-terminal)))
        (set-terminal-parameter terminal 'kkp--setup-started nil)
        (unless (member terminal kkp--active-terminal-list)
          (let ((enhancement-flag (kkp--calculate-flags-integer)))
            (unless (eq enhancement-flag 0)

              (send-string-to-terminal (kkp--csi-escape (format ">%su" enhancement-flag)) terminal)

              (push terminal kkp--active-terminal-list)
              (set-terminal-parameter terminal 'kkp--previous-normal-erase-is-backspace-val (terminal-parameter terminal 'normal-erase-is-backspace))
              (normal-erase-is-backspace-mode 1)

              ;; we register functions for each prefix to not interfere with e.g., M-[ I
              (with-selected-frame (car (frames-on-display-list terminal))
                (dolist (prefix kkp--key-prefixes)
                  (define-key input-decode-map (kkp--csi-escape (string prefix))
                              (lambda (_prompt) (kkp--process-keys prefix)))))))

So I guess there is some condition checking logic failed before that line for some reason.

benjaminor commented 1 year ago

Hi, thanks for opening the issue! Which versions of Emacs and wezterm are you running so I can try to reproduce it?

zhishi commented 1 year ago

the emacs version is 29.1.50 (I built from 29.1 branch). the wezterm version is WezTerm-windows-20230712-072601-f4abf8fd. I'm not sure if it's easy to repro, since the emacs server need run as daemon. but I can also repro it in local mode. which maybe easier for you debug: the wezterm still same version, and I directly run emacs 29.1 in wsl, emacs -nw

and after startup it always print 0u after current cursor position, image

then I run kpp-status got this result: KKP supported in this terminal. KKP not active in this terminal.

benjaminor commented 11 months ago

I suspect this might by a timing issue. To test this theory, please increase the value of kkp-terminal-query-timeout to a larger number, perhaps 1 or even more, and then see if this resolves the problem.

In my own tests, I managed to occasionally replicate your kkp-status output by setting kkp-terminal-query-timeout to an very low value, though this wasn't consistent.

zhishi commented 11 months ago

I tried increase it to 2 seconds, still same issue, and when I run kkp-status it indeed waited 2 seconds to show the result, but still: image

and when emacs started it still print that 0u on cursor position.

benjaminor commented 9 months ago

I'm sorry, I tried but I still cannot reproduce your issue. Could you maybe once again detail the steps you use to reproduce it? For example:

  1. start emacs in WSL with "emacs -nw"
  2. start wezterm in WSL
  3. ssh onto localhost with wezterm...
  4. etc.

And could you maybe report back what (kkp--query-terminal-sync "c") returns when you evaluate it inside Emacs in a terminal?

Hajitorus commented 8 months ago

I've got what seems to be a similar issue. I'm running Wezterm in Windows with Kitty keyboard enabled. I've narrowed it down to:

  1. ssh to Linux host in wezterm (which is configured with only config = wezterm.config_builder(); config.enable_kitty_keyboard = true)
  2. run emacs -Q -nw kkp.el
  3. (load-file "kkp.el")
  4. M-x kkp-status confirms kkp supported, not active
  5. M-x kkp-enable-in-terminal
  6. 0u is inserted at point, and (at least) \e[? is interpreted by emacs because it brings up a list of input map translations starting with M-[
  7. This behavior happens regardless of kkp-terminal-query-timeout.

My Emacs replies "GNU Emacs 29.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.37, cairo version 1.16.0)" to M-x version. Wezterm says it is "wezterm 20240203-110809-5046fc22".

Thanks for your attention to this issue already. I'm going to try to keep digging to see if I can resolve this myself and will let you know if I get closure.

If it helps at all, setting kkp-terminal-query-timeout to something high like 5 also shows that kkp-enable-in-terminal performs its steps instantly (inserting 0u, opening the help window) but kkp-status returns only after the timeout.

(kkp--query-terminal-sync "c") returns (27 91 63 54 49 59 54 59 55 59 50 50 ...) which my rusty ascii skills broke down to CSI c ? 6 1 ; 6 ; 7 ; 2 2.

benjaminor commented 8 months ago

Thank you @Hajitorus for your help! I can reproduce the issue now reliably and will investigate further.

benjaminor commented 8 months ago

Hi, @Hajitorus @zhishi , could you maybe check if branch https://github.com/benjaminor/kkp/tree/fix-issue9 fixes the issue with your setup? I have introduced a workaround which solves the issue for me, but would like to confirm it also helps you.

zhishi commented 8 months ago

doesn't work for me, maybe because I load lots of other packages? but thanks for continuing on investigation and fix.

ark3 commented 8 months ago

I'm seeing the same issue. Your fix in your branch does not make a difference for me, nor does increasing the timeout. As with @zhishi , I can enable kkp by skipping this test in kkp--terminal-setup:

          (string-match-p (rx line-start
                              (+ digit) ;; <flags>
                              "u\e[?"
                              (+ anychar)
                              eol) terminal-input)

In other words, if I delete or short-circuit that test, allowing the rest of the code in that function to run, then kkp mode is enabled and appears to work.

Edit to add: I'm using Alacritty 0.13.2, but I'd happily switch to WezTerm if we could get this to work cleanly there. GNU Emacs 29.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.16.0)

jzxu commented 7 months ago

I'm experiencing the same issue, and the work-around proposed by @zhishi and @ark3 works for me as well. I'm also using Alacritty 0.13.2.

DCsunset commented 6 months ago

I'm using Alacritty 0.13.2 as well, but the workaround posted above didn't work for me. I'm using emacs 29.3 and NixOS unstable.

Update: it actually works by following @ark3's workaround. The key is that I need to run kkp--terminal-setup directly instead of kkp-enable-in-terminal to make it work.

amosbird commented 5 months ago

I can reproduce the same behavior when kitty keyboard extension is already activated before running emacs.

printf "\e[>5u"
emacs