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