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#68214: Completion sorting customization by category Date: Wed, 03 Jan 2024 18:07:50 +0200 Organization: LINKOV.NET Message-ID: <868r5630ft.fsf@mail.linkov.net> References: <86a5pnzst1.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="38061"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: 68214@debbugs.gnu.org To: Daniel Mendler Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jan 03 17:09:30 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 1rL3oA-0009l2-IX for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 03 Jan 2024 17:09:30 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rL3ni-0002MR-O0; Wed, 03 Jan 2024 11:09:02 -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 1rL3ng-0002M1-8y for bug-gnu-emacs@gnu.org; Wed, 03 Jan 2024 11:09:00 -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 1rL3ne-0005nz-Vp for bug-gnu-emacs@gnu.org; Wed, 03 Jan 2024 11:08:58 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rL3nh-0003M6-Lb for bug-gnu-emacs@gnu.org; Wed, 03 Jan 2024 11:09:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 03 Jan 2024 16:09:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68214 X-GNU-PR-Package: emacs Original-Received: via spool by 68214-submit@debbugs.gnu.org id=B68214.170429809412840 (code B ref 68214); Wed, 03 Jan 2024 16:09:01 +0000 Original-Received: (at 68214) by debbugs.gnu.org; 3 Jan 2024 16:08:14 +0000 Original-Received: from localhost ([127.0.0.1]:53088 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rL3mv-0003L0-GT for submit@debbugs.gnu.org; Wed, 03 Jan 2024 11:08:14 -0500 Original-Received: from relay5-d.mail.gandi.net ([2001:4b98:dc4:8::225]:56665) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rL3mq-0003Ke-Oe for 68214@debbugs.gnu.org; Wed, 03 Jan 2024 11:08:12 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 495421C0004; Wed, 3 Jan 2024 16:07:57 +0000 (UTC) In-Reply-To: (Daniel Mendler's message of "Wed, 3 Jan 2024 08:20:37 +0100") 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:277276 Archived-At: --=-=-= Content-Type: text/plain > Would it make sense to define a general > `completion-metadata-override-get` function instead of > `completion--display-sort-function`? Thanks for the suggestion, Daniel. This is used now in a new patch below. > This function could be used to look up the other meta data > functions too, `display-sort-function`, `annotation-function`, > `affixation-function`, `group-function`, etc. All these meta data functions could be added later to completion-category-overrides after pushing the current patch. > (defun completion-metadata-override-get (metadata prop) > (if-let ((cat (completion-metadata-get metadata 'category)) > (over (completion--category-override cat prop))) > (cdr over) > (completion-metadata-get metadata prop))) > > I suggest to use `if-let` instead of `let*`, such that an override is > not retrieved if the category is nil. Maybe customization of completion-category-overrides could support a catch-all category `nil` for completions without metadata, like e.g. `nil` can be used in .dir-locals.el as a catch-all for all modes. But I'm not sure how useful this would be. > Besides that, in the `completion-category-overrides`, you use > `completion--sorting-type` for the `display-sort-function`, while it > should just be `function`. A new patch doesn't use anymore `completion--sorting-type` since there is a small difference between customization choices in `completions-sort` and `completion-category-overrides`: `completion-category-overrides` needs the value `identity` that prevents a fallback to `completions-sort` as `nil` does. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=completion-metadata-override-get.patch diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index fa2dcb4f698..ac07b92acb8 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1148,10 +1153,16 @@ completion-category-defaults (defcustom completion-category-overrides nil "List of category-specific user overrides for completion styles. + Each override has the shape (CATEGORY . ALIST) where ALIST is an association list that can specify properties such as: - `styles': the list of `completion-styles' to use for that category. - `cycle': the `completion-cycle-threshold' to use for that category. +- `display-sort-function': where `nil' means to use either the sorting +function from metadata or if it's nil then fall back to `completions-sort'; +`identity' means to not use any sorting to keep the original order; +and other values are the same as in `completions-sort'. + Categories are symbols such as `buffer' and `file', used when completing buffer and file names, respectively. @@ -1171,12 +1182,26 @@ completion-category-overrides ,completion--styles-type) (cons :tag "Completion Cycling" (const :tag "Select one value from the menu." cycle) - ,completion--cycling-threshold-type)))) + ,completion--cycling-threshold-type) + (cons :tag "Completion Sorting" + (const :tag "Select one value from the menu." + display-sort-function) + (choice (const :tag "Use default" nil) + (const :tag "No sorting" identity) + (const :tag "Alphabetical sorting" alphabetical) + (const :tag "Historical sorting" historical) + (function :tag "Custom function")))))) (defun completion--category-override (category tag) (or (assq tag (cdr (assq category completion-category-overrides))) (assq tag (cdr (assq category completion-category-defaults))))) +(defun completion-metadata-override-get (metadata prop) + (if-let ((cat (completion-metadata-get metadata 'category)) + (over (completion--category-override cat prop))) + (cdr over) + (completion-metadata-get metadata prop))) + (defun completion--styles (metadata) (let* ((cat (completion-metadata-get metadata 'category)) (over (completion--category-override cat 'styles))) @@ -2522,7 +2547,7 @@ minibuffer-completion-help (aff-fun (or (completion-metadata-get all-md 'affixation-function) (plist-get completion-extra-properties :affixation-function))) - (sort-fun (completion-metadata-get all-md 'display-sort-function)) + (sort-fun (completion-metadata-override-get all-md 'display-sort-function)) (group-fun (completion-metadata-get all-md 'group-function)) (mainbuf (current-buffer)) ;; If the *Completions* buffer is shown in a new --=-=-=--