diff options
| author | Štěpán Němec | 2020-03-07 18:26:44 +0100 |
|---|---|---|
| committer | Štěpán Němec | 2020-04-13 12:12:00 +0200 |
| commit | 188bd80a903d34ef6a85b09e99890433e7adceb7 (patch) | |
| tree | f8f32f801fcf61e0a4464bee9319c1ecc78e8943 | |
| parent | c395ebaf2142b4a142262353f730fb7b1fcea710 (diff) | |
| download | emacs-188bd80a903d34ef6a85b09e99890433e7adceb7.tar.gz emacs-188bd80a903d34ef6a85b09e99890433e7adceb7.zip | |
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'.
| -rw-r--r-- | lisp/emacs-lisp/subr-x.el | 9 | ||||
| -rw-r--r-- | lisp/gnus/gnus-sum.el | 14 | ||||
| -rw-r--r-- | lisp/vc/ediff-init.el | 10 | ||||
| -rw-r--r-- | lisp/vc/ediff-mult.el | 9 |
4 files changed, 20 insertions, 22 deletions
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]+\"." | |||
| 236 | TRIM-LEFT and TRIM-RIGHT default to \"[ \\t\\n\\r]+\"." | 236 | TRIM-LEFT and TRIM-RIGHT default to \"[ \\t\\n\\r]+\"." |
| 237 | (string-trim-left (string-trim-right string trim-right) trim-left)) | 237 | (string-trim-left (string-trim-right string trim-right) trim-left)) |
| 238 | 238 | ||
| 239 | ;;;###autoload | ||
| 240 | (defun string-truncate-left (string length) | ||
| 241 | "Truncate STRING to LENGTH, replacing initial surplus with \"...\"." | ||
| 242 | (let ((strlen (length string))) | ||
| 243 | (if (<= strlen length) | ||
| 244 | string | ||
| 245 | (setq length (max 0 (- length 3))) | ||
| 246 | (concat "..." (substring string (max 0 (- strlen 1 length))))))) | ||
| 247 | |||
| 239 | (defsubst string-blank-p (string) | 248 | (defsubst string-blank-p (string) |
| 240 | "Check whether STRING is either empty or only whitespace. | 249 | "Check whether STRING is either empty or only whitespace. |
| 241 | The following characters count as whitespace here: space, tab, newline and | 250 | The following characters count as whitespace here: space, tab, newline and |
diff --git a/lisp/gnus/gnus-sum.el b/lisp/gnus/gnus-sum.el index a47e6576230..6f367692ddd 100644 --- a/lisp/gnus/gnus-sum.el +++ b/lisp/gnus/gnus-sum.el | |||
| @@ -9494,15 +9494,15 @@ The 1st element is the button named by `gnus-collect-urls-primary-text'." | |||
| 9494 | (delete-dups urls))) | 9494 | (delete-dups urls))) |
| 9495 | 9495 | ||
| 9496 | (defun gnus-shorten-url (url max) | 9496 | (defun gnus-shorten-url (url max) |
| 9497 | "Return an excerpt from URL." | 9497 | "Return an excerpt from URL not exceeding MAX characters." |
| 9498 | (if (<= (length url) max) | 9498 | (if (<= (length url) max) |
| 9499 | url | 9499 | url |
| 9500 | (let ((parsed (url-generic-parse-url url))) | 9500 | (let* ((parsed (url-generic-parse-url url)) |
| 9501 | (concat (url-host parsed) | 9501 | (host (url-host parsed)) |
| 9502 | "..." | 9502 | (rest (concat (url-filename parsed) |
| 9503 | (substring (url-filename parsed) | 9503 | (when-let ((target (url-target parsed))) |
| 9504 | (- (length (url-filename parsed)) | 9504 | (concat "#" target))))) |
| 9505 | (max (- max (length (url-host parsed))) 0))))))) | 9505 | (concat host (string-truncate-left rest (- max (length host))))))) |
| 9506 | 9506 | ||
| 9507 | (defun gnus-summary-browse-url (&optional external) | 9507 | (defun gnus-summary-browse-url (&optional external) |
| 9508 | "Scan the current article body for links, and offer to browse them. | 9508 | "Scan the current article body for links, and offer to browse them. |
diff --git a/lisp/vc/ediff-init.el b/lisp/vc/ediff-init.el index e59d4b57b5c..da6509b7cbe 100644 --- a/lisp/vc/ediff-init.el +++ b/lisp/vc/ediff-init.el | |||
| @@ -1510,16 +1510,6 @@ This default should work without changes." | |||
| 1510 | (setq dir (substring dir 0 pos))) | 1510 | (setq dir (substring dir 0 pos))) |
| 1511 | (ediff-abbreviate-file-name (file-name-directory dir)))) | 1511 | (ediff-abbreviate-file-name (file-name-directory dir)))) |
| 1512 | 1512 | ||
| 1513 | (defun ediff-truncate-string-left (str newlen) | ||
| 1514 | ;; leave space for ... on the left | ||
| 1515 | (let ((len (length str)) | ||
| 1516 | substr) | ||
| 1517 | (if (<= len newlen) | ||
| 1518 | str | ||
| 1519 | (setq newlen (max 0 (- newlen 3))) | ||
| 1520 | (setq substr (substring str (max 0 (- len 1 newlen)))) | ||
| 1521 | (concat "..." substr)))) | ||
| 1522 | |||
| 1523 | (defsubst ediff-nonempty-string-p (string) | 1513 | (defsubst ediff-nonempty-string-p (string) |
| 1524 | (and (stringp string) (not (string= string "")))) | 1514 | (and (stringp string) (not (string= string "")))) |
| 1525 | 1515 | ||
diff --git a/lisp/vc/ediff-mult.el b/lisp/vc/ediff-mult.el index fee87e8352e..2b1b07927f8 100644 --- a/lisp/vc/ediff-mult.el +++ b/lisp/vc/ediff-mult.el | |||
| @@ -113,7 +113,6 @@ | |||
| 113 | (require 'ediff-wind) | 113 | (require 'ediff-wind) |
| 114 | (require 'ediff-util) | 114 | (require 'ediff-util) |
| 115 | 115 | ||
| 116 | |||
| 117 | ;; meta-buffer | 116 | ;; meta-buffer |
| 118 | (ediff-defvar-local ediff-meta-buffer nil "") | 117 | (ediff-defvar-local ediff-meta-buffer nil "") |
| 119 | (ediff-defvar-local ediff-parent-meta-buffer nil "") | 118 | (ediff-defvar-local ediff-parent-meta-buffer nil "") |
| @@ -1172,7 +1171,7 @@ behavior." | |||
| 1172 | ;; abbreviate the file name, if file exists | 1171 | ;; abbreviate the file name, if file exists |
| 1173 | (if (and (not (stringp fname)) (< file-size -1)) | 1172 | (if (and (not (stringp fname)) (< file-size -1)) |
| 1174 | "-------" ; file doesn't exist | 1173 | "-------" ; file doesn't exist |
| 1175 | (ediff-truncate-string-left | 1174 | (string-truncate-left |
| 1176 | (ediff-abbreviate-file-name fname) | 1175 | (ediff-abbreviate-file-name fname) |
| 1177 | max-filename-width))))))) | 1176 | max-filename-width))))))) |
| 1178 | 1177 | ||
| @@ -1266,7 +1265,7 @@ Useful commands: | |||
| 1266 | (if (= (mod membership-code ediff-membership-code1) 0) ; dir1 | 1265 | (if (= (mod membership-code ediff-membership-code1) 0) ; dir1 |
| 1267 | (let ((beg (point))) | 1266 | (let ((beg (point))) |
| 1268 | (insert (format "%-27s" | 1267 | (insert (format "%-27s" |
| 1269 | (ediff-truncate-string-left | 1268 | (string-truncate-left |
| 1270 | (ediff-abbreviate-file-name | 1269 | (ediff-abbreviate-file-name |
| 1271 | (if (file-directory-p (concat dir1 file)) | 1270 | (if (file-directory-p (concat dir1 file)) |
| 1272 | (file-name-as-directory file) | 1271 | (file-name-as-directory file) |
| @@ -1281,7 +1280,7 @@ Useful commands: | |||
| 1281 | (if (= (mod membership-code ediff-membership-code2) 0) ; dir2 | 1280 | (if (= (mod membership-code ediff-membership-code2) 0) ; dir2 |
| 1282 | (let ((beg (point))) | 1281 | (let ((beg (point))) |
| 1283 | (insert (format "%-26s" | 1282 | (insert (format "%-26s" |
| 1284 | (ediff-truncate-string-left | 1283 | (string-truncate-left |
| 1285 | (ediff-abbreviate-file-name | 1284 | (ediff-abbreviate-file-name |
| 1286 | (if (file-directory-p (concat dir2 file)) | 1285 | (if (file-directory-p (concat dir2 file)) |
| 1287 | (file-name-as-directory file) | 1286 | (file-name-as-directory file) |
| @@ -1295,7 +1294,7 @@ Useful commands: | |||
| 1295 | (if (= (mod membership-code ediff-membership-code3) 0) ; dir3 | 1294 | (if (= (mod membership-code ediff-membership-code3) 0) ; dir3 |
| 1296 | (let ((beg (point))) | 1295 | (let ((beg (point))) |
| 1297 | (insert (format " %-25s" | 1296 | (insert (format " %-25s" |
| 1298 | (ediff-truncate-string-left | 1297 | (string-truncate-left |
| 1299 | (ediff-abbreviate-file-name | 1298 | (ediff-abbreviate-file-name |
| 1300 | (if (file-directory-p (concat dir3 file)) | 1299 | (if (file-directory-p (concat dir3 file)) |
| 1301 | (file-name-as-directory file) | 1300 | (file-name-as-directory file) |