GNU bug report logs - #8634
24.0.50; `number-at-point' returns char value for `?' constructs - 1) doc, 2) new fns

Previous Next

Package: emacs;

Reported by: "Drew Adams" <drew.adams <at> oracle.com>

Date: Sat, 7 May 2011 15:31:02 UTC

Severity: minor

Tags: fixed

Found in version 24.0.50

Fixed in version 26.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 8634 in the body.
You can then email your comments to 8634 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Sat, 07 May 2011 15:31:03 GMT) Full text and rfc822 format available.

Acknowledgement sent to "Drew Adams" <drew.adams <at> oracle.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sat, 07 May 2011 15:31:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Drew Adams" <drew.adams <at> oracle.com>
To: <bug-gnu-emacs <at> gnu.org>
Subject: 24.0.50;
	`number-at-point' returns char value for `?' constructs - 1) doc, 2)
	new fns
Date: Sat, 7 May 2011 08:30:22 -0700
`number-at-point' is defined like this:

(defun number-at-point ()
  "Return the number at point, or nil if none is found."
  (form-at-point 'sexp 'numberp))

That uses `read-from-string' for the sexp at point, and testing whether the
result is `numberp'.

That's fine, I guess, but it means that with buffer text such as ?A or ?\A-\^@
you get a non-nil result: the character value (wholenump) for ?A, which is 65,
and 4194304, respectively.  If you are depending on code to find _numerals_ in
text and return their numeric values then this is not what you want.  In that
case, you want a function that returns nil when point is not on a numeral.

The current behavior could admittedly be useful sometimes, but:

1. This should be mentioned in the doc string, as it's hardly what an uninformed
user would expect, especially a newbie who doesn't yet know Emacs's char
representation (`?.') and the fact that, for Emacs, chars are numbers.  This
behavior is not obvious, given the current doc and function name.

2. It might not be what the user or calling code really _wants_ in many (most?)
cases.

It's no doubt too late to change the name (e.g. to something like
`number-or-char-at-point').  Some existing code probably depends on the current
behavior.

But it's not too late to add functions that do what many people might expect:
return the number represented by the numeral at point, or nil if there is no
numeral at point.

Here are two functions that could be added.  Whether you add them or not, please
do mention the full behavior (with the gotcha) in the `number-at-point' doc
string.

