Subject: bug#76676: 30.1.50; C-M-b in c-ts-mode moves too far

From: Juri Linkov <[email-masked]>
Date: Mon, 17 Mar 2025 19:56:20 +0200

> Now type C-M-b.  The expected result is that point moves to the
> corresponding opening brace, as shown below:
>
> [...]
>
> But in fact, point moves to the first character of 'if' before that:
>
>   if ((0 < scroll_conservatively

IME half the time C-M-b needs to move to the opening brace,
and other half the time to the first character of 'if'.

When need to operate on the block enclosed inside braces, then
C-M-b is expected to move where it moved traditionally in c-mode.

But often need to take advantage of the tree-sitter grammars
and to operate directly on its nodes, e.g. select the whole
'if' condition with its body to move to another place.

We still need to address this discrepancy between these two modes.
So I tried to create a parallel set of commands: one of them
using the current sexp/list navigation, and another using
any treesit nodes.  But the problem that this requires
more duplication such as adding treesit-mark-named-thing
to work like mark-sexp, etc.

Then I realized it should be sufficient to have just one
very short command to toggle the current mode.  And here it is:

  (defun treesit-toggle-named-thing ()
    (interactive)
    (setq-local treesit-sexp-type-regexp
      (unless treesit-sexp-type-regexp #'treesit-node-named)))

For the example above it toggles between moving to the
first character of 'if' and moving to the opening brace.