]> git.eshelyaron.com Git - emacs.git/commitdiff
tildify.el: Fix matched group indexes in end-regex building
authorMichal Nazarewicz <mina86@mina86.com>
Thu, 5 Jun 2014 14:39:18 +0000 (16:39 +0200)
committerMichal Nazarewicz <mina86@mina86.com>
Thu, 5 Jun 2014 14:39:18 +0000 (16:39 +0200)
* 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.

lisp/ChangeLog
lisp/textmodes/tildify.el
test/ChangeLog
test/automated/tildify-tests.el

index 7bbaf642d3c4ef6c80b08e1c0ea5bd8eb2d86210..936ae225a8be308de0546ba8c436633e8f1a0a1d 100644 (file)
@@ -1,5 +1,28 @@
 2014-06-05  Michal Nazarewicz  <mina86@mina86.com>
 
+       * textmodes/tildify.el (tildify-find-env): Fix matched group
+       indexes in end-regex building
+
+       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.
+
        * textmodes/tildify.el (tildify-find-env): Fix end-regex building
        in `tildify-find-env'
 
index 7e4b39b615a9c7ec7c293db4a6d05af5440050cb..7aa338ef2079b6710388b5a288c71414dc1d2701 100644 (file)
@@ -271,22 +271,22 @@ Return regexp for the end of the environment or nil if no environment was
 found."
   ;; Find environment
   (when (re-search-forward regexp nil t)
-    ;; Build end-env regexp
-    (let ((match (match-string 0))
-          (alist (tildify-mode-alist tildify-ignored-environments-alist)))
-      (save-match-data
+    (save-match-data
+      ;; Build end-env regexp
+      (let ((match (match-string 0))
+            (alist (tildify-mode-alist tildify-ignored-environments-alist)))
         (while (not (eq (string-match (caar alist) match) 0))
-          (setq alist (cdr alist))))
-      (let ((expression (cdar alist)))
-        (if (stringp expression)
-            expression
-          (mapconcat
-           (lambda (expr)
-             (if (stringp expr)
-                 expr
-               (regexp-quote (match-string expr))))
-           expression
-           ""))))))
+          (setq alist (cdr alist)))
+        (let ((expression (cdar alist)))
+          (if (stringp expression)
+              expression
+            (mapconcat
+             (lambda (expr)
+               (if (stringp expr)
+                   expr
+                 (regexp-quote (match-string expr match))))
+             expression
+             "")))))))
 
 (defun tildify-tildify (beg end ask)
   "Add tilde characters in the region between BEG and END.
index db32aae578be55a6a207d53b3f05303e2f768fef..93ef0980c9ae57dd713ad782c092b61e29c62d43 100644 (file)
@@ -1,5 +1,9 @@
 2014-06-05  Michal Nazarewicz  <mina86@mina86.com>
 
+       * automated/tildify-tests.el (tildify-test-find-env-group-index-bug):
+       New test checking end-regex building when multiple environment pairs
+       use integers to refer to capture groups.
+
        * automated/tildify-tests.el (tildify-test-find-env-end-re-bug): New
        test checking end-regex building in `tildify-find-env' function when
        integers (denoting capture groups) and strings are mixed together.
index 25e9f22adf343b117b3e80c00c7a007a834266b6..6fee28b386277e2cd34b83945b52fd837d11678d 100644 (file)
@@ -112,6 +112,18 @@ latter is missing, SENTENCE will be used in all placeholder positions."
       (should (string-equal "end-foo" (tildify-find-env "foo\\|bar"))))))
 
 
+(ert-deftest tildify-test-find-env-group-index-bug ()
+    "Tests generation of match-string indexes"
+  (with-temp-buffer
+    (let ((tildify-ignored-environments-alist
+           `((,major-mode ("start-\\(foo\\|bar\\)" . ("end-" 1))
+                          ("open-\\(foo\\|bar\\)" . ("close-" 1)))))
+          (beg-re "start-\\(foo\\|bar\\)\\|open-\\(foo\\|bar\\)"))
+      (insert "open-foo whatever close-foo")
+      (goto-char (point-min))
+      (should (string-equal "close-foo" (tildify-find-env beg-re))))))
+
+
 (provide 'tildify-tests)
 
 ;;; tildify-tests.el ends here