From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#73404: 30.0.50; [forward/kill/etc]-sexp commands do not behave as expected in tree-sitter modes Date: Fri, 27 Dec 2024 09:54:11 +0200 Organization: LINKOV.NET Message-ID: <87h66pr1qc.fsf@mail.linkov.net> References: <87plox4mtp.fsf@masteringemacs.org> <87frpm20t7.fsf@masteringemacs.org> <8634lmbs8t.fsf@gnu.org> <87bk0a1u0o.fsf@masteringemacs.org> <86tte2a5o3.fsf@gnu.org> <877cay1lqt.fsf@masteringemacs.org> <86frpma06f.fsf@gnu.org> <86ikueiekp.fsf@mail.linkov.net> <86ed4zg1cc.fsf@mail.linkov.net> <87zflac68t.fsf@mail.linkov.net> <87jzcdlxdp.fsf@mail.linkov.net> <87o71jocgs.fsf@mail.linkov.net> <87wmfwqg7e.fsf@mail.linkov.net> <87ikr8hpdu.fsf@mail.linkov.net> <87y103wsji.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31154"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) Cc: Mickey Petersen , Yuan Fu , Theodor Thornhill , Eli Zaretskii , 73404@debbugs.gnu.org To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Dec 27 09:28:20 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1tR5hk-0007wE-5u for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 27 Dec 2024 09:28:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tR5hV-00027L-Ca; Fri, 27 Dec 2024 03:28:05 -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 1tR5hT-00026y-3R for bug-gnu-emacs@gnu.org; Fri, 27 Dec 2024 03:28:03 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tR5hS-0004Zy-PG for bug-gnu-emacs@gnu.org; Fri, 27 Dec 2024 03:28:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=T7surxhzT5Vv0YFk088vyRvwQVObuYEzeVsjYGQ7HkU=; b=X2+cWwAv0U6pLSLX/jo/mkCOzV1TWoMweYnrlXVBtnuyma39PTaPXo+2V2u8m4iu0keSRy2kusWm7wQSZoPjpR3VNQm4Mqlbr884NUB07Uy1M/2arCSllgIXEq3RLNKdm6liua3eR9LKsn9x/AZ8kSY3oV5/Brbu78RdgESmtul5itZXbL1L6dAXS3M5VO9MM3DgETZ4QGeSCEaHjVaJUgGrJcRPoNDNaP6n978r0Wy4MKHW+1mGXX1J3bMT1YW2lbf8CWtuWft9+A9YcFTF8gQql9WcSGDZqpZrWm4I+f/a4h6hWBNC3Uv1KbNDLhOTXcjxSQhDZySGt55Q3ePNTw==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tR5hS-000365-ET for bug-gnu-emacs@gnu.org; Fri, 27 Dec 2024 03:28:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 27 Dec 2024 08:28:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 73404 X-GNU-PR-Package: emacs Original-Received: via spool by 73404-submit@debbugs.gnu.org id=B73404.173528806511871 (code B ref 73404); Fri, 27 Dec 2024 08:28:02 +0000 Original-Received: (at 73404) by debbugs.gnu.org; 27 Dec 2024 08:27:45 +0000 Original-Received: from localhost ([127.0.0.1]:44604 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tR5hA-00035O-Aa for submit@debbugs.gnu.org; Fri, 27 Dec 2024 03:27:44 -0500 Original-Received: from relay1-d.mail.gandi.net ([217.70.183.193]:47813) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tR5h6-000356-RI for 73404@debbugs.gnu.org; Fri, 27 Dec 2024 03:27:42 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id C992A240002; Fri, 27 Dec 2024 08:27:32 +0000 (UTC) In-Reply-To: (Stefan Monnier's message of "Wed, 25 Dec 2024 14:29:19 -0500") X-GND-Sasl: juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:297813 Archived-At: --=-=-= Content-Type: text/plain >>> Gentle reminder that `forward-sexp` is not a "list-navigation" function. >>> That would be `forward-list`. We very often use sexp commands and >>> functions to manipulate non-lists such as identifiers. >> Do you think it would be better to override low-level functions 'scan-lists' >> and 'scan-sexps' with new variables like 'scan-lists-function' >> and 'scan-sexps-function', instead of adding more variables for >> overriding top-level commands such as a new variable 'forward-list-function' >> and 'down-list-function', like the existing 'forward-sexp-function'? > > Don't know. I tried to override `scan-lists` and `scan-sexps` with advices (a proof of concept attached below), and it works nicely. For example, `C-M-d` moves down to the HTML element in html-ts-mode. But then I realized there are not too many places where such overriding might be useful. In fact, there is only 1 place in `show-paren--default` that uses `scan-sexps`. But even this occurrence can't be used, so I created `treesit-show-paren-data` for `show-paren-data-function` in bug#75122. And overriding `scan-lists` is useful only in `forward-list`, `down-list` and `up-list`. That's all. So clearly instead of overriding `scan-lists` and `scan-sexps`, better would be to add 3 new variables: `forward-list-function`, `down-list-function` and `up-list-function`. This gives the users more flexibility to choose for example navigation for C-M-f with a limited number of treesit lists + syntax symbols, and for C-M-n everything that is a list in treesit. This means that with start_atimer (-!-enum atimer_type type, struct timespec timestamp) C-M-f could skip only the next symbol and move to start_atimer (enum-!- atimer_type type, struct timespec timestamp) while C-M-n could skip the whole next `parameter_declaration` start_atimer (enum atimer_type type-!-, struct timespec timestamp) or vice versa depending on the values of all new options `...-function` in ts-modes. > What I do know is that in general we'd also want an `up-sexp` operation. > Currently we have an ugly kludge in `up-list` to try and use > `forward-sexp-function` (which is ugly both because > `forward-sexp-function` doesn't really provide the functionality we > need, and because it mixes up sexp and list navigation), and it would be > good to clean it up. Agreed, this distinction is required for treesit. Hopefully, this can be achieved by separating `forward-sexp-function` and `up-list-function` that in ts-modes could be set to new functions either `treesit-up-list` or `treesit-up-sexp`. PS: will try to refactor everything in this attachment to new `forward-list-function`, `down-list-function` and `up-list-function`: --=-=-= Content-Type: application/emacs-lisp Content-Disposition: inline; filename=scan-lists-advice.el Content-Transfer-Encoding: quoted-printable (defvar treesit--recursive-scan-call nil) (define-advice scan-lists (:around (ofun from count depth) treesit) (if (or treesit--recursive-scan-call (not treesit-thing-settings)) (funcall ofun from count depth) (save-excursion (goto-char from) (let ((treesit--recursive-scan-call t)) (cond ((=3D depth 0) (treesit-forward-list count) (point)) ((< depth 0) (when (treesit-forward-list count) (let* ((node (if (> count 0) (treesit-thing-prev (point) 'sexp-list) (treesit-thing-next (point) 'sexp-list))) (beg-node (when node (treesit-node-child node 0)))) (when beg-node (treesit-node-end beg-node))))) ((> depth 0) (let ((node (treesit-thing-at (point) 'sexp-list))) (while (and node (eq (point) (treesit-node-start node))) (setq node (treesit-parent-until node 'sexp-list))) (when node (treesit-node-start node))))))))) (define-advice scan-sexps (:around (ofun from count) treesit) (if (or treesit--recursive-scan-call (not treesit-thing-settings)) (funcall ofun from count) (save-excursion (goto-char from) (let ((treesit--recursive-scan-call t)) (treesit-forward-sexp-list count)) (point)))) (define-advice treesit-major-mode-setup (:after (&rest _) reset) (setq-local forward-sexp-function nil)) --=-=-=--