From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Spencer Baugh via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#70968: 29.2.50; choose-completion on an emacs22-style completion deletes text after point Date: Tue, 15 Oct 2024 14:53:41 -0400 Message-ID: References: <86bk56jhsp.fsf@gnu.org> <377f815c-52d2-4770-ae85-55e096e104b0@gutov.dev> <8634qhipgj.fsf@gnu.org> <7e05fd14-3499-4811-b4bc-b53186b15408@gutov.dev> <86ed5vzzru.fsf@gnu.org> <86o74qh9wv.fsf@gnu.org> <8479c25d-b4ae-4e89-9880-0857a996936a@gutov.dev> Reply-To: Spencer Baugh 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="3699"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Eli Zaretskii , 70968@debbugs.gnu.org, monnier@iro.umontreal.ca, juri@linkov.net To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Oct 15 20:54:59 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 1t0mh9-0000kY-5R for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 15 Oct 2024 20:54:59 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t0mgw-0000Jb-6f; Tue, 15 Oct 2024 14:54:47 -0400 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 1t0mgu-0000JO-ND for bug-gnu-emacs@gnu.org; Tue, 15 Oct 2024 14:54:44 -0400 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 1t0mgu-0007Sz-En for bug-gnu-emacs@gnu.org; Tue, 15 Oct 2024 14:54:44 -0400 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=IQoxUpDMAKqT5mUVt5DAs/TFPT4N+gkAxy8AK63eRck=; b=LWIzV2JRQD71ZZrsOne6WCt+s+ygMXzXV92ZgaPI/AaHsa+489A9OXoPyfbtlnD7kPjw//IKH9ElV3en6rh4aZz89R18bPMa0PkGQQUCORtTFT/l82BLUC8zjC3SurGsrto5ihcPBx/PNblose6IpOL6u0uk2V6tNrM5szgGas0WQOtifVh19NF/up1uq7SPCUUTZKFA3iD+fM40zteXRWdpOgAqsoke2KDj123zvYuEJMmnCepLueAeePA0XMeGUEvAWOBdRGKgKob/8oqbYLTH8ZHH0pcG3a4jr/7B641yftWsXLsQ5nhRKfmuAb/i6gCTL3nbMYlD0xBuHx06FA==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1t0mhC-0002yd-HE for bug-gnu-emacs@gnu.org; Tue, 15 Oct 2024 14:55:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Spencer Baugh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 15 Oct 2024 18:55:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70968 X-GNU-PR-Package: emacs Original-Received: via spool by 70968-submit@debbugs.gnu.org id=B70968.172901844911345 (code B ref 70968); Tue, 15 Oct 2024 18:55:02 +0000 Original-Received: (at 70968) by debbugs.gnu.org; 15 Oct 2024 18:54:09 +0000 Original-Received: from localhost ([127.0.0.1]:57462 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1t0mgK-0002wu-5Q for submit@debbugs.gnu.org; Tue, 15 Oct 2024 14:54:08 -0400 Original-Received: from mxout1.mail.janestreet.com ([38.105.200.78]:39743) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1t0mgH-0002wN-OP for 70968@debbugs.gnu.org; Tue, 15 Oct 2024 14:54:06 -0400 In-Reply-To: <8479c25d-b4ae-4e89-9880-0857a996936a@gutov.dev> (Dmitry Gutov's message of "Tue, 24 Sep 2024 03:03:41 +0300") DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=waixah; t=1729018421; bh=IQoxUpDMAKqT5mUVt5DAs/TFPT4N+gkAxy8AK63eRck=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=soiMeddUnjkbVV7XJxf6zXxsh+L4BPOM3odEZuFGJqAgPdKm8tbSX/m03K2MATu5M obSLhyaFcqO4Pd5NSpodXt+CV+iugJsIhrz/oOu9FtZqo/nCHDHWcf7HDMKuFrJfT6 gxX7yvIyFLkcNTDcCvRLwgTA80kxpVoBMkxdwvTGAy5INPEUnl8nHU2sEseJcrxU/k zOa7Cjs4m9og+b/mnsyDq0cuI30Z7PAa7tfxYsKk9Hogrrq3FplyqvwbASp6JCqWV0 B2w01odZgRKMPqWJHJlgLVoI2ugH76icS9F6oX0v3TKyJTOSdKXXUE5ESoR0bvv1sI mjL1aSExwPOXQ== 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:293638 Archived-At: --=-=-= Content-Type: text/plain Dmitry Gutov writes: > Hi Spencer, > > On 16/09/2024 22:54, Spencer Baugh wrote: >> Eli Zaretskii writes: >>> I'm okay with adding a new style, per B, but why do we need to >>> deprecate emacs22 at the same time? Let users who want this new >>> behavior customize their completion styles to use this new style >>> instead of emacs22. That'd be fully backward compatible, and will >>> solve the problem for those who don't like the current behavior. >> That's fair enough, we don't need to deprecate emacs22 at the same >> time, >> we can wait until the new style has been battle-tested. >> I think a new style should replace emacs22 in the default >> completion-styles eventually, but that can wait. >> And after working on this bug for a while, I now am convinced that >> the >> new style approach is straightforward, and is the best way. (Although >> it would also work to make these changes in the emacs22 style) > > I'm not quite convinced about the "new style only" approach myself, > but anyway we can discuss a solution that could be applied to any > style, opt-in or opt-out. > >> Attached is a patch which adds a new ignore-after-point style. The fix >> for this bug is entirely contained in the differences between >> completion-ignore-after-point-all-completions (c-iap-a-c) and >> completion-emacs22-all-completions (c-e22-a-c). >> c-e22-a-c always omits the text after point, which means >> choose-completion always deletes that text, causing this bug. >> c-iap-a-c includes the text after point in some cases. Whenever the >> text after point is included, it's grayed out with a new >> completions-ignored face to indicate it was ignored. >> The cases where c-iap-a-c includes the text after point are those >> where >> the user will have further chances to edit or complete with that text: >> - When we're doing minibuffer completion and choose-completion will >> immediately exit the minibuffer, the text after point is not included, >> since the user won't get any further changes to use it, and it's >> probably garbage. >> - When we're doing completion-at-point (or certain kinds of >> minibuffer >> completion, e.g. completing a directory in filename completion), the >> text after point is included. In such cases, the user can always >> delete it themselves later, or might want to actually use it. >> To make this work consistently with completion-try-completion (which >> keeps point before the ignored suffix in both the ignore-after-point and >> emacs22 styles), choose-completion-string now checks a new >> 'completion-position-after-insert text property, and moves point to that >> position. >> (There are two other changes which are general improvements >> unrelated to >> this bug: >> - The behavior of keeping point before the ignored suffix is more >> consistent. >> - Instead of hardcoding try-completion and all-completion, >> ignore-after-point uses the configured completion styles.) > > Thank you, I see a few problems with that approach (as discussed > privately as well). To recap: > > From my POV integrating the result with company-mode is > non-trivial. And the created visual doesn't seem optimal in a number > of edge cases (long prefix = weird-looking popup; this probably > applies to the *Completions* buffer as well, though maybe to a lesser > extent). I think this is preferable, though, and even if it isn't, it should still be supported. This keeps it explicit to the user exactly what text will be inserted into the buffer upon accepting a completion. Consider these two cases of completion (| representing point): A. switch-to-|asdf B. switch-to-|buffer In both cases, switch-to-next-buffer is a potential completion, provided by "emacs22" in the case A and "basic" in case B. But in case A, if switch-to-next-buffer is chosen, the "asdf" should be preserved, and in case B, if switch-to-next-buffer, the "buffer" should be deleted. Because they have different behavior, they should appear differently to the user. That's why I think case A should show "switch-to-next-bufferasdf" in the *Completions* buffer, with "asdf" grayed out via a "completions-ignored" face. If a user decides they don't want "asdf" text to be shown in case A, then they can customize that. If a frontend decides it doesn't want to show that text, it can omit the text. But it should at least be *possible* for the text to be shown. Otherwise there's no way to distinguish the two cases. > Another problem is both the "all-completions" method and the insertion > function call out to UI: choose-completion-string--should-exit > references minibuffer-completion-table and completion-no-auto-exit > directly, for example. I'm on the fence about coupling to the > completion category. That's not an important aspect, I've removed that dependency in my latest version. > Finally, the use of 'completion-position-after-insert' seems like it > could be used separately from the "ignored text", meaning the spans > don't have to match. So it could be a separate feature, one that's > easy enough to implement on its own. Yes, and I indeed think this feature is useful on its own, because it allows choose-completion to be fully equivalent to completion-try-completion. Right now completion-try-completion can change point, but choose-completion can't. That's limiting for a bunch of reasons, and the inability to fix this bug purely in a completion style is one of them. So I think we should just go ahead and implement this feature. And if we do so, it makes it possible to fix this bug with only changes inside a completion style. > None of the above would be insurmountable, but here's what I think > avoids it using the 'completion--adjust-metadata' thingy that was > originally added for 'flex' a few releases ago: adding a metadata key > 'completion-ignore-after-point'. > > The attached patch does not make a distinction for file name > completion - it just covers the core problem - but I think any UI > could use the addition and likewise lookup the 'file' category, and > print the "hidden" suffix in the Completions, and decide to drop the > suffix in your first scenario (file name completion with exit > imminent). > > Spencer, Stefan, WDYT? Your patch is simple, and it works for default completion, but two issues: - Your patch doesn't distinguish the two cases A and B that I described above. - Your patch will require company-mode and other frontends to change. My patch does not strictly require that. But if we're already requiring that, I think we should take the opportunity to add the more powerful feature completion-position-after-insert. Here's a simplified version of my earlier patch, which modifies the emacs22 style to make it easier to discuss. I think this is equally simple as your patch, but it: - Distinguishes the cases A and B by including the ignored text in the completion, just grayed out. - Fixes the bug for other frontends without requiring them to change (they get additional benefit when they change to support completion-position-after-insert) Note that due to a separate bug in completion--twq-all, used in filename completion, the graying-out face is dropped from the completion candidates before they reach *Completions*; so if you try this, try it by e.g. completing Lisp symbols. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Preserve-suffix-in-emacs22-style.patch >From dabd42adc78ef3c3e8f40f913325941246993628 Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Wed, 18 Sep 2024 08:52:54 -0400 Subject: [PATCH] Preserve suffix in emacs22 style --- lisp/minibuffer.el | 14 ++++++++++++-- lisp/simple.el | 11 +++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 804afe9cb43..a28041751aa 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -3771,10 +3771,20 @@ completion-emacs22-try-completion (setq suffix (substring suffix 1))) (cons (concat completion suffix) (length completion)))))) +(defface completions-ignored + '((t (:inherit shadow))) + "Face for text which was ignored by the completion style.") + (defun completion-emacs22-all-completions (string table pred point) - (let ((beforepoint (substring string 0 point))) + (let ((beforepoint (substring string 0 point)) + (suffix (propertize (substring string point) 'face 'completions-ignored))) (completion-hilit-commonality - (all-completions beforepoint table pred) + (mapcar + (lambda (elem) + (let ((s (concat elem suffix))) + (put-text-property 0 1 'completion-position-after-insert (length elem) s) + s)) + (all-completions beforepoint table pred)) point (car (completion-boundaries beforepoint table pred ""))))) diff --git a/lisp/simple.el b/lisp/simple.el index e35cfe0479b..881eb51e57a 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -10371,10 +10371,13 @@ choose-completion-string ;; comes from buffer-substring-no-properties. ;;(remove-text-properties 0 (length choice) '(mouse-face nil) choice) ;; Insert the completion into the buffer where it was requested. - (funcall (or insert-function completion-list-insert-choice-function) - (or (car base-position) (point)) - (or (cadr base-position) (point)) - choice) + (let ((beg (or (car base-position) (point))) + (end (or (cadr base-position) (point)))) + (funcall (or insert-function completion-list-insert-choice-function) + beg end choice) + (unless (string-empty-p choice) + (when-let ((pos (get-text-property 0 'completion-position-after-insert choice))) + (goto-char (+ pos beg))))) ;; Update point in the window that BUFFER is showing in. (let ((window (get-buffer-window buffer t))) (set-window-point window (point))) -- 2.39.3 --=-=-=--