(defun number-at-point-decimal ()
  "Return the number represented by the decimal numeral at point.
Return nil if none is found."
  (let ((strg  (thing-at-point 'sexp)))
    (and (stringp strg)        
         (if (fboundp 'string-match-p)
             (string-match-p "\\`[0-9]+\\'" strg)
           (string-match "\\`[0-9]+\\'" strg))
         (string-to-number strg))))

(defun number-at-point-hex ()
  "Return the number represented by the hex numeral at point.
Return nil if none is found."
  (let ((strg  (thing-at-point 'sexp)))
    (and (stringp strg)
         (if (fboundp 'string-match-p)
             (string-match-p "\\`[0-9a-fA-F]+\\'" strg)
           (string-match "\\`[0-9a-fA-F]+\\'" strg))
         (string-to-number strg 16))))
 

In GNU Emacs 24.0.50.1 (i386-mingw-nt5.1.2600)
 of 2011-04-25 on 3249CTO
Windowing system distributor `Microsoft Corp.', version 5.1.2600
configured using `configure --with-gcc (4.5) --no-opt --cflags
-Ic:/imagesupport/include'
 





Information forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Sat, 07 May 2011 15:35:02 GMT) Full text and rfc822 format available.

Message #8 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: "Drew Adams" <drew.adams <at> oracle.com>
To: <8634 <at> debbugs.gnu.org>
Subject: RE: bug#8634: 24.0.50;
	`number-at-point' returns char value for `?' constructs - 1) doc,
	2)new fns
Date: Sat, 7 May 2011 08:34:42 -0700
>          (if (fboundp 'string-match-p)
>              (string-match-p "\\`[0-9a-fA-F]+\\'" strg)
>            (string-match "\\`[0-9a-fA-F]+\\'" strg))

(Obviously, vanilla Emacs can just use `string-match-p', since it is defined.)





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Sun, 09 Feb 2014 06:47:01 GMT) Full text and rfc822 format available.

Message #11 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: "Drew Adams" <drew.adams <at> oracle.com>
Cc: 8634 <at> debbugs.gnu.org
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc, 2)
 new fns
Date: Sat, 08 Feb 2014 22:45:02 -0800
"Drew Adams" <drew.adams <at> oracle.com> writes:

> `number-at-point' is defined like this:
>
> (defun number-at-point ()
>   "Return the number at point, or nil if none is found."
>   (form-at-point 'sexp 'numberp))
>
> That uses `read-from-string' for the sexp at point, and testing whether the
> result is `numberp'.
>
> That's fine, I guess, but it means that with buffer text such as ?A or ?\A-\^@
> you get a non-nil result: the character value (wholenump) for ?A, which is 65,
> and 4194304, respectively.  If you are depending on code to find _numerals_ in
> text and return their numeric values then this is not what you want.  In that
> case, you want a function that returns nil when point is not on a numeral.

Looks like this is fixed now.  Closing.  Please reopen if you're still
seeing this.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




bug closed, send any further explanations to 8634 <at> debbugs.gnu.org and "Drew Adams" <drew.adams <at> oracle.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sun, 09 Feb 2014 06:47:04 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Sun, 09 Feb 2014 16:18:03 GMT) Full text and rfc822 format available.

Message #16 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 8634 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc,
 2)	new fns
Date: Sun, 09 Feb 2014 18:17:00 +0200
> From: Lars Ingebrigtsen <larsi <at> gnus.org>
> Date: Sat, 08 Feb 2014 22:45:02 -0800
> Cc: 8634 <at> debbugs.gnu.org
> 
> "Drew Adams" <drew.adams <at> oracle.com> writes:
> 
> > `number-at-point' is defined like this:
> >
> > (defun number-at-point ()
> >   "Return the number at point, or nil if none is found."
> >   (form-at-point 'sexp 'numberp))
> >
> > That uses `read-from-string' for the sexp at point, and testing whether the
> > result is `numberp'.
> >
> > That's fine, I guess, but it means that with buffer text such as ?A or ?\A-\^@
> > you get a non-nil result: the character value (wholenump) for ?A, which is 65,
> > and 4194304, respectively.  If you are depending on code to find _numerals_ in
> > text and return their numeric values then this is not what you want.  In that
> > case, you want a function that returns nil when point is not on a numeral.
> 
> Looks like this is fixed now.

Are you saying that "M-: (number-at-point) RET" with point at ?A says
"nil"?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 01:07:01 GMT) Full text and rfc822 format available.

Message #19 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 8634 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc,
 2)	new fns
Date: Sun, 09 Feb 2014 17:04:57 -0800
Eli Zaretskii <eliz <at> gnu.org> writes:

> Are you saying that "M-: (number-at-point) RET" with point at ?A says
> "nil"?

Yes.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 01:21:02 GMT) Full text and rfc822 format available.

Message #22 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Lars Ingebrigtsen <larsi <at> gnus.org>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 8634 <at> debbugs.gnu.org
Subject: RE: bug#8634: 24.0.50;	`number-at-point' returns char value for `?'
 constructs - 1) doc, 2)	new fns
Date: Sun, 9 Feb 2014 17:20:44 -0800 (PST)
> > Are you saying that "M-: (number-at-point) RET" with point at ?A
> > says "nil"?
> 
> Yes.

I don't see that, in a build from 2014-02-07.  Lars, are you sure
you put point on the `?' in the text `?A', and not just on an
isolated `A' in the text?  This is about reading character
literals.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 02:23:01 GMT) Full text and rfc822 format available.

Message #25 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 8634 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc,
 2)	new fns
Date: Sun, 09 Feb 2014 18:20:50 -0800
Drew Adams <drew.adams <at> oracle.com> writes:

> I don't see that, in a build from 2014-02-07.  Lars, are you sure
> you put point on the `?' in the text `?A', and not just on an
> isolated `A' in the text?  This is about reading character
> literals.

Yeah, I put point on the ? in ?A, say M-: (number-at-point), and Emacs
says "nil".

This is on Fedora 19.

But I take it that that's not what you and Eli are seeing?  I'll reopen
the bug.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




Did not alter fixed versions and reopened. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Mon, 10 Feb 2014 02:23:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 03:21:03 GMT) Full text and rfc822 format available.

Message #30 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 8634 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>
Subject: RE: bug#8634: 24.0.50;	`number-at-point' returns char value for `?'
 constructs - 1) doc, 2)	new fns
Date: Sun, 9 Feb 2014 19:20:10 -0800 (PST)
> > I don't see that, in a build from 2014-02-07.  Lars, are you sure
> > you put point on the `?' in the text `?A', and not just on an
> > isolated `A' in the text?  This is about reading character
> > literals.
> 
> Yeah, I put point on the ? in ?A, say M-: (number-at-point), and
> Emacs says "nil".

OK, thanks for double-checking.  Sounds like it might be a Windows
bug.

> This is on Fedora 19.
> But I take it that that's not what you and Eli are seeing?  I'll
> reopen the bug.

It's not what I see, at least.  Thx.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 03:42:01 GMT) Full text and rfc822 format available.

Message #33 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 8634 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc,
 2)	new fns
Date: Mon, 10 Feb 2014 05:41:30 +0200
> From: Lars Ingebrigtsen <larsi <at> gnus.org>
> Cc: drew.adams <at> oracle.com,  8634 <at> debbugs.gnu.org
> Date: Sun, 09 Feb 2014 17:04:57 -0800
> 
> Eli Zaretskii <eliz <at> gnu.org> writes:
> 
> > Are you saying that "M-: (number-at-point) RET" with point at ?A says
> > "nil"?
> 
> Yes.

Then we must be using a very different Emacs, or maybe the recipe is
not what I think it is.  For me, it still says "65", FWIW.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 04:01:01 GMT) Full text and rfc822 format available.

Message #36 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 8634 <at> debbugs.gnu.org, larsi <at> gnus.org
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc,
 2)	new fns
Date: Mon, 10 Feb 2014 05:59:51 +0200
> Date: Sun, 9 Feb 2014 19:20:10 -0800 (PST)
> From: Drew Adams <drew.adams <at> oracle.com>
> Cc: Eli Zaretskii <eliz <at> gnu.org>, 8634 <at> debbugs.gnu.org
> 
> > > I don't see that, in a build from 2014-02-07.  Lars, are you sure
> > > you put point on the `?' in the text `?A', and not just on an
> > > isolated `A' in the text?  This is about reading character
> > > literals.
> > 
> > Yeah, I put point on the ? in ?A, say M-: (number-at-point), and
> > Emacs says "nil".
> 
> OK, thanks for double-checking.  Sounds like it might be a Windows
> bug.

No, it's not Windows specific, because I see this on this machine:

  Linux fencepost.gnu.org 2.6.32-48-server #1trisquel3 SMP Mon Jun 17 20:00:36 UTC 2013 x86_64 GNU/Linux




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 04:02:01 GMT) Full text and rfc822 format available.

Message #39 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 8634 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc,
 2)	new fns
Date: Sun, 09 Feb 2014 19:59:31 -0800
Eli Zaretskii <eliz <at> gnu.org> writes:

>> From: Lars Ingebrigtsen <larsi <at> gnus.org>
>> Cc: drew.adams <at> oracle.com,  8634 <at> debbugs.gnu.org
>> Date: Sun, 09 Feb 2014 17:04:57 -0800
>> 
>> Eli Zaretskii <eliz <at> gnu.org> writes:
>> 
>> > Are you saying that "M-: (number-at-point) RET" with point at ?A says
>> > "nil"?
>> 
>> Yes.
>
> Then we must be using a very different Emacs, or maybe the recipe is
> not what I think it is.  For me, it still says "65", FWIW.

I just tried saying the same in the *scratch* buffer, and there it
returns 65!

Here in the Message buffer it returns nil.  It does in a
fundamental-mode buffer, too...

----

number-at-point is an autoloaded compiled Lisp function in
`thingatpt.el'.

(number-at-point)

Return the number at point, or nil if none is found.


-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 04:07:01 GMT) Full text and rfc822 format available.

Message #42 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Lars Ingebrigtsen <larsi <at> gnus.org>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 8634 <at> debbugs.gnu.org
Subject: RE: bug#8634: 24.0.50;	`number-at-point' returns char value for `?'
 constructs - 1) doc, 2)	new fns
