-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXConfigUI.py
440 lines (344 loc) · 14.3 KB
/
XConfigUI.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
'''A Notepad++ PythonScript User Interface to access settings from xconfig.py.
Properties set from the UI will be applied in runtime which are not saved.
A view widget shows settings applied which can be manually copied into
xconfig.properties.
The UI is optional as xconfig.py and xconfig.properties can apply settings
at PythonScript startup. Set PythonScript Initialisation from "Lazy" to
"AtStartup" to run xconfig.py at Notepad++ startup. The UI code will error
if xconfig.py has not been run at startup.
'''
import os
import sys
if sys.version_info.major < 3:
import Tkinter as tkinter
import ttk
tkinter.ttk = ttk
del ttk
editor.getChangeHistory = xconfig.editor.getChangeHistory
editor.setChangeHistory = xconfig.editor.setChangeHistory
else:
import tkinter
import tkinter.ttk
# Normal operation should be quiet.
verbose = 0
class XConfigUI():
def __init__(self):
'''Create the window.'''
self.tk =o= tkinter.Tk()
o.title('XConfigUI')
o.geometry('450x400')
o.resizable(True, False)
o.minsize(450, 400)
o.maxsize(950, 400)
self.create_widgets()
o.mainloop()
def create_widgets(self):
'''Create all widgets.'''
CONTROL = 4
def readonly_text(event):
# Disable keys except for Ctrl+A and Ctrl+C.
if not (event.state == CONTROL and event.keysym in ('a', 'c')):
return 'break'
def validate_entry(value):
# Get selected listbox item.
index = self.listbox.curselection()
if not index:
return False
key = self.listbox.get(index[0])
# Allow empty character.
if not value:
return True
# Allow hex number.
if '.colour' in key:
if len(value) > 6:
return False
for item in value:
if item.lower() not in '0123456789abcdef':
return False
return True
# Restrict to integers unless defined other.
if key.startswith(('caret.', 'change.history', 'edge.',
'fold.', 'lexer.', 'selection.', 'wrap.')):
# Allow any character for these properties.
if key in ('lexer.as.comment.character',
'lexer.asm.comment.delimiter'):
return True
# Allow integers.
if value.isdigit():
return True
return False
elif key in ('asp.default.language',
'autocomplete.visible.item.count',
'backspace.unindents',
'bookmark.marker',
'fold',
'horizontal.scroll.width',
'horizontal.scroll.width.tracking',
'idle.styling',
'html.tags.case.sensitive',
'nsis.ignorecase',
'nsis.uservars',
'ps.level',
'sql.backslash.escapes',
'styling.within.preprocessor',
'tab.draw.mode',
'tab.timmy.whinge.level',
'technology',
'virtual.space',
'whitespace.size'):
# Allow integers.
if value.isdigit():
return True
return False
# Allow any character for unspecified properties.
if verbose:
print(key, 'allows any character')
return True
register_entry = (self.tk.register(validate_entry), '%P')
self.file_settings = self.read_settings()
self.file_keys = sorted(self.file_settings.keys())
# Entry for listbox filtering.
self.entry =o= tkinter.Entry()
o.place(x=10, y=10, w=430)
o.bind('<Key>', self.update_listbox)
# Listbox of property items.
self.listbox =o= tkinter.Listbox()
o.place(x=10, y=35, w=430, h=300)
o.insert('end', *self.file_keys)
o.bind('<<ListboxSelect>>', self.on_listbox)
o.activate(0)
# Scrollbar for the listbox.
scrollbar =o= tkinter.Scrollbar(o)
o.pack(side='right', fill='both')
o.config(command=self.listbox.yview)
self.listbox.config(yscrollcommand=o.set)
# Label to display section of an existing value.
self.label_section =o= tkinter.Label(borderwidth=1, relief='sunken',
anchor='w')
o.place(x=20, y=359, w=80)
# Label to display origin of an existing value.
self.label_origin =o= tkinter.Label(borderwidth=1, relief='sunken',
anchor='w')
o.place(x=120, y=359, w=80)
# Entry for property value to be set.
self.entry_value =o= tkinter.Entry(validatecommand=register_entry,
validate='key')
o.place(x=220, y=359, w=80)
# Button to set property value.
self.button_set =o= tkinter.Button(command=self.on_button_set,
text='Set')
o.place(x=330, y=355, w=80)
# Text for description of selected property item.
self.text_desc =o= tkinter.Text()
o.place(x=455, y=10, w=480, h=180)
o.bind('<Key>', readonly_text)
o.insert('1.0', 'Select an item from the {} items in the list.'
.format(len(self.file_keys)))
# Text to view set properties.
self.view =o= tkinter.Text()
o.place(x=455, y=200, w=480, h=180)
o.bind('<Key>', readonly_text)
# Size grip to assist with window resize.
sizegrip =o= tkinter.ttk.Sizegrip(cursor='sb_h_double_arrow')
o.place(relx=1, rely=1, anchor='se')
# Update view and focus the first Entry.
self.update_view()
self.entry.focus_set()
def on_button_set(self):
'''Set the selected property on pressed Set Button.'''
# Get selected listbox item.
index = self.listbox.curselection()
if not index:
return
key = self.listbox.get(index[0])
# Get entry value.
value = self.entry_value.get()
if value:
xconfig.settings[key] = value
xconfig.reload()
else:
if key in xconfig.settings:
del xconfig.settings[key]
xconfig.reload()
# Update label and entry.
self.update_label(key)
self.update_view()
# Activate buffer to trigger event.
notepad.activateBufferID(notepad.getCurrentBufferID())
def on_listbox(self, event):
'''Update other widget values on Listbox selection.'''
# Get selected listbox item.
index = event.widget.curselection()
if not index:
return
key = event.widget.get(index[0])
value = ''
self.text_desc.delete('1.0', 'end')
self.entry_value.delete('0', 'end')
# Section from file.
section = self.file_settings[key].get('section')
self.label_section.config(text=section)
# Description from file.
desc = self.file_settings[key].get('desc')
self.text_desc.insert('1.0', desc)
# Update label, entry and view.
value = self.update_label(key)
if value:
self.entry_value.insert('0', value)
self.update_view()
def read_settings(self):
'''Read xconfig.properties into a dictionary with descriptions.
Return: Dictionary
'''
config_file = os.path.join(notepad.getPluginConfigDir(),
'xconfig.properties')
desc = ''
dic = {}
section = ''
with open(config_file) as r:
for line in r:
line = line.strip()
if not line or line.startswith('##'):
continue
if line.startswith('['):
section = line[1:-1]
# Get property description.
if line.startswith('#'):
desc = line[1:].strip()
continue
# Get properties keys and values.
parts = line.split('=', 1)
if len(parts) == 2:
key = parts[0].strip()
value = parts[1].strip()
dic[key] = {'desc': desc,
'section': section,
'value': value}
return dic
def update_label(self, key):
'''Update Label with value of "memory", "defined" or "".'''
# Set as memory.
value = xconfig.settings.get(key)
if value:
self.label_origin.config(text='memory')
return value
# Set as defined.
if key == 'autocomplete.visible.item.count':
value = str(editor.autoCGetMaxHeight())
elif key == 'backspace.unindents':
value = editor.getBackSpaceUnIndents()
value = '1' if value else '0'
elif key == 'bookmark.colour':
value = ''
elif key == 'bookmark.marker':
marker = xconfig.MARK_BOOKMARK
value = str(editor.markerSymbolDefined(marker))
elif key == 'caret.additional.blinks':
value = editor.getAdditionalCaretsBlink()
value = '1' if value else '0'
elif key == 'caret.line.visible.always':
value = editor.getCaretLineVisibleAlways()
value = '1' if value else '0'
elif key == 'caret.width':
value = str(editor.getCaretWidth())
elif key == 'change.history':
value = str(editor.getChangeHistory())
elif key.startswith('change.history.colour.'):
value = ''
elif key == 'change.history.indicator':
value = ''
elif key == 'change.history.marker.modified':
marker = xconfig.SC_MARKNUM_HISTORY_MODIFIED
value = str(editor.markerSymbolDefined(marker))
elif key == 'change.history.marker.reverted.modified':
marker = xconfig.SC_MARKNUM_HISTORY_REVERTED_TO_MODIFIED
value = str(editor.markerSymbolDefined(marker))
elif key == 'change.history.marker.reverted.origin':
marker = xconfig.SC_MARKNUM_HISTORY_REVERTED_TO_ORIGIN
value = str(editor.markerSymbolDefined(marker))
elif key == 'change.history.marker.saved':
marker = xconfig.SC_MARKNUM_HISTORY_SAVED
value = str(editor.markerSymbolDefined(marker))
elif key == 'edge.column':
value = str(editor.getEdgeColumn())
elif key == 'edge.mode':
value = str(editor.getEdgeMode())
elif key == 'fold.flags':
value = ''
elif key == 'horizontal.scroll.width':
value = str(editor.getScrollWidth())
elif key == 'horizontal.scroll.width.tracking':
value = editor.getScrollWidthTracking()
value = '1' if value else '0'
elif key == 'idle.styling':
value = str(editor.getIdleStyling())
elif key == 'selection.additional.typing':
value = editor.getAdditionalSelectionTyping()
value = '1' if value else '0'
elif key == 'selection.multipaste':
value = str(editor.getMultiPaste())
elif key == 'selection.multiple':
value = editor.getMultipleSelection()
value = '1' if value else '0'
elif key == 'selection.rectangular.switch.mouse':
value = editor.getMouseSelectionRectangularSwitch()
value = '1' if value else '0'
elif key == 'tab.draw.mode':
value = str(editor.getTabDrawMode())
elif key == 'technology':
value = str(editor.getTechnology())
elif key == 'virtual.space':
value = str(editor.getVirtualSpaceOptions())
elif key == 'whitespace.size':
value = str(editor.getWhitespaceSize())
elif key == 'wrap.indent.mode':
value = str(editor.getWrapIndentMode())
elif key == 'wrap.style':
value = str(editor.getWrapMode())
elif key == 'wrap.visual.flags':
value = str(editor.getWrapVisualFlags())
elif key == 'wrap.visual.flags.location':
value = str(editor.getWrapVisualFlagsLocation())
elif key == 'wrap.visual.startindent':
value = str(editor.getWrapStartIndent())
else:
if verbose:
if self.file_settings[key]['section'] != 'lexer':
print(key, 'is not a lexer setting')
value = str(editor.getProperty(key))
if value:
self.label_origin.config(text='defined')
return value
# Set as none.
self.label_origin.config(text='')
def update_listbox(self, event):
'''Update Listbox to match the substring of the Entry text.'''
text = self.entry.get()
# Update text if ANSI or backspace key.
if event.keycode >= 32 and event.keycode <= 126:
text += event.char
elif event.keycode == 8:
text = text[:-1]
# Insert new items into Listbox.
self.listbox.delete('0', 'end')
if text:
# By section name else by property name.
if text.startswith('['):
text = text[1:].lstrip()
for key in self.file_keys:
if text.lower() in self.file_settings[key].get('section'):
self.listbox.insert('end', key)
else:
for key in self.file_keys:
if text.lower() in key.lower():
self.listbox.insert('end', key)
else:
# All property names.
self.listbox.insert('end', *self.file_keys)
def update_view(self):
'''Update view from the xconfig.settings dictionary.'''
self.view.delete('1.0', 'end')
for key, value in xconfig.settings.items():
self.view.insert('end', '{} = {}\n'.format(key, value))
if __name__ == '__main__':
XConfigUI()