path stuff, autohandler ext not quite working, etc
diff --git a/lib/mako/exceptions.py b/lib/mako/exceptions.py
index 3f6c597..4891fdd 100644
--- a/lib/mako/exceptions.py
+++ b/lib/mako/exceptions.py
@@ -43,7 +43,9 @@
     """pulls the current exception from the sys traceback and extracts Mako-specific information."""
     def __init__(self):
         (self.source, self.lineno) = ("", 0)
-        (self.type, self.error, self.records) = self._init()
+        (t, self.error, self.records) = self._init()
+        if self.error is None:
+            self.error = t
         if isinstance(self.error, CompileException) or isinstance(self.error, SyntaxException):
             self.source = file(self.error.filename).read()
             self.lineno = self.error.lineno
diff --git a/lib/mako/ext/__init__.py b/lib/mako/ext/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/mako/ext/__init__.py
diff --git a/lib/mako/ext/autohandler.py b/lib/mako/ext/autohandler.py
new file mode 100644
index 0000000..843808f
--- /dev/null
+++ b/lib/mako/ext/autohandler.py
@@ -0,0 +1,22 @@
+"""adds autohandler functionality to Mako templates"""
+
+import posixpath, os, re
+
+def autohandler(context, _template_filename, name='autohandler'):
+    lookup = context.lookup
+    uri = lookup.filename_to_uri(_template_filename)
+    tokens = list(posixpath.split(posixpath.dirname(uri) + "/" + name))
+    if posixpath.basename(uri) == name:
+        tokens[-2:] = [name]
+    while len(tokens):
+        path = posixpath.join(*tokens)
+        psub = re.sub(r'^/', '',path)
+        print "TOKENS", tokens
+        for d in lookup.directories:
+            print "FILE", posixpath.join(d, psub)
+            if os.access(posixpath.join(d, psub), os.F_OK):
+                return path
+        if len(tokens) == 1:
+            break
+        tokens[-2:] = [name]
+        print "NOW THE TOKENS ARE:", tokens
\ No newline at end of file
diff --git a/lib/mako/lookup.py b/lib/mako/lookup.py
index b45643f..8ef1e3b 100644
--- a/lib/mako/lookup.py
+++ b/lib/mako/lookup.py
@@ -22,6 +22,10 @@
             return False
     def get_template(self, uri, relativeto=None):
         raise NotImplementedError()
+    def filename_to_uri(self, uri, filename):
+        """convert the given filename to a uri relative to this TemplateCollection."""
+        return uri
+        
     def adjust_uri(self, uri, filename):
         """adjust the given uri based on the calling filename.
         
@@ -65,24 +69,32 @@
     def adjust_uri(self, uri, filename):
         """adjust the given uri based on the calling filename."""
         try:
-            return self.__uri_convert[uri]
+            return self.__uri_convert[(uri, filename)]
         except KeyError:
             if uri[0] != '/':
                 u = posixpath.normpath(uri)
                 if filename is not None:
                     rr = self.__relativeize(filename)
                     if rr is not None:
-                        u = posixpath.dirname(rr) + u
-                return self.__uri_convert.setdefault(uri, u)
+                        u = posixpath.join(posixpath.dirname(rr), u)
+                    else:
+                        u = posixpath.join('/', u)
+                return self.__uri_convert.setdefault((uri, filename), u)
             else:
-                return self.__uri_convert.setdefault(uri, uri)
-                
+                return self.__uri_convert.setdefault((uri, filename), uri)
+    
+    def filename_to_uri(self, filename):
+        try:
+            return self.__uri_convert[filename]
+        except KeyError:
+            return self.__uri_convert.setdefault(filename, self.__relativeize(filename))
+                    
     def __relativeize(self, filename):
         """return the portion of a filename that is 'relative' to the directories in this lookup."""
         filename = posixpath.normpath(filename)
         for dir in self.directories:
             if filename[0:len(dir)] == dir:
-                return filename[len(dir) + 1:]
+                return filename[len(dir):]
         else:
             return None
             
diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py
index c98b914..78dce44 100644
--- a/lib/mako/runtime.py
+++ b/lib/mako/runtime.py
@@ -22,7 +22,7 @@
         # "caller" stack used by def calls with content
         self.caller_stack = [Undefined]
         data['caller'] = _StackFacade(self.caller_stack)
-
+    lookup = property(lambda self:self._with_template.lookup)
     def keys(self):
         return self._data.keys()
     def __getitem__(self, key):
diff --git a/test/lookup.py b/test/lookup.py
index 75c660b..4d8b0ee 100644
--- a/test/lookup.py
+++ b/test/lookup.py
@@ -29,10 +29,17 @@
         assert result_lines(t.render()) == [
             "this is sub index",
             "this is include 2"
-        ]
-        assert tl.get_template('/subdir/index.html').identifier == 'subdir_index_html'
 
-        t2 = tl.get_template('./subdir/index.html')
+        ]
+
+        assert tl.get_template('/subdir/index.html').identifier == '_subdir_index_html'
+        
+    def test_uri_adjust(self):
+        tl = lookup.TemplateLookup(directories=['/foo/bar'])
+        assert tl.filename_to_uri('/foo/bar/etc/lala/index.html') == '/etc/lala/index.html'
+
+        tl = lookup.TemplateLookup(directories=['./foo/bar'])
+        assert tl.filename_to_uri('./foo/bar/etc/index.html') == '/etc/index.html'
         
 if __name__ == '__main__':
     unittest.main()
\ No newline at end of file