about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cpp/tangle/030tangle.cc20
-rw-r--r--cpp/tangle/030tangle.test.cc7
2 files changed, 27 insertions, 0 deletions
diff --git a/cpp/tangle/030tangle.cc b/cpp/tangle/030tangle.cc
index bf2d3c66..2d1bc191 100644
--- a/cpp/tangle/030tangle.cc
+++ b/cpp/tangle/030tangle.cc
@@ -76,6 +76,19 @@ void process_next_hunk(istream& in, const string& directive, list<string>& out)
       return;
     }
 
+    if (next_tangle_token(directive_stream) == "then") {
+      string pat2 = next_tangle_token(directive_stream);
+      if (pat2 == "") {
+        RAISE << "No target for then directive following " << cmd << ".\n" << die();
+        return;
+      }
+      target = find_substr(out, target, pat2);
+      if (target == out.end()) {
+        RAISE << "Couldn't find target " << pat << " then " << pat2 << '\n' << die();
+        return;
+      }
+    }
+
     indent_all(hunk, target);
 
     if (cmd == "before") {
@@ -264,6 +277,13 @@ list<string>::iterator find_substr(list<string>& in, const string& pat) {
   return in.end();
 }
 
+list<string>::iterator find_substr(list<string>& in, list<string>::iterator p, const string& pat) {
+  for (; p != in.end(); ++p)
+    if (p->find(pat) != NOT_FOUND)
+      return p;
+  return in.end();
+}
+
 list<string>::iterator find_trim(list<string>& in, const string& pat) {
   for (list<string>::iterator p = in.begin(); p != in.end(); ++p)
     if (trim(*p) == pat)
diff --git a/cpp/tangle/030tangle.test.cc b/cpp/tangle/030tangle.test.cc
index 663161ba..cf42d71c 100644
--- a/cpp/tangle/030tangle.test.cc
+++ b/cpp/tangle/030tangle.test.cc
@@ -76,6 +76,13 @@ void test_tangle_replace_tracks_old_lines() {
   CHECK_TRACE_DOESNT_CONTAIN("tangle", "b {");
 }
 
+void test_tangle_nested_patterns() {
+  istringstream in("a\nc\nb\nc\nd\n:(after \"b\" then \"c\")\ne");
+  list<string> dummy;
+  tangle(in, dummy);
+  CHECK_TRACE_CONTENTS("tangle", "acbced");
+}
+
 // todo: include line numbers in tangle errors