From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Plug treesit.el into other emacs constructs Date: Mon, 26 Dec 2022 17:46:18 -0500 Message-ID: References: <87wn6whete.fsf@thornhill.no> <04BB786A-3ED1-4918-8583-17AA01A1E453@gmail.com> <4E3940CA-67A6-45B7-8785-4E60FDECCDFB@gmail.com> <4315EFC6-7AA8-4A48-845C-9CA8B88034D9@thornhill.no> <87bko521n0.fsf@thornhill.no> <87359h1ybt.fsf@thornhill.no> <871qp01msi.fsf@thornhill.no> <87v8mczb6b.fsf@thornhill.no> <87sfhgz9s8.fsf@thornhill.no> <87pmckz8p0.fsf@thornhill.no> <5DF07C4E-2CCD-4561-AFFB-D5D81D67BFE0@thornhill.no> <87o7rq7zf2.fsf@thornhill.no> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31012"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Yuan Fu , emacs-devel@gnu.org, eliz@gnu.org To: Theodor Thornhill Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Dec 26 23:47:02 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p9wFK-0007tP-AU for ged-emacs-devel@m.gmane-mx.org; Mon, 26 Dec 2022 23:47:02 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p9wEr-0002Lv-92; Mon, 26 Dec 2022 17:46:33 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p9wEj-0002Lb-QQ for emacs-devel@gnu.org; Mon, 26 Dec 2022 17:46:25 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p9wEh-0008LQ-L4; Mon, 26 Dec 2022 17:46:25 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 7BF3A442EC2; Mon, 26 Dec 2022 17:46:21 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id A230D442EB7; Mon, 26 Dec 2022 17:46:19 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1672094779; bh=arjDjl4G69NRL5FR2Z3FDK0G5EWoyVJOV5m5uGXyu94=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=Awj2XDiRqExnwUFF6TmUA0ilhOH2roSFluyW2uMrgvSS1XcSw/+6Hj5SdBkkToaf5 JzXfufOMDRnF66NTDWTQ5wVd8bNp8t68x1InDzxr4gx38yopaT3MMzvnZcixCa9B2e 0gNs5B3GDQmBDKelfFcVg8QTTyLoP2bbZ8QHvAMFzPjGL8GWg7mfo35Sc4EbCzMeS+ eBL/cslteoORZxtCvf9/j3AQmAZSv7TWMbaebniYxbgYlLOq6KXr4Buo0VQJyU4NpO 6Y7LWWU3aimTP9WScko/QqgBXozgSmVoxAKW/G8PkuLmKYGB+0I4FAFXbD+cMJGEba SmYXcE98GG/Cw== Original-Received: from pastel (unknown [45.72.200.228]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 4E9071201F4; Mon, 26 Dec 2022 17:46:19 -0500 (EST) In-Reply-To: <87o7rq7zf2.fsf@thornhill.no> (Theodor Thornhill's message of "Mon, 26 Dec 2022 20:11:45 +0100") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:301944 Archived-At: > +(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