aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorPhil Sainty2025-06-01 14:59:38 +1200
committerPhil Sainty2026-01-24 02:03:31 +1300
commit0f1963b2cc0ebda15b3581eec9bfef5215e0c6b6 (patch)
treee10954df0debbbfd4e5ba6af19dec2548d17acae /lisp
parentb4a5948d3330f7ca02c61075eed94b467645ea83 (diff)
downloademacs-scratch/string-common-prefix.tar.gz
emacs-scratch/string-common-prefix.zip
Add function `string-common-prefix'scratch/string-common-prefix
* lisp/subr.el: (string-common-prefix): New function. * etc/NEWS: * doc/lispref/strings.texi: * doc/lispref/minibuf.texi: * lisp/emacs-lisp/shortdoc.el: Documentation.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/emacs-lisp/shortdoc.el9
-rw-r--r--lisp/subr.el26
2 files changed, 34 insertions, 1 deletions
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index 70583e08dbd..af15d15523d 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -385,8 +385,15 @@ A FUNC form can have any number of `:no-eval' (or `:no-value'),
385 :eval (reverse "foo")) 385 :eval (reverse "foo"))
386 (substring-no-properties 386 (substring-no-properties
387 :eval (substring-no-properties (propertize "foobar" 'face 'bold) 0 3)) 387 :eval (substring-no-properties (propertize "foobar" 'face 'bold) 0 3))
388 (string-common-prefix
389 :eval (string-common-prefix '("foobar" "foozot"))
390 :eval (string-common-prefix '("foobar" "foozot" "gazonk"))
391 :eval (string-common-prefix nil))
388 (try-completion 392 (try-completion
389 :eval (try-completion "foo" '("foobar" "foozot" "gazonk"))) 393 :eval (try-completion "f" '("foobar" "foozot" "gazonk"))
394 :eval (try-completion "f" '("foo"))
395 :eval (try-completion "foo" '("foo"))
396 :eval (try-completion "foo" nil))
390 "Unicode Strings" 397 "Unicode Strings"
391 (string-glyph-split 398 (string-glyph-split
392 :eval (string-glyph-split "Hello, πŸ‘ΌπŸ»πŸ§‘πŸΌβ€πŸ€β€πŸ§‘πŸ»")) 399 :eval (string-glyph-split "Hello, πŸ‘ΌπŸ»πŸ§‘πŸΌβ€πŸ€β€πŸ§‘πŸ»"))
diff --git a/lisp/subr.el b/lisp/subr.el
index d307a07f05b..a5191ba74f3 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -6129,6 +6129,32 @@ attention to letter-case differences."
6129 (eq t (compare-strings suffix nil nil 6129 (eq t (compare-strings suffix nil nil
6130 string start-pos nil ignore-case))))) 6130 string start-pos nil ignore-case)))))
6131 6131
6132(defun string-common-prefix (strings &optional ignore-case)
6133 "Return the longest common prefix from a collection of STRINGS.
6134
6135Return \"\" if there is no common prefix or if STRINGS is nil.
6136If STRINGS contains exactly one string, return that string.
6137
6138If IGNORE-CASE is non-nil, letter case is ignored when matching the
6139substrings, but no guarantee is made about the letter-case of the return
6140value, except that it comes from one of the members of STRINGS.
6141
6142STRINGS may be a list of strings or any other collection type supported
6143by `try-completion' and `all-completions' (which see). To find the
6144common prefix of a subset of STRINGS (filtered by string prefix, regular
6145expression, or predicate function), use `all-completions' to perform the
6146filtering and pass the result to `string-common-prefix' as STRINGS."
6147 ;; Note that `try-completion' is not affected by `completion-styles'.
6148 (let* ((completion-ignore-case ignore-case)
6149 (completion-regexp-list nil)
6150 (prefix (try-completion "" strings)))
6151 (if (stringp prefix)
6152 prefix
6153 ;; nil means there was no match.
6154 ;; t means that the "" argument was an exact match.
6155 ;; We always return a string, so we treat both cases the same.
6156 "")))
6157
6132(defun bidi-string-mark-left-to-right (str) 6158(defun bidi-string-mark-left-to-right (str)
6133 "Return a string that can be safely inserted in left-to-right text. 6159 "Return a string that can be safely inserted in left-to-right text.
6134 6160