diff options
| author | Tino Calancha | 2016-12-30 15:31:01 +0900 |
|---|---|---|
| committer | Tino Calancha | 2016-12-30 15:31:01 +0900 |
| commit | c336420d9f2ffe5270d7deec360d84e1f45b4a55 (patch) | |
| tree | edbab7c255781c4963da04896c90bc706c90c104 | |
| parent | 9672f2c916b5909cc5836f67edc4d66842cce7cd (diff) | |
| download | emacs-c336420d9f2ffe5270d7deec360d84e1f45b4a55.tar.gz emacs-c336420d9f2ffe5270d7deec360d84e1f45b4a55.zip | |
ffap-string-at-point: Limit max length of active region
Prevents that 'ffap-guesser' waste time checking large strings
which are likely not valid candidates (Bug#25243).
* lisp/ffap.el (ffap-max-region-length): New variable.
(ffap-string-at-point): Use it.
* test/lisp/ffap-tests.el: New test suite.
(ffap-tests-25243): Add test for this bug.
| -rw-r--r-- | lisp/ffap.el | 25 | ||||
| -rw-r--r-- | test/lisp/ffap-tests.el | 54 |
2 files changed, 72 insertions, 7 deletions
diff --git a/lisp/ffap.el b/lisp/ffap.el index 3d7cebadcf6..99bb65faafe 100644 --- a/lisp/ffap.el +++ b/lisp/ffap.el | |||
| @@ -203,6 +203,11 @@ Sensible values are nil, \"news\", or \"mailto\"." | |||
| 203 | ) | 203 | ) |
| 204 | :group 'ffap) | 204 | :group 'ffap) |
| 205 | 205 | ||
| 206 | (defvar ffap-max-region-length 1024 | ||
| 207 | "Maximum active region length. | ||
| 208 | When the region is active and larger than this value, | ||
| 209 | `ffap-string-at-point' returns an empty string.") | ||
| 210 | |||
| 206 | 211 | ||
| 207 | ;;; Peanut Gallery (More User Variables): | 212 | ;;; Peanut Gallery (More User Variables): |
| 208 | ;; | 213 | ;; |
| @@ -1101,8 +1106,10 @@ MODE (defaults to value of `major-mode') is a symbol used to look up | |||
| 1101 | string syntax parameters in `ffap-string-at-point-mode-alist'. | 1106 | string syntax parameters in `ffap-string-at-point-mode-alist'. |
| 1102 | If MODE is not found, we use `file' instead of MODE. | 1107 | If MODE is not found, we use `file' instead of MODE. |
| 1103 | If the region is active, return a string from the region. | 1108 | If the region is active, return a string from the region. |
| 1104 | Sets the variable `ffap-string-at-point' and the variable | 1109 | Set the variable `ffap-string-at-point' and the variable |
| 1105 | `ffap-string-at-point-region'." | 1110 | `ffap-string-at-point-region'. |
| 1111 | When the region is active and larger than `ffap-max-region-length', | ||
| 1112 | return an empty string, and set `ffap-string-at-point-region' to '(1 1)." | ||
| 1106 | (let* ((args | 1113 | (let* ((args |
| 1107 | (cdr | 1114 | (cdr |
| 1108 | (or (assq (or mode major-mode) ffap-string-at-point-mode-alist) | 1115 | (or (assq (or mode major-mode) ffap-string-at-point-mode-alist) |
| @@ -1119,11 +1126,15 @@ Sets the variable `ffap-string-at-point' and the variable | |||
| 1119 | (save-excursion | 1126 | (save-excursion |
| 1120 | (skip-chars-forward (car args)) | 1127 | (skip-chars-forward (car args)) |
| 1121 | (skip-chars-backward (nth 2 args) pt) | 1128 | (skip-chars-backward (nth 2 args) pt) |
| 1122 | (point))))) | 1129 | (point)))) |
| 1123 | (setq ffap-string-at-point | 1130 | (region-len (- (max beg end) (min beg end)))) |
| 1124 | (buffer-substring-no-properties | 1131 | (if (and (natnump ffap-max-region-length) |
| 1125 | (setcar ffap-string-at-point-region beg) | 1132 | (< region-len ffap-max-region-length)) ; Bug#25243. |
| 1126 | (setcar (cdr ffap-string-at-point-region) end))))) | 1133 | (setf ffap-string-at-point-region (list beg end) |
| 1134 | ffap-string-at-point | ||
| 1135 | (buffer-substring-no-properties beg end)) | ||
| 1136 | (setf ffap-string-at-point-region (list 1 1) | ||
| 1137 | ffap-string-at-point "")))) | ||
| 1127 | 1138 | ||
| 1128 | (defun ffap-string-around () | 1139 | (defun ffap-string-around () |
| 1129 | ;; Sometimes useful to decide how to treat a string. | 1140 | ;; Sometimes useful to decide how to treat a string. |
diff --git a/test/lisp/ffap-tests.el b/test/lisp/ffap-tests.el new file mode 100644 index 00000000000..61fa891fe72 --- /dev/null +++ b/test/lisp/ffap-tests.el | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | ;;; ffap-tests.el --- Test suite for ffap.el -*- lexical-binding: t -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2016 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Tino Calancha <tino.calancha@gmail.com> | ||
| 6 | |||
| 7 | ;; This file is part of GNU Emacs. | ||
| 8 | |||
| 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 10 | ;; it under the terms of the GNU General Public License as published by | ||
| 11 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 12 | ;; (at your option) any later version. | ||
| 13 | |||
| 14 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | ;; GNU General Public License for more details. | ||
| 18 | |||
| 19 | ;; You should have received a copy of the GNU General Public License | ||
| 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | |||
| 22 | ;;; Commentary: | ||
| 23 | |||
| 24 | ;;; Code: | ||
| 25 | |||
| 26 | (require 'ert) | ||
| 27 | (require 'ffap) | ||
| 28 | |||
| 29 | (ert-deftest ffap-tests-25243 () | ||
| 30 | "Test for http://debbugs.gnu.org/25243 ." | ||
| 31 | (let ((file (make-temp-file "test-Bug#25243"))) | ||
| 32 | (unwind-protect | ||
| 33 | (with-temp-file file | ||
| 34 | (let ((str "diff --git b/lisp/ffap.el a/lisp/ffap.el | ||
| 35 | index 3d7cebadcf..ad4b70d737 100644 | ||
| 36 | --- b/lisp/ffap.el | ||
| 37 | +++ a/lisp/ffap.el | ||
| 38 | @@ -203,6 +203,9 @@ ffap-foo-at-bar-prefix | ||
| 39 | ")) | ||
| 40 | (transient-mark-mode 1) | ||
| 41 | (when (natnump ffap-max-region-length) | ||
| 42 | (insert | ||
| 43 | (concat | ||
| 44 | str | ||
| 45 | (make-string ffap-max-region-length #xa) | ||
| 46 | (format "%s ENDS HERE" file))) | ||
| 47 | (mark-whole-buffer) | ||
| 48 | (should (equal "" (ffap-string-at-point))) | ||
| 49 | (should (equal '(1 1) ffap-string-at-point-region))))) | ||
| 50 | (and (file-exists-p file) (delete-file file))))) | ||
| 51 | |||
| 52 | (provide 'ffap-tests) | ||
| 53 | |||
| 54 | ;;; ffap-tests.el ends here | ||