1
# -*- coding: utf-8 -*-
2
# Copyright © 2005 Lateef Alabi-Oki
4
# This file is part of Scribes.
6
# Scribes is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# Scribes is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Scribes; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
All cursor and caret operations are defined in this module. The module should be
22
designed to be as generic as possible.
24
@author: Lateef Alabi-Oki
25
@organization: The Scribes Project
26
@copyright: Copyright © 2005 Lateef Alabi-Oki
27
@license: GNU GPLv2 or Later
28
@contact: mystilleef@gmail.com
31
def get_cursor_iterator(textbuffer):
33
Get the cursor's iterator in the text buffer.
35
@param textbuffer: A text buffer.
36
@type textbuffer: A gtk.TextBuffer object.
38
@return: The position of the cursor in the text buffer.
39
@rtype: A gtk.TextIter object.
41
cursor_mark = textbuffer.get_insert()
42
cursor_iterator = textbuffer.get_iter_at_mark(cursor_mark)
43
return cursor_iterator
45
def get_cursor_line(textbuffer):
47
Return the line the cursor is on.
49
@param textbuffer: A text buffer.
50
@type textbuffer: A gtk.TextBuffer object.
52
@return: The line the cursor is on.
53
@rtype: An Integer object.
55
cursor_iterator = get_cursor_iterator(textbuffer)
56
cursor_line = cursor_iterator.get_line()
59
def get_cursor_index(textbuffer):
61
Return the index of the cursor on the cursor line.
63
@param textbuffer: A text buffer.
64
@type textbuffer: A gtk.TextBuffer object.
66
@return: The index of the cursor on the cursor line.
67
@rtype: An Integer object.
69
cursor_iterator = get_cursor_iterator(textbuffer)
70
cursor_index = cursor_iterator.get_line_index()
73
def get_cursor_position(textview):
75
Return the line and column the cursor is on.
77
@param textbuffer: A text buffer.
78
@type textbuffer: A gtk.TextBuffer object.
80
@return: A tuple containing line and column the cursor is on.
81
@rtype: A Tuple object.
83
textbuffer = textview.get_buffer()
84
cursor_line = get_cursor_line(textbuffer)
85
start_iterator = textbuffer.get_iter_at_line(cursor_line)
86
cursor_iterator = get_cursor_iterator(textbuffer)
87
line_text = textbuffer.get_text(start_iterator, cursor_iterator)
88
tabs_width = textview.get_tabs_width()
90
from operator import eq
91
for characters in line_text:
92
if eq(characters, "\t"):
93
count += (tabs_width - (count % tabs_width))
97
return cursor_line, cursor_column
99
def update_cursor_position(statusbar, textview):
100
line, column = get_cursor_position(textview)
101
from internationalization import msg0329
102
string = msg0329 % ((line + 1), (column + 1))
103
statusbar.pop(statusbar.context_id)
104
statusbar.context_id = statusbar.get_context_id(string)
105
statusbar.push(statusbar.context_id, string)
108
def set_textview_cursor(textview, cursor_type=None):
110
Change the textview cursor to the one specified by the cursor_type
111
parameter. If no cursor_type is provided, hide the textview cursor.
113
@param textview: A textview object for a text buffer.
114
@type textview: A gtk.TextView object.
116
@param cursor_type: A bitmap image used for the mouse pointer.
117
@type cursor_type: A gtk.gdk Cursor Constant.
120
from gtk import TEXT_WINDOW_TEXT
121
window = textview.get_window(TEXT_WINDOW_TEXT)
122
from gtk.gdk import Cursor
124
window.set_cursor(Cursor(cursor_type))
126
# Make the textview cursor invisible.
127
from gtk.gdk import Pixmap, Color
128
pixmap = Pixmap(None, 1, 1, 1)
130
window.set_cursor(Cursor(pixmap, pixmap, color, color, 0, 0))
135
def show_textview_cursor(textview):
137
Show the default "XTERM" textview cursor.
139
@param textview: A textview object for a text buffer.
140
@type textview: A gtk.TextView object.
143
from gtk.gdk import XTERM
144
set_textview_cursor(textview, XTERM)
147
def hide_textview_cursor(textview):
149
Hide the textview cursor.
151
@param textview: A textview object for a text buffer.
152
@type textview: A gtk.TextView object.
155
set_textview_cursor(textview)
158
def show_busy_textview_cursor(textview):
160
Show the "WATCH" textview cursor.
162
@param textview: A textview object for a text buffer.
163
@type textview: A gtk.TextView object.
166
from gtk.gdk import WATCH
167
set_textview_cursor(textview, WATCH)
170
def move_view_to_cursor(textview):
171
textbuffer = textview.get_buffer()
172
cursor_mark = textbuffer.get_insert()
173
textview.scroll_to_mark(cursor_mark, 0.05, False, 0.5, 0.5)
176
def word_to_cursor(textbuffer):
177
cursor_position = get_cursor_iterator(textbuffer)
178
if cursor_position.starts_line():
180
from cursor import get_cursor_line
181
line = get_cursor_line(textbuffer)
182
begin_position = textbuffer.get_iter_at_line(line)
183
text = textbuffer.get_text(begin_position, cursor_position)
185
if not text[-1] in (" ", "\t"):
186
text = text.replace("\t", " ")
187
string_list = text.split(" ")
188
return string_list[-1]
191
def get_template_trigger(textbuffer):
192
cursor_position = get_cursor_iterator(textbuffer)
193
if cursor_position.starts_line():
195
iterator = cursor_position.copy()
196
iterator.backward_char()
197
do_forward_char = True
198
found_alphanumeric_characters = False
199
while iterator.get_char().isalnum():
200
found_alphanumeric_characters = True
201
if iterator.starts_line():
202
do_forward_char = False
204
iterator.backward_char()
205
if found_alphanumeric_characters is False:
208
iterator.forward_char()
209
trigger = textbuffer.get_text(iterator, cursor_position)
212
def get_word_to_cursor(textbuffer):
213
cursor_position = get_cursor_iterator(textbuffer)
214
from word import ends_word, get_word
215
from operator import not_
216
if not_(ends_word(cursor_position)): return None
217
word = get_word(textbuffer, cursor_position)
220
def get_word_before_cursor(textbuffer):
221
word = get_word_to_cursor(textbuffer)
222
if word and len(word) > 2:
226
def get_cursor_window_coordinates(textview):
228
Get the window coordinates of the cursor in the text editor' buffer.
230
@param editor: Reference to the editor object.
231
@type editor: An editor object
233
@return: The position of the cursor in the text editor's buffer
234
@rtype: A tuple representing the x and y coordinates of the cursor's
235
position in the text editor' buffer.
237
# Get the cursor's iterator.
238
cursor_iterator = get_cursor_iterator(textview.get_buffer())
240
# Get the cursor's buffer coordinates.
241
rectangle = textview.get_iter_location(cursor_iterator)
243
# Get the cursor's window coordinates.
244
from gtk import TEXT_WINDOW_TEXT
245
position = textview.buffer_to_window_coords(TEXT_WINDOW_TEXT, rectangle.x,
247
cursor_x = position[0]
248
cursor_y = position[1]
249
return cursor_x, cursor_y
251
def get_cursor_size(textview):
253
Get the cursor's size.
255
@param editor: Reference to the editor object.
256
@type editor: An editor object
258
# Get the cursor's iterator.
259
cursor_iterator = get_cursor_iterator(textview.get_buffer())
261
# Get the cursor's size via its buffer coordinates.
262
rectangle = textview.get_iter_location(cursor_iterator)
263
cursor_width = rectangle.width
264
cursor_height = rectangle.height
265
return cursor_width, cursor_height
268
from psyco import bind
269
bind(update_cursor_position)
270
bind(get_cursor_size)
271
bind(get_word_before_cursor)
272
bind(get_word_to_cursor)
273
bind(get_template_trigger)
274
bind(get_cursor_iterator)
275
bind(get_cursor_line)
276
bind(get_cursor_index)
277
bind(get_cursor_window_coordinates)
278
bind(move_view_to_cursor)