Fix for text plug-in scripts on 64-bit platforms.
[blender-staging.git] / release / scripts / textplugin_templates.py
1 #!BPY
2 """
3 Name: 'Template Completion | Tab'
4 Blender: 246
5 Group: 'TextPlugin'
6 Shortcut: 'Tab'
7 Tooltip: 'Completes templates based on the text preceding the cursor'
8 """
9
10 # Only run if we have the required modules
11 try:
12         import bpy
13         from BPyTextPlugin import *
14         from Blender import Text
15 except ImportError:
16         OK = False
17 else:
18         OK = True
19
20 templates = {
21         'ie':
22                 'if ${1:cond}:\n'
23                 '\t${2}\n'
24                 'else:\n'
25                 '\t${3}\n',
26         'iei':
27                 'if ${1:cond}:\n'
28                 '\t${2}\n'
29                 'elif:\n'
30                 '\t${3}\n'
31                 'else:\n'
32                 '\t${4}\n',
33         'def':
34                 'def ${1:name}(${2:params}):\n'
35                 '\t"""(${2}) - ${3:comment}"""\n'
36                 '\t${4}',
37         'cls':
38                 'class ${1:name}(${2:parent}):\n'
39                 '\t"""${3:docs}"""\n'
40                 '\t\n'
41                 '\tdef __init__(self, ${4:params}):\n'
42                 '\t\t"""Creates a new ${1}"""\n'
43                 '\t\t${5}',
44         'class':
45                 'class ${1:name}(${2:parent}):\n'
46                 '\t"""${3:docs}"""\n'
47                 '\t\n'
48                 '\tdef __init__(self, ${4:params}):\n'
49                 '\t\t"""Creates a new ${1}"""\n'
50                 '\t\t${5}'
51 }
52
53 def main():
54         txt = bpy.data.texts.active
55         if not txt:
56                 return
57         
58         row, c = txt.getCursorPos()
59         line = txt.asLines(row, row+1)[0]
60         indent=0
61         while indent<c and (line[indent]==' ' or line[indent]=='\t'):
62                 indent += 1
63         
64         # Check we are in a normal context
65         if get_context(txt) != CTX_NORMAL:
66                 return
67         
68         targets = get_targets(line, c-1);
69         if len(targets) != 1: return
70         
71         color = (0, 192, 32)
72         
73         for trigger, template in templates.items():
74                 if trigger != targets[0]: continue
75                 inserts = {}
76                 txt.delete(-len(trigger)-1)
77                 y, x = txt.getCursorPos()
78                 first = None
79                 
80                 # Insert template text and parse for insertion points
81                 count = len(template); i = 0
82                 while i < count:
83                         if i<count-1 and template[i]=='$' and template[i+1]=='{':
84                                 i += 2
85                                 e = template.find('}', i)
86                                 item = template[i:e].split(':')
87                                 if len(item)<2: item.append('')
88                                 if not inserts.has_key(item[0]):
89                                         inserts[item[0]] = (item[1], [(x, y)])
90                                 else:
91                                         inserts[item[0]][1].append((x, y))
92                                         item[1] = inserts[item[0]][0]
93                                 if not first: first = (item[1], x, y)
94                                 txt.insert(item[1])
95                                 x += len(item[1])
96                                 i = e
97                         else:
98                                 txt.insert(template[i])
99                                 if template[i] == '\n':
100                                         txt.insert(line[:indent])
101                                         y += 1
102                                         x = indent
103                                 else:
104                                         x += 1
105                         i += 1
106                 
107                 # Insert markers at insertion points
108                 for id, (text, points) in inserts.items():
109                         for x, y in points:
110                                 txt.setCursorPos(y, x)
111                                 txt.setSelectPos(y, x+len(text))
112                                 txt.markSelection((hash(text)+int(id)) & 0xFFFF, color,
113                                                 Text.TMARK_TEMP | Text.TMARK_EDITALL)
114                 if first:
115                         text, x, y = first
116                         txt.setCursorPos(y, x)
117                         txt.setSelectPos(y, x+len(text))
118                 break
119                 
120
121 # Check we are running as a script and not imported as a module
122 if __name__ == "__main__" and OK:
123         main()