>From 393f55de705513f5514a3e0250d81053e838c447 Mon Sep 17 00:00:00 2001 From: Alex Branham 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