unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#68214: Completion sorting customization by category
@ 2024-01-02 17:07 Juri Linkov
  2024-01-03  7:20 ` Daniel Mendler
  0 siblings, 1 reply; 16+ messages in thread
From: Juri Linkov @ 2024-01-02 17:07 UTC (permalink / raw)
  To: 68214

[-- Attachment #1: Type: text/plain, Size: 787 bytes --]

0. emacs -Q
1. M-x calendar RET
2. 'g d' (calendar-goto-date)
3. type any year and RET
4. on the prompt "Month name: " type TAB

Month names are sorted alphabetically that makes no sense:

  April August December February January July
  June  March  May      November October September

What is worse is that currently it's impossible to customize
this sorting order.

This was discussed recently in
https://lists.gnu.org/archive/html/emacs-devel/2023-11/msg01233.html

Here is a patch that allows such customization

 (setopt completion-category-overrides
         '((calendar-month
            (display-sort-function . identity))))

that will sort month names chronologically:

  January February March     April   May      June
  July    August   September October November December


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: completion-sort.patch --]
[-- Type: text/x-diff, Size: 3907 bytes --]

diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el
index a25684f7b5d..e01d5d792a6 100644
--- a/lisp/calendar/calendar.el
+++ b/lisp/calendar/calendar.el
@@ -2339,7 +2339,11 @@ calendar-read-date
          (month (cdr (assoc-string
                       (completing-read
                        (format-prompt "Month name" defmon)
-                       (append month-array nil)
+                       (lambda (string pred action)
+	                 (if (eq action 'metadata)
+		             '(metadata (category . calendar-month))
+	                   (complete-with-action
+                            action (append month-array nil) string pred)))
                        nil t nil nil defmon)
                       (calendar-make-alist month-array 1) t)))
          (defday (calendar-extract-day default-date))
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index fa2dcb4f698..67870ad8054 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1106,6 +1111,13 @@ completion--cycling-threshold-type
            (const :tag "Always cycle" t)
            (integer :tag "Threshold")))
 
+(defconst completion--sorting-type
+  '(choice (const :tag "Undefined" nil)
+           (const :tag "Keep unsorted" identity)
+           (const :tag "Alphabetical sorting" alphabetical)
+           (const :tag "Historical sorting" historical)
+           (function :tag "Custom function")))
+
 (defcustom completion-styles
   ;; First, use `basic' because prefix completion has been the standard
   ;; for "ever" and works well in most cases, so using it first
@@ -1171,7 +1183,11 @@ 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)
+                 ,completion--sorting-type))))
 
 (defun completion--category-override (category tag)
   (or (assq tag (cdr (assq category completion-category-overrides)))
@@ -1334,10 +1350,7 @@ completions-sort
 If the completion-specific metadata provides a
 `display-sort-function', that function overrides the value of
 this variable."
-  :type '(choice (const :tag "No sorting" nil)
-                 (const :tag "Alphabetical sorting" alphabetical)
-                 (const :tag "Historical sorting" historical)
-                 (function :tag "Custom function"))
+  :type completion--sorting-type
   :version "30.1")
 
 (defcustom completions-group nil
@@ -1618,6 +1631,12 @@ completion--metadata
     (if (eq (car bounds) base) md-at-point
       (completion-metadata (substring string 0 base) table pred))))
 
+(defun completion--display-sort-function (metadata)
+  (let* ((cat (completion-metadata-get metadata 'category))
+         (over (completion--category-override cat 'display-sort-function)))
+    (if over (cdr over)
+      (completion-metadata-get metadata 'display-sort-function))))
+
 (defun minibuffer--sort-by-key (elems keyfun)
   "Return ELEMS sorted by increasing value of their KEYFUN.
 KEYFUN takes an element of ELEMS and should return a numerical value."
@@ -2522,7 +2541,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--display-sort-function all-md))
              (group-fun (completion-metadata-get all-md 'group-function))
              (mainbuf (current-buffer))
              ;; If the *Completions* buffer is shown in a new

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2024-01-10  8:53 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-02 17:07 bug#68214: Completion sorting customization by category Juri Linkov
2024-01-03  7:20 ` Daniel Mendler
2024-01-03 16:07   ` Juri Linkov
2024-01-03 17:12     ` Daniel Mendler
2024-01-04 17:21       ` Juri Linkov
2024-01-05  7:59         ` Juri Linkov
2024-01-05  8:33           ` Daniel Mendler
2024-01-06 17:33             ` Juri Linkov
2024-01-06 17:59               ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-07 18:05                 ` Juri Linkov
2024-01-07 18:37                   ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-09 17:59                     ` Juri Linkov
2024-01-09 18:14                       ` Juri Linkov
2024-01-09 18:31                         ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-10  7:35                           ` Juri Linkov
2024-01-10  8:53                             ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).