Subject: bug#77256: Treesit language-at-point

From: Stefan Monnier <[email-masked]>
Date: Wed, 09 Apr 2025 11:02:28 -0400

>> Is it?
>> I can't find any code out there that seems to care what is its default
>> value (all users set it but doesn't look at it, AFAICT).
>
> This case looks problematic:
>
>                  ;; LANG can be nil.  We don't want to use the fallback
>                  ;; in `treesit-language-at', so here we call
>                  ;; `treesit-language-at-point-function' directly.
>                  (let* ((lang (and treesit-language-at-point-function
>                                    (funcall treesit-language-at-point-function
>                                             pos)))

Ah, indeed you're right.

> But I believe this whole 'let*' can be replaced by just
> the 'treesit-parsers-at' call.  I hope Yuan could confirm this.

[ Not familiar enough with this code, so I'll let Yuan chime in.  ]

> If no more code outside relies on the default value,
> the default function could be added like this:
[...]
> -  (if treesit-language-at-point-function
> -      (funcall treesit-language-at-point-function position)
> -    (treesit-parser-language
> -     (car (treesit-parsers-at position)))))
> +  (funcall treesit-language-at-point-function position))

Usually, I use something like:

    (funcall (or treesit-language-at-point-function
                 #'treesit-language-at-point-default)
             position)

when I make such a change, so as to preserve some compatibility with
code that sets the var to nil.

> BTW, I noticed now that indentation of the default value
> in defvar-local is incorrect and differs from defvar and defcustom.
> Here is the fix:
>
> diff --git a/lisp/subr.el b/lisp/subr.el
> index be847aab28a..164ed2a6f63 100644
> --- a/lisp/subr.el
> +++ b/lisp/subr.el
> @@ -197,7 +197,7 @@ defvar-local
>  Like `defvar' but additionally marks the variable as being automatically
>  buffer-local wherever it is set.
>  \n(fn SYMBOL &optional VALUE DOCSTRING)"
> -  (declare (debug defvar) (doc-string 3) (indent 2))
> +  (declare (debug defvar) (doc-string 3) (indent defun))
>    ;; Can't use backquote here, it's too early in the bootstrap.
>    (let ((value (car-safe args))
>          (docstring (car-safe (cdr-safe args))))

Good call, feel free to push, thanks.

        Stefan