emacs-diffs
[Top][All Lists]
Advanced

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

master 7020fce: New options read-char-by-name-sort and read-char-by-name


From: Juri Linkov
Subject: master 7020fce: New options read-char-by-name-sort and read-char-by-name-group (bug#46240)
Date: Tue, 9 Feb 2021 13:13:08 -0500 (EST)

branch: master
commit 7020fce353b3e836c03703683e447a9ddf209b6a
Author: Juri Linkov <juri@linkov.net>
Commit: Juri Linkov <juri@linkov.net>

    New options read-char-by-name-sort and read-char-by-name-group (bug#46240)
    
    * lisp/international/mule-cmds.el (mule--ucs-names-sort-by-code)
    (mule--ucs-names-group): New functions.
    (read-char-by-name-sort, read-char-by-name-group): New defcustoms.
    (read-char-by-name): Use them.
---
 etc/NEWS                        |  8 +++++
 lisp/international/mule-cmds.el | 66 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 5325e87..bd209de 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -872,6 +872,14 @@ so e.g. like 'C-x 8 [' inserts a left single quotation 
mark,
 'C-x \ [' does the same.
 
 ---
+*** New user options 'read-char-by-name-sort' and 'read-char-by-name-group'.
+'read-char-by-name-sort' defines the sorting order of characters for
+completion of 'C-x 8 RET TAB' and can be customized to sort them
+by codepoints instead of character names by default.  The 't' value of
+'read-char-by-name-group' groups the characters for completion of
+'C-x 8 RET TAB' by Unicode blocks.
+
+---
 *** Improved language transliteration in Malayalam input methods.
 Added a new Mozhi scheme.  The inapplicable ITRANS scheme is now
 deprecated.  Errors in the Inscript method were corrected.
diff --git a/lisp/international/mule-cmds.el b/lisp/international/mule-cmds.el
index 5dc3de4..5f66328 100644
--- a/lisp/international/mule-cmds.el
+++ b/lisp/international/mule-cmds.el
@@ -3077,12 +3077,48 @@ on encoding."
         (puthash "BELL (BEL)" ?\a names)
         (setq ucs-names names))))
 
+(defun mule--ucs-names-sort-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-affixation (names)
   (mapcar (lambda (name)
             (let ((char (gethash name ucs-names)))
               (list name (concat (if char (format "%c" char) " ") "\t") "")))
           names))
 
+(defun mule--ucs-names-group (names)
+  (let* ((codes-and-names
+          (mapcar (lambda (name) (cons (gethash name ucs-names) name)) names))
+         (grouped
+          (seq-group-by
+           (lambda (code-name)
+             (let ((script (aref char-script-table (car code-name))))
+               (if script (symbol-name script) "ungrouped")))
+           codes-and-names))
+         names-with-header header)
+    (dolist (group (sort grouped (lambda (a b) (string< (car a) (car b)))))
+      (setq header t)
+      (dolist (code-name (cdr group))
+        (push (list
+               (cdr code-name)
+               (concat
+                (if header
+                    (progn
+                      (setq header nil)
+                      (concat "\n" (propertize
+                                    (format "* %s\n" (car group))
+                                    'face 'header-line)))
+                  "")
+                ;; prefix
+                (if (car code-name) (format "%c" (car code-name)) " ") "\t")
+               ;; suffix
+               "")
+              names-with-header)))
+    (nreverse names-with-header)))
+
 (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 +3140,23 @@ Return nil if STRING does not name a character."
                                            ignore-case))
                 code)))))))
 
+(defcustom read-char-by-name-sort nil
+  "How to sort characters for `read-char-by-name' completion.
+Defines the sorting order either by character names or their codepoints."
+  :type '(choice
+          (const :tag "Sort by character names" nil)
+          (const :tag "Sort by character codepoints" code))
+  :group 'mule
+  :version "28.1")
+
+(defcustom read-char-by-name-group nil
+  "How to group characters for `read-char-by-name' completion.
+When t, split characters to sections of Unicode blocks
+sorted alphabetically."
+  :type 'boolean
+  :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
@@ -3117,6 +3170,9 @@ preceded by an asterisk `*' and use completion, it will 
show all
 the characters whose names include that substring, not necessarily
 at the beginning of the name.
 
+The options `read-char-by-name-sort' and `read-char-by-name-group'
+define the sorting order of completion characters and how to group them.
+
 Accept a name like \"CIRCULATION FUNCTION\", a hexadecimal
 number like \"2A10\", or a number in hash notation (e.g.,
 \"#x2a10\" for hex, \"10r10768\" for decimal, or \"#o25020\" for
@@ -3130,8 +3186,14 @@ as names, not numbers."
           prompt
           (lambda (string pred action)
             (if (eq action 'metadata)
-                '(metadata
-                  (affixation-function . mule--ucs-names-affixation)
+                `(metadata
+                  (display-sort-function
+                   . ,(when (eq read-char-by-name-sort 'code)
+                        'mule--ucs-names-sort-by-code))
+                  (affixation-function
+                   . ,(if read-char-by-name-group
+                          'mule--ucs-names-group
+                        'mule--ucs-names-affixation))
                   (category . unicode-name))
               (complete-with-action action (ucs-names) string pred)))))
         (char



reply via email to

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