-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustominsert.py
More file actions
executable file
·167 lines (150 loc) · 5.71 KB
/
Copy pathCustominsert.py
File metadata and controls
executable file
·167 lines (150 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#Custominsert.py
#
#author yanni4night@gmail.com
#datetime 2013-11-15[14:22:57]
#update 2014-09-13[17:41:53]
#update 2016-06-28[11:46:05]
#version 1.4.0
import sublime, sublime_plugin
import re, datetime, os, socket, json
PLUGIN_NAME = 'Custominsert'
DIRNAME = os.path.dirname(os.path.realpath(__file__));
def get_settings():
'''
read sublime-settings file
'''
global SETTINGS
global PLUGIN_NAME
return sublime.load_settings(PLUGIN_NAME + '.sublime-settings')
def plugin_loaded():
'''
This function should be called when plugin loaded.
It generate context menus and commands automatically.
'''
global PLUGIN_NAME
SETTINGS = get_settings()
actions = SETTINGS.get('actions') or {}
action_keys = actions.keys() or []
if SETTINGS.get('auto_generate_context_menus'):
menus = []
for action in action_keys:
if action is not '':
menus.append({"id": "custominsert_" + action,"command": "custominsert","args": {"action": action},"caption": "InsertCustom " + action.capitalize()})
print(json.dumps(menus))
#update menu profile safely
try:
try:
menuHandle = open(DIRNAME + '/Context.sublime-menu', 'w')
menuHandle.write(json.dumps(menus, indent = 4))
finally:
menuHandle.close()
except:
pass
if SETTINGS.get('auto_generate_commands'):
commands = []
for action in action_keys:
if action is not '':
commands.append({"caption": "InsertCustom " + action.capitalize(), "command": "custominsert","args":{"action": action} })
#update commands profile safely
try:
try:
cmdHandle = open(DIRNAME + '/' + PLUGIN_NAME + '.sublime-commands','w')
cmdHandle.write(json.dumps(commands, indent = 4))
finally:
cmdHandle.close()
except:
pass
#load at first
plugin_loaded()
def get_local_ip():
'''Stupid way to get IP address'''
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('w3.org', 80))
return s.getsockname()[0]
except:
return '127.0.0.1'
def get_ext(filename):
ext = os.path.splitext(filename or '')[1]
#remove . at start position
p = re.compile('^\.')
return re.sub(p, '', ext)
class CustominsertCommand(sublime_plugin.TextCommand):
def get_settings_param(self, chains = [], default = ''):
'''get params in dict chains'''
obj = self.settings
for e in chains:
obj = obj.get(e)
if not obj:
return default
return obj
def get_predefined_param(self, match):
'''{%%}'''
key = match.group(1)
if key == 'filename':
return os.path.basename(self.view.file_name() or '')
elif key == 'filepath':
return self.view.file_name() or ''
elif key == 'dirname':
return os.path.dirname(self.view.file_name() or '')
elif key == 'platform':
return sublime.platform()
elif key == 'arch':
return sublime.arch()
elif key == 'encoding':
encoding = self.view.encoding()
return encoding if 'Undefined' != encoding else self.settings.get('default_encoding')
elif key == 'ip':
return get_local_ip()
elif key == 'user':
user = os.getlogin() if 'windows' != sublime.platform() else ''
if user:
return user
#windows?
user = os.popen('whoami').read()
p = re.compile('[\r\n]', re.M)
return re.sub(p, '', user)
elif key == 'ext':
return get_ext(self.view.file_name())
elif key == 'year':
t = datetime.datetime.today()
return t.strftime('%Y')
elif key == 'datetime':
t = datetime.datetime.today()
return t.strftime(self.get_action_param('datetime_format', '%Y-%m-%d %H:%M:%S'))
return match.group(1)
def get_defined_param(self ,match):
'''{{}} '''
key = match.group(1)
return self.get_settings_param(['actions', self.action, 'data', key], self.get_settings_param(['data', key]))
def get_action_param(self, key, default = ''):
'''get params of actions'''
return self.get_settings_param(['actions', self.action, key], self.get_settings_param([key]))
def run(self, edit, action = ''):
global SETTINGS
v = self.view
#save settings
self.settings = get_settings()
#save action
self.action = action;
content = self.get_action_param('content')
if isinstance(content, dict):
fext = get_ext(self.view.file_name())
default_content = content.get('default')
for ext in content:
if ext == fext:
default_content = content.get(ext) or default_content
break
content = default_content
#replace {%%}
p = re.compile('\{%\s*?([\w]+)\s*?%\}', re.LOCALE | re.MULTILINE)
content = re.sub(p, self.get_predefined_param, content)
#replace {{}}
p = re.compile('\{\{\s*?([\w]+)\s*?\}\}', re.LOCALE | re.MULTILINE)
content = re.sub(p, self.get_defined_param, content)
#insert position
if 'start' == self.get_action_param('position'):
v.insert(edit, 0, content)
else:
for sel in v.sel():
v.insert(edit,sel.begin(), content)