From: tomas@tuxteam.de
To: help-gnu-emacs@gnu.org
Subject: Re: How to read an integer from the minibuffer
Date: Sat, 13 Nov 2021 09:17:45 +0100 [thread overview]
Message-ID: <20211113081745.GB15896@tuxteam.de> (raw)
In-Reply-To: <YY9c92V+QQCPKCsg@protected.localdomain>
[-- Attachment #1: Type: text/plain, Size: 2955 bytes --]
On Sat, Nov 13, 2021 at 09:36:39AM +0300, Jean Louis wrote:
> * tomas@tuxteam.de <tomas@tuxteam.de> [2021-11-12 23:25]:
[...]
> > Why not simply numberp?
> >
> > (and (numberp s) (string-to-number s))
>
> (numberp "123") ⇒ nil
>
> It checks if object is number. That is why it is not usable to check
> if string is actual number.
D'oh, you are right. That'd been too easy ;-)
It seems you'll have to go with a regexp, then do string-to-number.
Then, again, you'll have to decide: what subset of Emacs's number
input syntax do you want to implement? Signed/unsigned? Integers?
Floats? Exponential notation? Bases other than 10?
As far as Emacs is concerned, for example, this is a number
#27r21 -> 55
(this would be 21 in base 27). As is this:
#b10101 -> 21
That gets interesting once you mix multibase ints and floats: is an
e a digit (in a base greater than 14) or the exponential marker?
(my hunch is that floats & exponential notation is only allowed for
base 10, but more doc reading would be necessary).
Or do you want to set the rules, independently of what Emacs lisp
does? Then you better do that explicitly.
I think you only can get down to work once you've cleared that.
To get you started, here would be a regexp to (roughly) accept
floats:
^[+-]?[0-9]*\(?:\.[0-9]+\)\(?:[eE][0-9]+\)$
...the backslashes having to be escaped if you put that into a
string, of course.
Add [[:space:]]* in front (or at the end) of it if you want to
accept leading (or trailing) space.
Here's a "packaged" version for the convenience of your buffer:
(seq-do
(lambda (tst)
(insert
(format
"'%s' => %s\n"
tst
(string-match-p "^[+-]?[0-9]*\\(?:\\.[0-9]+\\)?\\(?:[eE][0-9]+\\)?$" tst))))
'("123"
" 123"
"123 "
""
"."
"3.141593"
".0"
"1E12"
"6.02e23"))
Note a couple of things:
- note that 0 means success: actually `string-match' is
returning the position where the match starts, and it can
only be 0, since our regexp is anchored at the beginning
(^); on failure it returns nil
- it does accept an empty string. This comes from the fact that
we allow the part before the decimal point to be empty (we
might like to accept ".23"), but also the decimal point and
all that to its right to be missing ("123" is a nice number,
after all). This can, of course, be fixed. At the cost of an
uglier regexp or, alternatively, some post-processing-
- if you are going to accept leading (and trailing? You didn't
make your position explicit yet) whitespace, you could put
the "number part" in a capture group \(...\) so you can
retrieve it after the match.
- write a series of tests yourself: this will help you to
better specify what you want to consider as a number
- other bases are left as an exercise to the reader ;^)
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
next prev parent reply other threads:[~2021-11-13 8:17 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
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 [this message]
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=20211113081745.GB15896@tuxteam.de \
--to=tomas@tuxteam.de \
--cc=help-gnu-emacs@gnu.org \
/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).