Date: Sun, 9 Feb 2014 20:06:42 -0800 (PST)
> I just tried saying the same in the *scratch* buffer, and there it
> returns 65!
> 
> Here in the Message buffer it returns nil.  It does in a
> fundamental-mode buffer, too...

Sounds like we might be getting somewhere now.  Something in
certain buffers causes `number-at-point' to do the wrong thing.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Mon, 10 Feb 2014 14:28:02 GMT) Full text and rfc822 format available.

Message #45 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Nicolas Richard <theonewiththeevillook <at> yahoo.fr>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 8634 <at> debbugs.gnu.org, Lars Ingebrigtsen <larsi <at> gnus.org>,
 Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc,
 2)	new fns
Date: Mon, 10 Feb 2014 15:27:27 +0100
Drew Adams <drew.adams <at> oracle.com> writes:

>> I just tried saying the same in the *scratch* buffer, and there it
>> returns 65!
>> 
>> Here in the Message buffer it returns nil.  It does in a
>> fundamental-mode buffer, too...
>
> Sounds like we might be getting somewhere now.  Something in
> certain buffers causes `number-at-point' to do the wrong thing.

That is because number-at-point uses the 'sexp at point, which depends
on the syntax table.

The following definition seems to fix that dependance (500 is arbitrary
choice -- the same arbitrary choice as for emails) :

diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el
index 9a40049..7b4cf56 100644
--- a/lisp/thingatpt.el
+++ b/lisp/thingatpt.el
@@ -581,7 +581,10 @@ Signal an error if the entire string was not used."
 ;;;###autoload
 (defun number-at-point ()
   "Return the number at point, or nil if none is found."
-  (form-at-point 'sexp 'numberp))
+  (when (thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
+    (string-to-number
+     (buffer-substring (match-beginning 0) (match-end 0)))))
+
 (put 'number 'thing-at-point 'number-at-point)
 ;;;###autoload
 (defun list-at-point ()


-- 
Nico.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#8634; Package emacs. (Thu, 28 Apr 2016 10:39:02 GMT) Full text and rfc822 format available.

Message #48 received at 8634 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Nicolas Richard <theonewiththeevillook <at> yahoo.fr>
Cc: 8634 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>,
 Drew Adams <drew.adams <at> oracle.com>
Subject: Re: bug#8634: 24.0.50;
 `number-at-point' returns char value for `?' constructs - 1) doc,
 2)	new fns
Date: Thu, 28 Apr 2016 12:38:54 +0200
Nicolas Richard <theonewiththeevillook <at> yahoo.fr> writes:

> The following definition seems to fix that dependance (500 is arbitrary
> choice -- the same arbitrary choice as for emails) :
>
> diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el
> index 9a40049..7b4cf56 100644
> --- a/lisp/thingatpt.el
> +++ b/lisp/thingatpt.el
> @@ -581,7 +581,10 @@ Signal an error if the entire string was not used."
>  ;;;###autoload
>  (defun number-at-point ()
>    "Return the number at point, or nil if none is found."
> -  (form-at-point 'sexp 'numberp))
> +  (when (thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
> +    (string-to-number
> +     (buffer-substring (match-beginning 0) (match-end 0)))))
> +
>  (put 'number 'thing-at-point 'number-at-point)
>  ;;;###autoload
>  (defun list-at-point ()

Thanks; applied to the trunk.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) fixed. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Thu, 28 Apr 2016 10:40:01 GMT) Full text and rfc822 format available.

bug marked as fixed in version 25.2, send any further explanations to 8634 <at> debbugs.gnu.org and "Drew Adams" <drew.adams <at> oracle.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Thu, 28 Apr 2016 10:40:01 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 26 May 2016 11:24:04 GMT) Full text and rfc822 format available.

bug unarchived. Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Sun, 04 Dec 2016 02:50:02 GMT) Full text and rfc822 format available.

bug Marked as fixed in versions 26.1. Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Sun, 04 Dec 2016 02:50:03 GMT) Full text and rfc822 format available.

bug No longer marked as fixed in versions 25.2. Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Sun, 04 Dec 2016 02:50:03 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 01 Jan 2017 12:24:13 GMT) Full text and rfc822 format available.

This bug report was last modified 8 years and 166 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.