2

In Windows, it is possible to type the Alt key together with some number to obtain the character associated to that number. This is useful when your keyboard is not properly mapped or is missing that character. In Emacs, doing this triggers a command repetition, as the correct way to print a character from a number is to use C-q number.

Beyond trashing the screen, if the users types M-number and does not see the desired character, they will most likely type again, and the ultimate effect is to freeze Emacs. This happens because, as soon as they type a non-number key, they trigger a number of repetitions too high

I can think of two possible theoretical solutions to avoid this incident: one is to disable the bindings associated to the repetitions, and the other is to rewrite the repetition function so that, if the number is, say, above 1000, then you are asked a confirmation before executing it, or the function simply returns.

As for the latter, one could rewrite the function digit-argument so that it returns when the digit variable is higher than some threshold.

I am not sure if there is a better way.

6
  • Just what is the question?
    – Drew
    Commented Oct 6, 2022 at 16:50
  • 1
    Avoiding involuntary command repetitions, by disabling or modifying the function triggering them
    – antonio
    Commented Oct 6, 2022 at 18:41
  • What is "the character associated to that number" for M-0 to M-9? And if your OS was actually intercepting those key strokes, Emacs would be receiving that resulting character rather than the modified digit, so it's not clear that what you've described is actually accurate.
    – phils
    Commented Oct 7, 2022 at 1:46
  • I asked because "involuntary command repetitions" wasn't very clear, to me. I translated the title to something closer to Emacs-speak. What I think you're talking about is the use of Alt + a digit key to provide a numeric prefix argument to a command or another key.
    – Drew
    Commented Oct 7, 2022 at 1:55
  • @Drew: fine, thank you
    – antonio
    Commented Oct 8, 2022 at 13:25

2 Answers 2

2

I guess you want Alt + a digit key to insert the character of that digit. In Emacs, Alt + 3 is written M-3 (Meta 3).

If you want M-3 to insert the character 3, and so on then do this:

(define-key esc-map [?0] "0")
(define-key esc-map [?1] "1")
(define-key esc-map [?2] "2")
(define-key esc-map [?3] "3")
(define-key esc-map [?4] "4")
(define-key esc-map [?5] "5")
(define-key esc-map [?6] "6")
(define-key esc-map [?7] "7")
(define-key esc-map [?8] "8")
(define-key esc-map [?9] "9")

If you want such keys to do nothing then do this:

(define-key esc-map [?0] nil)
(define-key esc-map [?1] nil)
(define-key esc-map [?2] nil)
(define-key esc-map [?3] nil)
(define-key esc-map [?4] nil)
(define-key esc-map [?5] nil)
(define-key esc-map [?6] nil)
(define-key esc-map [?7] nil)
(define-key esc-map [?8] nil)
(define-key esc-map [?9] nil)

Variable esc-map has as its value the keymap for Meta keys.

You can use the Esc key in place of the Alt modifier key. Hit ESC and then a digit, ESC 3 etc. in place of M-3 etc. And the above rebindings also bind key ESC followed by a digit.


I don't advise doing any of this, however. I recommend that you learn the Emacs use of ESC and the M- prefix, including for digits. Trying to have Emacs mimic inferior habits from MS Windows (or whatever) isn't worth it, IMHO. Those are just habits to grow out of, IMO.

1
  • Thanks, the esc-map is what I was missing, which closes the issue. Regarding "bad habits", I once found myself in a situation where I inadvertently triggered too many repetitions in a comint buffer, froze Emacs and lost my work. So, I propose also my solution based on advising (digit-argument).
    – antonio
    Commented Oct 12, 2022 at 17:22
0

Another solution, where you cannot make more than 1000 repetitions.

(defun my-digit-argument (x)
  (when (> prefix-arg 1000)
    (setq prefix-arg nil)
    (message "Too many repetitions")))
 
(advice-add 'digit-argument :after #'my-digit-argument)

Your Answer

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

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