GNU bug report logs -
#32504
[PATCH] syntax-is-{comment|string}-p
Previous Next
Reported by: Alex Branham <alex.branham <at> gmail.com>
Date: Wed, 22 Aug 2018 20:20:02 UTC
Severity: wishlist
Tags: fixed, patch
Fixed in version 27.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 32504 in the body.
You can then email your comments to 32504 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Wed, 22 Aug 2018 20:20:03 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Alex Branham <alex.branham <at> gmail.com>
:
New bug report received and forwarded. Copy sent to
bug-gnu-emacs <at> gnu.org
.
(Wed, 22 Aug 2018 20:20:03 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
[Message part 1 (text/plain, inline)]
Hello -
I got tired of checking/remembering whether (nth 3 (syntax-ppss)) was a
string or a comment, so I've created two simple functions that wrap that
in a more descriptive name. I've noticed that there are a handful of
these that other modes have written, so evidently others wanted them as
well at some point.
There are two patches attached. The first creates new functions
syntax-is-comment-p and syntax-is-string-p.
The second changes most of the uses of (nth 3 (syntax-ppss)) and (nth 4
(syntax-ppss)) to use these new functions. I'm attaching it as a
separate patch since I'm not sure if yall will want it or not; it's easy
to squash if you do.
Thanks,
Alex
------------------------------------------------------------
From 393f55de705513f5514a3e0250d81053e838c447 Mon Sep 17 00:00:00 2001
From: Alex Branham <alex.branham <at> gmail.com>
Date: Wed, 22 Aug 2018 14:44:26 -0500
Subject: [PATCH 1/2] Add new functions syntax-is-{comment|string}-p
* lisp/emacs-lisp/syntax.el (syntax-is-comment-p, syntax-is-string-p):
New functions
* doc/lispref/syntax.texi: Add documentation for new functions
---
doc/lispref/syntax.texi | 10 ++++++++++
etc/NEWS | 4 ++++
lisp/emacs-lisp/syntax.el | 12 ++++++++++++
3 files changed, 26 insertions(+)
diff --git a/doc/lispref/syntax.texi b/doc/lispref/syntax.texi
index dcfade3f67..e6220e5cf9 100644
--- a/doc/lispref/syntax.texi
+++ b/doc/lispref/syntax.texi
@@ -790,6 +790,16 @@ Position Parse
Hooks}).
@end defun
+@defun syntax-is-comment-p &optional pos
+This function returns non-nil when @code{POS} (which defaults to
+point) is in a comment accord to @code{syntax-ppss}
+@end defun
+
+@defun syntax-is-string-p &optional pos
+This function returns non-nil when @code{POS} (which defaults to
+point) is in a string accord to @code{syntax-ppss}
+@end defun
+
@node Parser State
@subsection Parser State
@cindex parser state
diff --git a/etc/NEWS b/etc/NEWS
index d757f52466..e138105e72 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -963,6 +963,10 @@ versions.
The new variable 'comment-use-syntax-ppss' can be set to nil to recover the old
behavior if needed.
+** New functions 'syntax-is-comment-p' and 'syntax-is-string-p'.
+These can be used as a shorthand instead of checking, for example,
+(nth 3 (syntax-ppss)).
+
** The 'server-name' and 'server-socket-dir' variables are set when a
socket has been passed to Emacs.
diff --git a/lisp/emacs-lisp/syntax.el b/lisp/emacs-lisp/syntax.el
index ad1a9665ff..2a262c6036 100644
--- a/lisp/emacs-lisp/syntax.el
+++ b/lisp/emacs-lisp/syntax.el
@@ -616,6 +616,18 @@ syntax-ppss
;; a nil state.
(parse-partial-sexp (point-min) pos))))))
+(defun syntax-is-comment-p (&optional pos)
+ "Return non-nil if POS is inside a comment.
+POS defaults to `point'."
+ (let ((pos (or pos (point))))
+ (nth 4 (syntax-ppss pos))))
+
+(defun syntax-is-string-p (&optional pos)
+ "Return non-nil if POS is inside a string.
+POS defaults to `point'."
+ (let ((pos (or pos (point))))
+ (nth 3 (syntax-ppss pos))))
+
;; Debugging functions
(defun syntax-ppss-debug ()
--
2.18.0
[0001-Add-new-functions-syntax-is-comment-string-p.patch (text/x-patch, attachment)]
[0002-Prefer-using-new-syntax-is-comment-string-p-function.patch (text/x-patch, attachment)]
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Thu, 23 Aug 2018 03:13:02 GMT)
Full text and
rfc822 format available.
Message #8 received at 32504 <at> debbugs.gnu.org (full text, mbox):
Alex Branham <alex.branham <at> gmail.com> writes:
> I got tired of checking/remembering whether (nth 3 (syntax-ppss)) was a
> string or a comment, so I've created two simple functions that wrap that
> in a more descriptive name. I've noticed that there are a handful of
> these that other modes have written, so evidently others wanted them as
> well at some point.
What about syntax-ppss-context in syntax.el?
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Thu, 23 Aug 2018 13:02:01 GMT)
Full text and
rfc822 format available.
Message #11 received at 32504 <at> debbugs.gnu.org (full text, mbox):
On Wed 22 Aug 2018 at 22:12, Noam Postavsky <npostavs <at> gmail.com> wrote:
> Alex Branham <alex.branham <at> gmail.com> writes:
>
>> I got tired of checking/remembering whether (nth 3 (syntax-ppss)) was a
>> string or a comment, so I've created two simple functions that wrap that
>> in a more descriptive name. I've noticed that there are a handful of
>> these that other modes have written, so evidently others wanted them as
>> well at some point.
>
> What about syntax-ppss-context in syntax.el?
Thanks, I didn't know about syntax-ppss-context. It doesn't seem to be
documented anywhere.
I think I still slightly prefer having these two functions since when
searching for something like "syntax string" syntax-is-string-p will pop
up but syntax-ppss-context won't.
Thanks again,
Alex
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Thu, 23 Aug 2018 13:59:02 GMT)
Full text and
rfc822 format available.
Message #14 received at 32504 <at> debbugs.gnu.org (full text, mbox):
> From: Alex Branham <alex.branham <at> gmail.com>
> Date: Thu, 23 Aug 2018 08:01:36 -0500
> Cc: 32504 <at> debbugs.gnu.org
>
> > What about syntax-ppss-context in syntax.el?
>
> Thanks, I didn't know about syntax-ppss-context. It doesn't seem to be
> documented anywhere.
If it's useful, we could document it.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Thu, 23 Aug 2018 23:19:01 GMT)
Full text and
rfc822 format available.
Message #17 received at 32504 <at> debbugs.gnu.org (full text, mbox):
Note also the "Todo" item in syntax.el:
;; - new functions `syntax-state', ... to replace uses of
parse-partial-state
;; with something higher-level (similar to syntax-ppss-context).
Presumably that comment should be removed in any patch for this bug.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Fri, 24 Aug 2018 16:12:02 GMT)
Full text and
rfc822 format available.
Message #20 received at 32504 <at> debbugs.gnu.org (full text, mbox):
On Thu 23 Aug 2018 at 08:58, Eli Zaretskii <eliz <at> gnu.org> wrote:
>> From: Alex Branham <alex.branham <at> gmail.com>
>> Date: Thu, 23 Aug 2018 08:01:36 -0500
>> Cc: 32504 <at> debbugs.gnu.org
>>
>> > What about syntax-ppss-context in syntax.el?
>>
>> Thanks, I didn't know about syntax-ppss-context. It doesn't seem to be
>> documented anywhere.
>
> If it's useful, we could document it.
I'm ambivalent about its usefulness - I don't see it used anywhere in
Emacs's code and only once in the packages I have installed.
Alex
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Fri, 24 Aug 2018 19:29:02 GMT)
Full text and
rfc822 format available.
Message #23 received at 32504 <at> debbugs.gnu.org (full text, mbox):
> From: Alex Branham <alex.branham <at> gmail.com>
> Cc: npostavs <at> gmail.com, 32504 <at> debbugs.gnu.org
> Date: Fri, 24 Aug 2018 11:11:47 -0500
>
> >> > What about syntax-ppss-context in syntax.el?
> >>
> >> Thanks, I didn't know about syntax-ppss-context. It doesn't seem to be
> >> documented anywhere.
> >
> > If it's useful, we could document it.
>
> I'm ambivalent about its usefulness - I don't see it used anywhere in
> Emacs's code and only once in the packages I have installed.
That could be because it isn't documented. After all, that was your
reason for not knowing about it, right?
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Fri, 24 Aug 2018 19:50:02 GMT)
Full text and
rfc822 format available.
Message #26 received at 32504 <at> debbugs.gnu.org (full text, mbox):
On Fri 24 Aug 2018 at 14:28, Eli Zaretskii <eliz <at> gnu.org> wrote:
> That could be because it isn't documented. After all, that was your
> reason for not knowing about it, right?
Yes, absolutely. I meant I'm ambivalent about its usefulness versus
syntax-is-comment/string-p, which seem more descriptive to me.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Sat, 25 Aug 2018 02:27:02 GMT)
Full text and
rfc822 format available.
Message #29 received at 32504 <at> debbugs.gnu.org (full text, mbox):
Alex Branham <alex.branham <at> gmail.com> writes:
>>> Thanks, I didn't know about syntax-ppss-context. It doesn't seem to be
>>> documented anywhere.
>>
>> If it's useful, we could document it.
>
> I'm ambivalent about its usefulness - I don't see it used anywhere in
> Emacs's code and only once in the packages I have installed.
For a bit of prior art, there are some similar functions in python.el:
python-syntax-context, python-syntax-context-type,
python-syntax-comment-or-string-p, python-syntax-closing-paren-p.
Also, syntax.el has a couple of commented out "XEmacs compatibility
functions": buffer-syntactic-context, buffer-syntactic-context-depth.
I think it would make sense to provide symbolic names for all the
elements returned by syntax-ppss, maybe like this:
(cl-defstruct (syntax-state (:constructor nil)
(:type list))
depth list-start sexp-end
string-start comment quoted
min-depth comment-style comment-or-string-start
open-parens syntax-sequence)
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Sun, 26 Aug 2018 06:31:01 GMT)
Full text and
rfc822 format available.
Message #32 received at submit <at> debbugs.gnu.org (full text, mbox):
On 24.08.2018 21:49, Alex Branham wrote:
>
> On Fri 24 Aug 2018 at 14:28, Eli Zaretskii <eliz <at> gnu.org> wrote:
>
>> That could be because it isn't documented. After all, that was your
>> reason for not knowing about it, right?
>
> Yes, absolutely. I meant I'm ambivalent about its usefulness versus
> syntax-is-comment/string-p, which seem more descriptive to me.
>
>
>
+1
Using here my home-made ar-in-comment-p, ar-in-string-p
But there is another thing, syntax-ppss-context isn't that convenient:
it requires still a check for eq instead of simply delivering a boolean.
Andreas
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Thu, 30 Aug 2018 14:41:01 GMT)
Full text and
rfc822 format available.
Message #35 received at 32504 <at> debbugs.gnu.org (full text, mbox):
> I think it would make sense to provide symbolic names for all the
> elements returned by syntax-ppss, maybe like this:
That makes sense to me.
> (cl-defstruct (syntax-state (:constructor nil)
> (:type list))
> depth list-start sexp-end
> string-start comment quoted
> min-depth comment-style comment-or-string-start
> open-parens syntax-sequence)
I'm not a big fan of the docstrings generated by this, is there a way to
change them to be more helpful? For example, syntax-state-comment looks
like this:
syntax-state-comment is a Lisp function.
(syntax-state-comment CL-X)
This function has a compiler macro ‘syntax-state-comment--cmacro’.
Access slot "comment" of ‘(syntax-state (:constructor nil) (:type list))’ struct CL-X.
But it would be a lot better (in my eyes, anyway) if it looked something
like this, had the -p suffix, and made CL-X default to point:
syntax-state-comment-p is a Lisp function.
(syntax-state-comment-p &optional CL-X)
This function has a compiler macro ‘syntax-state-comment-p--cmacro’.
Return non-nil if the syntax of CL-X is a comment. CL-X defaults to point.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Fri, 31 Aug 2018 01:23:01 GMT)
Full text and
rfc822 format available.
Message #38 received at 32504 <at> debbugs.gnu.org (full text, mbox):
Alex Branham <alex.branham <at> gmail.com> writes:
>> (cl-defstruct (syntax-state (:constructor nil)
>> (:type list))
>> depth list-start sexp-end
>> string-start comment quoted
^^^^^^^^^^^^
That should be string-terminator
>> min-depth comment-style comment-or-string-start
>> open-parens syntax-sequence)
>
> I'm not a big fan of the docstrings generated by this, is there a way to
> change them to be more helpful?
Hmm, doesn't look like it. Well, that's just a convenient way of
defining them all quickly for discussion, it wouldn't be that much
harder to write out defuns I guess.
> But it would be a lot better (in my eyes, anyway) if it looked something
> like this, had the -p suffix, and made CL-X default to point:
> Return non-nil if the syntax of CL-X is a comment.
I don't think it should be a -p predicate, because there is meaningful
distinction between non-nil values:
4. nil if outside a comment, t if inside a non-nestable comment,
else an integer (the current comment nesting).
> CL-X defaults to point.
And, my intention is to operate on the return value of syntax-ppss, not
give a point to call it with. So it could be used as:
(syntax-state-comment (syntax-ppss))
or
(let ((ppss (syntax-ppss)))
(and (syntax-state-comment ppss)
(syntax-state-comment-or-string-start ppss)))
or
(syntax-state-comment (parse-partial-sexp ...))
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Fri, 31 Aug 2018 02:48:01 GMT)
Full text and
rfc822 format available.
Message #41 received at 32504 <at> debbugs.gnu.org (full text, mbox):
On Thu 30 Aug 2018 at 20:21, Noam Postavsky <npostavs <at> gmail.com> wrote:
>> I'm not a big fan of the docstrings generated by this, is there a way to
>> change them to be more helpful?
>
> Hmm, doesn't look like it. Well, that's just a convenient way of
> defining them all quickly for discussion, it wouldn't be that much
> harder to write out defuns I guess.
Fair enough.
> I don't think it should be a -p predicate, because there is meaningful
> distinction between non-nil values:
>
> 4. nil if outside a comment, t if inside a non-nestable comment,
> else an integer (the current comment nesting).
That makes sense, but the -p predicate seems more discoverable to me. We
can always note in the docstring that the actual return value is usable.
I don't have a super strong opinion though.
>> CL-X defaults to point.
>
> And, my intention is to operate on the return value of syntax-ppss, not
> give a point to call it with. So it could be used as:
>
> (syntax-state-comment (syntax-ppss))
>
> or
>
> (let ((ppss (syntax-ppss)))
> (and (syntax-state-comment ppss)
> (syntax-state-comment-or-string-start ppss)))
>
> or
>
> (syntax-state-comment (parse-partial-sexp ...))
Right, but whenever I want to call on these functions, 90% of the time
the question I'm trying to answer is "Is point in a comment/string?" And
the 3rd party ***-is-comment/string-p functions I've looked at usually
(always?) look at positions.
Thanks,
Alex
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Sun, 23 Jun 2019 22:06:01 GMT)
Full text and
rfc822 format available.
Message #44 received at 32504 <at> debbugs.gnu.org (full text, mbox):
Noam Postavsky <npostavs <at> gmail.com> writes:
> Alex Branham <alex.branham <at> gmail.com> writes:
>
>> I got tired of checking/remembering whether (nth 3 (syntax-ppss)) was a
>> string or a comment, so I've created two simple functions that wrap that
>> in a more descriptive name. I've noticed that there are a handful of
>> these that other modes have written, so evidently others wanted them as
>> well at some point.
>
> What about syntax-ppss-context in syntax.el?
I went ahead and documented it; if I read this thread right, it's
sufficient for the use case presented.
--
(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
.
(Sun, 23 Jun 2019 22:06:02 GMT)
Full text and
rfc822 format available.
bug marked as fixed in version 27.1, send any further explanations to
32504 <at> debbugs.gnu.org and Alex Branham <alex.branham <at> gmail.com>
Request was from
Lars Ingebrigtsen <larsi <at> gnus.org>
to
control <at> debbugs.gnu.org
.
(Sun, 23 Jun 2019 22:06:02 GMT)
Full text and
rfc822 format available.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Tue, 09 Jul 2019 00:06:02 GMT)
Full text and
rfc822 format available.
Message #51 received at 32504 <at> debbugs.gnu.org (full text, mbox):
[Message part 1 (text/plain, inline)]
Alex Branham <alex.branham <at> gmail.com> writes:
> On Thu 30 Aug 2018 at 20:21, Noam Postavsky <npostavs <at> gmail.com> wrote:
>
>>> I'm not a big fan of the docstrings generated by this, is there a way to
>>> change them to be more helpful?
>>
>> Hmm, doesn't look like it. Well, that's just a convenient way of
>> defining them all quickly for discussion, it wouldn't be that much
>> harder to write out defuns I guess.
>
> Fair enough.
The recent talk about accessors for decoded time reminded me of this.
Here's an initial patch.
[0001-Add-named-accessors-for-syntax-ppss-state-Bug-32504.patch (text/plain, attachment)]
[Message part 3 (text/plain, inline)]
>>> CL-X defaults to point.
>>
>> And, my intention is to operate on the return value of syntax-ppss, not
>> give a point to call it with.
>
> Right, but whenever I want to call on these functions, 90% of the time
> the question I'm trying to answer is "Is point in a comment/string?" And
> the 3rd party ***-is-comment/string-p functions I've looked at usually
> (always?) look at positions.
Well, for me I just want to stop having to look up the docstring of
parse-partial-sexp every damn time I see (nth X PPSS). But I wouldn't
be against having syntax-is-comment/string-p which take a position as
well.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#32504
; Package
emacs
.
(Thu, 11 Jul 2019 05:12:01 GMT)
Full text and
rfc822 format available.
Message #54 received at 32504 <at> debbugs.gnu.org (full text, mbox):
On 2019-07-08 20:05 -0400, Noam Postavsky wrote:
> Well, for me I just want to stop having to look up the docstring of
> parse-partial-sexp every damn time I see (nth X PPSS). But I wouldn't
> be against having syntax-is-comment/string-p which take a position as
> well.
syntax-ppss-context looks better. It is there for a long time and I have
personally used it many times (13 times in one of my projects).
Introduce a new set seems overkill at this point.
BTW, please don't name functions combining -is- and -p. They sound odd.
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Thu, 08 Aug 2019 11:24:04 GMT)
Full text and
rfc822 format available.
This bug report was last modified 5 years and 136 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.