Skip to content

Some terminals need keypad mode (smkx) #2139

Closed

Description

@pickfire

Hi,
Fish shell (friendly interactive shell) is design to be friendly. st required to do something in some programs in order to use delete. Look http://git.suckless.org/st/tree/FAQ#n32 for more info. I hope fish shell can avoid these issues to become friendly to the users and prevent unneeded configuration.

Activity

  1. zanchey commented on Jun 18, 2015

    @zanchey
    Member

    I'm struggling to understand. Can you describe what the difference between you are seeing and what you expect is?

  2. pickfire commented on Jun 21, 2015

    @pickfire
    ContributorAuthor

    Without tput smkx or printf '\033[?1h\033=' >/dev/tty in config, when you press <DELETE> in st:

    Welcome to fish, the friendly interactive shell
    Type help for instructions on how to use fish
    ➜  ~ [P
    
  3. zanchey commented on Jul 1, 2015

    @zanchey
    Member

    Useful reading:

    AIUI:

    • the terminal keyboard can be in keypad_local/normal/raw/non-transmit mode or keypad_xmit/application/transmit mode (all of these names are used)
    • by default the terminal assumes that the keypad is in "normal" mode
    • in "normal" mode stterm does not send any codes for the keypad delete key, sends an ASCII DEL for the standard delete key, and sends ESC [ D for the keypad left and standard left keys
    • in "application" mode stterm does send the delete key code listed in its termcap under kdch for both the keypad and the standard delete key, and sends ESC O t from the keypad left and ESC O D from the standard left key

    This is different to, say, gnome-terminal, which always sends the key code listed under kdch for the standard delete and keypad delete keys regardless of keypad mode. Setting "application mode" produces ESC O D from both left keys and "normal mode" sends ESC [ D for both left keys.

    I'm not terribly convinced that stterm actually does sensible things here but I think it would be reasonable to do the same thing that ZSH is configured to do under Debian: send smkx before entering the prompt and rmkx when leaving. Adding the extra bindings that stterm sends is an additional exercise.

  4. pickfire commented on Jul 1, 2015

    @pickfire
    ContributorAuthor

    I don't understand what you mean? Do you mean to set smkx manually in fish.config?

  5. zanchey commented on Jul 2, 2015

    @zanchey
    Member

    No, to echo the smkx sequence when accepting input and rmkx when leaving that state.

  6. added this to the milestone on Jul 2, 2015
  7. pickfire commented on Jul 2, 2015

    @pickfire
    ContributorAuthor

    On Wed, Jul 01, 2015 at 11:40:35PM -0700, David Adam wrote:

    No, to echo the smkx sequence when going accepting input and rmkx when leaving that state.

    In fish? Manually?


    < Do what you like, like what you do. >


        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
    
  8. zanchey commented on Jul 2, 2015

    @zanchey
    Member

    In fish.

  9. pickfire commented on Jul 2, 2015

    @pickfire
    ContributorAuthor

    Manually?

  10. pickfire commented on Sep 1, 2015

    @pickfire
    ContributorAuthor

    Do you think I need to patch st for the smkx?

  11. faho commented on Sep 1, 2015

    @faho
    Member

    Putting this in config.fish should solve the issue:

    switch $TERM
        case 'st-*' # suckless' simple terminal
                    # Enable keypad, do it once before fish_postexec ever fires
            tput smkx
            function st_smkx --on-event fish_postexec
                tput smkx
            end
            function st_rmkx --on-event fish_preexec
                tput rmkx
            end
    end

    Should we ship this by default, e.g. in __fish_config_interactive?

    Do you think I need to patch st for the smkx?

    That would work too, though I don't think st upstream would take it.

  12. pickfire commented on Sep 1, 2015

    @pickfire
    ContributorAuthor

    I said that k doesn't work, #2309 (comment) talked about this.

    That would work too, though I don't think st upstream would take it.

    I thought about that too, but we could still add that to http://st.suckless.org/patches/ first and see if they want to take that.

    The patch you give works like a charm. I hope you can add it to fish.

    In a nutshell, the problem is with smkx or delete?

  13. faho commented on Sep 1, 2015

    @faho
    Member

    I said that k doesn't work, #2309 (comment) talked about this.

    There you said it didn't work in bash, and then you later managed to do it anyway.

    We could still add that to http://st.suckless.org/patches/ first and see if they want to take that.

    It's a major change so I don't think they'd appreciate it. But if you want, go ahead.

    In a nutshell, the problem is with smkx or delete?

    No. The problem is st seems to be the only terminal that sends weird keycodes when the keypad is not explicitly activated. It's not "delete" since it also affects other keys. It's not "smkx" but the fact that it's necessary.

  14. pickfire commented on Sep 1, 2015

    @pickfire
    ContributorAuthor

    There you said it didn't work in bash, and then you later managed to do it anyway.

    Sorry, I only show you the link for the solution for bash, that doesn't happened in fish.

    It's a major change so I don't think they'd appreciate it. But if you want, go ahead.

    Yeah, I can do that, I just don't know how to check which keys is abnormal.

    No. The problem is st seems to be the only terminal that sends weird keycodes when the keypad is not explicitly activated. It's not "delete" since it also affects other keys. It's not "smkx" but the fact that it's necessary.

    So, st is wrong, fish is **right* is al what you want to say? (took from st)

    But it sems that zsh is doing everything correct, I haven't found an issue with zsh except speed issues.

  15. faho commented on Sep 1, 2015

    @faho
    Member

    So, st is wrong, fish is *right is al what you want to say?

    st does something apparently nobody else does, but I guess they'd like to keep their way and they might have a reason for it.

    But it sems that zsh is doing everything correct

    It's quite possible zsh already has a hack like what I proposed above.

  16. 42 remaining items

  17. floam commented on Sep 4, 2016

    @floam
    Member

    The kind of "hack" like described above can be better than what we do currently, though.

    Back to reality: the trouble has sometimes been the user has TERM=xterm but we think their xterm can do 256 colors or they won't hurt. If we were to make an idealized xterm-256color entry and then convert it to C and compile it in as fishunknown-256color, what we could at least do is switch to that at runtime internally when things are looking grim, and then let that run the show form there. We can at least, this way, not do the nasty stuff later in the code where we spin plates on one foot trying to pull off a big lie. We can really just get a string from tparm(set_a_foreground, 256fgval)) check for error values returned, and write that to the screen if it worked or not rite it if it did not, and know that we sent the right thing to everyone we can, instead of nesting 5 levels deep with if/else clauses.

    It can solve everything as long as we put out releases and update the entries time to time.

    (Rarely) we totally give up and set TERM=ansi hoping for the best.

    What we don't actually do is compile in a known safe TERM=ansi or TERM=linux. We could!

  18. floam commented on Sep 4, 2016

    @floam
    Member

    It can solve everything as long as we put out releases and update the entries time to time.

    Except for everything curses/terminfo isn't doing right/can't do/will never do. The structures can't accommodate 24bit colors and for some stupid reason they won't just put a darned boolean there. We could technically mitigate by adding that ourselves to every entry, and then doing a use=actual_entry. Still!

    And all the runtime support code to detect Terminal.app/Konsole/etc, where, unlike st, which could be accommodated quite easily, everyone else lies about TERM- it can't just automagically be put in place for them transparently after we #include that header. We still need to do that in C++. We need to detect them correctly, pick the appropriate entries for them (the real them), in a way that won't break things for other people, or piss off users of that terminal when we haven't made some change they want in there.

    That all sucks.

  19. modified the milestones: fish 2.5.0, on Jan 6, 2017
  20. krader1961 commented on Mar 15, 2017

    @krader1961
    Contributor

    FWIW, the tmux project recently recognized the need to set smkx mode. Whether or not fish should do something similar is still open for debate.

  21. felixsanz commented on May 27, 2017

    @felixsanz

    Hi everyone, i've been investigating about this a few hours but i'm still confused. Whats the status on this?

    If i want to get the DEL key working on st, what i have to do? Using tput smkx seems to fix it.

    There is a hacky functions above that set smkx and rmkx based on events, but why is rmkx needed for? What if i just add tput smkx to my fish config file? From the last comment, it seems like some people (i.e. tmux) has smkx by default, with no rmkx anymore. I'm wrong here?

    So... what is the solution here? Are we waiting for something to be implemented? This should be fixed at runtime?

    Thanks

  22. wilriker commented on Jun 1, 2017

    @wilriker

    This has been "fixed" when using fish inside latest tmux by the commit referenced by @krader1961 above - but IMHO setting smkx hardcoded by tmux is again more of a workaround then a real fix/solution.

    @felixsanz smkx and rmkx are (put simply) two different modes a terminal can be in. The first is keyboard-interactive mode, i.e. the user is supposed to type in something and the latter is a mode where users are not supposed to do some inputs, e.g. because a program is still running but not expecting any input. Why pressing DEL in rmkx leads to a different keycode being interpreted crosses the borders of my historical knowledge (someone obviously had some intention when defining this). st is following this strict convention and if the shell (or some other process) is not setting the correct mode then st will interpret key presses depending on the currently set mode.

    Solutions are plentiful, mostly workarounds (like setting modes based on events or as tmux does it by just setting smkx hardcoded) but the problem is doing it right, meaning implementing in it a way where it does not break compatibility with terminals that cannot handle these switches correctly.

    I, personally, solved this issue by using the event-based approach plus functions that set the required mode where the event-based approach fails (namely neovim). This works great for me. I even extended the event-based approach a little bit to only consider st and interactive mode of fish.

    if status is-interactive
        switch $TERM
    
            # Fix DEL key in st
            case 'st*'
                set -gx is_simple_terminal 1
    
            case "linux"
                set -ex is_simple_terminal
                function fish_prompt
                    fish_fallback_prompt
                end
        end
    
        if set -q is_simple_terminal
            tput smkx ^/dev/null
            function fish_enable_keypad_transmit --on-event fish_postexec
                tput smkx ^/dev/null
            end
    
            function fish_disable_keypad_transmit --on-event fish_preexec
                tput rmkx ^/dev/null
            end
        end
    end
  23. jdebp commented on May 14, 2018

    @jdebp

    There's a lot of misinformation here. Here's what you need to know:

    • Terminals have calculator, cursor, and editing keypads. DEC VT keyboards had the cursor keypad before the calculator keypad. IBM PC keyboards had the calculator keypad (in the Model F) before the cursor keypad (in the Model M).
    • The cursor and calculator keypads can be in either application or normal mode. In normal mode they usually send a DECFNK or some other CSI-introduced control sequence. In application mode they usually send a character shifted by SS3. The idea was that systems that wanted essentially a big bunch of application function keys used application mode, and systems that were happy with DECFNK and ECMA-48-style control sequences that (say) related both up arrow keys to the CUU control sequence, used normal mode.
    • These keypads are individually switchable between application and normal modes in DEC VT alikes, with the private mode settings DECCKM (Cursor Keypad Mode) and DECNKM (Numeric Keypad Mode), respectively.
    • termcap and terminfo sometimes conflate actual terminal capabilities into an abstract model that is significantly limited compared to the capabilities of the actual terminals and slightly off-kilter. In this case, keypad_local and keypad_xmit conflate the cursor and calculator keypad settings into one and imply notions of local versus remote keys, which is not in fact what they do at all. Cursor and calculator keypad keys always transmit shift sequences and control sequences. These capabilities merely switch between two sets.

    All of this stuff is documented. Here is a list of some of the doco to read.

    As you can see from the doco, what people are trying to emulate is this:

    • The normal mode control sequence for Delete on the calculator keypad is either DEL or . according to the NumLock state, or DECFNK 3 if the ALT modifier is in effect; and the application mode shift sequence is SS3 n.
    • The control sequence for Delete on the editing keypad is always DECFNK 3, because the editing keypad does not have an application/normal mode switch.
    • The normal mode control sequence for Left Arrow on the calculator keypad is either CUL (i.e. CSI D) or 4 according to the NumLock state, or DECFNK 7 if the ALT modifier is in effect; and the application mode shift sequence is SS3 t.
    • The normal mode control sequence for Left Arrow on the cursor keypad is CUL (i.e. CSI D), or DECFNK 7 if the ALT modifier is in effect; and the application mode shift sequence is SS3 D.

    For what it's worth, terminfo is pretty much the wrong tool for the job for decoding this stuff. It is wholly inadequate. There are far more possibilities than are accountable for with a simple kdch1= setting. DECFNK control sequences also incorporate modifier information as optional parameters (which is, to repeat, in the doco).

    What I have done in my softwares is use an ECMA-48 state machine, checking completed CSI control sequences to see whether they are DECFNK and decoding the extended/function key and modifiers from them, decoding CSI A to CSI Z into their various DEC VT normal mode extended keys or SCO console function keys, decoding SS3 shifted characters into DEC VT application mode keys, and handling most of the rest as simple character input.

    Other people take similar approaches. NeoVIM uses libtermkey, whose CSI driver is roughly a state machine too. It does not correctly handle SCO console function keys (which is a protocol distinct from the DECFNK protocol, and conflicting with DEC's ECMA-48 CSI sequences, with a different ancestry, that you will find used by some terminal emulators including the FreeBSD kernel's built-in terminal emulator). And it does not account for the mess that is the Linux kernel's built-in terminal emulator which erroneously swaps Home and End with Find and Select. If you want to adopt it, you'll have to remedy those.

  24. faho commented on Apr 2, 2024

    @faho
    Member

    The way forward here is #10359 - make the terminal report the actual keys in a better way.

    I'll be closing this because keypad mode is pretty gnarly.

  25. removed this from the fish-future milestone on Apr 2, 2024
  26. added this to the will-not-implement milestone on Apr 3, 2024
  27. locked as resolved and limited conversation to collaborators on Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions