This repository has been archived by the owner on Aug 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
zen_editor.py
403 lines (335 loc) · 14.2 KB
/
zen_editor.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
'''
High-level editor interface that communicates with underlying editor (like
Espresso, Coda, etc.) or browser.
Basically, you should call <code>set_context(obj)</code> method to
set up undelying editor context before using any other method.
This interface is used by <i>zen_actions.py</i> for performing different
actions like <b>Expand abbreviation</b>
@example
import zen_editor
zen_editor.set_context(obj);
//now you are ready to use editor object
zen_editor.get_selection_range();
@author Sergey Chikuyonok (serge.che@gmail.com)
@link http://chikuyonok.ru
Changes for Gedit Plugin
@author Franck Marcia (franck.marcia@gmail.com)
@link http://github.com/fmarcia/zen-coding-gedit
'''
import zen_core, zen_actions, zen_file
import os, re, locale
from image_size import update_image_size
import zen_dialog
class ZenEditor():
def __init__(self):
self.last_wrap = ''
self.last_expand = ''
zen_core.set_caret_placeholder('')
def set_context(self, context):
"""
Setup underlying editor context. You should call this method
<code>before</code> using any Zen Coding action.
@param context: context object
"""
self.context = context # window
self.buffer = self.context.get_active_view().get_buffer()
self.view = context.get_active_view()
self.document = context.get_active_document()
default_locale = locale.getdefaultlocale()[0]
if default_locale:
lang = re.sub(r'_[^_]+$', '', default_locale)
if lang != default_locale:
zen_core.set_variable('lang', lang)
zen_core.set_variable('locale', default_locale.replace('_', '-'))
else:
zen_core.set_variable('lang', default_locale)
zen_core.set_variable('locale', default_locale)
self.encoding = self.document.get_encoding().get_charset()
zen_core.set_variable('charset', self.encoding)
if self.view.get_insert_spaces_instead_of_tabs():
zen_core.set_variable('indentation', " " * context.get_active_view().get_tab_width())
else:
zen_core.set_variable('indentation', "\t")
#zen_core.set_newline(???)
def get_selection_range(self):
"""
Returns character indexes of selected text
@return: list of start and end indexes
@example
start, end = zen_editor.get_selection_range();
print('%s, %s' % (start, end))
"""
offset_start = self.get_insert_offset()
offset_end = self.get_selection_bound_offset()
if offset_start < offset_end:
return offset_start, offset_end
return offset_end, offset_start
def create_selection(self, offset_start, offset_end=None):
"""
Creates selection from <code>start</code> to <code>end</code> character
indexes. If <code>end</code> is ommited, this method should place caret
and <code>start</code> index
@type start: int
@type end: int
@example
zen_editor.create_selection(10, 40)
# move caret to 15th character
zen_editor.create_selection(15)
"""
if offset_end is None:
iter_start = self.buffer.get_iter_at_offset(offset_start)
self.buffer.place_cursor(iter_start)
else:
iter_start = self.buffer.get_iter_at_offset(offset_start)
iter_end = self.buffer.get_iter_at_offset(offset_end)
self.buffer.select_range(iter_start, iter_end)
def get_current_line_range(self):
"""
Returns current line's start and end indexes
@return: list of start and end indexes
@example
start, end = zen_editor.get_current_line_range();
print('%s, %s' % (start, end))
"""
iter_current = self.get_insert_iter()
offset_start = self.buffer.get_iter_at_line(iter_current.get_line()).get_offset()
offset_end = offset_start + iter_current.get_chars_in_line() - 1
return offset_start, offset_end
def get_caret_pos(self):
""" Returns current caret position """
return self.get_insert_offset()
def set_caret_pos(self, pos):
"""
Set new caret position
@type pos: int
"""
self.buffer.place_cursor(self.buffer.get_iter_at_offset(pos))
def get_current_line(self):
"""
Returns content of current line
@return: str
"""
offset_start, offset_end = self.get_current_line_range()
iter_start = self.buffer.get_iter_at_offset(offset_start)
iter_end = self.buffer.get_iter_at_offset(offset_end)
return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
def replace_content(self, value, offset_start=None, offset_end=None):
"""
Replace editor's content or it's part (from <code>start</code> to
<code>end</code> index). If <code>value</code> contains
<code>caret_placeholder</code>, the editor will put caret into
this position. If you skip <code>start</code> and <code>end</code>
arguments, the whole target's content will be replaced with
<code>value</code>.
If you pass <code>start</code> argument only,
the <code>value</code> will be placed at <code>start</code> string
index of current content.
If you pass <code>start</code> and <code>end</code> arguments,
the corresponding substring of current target's content will be
replaced with <code>value</code>
@param value: Content you want to paste
@type value: str
@param start: Start index of editor's content
@type start: int
@param end: End index of editor's content
@type end: int
"""
if offset_start is None and offset_end is None:
iter_start = self.buffer.get_iter_at_offset(0)
iter_end = self.get_end_iter()
elif offset_end is None:
iter_start = self.buffer.get_iter_at_offset(offset_start)
iter_end = self.buffer.get_iter_at_offset(offset_start)
else:
iter_start = self.buffer.get_iter_at_offset(offset_start)
iter_end = self.buffer.get_iter_at_offset(offset_end)
self.buffer.delete(iter_start, iter_end)
self.set_caret_pos(offset_start)
self.insertion_start = self.get_insert_offset()
padding = zen_actions.get_current_line_padding(self)
self.buffer.insert_at_cursor(zen_core.pad_string(value, padding))
self.insertion_end = self.get_insert_offset()
def get_content(self):
"""
Returns editor's content
@return: str
"""
iter_start = self.buffer.get_iter_at_offset(0)
iter_end = self.get_end_iter()
return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
def get_syntax(self):
"""
Returns current editor's syntax mode
@return: str
"""
lang = self.context.get_active_document().get_language()
lang = lang and lang.get_name()
if lang == 'CSS': lang = 'css'
elif lang == 'XSLT': lang = 'xsl'
else: lang = 'html'
return lang
def get_profile_name(self):
"""
Returns current output profile name (@see zen_coding#setup_profile)
@return {String}
"""
return 'xhtml'
def prompt(self, title):
"""
Ask user to enter something
@param title: Dialog title
@type title: str
@return: Entered data
@since: 0.65
"""
done, result = zen_dialog.main(self, self.context, None, title)
if done:
return result
return ''
def get_selection(self):
"""
Returns current selection
@return: str
@since: 0.65
"""
offset_start, offset_end = self.get_selection_range()
iter_start = self.buffer.get_iter_at_offset(offset_start)
iter_end = self.buffer.get_iter_at_offset(offset_end)
return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
def get_file_path(self):
"""
Returns current editor's file path
@return: str
@since: 0.65
"""
return re.sub('^file://', '', self.document.get_uri())
#---------------------------------------------------------------------------------------
def get_insert_iter(self):
return self.buffer.get_iter_at_mark(self.buffer.get_insert())
def get_insert_offset(self):
return self.get_insert_iter().get_offset()
def get_selection_bound_iter(self):
return self.buffer.get_iter_at_mark(self.buffer.get_selection_bound())
def get_selection_bound_offset(self):
return self.get_selection_bound_iter().get_offset()
def get_end_iter(self):
return self.buffer.get_iter_at_offset(self.buffer.get_char_count())
def get_end_offset(self):
return self.get_end_iter().get_offset()
#---------------------------------------------------------------------------------------
def start_edit(self):
# bug when the cursor is at the very beginning
if self.insertion_start == 0:
self.insertion_start = 1
self.set_caret_pos(self.insertion_start)
if not self.next_edit_point() or (self.get_insert_offset() > self.insertion_end):
self.set_caret_pos(self.insertion_end)
def show_caret(self):
self.view.scroll_mark_onscreen(self.buffer.get_insert())
#---------------------------------------------------------------------------------------
def get_user_settings_error(self):
return zen_core.get_variable('user_settings_error')
#---------------------------------------------------------------------------------------
def expand_abbreviation(self, window):
self.set_context(window)
self.buffer.begin_user_action()
result = zen_actions.expand_abbreviation(self)
if result:
self.start_edit()
self.buffer.end_user_action()
#---------------------------------------------------------------------------------------
def save_selection(self):
self.save_offset_insert = self.get_insert_offset()
self.save_offset_selection_bound = self.get_selection_bound_offset()
def restore_selection(self):
iter_insert = self.buffer.get_iter_at_offset(self.save_offset_insert)
iter_selection_bound = self.buffer.get_iter_at_offset(self.save_offset_selection_bound)
self.buffer.select_range(iter_insert, iter_selection_bound)
#---------------------------------------------------------------------------------------
def do_expand_with_abbreviation(self, done, abbr):
self.buffer.begin_user_action()
if done:
self.buffer.undo()
self.restore_selection()
content = zen_core.expand_abbreviation(abbr, self.get_syntax(), self.get_profile_name())
if content:
self.replace_content(content, self.get_insert_offset())
self.buffer.end_user_action()
return not not content
def expand_with_abbreviation(self, window):
self.set_context(window)
self.save_selection()
done, self.last_expand = zen_dialog.main(self, window, self.do_expand_with_abbreviation, self.last_expand)
if done:
self.start_edit()
#---------------------------------------------------------------------------------------
def do_wrap_with_abbreviation(self, done, abbr):
self.buffer.begin_user_action()
if done:
self.buffer.undo()
self.restore_selection()
result = zen_actions.wrap_with_abbreviation(self, abbr)
self.buffer.end_user_action()
return result
def wrap_with_abbreviation(self, window):
self.set_context(window)
self.save_selection()
done, self.last_wrap = zen_dialog.main(self, window, self.do_wrap_with_abbreviation, self.last_wrap)
if done:
self.start_edit()
#---------------------------------------------------------------------------------------
def match_pair_inward(self, window):
self.set_context(window)
zen_actions.match_pair_inward(self)
def match_pair_outward(self, window):
self.set_context(window)
zen_actions.match_pair_outward(self)
def merge_lines(self, window):
self.set_context(window)
self.buffer.begin_user_action()
result = zen_actions.merge_lines(self)
self.buffer.end_user_action()
return result
#---------------------------------------------------------------------------------------
def prev_edit_point(self, window=None):
if window:
self.set_context(window)
result = zen_actions.prev_edit_point(self)
self.show_caret()
return result
def next_edit_point(self, window=None):
if window:
self.set_context(window)
result = zen_actions.next_edit_point(self)
self.show_caret()
return result
#---------------------------------------------------------------------------------------
def update_image_size(self, window):
self.set_context(window)
self.buffer.begin_user_action()
update_image_size(self)
self.buffer.end_user_action()
def encode_decode_base64(self, window):
self.set_context(window)
self.buffer.begin_user_action()
zen_actions.encode_decode_base64(self)
self.buffer.end_user_action()
#---------------------------------------------------------------------------------------
def remove_tag(self, window):
self.set_context(window)
self.buffer.begin_user_action()
result = zen_actions.remove_tag(self)
self.buffer.end_user_action()
return result
def split_join_tag(self, window):
self.set_context(window)
self.buffer.begin_user_action()
result = zen_actions.split_join_tag(self)
self.buffer.end_user_action()
return result
def toggle_comment(self, window):
self.set_context(window)
self.buffer.begin_user_action()
result = zen_actions.toggle_comment(self)
self.buffer.end_user_action()
return result