From 7f2fb992110a60a8b583db76fd3b3ee0f1c7efb9 Mon Sep 17 00:00:00 2001 From: Tino Calancha Date: Wed, 11 May 2022 18:01:11 +0200 Subject: [PATCH] char-uppercase-p: New predicate Return non-nil if its argument is an uppercase character. Suggested in Bug#54804. * lisp/subr.el (char-uppercase-p): New defun. * etc/NEWS (Lisp Changes in Emacs 29.1): Announce it * doc/lispref/display.texi (Size of Displayed Text): Document it. * test/lisp/subr-tests.el (test-char-uppercase-p): Add a test. --- doc/lispref/display.texi | 5 +++++ etc/NEWS | 5 ++++- lisp/simple.el | 8 ++++++++ test/lisp/subr-tests.el | 7 +++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi index 0ab683d234d..f428fb858b0 100644 --- a/doc/lispref/display.texi +++ b/doc/lispref/display.texi @@ -2010,6 +2010,11 @@ Tables}). The width of a tab character is usually @code{tab-width} (@pxref{Usual Display}). @end defun +@defun char-uppercase-p char +Return non-@code{nil} if @var{char} is an uppercase character +according to Unicode. +@end defun + @defun string-width string &optional from to This function returns the width in columns of the string @var{string}, if it were displayed in the current buffer and the selected window. diff --git a/etc/NEWS b/etc/NEWS index 991088a067f..ddb83ce410d 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1781,6 +1781,10 @@ functions. * Lisp Changes in Emacs 29.1 ++++ +** New predicate 'char-uppercase-p'. +This returns non-nil if its argument its an uppercase character. + ** Byte compilation --- @@ -1793,7 +1797,6 @@ I.e., double-quoting the 'bar', which is almost never the correct value. The byte compiler will now issue a warning if it encounters these forms. - +++ *** 'restore-buffer-modified-p' can now alter buffer auto-save state. With a FLAG value of 'autosaved', it will mark the buffer as having diff --git a/lisp/simple.el b/lisp/simple.el index 89fb0ea97ec..3812f6d8c6a 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -6054,6 +6054,14 @@ and KILLP is t if a prefix arg was specified." ;; Avoid warning about delete-backward-char (with-no-warnings (delete-backward-char n killp)))) +(defun char-uppercase-p (char) + "Return non-nil if CHAR is an upper-case character. +If the Unicode tables are not yet available, e.g. during bootstrap, +then gives correct answers only for ASCII characters." + (cond ((unicode-property-table-internal 'lowercase) + (characterp (get-char-code-property char 'lowercase))) + ((and (>= char ?A) (<= char ?Z))))) + (defun zap-to-char (arg char) "Kill up to and including ARGth occurrence of CHAR. Case is ignored if `case-fold-search' is non-nil in the current buffer. diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 89803e5ce2e..a25eb363b01 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -1074,5 +1074,12 @@ final or penultimate step during initialization.")) (should (= subr-test--local 2)) (should-not (boundp 'subr-test--unexist))))) +(ert-deftest test-char-uppercase-p () + "Tests for `char-uppercase-p'." + (dolist (c (list ?R ?S ?Ω ?Ψ)) + (should (char-uppercase-p c))) + (dolist (c (list ?a ?b ?α ?β)) + (should-not (char-uppercase-p c)))) + (provide 'subr-tests) ;;; subr-tests.el ends here -- 2.39.2