all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Theodor Thornhill <theo@thornhill.no>
Cc: Yuan Fu <casouri@gmail.com>,  emacs-devel@gnu.org,  eliz@gnu.org
Subject: Re: Plug treesit.el into other emacs constructs
Date: Mon, 26 Dec 2022 17:46:18 -0500	[thread overview]
Message-ID: <jwvo7rp94aw.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87o7rq7zf2.fsf@thornhill.no> (Theodor Thornhill's message of "Mon, 26 Dec 2022 20:11:45 +0100")

> +(defvar-local transpose-sexps-function nil
> +  "If non-nil, `transpose-sexps' delegates to this function.
> +
> +The return value of this function is expected to be a cons of two
> +conses, denoting the positions in the current buffer to be
> +transposed.  If no such pair of positions is available, signal
> +USER-ERROR.")

This docstring needs to tell what args are passed to the function.

I see you make it return a pair of pairs, so it has to handle all the
semantics of `transpose-sexps`.  My intuition told me to go with
a function that returns a pair of positions (i.e. it takes an ARG and
returns the BEG..END of the ARGth sibling).  I suspect it would fit
within `transpose-subr` a bit better.

> +     (if transpose-sexps-function transpose-sexps-function
> +       (lambda (arg)

Aka (or transpose-sexps-function (lambda (arg) ...))
But even better is to put the `lambda` in the default value of the
variable, so you just use `transpose-sexps-function` unconditionally.

> +  (let* ((aux (if special mover
> +	        (lambda (x)
> +		  (cons (progn (funcall mover x) (point))
> +		        (progn (funcall mover (- x)) (point))))))

If `mover` is changed to return a pair of positions, than the above can
just be:

> +  (let* ((aux (if special mover
> +	        (lambda (x)
> +		  (cons (progn (funcall mover x) (point))
> +  		        (progn (funcall mover (- x)) (point))))))


> +	 (pos1 (save-excursion (funcall aux arg)))
> +         pos2)
>      (cond
> +     ((and (consp (car pos1)) (consp (cdr pos1)))
> +      (transpose-subr-1 (car pos1) (cdr pos1)))
>       ((= arg 0)
>        (save-excursion
>  	(setq pos1 (funcall aux 1))
> diff --git a/lisp/treesit.el b/lisp/treesit.el
> index cefbed1a16..9f0965ac68 100644
> --- a/lisp/treesit.el
> +++ b/lisp/treesit.el
> @@ -1582,6 +1582,27 @@ treesit-search-forward-goto
>        (goto-char current-pos)))
>      node))
>  
> +(defun treesit-transpose-sexps (&optional arg)
> +  "Tree-sitter `transpose-sexps' function.
> +Arg is the same as in `transpose-sexps'.
> +
> +Return a pair of positions describing the regions to transpose
> +for use in `transpose-subr' and friends."
> +  (let* ((parent (treesit-node-parent (treesit-node-at (point))))
> +         (child (treesit-node-child parent 0 t)))
> +    (named-let loop ((prev child)
> +                     (next (treesit-node-child
> +                            parent (+ arg (treesit-node-index child t))
> +                            t)))
> +      (if (< (point) (or (treesit-node-end next)
> +                         (user-error "Don't have two things to transpose")))
> +          (cons (cons (treesit-node-start prev)
> +                      (treesit-node-end prev))
> +                (cons (treesit-node-start next)
> +                      (treesit-node-end next)))
> +        (loop (treesit-node-next-sibling prev t)
> +              (treesit-node-next-sibling next t))))))
> +
>  ;;; Navigation, defun, things
>  ;;
>  ;; Emacs lets you define "things" by a regexp that matches the type of
> @@ -2111,7 +2132,8 @@ treesit-major-mode-setup
>    ;; Defun name.
>    (when treesit-defun-name-function
>      (setq-local add-log-current-defun-function
> -                #'treesit-add-log-current-defun)))
> +                #'treesit-add-log-current-defun))
> +  (setq-local transpose-sexps-function #'treesit-transpose-sexps))
>  
>  ;;; Debugging




  reply	other threads:[~2022-12-26 22:46 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-12 14:33 Plug treesit.el into other emacs constructs Theodor Thornhill
2022-12-12 14:45 ` Eli Zaretskii
2022-12-13 18:17   ` Theodor Thornhill
2022-12-12 15:46 ` Stefan Monnier
2022-12-13 18:27   ` Theodor Thornhill
2022-12-13 19:37     ` Stefan Monnier
2022-12-13 19:53       ` Yuan Fu
2022-12-13 20:06         ` Perry Smith
2022-12-13 23:19         ` Stefan Monnier
2022-12-14  8:14           ` Yuan Fu
2022-12-14  8:42             ` Theodor Thornhill
2022-12-14 14:01             ` Stefan Monnier
2022-12-14 16:24               ` Theodor Thornhill
2022-12-14 17:46                 ` Stefan Monnier
2022-12-14 18:07                   ` Theodor Thornhill
2022-12-14 19:25                     ` Stefan Monnier
2022-12-14 19:35                       ` Stefan Monnier
2022-12-14 20:04                       ` Theodor Thornhill
2022-12-14 20:50                         ` Stefan Monnier
2022-12-14 21:15                           ` Theodor Thornhill
2022-12-14 21:34                             ` Stefan Monnier
2022-12-15 19:37                               ` Theodor Thornhill
2022-12-15 19:56                                 ` Stefan Monnier
2022-12-15 20:03                                   ` Theodor Thornhill
2022-12-15 20:33                                     ` Theodor Thornhill
2022-12-15 20:57                                       ` Theodor Thornhill
2022-12-24  7:00                                         ` Eli Zaretskii
2022-12-24  8:44                                           ` Yuan Fu
2022-12-24 14:01                                         ` Stefan Monnier
2022-12-24 14:15                                           ` Theodor Thornhill
2022-12-26 19:11                                             ` Theodor Thornhill
2022-12-26 22:46                                               ` Stefan Monnier [this message]
2022-12-26 22:51                                                 ` Stefan Monnier
2022-12-27 22:15                                                   ` Theodor Thornhill via Emacs development discussions.
2022-12-28  0:12                                                     ` Stefan Monnier
2022-12-28  9:26                                                       ` Theodor Thornhill via Emacs development discussions.
2022-12-28 18:01                                                         ` Stefan Monnier
2022-12-28 18:27                                                           ` Theodor Thornhill
2022-12-26 22:56                                                 ` Theodor Thornhill
2022-12-27 15:46                                   ` Lynn Winebarger
2022-12-14 23:31               ` Yuan Fu
2022-12-15  0:05                 ` Yuan Fu
2022-12-15  7:09                   ` Eli Zaretskii
2022-12-15  7:14                     ` Theodor Thornhill
2022-12-15  4:37                 ` Stefan Monnier
2022-12-15  5:59                 ` Theodor Thornhill
2022-12-15 21:23                   ` Yuan Fu
2022-12-15 21:28                     ` Theodor Thornhill
2022-12-13 20:02       ` Theodor Thornhill
2022-12-13 23:10         ` Stefan Monnier
2022-12-14 23:32   ` Stephen Leake
2022-12-16 10:02     ` Kévin Le Gouguec
2022-12-16 11:54       ` [SPAM UNSURE] " Stephen Leake
2022-12-17 15:30         ` Kévin Le Gouguec

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvo7rp94aw.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=casouri@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=theo@thornhill.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.