From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: tomas@tuxteam.de Newsgroups: gmane.emacs.help Subject: Re: How to read an integer from the minibuffer Date: Sat, 13 Nov 2021 09:17:45 +0100 Message-ID: <20211113081745.GB15896@tuxteam.de> References: <87y25u9pzj.fsf@zoho.eu> <993162c2a13d5ea3b0aa@heytings.org> <87lf1u9pdj.fsf@zoho.eu> <993162c2a1f85349fcfa@heytings.org> <87fss29on7.fsf@zoho.eu> <87v90x5g7p.fsf@zoho.eu> <20211112202423.GA25370@tuxteam.de> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="aM3YZ0Iwxop3KEKx" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34108"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/1.5.21 (2010-09-15) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 13 09:18:23 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mloEx-0008fx-3p for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 13 Nov 2021 09:18:23 +0100 Original-Received: from localhost ([::1]:60376 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mloEv-00021J-R8 for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 13 Nov 2021 03:18:21 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:44614) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mloEW-00020x-0w for help-gnu-emacs@gnu.org; Sat, 13 Nov 2021 03:17:56 -0500 Original-Received: from mail.tuxteam.de ([5.199.139.25]:50786) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.90_1) (envelope-from ) id 1mloET-0007dD-PD for help-gnu-emacs@gnu.org; Sat, 13 Nov 2021 03:17:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:To:From:Date; bh=yuI7Vpm2K7ToWYxlb5gg6uWG/3j1FkjpCuqLOQ0AEj8=; b=PMMwoBhQ2G4b+PCqWY+3070sgUSdbXf32Xcg7tlLlbTN0o0HpBfwQAfR2US3yGVOfrELRpIWct7Dm4BEi0QP38XF2tX4ZUmzJksIvcy0UzwZEWLmoBHKLsDjpmfoPLBqN90LnX3N1KvsJVldEuPFkGzvuhyOV54lsIdTna1sM6SjxzqlrB9SLLl7nVqCjp5a+IPH3wJKOjK/ZmO2pkCwVy9M68vf2c92YdqiwyufNa3V5vyQZgn+Y+95cM9cqx3dUo4dGp8PtWnV/pH25369H1CFbvTCnYBcs2PCj5RLZjcsGJecWfVZVKt53oHj4lA5DX3pQywsLOqAIEAZGucZQQ==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1mloEL-0004Vv-Ej for help-gnu-emacs@gnu.org; Sat, 13 Nov 2021 09:17:45 +0100 Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=5.199.139.25; envelope-from=tomas@tuxteam.de; helo=mail.tuxteam.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:134562 Archived-At: --aM3YZ0Iwxop3KEKx Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Nov 13, 2021 at 09:36:39AM +0300, Jean Louis wrote: > * tomas@tuxteam.de [2021-11-12 23:25]: [...] > > Why not simply numberp? > >=20 > > (and (numberp s) (string-to-number s)) >=20 > (numberp "123") =E2=87=92 nil >=20 > 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]+\)$ =2E..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' =3D> %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 --aM3YZ0Iwxop3KEKx Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAmGPdKkACgkQBcgs9XrR2kYO6QCeMxssZmsoDWl79KX8EH+HH750 tsUAnRH+3OoNL3JOOESAnPBjVUd7VZWV =Esy3 -----END PGP SIGNATURE----- --aM3YZ0Iwxop3KEKx--