[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#32504: [PATCH] syntax-is-{comment|string}-p
From: |
Alex Branham |
Subject: |
bug#32504: [PATCH] syntax-is-{comment|string}-p |
Date: |
Wed, 22 Aug 2018 15:05:35 -0500 |
User-agent: |
mu4e 1.0; emacs 26.1 |
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@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
Description: Text Data
0002-Prefer-using-new-syntax-is-comment-string-p-function.patch
Description: Text Data
- bug#32504: [PATCH] syntax-is-{comment|string}-p,
Alex Branham <=
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Noam Postavsky, 2018/08/22
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Alex Branham, 2018/08/23
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Eli Zaretskii, 2018/08/23
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Alex Branham, 2018/08/24
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Eli Zaretskii, 2018/08/24
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Alex Branham, 2018/08/24
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Andreas Röhler, 2018/08/26
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Noam Postavsky, 2018/08/24
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Alex Branham, 2018/08/30
- bug#32504: [PATCH] syntax-is-{comment|string}-p, Noam Postavsky, 2018/08/30