diff options
| author | Phil Sainty | 2025-06-01 14:59:38 +1200 |
|---|---|---|
| committer | Phil Sainty | 2026-01-24 02:03:31 +1300 |
| commit | 0f1963b2cc0ebda15b3581eec9bfef5215e0c6b6 (patch) | |
| tree | e10954df0debbbfd4e5ba6af19dec2548d17acae | |
| parent | b4a5948d3330f7ca02c61075eed94b467645ea83 (diff) | |
| download | emacs-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.
| -rw-r--r-- | doc/lispref/minibuf.texi | 3 | ||||
| -rw-r--r-- | doc/lispref/strings.texi | 37 | ||||
| -rw-r--r-- | etc/NEWS | 6 | ||||
| -rw-r--r-- | lisp/emacs-lisp/shortdoc.el | 9 | ||||
| -rw-r--r-- | lisp/subr.el | 26 |
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 | |||
| 248 | properties removed. | 248 | properties 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 | ||
| 254 | This function returns the longest initial substring common to all | ||
| 255 | members of @var{strings}. It returns an empty string if there is no | ||
| 256 | common prefix or if @var{strings} is @code{nil}. If @var{strings} | ||
| 257 | contains 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 | |||
| 268 | If @var{ignore-case} is non-@code{nil}, letter case is ignored when | ||
| 269 | matching the substrings, but no guarantee is made about the letter-case | ||
| 270 | of 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 | ||
| 274 | supported 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 | ||
| 277 | features of the completion functions (by string prefix, regular | ||
| 278 | expression, 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 |
| @@ -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'. | ||
| 3883 | Return the longest common prefix from a collection of strings. This | ||
| 3884 | function is similar to 'try-completion', but it returns a string in all | ||
| 3885 | cases. | ||
| 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. |
| 3883 | Macros that define an object in a way that makes the object's name and | 3889 | Macros that define an object in a way that makes the object's name and |
| 3884 | the macro call site defining the object hard to associate can add an | 3890 | the 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 | |||
| 6135 | Return \"\" if there is no common prefix or if STRINGS is nil. | ||
| 6136 | If STRINGS contains exactly one string, return that string. | ||
| 6137 | |||
| 6138 | If IGNORE-CASE is non-nil, letter case is ignored when matching the | ||
| 6139 | substrings, but no guarantee is made about the letter-case of the return | ||
| 6140 | value, except that it comes from one of the members of STRINGS. | ||
| 6141 | |||
| 6142 | STRINGS may be a list of strings or any other collection type supported | ||
| 6143 | by `try-completion' and `all-completions' (which see). To find the | ||
| 6144 | common prefix of a subset of STRINGS (filtered by string prefix, regular | ||
| 6145 | expression, or predicate function), use `all-completions' to perform the | ||
| 6146 | filtering 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 | ||