diff options
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r-- | lisp/emacs-lisp/shortdoc.el | 2 | ||||
-rw-r--r-- | lisp/emacs-lisp/subr-x.el | 19 |
2 files changed, 12 insertions, 9 deletions
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index 7bb7d233b47..eb57e706608 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -145,7 +145,7 @@ There can be any number of :example/:result elements." :eval (substring "foobar" 3)) (string-limit :eval (string-limit "foobar" 3) - :eval (string-limit "foobar" -3) + :eval (string-limit "foobar" 3 t) :eval (string-limit "foobar" 10)) (split-string :eval (split-string "foo bar") diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 6f4f7ed5dce..b79482fd4b3 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -286,17 +286,20 @@ result will have lines that are longer than LENGTH." (fill-region (point-min) (point-max))) (buffer-string))) -(defun string-limit (string length) +(defun string-limit (string length &optional end) "Return (up to) a LENGTH substring of STRING. If STRING is shorter than or equal to LENGTH, the entire string -is returned unchanged. If STRING is longer than LENGTH, and -LENGTH is a positive number, return a substring consisting of the -first LENGTH characters of STRING. If LENGTH is negative, return -a substring consisting of the last LENGTH characters of STRING." +is returned unchanged. + +If STRING is longer than LENGTH, return a substring consisting of +the first LENGTH characters of STRING. If END is non-nil, return +the last LENTGH characters instead." + (unless (natnump length) + (signal 'wrong-type-argument (list 'natnump length))) (cond - ((<= (length string) (abs length)) string) - ((>= length 0) (substring string 0 length)) - ((substring string length)))) + ((<= (length string) length) string) + (end (substring string (- (length string) length))) + (t (substring string 0 length)))) (defun string-lines (string &optional omit-nulls) "Split STRING into a list of lines. |