-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsourceview.py
More file actions
executable file
·582 lines (499 loc) · 19.9 KB
/
sourceview.py
File metadata and controls
executable file
·582 lines (499 loc) · 19.9 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# [SNIPPET_NAME: GtkSourceView Example]
# [SNIPPET_CATEGORIES: PyGTK, PyGTKSourceView]
# [SNIPPET_DESCRIPTION: Demonstrates using Actions with a gtk.Action and gtk.AccelGroup]
#
# This script shows the use of pygtksourceview module, the python wrapper
# of gtksourceview C library.
# It has been directly translated from test-widget.c
# and modified to use gtksourceview2
#
# Copyright (C) 2004 - Iñigo Serna <inigoserna@telefonica.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Modify by stevek - add
# save feature - popup to open new file if not file is supplied - tab is 4 and use space at startup
# default lang is python if not detected from file extension.
import os, os.path
import sys
import pygtk
pygtk.require ('2.0')
import gtk
if gtk.pygtk_version < (2,10,0):
print "PyGtk 2.10 or later required for this example"
raise SystemExit
import gtksourceview2
import pango
######################################################################
##### global vars
windows = [] # this list contains all view windows
MARK_CATEGORY_1 = 'one'
MARK_CATEGORY_2 = 'two'
DATADIR = '/usr/share'
LANG_TAB = {
'd.py': 100,
'd.php': 101,
'd.pl': 102,
'd.js': 103,
'd.c':104,
'd.cpp':105,
'd.rb': 106,
'd.lua':107,
'd.java':108,
'd.sh':109,
'd.tcl':110,
'd.sql': 111,
'd.xml': 112,
'd.html': 113,
'd.css': 114,
'd.json': 115,
'd.sgml': 116,
'd.xslt': 117,
'd.dtd': 118,
'd.jsp': 119
}
######################################################################
##### error dialog
def error_dialog(parent, msg):
dialog = gtk.MessageDialog(parent,
gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_ERROR,
gtk.BUTTONS_OK,
msg)
dialog.run()
dialog.destroy()
######################################################################
##### remove all markers
def remove_all_marks(buffer):
begin, end = buffer.get_bounds()
buffer.remove_source_marks(begin, end)
######################################################################
##### load file
def load_file(buffer, path):
buffer.begin_not_undoable_action()
if path != 'empty':
try: txt = open(path).read()
except:
return False
else: txt = ''
buffer.set_text(txt)
buffer.set_data('filename', path)
buffer.end_not_undoable_action()
buffer.set_modified(False)
buffer.place_cursor(buffer.get_start_iter())
return True
######################################################################
##### buffer creation
def open_file(buffer, filename):
# get the new language for the file mimetype
manager = buffer.get_data('languages-manager')
if filename != 'empty':
if not os.path.isfile(filename):
open(filename, 'w').write('')
if os.path.isabs(filename):
path = filename
else:
path = os.path.abspath(filename)
else: path = filename
language = manager.guess_language(filename)
if language:
buffer.set_highlight_syntax(True)
buffer.set_language(language)
else:
print 'No language found for file "%s. set to python"' % filename
buffer.set_highlight_syntax(True)
language = manager.guess_language(filename + ".py")
buffer.set_language(language)
remove_all_marks(buffer)
load_file(buffer, path) # TODO: check return
return True
######################################################################
##### Printing callbacks
def begin_print_cb(operation, context, compositor):
while not compositor.paginate(context):
pass
n_pages = compositor.get_n_pages()
operation.set_n_pages(n_pages)
def draw_page_cb(operation, context, page_nr, compositor):
compositor.draw_page(context, page_nr)
######################################################################
##### Action callbacks
def numbers_toggled_cb(action, sourceview):
sourceview.set_show_line_numbers(action.get_active())
def marks_toggled_cb(action, sourceview):
sourceview.set_show_line_marks(action.get_active())
def margin_toggled_cb(action, sourceview):
sourceview.set_show_right_margin(action.get_active())
def auto_indent_toggled_cb(action, sourceview):
sourceview.set_auto_indent(action.get_active())
def insert_spaces_toggled_cb(action, sourceview):
sourceview.set_insert_spaces_instead_of_tabs(action.get_active())
def tabs_toggled_cb(action, action2, sourceview):
sourceview.set_tab_width(action.get_current_value())
def lang_toggled_cb(action, action2, sourceview):
buffer = sourceview.get_buffer()
manager = buffer.get_data('languages-manager')
INVERSE_LANG_TAB = dict([[v,k] for k,v in LANG_TAB.items()])
filename = INVERSE_LANG_TAB[action.get_current_value()]
language = manager.guess_language(filename)
if language:
buffer.set_highlight_syntax(True)
buffer.set_language(language)
else: print("Can not find language {0}".format(filename))
def new_view_cb(action, sourceview):
window = create_view_window(sourceview.get_buffer(), sourceview)
window.set_default_size(400, 400)
window.show()
def print_cb(action, sourceview):
window = sourceview.get_toplevel()
buffer = sourceview.get_buffer()
compositor = gtksourceview2.print_compositor_new_from_view(sourceview)
compositor.set_wrap_mode(gtk.WRAP_CHAR)
compositor.set_highlight_syntax(True)
compositor.set_print_line_numbers(5)
compositor.set_header_format(False, 'Printed on %A', None, '%F')
filename = buffer.get_data('filename')
compositor.set_footer_format(True, '%T', filename, 'Page %N/%Q')
compositor.set_print_header(True)
compositor.set_print_footer(True)
print_op = gtk.PrintOperation()
print_op.connect("begin-print", begin_print_cb, compositor)
print_op.connect("draw-page", draw_page_cb, compositor)
res = print_op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, window)
if res == gtk.PRINT_OPERATION_RESULT_ERROR:
error_dialog(window, "Error printing file:\n\n" + filename)
elif res == gtk.PRINT_OPERATION_RESULT_APPLY:
print 'file printed: "%s"' % filename
######################################################################
##### Buffer action callbacks
def open_file_cb(action, buffer):
chooser = gtk.FileChooserDialog('Open file...', None,
gtk.FILE_CHOOSER_ACTION_SAVE,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
response = chooser.run()
if response == gtk.RESPONSE_OK:
filename = chooser.get_filename()
print("debug: ", filename)
if filename:
open_file(buffer, filename)
chooser.destroy()
def save_file_cb(action, buffer):
""" save buffer to the current file """
filename = buffer.get_data('filename')
mf = open(filename, "w")
mf.write(buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter()) )
mf.close()
def update_cursor_position(buffer, view):
tabwidth = view.get_tab_width()
pos_label = view.get_data('pos_label')
iter = buffer.get_iter_at_mark(buffer.get_insert())
nchars = iter.get_offset()
row = iter.get_line() + 1
start = iter.copy()
start.set_line_offset(0)
col = 0
while start.compare(iter) < 0:
if start.get_char() == '\t':
col += tabwidth - col % tabwidth
else:
col += 1
start.forward_char()
pos_label.set_text('char: %d, line: %d, column: %d' % (nchars, row, col+1))
def move_cursor_cb (buffer, cursoriter, mark, view):
update_cursor_position(buffer, view)
def window_deleted_cb(widget, ev, view):
if windows[0] == widget:
gtk.main_quit()
else:
# remove window from list
windows.remove(widget)
# we return False since we want the window destroyed
return False
return True
def button_press_cb(view, ev):
buffer = view.get_buffer()
if not view.get_show_line_marks():
return False
# check that the click was on the left gutter
if ev.window == view.get_window(gtk.TEXT_WINDOW_LEFT):
if ev.button == 1:
mark_category = MARK_CATEGORY_1
else:
mark_category = MARK_CATEGORY_2
x_buf, y_buf = view.window_to_buffer_coords(gtk.TEXT_WINDOW_LEFT,
int(ev.x), int(ev.y))
# get line bounds
line_start = view.get_line_at_y(y_buf)[0]
# get the markers already in the line
mark_list = buffer.get_source_marks_at_line(line_start.get_line(), mark_category)
# search for the marker corresponding to the button pressed
for m in mark_list:
if m.get_category() == mark_category:
# a marker was found, so delete it
buffer.delete_mark(m)
break
else:
# no marker found, create one
buffer.create_source_mark(None, mark_category, line_start)
return False
######################################################################
##### Actions & UI definition
buffer_actions = [
('Open', gtk.STOCK_OPEN, '_Open', '<control>O', 'Open a file', open_file_cb),
('Save', gtk.STOCK_SAVE, '_Save', '<control>s', 'Save', save_file_cb),
('Quit', gtk.STOCK_QUIT, '_Quit', '<control>Q', 'Exit the application', gtk.main_quit)
]
view_actions = [
('FileMenu', None, '_File'),
('ViewMenu', None, '_View'),
('Print', gtk.STOCK_PRINT, '_Print', '<control>P', 'Print the file', print_cb),
('NewView', gtk.STOCK_NEW, '_New View', None, 'Create a new view of the file', new_view_cb),
('TabsWidth', None, '_Tabs Width'),
('ChangeLang', None, '_Language')
]
toggle_actions = [
('ShowNumbers', None, 'Show _Line Numbers', None, 'Toggle visibility of line numbers in the left margin', numbers_toggled_cb, False),
('ShowMarkers', None, 'Show _Markers', None, 'Toggle visibility of markers in the left margin', marks_toggled_cb, False),
('ShowMargin', None, 'Show M_argin', None, 'Toggle visibility of right margin indicator', margin_toggled_cb, False),
('AutoIndent', None, 'Enable _Auto Indent', None, 'Toggle automatic auto indentation of text', auto_indent_toggled_cb, False),
('InsertSpaces', None, 'Insert _Spaces Instead of Tabs', None, 'Whether to insert space characters when inserting tabulations', insert_spaces_toggled_cb, False)
]
radio_actions = [
('TabsWidth4', None, '4', None, 'Set tabulation width to 4 spaces', 4),
('TabsWidth6', None, '6', None, 'Set tabulation width to 6 spaces', 6),
('TabsWidth8', None, '8', None, 'Set tabulation width to 8 spaces', 8),
('TabsWidth10', None, '10', None, 'Set tabulation width to 10 spaces', 10),
('TabsWidth12', None, '12', None, 'Set tabulation width to 12 spaces', 12)
]
radio_lang_action = [
('Python', None, 'Python', None, 'Set lang to python', LANG_TAB['d.py']),
('PHP', None, 'PHP', None, 'Set lang to PHP', LANG_TAB['d.php']),
('Perl', None, 'Perl', None, 'Set lang to Perl', LANG_TAB['d.pl']),
('JavaScript', None, 'JavaScript', None, 'Set lang to JavaScript', LANG_TAB['d.js']),
('C', None, 'C', None, 'Set lang to C', LANG_TAB['d.c']),
('C++', None, 'C++', None, 'Set lang to C++', LANG_TAB['d.cpp']),
('Ruby', None, 'Ruby', None, 'Set lang to Ruby', LANG_TAB['d.rb']),
('Lua', None, 'Lua', None, 'Set lang to lua', LANG_TAB['d.lua']),
('Java', None, 'Java', None, 'Set lang to Java', LANG_TAB['d.java']),
('Bash', None, 'bash', None, 'Set lang to bash', LANG_TAB['d.sh']),
('SQL', None, 'sql', None, 'Set lang to sql', LANG_TAB['d.sql']),
('Tcl', None, 'tcl', None, 'Set lang to tcl', LANG_TAB['d.tcl']),
('XML', None, 'xml', None, 'Set lang to xml', LANG_TAB['d.xml']),
('HTML', None, 'html', None, 'Set lang to html', LANG_TAB['d.html']),
('CSS', None, 'css', None, 'Set lang to css', LANG_TAB['d.css']),
#('json', None, 'json', None, 'Set lang to json', LANG_TAB['d.json']),
#('SGML', None, 'sgml', None, 'Set lang to sgml', LANG_TAB['d.sgml']),
('XSLT', None, 'xslt', None, 'Set lang to xslt', LANG_TAB['d.xslt']),
('DTD', None, 'dtd', None, 'Set lang to dtd', LANG_TAB['d.dtd']),
#('JSP', None, 'jsp', None, 'Set lang to jsp', LANG_TAB['d.jsp'])
]
view_ui_description = """
<ui>
<menubar name='MainMenu'>
<menu action='FileMenu'>
<menuitem action='NewView'/>
<placeholder name="FileMenuAdditions"/>
<separator/>
<menuitem action='Print'/>
</menu>
<menu action='ChangeLang'>
<menuitem action='Python'/>
<menuitem action='PHP'/>
<menuitem action='Perl'/>
<menuitem action='Lua'/>
<menuitem action='Bash'/>
<menuitem action='Ruby'/>
<menuitem action='JavaScript'/>
<menuitem action='C'/>
<menuitem action='C++'/>
<menuitem action='Java'/>
<menuitem action='Tcl'/>
<menuitem action='SQL'/>
<menuitem action='XML'/>
<menuitem action='HTML'/>
<menuitem action='CSS'/>
<menuitem action='XSLT'/>
<menuitem action='DTD'/>
</menu>
<menu action='ViewMenu'>
<separator/>
<menuitem action='ShowNumbers'/>
<menuitem action='ShowMarkers'/>
<menuitem action='ShowMargin'/>
<separator/>
<menuitem action='AutoIndent'/>
<menuitem action='InsertSpaces'/>
<separator/>
<menu action='TabsWidth'>
<menuitem action='TabsWidth4'/>
<menuitem action='TabsWidth6'/>
<menuitem action='TabsWidth8'/>
<menuitem action='TabsWidth10'/>
<menuitem action='TabsWidth12'/>
</menu>
</menu>
</menubar>
</ui>
"""
buffer_ui_description = """
<ui>
<menubar name='MainMenu'>
<menu action='FileMenu'>
<placeholder name="FileMenuAdditions">
<menuitem action='Open'/>
<menuitem action='Save'/>
</placeholder>
<separator/>
<menuitem action='Quit'/>
</menu>
<menu action='ViewMenu'>
</menu>
</menubar>
</ui>
"""
######################################################################
##### create view window
def create_view_window(buffer, sourceview=None):
# window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_border_width(0)
window.set_title('PyGtkSourceView Demo')
windows.append(window) # this list contains all view windows
# view
view = gtksourceview2.View(buffer)
# stevek set initial state
view.set_tab_width(4)
view.set_insert_spaces_instead_of_tabs(True)
view.set_wrap_mode(gtk.WRAP_WORD)
# end stevek
buffer.connect('mark_set', move_cursor_cb, view)
buffer.connect('changed', update_cursor_position, view)
view.connect('button-press-event', button_press_cb)
window.connect('delete-event', window_deleted_cb, view)
# action group and UI manager
action_group = gtk.ActionGroup('ViewActions')
action_group.add_actions(view_actions, view)
action_group.add_toggle_actions(toggle_actions, view)
action_group.add_radio_actions(radio_actions, -1, tabs_toggled_cb, view)
action_group.add_radio_actions(radio_lang_action, -1, lang_toggled_cb, view)
ui_manager = gtk.UIManager()
ui_manager.insert_action_group(action_group, 0)
# save a reference to the ui manager in the window for later use
window.set_data('ui_manager', ui_manager)
accel_group = ui_manager.get_accel_group()
window.add_accel_group(accel_group)
ui_manager.add_ui_from_string(view_ui_description)
# misc widgets
vbox = gtk.VBox(0, False)
sw = gtk.ScrolledWindow()
sw.set_shadow_type(gtk.SHADOW_IN)
pos_label = gtk.Label('Position')
view.set_data('pos_label', pos_label)
menu = ui_manager.get_widget('/MainMenu')
# layout widgets
window.add(vbox)
vbox.pack_start(menu, False, False, 0)
vbox.pack_start(sw, True, True, 0)
sw.add(view)
vbox.pack_start(pos_label, False, False, 0)
# setup view
font_desc = pango.FontDescription('monospace 10')
if font_desc:
view.modify_font(font_desc)
# change view attributes to match those of sourceview
if sourceview:
action = action_group.get_action('ShowNumbers')
action.set_active(sourceview.get_show_line_numbers())
action = action_group.get_action('ShowMarkers')
action.set_active(sourceview.get_show_line_marks())
action = action_group.get_action('ShowMargin')
action.set_active(sourceview.get_show_right_margin())
action = action_group.get_action('AutoIndent')
action.set_active(sourceview.get_auto_indent())
action = action_group.get_action('InsertSpaces')
action.set_active(sourceview.get_insert_spaces_instead_of_tabs())
action = action_group.get_action('TabsWidth%d' % sourceview.get_tab_width())
if action:
action.set_active(True)
# add marker pixbufs
pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join(DATADIR,
'pixmaps/apple-green.png'))
if pixbuf:
view.set_mark_category_pixbuf(MARK_CATEGORY_1, pixbuf)
else:
print 'could not load marker 1 image. Spurious messages might get triggered'
pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join(DATADIR,
'pixmaps/apple-red.png'))
if pixbuf:
view.set_mark_category_pixbuf(MARK_CATEGORY_2, pixbuf)
else:
print 'could not load marker 2 image. Spurious messages might get triggered'
vbox.show_all()
return window
######################################################################
##### create main window
def create_main_window(buffer):
window = create_view_window(buffer)
ui_manager = window.get_data('ui_manager')
# buffer action group
action_group = gtk.ActionGroup('BufferActions')
action_group.add_actions(buffer_actions, buffer)
ui_manager.insert_action_group(action_group, 1)
# merge buffer ui
ui_manager.add_ui_from_string(buffer_ui_description)
# preselect menu checkitems
groups = ui_manager.get_action_groups()
# retrieve the view action group at position 0 in the list
action_group = groups[0]
action = action_group.get_action('ShowNumbers')
action.set_active(True)
action = action_group.get_action('ShowMarkers')
action.set_active(True)
action = action_group.get_action('ShowMargin')
action.set_active(True)
action = action_group.get_action('AutoIndent')
action.set_active(True)
action = action_group.get_action('InsertSpaces')
action.set_active(True)
action = action_group.get_action('TabsWidth4')
action.set_active(True)
return window
######################################################################
##### main
def main(args):
# create buffer
lm = gtksourceview2.LanguageManager()
buffer = gtksourceview2.Buffer()
buffer.set_data('languages-manager', lm)
# parse arguments
if len(args) >= 2:
open_file(buffer, args[1])
else:
open_file_cb(None, buffer)
# create first window
window = create_main_window(buffer)
window.set_default_size(500, 500)
window.show()
# main loop
if __name__ == '__main__': gtk.main()
if __name__ == '__main__':
if '--debug' in sys.argv:
import pdb
pdb.run('main(sys.argv)')
else:
main(sys.argv)