From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Daniel Mendler Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] `completing-read` - allow history=t, sorting improvements Date: Tue, 20 Apr 2021 00:29:24 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------CCECD6DDD38BC426CF603649" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6658"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Stefan Monnier To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Apr 20 03:12:10 2021 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 1lYevy-0001cV-33 for ged-emacs-devel@m.gmane-mx.org; Tue, 20 Apr 2021 03:12:10 +0200 Original-Received: from localhost ([::1]:53338 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lYevw-0005d2-Hf for ged-emacs-devel@m.gmane-mx.org; Mon, 19 Apr 2021 21:12:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60972) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lYcOp-0008BM-Ds for emacs-devel@gnu.org; Mon, 19 Apr 2021 18:29:47 -0400 Original-Received: from server.qxqx.de ([2a01:4f8:121:346::180]:55301 helo=mail.qxqx.de) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lYcOm-0007pX-V4 for emacs-devel@gnu.org; Mon, 19 Apr 2021 18:29:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=qxqx.de; s=mail1392553390; h=Content-Type:In-Reply-To:MIME-Version:Date:Message-ID:Cc: From:References:To:Subject:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=g9/ubwfnlB2EFybm49GMGz+zVgeH+ZGLS2HDVhTEq1Y=; b=ya1qD4b3msQyqhQHjtKQPcKWdF QpsogWzXhzuK5Yx+L/GXDp6pamQp0di64oaWdQU9cqa18vg2bbWFxSDZexNDVNxUyIjkVfG4khn75 giNWqyXV7rlgccjFx5EyAfbwemkqhFLftwa5pStawpsDRRtLmaLm0iPXyD91fo9UHBtk=; In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=2a01:4f8:121:346::180; envelope-from=daniel@mendler.net; helo=mail.qxqx.de X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Mon, 19 Apr 2021 21:10:42 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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" Xref: news.gmane.io gmane.emacs.devel:268197 Archived-At: This is a multi-part message in MIME format. --------------CCECD6DDD38BC426CF603649 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 4/19/21 11:52 PM, Stefan Monnier wrote: >> But if this get improved, people may throw more candidates at it and >> then we will end up again with a threshold. > > That's the main point, indeed: rather than try to handle larger sets, we > want to avoid larger sets. I agree - it is always good to cut off a large amount of the work early on. > That's why we have > `icomplete-show-matches-on-no-input` and `company-minimum-prefix-length` > (tho there can be other approaches like pushing the processing of sets > to a separate async process). Yes, these are reasonable settings. However many people like to see candidates up front, me included (at least currently, taste changes) - like in Ido, Ivy, Selectrum, Vertico etc. Then I am not very fond of operations happening after some idle time. Having an input limits feels more deterministic. If Emacs can be made such that it fits for different usage patterns that's great. But I would not over do it in optimizing for a million candidates - therefore my poor man's "solution" of a threshold in Vertico. Btw, I attached the updated patch for the boundaries. I am not sure if the approach I took there is a good one, this works only in restricted set of scenarios (not with partial-completion/initials unfortunately). So this should be discussed. Then you mentioned test cases - to clarify, do you want to see some kind of integration test where a mockup minibuffer-completion-table is set-up and the results of `completion-all-sorted-completions` are checked, or some smaller tests of the helper functions? I looked briefly into test/lisp/minibuffer.el - the test should probably go there? Daniel --------------CCECD6DDD38BC426CF603649 Content-Type: text/x-diff; charset=UTF-8; name="0002-completion-all-sorted-completions-Add-completion-bou.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0002-completion-all-sorted-completions-Add-completion-bou.pa"; filename*1="tch" >From a09ea8f4eb9a341f6c4ab090ec0c49acf070589a Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Tue, 20 Apr 2021 00:01:44 +0200 Subject: [PATCH] completion-all-sorted-completions: Add completion boundary support Remove the completion prefix from the history elements. This is a heuristic which does not work for all completion styles. In particular when using partial-completion or initials, the candidates contain directories. Is there a better more general solution? minibuffer.el (completion-all-sorted-completions): The history is preprocessed by the function `minibuffer--sort-preprocess-history`. --- lisp/minibuffer.el | 49 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 4ed596430c..d340aec98b 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1381,6 +1381,42 @@ minibuffer--sort-by-length-alpha (and (= (length c1) (length c2)) (string< c1 c2)))))) +(defun minibuffer--sort-preprocess-history (start string) + "Preprocess history, remove completion prefixes. +STRING is the minibuffer content. +START is the start position of the completion." + (let* ((def (car-safe minibuffer-default)) + (hist (symbol-value minibuffer-history-variable)) + (hist (if def (cons def hist) hist)) + (bounds (completion-boundaries + (substring string 0 (- (point) start)) + minibuffer-completion-table + minibuffer-completion-predicate + "")) + (pre (substring string 0 (car bounds))) + (pre-len (length pre))) + ;; Preprocess history if completion boundaries are used + (cond + ;; Special handling of file name candidates. + ;; Drop prefix and everything after the first "/". + (minibuffer-completing-file-name + (setq hist (delq nil + (mapcar + (lambda (c) + (when (string-prefix-p pre c) + (let ((pos (string-match-p "/" c pre-len))) + (substring c pre-len (and pos (1+ pos)))))) + hist)))) + ;; Drop prefix before the completion boundary + ((/= pre-len 0) + (setq hist + (delq nil (mapcar + (lambda (c) + (when (string-prefix-p pre c) + (substring c pre-len))) + hist)))) + (t hist)))) + (defun completion-all-sorted-completions (&optional start end) (or completion-all-sorted-completions (let* ((start (or start (minibuffer-prompt-end))) @@ -1410,21 +1446,16 @@ completion-all-sorted-completions (setq all (delete-dups all)) (setq last (last all)) - (cond - (sort-fun - (setq all (funcall sort-fun all))) - (t + (if sort-fun + (setq all (funcall sort-fun all)) ;; Sort first by length and alphabetically. (setq all (minibuffer--sort-by-length-alpha all)) - ;; Sort by history position, put the default, if it ;; exists, on top. (when (and (minibufferp) (not (eq minibuffer-history-variable t))) - (let ((def (car-safe minibuffer-default)) - (hist (symbol-value minibuffer-history-variable))) (setq all (minibuffer--sort-by-position - (if def (cons def hist) hist) - all)))))) + (minibuffer--sort-preprocess-history start string) + all)))) ;; Cache the result. This is not just for speed, but also so that ;; repeated calls to minibuffer-force-complete can cycle through -- 2.20.1 --------------CCECD6DDD38BC426CF603649--