--- orig/MoinMoin/formatter/text_html.py
+++ mod/MoinMoin/formatter/text_html.py
@@ -31,6 +31,7 @@
         self._in_code = 0
         self._base_depth = 0
         self._show_section_numbers = None
+        self._is_included = False
 
         if not hasattr(request, '_fmt_hd_counters'):
             request._fmt_hd_counters = []
@@ -197,7 +198,11 @@
         # remember depth of first heading, and adapt counting depth accordingly
         if not self._base_depth:
             self._base_depth = depth
-        count_depth = max(depth - (self._base_depth - 1), 1)
+
+        if not self._is_included:
+            count_depth = max(depth - (self._base_depth - 1), 1)
+        else:
+            count_depth = depth
 
         # check numbering, possibly changing the default
         if self._show_section_numbers is None:


--- orig/MoinMoin/macro/Include.py
+++ mod/MoinMoin/macro/Include.py
@@ -19,7 +19,7 @@
 
 _sysmsg = '<p><strong class="%s">%s</strong></p>'
 _arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'
-_arg_level = r',\s*(?P<level>\d+)'
+_arg_level = r',\s*(?P<level>\d*)'
 _arg_from = r'(,\s*from=(?P<fquote>[\'"])(?P<from>.+?)(?P=fquote))?'
 _arg_to = r'(,\s*to=(?P<tquote>[\'"])(?P<to>.+?)(?P=tquote))?'
 _arg_sort = r'(,\s*sort=(?P<sort>(ascending|descending)))?'
@@ -45,7 +45,7 @@
 
 Dependencies = ["pages"] # included page
 
-def execute(macro, text, args_re=re.compile(_args_re_pattern)):
+def execute(macro, text, args_re=re.compile(_args_re_pattern), called_by_toc=0):
     _ = macro.request.getText
 
     # return immediately if getting links for the current page
@@ -160,6 +160,10 @@
         ##result.append("*** f=%s t=%s ***" % (from_re, to_re))
         ##result.append("*** f=%d t=%d ***" % (from_pos, to_pos))
 
