unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michal Nazarewicz <mina86@mina86.com>
To: 17699@debbugs.gnu.org
Subject: bug#17699: [PATCH 2/7] tildify.el: Fix matched group indexes in end-regex building
Date: Thu,  5 Jun 2014 13:27:31 +0200	[thread overview]
Message-ID: <1401967656-16171-2-git-send-email-mina86@mina86.com> (raw)
In-Reply-To: <1401967656-16171-1-git-send-email-mina86@mina86.com>

* 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                  | 23 +++++++++++++++++++++++
 lisp/textmodes/tildify.el       | 30 +++++++++++++++---------------
 test/ChangeLog                  |  4 ++++
 test/automated/tildify-tests.el | 12 ++++++++++++
 4 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index dc3c42a..523b2a9 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -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'
 
diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index 7e4b39b..7aa338e 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
 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.
diff --git a/test/ChangeLog b/test/ChangeLog
index db32aae..93ef098 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -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.
diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el
index 25e9f22..6fee28b 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."
       (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
-- 
2.0.0.526.g5318336






  reply	other threads:[~2014-06-05 11:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-05 11:24 bug#17699: Various tildify.el improvements Michal Nazarewicz
2014-06-05 11:27 ` bug#17699: [PATCH 1/7] tildify.el: Fix end-regex building in `tildify-find-env' Michal Nazarewicz
2014-06-05 11:27   ` Michal Nazarewicz [this message]
2014-06-05 11:27   ` bug#17699: [PATCH 3/7] tildify.el: Improve defcustom's types Michal Nazarewicz
2014-06-05 11:27   ` bug#17699: [PATCH 4/7] tildify.el: Better support for XML Michal Nazarewicz
2014-06-05 11:27   ` bug#17699: [PATCH 5/7] tildify.el: Optimise environments regexes Michal Nazarewicz
2014-06-05 11:27   ` bug#17699: [PATCH 6/7] tildify.el: Rewrite `tildify-region' and co., add foreach function Michal Nazarewicz
2014-06-05 11:27   ` bug#17699: [PATCH 7/7] * tests/automated/tildify-tests.el (tildify-test--test): Optimise the test slightly by reusing the same temporary buffer across multiple test cases Michal Nazarewicz
2014-06-05 13:54 ` bug#17699: Various tildify.el improvements Stefan Monnier
2014-06-05 14:47   ` Michal Nazarewicz
2014-06-06 17:35     ` Glenn Morris
2014-06-06 18:13       ` Michal Nazarewicz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1401967656-16171-2-git-send-email-mina86@mina86.com \
    --to=mina86@mina86.com \
    --cc=17699@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).