Subject: bug#31204: 25.3; Make word motion more customizable

From: Lars Ingebrigtsen <[email-masked]>
Date: Wed, 15 Jun 2022 17:03:16 +0200

Lars Ingebrigtsen <[email-masked]> writes:

>> As an illustration (where ‘|’ specifies word motion stops when going
>> left to right):
>>
>>     foo| ***| +++| (|bar|)|

[...]

> I think this sounds like it could be useful.  If we added such a hook to
> `forward-word', what would the rest of the code look like to make
> `C-<right>' work this way?

I've played a bit at the patch below, but I tend to think that this is
going about things the wrong way.  That is, for something like this to
work meaningfully, it would require a lot of setup (because
Vfind_word_boundary_function_table) would also have to be altered in
conjunction with this.

I.e., it's really about changing the definition of what a "word" is, and
in that case, I think it would be easier to just do that in a syntax
table, and then everything would work automatically.

(Or by advising the functions here.)

So I don't think it'd be worth it to proceed with something like the
below, and I'm therefore closing this bug report.

diff --git a/src/syntax.c b/src/syntax.c
index f9022d18d2..02d4dd4b9a 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -1462,20 +1462,33 @@ scan_words (ptrdiff_t from, EMACS_INT count)
 
   while (count > 0)
     {
-      while (true)
+      if (!NILP (Vfind_word_start_function))
 	{
-	  if (from == end)
+	  Lisp_Object np = call2 (Vfind_word_start_function,
+				  make_fixnum (from), make_fixnum (end));
+	  if (!FIXNUMP (np))
 	    return 0;
-	  UPDATE_SYNTAX_TABLE_FORWARD (from);
+	  from = XFIXNUM (np);
+	  from_byte = CHAR_TO_BYTE (from);
 	  ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
-	  code = SYNTAX (ch0);
-	  inc_both (&from, &from_byte);
-	  if (words_include_escapes
-	      && (code == Sescape || code == Scharquote))
-	    break;
-	  if (code == Sword)
-	    break;
-	  rarely_quit (from);
+	}
+      else
+	{
+	  while (true)
+	    {
+	      if (from == end)
+		return 0;
+	      UPDATE_SYNTAX_TABLE_FORWARD (from);
+	      ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
+	      code = SYNTAX (ch0);
+	      inc_both (&from, &from_byte);
+	      if (words_include_escapes
+		  && (code == Sescape || code == Scharquote))
+		break;
+	      if (code == Sword)
+		break;
+	      rarely_quit (from);
+	    }
 	}
       /* Now CH0 is a character which begins a word and FROM is the
          position of the next character.  */
@@ -3792,6 +3805,12 @@ syms_of_syntax (void)
 In both cases, LIMIT bounds the search. */);
   Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
 
+  DEFVAR_LISP ("find-word-start-function",
+	       Vfind_word_start_function,
+	       doc: /* Function called to find the start of a word.
+It's called with two parameters, POS and LIMIT.  */);
+  Vfind_word_start_function = Qnil;
+
   DEFVAR_BOOL ("comment-end-can-be-escaped", comment_end_can_be_escaped,
                doc: /* Non-nil means an escaped ender inside a comment doesn't end the comment.  */);
   comment_end_can_be_escaped = false;

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no