-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor.py
More file actions
64 lines (58 loc) · 1.92 KB
/
cursor.py
File metadata and controls
64 lines (58 loc) · 1.92 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
class Cursor:
def __init__(self, lines):
self.lines = lines
self.cx = 0
self.cy = 0
self.scroll = 0
self.scroll_x = 0
self.visual_start = None
self.marks = {}
def fix_cursor(self):
if self.cy < 0:
self.cy = 0
if self.cy >= len(self.lines):
self.cy = len(self.lines) - 1
if self.cx < 0:
self.cx = 0
if self.cx > len(self.lines[self.cy]):
self.cx = len(self.lines[self.cy])
if self.cx < self.scroll_x:
self.scroll_x = self.cx
def move_cursor(self, dy, dx):
self.cy += dy
self.fix_cursor()
self.cx += dx
if self.cx > len(self.lines[self.cy]):
self.cx = len(self.lines[self.cy])
if self.cx < 0:
self.cx = 0
if self.cx < self.scroll_x:
self.scroll_x = self.cx
def move_word_forward(self):
line = self.lines[self.cy]
i = self.cx + 1
while i < len(line) and line[i].isalnum():
i += 1
while i < len(line) and not line[i].isalnum():
i += 1
self.cx = i if i <= len(line) else len(line)
def move_word_backward(self):
line = self.lines[self.cy]
i = self.cx - 1
while i > 0 and line[i].isalnum():
i -= 1
while i > 0 and not line[i].isalnum():
i -= 1
self.cx = i if i >= 0 else 0
def get_visual_range(self):
if self.visual_start is None:
return None
(y1, x1), (y2, x2) = self.visual_start, (self.cy, self.cx)
if (y1, x1) > (y2, x2):
(y1, x1), (y2, x2) = (y2, x2), (y1, x1)
return (y1, x1, y2, x2)
def visual_select(self):
if self.visual_start is None:
self.visual_start = (self.cy, self.cx)
else:
self.visual_start = None