Subject: Re: bug#76784: 31.0.50; up-list works incorrectly on emacs-30 if forward-sexp-function is set to treesit-forward-sexp

From: Juri Linkov <[email-masked]>
Date: Thu, 06 Mar 2025 19:53:49 +0200

> The bug is reproducible for clojure-ts-mode. If "sexp" thing is defined for
> tree-sitter mode, forward-sexp-function is set to treesit-forward-sexp
> (https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/treesit.el?h=emacs-30#n3147)
>
> This setting triggers condition in the up-list function
> (https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/emacs-lisp/lisp.el?h=emacs-30#n258)
>
> If point is outside of a top-level sexp, up-list function calls
> forward-list until point position reaches the bottom of the buffer.
>
> Expected behavior: point stays at the end of sexp and message "At the top
> level" is shown in the echo area.
>
> On master branch this issue can be fixed by setting up-list-function to
> treesit-up-list, but on emacs-30 up-list-function doesn't exist.

Thanks for the bug report.  This can be fixed for Emacs 31 (i.e. current
master) by such patch.  Also please note that treesit-transpose-sexps
is working again on Emacs 31.

> Would it be possible to fix the issue in the next bugfix release (if it's
> planned)?

You could try to convince our maintainers to consider backporting the fix
from master to the release branch.

@@ -918,10 +918,16 @@
     "unquote_splicing_lit" "unquoting_lit")
   "A regular expression that matches nodes that can be treated as s-expressions.")
 
+(defconst clojure-ts--list-nodes
+  '("list_lit" "anon_fn_lit" "read_cond_lit" "splicing_read_cond_lit"
+    "map_lit" "ns_map_lit" "vec_lit" "set_lit")
+  "A regular expression that matches nodes that can be treated as lists.")
+
 (defconst clojure-ts--thing-settings
   `((clojure
-     (sexp ,(regexp-opt clojure-ts--sexp-nodes)
-           text ,(regexp-opt '("comment"))))))
+     (sexp ,(regexp-opt clojure-ts--sexp-nodes))
+     (list ,(regexp-opt clojure-ts--list-nodes))
+     (text ,(regexp-opt '("comment"))))))
 
 (defvar clojure-ts-mode-map
   (let ((map (make-sparse-keymap)))
@@ -1043,7 +1049,8 @@
       ;; Workaround for treesit-transpose-sexps not correctly working with
       ;; treesit-thing-settings on Emacs 30.
       ;; Once treesit-transpose-sexps it working again this can be removed
-      (when (fboundp 'transpose-sexps-default-function)
+      (when (and (fboundp 'transpose-sexps-default-function)
+                 (< emacs-major-version 31))
         (setq-local transpose-sexps-function #'transpose-sexps-default-function)))))
 
 ;; For Emacs 30+, so that `clojure-ts-mode' is treated as deriving from