Add 'char-to-name' convenience function
authorRobert Pluim <rpluim@gmail.com>
Fri, 7 Jun 2024 10:05:34 +0000 (12:05 +0200)
committerRobert Pluim <rpluim@gmail.com>
Tue, 11 Jun 2024 09:06:37 +0000 (11:06 +0200)
* lisp/international/mule-cmds.el (char-to-name): New function.  This is
the inverse of 'char-from-name'.
* doc/lispref/nonascii.texi (Character Codes): Document it.
* etc/NEWS: Announce it.

doc/lispref/nonascii.texi
etc/NEWS
lisp/international/mule-cmds.el

index 1482becb9f57d4a0866f4d15bbfa77189ef51b98..d9fb9e306f05014fc87347324a1682292eb5a1f6 100644 (file)
@@ -435,6 +435,11 @@ This function returns @code{nil} if @var{string} does not name a character.
 @end example
 @end defun
 
+@defun char-to-name char
+This function returns the Unicode name of @var{char}.  Returns
+@code{nil} if @var{char} is not a character or has no Unicode name.
+@end defun
+
 @defun get-byte &optional pos string
 This function returns the byte at character position @var{pos} in the
 current buffer.  If the current buffer is unibyte, this is literally
index a0223fb2dc602ec06434210c972a2f9dd5e5e89d..c18872189e4882d6a06159be204674886588bffc 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2244,6 +2244,10 @@ it returns a short "unique" string that identifies the function.
 In either case, the string is propertized so clicking on it gives
 further details.
 
+** New function 'char-to-name'.
+This is a convenience function to return the Unicode name of a char (if
+it has one).
+
 ** New function 'cl-type-of'.
 This function is like 'type-of' except that it sometimes returns
 a more precise type.  For example, for nil and t it returns 'null'
index e80c42f523ac7e0fe30af612c22738518b466857..7d784ef3b1ba083a703f90dee9a67460d62d4443 100644 (file)
@@ -3183,6 +3183,13 @@ on encoding."
            (script (and char (aref char-script-table char))))
       (if script (symbol-name script) "ungrouped"))))
 
+(defun char-to-name (char)
+  "Return the Unicode name for CHAR, if it has one, else nil.
+Return nil if CHAR is not a character."
+  (and (characterp char)
+       (or (get-char-code-property char 'name)
+           (get-char-code-property char 'old-name))))
+
 (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.