1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-10 16:20:17 -08:00

Add new variable 'down-list-function' for 'treesit-down-list'

* lisp/emacs-lisp/lisp.el (down-list-default-function): New function.
(down-list-function): New variable (bug#73404).
(down-list): Move meat to 'down-list-default-function',
and call 'down-list-function' when non-nil.  Don't raise an error
in strings or comments when 'down-list-function' is non-nil.

* lisp/treesit.el (treesit--scan-error): New internal function.
(treesit-forward-sexp, treesit-forward-list): Use 'treesit--scan-error'.
(treesit-down-list): New function.
(treesit-major-mode-setup): Set 'down-list-function' to
'treesit-down-list'.
This commit is contained in:
Juri Linkov 2024-12-29 19:51:18 +02:00
parent 3db984c72b
commit 3c50edb2b5
3 changed files with 60 additions and 20 deletions

View file

@ -187,6 +187,17 @@ report errors as appropriate for this kind of usage."
(or arg (setq arg 1))
(forward-list (- arg) interactive))
(defun down-list-default-function (&optional arg)
"Default function for `down-list-function'."
(let ((inc (if (> arg 0) 1 -1)))
(while (/= arg 0)
(goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
(setq arg (- arg inc)))))
(defvar down-list-function nil
"If non-nil, `down-list' delegates to this function.
Should take the same arguments and behave similarly to `down-list'.")
(defun down-list (&optional arg interactive)
"Move forward down one level of parentheses.
This command will also work on other parentheses-like expressions
@ -194,20 +205,21 @@ defined by the current language mode.
With ARG, do this that many times.
A negative argument means move backward but still go down a level.
This command assumes point is not in a string or comment.
Calls `down-list-function' to do the work, if that is non-nil.
If INTERACTIVE is non-nil, as it is interactively,
report errors as appropriate for this kind of usage."
(interactive "^p\nd")
(when (ppss-comment-or-string-start (syntax-ppss))
(when (and (null down-list-function)
(ppss-comment-or-string-start (syntax-ppss)))
(user-error "This command doesn't work in strings or comments"))
(if interactive
(condition-case _
(down-list arg nil)
(scan-error (user-error "At bottom level")))
(or arg (setq arg 1))
(let ((inc (if (> arg 0) 1 -1)))
(while (/= arg 0)
(goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
(setq arg (- arg inc))))))
(if down-list-function
(funcall down-list-function arg)
(down-list-default-function arg))))
(defun backward-up-list (&optional arg escape-strings no-syntax-crossing)
"Move backward out of one level of parentheses.