dasha-framework 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dasha/__init__.py +9 -0
- dasha/__main__.py +534 -0
- dasha/animation.py +90 -0
- dasha/application.py +276 -0
- dasha/border.py +34 -0
- dasha/canvas.py +331 -0
- dasha/client.py +642 -0
- dasha/clipboard.py +94 -0
- dasha/config.py +89 -0
- dasha/desktop.py +58 -0
- dasha/dialog.py +601 -0
- dasha/events.py +471 -0
- dasha/filedialog.py +323 -0
- dasha/help.py +900 -0
- dasha/helppages/applications.md +25 -0
- dasha/helppages/index.md +20 -0
- dasha/helppages/keyboard.md +45 -0
- dasha/helppages/menus.md +32 -0
- dasha/helppages/running.md +18 -0
- dasha/helppages/settings.md +25 -0
- dasha/helppages/windows.md +36 -0
- dasha/host.py +120 -0
- dasha/hosting.py +1081 -0
- dasha/inbox.py +122 -0
- dasha/install.py +369 -0
- dasha/keytest.py +86 -0
- dasha/logs.py +104 -0
- dasha/manager.py +2468 -0
- dasha/markdown.py +1173 -0
- dasha/menu.py +701 -0
- dasha/printing.py +42 -0
- dasha/processes.py +293 -0
- dasha/protocol.py +335 -0
- dasha/pseudoterminal/__init__.py +43 -0
- dasha/pseudoterminal/_posix.py +133 -0
- dasha/pseudoterminal/_windows.py +352 -0
- dasha/pseudoterminal/base.py +151 -0
- dasha/py.typed +0 -0
- dasha/recovery.py +239 -0
- dasha/registry.py +475 -0
- dasha/remote.py +284 -0
- dasha/rundialog.py +342 -0
- dasha/scrollbar.py +193 -0
- dasha/settings/__init__.py +326 -0
- dasha/settings/appearance.py +353 -0
- dasha/settings/applications.py +165 -0
- dasha/settings/colours.py +94 -0
- dasha/settings/keys.py +225 -0
- dasha/settings/lists.py +11 -0
- dasha/shell.py +118 -0
- dasha/shortcuts.py +536 -0
- dasha/statusbar.py +111 -0
- dasha/style.py +256 -0
- dasha/terminal/__init__.py +236 -0
- dasha/terminal/_posix.py +130 -0
- dasha/terminal/_winbuffer.py +264 -0
- dasha/terminal/_windows.py +349 -0
- dasha/terminal/ansi.py +46 -0
- dasha/theme.py +749 -0
- dasha/vt.py +1216 -0
- dasha/widgets/__init__.py +90 -0
- dasha/widgets/autocomplete.py +153 -0
- dasha/widgets/base.py +298 -0
- dasha/widgets/button.py +224 -0
- dasha/widgets/checkbox.py +52 -0
- dasha/widgets/cluster.py +357 -0
- dasha/widgets/collapsible.py +224 -0
- dasha/widgets/combobox.py +157 -0
- dasha/widgets/container.py +182 -0
- dasha/widgets/editing.py +195 -0
- dasha/widgets/groupbox.py +126 -0
- dasha/widgets/label.py +106 -0
- dasha/widgets/listbox.py +535 -0
- dasha/widgets/paragraph.py +265 -0
- dasha/widgets/password.py +60 -0
- dasha/widgets/progressbar.py +196 -0
- dasha/widgets/radiobutton.py +61 -0
- dasha/widgets/scrollbar.py +157 -0
- dasha/widgets/slider.py +196 -0
- dasha/widgets/spinbox.py +237 -0
- dasha/widgets/splitter.py +274 -0
- dasha/widgets/switch.py +190 -0
- dasha/widgets/table.py +413 -0
- dasha/widgets/tabs.py +291 -0
- dasha/widgets/terminalview.py +475 -0
- dasha/widgets/textarea.py +990 -0
- dasha/widgets/textbox.py +591 -0
- dasha/widgets/tree.py +443 -0
- dasha/window.py +992 -0
- dasha/windowmenu.py +165 -0
- dasha_framework-0.0.1.dist-info/METADATA +346 -0
- dasha_framework-0.0.1.dist-info/RECORD +96 -0
- dasha_framework-0.0.1.dist-info/WHEEL +5 -0
- dasha_framework-0.0.1.dist-info/entry_points.txt +3 -0
- dasha_framework-0.0.1.dist-info/licenses/LICENSE +201 -0
- dasha_framework-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,990 @@
|
|
|
1
|
+
"""Text areas, which hold many lines of text to edit."""
|
|
2
|
+
|
|
3
|
+
from bisect import bisect_right
|
|
4
|
+
from collections import Counter
|
|
5
|
+
from collections.abc import Callable, Iterable
|
|
6
|
+
from typing import NamedTuple
|
|
7
|
+
|
|
8
|
+
from dasha.canvas import Canvas, Rect
|
|
9
|
+
from dasha.clipboard import CLIPBOARD, Clipboard
|
|
10
|
+
from dasha.events import MouseEvent
|
|
11
|
+
from dasha.shortcuts import Shortcuts
|
|
12
|
+
from dasha.style import BLUE, BRIGHT_WHITE, BRIGHT_YELLOW, CYAN, GREEN, Style
|
|
13
|
+
from dasha.widgets.base import Widget
|
|
14
|
+
from dasha.menu import Menu
|
|
15
|
+
from dasha.widgets.editing import UndoHistory, edit_menu, word_left, word_right
|
|
16
|
+
|
|
17
|
+
type Position = tuple[int, int]
|
|
18
|
+
"""A place in the text: (line, character), both counted from 0."""
|
|
19
|
+
|
|
20
|
+
NUMBERS_BAR = "\N{BOX DRAWINGS LIGHT VERTICAL}"
|
|
21
|
+
"""The bar between the line numbers and the text: ``│`` (code page 437 179)."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _Change(NamedTuple):
|
|
25
|
+
"""One undoable change: at ``start``, ``removed`` was replaced by
|
|
26
|
+
``inserted``; the cursor and selection were as given."""
|
|
27
|
+
|
|
28
|
+
start: Position
|
|
29
|
+
removed: str
|
|
30
|
+
inserted: str
|
|
31
|
+
cursor: Position
|
|
32
|
+
anchor: Position | None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# pylint: disable-next=too-many-instance-attributes,too-many-public-methods
|
|
36
|
+
class TextArea(Widget):
|
|
37
|
+
"""A ``TextArea`` holds many lines of text to edit. Put one in an
|
|
38
|
+
otherwise empty window, and the window becomes a text editor:
|
|
39
|
+
|
|
40
|
+
::
|
|
41
|
+
|
|
42
|
+
editor = Window("Editor", rect, resizable=True, maximisable=True)
|
|
43
|
+
area = editor.add(TextArea(0, 0, text=notes))
|
|
44
|
+
|
|
45
|
+
**Size and scrolling.** A text area is always at least as big as the rest of its
|
|
46
|
+
window's interior (``width`` and ``height``, both 1 by default, are the least it
|
|
47
|
+
is), and it follows the window as it is resized or maximised. It also grows with
|
|
48
|
+
its text: when the text is longer or wider than the window, the text area is
|
|
49
|
+
bigger than the window, so the window's own frame scroll bars appear, and the
|
|
50
|
+
window scrolls to keep the cursor in view. When the cursor fits without
|
|
51
|
+
scrolling sideways, the window shows the lines from their start. Deleting text
|
|
52
|
+
shrinks it again, and the scroll bars go. Only the lines and columns showing are
|
|
53
|
+
drawn, so long texts cost no more than short ones.
|
|
54
|
+
|
|
55
|
+
**Word wrap.** With ``word_wrap`` on (off by default), a line too long for the
|
|
56
|
+
window goes on in the rows below it, broken after a space where it can be, and
|
|
57
|
+
within a word only when the word is longer than a row. The text area then stays
|
|
58
|
+
as wide as the window, which never scrolls sideways; resizing the window wraps
|
|
59
|
+
the text again. The line breaks in the text stay where they were: wrapping only
|
|
60
|
+
changes how it is shown. Up and Down, PgUp and PgDn, and clicks go by the rows
|
|
61
|
+
shown, while Home and End, Ctrl+Y and a double-click take the whole line.
|
|
62
|
+
|
|
63
|
+
**Line numbers.** With ``line_numbers`` on (off by default), each line's number
|
|
64
|
+
shows at its left, right-aligned in a column of its own, with ``│`` between it
|
|
65
|
+
and the text. The column is ``number_digits`` (3) wide, and wider once there are
|
|
66
|
+
more lines than that many digits can number: four digits from line 1000. With
|
|
67
|
+
word wrap, a line's number is on its first row only. The numbers stay in view
|
|
68
|
+
as the window scrolls sideways; a click on them goes to the start of that row.
|
|
69
|
+
|
|
70
|
+
**Editing.** It edits as a text box does, with the same keys, plus some of its
|
|
71
|
+
own:
|
|
72
|
+
|
|
73
|
+
- Left / Right (``text_left`` / ``text_right``): Move a character, onto the line
|
|
74
|
+
above or below at the ends
|
|
75
|
+
- Ctrl+Left / Ctrl+Right (``text_word_left`` / ``text_word_right``): Move a
|
|
76
|
+
word, onto the line above or below at the ends
|
|
77
|
+
- Up / Down (``area_up`` / ``area_down``): Move a line, keeping to the same
|
|
78
|
+
column where the line is long enough
|
|
79
|
+
- PgUp / PgDn (``area_page_up`` / ``area_page_down``): Move a page: what the
|
|
80
|
+
window shows, less one line
|
|
81
|
+
- Home / End (``text_home`` / ``text_end``): Go to the start / end of the line
|
|
82
|
+
- Ctrl+Home / Ctrl+End (``area_top`` / ``area_bottom``): Go to the start / end
|
|
83
|
+
of the text
|
|
84
|
+
- Shift with any of these (bar Ctrl+Home/End) (``text_select_*``,
|
|
85
|
+
``area_select_up`` / ``area_select_down``): Select as it moves
|
|
86
|
+
- Ctrl+A (``text_select_all``): Select all the text
|
|
87
|
+
- Enter (``area_newline``): Start a new line
|
|
88
|
+
- Tab (``area_tab``): Type a tab
|
|
89
|
+
- Ctrl+Tab / Ctrl+Shift+Tab (``area_next_widget`` / ``area_previous_widget``):
|
|
90
|
+
Move the focus to the next / previous control
|
|
91
|
+
- Ctrl+Z (``text_undo``): Undo the last change; again for the one before
|
|
92
|
+
- Alt+Z, Ctrl+Shift+Z (``text_redo``): Redo what was undone
|
|
93
|
+
- Backspace / Del (``text_backspace`` / ``text_delete``): Delete the selection,
|
|
94
|
+
or a character; at a line's start or end, join the lines
|
|
95
|
+
- Ctrl+Y (``area_delete_line``): Delete the line
|
|
96
|
+
- Ctrl+X / Ctrl+C / Ctrl+V (``text_cut`` / ``text_copy`` / ``text_paste``): Cut,
|
|
97
|
+
copy and paste, line breaks and all
|
|
98
|
+
- Click: Put the cursor there (past a line's end, at its end)
|
|
99
|
+
- Shift+click, drag: Select to there
|
|
100
|
+
- Double-click: Select the whole line
|
|
101
|
+
|
|
102
|
+
Typing a character, pressing Enter or pasting (with Ctrl+V or through the
|
|
103
|
+
terminal) replaces the selection, just as in a text box. Only text is pasted:
|
|
104
|
+
anything else on the clipboard is ignored. Shift+Tab and Escape are left for the
|
|
105
|
+
window, and the mouse wheel scrolls the window.
|
|
106
|
+
|
|
107
|
+
**Tabs and the focus.** Tab types a tab, which stays in the text and is drawn as
|
|
108
|
+
spaces up to the next multiple of ``tab_size`` (4) columns. The cursor, clicks,
|
|
109
|
+
selections and Up/Down all go by those columns on screen. Since Tab is taken,
|
|
110
|
+
Ctrl+Tab and Ctrl+Shift+Tab move the focus to the next or previous control
|
|
111
|
+
instead, in any window: out of a text area, and into one from the control before
|
|
112
|
+
it. Many terminals send Ctrl+Tab just as they send Tab, so it never reaches
|
|
113
|
+
Dasha Framework; those that can tell them apart send it in the "CSI u" or xterm
|
|
114
|
+
"modifyOtherKeys" form, which Dasha Framework reads. Where it does not arrive,
|
|
115
|
+
``area_next_widget`` and ``area_previous_widget`` can be bound to other keys
|
|
116
|
+
(``dasha --key-test`` shows what arrives). They cannot both be left without
|
|
117
|
+
keys.
|
|
118
|
+
|
|
119
|
+
**Undo.** Ctrl+Z undoes the last change, and pressing it again undoes the one
|
|
120
|
+
before, back through the last ``undo_limit`` changes (20; any number is fine,
|
|
121
|
+
2000 say, or ``None`` for no limit; set it on ``TextArea`` for a whole project
|
|
122
|
+
or on one text area). Typing is undone a word at a time: a run of typed
|
|
123
|
+
characters is one change, and a space starts the next. Undoing puts the cursor
|
|
124
|
+
and the selection back as they were. Setting ``text`` from code starts afresh,
|
|
125
|
+
with nothing to undo.
|
|
126
|
+
|
|
127
|
+
**Redo.** Alt+Z redoes what Ctrl+Z undid, a step at a time, cursor and selection
|
|
128
|
+
included, and Ctrl+Shift+Z does the same in terminals that send it: most send
|
|
129
|
+
Ctrl+Shift+Z just as they send Ctrl+Z, so Alt+Z, which arrives everywhere, is
|
|
130
|
+
the first key for it (Ctrl+Y deletes a line). As in any
|
|
131
|
+
editor, making a new change forgets what could be redone. Text boxes undo and
|
|
132
|
+
redo the same way.
|
|
133
|
+
|
|
134
|
+
``text`` (and ``value``) is all the text, its lines joined with ``"\\n"``, and
|
|
135
|
+
``lines`` the lines themselves (each one line, however many rows it wraps to).
|
|
136
|
+
Text put in has its line breaks made ``\\n``. Control characters stay, as a file
|
|
137
|
+
had them, drawn as their code page 437 pictures (escape as ←), never sent to
|
|
138
|
+
the terminal to act on. ``cursor`` is (line, character),
|
|
139
|
+
``selection`` the selected (start, end) and ``selected_text`` what they hold.
|
|
140
|
+
``select``, ``select_all``, ``select_line``, ``insert``, ``delete_line``,
|
|
141
|
+
``cut``, ``copy``, ``paste``, ``undo`` and ``redo`` do from code what the keys
|
|
142
|
+
do. ``on_change(area)`` is called after each edit, undoing included. Colours:
|
|
143
|
+
``textarea`` (bright yellow on blue), ``textarea.selected`` and
|
|
144
|
+
``textarea.numbers`` (cyan on blue).
|
|
145
|
+
"""
|
|
146
|
+
|
|
147
|
+
focusable = True
|
|
148
|
+
style: Style = Style(foreground=BRIGHT_YELLOW, background=BLUE)
|
|
149
|
+
selected_style: Style = Style(foreground=BRIGHT_WHITE, background=GREEN, bold=True)
|
|
150
|
+
"""The style of selected text, while the text area has the focus."""
|
|
151
|
+
number_style: Style = Style(foreground=CYAN, background=BLUE)
|
|
152
|
+
"""The style of the line numbers, and of the bar beside them."""
|
|
153
|
+
number_digits: int = 3
|
|
154
|
+
"""The smallest width of the line numbers' column, in digits; it is
|
|
155
|
+
wider when there are more lines than that many digits can number."""
|
|
156
|
+
clipboard: Clipboard = CLIPBOARD
|
|
157
|
+
"""The clipboard used for cut, copy and paste."""
|
|
158
|
+
tab_size: int = 4
|
|
159
|
+
"""How many columns apart the tab stops are."""
|
|
160
|
+
undo_limit: int | None = 20
|
|
161
|
+
"""How many changes Ctrl+Z can undo, oldest forgotten first. Any number
|
|
162
|
+
will do (2000, say), or ``None`` for no limit; set it on the class for a
|
|
163
|
+
whole project, or on one text area."""
|
|
164
|
+
|
|
165
|
+
def __init__( # pylint: disable=too-many-arguments
|
|
166
|
+
self,
|
|
167
|
+
row: int,
|
|
168
|
+
column: int,
|
|
169
|
+
width: int = 1,
|
|
170
|
+
height: int = 1,
|
|
171
|
+
*,
|
|
172
|
+
text: str = "",
|
|
173
|
+
word_wrap: bool = False,
|
|
174
|
+
line_numbers: bool = False,
|
|
175
|
+
on_change: Callable[["TextArea"], object] | None = None,
|
|
176
|
+
name: str | None = None,
|
|
177
|
+
status: str = "",
|
|
178
|
+
) -> None:
|
|
179
|
+
super().__init__(row, column, width, height, name=name, status=status)
|
|
180
|
+
self._least = (max(1, width), max(1, height))
|
|
181
|
+
self.on_change = on_change
|
|
182
|
+
self.lines: list[str] = [""]
|
|
183
|
+
"""The text, a line at a time (without line breaks)."""
|
|
184
|
+
self._cursor: Position = (0, 0)
|
|
185
|
+
self._anchor: Position | None = None # the other end of the selection
|
|
186
|
+
self._goal = 0 # the screen column Up and Down aim for
|
|
187
|
+
self._history: UndoHistory[_Change] = UndoHistory()
|
|
188
|
+
self._typing_at: Position | None = None # where typing goes on a step
|
|
189
|
+
# How many lines are drawn how wide, and at what tab size: kept up
|
|
190
|
+
# to date as lines change, so finding the widest line on every
|
|
191
|
+
# keystroke does not mean measuring them all.
|
|
192
|
+
self._widths: Counter[int] = Counter()
|
|
193
|
+
self._widths_tab_size = self.tab_size
|
|
194
|
+
# Word wrap: where each line is broken (the index of the first
|
|
195
|
+
# character of each row after its first), for rows how many columns
|
|
196
|
+
# wide (0: to be worked out again) at what tab size; and the row
|
|
197
|
+
# each line starts on, worked out again when the rows change.
|
|
198
|
+
self._word_wrap = word_wrap
|
|
199
|
+
self._line_numbers = line_numbers
|
|
200
|
+
self._breaks: list[tuple[int, ...]] = []
|
|
201
|
+
self._wrap_limit = 0
|
|
202
|
+
self._wrap_tab_size = self.tab_size
|
|
203
|
+
self._row_starts: list[int] | None = None
|
|
204
|
+
self.text = text
|
|
205
|
+
|
|
206
|
+
# The text
|
|
207
|
+
|
|
208
|
+
@property
|
|
209
|
+
def text(self) -> str:
|
|
210
|
+
"""All the text. Setting it puts the cursor at the start, and forgets
|
|
211
|
+
everything that could be undone."""
|
|
212
|
+
return "\n".join(self.lines)
|
|
213
|
+
|
|
214
|
+
@text.setter
|
|
215
|
+
def text(self, text: str) -> None:
|
|
216
|
+
self.lines = self._clean(text).split("\n")
|
|
217
|
+
self._count_widths()
|
|
218
|
+
self._wrap_limit = 0 # wrapped afresh by fit
|
|
219
|
+
self._cursor, self._anchor, self._goal = (0, 0), None, 0
|
|
220
|
+
self._history.clear()
|
|
221
|
+
self._typing_at = None
|
|
222
|
+
self.fit()
|
|
223
|
+
|
|
224
|
+
@property
|
|
225
|
+
def value(self) -> str:
|
|
226
|
+
return self.text
|
|
227
|
+
|
|
228
|
+
@property
|
|
229
|
+
def line_numbers(self) -> bool:
|
|
230
|
+
"""Whether each line's number is shown at its left. Off by default."""
|
|
231
|
+
return self._line_numbers
|
|
232
|
+
|
|
233
|
+
@line_numbers.setter
|
|
234
|
+
def line_numbers(self, on: bool) -> None:
|
|
235
|
+
self._line_numbers = on
|
|
236
|
+
self.fit()
|
|
237
|
+
self._reveal()
|
|
238
|
+
|
|
239
|
+
@property
|
|
240
|
+
def numbers_width(self) -> int:
|
|
241
|
+
"""How many columns the line numbers take, including the bar; none
|
|
242
|
+
without ``line_numbers``."""
|
|
243
|
+
if not self._line_numbers:
|
|
244
|
+
return 0
|
|
245
|
+
return max(self.number_digits, len(str(len(self.lines)))) + 1
|
|
246
|
+
|
|
247
|
+
@property
|
|
248
|
+
def _text_column(self) -> int:
|
|
249
|
+
"""Where in the window's content the text's first column is: after
|
|
250
|
+
the line numbers."""
|
|
251
|
+
return self.rect.column + self.numbers_width
|
|
252
|
+
|
|
253
|
+
@property
|
|
254
|
+
def word_wrap(self) -> bool:
|
|
255
|
+
"""Whether long lines continue on the rows below, rather than past
|
|
256
|
+
the window's right edge. Off by default."""
|
|
257
|
+
return self._word_wrap
|
|
258
|
+
|
|
259
|
+
@word_wrap.setter
|
|
260
|
+
def word_wrap(self, on: bool) -> None:
|
|
261
|
+
self._word_wrap = on
|
|
262
|
+
self._wrap_limit = 0
|
|
263
|
+
self.fit()
|
|
264
|
+
self._goal = self._goal_of(self._cursor)
|
|
265
|
+
self._reveal()
|
|
266
|
+
|
|
267
|
+
@staticmethod
|
|
268
|
+
def _clean(text: str) -> str:
|
|
269
|
+
"""Text as it is kept: ``\\n`` line breaks, and the rest as it came,
|
|
270
|
+
control characters too (drawn as pictures), bar surrogates."""
|
|
271
|
+
text = text.replace("\r\n", "\n").replace("\r", "\n")
|
|
272
|
+
if text.replace("\n", "").replace("\t", "").isprintable():
|
|
273
|
+
return text # nothing to look at: no need to go a character at a time
|
|
274
|
+
return "".join(char for char in text if not "\ud800" <= char <= "\udfff")
|
|
275
|
+
|
|
276
|
+
@property
|
|
277
|
+
def cursor(self) -> Position:
|
|
278
|
+
"""Where typing goes, as (line, character). Setting it clears the
|
|
279
|
+
selection."""
|
|
280
|
+
return self._cursor
|
|
281
|
+
|
|
282
|
+
@cursor.setter
|
|
283
|
+
def cursor(self, position: Position) -> None:
|
|
284
|
+
self._move(position, extend=False)
|
|
285
|
+
|
|
286
|
+
def _clamp(self, position: Position) -> Position:
|
|
287
|
+
line = max(0, min(position[0], len(self.lines) - 1))
|
|
288
|
+
return line, max(0, min(position[1], len(self.lines[line])))
|
|
289
|
+
|
|
290
|
+
# Tabs: characters to screen columns and back
|
|
291
|
+
|
|
292
|
+
def _shown(self, line: str) -> str:
|
|
293
|
+
"""A line as it is drawn, its tabs spread to the tab stops."""
|
|
294
|
+
return line.expandtabs(self.tab_size)
|
|
295
|
+
|
|
296
|
+
def _count_widths(self) -> None:
|
|
297
|
+
"""Measure every line afresh."""
|
|
298
|
+
self._widths = Counter(len(self._shown(line)) for line in self.lines)
|
|
299
|
+
self._widths_tab_size = self.tab_size
|
|
300
|
+
|
|
301
|
+
def _recount(self, removed: Iterable[str], added: Iterable[str]) -> None:
|
|
302
|
+
"""Lines ``removed`` have gone and ``added`` arrived: count their
|
|
303
|
+
widths out and in."""
|
|
304
|
+
widths = self._widths
|
|
305
|
+
for line in removed:
|
|
306
|
+
width = len(self._shown(line))
|
|
307
|
+
widths[width] -= 1
|
|
308
|
+
if widths[width] <= 0:
|
|
309
|
+
del widths[width]
|
|
310
|
+
widths.update(len(self._shown(line)) for line in added)
|
|
311
|
+
|
|
312
|
+
def _column_of(self, position: Position) -> int:
|
|
313
|
+
"""The screen column (within the text area) of a position."""
|
|
314
|
+
line, index = position
|
|
315
|
+
return len(self._shown(self.lines[line][:index]))
|
|
316
|
+
|
|
317
|
+
def _index_at(self, line: int, column: int) -> int:
|
|
318
|
+
"""The character in a line that a screen column falls on (or after
|
|
319
|
+
the last, past the end); within a tab, the tab."""
|
|
320
|
+
width = 0
|
|
321
|
+
for index, char in enumerate(self.lines[line]):
|
|
322
|
+
after = (
|
|
323
|
+
(width // self.tab_size + 1) * self.tab_size
|
|
324
|
+
if char == "\t"
|
|
325
|
+
else width + 1
|
|
326
|
+
)
|
|
327
|
+
if column < after:
|
|
328
|
+
return index
|
|
329
|
+
width = after
|
|
330
|
+
return len(self.lines[line])
|
|
331
|
+
|
|
332
|
+
# Word wrap: lines into rows and back
|
|
333
|
+
|
|
334
|
+
def _line_breaks(self, text: str, limit: int) -> tuple[int, ...]:
|
|
335
|
+
"""Where a line is broken into rows ``limit`` columns wide: after the
|
|
336
|
+
last space that fits (a space that does not fit stays at the end of
|
|
337
|
+
its row), or within a word too long for a row. The last row is
|
|
338
|
+
never wider than ``limit``, leaving a cell for the cursor after it."""
|
|
339
|
+
if len(text) <= limit and "\t" not in text:
|
|
340
|
+
return () # fits, and no tabs to measure
|
|
341
|
+
ends = [] # the screen column after each character
|
|
342
|
+
column, tab = 0, self.tab_size
|
|
343
|
+
for char in text:
|
|
344
|
+
column = (column // tab + 1) * tab if char == "\t" else column + 1
|
|
345
|
+
ends.append(column)
|
|
346
|
+
breaks: list[int] = []
|
|
347
|
+
start = base = 0 # the row's first character, and its column
|
|
348
|
+
while ends[-1] - base > limit:
|
|
349
|
+
fits = bisect_right(ends, base + limit, lo=start) # the first that does not
|
|
350
|
+
if text[fits] == " ":
|
|
351
|
+
cut = fits + 1
|
|
352
|
+
else:
|
|
353
|
+
space = text.rfind(" ", start, fits)
|
|
354
|
+
cut = space + 1 if space >= start else max(fits, start + 1)
|
|
355
|
+
breaks.append(cut)
|
|
356
|
+
if cut >= len(text):
|
|
357
|
+
break # a space that did not fit ends the line: the cursor goes on
|
|
358
|
+
start, base = cut, ends[cut - 1]
|
|
359
|
+
return tuple(breaks)
|
|
360
|
+
|
|
361
|
+
def _wrap(self, limit: int) -> None:
|
|
362
|
+
"""Break every line into rows ``limit`` columns wide, afresh."""
|
|
363
|
+
self._wrap_limit = limit
|
|
364
|
+
self._wrap_tab_size = self.tab_size
|
|
365
|
+
self._breaks = [self._line_breaks(line, limit) for line in self.lines]
|
|
366
|
+
self._row_starts = None
|
|
367
|
+
|
|
368
|
+
def _rewrap(self, first: int, removed: int, added: list[str]) -> None:
|
|
369
|
+
"""``removed`` lines from ``first`` have been replaced by ``added``:
|
|
370
|
+
break those into rows (if word wrap is on and the rows are known)."""
|
|
371
|
+
if not self._word_wrap or not self._wrap_limit:
|
|
372
|
+
return
|
|
373
|
+
new = [self._line_breaks(line, self._wrap_limit) for line in added]
|
|
374
|
+
old = self._breaks[first : first + removed]
|
|
375
|
+
if [len(rows) for rows in old] != [len(rows) for rows in new]:
|
|
376
|
+
self._row_starts = None # lines start on other rows now
|
|
377
|
+
self._breaks[first : first + removed] = new
|
|
378
|
+
|
|
379
|
+
def _line_rows(self) -> list[int]:
|
|
380
|
+
"""The row each line starts on, with word wrap."""
|
|
381
|
+
if self._row_starts is None:
|
|
382
|
+
starts, row = [], 0
|
|
383
|
+
for breaks in self._breaks:
|
|
384
|
+
starts.append(row)
|
|
385
|
+
row += len(breaks) + 1
|
|
386
|
+
self._row_starts = starts
|
|
387
|
+
return self._row_starts
|
|
388
|
+
|
|
389
|
+
def _row_count(self) -> int:
|
|
390
|
+
"""How many rows the text takes."""
|
|
391
|
+
if not self._word_wrap:
|
|
392
|
+
return len(self.lines)
|
|
393
|
+
return self._line_rows()[-1] + len(self._breaks[-1]) + 1
|
|
394
|
+
|
|
395
|
+
def _segment(self, row: int) -> tuple[int, int, int, bool]:
|
|
396
|
+
"""The line a row shows part of, its characters from and to (not
|
|
397
|
+
including), and whether it is the line's last row."""
|
|
398
|
+
if not self._word_wrap:
|
|
399
|
+
return row, 0, len(self.lines[row]), True
|
|
400
|
+
starts = self._line_rows()
|
|
401
|
+
line = bisect_right(starts, row) - 1
|
|
402
|
+
breaks = self._breaks[line]
|
|
403
|
+
part = row - starts[line]
|
|
404
|
+
start = breaks[part - 1] if part else 0
|
|
405
|
+
last = part == len(breaks)
|
|
406
|
+
return line, start, len(self.lines[line]) if last else breaks[part], last
|
|
407
|
+
|
|
408
|
+
def _row_of(self, position: Position) -> tuple[int, int]:
|
|
409
|
+
"""The row a position is shown on, and its column in that row. At a
|
|
410
|
+
break, it is the start of the row after."""
|
|
411
|
+
if not self._word_wrap:
|
|
412
|
+
return position[0], self._column_of(position)
|
|
413
|
+
line, index = position
|
|
414
|
+
breaks = self._breaks[line]
|
|
415
|
+
part = bisect_right(breaks, index)
|
|
416
|
+
start = breaks[part - 1] if part else 0
|
|
417
|
+
row = self._line_rows()[line] + part
|
|
418
|
+
return row, self._column_of(position) - self._column_of((line, start))
|
|
419
|
+
|
|
420
|
+
def _at_row(self, row: int, column: int) -> Position:
|
|
421
|
+
"""The position a column of a row falls on: within the row, or at
|
|
422
|
+
its end (just before its last character, for a row the next goes on
|
|
423
|
+
from, since after it is the next row's start)."""
|
|
424
|
+
row = max(0, min(row, self._row_count() - 1))
|
|
425
|
+
line, start, end, last = self._segment(row)
|
|
426
|
+
index = self._index_at(line, self._column_of((line, start)) + max(0, column))
|
|
427
|
+
return line, max(start, min(index, end if last else end - 1))
|
|
428
|
+
|
|
429
|
+
def _goal_of(self, position: Position) -> int:
|
|
430
|
+
"""The column Up and Down aim for from a position: on its row."""
|
|
431
|
+
return self._row_of(position)[1]
|
|
432
|
+
|
|
433
|
+
# The selection
|
|
434
|
+
|
|
435
|
+
@property
|
|
436
|
+
def selection(self) -> tuple[Position, Position] | None:
|
|
437
|
+
"""The selected text's (start, end), or ``None``."""
|
|
438
|
+
if self._anchor is None or self._anchor == self._cursor:
|
|
439
|
+
return None
|
|
440
|
+
return min(self._anchor, self._cursor), max(self._anchor, self._cursor)
|
|
441
|
+
|
|
442
|
+
@property
|
|
443
|
+
def selected_text(self) -> str:
|
|
444
|
+
"""The selected text, lines joined with ``"\\n"``."""
|
|
445
|
+
selection = self.selection
|
|
446
|
+
return "" if selection is None else self._between(*selection)
|
|
447
|
+
|
|
448
|
+
def _between(self, start: Position, end: Position) -> str:
|
|
449
|
+
"""The text from ``start`` to ``end``."""
|
|
450
|
+
(first, begin), (last, finish) = start, end
|
|
451
|
+
if first == last:
|
|
452
|
+
return self.lines[first][begin:finish]
|
|
453
|
+
middle = self.lines[first + 1 : last]
|
|
454
|
+
return "\n".join(
|
|
455
|
+
[self.lines[first][begin:], *middle, self.lines[last][:finish]]
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
def select(self, start: Position, end: Position) -> None:
|
|
459
|
+
"""Select from ``start`` to ``end``, leaving the cursor at ``end``."""
|
|
460
|
+
self._anchor = self._clamp(start)
|
|
461
|
+
self._move(end, extend=True)
|
|
462
|
+
|
|
463
|
+
def select_all(self) -> None:
|
|
464
|
+
"""Select all the text (Ctrl+A)."""
|
|
465
|
+
self.select((0, 0), (len(self.lines) - 1, len(self.lines[-1])))
|
|
466
|
+
|
|
467
|
+
def select_line(self, line: int) -> None:
|
|
468
|
+
"""Select one whole line, as a double-click does."""
|
|
469
|
+
line = self._clamp((line, 0))[0]
|
|
470
|
+
self.select((line, 0), (line, len(self.lines[line])))
|
|
471
|
+
|
|
472
|
+
def _move(self, position: Position, extend: bool, keep_goal: bool = False) -> None:
|
|
473
|
+
"""Move the cursor; with ``extend`` the selection goes with it."""
|
|
474
|
+
if not extend:
|
|
475
|
+
self._anchor = None
|
|
476
|
+
elif self._anchor is None:
|
|
477
|
+
self._anchor = self._cursor
|
|
478
|
+
self._cursor = self._clamp(position)
|
|
479
|
+
if not keep_goal:
|
|
480
|
+
self._goal = self._goal_of(self._cursor)
|
|
481
|
+
self._reveal()
|
|
482
|
+
|
|
483
|
+
def _vertical(self, lines: int) -> Position:
|
|
484
|
+
"""The position ``lines`` (rows, with word wrap) up (negative) or
|
|
485
|
+
down, at the goal column."""
|
|
486
|
+
if self._word_wrap:
|
|
487
|
+
return self._at_row(self._row_of(self._cursor)[0] + lines, self._goal)
|
|
488
|
+
line = max(0, min(self._cursor[0] + lines, len(self.lines) - 1))
|
|
489
|
+
return line, self._index_at(line, self._goal)
|
|
490
|
+
|
|
491
|
+
def _word(self, direction: int) -> Position:
|
|
492
|
+
"""Where Ctrl+Left (-1) or Ctrl+Right (1) goes, onto the line above
|
|
493
|
+
or below from the start or end of a line."""
|
|
494
|
+
line, index = self._cursor
|
|
495
|
+
text = self.lines[line]
|
|
496
|
+
if direction < 0:
|
|
497
|
+
if index == 0 and line > 0:
|
|
498
|
+
return line - 1, len(self.lines[line - 1])
|
|
499
|
+
return line, word_left(text, index)
|
|
500
|
+
if index == len(text) and line < len(self.lines) - 1:
|
|
501
|
+
return line + 1, 0
|
|
502
|
+
return line, word_right(text, index)
|
|
503
|
+
|
|
504
|
+
# Editing
|
|
505
|
+
|
|
506
|
+
def insert(self, text: str) -> bool:
|
|
507
|
+
"""Put text in place of the selection, or at the cursor, as typing
|
|
508
|
+
and pasting do; the text may contain line breaks and tabs. Returns
|
|
509
|
+
``False`` if nothing changed."""
|
|
510
|
+
text = self._clean(text)
|
|
511
|
+
start, end = self.selection or (self._cursor, self._cursor)
|
|
512
|
+
if not text and start == end:
|
|
513
|
+
return False
|
|
514
|
+
self._replace(start, end, text)
|
|
515
|
+
return True
|
|
516
|
+
|
|
517
|
+
def _type(self, text: str) -> None:
|
|
518
|
+
"""A character typed: as ``insert``, but a run of typing (to the end
|
|
519
|
+
of a word) is undone in one go."""
|
|
520
|
+
typing_on = (
|
|
521
|
+
self.selection is None
|
|
522
|
+
and self._typing_at == self._cursor
|
|
523
|
+
and not text.isspace()
|
|
524
|
+
and self._history.last is not None
|
|
525
|
+
)
|
|
526
|
+
if typing_on:
|
|
527
|
+
last = self._history.last
|
|
528
|
+
assert last is not None
|
|
529
|
+
self._replace(self._cursor, self._cursor, text, record=False)
|
|
530
|
+
self._history.replace_last(last._replace(inserted=last.inserted + text))
|
|
531
|
+
else:
|
|
532
|
+
self.insert(text)
|
|
533
|
+
self._typing_at = self._cursor
|
|
534
|
+
|
|
535
|
+
def _replace(
|
|
536
|
+
self, start: Position, end: Position, text: str, *, record: bool = True
|
|
537
|
+
) -> None:
|
|
538
|
+
"""Replace the text from ``start`` to ``end`` with ``text``, leave
|
|
539
|
+
the cursor after it, remember it for undoing (unless ``record`` is
|
|
540
|
+
off), and tell whoever is listening."""
|
|
541
|
+
if record:
|
|
542
|
+
change = _Change(
|
|
543
|
+
start, self._between(start, end), text, self._cursor, self._anchor
|
|
544
|
+
)
|
|
545
|
+
self._history.push(change, self.undo_limit)
|
|
546
|
+
self._typing_at = None
|
|
547
|
+
before = self.lines[start[0]][: start[1]]
|
|
548
|
+
after = self.lines[end[0]][end[1] :]
|
|
549
|
+
parts = text.split("\n")
|
|
550
|
+
new = [before + parts[0], *parts[1:]]
|
|
551
|
+
self._cursor = (start[0] + len(new) - 1, len(new[-1]))
|
|
552
|
+
new[-1] += after
|
|
553
|
+
self._recount(self.lines[start[0] : end[0] + 1], new)
|
|
554
|
+
self.lines[start[0] : end[0] + 1] = new
|
|
555
|
+
self._rewrap(start[0], end[0] + 1 - start[0], new)
|
|
556
|
+
self._anchor = None
|
|
557
|
+
self._goal = self._goal_of(self._cursor)
|
|
558
|
+
self.fit()
|
|
559
|
+
self._reveal()
|
|
560
|
+
if self.on_change is not None:
|
|
561
|
+
self.on_change(self)
|
|
562
|
+
|
|
563
|
+
@property
|
|
564
|
+
def can_undo(self) -> bool:
|
|
565
|
+
"""Whether there is a change to undo."""
|
|
566
|
+
return len(self._history) > 0
|
|
567
|
+
|
|
568
|
+
def popup_menu(self, shortcuts: Shortcuts) -> Menu | None:
|
|
569
|
+
"""The area's ``context_menu``, or otherwise Undo, Cut, Copy, Paste and
|
|
570
|
+
Select all, each greyed out when it would do nothing."""
|
|
571
|
+
if self.context_menu is not None:
|
|
572
|
+
return self.context_menu
|
|
573
|
+
return edit_menu(self, shortcuts)
|
|
574
|
+
|
|
575
|
+
def undo(self) -> bool:
|
|
576
|
+
"""Undo the latest change (Ctrl+Z), putting the cursor and selection
|
|
577
|
+
back as they were; ``False`` if there is nothing to undo."""
|
|
578
|
+
change = self._history.pop()
|
|
579
|
+
if change is None:
|
|
580
|
+
return False
|
|
581
|
+
self._history.undone(self._apply(change))
|
|
582
|
+
return True
|
|
583
|
+
|
|
584
|
+
def redo(self) -> bool:
|
|
585
|
+
"""Redo the latest change undone (Alt+Z, or Ctrl+Shift+Z where the
|
|
586
|
+
terminal sends it); ``False`` if there is nothing to redo."""
|
|
587
|
+
change = self._history.pop_redo()
|
|
588
|
+
if change is None:
|
|
589
|
+
return False
|
|
590
|
+
self._history.push(self._apply(change), self.undo_limit, redoing=True)
|
|
591
|
+
return True
|
|
592
|
+
|
|
593
|
+
def _apply(self, change: _Change) -> _Change:
|
|
594
|
+
"""Put ``change.removed`` back where ``change.inserted`` is, and the
|
|
595
|
+
cursor and selection as ``change`` has them. Returns the change
|
|
596
|
+
that reverses this, to redo (or undo again)."""
|
|
597
|
+
parts = change.inserted.split("\n")
|
|
598
|
+
if len(parts) == 1:
|
|
599
|
+
end = (change.start[0], change.start[1] + len(parts[0]))
|
|
600
|
+
else:
|
|
601
|
+
end = (change.start[0] + len(parts) - 1, len(parts[-1]))
|
|
602
|
+
reverse = _Change(
|
|
603
|
+
change.start, change.inserted, change.removed, self._cursor, self._anchor
|
|
604
|
+
)
|
|
605
|
+
self._replace(change.start, end, change.removed, record=False)
|
|
606
|
+
self._cursor, self._anchor = change.cursor, change.anchor
|
|
607
|
+
self._goal = self._goal_of(self._cursor)
|
|
608
|
+
self._reveal()
|
|
609
|
+
return reverse
|
|
610
|
+
|
|
611
|
+
def _back(self, position: Position) -> Position:
|
|
612
|
+
"""The position one character back, onto the previous line if need be."""
|
|
613
|
+
line, column = position
|
|
614
|
+
if column > 0:
|
|
615
|
+
return line, column - 1
|
|
616
|
+
if line > 0:
|
|
617
|
+
return line - 1, len(self.lines[line - 1])
|
|
618
|
+
return position
|
|
619
|
+
|
|
620
|
+
def _forward(self, position: Position) -> Position:
|
|
621
|
+
"""The position one character on, onto the next line if need be."""
|
|
622
|
+
line, column = position
|
|
623
|
+
if column < len(self.lines[line]):
|
|
624
|
+
return line, column + 1
|
|
625
|
+
if line < len(self.lines) - 1:
|
|
626
|
+
return line + 1, 0
|
|
627
|
+
return position
|
|
628
|
+
|
|
629
|
+
def delete_line(self) -> None:
|
|
630
|
+
"""Delete the line the cursor is on (Ctrl+Y)."""
|
|
631
|
+
line = self._cursor[0]
|
|
632
|
+
if len(self.lines) == 1:
|
|
633
|
+
self._replace((0, 0), (0, len(self.lines[0])), "")
|
|
634
|
+
elif line < len(self.lines) - 1:
|
|
635
|
+
self._replace((line, 0), (line + 1, 0), "")
|
|
636
|
+
else:
|
|
637
|
+
previous = len(self.lines[line - 1])
|
|
638
|
+
self._replace((line - 1, previous), (line, len(self.lines[line])), "")
|
|
639
|
+
|
|
640
|
+
# The clipboard
|
|
641
|
+
|
|
642
|
+
def copy(self) -> bool:
|
|
643
|
+
"""Copy the selection to the clipboard; ``False`` if nothing is."""
|
|
644
|
+
if self.selection is None:
|
|
645
|
+
return False
|
|
646
|
+
self.clipboard.copy_text(self.selected_text)
|
|
647
|
+
return True
|
|
648
|
+
|
|
649
|
+
def cut(self) -> bool:
|
|
650
|
+
"""Copy the selection to the clipboard and delete it."""
|
|
651
|
+
if not self.copy():
|
|
652
|
+
return False
|
|
653
|
+
return self.insert("")
|
|
654
|
+
|
|
655
|
+
def paste(self) -> bool:
|
|
656
|
+
"""Put the clipboard's text in place of the selection, or at the
|
|
657
|
+
cursor. Nothing happens unless the clipboard holds text."""
|
|
658
|
+
text = self.clipboard.text
|
|
659
|
+
return bool(text) and self.insert(text or "")
|
|
660
|
+
|
|
661
|
+
def handle_paste(self, text: str) -> bool:
|
|
662
|
+
"""Text pasted through the terminal is inserted as if typed."""
|
|
663
|
+
return self.insert(text)
|
|
664
|
+
|
|
665
|
+
# Layout
|
|
666
|
+
|
|
667
|
+
def on_add(self) -> None:
|
|
668
|
+
self.fit()
|
|
669
|
+
|
|
670
|
+
def follow_resize(self, before: tuple[int, int], after: tuple[int, int]) -> None:
|
|
671
|
+
"""A text area always fits its window, whatever its ``grow`` is."""
|
|
672
|
+
del before, after
|
|
673
|
+
self.fit()
|
|
674
|
+
|
|
675
|
+
def fit(self) -> None:
|
|
676
|
+
"""Be at least as big as the rest of the window's interior, and as
|
|
677
|
+
big as the text (plus a column for the cursor after the end); with
|
|
678
|
+
word wrap, exactly as wide as the interior, and as tall as the rows."""
|
|
679
|
+
rect = self.rect
|
|
680
|
+
least_width, least_height = self._least
|
|
681
|
+
if self.window is not None:
|
|
682
|
+
interior = self.window.interior
|
|
683
|
+
least_width = max(least_width, interior.width - rect.column)
|
|
684
|
+
least_height = max(least_height, interior.height - rect.row)
|
|
685
|
+
if self.tab_size != self._widths_tab_size:
|
|
686
|
+
self._count_widths()
|
|
687
|
+
numbers = self.numbers_width
|
|
688
|
+
if self._word_wrap:
|
|
689
|
+
# A column for the cursor at the end, and the line numbers'.
|
|
690
|
+
limit = max(1, least_width - 1 - numbers)
|
|
691
|
+
if limit != self._wrap_limit or self.tab_size != self._wrap_tab_size:
|
|
692
|
+
self._wrap(limit)
|
|
693
|
+
height = max(least_height, self._row_count())
|
|
694
|
+
self.rect = Rect(rect.row, rect.column, height, least_width)
|
|
695
|
+
else:
|
|
696
|
+
widest = max(self._widths, default=0)
|
|
697
|
+
width = max(least_width, widest + 1 + numbers)
|
|
698
|
+
height = max(least_height, len(self.lines))
|
|
699
|
+
self.rect = Rect(rect.row, rect.column, height, width)
|
|
700
|
+
if self.window is not None:
|
|
701
|
+
self.window.scroll_by(0, 0) # back in range, if the text shrank
|
|
702
|
+
|
|
703
|
+
def _reveal(self) -> None:
|
|
704
|
+
"""Scroll the window to show the cursor."""
|
|
705
|
+
window = self.window
|
|
706
|
+
if window is not None:
|
|
707
|
+
row, column = self._row_of(self._cursor)
|
|
708
|
+
numbers = self.numbers_width
|
|
709
|
+
# The cursor's cell, and room for the line numbers left of it,
|
|
710
|
+
# since they stay on the left as the window scrolls sideways.
|
|
711
|
+
cell = Rect(
|
|
712
|
+
self.rect.row + row,
|
|
713
|
+
self._text_column + column - numbers,
|
|
714
|
+
1,
|
|
715
|
+
numbers + 1,
|
|
716
|
+
)
|
|
717
|
+
window.scroll_into_view(cell)
|
|
718
|
+
if cell.right <= window.interior.width:
|
|
719
|
+
# The cursor fits without scrolling sideways: show the
|
|
720
|
+
# lines from their start, as editors do.
|
|
721
|
+
window.scroll_to(window.scroll_row, 0)
|
|
722
|
+
|
|
723
|
+
def _page(self) -> int:
|
|
724
|
+
"""Lines a page moves: what the window shows, less one."""
|
|
725
|
+
if self.window is None:
|
|
726
|
+
return max(1, self.rect.height - 1)
|
|
727
|
+
return max(1, self.window.interior.height - 1)
|
|
728
|
+
|
|
729
|
+
# Drawing
|
|
730
|
+
|
|
731
|
+
def _visible(self) -> tuple[int, int, int, int]:
|
|
732
|
+
"""The rows and columns that can show: (first row, row count, first
|
|
733
|
+
column, column count), so a long text costs no more to draw than a
|
|
734
|
+
short one. Without word wrap, each row is a line."""
|
|
735
|
+
if self.window is None:
|
|
736
|
+
return 0, self._row_count(), 0, self.rect.width
|
|
737
|
+
interior = self.window.interior
|
|
738
|
+
return (
|
|
739
|
+
max(0, self.window.scroll_row - self.rect.row),
|
|
740
|
+
interior.height + 1,
|
|
741
|
+
max(0, self.window.scroll_column - self._text_column),
|
|
742
|
+
interior.width + 1,
|
|
743
|
+
)
|
|
744
|
+
|
|
745
|
+
def paint(self, canvas: Canvas, origin: Rect, focused: bool) -> None:
|
|
746
|
+
rect = self.rect
|
|
747
|
+
top, left = origin.row + rect.row, origin.column + self._text_column
|
|
748
|
+
canvas.fill(
|
|
749
|
+
origin.intersect(
|
|
750
|
+
Rect(top, origin.column + rect.column, rect.height, rect.width)
|
|
751
|
+
),
|
|
752
|
+
" ",
|
|
753
|
+
self.style,
|
|
754
|
+
)
|
|
755
|
+
first, count, column, width = self._visible()
|
|
756
|
+
selection = self.selection if focused else None
|
|
757
|
+
for row in range(first, min(self._row_count(), first + count)):
|
|
758
|
+
self._paint_row(canvas, origin, row, (column, width), selection)
|
|
759
|
+
numbers_end = self._paint_numbers(canvas, origin, first, count)
|
|
760
|
+
if focused:
|
|
761
|
+
row, at = self._row_of(self._cursor)
|
|
762
|
+
if left + at >= numbers_end: # not scrolled under the numbers
|
|
763
|
+
canvas.place_cursor(top + row, left + at)
|
|
764
|
+
|
|
765
|
+
def _numbers_column(self) -> int:
|
|
766
|
+
"""Where in the window's content the line numbers are: at the text
|
|
767
|
+
area's left, or the window's, whichever is further right."""
|
|
768
|
+
scrolled = self.window.scroll_column if self.window is not None else 0
|
|
769
|
+
return max(self.rect.column, scrolled)
|
|
770
|
+
|
|
771
|
+
def _paint_numbers(
|
|
772
|
+
self, canvas: Canvas, origin: Rect, first: int, count: int
|
|
773
|
+
) -> int:
|
|
774
|
+
"""Draw the line numbers of the rows that show, each by its line's
|
|
775
|
+
first row, and the bar beside them, all the text area's height.
|
|
776
|
+
Returns the screen column just after them."""
|
|
777
|
+
width = self.numbers_width
|
|
778
|
+
left = origin.column + self._numbers_column()
|
|
779
|
+
if not width:
|
|
780
|
+
return left
|
|
781
|
+
rows = self._row_count()
|
|
782
|
+
for row in range(first, min(self.rect.height, first + count)):
|
|
783
|
+
number = ""
|
|
784
|
+
if row < rows:
|
|
785
|
+
line, begin, _, _ = self._segment(row)
|
|
786
|
+
number = str(line + 1) if begin == 0 else ""
|
|
787
|
+
canvas.write(
|
|
788
|
+
origin.row + self.rect.row + row,
|
|
789
|
+
left,
|
|
790
|
+
number.rjust(width - 1) + NUMBERS_BAR,
|
|
791
|
+
self.number_style,
|
|
792
|
+
clip=origin,
|
|
793
|
+
)
|
|
794
|
+
return left + width
|
|
795
|
+
|
|
796
|
+
def _paint_row( # pylint: disable=too-many-arguments
|
|
797
|
+
self,
|
|
798
|
+
canvas: Canvas,
|
|
799
|
+
origin: Rect,
|
|
800
|
+
row: int,
|
|
801
|
+
view: tuple[int, int],
|
|
802
|
+
selection: tuple[Position, Position] | None,
|
|
803
|
+
) -> None:
|
|
804
|
+
"""Draw one row (a line, or with word wrap part of one): the columns
|
|
805
|
+
of it ``view`` says (the first, and how many), and its selection."""
|
|
806
|
+
number, begin, finish, last = self._segment(row)
|
|
807
|
+
shown = self._shown(self.lines[number])
|
|
808
|
+
# The columns of the line as drawn that this row shows.
|
|
809
|
+
offset = self._column_of((number, begin))
|
|
810
|
+
end = len(shown) if last else self._column_of((number, finish))
|
|
811
|
+
low = offset + view[0]
|
|
812
|
+
high = min(end, low + view[1])
|
|
813
|
+
canvas.write(
|
|
814
|
+
origin.row + self.rect.row + row,
|
|
815
|
+
origin.column + self._text_column + view[0],
|
|
816
|
+
shown[low:high],
|
|
817
|
+
self.style,
|
|
818
|
+
clip=origin,
|
|
819
|
+
)
|
|
820
|
+
if selection is not None:
|
|
821
|
+
# The line break's cell is on the line's last row.
|
|
822
|
+
self._paint_selected(
|
|
823
|
+
canvas,
|
|
824
|
+
origin,
|
|
825
|
+
(row, number, offset),
|
|
826
|
+
selection,
|
|
827
|
+
(low, low + view[1] if last else high),
|
|
828
|
+
)
|
|
829
|
+
|
|
830
|
+
def _paint_selected( # pylint: disable=too-many-arguments,too-many-locals
|
|
831
|
+
self,
|
|
832
|
+
canvas: Canvas,
|
|
833
|
+
origin: Rect,
|
|
834
|
+
place: tuple[int, int, int],
|
|
835
|
+
selection: tuple[Position, Position],
|
|
836
|
+
view: tuple[int, int],
|
|
837
|
+
) -> None:
|
|
838
|
+
"""Highlight the selected part of one row, and a cell for its line's
|
|
839
|
+
break when the selection goes on past it. ``place`` is the row, its
|
|
840
|
+
line, and the column of the line as drawn that the row starts at;
|
|
841
|
+
``view`` the columns of the line it shows, from and to."""
|
|
842
|
+
row, number, offset = place
|
|
843
|
+
(first, start), (last, end) = selection
|
|
844
|
+
if not first <= number <= last:
|
|
845
|
+
return
|
|
846
|
+
shown = self._shown(self.lines[number]) + " " # the line break, as a cell
|
|
847
|
+
low = self._column_of((number, start)) if number == first else 0
|
|
848
|
+
high = self._column_of((number, end)) if number == last else len(shown)
|
|
849
|
+
low, high = max(low, view[0]), min(high, view[1])
|
|
850
|
+
if low < high:
|
|
851
|
+
canvas.write(
|
|
852
|
+
origin.row + self.rect.row + row,
|
|
853
|
+
origin.column + self._text_column + low - offset,
|
|
854
|
+
shown[low:high],
|
|
855
|
+
self.selected_style,
|
|
856
|
+
clip=origin,
|
|
857
|
+
)
|
|
858
|
+
|
|
859
|
+
# Input
|
|
860
|
+
|
|
861
|
+
def key_help(self, shortcuts: Shortcuts) -> list[tuple[str, str]]:
|
|
862
|
+
window = self.window
|
|
863
|
+
if window is None or sum(w.can_focus() for w in window.widgets) < 2:
|
|
864
|
+
return []
|
|
865
|
+
key = shortcuts.hint("area_next_widget")
|
|
866
|
+
return [(key, "Next")] if key else []
|
|
867
|
+
|
|
868
|
+
def handle_key(self, key: str, shortcuts: Shortcuts) -> bool:
|
|
869
|
+
"""Edit the text. Tab types a tab, while Ctrl+Tab, Ctrl+Shift+Tab
|
|
870
|
+
(and the window's own focus keys other than Tab) and Escape are left
|
|
871
|
+
for the window."""
|
|
872
|
+
if shortcuts.matches("area_tab", key):
|
|
873
|
+
self._type("\t")
|
|
874
|
+
return True
|
|
875
|
+
leaving = (
|
|
876
|
+
"area_next_widget",
|
|
877
|
+
"area_previous_widget",
|
|
878
|
+
"next_widget",
|
|
879
|
+
"previous_widget",
|
|
880
|
+
"dialog_cancel",
|
|
881
|
+
)
|
|
882
|
+
if any(shortcuts.matches(action, key) for action in leaving):
|
|
883
|
+
return False
|
|
884
|
+
return (
|
|
885
|
+
self._movement_key(key, shortcuts)
|
|
886
|
+
or self._command_key(key, shortcuts)
|
|
887
|
+
or self._editing_key(key, shortcuts)
|
|
888
|
+
)
|
|
889
|
+
|
|
890
|
+
def _movement_key(self, key: str, shortcuts: Shortcuts) -> bool:
|
|
891
|
+
line = self._cursor[0]
|
|
892
|
+
selection = self.selection
|
|
893
|
+
last = len(self.lines) - 1
|
|
894
|
+
page = self._page()
|
|
895
|
+
left = selection[0] if selection else self._back(self._cursor)
|
|
896
|
+
right = selection[1] if selection else self._forward(self._cursor)
|
|
897
|
+
moves: dict[str, tuple[Position, bool, bool]] = {
|
|
898
|
+
# action: (where to, extend the selection, keep the goal column)
|
|
899
|
+
"text_left": (left, False, False),
|
|
900
|
+
"text_right": (right, False, False),
|
|
901
|
+
"text_word_left": (self._word(-1), False, False),
|
|
902
|
+
"text_word_right": (self._word(1), False, False),
|
|
903
|
+
"text_home": ((line, 0), False, False),
|
|
904
|
+
"text_end": ((line, len(self.lines[line])), False, False),
|
|
905
|
+
"area_up": (self._vertical(-1), False, True),
|
|
906
|
+
"area_down": (self._vertical(1), False, True),
|
|
907
|
+
"area_page_up": (self._vertical(-page), False, True),
|
|
908
|
+
"area_page_down": (self._vertical(page), False, True),
|
|
909
|
+
"area_top": ((0, 0), False, False),
|
|
910
|
+
"area_bottom": ((last, len(self.lines[last])), False, False),
|
|
911
|
+
"text_select_left": (self._back(self._cursor), True, False),
|
|
912
|
+
"text_select_right": (self._forward(self._cursor), True, False),
|
|
913
|
+
"text_select_word_left": (self._word(-1), True, False),
|
|
914
|
+
"text_select_word_right": (self._word(1), True, False),
|
|
915
|
+
"text_select_home": ((line, 0), True, False),
|
|
916
|
+
"text_select_end": ((line, len(self.lines[line])), True, False),
|
|
917
|
+
"area_select_up": (self._vertical(-1), True, True),
|
|
918
|
+
"area_select_down": (self._vertical(1), True, True),
|
|
919
|
+
}
|
|
920
|
+
for action, (position, extend, keep_goal) in moves.items():
|
|
921
|
+
if shortcuts.matches(action, key):
|
|
922
|
+
self._move(position, extend, keep_goal)
|
|
923
|
+
return True
|
|
924
|
+
return False
|
|
925
|
+
|
|
926
|
+
def _command_key(self, key: str, shortcuts: Shortcuts) -> bool:
|
|
927
|
+
commands: dict[str, Callable[[], object]] = {
|
|
928
|
+
"text_cut": self.cut,
|
|
929
|
+
"text_copy": self.copy,
|
|
930
|
+
"text_paste": self.paste,
|
|
931
|
+
"text_undo": self.undo,
|
|
932
|
+
"text_redo": self.redo,
|
|
933
|
+
"text_select_all": self.select_all,
|
|
934
|
+
"area_newline": lambda: self.insert("\n"),
|
|
935
|
+
"area_delete_line": self.delete_line,
|
|
936
|
+
}
|
|
937
|
+
for action, command in commands.items():
|
|
938
|
+
if shortcuts.matches(action, key):
|
|
939
|
+
command()
|
|
940
|
+
return True
|
|
941
|
+
return False
|
|
942
|
+
|
|
943
|
+
def _editing_key(self, key: str, shortcuts: Shortcuts) -> bool:
|
|
944
|
+
selection = self.selection
|
|
945
|
+
if shortcuts.matches("text_backspace", key):
|
|
946
|
+
if selection is not None:
|
|
947
|
+
self.insert("")
|
|
948
|
+
elif (back := self._back(self._cursor)) != self._cursor:
|
|
949
|
+
self._replace(back, self._cursor, "")
|
|
950
|
+
return True
|
|
951
|
+
if shortcuts.matches("text_delete", key):
|
|
952
|
+
if selection is not None:
|
|
953
|
+
self.insert("")
|
|
954
|
+
elif (forward := self._forward(self._cursor)) != self._cursor:
|
|
955
|
+
self._replace(self._cursor, forward, "")
|
|
956
|
+
return True
|
|
957
|
+
if len(key) == 1 and key.isprintable():
|
|
958
|
+
self._type(key) # in place of the selection, if there is one
|
|
959
|
+
return True
|
|
960
|
+
return False
|
|
961
|
+
|
|
962
|
+
def _position_at(self, row: int, column: int) -> Position:
|
|
963
|
+
"""The text position for a place within the text area; on the line
|
|
964
|
+
numbers, the start of the row (or of what shows of it)."""
|
|
965
|
+
column = max(column, self._numbers_column() + self.numbers_width)
|
|
966
|
+
column -= self._text_column
|
|
967
|
+
if self._word_wrap:
|
|
968
|
+
return self._at_row(row - self.rect.row, column)
|
|
969
|
+
line = self._clamp((row - self.rect.row, 0))[0]
|
|
970
|
+
return line, self._index_at(line, column)
|
|
971
|
+
|
|
972
|
+
def handle_mouse(self, event: MouseEvent, row: int, column: int) -> bool:
|
|
973
|
+
"""A click puts the cursor there, Shift with a click selects to
|
|
974
|
+
there, a drag selects, and a double-click selects the line. The wheel
|
|
975
|
+
is left for the window, which scrolls."""
|
|
976
|
+
if event.button != "left":
|
|
977
|
+
return False
|
|
978
|
+
position = self._position_at(row, column)
|
|
979
|
+
if event.action == "press":
|
|
980
|
+
if event.clicks >= 2:
|
|
981
|
+
self.select_line(position[0])
|
|
982
|
+
else:
|
|
983
|
+
self._move(position, extend=event.shift)
|
|
984
|
+
if self._anchor is None:
|
|
985
|
+
self._anchor = self._cursor # where a drag selects from
|
|
986
|
+
return True
|
|
987
|
+
if event.action == "drag":
|
|
988
|
+
self._move(position, extend=True)
|
|
989
|
+
return True
|
|
990
|
+
return event.action == "release"
|