Templates/TemplateViewer.py

changeset 108
03d3adf59bde
parent 97
c4086afea02b
child 432
497e188ee86e
child 505
2219cb4f46c5
equal deleted inserted replaced
107:918a6be6f051 108:03d3adf59bde
592 keyfmt % 'year': str(today.year)} 592 keyfmt % 'year': str(today.year)}
593 593
594 if project.name: 594 if project.name:
595 varValues[keyfmt % 'project_name'] = project.name 595 varValues[keyfmt % 'project_name'] = project.name
596 596
597 if project.ppath:
598 varValues[keyfmt % 'project_path'] = project.ppath
599
597 path_name = editor.getFileName() 600 path_name = editor.getFileName()
598 if path_name: 601 if path_name:
599 dir_name, file_name = os.path.split(path_name) 602 dir_name, file_name = os.path.split(path_name)
600 base_name, ext = os.path.splitext(file_name) 603 base_name, ext = os.path.splitext(file_name)
601 if ext: 604 if ext:
605 keyfmt % 'dir_name': dir_name, 608 keyfmt % 'dir_name': dir_name,
606 keyfmt % 'file_name': file_name, 609 keyfmt % 'file_name': file_name,
607 keyfmt % 'base_name': base_name, 610 keyfmt % 'base_name': base_name,
608 keyfmt % 'ext': ext 611 keyfmt % 'ext': ext
609 }) 612 })
613
614 varValues[keyfmt % 'clipboard:ml'] = QApplication.clipboard().text()
615 varValues[keyfmt % 'clipboard'] = QApplication.clipboard().text()
616
617 if editor.hasSelectedText():
618 varValues[keyfmt % 'cur_select:ml'] = editor.selectedText()
619 varValues[keyfmt % 'cur_select'] = editor.selectedText()
620 else:
621 varValues[keyfmt % 'cur_select:ml'] = os.linesep
622 varValues[keyfmt % 'cur_select'] = ""
623
624 varValues[keyfmt % 'insertion'] = "i_n_s_e_r_t_i_o_n"
625
626 varValues[keyfmt % 'select_start'] = "s_e_l_e_c_t_s_t_a_r_t"
627 varValues[keyfmt % 'select_end'] = "s_e_l_e_c_t_e_n_d"
628
610 return varValues 629 return varValues
611 630
612 def applyTemplate(self, itm): 631 def applyTemplate(self, itm):
613 """ 632 """
614 Public method to apply the template. 633 Public method to apply the template.
653 indent = line.replace(line.lstrip(), "") 672 indent = line.replace(line.lstrip(), "")
654 txt, lines, count = itm.getExpandedText(varValues, indent) 673 txt, lines, count = itm.getExpandedText(varValues, indent)
655 # It should be done in this way to allow undo 674 # It should be done in this way to allow undo
656 editor.beginUndoAction() 675 editor.beginUndoAction()
657 if editor.hasSelectedText(): 676 if editor.hasSelectedText():
677 line = editor.getSelection()[0]
658 editor.removeSelectedText() 678 editor.removeSelectedText()
659 line, index = editor.getCursorPosition() 679 else:
680 line = editor.getCursorPosition()[0]
681
682 if len(indent) > 0:
683 count += len(indent)
684
685 if "i_n_s_e_r_t_i_o_n" in txt and "s_e_l_e_c_t" in txt:
686 txt = "'Insertion and selection can not be in template together'"
687
688 if "i_n_s_e_r_t_i_o_n" in txt:
689 lines = 1
690 for aline in txt.splitlines():
691 count = aline.find("i_n_s_e_r_t_i_o_n")
692 if count >= 0:
693 txt = txt.replace("i_n_s_e_r_t_i_o_n", "")
694 if len(indent) > 0:
695 count += len(indent)
696 break
697 else:
698 lines += 1
699
700 setselect = False
701 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:
702 setselect = True
703 linea = 1
704 for aline in txt.splitlines():
705 posa = aline.find("s_e_l_e_c_t_s_t_a_r_t")
706 if posa >= 0:
707 txt = txt.replace("s_e_l_e_c_t_s_t_a_r_t", "")
708 break
709 else:
710 linea += 1
711 lineb = 1
712 for aline in txt.splitlines():
713 posb = aline.find("s_e_l_e_c_t_e_n_d")
714 if posb >= 0:
715 txt = txt.replace("s_e_l_e_c_t_e_n_d", "")
716 break
717 else:
718 lineb += 1
719
660 editor.insert(txt) 720 editor.insert(txt)
661 editor.setCursorPosition(line + lines - 1, 721
662 count and index + count or 0) 722 if setselect:
723 editor.setSelection(line + linea - 1, posa, line + lineb - 1, posb)
724 else:
725 editor.setCursorPosition(line + lines - 1, count)
726
663 editor.endUndoAction() 727 editor.endUndoAction()
664 editor.setFocus() 728 editor.setFocus()
665 729
666 def applyNamedTemplate(self, templateName): 730 def applyNamedTemplate(self, templateName):
667 """ 731 """

eric ide

mercurial