Function: read-char-by-name

read-char-by-name is a byte-compiled function defined in mule-cmds.el.gz.

Signature

(read-char-by-name PROMPT &optional ALLOW-SINGLE)

Documentation

Read a character by its Unicode name or hex number string.

Display PROMPT and read a string that represents a character by its Unicode property name or old-name.

This function returns the character as a number.

You can type a few of the first letters of the Unicode name and use completion. If you type a substring of the Unicode name 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, completions-group, and completions-group-sort define the sorting order of completion characters, whether to group them, and how to sort groups.

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
octal). Treat otherwise-ambiguous strings like "BED" (U+1F6CF) as names, not numbers.

Optional arg ALLOW-SINGLE non-nil means to additionally allow single characters to be treated as standing for themselves.

Source Code

;; Defined in /usr/src/emacs/lisp/international/mule-cmds.el.gz
(defun read-char-by-name (prompt &optional allow-single)
  "Read a character by its Unicode name or hex number string.
Display PROMPT and read a string that represents a character by its
Unicode property `name' or `old-name'.

This function returns the character as a number.

You can type a few of the first letters of the Unicode name and
use completion.  If you type a substring of the Unicode name
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', `completions-group', and
`completions-group-sort' define the sorting order of completion characters,
whether to group them, and how to sort groups.

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
octal).  Treat otherwise-ambiguous strings like \"BED\" (U+1F6CF)
as names, not numbers.

Optional arg ALLOW-SINGLE non-nil means to additionally allow
single characters to be treated as standing for themselves."
  (let* ((enable-recursive-minibuffers t)
	 (completion-ignore-case t)
	 (completion-tab-width 4)
	 (input
	  (completing-read
	   prompt
	   (lambda (string pred action)
	     (if (eq action 'metadata)
		 `(metadata
		   (display-sort-function
		    . ,(when (eq read-char-by-name-sort 'code)
			 #'mule--ucs-names-sort-by-code))
		   (affixation-function
		    . ,#'mule--ucs-names-affixation)
		   (group-function
		    . ,(when completions-group
			 #'mule--ucs-names-group))
		   (category . unicode-name))
	       (complete-with-action action (ucs-names) string pred)))))
	 (char
          (cond
           ((char-from-name input t))
           ((and allow-single
                 (string-match-p "\\`.\\'" input)
                 (ignore-errors (string-to-char input))))
           ((string-match-p "\\`[[:xdigit:]]+\\'" input)
            (ignore-errors (string-to-number input 16)))
           ((string-match-p "\\`#\\([bBoOxX]\\|[0-9]+[rR]\\)[0-9a-zA-Z]+\\'"
                            input)
            (ignore-errors (read input))))))
    (unless (characterp char)
      (error "Invalid character"))
    char))