From d005e685e1df7692085378633348db39a5190374 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Sun, 16 Apr 2023 20:33:51 -0700 Subject: [PATCH] New helper function assq_no_signal * src/fns.c (assq_no_signal): New function. * src/lisp.h (assoc_no_signal): Declare it. * src/treesit.c (safe_assq): Remove function. (treesit_traverse_get_predicate): Change safe_assq to assq_no_signal. --- src/fns.c | 14 ++++++++++++++ src/lisp.h | 1 + src/treesit.c | 15 ++------------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/fns.c b/src/fns.c index e92ef7e4c81..75acb6f4598 100644 --- a/src/fns.c +++ b/src/fns.c @@ -1966,6 +1966,20 @@ assq_no_quit (Lisp_Object key, Lisp_Object alist) return Qnil; } +/* Assq but doesn't signal. Unlike assq_no_quit, this function still + detect circular lists; like assq_no_quit, this function does not + allow quits and never signals. If anything goes wrong, it returns + Qnil. */ +Lisp_Object +assq_no_signal (Lisp_Object key, Lisp_Object alist) +{ + Lisp_Object tail = alist; + FOR_EACH_TAIL_SAFE (tail) + if (CONSP (XCAR (tail)) && EQ (XCAR (XCAR (tail)), key)) + return XCAR (tail); + return Qnil; +} + DEFUN ("assoc", Fassoc, Sassoc, 2, 3, 0, doc: /* Return non-nil if KEY is equal to the car of an element of ALIST. The value is actually the first element of ALIST whose car equals KEY. diff --git a/src/lisp.h b/src/lisp.h index 4e17e369312..ab66109d5df 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4031,6 +4031,7 @@ extern Lisp_Object concat3 (Lisp_Object, Lisp_Object, Lisp_Object); extern bool equal_no_quit (Lisp_Object, Lisp_Object); extern Lisp_Object nconc2 (Lisp_Object, Lisp_Object); extern Lisp_Object assq_no_quit (Lisp_Object, Lisp_Object); +extern Lisp_Object assq_no_signal (Lisp_Object, Lisp_Object); extern Lisp_Object assoc_no_quit (Lisp_Object, Lisp_Object); extern void clear_string_char_byte_cache (void); extern ptrdiff_t string_char_to_byte (Lisp_Object, ptrdiff_t); diff --git a/src/treesit.c b/src/treesit.c index d2dd83b29fe..48abbc5fb4e 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -3153,17 +3153,6 @@ treesit_traverse_child_helper (TSTreeCursor *cursor, } } -/* Assq but doesn't signal. */ -static Lisp_Object -safe_assq (Lisp_Object key, Lisp_Object alist) -{ - Lisp_Object tail = alist; - FOR_EACH_TAIL_SAFE (tail) - if (CONSP (XCAR (tail)) && EQ (XCAR (XCAR (tail)), key)) - return XCAR (tail); - return Qnil; -} - /* Given a symbol THING, and a language symbol LANGUAGE, find the corresponding predicate definition in treesit-things-settings. Don't check for the type of THING and LANGUAGE. @@ -3172,11 +3161,11 @@ safe_assq (Lisp_Object key, Lisp_Object alist) static Lisp_Object treesit_traverse_get_predicate (Lisp_Object thing, Lisp_Object language) { - Lisp_Object cons = safe_assq (language, Vtreesit_thing_settings); + Lisp_Object cons = assq_no_quit (language, Vtreesit_thing_settings); if (NILP (cons)) return Qnil; Lisp_Object definitions = XCDR (cons); - Lisp_Object entry = safe_assq (thing, definitions); + Lisp_Object entry = assq_no_quit (thing, definitions); if (NILP (entry)) return Qnil; /* ENTRY looks like (THING PRED). */ -- 2.39.2