1

I want to perform the same task as in the function below, but without having to press ENTER every time I select an option.

(defun my-fun ()
  (interactive)
  (setq something (read-string "Options:\n [m]: Do A.\n [n] Do B.\nINPUT: "))
  (pcase something
    ("m" (insert "m pressed\n"))
    ("n" (insert "n pressed\n")))
  (my-fun))

What is the correct way to do it? It is probably a basic function (some alternative to read string?), but I haven't been able to find it.

  • you may be interested in hydra as a mode to do just that – Joafigue Apr 17 '17 at 20:09
4

You apparently want to read a character and immediately dispatch behavior based on what it is.

Help > Search Documentation > Find Any Object by Name (command apropos) tells you, for read plus char tells you about function read-char.

C-h f read-char:

read-char is a built-in function in `C source code'.

(read-char &optional PROMPT INHERIT-INPUT-METHOD SECONDS)

Read a character from the command input (keyboard or macro). It is returned as a number. If the character has modifiers, they are resolved and reflected to the character code if possible (e.g. C-SPC -> 0).

If the user generates an event which is not a character (i.e. a mouse click or function key event), read-char signals an error. As an exception, switch-frame events are put off until non-character events can be read.

If you want to read non-character events, or ignore them, call read-event or read-char-exclusive instead.

If the optional argument PROMPT is non-nil, display that as a prompt.

If the optional argument INHERIT-INPUT-METHOD is non-nil and some input method is turned on in the current buffer, that input method is used for reading a character.

If the optional argument SECONDS is non-nil, it should be a number specifying the maximum number of seconds to wait for input. If no input arrives in that time, return nil. SECONDS may be a floating-point value.

| improve this answer | |
  • That's it! I only had to change "m" to 109 and so on... – Jacek Apr 17 '17 at 16:06
  • 5
    @Jacek if you want something more readable, you can use ?m as the character literal that evaluates to 109. – zck Apr 17 '17 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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