--- /Users/larry/Desktop/TemplateViewer_bkup.py	Wed Jan  6 09:23:42 2010
+++ /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/eric4/Templates/TemplateViewer.py	Wed Jan  6 09:08:43 2010
@@ -307,7 +307,8 @@
         lines = txt.splitlines()
         lineCount = len(lines)
         lineLen = len(lines[-1])
-        txt = prefix.join(lines).lstrip()
+#        txt = prefix.join(lines).lstrip()
+        txt = prefix.join(lines)
         if trailingEol:
             txt = "%s%s" % (txt, os.linesep)
             lineCount += 1
@@ -604,6 +605,9 @@
         if project.name:
             varValues[keyfmt % 'project_name'] = project.name
 
+        if project.ppath:
+            varValues[keyfmt % 'project_path'] = project.ppath
+
         path_name = editor.getFileName()
         if path_name:
             dir_name, file_name = os.path.split(path_name)
@@ -617,6 +621,17 @@
                     keyfmt % 'base_name': base_name,
                     keyfmt % 'ext': ext
             })
+
+        if editor.hasSelectedText():
+            varValues[keyfmt % 'cur_select:ml'] = unicode(editor.selectedText() )
+        else:
+            varValues[keyfmt % 'cur_select:ml'] = os.linesep
+
+        varValues[keyfmt % 'insertion'] = unicode("i_n_s_e_r_t_i_o_n")
+        
+        varValues[keyfmt % 'select_start'] = unicode("s_e_l_e_c_t_s_t_a_r_t")
+        varValues[keyfmt % 'select_end'] = unicode("s_e_l_e_c_t_e_n_d")
+
         return varValues
 
     def applyTemplate(self, itm):
@@ -665,11 +680,54 @@
             # It should be done on this way to allow undo
             editor.beginUndoAction()
             if editor.hasSelectedText():
+                line = editor.getSelection()[0]
                 editor.removeSelectedText()
-            line, index = editor.getCursorPosition()
+            else:
+                line = editor.getCursorPosition()[0]
+
+            if indent > 0:
+                    count += len(indent)
+            
+            if "i_n_s_e_r_t_i_o_n" in txt and "s_e_l_e_c_t" in txt:
+                txt = unicode("'Insertion and selection can not be in template together'")
+                
+            if "i_n_s_e_r_t_i_o_n" in txt:
+                lines = 1
+                for aline in txt.splitlines():
+                    count =  aline.find("i_n_s_e_r_t_i_o_n")
+                    if count >= 0:
+                        txt = txt.replace("i_n_s_e_r_t_i_o_n",  "")
+                        break
+                    else:
+                        lines += 1
+            
+            setselect = False
+            if "s_e_l_e_c_t_s_t_a_r_t" in txt and "s_e_l_e_c_t_e_n_d" in txt:
+                setselect = True
+                linea = 1
+                for aline in txt.splitlines():
+                    posa =  aline.find("s_e_l_e_c_t_s_t_a_r_t")
+                    if posa >= 0:
+                        txt = txt.replace("s_e_l_e_c_t_s_t_a_r_t",  "")
+                        break
+                    else:
+                        linea += 1
+                lineb = 1
+                for aline in txt.splitlines():
+                    posb =  aline.find("s_e_l_e_c_t_e_n_d")
+                    if posb >= 0:
+                        txt = txt.replace("s_e_l_e_c_t_e_n_d",  "")
+                        break
+                    else:
+                        lineb += 1          
+
             editor.insert(txt)
-            editor.setCursorPosition(line + lines - 1, 
-                count and index + count or 0)
+            
+            if setselect:
+                editor.setSelection(line + linea - 1, posa, line + lineb - 1, posb)
+            else:
+                editor.setCursorPosition(line + lines - 1, count)
+                
             editor.endUndoAction()
             editor.setFocus()
 
