bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#46240: Sorting order of read-char-by-name


From: Juri Linkov
Subject: bug#46240: Sorting order of read-char-by-name
Date: Thu, 04 Feb 2021 11:32:27 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu)

>> I've implemented this in
>> https://lists.gnu.org/archive/html/emacs-devel/2020-11/msg00884.html
>> but the implementation was not too compact,
>> so I'm not sure if it's worth adding as an option.
>
> Looks nice!  Adding it as an option sounds like a good idea to me...
> but would this need another variable in addition to the other variable
> you proposed to just alter the sorting, or could these things somehow be
> the same variable?

Better to try adding all options to the same variable
to reduce the number of knobs.

> Adding these headers would only make sense if the user is sorting by
> code instead of name...  so could the `read-char-by-name-sort-function'
> variable instead be, say, `read-char-by-name-display' with values
> `names', `code', `sections' (where `names' would be the current one,
> `code' just sort by code, and `sections' would sort by code, and display
> headings)?  Or something along those lines?

Yep.  The only difference that this patch (that contains previous
implementation of grouping by blocks) uses `nil' instead of `names'
since this is the default value.

Oops, I noticed that my previous implementation sorted by names
inside each block, not by code.  I'm not sure if this makes sense?
Definitely, sorting by code inside blocks should be implemented
as the primary feature, but should an additional option with previous
implementation be retained to sort inside blocks by names too?

diff --git a/lisp/international/mule-cmds.el b/lisp/international/mule-cmds.el
index 5dc3de4422..465fd1bf53 100644
--- a/lisp/international/mule-cmds.el
+++ b/lisp/international/mule-cmds.el
@@ -3083,6 +3083,43 @@ mule--ucs-names-affixation
               (list name (concat (if char (format "%c" char) " ") "\t") "")))
           names))
 
+(defun mule--ucs-names-by-code (names)
+  (let* ((codes-and-names
+          (mapcar (lambda (name) (cons (gethash name ucs-names) name)) names))
+         (sorted (sort codes-and-names (lambda (a b) (< (car a) (car b))))))
+    (mapcar #'cdr sorted)))
+
+(defun mule--ucs-names-by-group (names)
+  (let* ((names-chars
+          (mapcar (lambda (name) (cons name (gethash name ucs-names))) names))
+         (groups-names
+          (seq-group-by
+           (lambda (name-char)
+             (let ((script (aref char-script-table (cdr name-char))))
+               (if script (symbol-name script) "ungrouped")))
+           names-chars))
+         names-headers header)
+    (dolist (group groups-names)
+      (setq header t)
+      (dolist (name-char (cdr group))
+        (push (list (car name-char)
+                    (concat
+                     ;; header
+                     (if header
+                         (progn
+                           (setq header nil)
+                           (concat "\n" (propertize
+                                         (format "* %s\n" (car group))
+                                         'face 'header-line)))
+                       "")
+                     ;; prefix
+                     (if (cdr name-char) (format "%c" (cdr name-char)) " ")
+                     " ")
+                    ;; suffix
+                    "")
+              names-headers)))
+    (nreverse names-headers)))
+
 (defun char-from-name (string &optional ignore-case)
   "Return a character as a number from its Unicode name STRING.
 If optional IGNORE-CASE is non-nil, ignore case in STRING.
@@ -3104,6 +3141,15 @@ char-from-name
                                            ignore-case))
                 code)))))))
 
+(defcustom read-char-by-name-display nil
+  "How to display characters by `read-char-by-name' completion."
+  :type '(choice
+          (const :tag "Sort by character names" nil)
+          (const :tag "Sort by character codepoints" code)
+          (const :tag "Group by Unicode blocks" sections))
+  :group 'mule
+  :version "28.1")
+
 (defun read-char-by-name (prompt)
   "Read a character by its Unicode name or hex number string.
 Display PROMPT and read a string that represents a character by its
@@ -3130,8 +3176,14 @@ read-char-by-name
           prompt
           (lambda (string pred action)
             (if (eq action 'metadata)
-                '(metadata
-                  (affixation-function . mule--ucs-names-affixation)
+                `(metadata
+                  (affixation-function
+                    . ,(if (eq read-char-by-name-display 'sections)
+                           'mule--ucs-names-by-group
+                         'mule--ucs-names-affixation))
+                  (display-sort-function
+                    . ,(when (eq read-char-by-name-display 'code)
+                         'mule--ucs-names-by-code))
                   (category . unicode-name))
               (complete-with-action action (ucs-names) string pred)))))
         (char

reply via email to

[Prev in Thread] Current Thread [Next in Thread]