From 81fd5603ce701a0acae096314c1f7ab1db69c64f Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 15 Jun 2021 16:50:51 +0200 Subject: [PATCH] Add a new function syntax-class-to-char * doc/lispref/syntax.texi (Syntax Table Internals): Document it. * src/syntax.c (Fsyntax_class_to_char): New function (bug#37452). --- doc/lispref/syntax.texi | 5 +++++ etc/NEWS | 5 +++++ src/syntax.c | 18 ++++++++++++++++++ test/src/syntax-tests.el | 11 +++++++++++ 4 files changed, 39 insertions(+) diff --git a/doc/lispref/syntax.texi b/doc/lispref/syntax.texi index bde7075b0df..deec3f44c08 100644 --- a/doc/lispref/syntax.texi +++ b/doc/lispref/syntax.texi @@ -1047,6 +1047,11 @@ Given a syntax descriptor @var{desc} (a string), this function returns the corresponding raw syntax descriptor. @end defun +@defun syntax-class-to-char syntax +Given a raw syntax descriptor @var{syntax} (an integer), this function +returns the corresponding syntax descriptor (a character). +@end defun + @defun syntax-after pos This function returns the raw syntax descriptor for the character in the buffer after position @var{pos}, taking account of syntax diff --git a/etc/NEWS b/etc/NEWS index 367cd5972ac..60f3172041a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2843,6 +2843,11 @@ customize them. * Lisp Changes in Emacs 28.1 ++++ +** New function 'syntax-class-to-char'. +This does the almost the opposite of 'string-to-syntax' -- it returns +the syntax descriptor (a character) given a raw syntax descriptor. + +++ ** New function 'buffer-local-boundp'. This predicate says whether a symbol is bound in a specific buffer. diff --git a/src/syntax.c b/src/syntax.c index 9fbf88535f3..7bba336744a 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -1109,6 +1109,23 @@ this is probably the wrong function to use, because it can't take return make_fixnum (syntax_code_spec[SYNTAX (char_int)]); } +DEFUN ("syntax-class-to-char", Fsyntax_class_to_char, + Ssyntax_class_to_char, 1, 1, 0, + doc: /* Return the syntax char of CLASS, described by an integer. +For example, if SYNTAX is word constituent (the integer 2), the +character `w' (119) is returned. */) + (Lisp_Object syntax) +{ + int syn; + CHECK_FIXNUM (syntax); + syn = XFIXNUM (syntax); + + if (syn < 0 || syn >= sizeof syntax_code_spec) + args_out_of_range (make_fixnum (sizeof syntax_code_spec - 1), + syntax); + return make_fixnum (syntax_code_spec[syn]); +} + DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0, doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */) (Lisp_Object character) @@ -3782,6 +3799,7 @@ In both cases, LIMIT bounds the search. */); defsubr (&Scopy_syntax_table); defsubr (&Sset_syntax_table); defsubr (&Schar_syntax); + defsubr (&Ssyntax_class_to_char); defsubr (&Smatching_paren); defsubr (&Sstring_to_syntax); defsubr (&Smodify_syntax_entry); diff --git a/test/src/syntax-tests.el b/test/src/syntax-tests.el index 479b818935f..e4e3054d37a 100644 --- a/test/src/syntax-tests.el +++ b/test/src/syntax-tests.el @@ -21,6 +21,7 @@ (require 'ert) (require 'ert-x) +(require 'cl-lib) (ert-deftest parse-partial-sexp-continue-over-comment-marker () "Continue a parse that stopped in the middle of a comment marker." @@ -56,6 +57,16 @@ (should (equal (parse-partial-sexp aftC pointX nil nil pps-aftC) ppsX))))) +(ert-deftest syntax-class-character-test () + (cl-loop for char across " .w_()'\"$\\/<>@!|" + for i from 0 + do (should (= char (syntax-class-to-char i))) + when (string-to-syntax (string char)) + do (should (= char (syntax-class-to-char + (car (string-to-syntax (string char))))))) + (should-error (syntax-class-to-char -1)) + (should-error (syntax-class-to-char 200))) + (ert-deftest parse-partial-sexp-paren-comments () "Test syntax parsing with paren comment markers. Specifically, where the first character of the comment marker is -- 2.39.5