Skip to main content
8 of 8
replaced http://emacs.stackexchange.com/ with https://emacs.stackexchange.com/

How to extend the <ESC> => M- translation to non-character keys?

This question is a follow-up to this answer (q.v.).

I understand that Emacs interprets esc as M- only when the keystroke that follows it corresponds to a "character" (e.g. A, 5, ;, etc.), as opposed to, e.g., , F5, home, etc.

What can I add to my .emacs file to extend the esc ⇒ M- interpretation to the case where the following key is a "non-character" key?


OK, the following snippet takes care of my immediate problem, but it's not an answer to my question (see below):

(add-hook 'org-mode-hook
    (lambda ()
        (define-key org-mode-map [27 S-down] 'org-shiftmetadown)
        (define-key org-mode-map [27 S-up] 'org-shiftmetaup)
        (define-key org-mode-map [27 S-right] 'org-shiftmetaright)
        (define-key org-mode-map [27 S-left] 'org-shiftmetaleft)
        (define-key org-mode-map [27 down] 'org-metadown)
        (define-key org-mode-map [27 up] 'org-metaup)
        (define-key org-mode-map [27 right] 'org-metaright)
        (define-key org-mode-map [27 left] 'org-metaleft)
        (define-key org-mode-map [27 return] 'org-meta-return)
        (define-key org-mode-map [27 S-return] 'org-insert-todo-heading)))

To find the bindings I needed to define, I got a listing of org-mode-map (with C-h v), and searched for M- to get all the bindings beginning with M-:

  (M-S-down . org-shiftmetadown)
  (M-S-up . org-shiftmetaup)
  (M-S-right . org-shiftmetaright)
  (M-S-left . org-shiftmetaleft)
  (M-down . org-metadown)
  (M-up . org-metaup)
  (M-right . org-metaright)
  (M-left . org-metaleft)
  (M-return . org-meta-return)
  (M-S-return . org-insert-todo-heading)

Each one of these lines is the basis for a corresponding define-key expression in the mode-hook shown above. The transformation should be pretty obvious. (The 27 in the define-key expressions is the code for esc.)

One would need to do this sort of thing for every single mode X that defines a key binding of the form M- followed by a non-character key sequence. Specifically, for every such binding, one would have to include a define-key expression in a mode-hook for mode X. Therefore, this approach is obviously a very laborious, fragile, and hard-to-maintain hack. It does not do what the question requests, but instead painstakingly simulates it for certain special cases. For this reason I did not post it as an answer, in the hopes that someone will post an actual answer to the original question.

kjo
  • 3.3k
  • 21
  • 49