From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: How to read an integer from the minibuffer
Date: Fri, 12 Nov 2021 01:28:32 +0100 [thread overview]
Message-ID: <87y25u9pzj.fsf@zoho.eu> (raw)
In-Reply-To: d46f031af8779f83b0e1@heytings.org
Gregory Heytings wrote:
>> Cool (honestly). But do show how to use that to read an
>> integer and only an integer ...
>
> You mean, how these two answers can be combined?
Nope, I don't like the first solution since it disallows
certain input chars, that feels like the shell would disallow
-j after 'ls' just because there is no such option to ls(1)
... i.e., that doesn't feel good.
This time try it for real:
(read-number "try enter a string: ")
That's better.
Rather, I meant show how to use the second solution to solve
the OPs example, i.e. to read an integer and only an integer.
> (defun restricted-read-from-minibuffer (prompt regexp &optional allowed-chars)
> "Read a string from the minibuffer, with additional constraints.
> If the input does not match REGEXP, read a string again.
> If ALLOWED-CHARS is a string, the only allowed characters are those in
> that string."
> (let ((map nil)
> (string nil))
> (when (stringp allowed-chars)
> (let ((m (make-keymap)))
> (define-key m [t]
> (lambda ()
> (interactive)
> (message "Character not allowed")))
> (define-key m (kbd "RET") #'exit-minibuffer)
> (define-key m (kbd "<return>") #'exit-minibuffer)
> (define-key m (kbd "C-j") #'exit-minibuffer)
> (define-key m (kbd "C-g") #'abort-minibuffers)
> (dolist (c (split-string allowed-chars "" t))
> (define-key m c #'self-insert-command))
> (setq map m)))
> (while (progn
> (setq string (read-from-minibuffer prompt nil map))
> (when regexp
> (unless (string-match regexp string nil t)
> (message "Unexpected input.")
> (sit-for 1)
> t))))
> string))
>
> With this,
>
> (restricted-read-from-minibuffer "Integer? " "^[0-9][0-9]*$" "0123456789")
I get
if: Wrong number of arguments: string-match, 4
but that's a minor bug I suspect ...
You can also write the integer regexp like below
(string-match "^[0-9]\\{1,\\}$" "99") ; 0, The Great One
(string-match "^[0-9]\\{1,\\}$" "88") ; 0, Big Eric
or maybe "^[1-9][0-9]*$" ...
> will read "an integer and only an integer". And you can wrap
> it into a string-to-number to get the integer itself.
>
> (BTW, it seems that there's no way in Elisp to "expand"
> a regexp charset, e.g. "[0-9]" into "0123456789". That would
> make the ALLOWED-CHARS argument easier to type in.)
There is such a package, xr - the reverse of rx, LOL :)
https://elpa.gnu.org/packages/xr.html
--
underground experts united
https://dataswamp.org/~incal
next prev parent reply other threads:[~2021-11-12 0:28 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-11 4:53 How to read an integer from the minibuffer Marcin Borkowski
2021-11-11 5:11 ` Po Lu
2021-11-11 7:18 ` Marcin Borkowski
2021-11-12 7:21 ` Yuri Khan
2021-11-13 6:59 ` Marcin Borkowski
2021-11-13 8:43 ` tomas
2021-11-13 7:33 ` Jean Louis
2021-11-16 6:39 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-16 7:37 ` Yuri Khan
2021-11-16 8:00 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-11 6:27 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-11 10:25 ` Gregory Heytings
2021-11-11 10:28 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-11 11:00 ` Gregory Heytings
2021-11-11 13:20 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-11 11:17 ` Gregory Heytings
2021-11-11 13:39 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-11 14:30 ` Gregory Heytings
2021-11-12 0:28 ` Emanuel Berg via Users list for the GNU Emacs text editor [this message]
2021-11-12 0:37 ` Gregory Heytings
2021-11-12 0:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12 0:52 ` Gregory Heytings
2021-11-12 0:57 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12 19:05 ` Jean Louis
2021-11-12 19:25 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12 19:55 ` Jean Louis
2021-11-12 21:14 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-13 6:37 ` Jean Louis
2021-11-16 6:21 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12 19:56 ` Jean Louis
2021-11-12 20:02 ` Jean Louis
2021-11-12 20:24 ` tomas
2021-11-12 21:15 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12 21:30 ` tomas
2021-11-12 21:34 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-13 6:46 ` Jean Louis
2021-11-13 7:32 ` tomas
2021-11-16 6:24 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-13 6:36 ` Jean Louis
2021-11-13 8:17 ` tomas
2021-11-13 8:44 ` Jean Louis
2021-11-16 6:15 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-16 6:03 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12 1:09 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12 1:12 ` Gregory Heytings
2021-11-12 3:07 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12 6:21 ` Marcin Borkowski
2021-11-16 7:52 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-16 8:05 ` Yuri Khan
2021-11-16 9:38 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-16 11:18 ` Yuri Khan
2021-11-16 11:37 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-16 11:52 ` Yuri Khan
2021-11-16 12:00 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-16 13:10 ` Emanuel Berg via Users list for the GNU Emacs text editor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87y25u9pzj.fsf@zoho.eu \
--to=help-gnu-emacs@gnu.org \
--cc=moasenwood@zoho.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).