Subject: 30.0.50; Minor search_buffer cleanup

From: Basil Contovounesios <[email-masked]>
Date: Tue, 13 Jun 2023 18:30:43 +0100

Severity: minor
Tags: patch

I noticed what used to be commentary for search_buffer slowly drifted
away from the function it was describing.

I brought the two together again, updating the commentary to reflect the
function's current interface.

In the process I noticed the RE argument to search_buffer and
search_command was always used as a boolean flag, so changed its type
accordingly.

WDYT?

Thanks,

-- 
Basil

>From 8801c3179139eac8049427fbf10971aef57ba11f Mon Sep 17 00:00:00 2001
From: "Basil L. Contovounesios" <[email-masked]>
Date: Sat, 3 Jun 2023 14:46:19 +0100
Subject: [PATCH] Minor search_buffer cleanup

* src/search.c: Move commentary that used to precede search_buffer
to that location once more.  Update it for the current arglist.
(search_command, search_buffer_non_re, search_buffer):
* src/lisp.h (search_buffer): Turn RE flag/argument from an int into
a bool.  All callers updated.
---
 src/lisp.h    |  2 +-
 src/search.c  | 50 ++++++++++++++++++++++++++------------------------
 src/treesit.c |  2 +-
 3 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/src/lisp.h b/src/lisp.h
index 2978de962d9..44af2ef8a2a 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -4809,7 +4809,7 @@ fast_c_string_match_ignore_case (Lisp_Object regexp,
 					   ptrdiff_t, ptrdiff_t *);
 extern EMACS_INT search_buffer (Lisp_Object, ptrdiff_t, ptrdiff_t,
 				ptrdiff_t, ptrdiff_t, EMACS_INT,
-				int, Lisp_Object, Lisp_Object, bool);
+				bool, Lisp_Object, Lisp_Object, bool);
 extern void syms_of_search (void);
 extern void clear_regexp_cache (void);
 
diff --git a/src/search.c b/src/search.c
index 0bb52c03eef..a3b6fa8bb85 100644
--- a/src/search.c
+++ b/src/search.c
@@ -1027,7 +1027,7 @@ find_before_next_newline (ptrdiff_t from, ptrdiff_t to,
 
 static Lisp_Object
 search_command (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror,
-		Lisp_Object count, int direction, int RE, bool posix)
+		Lisp_Object count, int direction, bool RE, bool posix)
 {
   EMACS_INT np;
   EMACS_INT lim;
@@ -1130,21 +1130,6 @@ trivial_regexp_p (Lisp_Object regexp)
   return 1;
 }
 
