diff options
| author | Juri Linkov | 2024-04-14 19:18:31 +0300 |
|---|---|---|
| committer | Juri Linkov | 2024-04-14 19:22:01 +0300 |
| commit | 568c1741352a4932508fbbd474b9fd9ebe90ddfb (patch) | |
| tree | ee663e9acb86a02a742f83214ddc9a6b3524380a | |
| parent | cd113d8c45ccf3bfa8b687c06a5d03618adf7a2c (diff) | |
| download | emacs-568c1741352a4932508fbbd474b9fd9ebe90ddfb.tar.gz emacs-568c1741352a4932508fbbd474b9fd9ebe90ddfb.zip | |
Add 'forward-sexp-default-function' to be used by 'treesit-forward-sexp'
* lisp/emacs-lisp/lisp.el (forward-sexp-default-function):
New function with body from 'forward-sexp' (bug#68993).
(forward-sexp-function): Change the default value from nil to
'forward-sexp-default-function'.
(forward-sexp): Use either 'forward-sexp-function' or
'forward-sexp-default-function'.
* lisp/treesit.el (treesit-forward-sexp): In nodes of type 'text'
fall back to 'forward-sexp-default-function'. Improve docstring.
* doc/lispref/positions.texi (List Motion): Fix pxref.
| -rw-r--r-- | doc/lispref/positions.texi | 4 | ||||
| -rw-r--r-- | etc/NEWS | 4 | ||||
| -rw-r--r-- | lisp/emacs-lisp/lisp.el | 14 | ||||
| -rw-r--r-- | lisp/treesit.el | 20 |
4 files changed, 30 insertions, 12 deletions
diff --git a/doc/lispref/positions.texi b/doc/lispref/positions.texi index 5e0143c7131..9193c1063d1 100644 --- a/doc/lispref/positions.texi +++ b/doc/lispref/positions.texi | |||
| @@ -892,8 +892,8 @@ parser information to move across syntax constructs. Since what | |||
| 892 | exactly is considered a sexp varies between languages, a major mode | 892 | exactly is considered a sexp varies between languages, a major mode |
| 893 | should set @code{treesit-thing-settings} to determine that. Then | 893 | should set @code{treesit-thing-settings} to determine that. Then |
| 894 | the mode can get navigation-by-sexp functionality for free, by using | 894 | the mode can get navigation-by-sexp functionality for free, by using |
| 895 | @code{forward-sexp} and @code{backward-sexp}(@pxref{Moving by | 895 | @code{forward-sexp} and @code{backward-sexp}(@pxref{Expressions, |
| 896 | Sentences,,, emacs, The extensible self-documenting text editor}). | 896 | ,, emacs, The extensible self-documenting text editor}). |
| 897 | 897 | ||
| 898 | @node Skipping Characters | 898 | @node Skipping Characters |
| 899 | @subsection Skipping Characters | 899 | @subsection Skipping Characters |
| @@ -2103,6 +2103,10 @@ All tree-sitter enabled modes that define 'sentence' in | |||
| 2103 | 2103 | ||
| 2104 | ** Functions and variables to move by program sexps | 2104 | ** Functions and variables to move by program sexps |
| 2105 | 2105 | ||
| 2106 | *** New function 'forward-sexp-default-function'. | ||
| 2107 | The previous implementation of 'forward-sexp' is moved into its | ||
| 2108 | own function, to be bound by 'forward-sexp-function'. | ||
| 2109 | |||
| 2106 | *** New function 'treesit-forward-sexp'. | 2110 | *** New function 'treesit-forward-sexp'. |
| 2107 | Tree-sitter conditionally sets 'forward-sexp-function' for major modes | 2111 | Tree-sitter conditionally sets 'forward-sexp-function' for major modes |
| 2108 | that have defined 'sexp' in 'treesit-thing-settings' to enable | 2112 | that have defined 'sexp' in 'treesit-thing-settings' to enable |
diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index c57b1357f63..bd0b38db7ea 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el | |||
| @@ -45,7 +45,12 @@ This affects `insert-parentheses' and `insert-pair'." | |||
| 45 | :type 'boolean | 45 | :type 'boolean |
| 46 | :group 'lisp) | 46 | :group 'lisp) |
| 47 | 47 | ||
| 48 | (defvar forward-sexp-function nil | 48 | (defun forward-sexp-default-function (&optional arg) |
| 49 | "Default function for `forward-sexp-function'." | ||
| 50 | (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) | ||
| 51 | (if (< arg 0) (backward-prefix-chars))) | ||
| 52 | |||
| 53 | (defvar forward-sexp-function #'forward-sexp-default-function | ||
| 49 | ;; FIXME: | 54 | ;; FIXME: |
| 50 | ;; - for some uses, we may want a "sexp-only" version, which only | 55 | ;; - for some uses, we may want a "sexp-only" version, which only |
| 51 | ;; jumps over a well-formed sexp, rather than some dwimish thing | 56 | ;; jumps over a well-formed sexp, rather than some dwimish thing |
| @@ -74,10 +79,9 @@ report errors as appropriate for this kind of usage." | |||
| 74 | "No next sexp" | 79 | "No next sexp" |
| 75 | "No previous sexp")))) | 80 | "No previous sexp")))) |
| 76 | (or arg (setq arg 1)) | 81 | (or arg (setq arg 1)) |
| 77 | (if forward-sexp-function | 82 | (funcall (or forward-sexp-function |
| 78 | (funcall forward-sexp-function arg) | 83 | #'forward-sexp-default-function) |
| 79 | (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) | 84 | arg))) |
| 80 | (if (< arg 0) (backward-prefix-chars))))) | ||
| 81 | 85 | ||
| 82 | (defun backward-sexp (&optional arg interactive) | 86 | (defun backward-sexp (&optional arg interactive) |
| 83 | "Move backward across one balanced expression (sexp). | 87 | "Move backward across one balanced expression (sexp). |
diff --git a/lisp/treesit.el b/lisp/treesit.el index 1443162f79c..2973aba771c 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -2138,14 +2138,24 @@ however, smaller in scope than sentences. This is used by | |||
| 2138 | (defun treesit-forward-sexp (&optional arg) | 2138 | (defun treesit-forward-sexp (&optional arg) |
| 2139 | "Tree-sitter implementation for `forward-sexp-function'. | 2139 | "Tree-sitter implementation for `forward-sexp-function'. |
| 2140 | 2140 | ||
| 2141 | ARG is described in the docstring of `forward-sexp-function'. If | 2141 | ARG is described in the docstring of `forward-sexp-function'. |
| 2142 | there are no further sexps to move across, signal `scan-error' | 2142 | |
| 2143 | like `forward-sexp' does. If point is already at top-level, | 2143 | If point is inside a text environment where tree-sitter is not |
| 2144 | return nil without moving point." | 2144 | supported, go forward a sexp using `forward-sexp-default-function'. |
| 2145 | If point is inside code, use tree-sitter functions with the | ||
| 2146 | following behavior. If there are no further sexps to move across, | ||
| 2147 | signal `scan-error' like `forward-sexp' does. If point is already | ||
| 2148 | at top-level, return nil without moving point. | ||
| 2149 | |||
| 2150 | What constitutes as text and source code sexp is determined | ||
| 2151 | by `text' and `sexp' in `treesit-thing-settings'." | ||
| 2145 | (interactive "^p") | 2152 | (interactive "^p") |
| 2146 | (let ((arg (or arg 1)) | 2153 | (let ((arg (or arg 1)) |
| 2147 | (pred (or treesit-sexp-type-regexp 'sexp))) | 2154 | (pred (or treesit-sexp-type-regexp 'sexp))) |
| 2148 | (or (if (> arg 0) | 2155 | (or (when (treesit-node-match-p (treesit-node-at (point)) 'text t) |
| 2156 | (funcall #'forward-sexp-default-function arg) | ||
| 2157 | t) | ||
| 2158 | (if (> arg 0) | ||
| 2149 | (treesit-end-of-thing pred (abs arg) 'restricted) | 2159 | (treesit-end-of-thing pred (abs arg) 'restricted) |
| 2150 | (treesit-beginning-of-thing pred (abs arg) 'restricted)) | 2160 | (treesit-beginning-of-thing pred (abs arg) 'restricted)) |
| 2151 | ;; If we couldn't move, we should signal an error and report | 2161 | ;; If we couldn't move, we should signal an error and report |