From 188bd80a903d34ef6a85b09e99890433e7adceb7 Mon Sep 17 00:00:00 2001 From: Štěpán Němec Date: Sat, 7 Mar 2020 18:26:44 +0100 Subject: gnus-shorten-url: Improve and avoid args-out-of-range error 'gnus-shorten-url' (used by 'gnus-summary-browse-url') ignored fragment identifiers and didn't check substring bounds, in some cases leading to runtime errors, e.g.: (gnus-shorten-url "https://some.url.with/path/and#also_a_long_target" 40) ;; => Lisp error: (args-out-of-range "/path/and" -18 nil) This commit makes it account for #fragments and fixes faulty string computation, reusing existing helper function. (bug#39980) * lisp/vc/ediff-init.el (ediff-truncate-string-left): Rename to 'string-truncate-left' and move... * lisp/emacs-lisp/subr-x.el (string-truncate-left): ...here. All callers changed. * lisp/gnus/gnus-sum.el (gnus-shorten-url): Fix args-out-of-range error, don't drop #fragments, use 'string-truncate-left'. --- lisp/emacs-lisp/subr-x.el | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lisp/emacs-lisp') diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 044c9aada0d..9f96ac50d1c 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -236,6 +236,15 @@ REGEXP defaults to \"[ \\t\\n\\r]+\"." TRIM-LEFT and TRIM-RIGHT default to \"[ \\t\\n\\r]+\"." (string-trim-left (string-trim-right string trim-right) trim-left)) +;;;###autoload +(defun string-truncate-left (string length) + "Truncate STRING to LENGTH, replacing initial surplus with \"...\"." + (let ((strlen (length string))) + (if (<= strlen length) + string + (setq length (max 0 (- length 3))) + (concat "..." (substring string (max 0 (- strlen 1 length))))))) + (defsubst string-blank-p (string) "Check whether STRING is either empty or only whitespace. The following characters count as whitespace here: space, tab, newline and -- cgit v1.2.3