help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to read an integer from the minibuffer


From: Emanuel Berg
Subject: Re: How to read an integer from the minibuffer
Date: Thu, 11 Nov 2021 14:39:02 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Gregory Heytings wrote:

>> I'd like to read an integer (or something else e.g.
>> matching a regex) from a minibuffer. Basically, I'm after
>> a version of `read-string', but either allowing only some
>> characters, or accepting only input matching some regex
>> (possibly both).
>>
>> How do I do that? One way would be to use
>> `read-from-minibuffer' with a suitable keymap, but that
>> seems slightly low-level. If that is the way to go, is
>> there a good way to set up a keymap so that nothing except
>> some specified characters are self-inserting? IOW, is
>> `suppress-keymap' the way to go or is there some other way?
>>
>
> Another way:
>
> (defun restricted-read-from-minibuffer (prompt regexp)
>   "Read a string matching REGEXP from the minibuffer, prompting with PROMPT."
>   (let ((s nil))
>     (while (progn
>            (setq s (read-from-minibuffer prompt))
>            (unless (string-match regexp s)
>              (message "Unexpected input.")
>              (sit-for 1)
>              t)))
>     s))

Cool (honestly). But do show how to use that to read an
integer and only an integer ...

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]