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.
Full log
View this message in rfc822 format
[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)]
This bug report was last modified 5 years and 313 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.