-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourceview.py
More file actions
109 lines (87 loc) · 3.73 KB
/
sourceview.py
File metadata and controls
109 lines (87 loc) · 3.73 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
# Copyright (C) 2009 Vincent Legoll <vincent.legoll@gmail.com>
# Copyright (C) 2010-2011, 2013-2014 Kai Willadsen <kai.willadsen@gmail.com>
#
# 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, see <http://www.gnu.org/licenses/>.
from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import GLib
from gi.repository import Gtk
from gi.repository import GtkSource
from meld.settings import bind_settings, settings
class LanguageManager(object):
manager = GtkSource.LanguageManager()
@classmethod
def get_language_from_file(cls, filename):
f = Gio.File.new_for_path(filename)
try:
info = f.query_info(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
0, None)
except GLib.GError:
return None
content_type = info.get_content_type()
return cls.manager.guess_language(filename, content_type)
@classmethod
def get_language_from_mime_type(cls, mime_type):
content_type = Gio.content_type_from_mime_type(mime_type)
return cls.manager.guess_language(None, content_type)
class MeldSourceView(GtkSource.View):
__gtype_name__ = "MeldSourceView"
__gsettings_bindings__ = (
('indent-width', 'tab-width'),
('insert-spaces-instead-of-tabs', 'insert-spaces-instead-of-tabs'),
('draw-spaces', 'draw-spaces'),
('wrap-mode', 'wrap-mode'),
)
replaced_entries = (
# We replace the default GtkSourceView undo mechanism
(Gdk.KEY_z, Gdk.ModifierType.CONTROL_MASK),
(Gdk.KEY_z, Gdk.ModifierType.CONTROL_MASK |
Gdk.ModifierType.SHIFT_MASK),
# We replace the default line movement behaviour of Alt+Up/Down
(Gdk.KEY_Up, Gdk.ModifierType.MOD1_MASK),
(Gdk.KEY_KP_Up, Gdk.ModifierType.MOD1_MASK),
(Gdk.KEY_Down, Gdk.ModifierType.MOD1_MASK),
(Gdk.KEY_KP_Down, Gdk.ModifierType.MOD1_MASK),
# ...and Alt+Left/Right
(Gdk.KEY_Left, Gdk.ModifierType.MOD1_MASK),
(Gdk.KEY_KP_Left, Gdk.ModifierType.MOD1_MASK),
(Gdk.KEY_Right, Gdk.ModifierType.MOD1_MASK),
(Gdk.KEY_KP_Right, Gdk.ModifierType.MOD1_MASK),
)
def __init__(self, *args, **kwargs):
super(MeldSourceView, self).__init__(*args, **kwargs)
bind_settings(self)
binding_set = Gtk.binding_set_find('GtkSourceView')
for key, modifiers in self.replaced_entries:
Gtk.binding_entry_remove(binding_set, key, modifiers)
def late_bind(self):
settings.bind(
'show-line-numbers', self, 'show-line-numbers',
Gio.SettingsBindFlags.DEFAULT)
def get_y_for_line_num(self, line):
buf = self.get_buffer()
it = buf.get_iter_at_line(line)
y, h = self.get_line_yrange(it)
if line >= buf.get_line_count():
return y + h - 1
return y
def get_line_num_for_y(self, y):
return self.get_line_at_y(y)[0].get_line()
class CommitMessageSourceView(GtkSource.View):
__gtype_name__ = "CommitMessageSourceView"
__gsettings_bindings__ = (
('indent-width', 'tab-width'),
('insert-spaces-instead-of-tabs', 'insert-spaces-instead-of-tabs'),
('draw-spaces', 'draw-spaces'),
)