aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/lispref/minibuf.texi3
-rw-r--r--doc/lispref/strings.texi37
-rw-r--r--etc/NEWS6
-rw-r--r--lisp/emacs-lisp/shortdoc.el9
-rw-r--r--lisp/subr.el26
5 files changed, 80 insertions, 1 deletions
diff --git a/doc/lispref/minibuf.texi b/doc/lispref/minibuf.texi
index 9d73aa89b2d..066bfc79718 100644
--- a/doc/lispref/minibuf.texi
+++ b/doc/lispref/minibuf.texi
@@ -965,6 +965,9 @@ too short). Both of those begin with the string @samp{foobar}.
965 @result{} "foobar" 965 @result{} "foobar"
966@end group 966@end group
967@end smallexample 967@end smallexample
968
969 See also the function @code{string-common-prefix} in
970@ref{Creating Strings}.
968@end defun 971@end defun
969 972
970@defun all-completions string collection &optional predicate 973@defun all-completions string collection &optional predicate
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index 44be529d562..caa29747ff5 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -248,6 +248,43 @@ equivalent to 0. Thus, @w{@code{(substring-no-properties
248properties removed. 248properties removed.
249@end defun 249@end defun
250 250
251@cindex common prefix of a collection of strings
252@cindex string completion
253@defun string-common-prefix strings &optional ignore-case
254This function returns the longest initial substring common to all
255members of @var{strings}. It returns an empty string if there is no
256common prefix or if @var{strings} is @code{nil}. If @var{strings}
257contains exactly one string, it returns that string.
258
259@example
260(string-common-prefix '("foobar" "foozot"))
261 @result{} "foo"
262(string-common-prefix '("foobar" "foozot" "gazonk"))
263 @result{} ""
264(string-common-prefix '("foobar"))
265 @result{} "foobar"
266@end example
267
268If @var{ignore-case} is non-@code{nil}, letter case is ignored when
269matching the substrings, but no guarantee is made about the letter-case
270of the return value, except that it comes from one of the members of
271@var{strings}.
272
273@var{strings} may be a list of strings or any other collection type
274supported by @code{try-completion} and @code{all-completions}
275(@pxref{Basic Completion}). This function is similar to
276@code{try-completion}, but always returns a string. The filtering
277features of the completion functions (by string prefix, regular
278expression, and predicate function) are available using
279@code{all-completions}:
280
281@example
282(string-common-prefix (all-completions
283 "foo" '("foobar" "foobaz" "gazonk")))
284 @result{} "fooba"
285@end example
286@end defun
287
251@defun concat &rest sequences 288@defun concat &rest sequences
252@cindex copying strings 289@cindex copying strings
253@cindex concatenating strings 290@cindex concatenating strings
diff --git a/etc/NEWS b/etc/NEWS
index 2cb1978738e..256c8ab139d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -3879,6 +3879,12 @@ It offers a more concise way to create a completion table with metadata.
3879** 'all-completions' and 'unintern' no longer support old calling conventions. 3879** 'all-completions' and 'unintern' no longer support old calling conventions.
3880 3880
3881+++ 3881+++
3882** New function 'string-common-prefix'.
3883Return the longest common prefix from a collection of strings. This
3884function is similar to 'try-completion', but it returns a string in all
3885cases.
3886
3887+++
3882** New symbol property 'find-function-type-alist' used by 'find-function' etc. 3888** New symbol property 'find-function-type-alist' used by 'find-function' etc.
3883Macros that define an object in a way that makes the object's name and 3889Macros that define an object in a way that makes the object's name and
3884the macro call site defining the object hard to associate can add an 3890the macro call site defining the object hard to associate can add an
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