-/* Search for the n'th occurrence of STRING in the current buffer,
-   starting at position POS and stopping at position LIM,
-   treating STRING as a literal string if RE is false or as
-   a regular expression if RE is true.
-
-   If N is positive, searching is forward and LIM must be greater than POS.
-   If N is negative, searching is backward and LIM must be less than POS.
-
-   Returns -x if x occurrences remain to be found (x > 0),
-   or else the position at the beginning of the Nth occurrence
-   (if searching backward) or the end (if searching forward).
-
-   POSIX is nonzero if we want full backtracking (POSIX style)
-   for this pattern.  0 means backtrack only enough to get a valid match.  */
-
 #define TRANSLATE(out, trt, d)			\
 do						\
   {						\
@@ -1308,7 +1293,7 @@ search_buffer_re (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
 static EMACS_INT
 search_buffer_non_re (Lisp_Object string, ptrdiff_t pos,
                       ptrdiff_t pos_byte, ptrdiff_t lim, ptrdiff_t lim_byte,
-                      EMACS_INT n, int RE, Lisp_Object trt, Lisp_Object inverse_trt,
+                      EMACS_INT n, bool RE, Lisp_Object trt, Lisp_Object inverse_trt,
                       bool posix)
 {
   unsigned char *raw_pattern, *pat;
@@ -1507,10 +1492,27 @@ search_buffer_non_re (Lisp_Object string, ptrdiff_t pos,
   return result;
 }
 
+/* Search for the Nth occurrence of STRING in the current buffer,
+   from buffer position POS/POS_BYTE until LIM/LIM_BYTE,
+   treating STRING as a literal string if RE is false
+   or as a regular expression if RE is true.
+
+   If N is positive, searching is forward and LIM must be greater than POS.
+   If N is negative, searching is backward and LIM must be less than POS.
+
+   Returns -x if x occurrences remain to be found (x > 0),
+   or else the position at the beginning of the Nth occurrence
+   (if searching backward) or the end (if searching forward).
+
+   TRT and INVERSE_TRT are translation tables.
+
+   POSIX is true if we want full backtracking (POSIX style) for this pattern.
+   False means backtrack only enough to get a valid match.  */
+
 EMACS_INT
 search_buffer (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
 	       ptrdiff_t lim, ptrdiff_t lim_byte, EMACS_INT n,
-	       int RE, Lisp_Object trt, Lisp_Object inverse_trt, bool posix)
+	       bool RE, Lisp_Object trt, Lisp_Object inverse_trt, bool posix)
 {
   if (running_asynch_code)
     save_search_regs ();
@@ -2219,7 +2221,7 @@ DEFUN ("search-backward", Fsearch_backward, Ssearch_backward, 1, 4,
 See also the functions `match-beginning', `match-end' and `replace-match'.  */)
   (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
-  return search_command (string, bound, noerror, count, -1, 0, 0);
+  return search_command (string, bound, noerror, count, -1, false, false);
 }
 
 DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "MSearch: ",
@@ -2244,7 +2246,7 @@ DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "MSearch: ",
 See also the functions `match-beginning', `match-end' and `replace-match'.  */)
   (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
-  return search_command (string, bound, noerror, count, 1, 0, 0);
+  return search_command (string, bound, noerror, count, 1, false, false);
 }
 
 DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4,
@@ -2260,7 +2262,7 @@ DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4,
 anchor `(elisp) re-search-backward' for details.  */)
   (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
-  return search_command (regexp, bound, noerror, count, -1, 1, 0);
+  return search_command (regexp, bound, noerror, count, -1, true, false);
 }
 
 DEFUN ("re-search-forward", Fre_search_forward, Sre_search_forward, 1, 4,
@@ -2291,7 +2293,7 @@ DEFUN ("re-search-forward", Fre_search_forward, Sre_search_forward, 1, 4,
 and `replace-match'.  */)
   (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
-  return search_command (regexp, bound, noerror, count, 1, 1, 0);
+  return search_command (regexp, bound, noerror, count, 1, true, false);
 }
 
 DEFUN ("posix-search-backward", Fposix_search_backward, Sposix_search_backward, 1, 4,
@@ -2319,7 +2321,7 @@ DEFUN ("posix-search-backward", Fposix_search_backward, Sposix_search_backward,
 and `replace-match'.  */)
   (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
-  return search_command (regexp, bound, noerror, count, -1, 1, 1);
+  return search_command (regexp, bound, noerror, count, -1, true, true);
 }
 
 DEFUN ("posix-search-forward", Fposix_search_forward, Sposix_search_forward, 1, 4,
@@ -2347,7 +2349,7 @@ DEFUN ("posix-search-forward", Fposix_search_forward, Sposix_search_forward, 1,
 and `replace-match'.  */)
   (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
-  return search_command (regexp, bound, noerror, count, 1, 1, 1);
+  return search_command (regexp, bound, noerror, count, 1, true, true);
 }
 
 DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 5, 0,
diff --git a/src/treesit.c b/src/treesit.c
index 0af0e347694..e69cb872a9a 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -2582,7 +2582,7 @@ treesit_predicate_match (Lisp_Object args, struct capture_range captures,
   ZV_BYTE = end_byte;
 
   ptrdiff_t val = search_buffer (regexp, start_pos, start_byte,
-				 end_pos, end_byte, 1, 1, Qnil, Qnil, false);
+				 end_pos, end_byte, 1, true, Qnil, Qnil, false);
 
   BEGV = old_begv;
   BEGV_BYTE = old_begv_byte;
-- 
2.34.1

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.16.0, Xaw3d scroll bars) of 2023-06-13 built on blc
Repository revision: 81932ebcfa56a33fcb1c7d9f91094e2b1f6e9b77
Repository branch: blc/treesit/master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101004
System Description: Ubuntu 22.04.2 LTS

Configured using:
 'configure CC=gcc-12 'CFLAGS=-Og -ggdb3' --prefix=/home/bic/.local
 --with-file-notification=yes --with-x --with-x-toolkit=lucid'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS TREE_SITTER WEBP X11 XAW3D XDBE XIM XINPUT2 XPM
LUCID ZLIB

Important settings:
  value of $LC_MONETARY: en_IE.UTF-8
  value of $LC_NUMERIC: en_IE.UTF-8
  value of $LC_TIME: en_IE.UTF-8
  value of $LANG: en_GB.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date subr-x mm-decode
mm-bodies mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader
cl-loaddefs cl-lib sendmail rfc2047 rfc2045 ietf-drums mm-util
mail-prsvr mail-utils rmc iso-transl tooltip cconv eldoc paren electric
uniquify ediff-hook vc-hooks lisp-float-type elisp-mode mwheel
term/x-win x-win term/common-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch easymenu
timer select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer nadvice seq simple cl-generic
indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite emoji-zwj charscript charprop case-table epa-hook
jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
theme-loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads dbusbind inotify lcms2
dynamic-setting system-font-setting font-render-setting cairo x-toolkit
xinput2 x multi-tty make-network-process emacs)

Memory information:
((conses 16 36787 10186)
 (symbols 48 5178 0)
 (strings 32 13887 2078)
 (string-bytes 1 379585)
 (vectors 16 9301)
 (vector-slots 8 148642 11545)
 (floats 8 23 25)
 (intervals 56 241 0)
 (buffers 984 10))