+        if called_by_toc:
+            result.append(inc_page.get_raw_body())
+            continue
+
         # edit icon
         edit_icon = inc_page.link_to(macro.request,
             macro.request.theme.make_icon("edit"),
@@ -177,8 +181,10 @@
             if print_mode:
                 result.append(macro.formatter.heading(level, heading))
             else:
+                import sha
                 result.append(macro.formatter.heading(level,
                     inc_page.link_to(macro.request, heading, css_class="include-heading-link"),
+                    id="head-"+sha.new(inc_name + heading).hexdigest(),
                     icons=edit_icon.replace('<img ', '<img align="right" ')))
 
         # set or increment include marker
@@ -189,7 +195,17 @@
         strfile = cStringIO.StringIO()
         macro.request.redirect(strfile)
         try:
+            inc_page.formatter._is_included = True
+            # caching prevents Include macro from working correctly,
+            # especially with "from" and "to" parameter.
+            # so disable caching feature temporarily when showing the page.
+            caching_flag = False
+            if "text_html" in config.caching_formats:
+                caching_flag = True
+                config.caching_formats.remove("text_html")
             inc_page.send_page(macro.request, content_only=1, content_id="Include_%s" % wikiutil.quoteWikiname(inc_page.page_name) )
+            if caching_flag:
+                config.caching_formats.append("text_html")
             result.append(strfile.getvalue())
         finally:
             macro.request.redirect()


--- orig/MoinMoin/macro/TableOfContents.py
+++ mod/MoinMoin/macro/TableOfContents.py
@@ -10,11 +10,104 @@
 
 # Imports
 import re, sha
+from MoinMoin import wikiutil
 
 Dependencies = ["page"]
 
-def execute(macro, args):
+def parse_line(line, macro, pagename):
+    global result
+    global baseindent
+    global indent
+    global titles
+    global mindepth
+    global maxdepth
+
     heading = re.compile(r"^\s*(?P<hmarker>=+)\s(.*)\s(?P=hmarker)$")
+    # FIXME this also finds "headlines" in {{{ code sections }}}:
+    match = heading.search(line)
+    if not match: return
+    title_text = match.group(2)
+    titles.setdefault(pagename + title_text, 0)
+    titles[pagename + title_text] += 1
+
+    # Get new indent level
+    newindent = len(match.group(1))
+    if newindent > maxdepth: return
+    if newindent < mindepth: return
+    if not indent:
+        baseindent = newindent - 1
+        indent = baseindent
+
+    # Close lists
+    for i in range(0,indent-newindent):
+        result.append(macro.formatter.number_list(0))
+
+    # Open Lists
+    for i in range(0,newindent-indent):
+        result.append(macro.formatter.number_list(1))
+
+    # Add the heading
+    unique_id = ''
+    if titles[pagename + title_text] > 1:
+        unique_id = '-%d' % titles[pagename + title_text]
+
+    result.append(macro.formatter.listitem(1))
+    result.append(macro.formatter.anchorlink(
+        "head-" + sha.new(pagename + title_text).hexdigest() + unique_id, title_text))
+    result.append(macro.formatter.listitem(0))
+    
+    # Set new indent level
+    indent = newindent
+
+def process_lines(macro, lines, pagename):
+    global lineno
+    global include
+    for line in lines:
+        # Filter out the headings
+        lineno = lineno + 1
+        match = include.match(line)
+        if match:
+            # this is an [[Include()]] line.
+            # now parse the included page and do the work on it.
+
+            ## get heading and level from Include() line.
+            args = r'^(?P<name>[^,]+),?(?:\s*(?:(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))?,?(?:\s*(?P<level>\d*))?)?'
+
+            tmp = re.search(args, match.group(1))
+            if tmp and tmp.group("name"):
+                inc_pagename = tmp.group("name")
+            else:
+                # no pagename?  ignore it
+                continue
+            if tmp.group("htext"):
+                heading = tmp.group("htext")
+                if tmp.group("level"):
+                    level = int(tmp.group("level"))
+                else:
+                    level = 1
+                tmp_line = "%s %s %s" % ("=" * level, heading, "=" * level)
+                inc_page_lines = [tmp_line]
+            else:
+                inc_page_lines = []
+
+            include_macro = wikiutil.importPlugin('macro', "Include")
+            inc_page_lines = inc_page_lines \
+                + include_macro(macro, match.group(1), called_by_toc=1).split("\n")
+            process_lines(macro, inc_page_lines, inc_pagename)
+        else:
+            parse_line(line, macro, pagename)
+
+def execute(macro, args):
+    global result
+    global baseindent
+    global indent
+    global titles
+    global mindepth
+    global maxdepth
+    global lineno
+    global include
+
+    include = re.compile(r"^\[\[Include\((.*)\)\]\]")
     result = []
     baseindent = 0
     indent = 0
@@ -31,44 +124,8 @@
     except (ValueError, TypeError):
         maxdepth = 99
 
-    for line in macro.parser.lines:
-        # Filter out the headings
-        lineno = lineno + 1
-        # FIXME this also finds "headlines" in {{{ code sections }}}:
-        match = heading.match(line)
-        if not match: continue
-        title_text = match.group(2)
-        titles.setdefault(title_text, 0)
-        titles[title_text] += 1
-
-        # Get new indent level
-        newindent = len(match.group(1))
-        if newindent > maxdepth: continue
-        if newindent < mindepth: continue
-        if not indent:
-            baseindent = newindent - 1
-            indent = baseindent
-
-        # Close lists
-        for i in range(0,indent-newindent):
-            result.append(macro.formatter.number_list(0))
-
-        # Open Lists
-        for i in range(0,newindent-indent):
-            result.append(macro.formatter.number_list(1))
-
-        # Add the heading
-        unique_id = ''
-        if titles[title_text] > 1:
-            unique_id = '-%d' % titles[title_text]
-
-        result.append(macro.formatter.listitem(1))
-        result.append(macro.formatter.anchorlink(
-            "head-" + sha.new(title_text).hexdigest() + unique_id, title_text))
-        result.append(macro.formatter.listitem(0))
-        
-        # Set new indent level
-        indent = newindent
+    pagename = macro.formatter.page.page_name
+    process_lines(macro, macro.parser.lines, pagename)
 
     # Close pending lists
     for i in range(baseindent, indent):


--- orig/MoinMoin/parser/wiki.py
+++ mod/MoinMoin/parser/wiki.py
@@ -737,15 +737,16 @@
             level = level+1
         depth = min(5,level)
 
+        pagename = self.formatter.page.page_name
         title_text = h[level:-level].strip()
-        self.titles.setdefault(title_text, 0)
-        self.titles[title_text] += 1
+        self.titles.setdefault(pagename + title_text, 0)
+        self.titles[pagename + title_text] += 1
 
         unique_id = ''
-        if self.titles[title_text] > 1:
-            unique_id = '-%d' % self.titles[title_text]
+        if self.titles[pagename + title_text] > 1:
+            unique_id = '-%d' % self.titles[pagename + title_text]
 
-        return self.formatter.heading(depth, self.highlight_text(title_text), icons=icons, id="head-"+sha.new(title_text).hexdigest()+unique_id)
+        return self.formatter.heading(depth, self.highlight_text(title_text), icons=icons, id="head-"+sha.new(pagename + title_text).hexdigest()+unique_id)
 
 
     def _processor_repl(self, word):


