aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Nazarewicz2014-06-05 16:39:18 +0200
committerMichal Nazarewicz2014-06-05 16:39:18 +0200
commit9342feecdd92b769b1f45a6feea8ad34985c5049 (patch)
tree5be0d974eccfc0bb98effbe6735cb29271f13d69
parentaf9a3b28c0ca250ed245bd54c8737792916fe4c6 (diff)
downloademacs-9342feecdd92b769b1f45a6feea8ad34985c5049.tar.gz
emacs-9342feecdd92b769b1f45a6feea8ad34985c5049.zip
tildify.el: Fix matched group indexes in end-regex building
* lisp/textmodes/tildifi.el (tildify-find-env): When looking for a start of an ignore-environment, the regex is built by concatenating regexes of all the environments configured in `tildify-ignored-environments-alist'. So for example, the following list could be used to match TeX's \verb and \verb* commands: (("\\\\verb\\(.\\)" . (1)) ("\\\\verb\\*\\(.\\)" . (1))) This would result in the following regex being used to find the start of any of the variants of the \verb command: \\\\verb\\(.\\)\\|\\\\verb\\*\\(.\\) But now, if “\\\\verb\\*\\(.\\)” matches, the first capture group won't match anything, and thus (match-string 1) will be nil, which will cause building of the end-matching regex to fail. Fix this by using capture groups from the time when the opening regexes are matched individually. * tests/automated/tildify-tests.el (tildify-test-find-env-group-index-bug): New test validating fix to the above bug.
-rw-r--r--lisp/ChangeLog23
-rw-r--r--lisp/textmodes/tildify.el30
-rw-r--r--test/ChangeLog4
-rw-r--r--test/automated/tildify-tests.el12
4 files changed, 54 insertions, 15 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 7bbaf642d3c..936ae225a8b 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,28 @@
12014-06-05 Michal Nazarewicz <mina86@mina86.com> 12014-06-05 Michal Nazarewicz <mina86@mina86.com>
2 2
3 * textmodes/tildify.el (tildify-find-env): Fix matched group
4 indexes in end-regex building
5
6 When looking for a start of an ignore-environment, the regex is built
7 by concatenating regexes of all the environments configured in
8 `tildify-ignored-environments-alist'. So for example, the following
9 list could be used to match TeX's \verb and \verb* commands:
10
11 (("\\\\verb\\(.\\)" . (1))
12 ("\\\\verb\\*\\(.\\)" . (1)))
13
14 This would result in the following regex being used to find the start
15 of any of the variants of the \verb command:
16
17 \\\\verb\\(.\\)\\|\\\\verb\\*\\(.\\)
18
19 But now, if “\\\\verb\\*\\(.\\)” matches, the first capture group
20 won't match anything, and thus (match-string 1) will be nil, which
21 will cause building of the end-matching regex to fail.
22
23 Fix this by using capture groups from the time when the opening
24 regexes are matched individually.
25
3 * textmodes/tildify.el (tildify-find-env): Fix end-regex building 26 * textmodes/tildify.el (tildify-find-env): Fix end-regex building
4 in `tildify-find-env' 27 in `tildify-find-env'
5 28
diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index 7e4b39b615a..7aa338ef207 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -271,22 +271,22 @@ Return regexp for the end of the environment or nil if no environment was
271found." 271found."
272 ;; Find environment 272 ;; Find environment
273 (when (re-search-forward regexp nil t) 273 (when (re-search-forward regexp nil t)
274 ;; Build end-env regexp 274 (save-match-data
275 (let ((match (match-string 0)) 275 ;; Build end-env regexp
276 (alist (tildify-mode-alist tildify-ignored-environments-alist))) 276 (let ((match (match-string 0))
277 (save-match-data 277 (alist (tildify-mode-alist tildify-ignored-environments-alist)))
278 (while (not (eq (string-match (caar alist) match) 0)) 278 (while (not (eq (string-match (caar alist) match) 0))
279 (setq alist (cdr alist)))) 279 (setq alist (cdr alist)))
280 (let ((expression (cdar alist))) 280 (let ((expression (cdar alist)))
281 (if (stringp expression) 281 (if (stringp expression)
282 expression 282 expression
283 (mapconcat 283 (mapconcat
284 (lambda (expr) 284 (lambda (expr)
285 (if (stringp expr) 285 (if (stringp expr)
286 expr 286 expr
287 (regexp-quote (match-string expr)))) 287 (regexp-quote (match-string expr match))))
288 expression 288 expression
289 "")))))) 289 "")))))))
290 290
291(defun tildify-tildify (beg end ask) 291(defun tildify-tildify (beg end ask)
292 "Add tilde characters in the region between BEG and END. 292 "Add tilde characters in the region between BEG and END.
diff --git a/test/ChangeLog b/test/ChangeLog
index db32aae578b..93ef0980c9a 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,5 +1,9 @@
12014-06-05 Michal Nazarewicz <mina86@mina86.com> 12014-06-05 Michal Nazarewicz <mina86@mina86.com>
2 2
3 * automated/tildify-tests.el (tildify-test-find-env-group-index-bug):
4 New test checking end-regex building when multiple environment pairs
5 use integers to refer to capture groups.
6
3 * automated/tildify-tests.el (tildify-test-find-env-end-re-bug): New 7 * automated/tildify-tests.el (tildify-test-find-env-end-re-bug): New
4 test checking end-regex building in `tildify-find-env' function when 8 test checking end-regex building in `tildify-find-env' function when
5 integers (denoting capture groups) and strings are mixed together. 9 integers (denoting capture groups) and strings are mixed together.
diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el
index 25e9f22adf3..6fee28b3862 100644
--- a/test/automated/tildify-tests.el
+++ b/test/automated/tildify-tests.el
@@ -112,6 +112,18 @@ latter is missing, SENTENCE will be used in all placeholder positions."
112 (should (string-equal "end-foo" (tildify-find-env "foo\\|bar")))))) 112 (should (string-equal "end-foo" (tildify-find-env "foo\\|bar"))))))
113 113
114 114
115(ert-deftest tildify-test-find-env-group-index-bug ()
116 "Tests generation of match-string indexes"
117 (with-temp-buffer
118 (let ((tildify-ignored-environments-alist
119 `((,major-mode ("start-\\(foo\\|bar\\)" . ("end-" 1))
120 ("open-\\(foo\\|bar\\)" . ("close-" 1)))))
121 (beg-re "start-\\(foo\\|bar\\)\\|open-\\(foo\\|bar\\)"))
122 (insert "open-foo whatever close-foo")
123 (goto-char (point-min))
124 (should (string-equal "close-foo" (tildify-find-env beg-re))))))
125
126
115(provide 'tildify-tests) 127(provide 'tildify-tests)
116 128
117;;; tildify-tests.el ends here 129;;; tildify-tests.el ends here