5

I tried to check what function shift-right is bound to which extends the region right by a character by default. But C-h k says:

<right> (translated from <S-right>) runs the command right-char (found
in global-map), which is an interactive compiled Lisp function in
‘bindings.el’.

If S-right does something else than simple right then why does it say it is bound to right?

1

2 Answers 2

3

Taken from this emacs wiki page:

Emacs is set up so that the keys, particularly all the control-, meta-, alt- keybindings keep working if you inadvertantly leave the shift-lock key on. So it assumes that you were looking for the unshifted one if this is the only keybinding defined on that key. Hence it sees the shifted function key, checks to see if the shifted function key has been bound to a function and if it doesn’t find a keybinding, checks to see if there is anything bound to the unshifted key. If it finds a binding, it gives you that one instead of the one you asked for.

4
  • So how to find out what function S-right runs which does something else than simple right?
    – Tom
    Aug 4, 2019 at 16:10
  • If there is something bound to it, it will show it. In your case there isn't so it gets translated to right. Execute (global-set-key [(shift f1)] 'info) and check again.
    – Hubisan
    Aug 4, 2019 at 16:16
  • But you surely realise that S-right does more, it extends the selection even in default emacs 26.2 with -q. So it's not just for me. So the question is what function in emacs performs the shift detection part in this case?
    – Tom
    Aug 4, 2019 at 16:21
  • 2
    The intereactive code ^ in the interactive statement of a function is what tells Emacs to do something different if the shift key is depressed; e.g., (interactive "^p") used by right-char and left-char. See the interactive codes section of the Emacs manual for more information: gnu.org/software/emacs/manual/html_node/elisp/… If we were to remove the ^ from the interactive statement in left-char and/or right-char, then the shift (if depressed) would have no effect.
    – lawlist
    Aug 4, 2019 at 16:27
5

If an input event is not bound and contains the Shift modifier or is an uppercase character, Emacs converts it to the corresponding unshifted or lowercase event. This is discreetly documented in the Emacs Lisp manual under read-key-sequence. Unlike what the docstring of read-key-sequence says, this isn't limited to the first event in a sequence: if KEY1 is bound to a keymap (so it's a prefix key) but S-KEY1 is not bound, pressing S-KEY1 KEY2 results in an event for KEY1 KEY2. However, if S-KEY1 is a prefix key, S-KEY1 KEY2 remains unchanged even if S-KEY1 KEY2 is not bound, regardless of what KEY1 KEY2 might be bound to.

Commands can find out whether such a translation happened by checking the variable this-command-keys-shift-translated. Few commands do. The only ones I can think of are the cursor motion commands that select text when Shift is pressed, like right-char. This mechanism is handled by handle-shift-selection which is triggered for every command that has ^ in its interactive specification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.