forked from thonny/thonny
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtktextext.py
More file actions
1293 lines (1065 loc) · 45 KB
/
Copy pathtktextext.py
File metadata and controls
1293 lines (1065 loc) · 45 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
"""Extensions for tk.Text"""
import sys
import time
import tkinter
import tkinter as tk
from logging import getLogger
from tkinter import TclError
from tkinter import font as tkfont
from tkinter import ttk
logger = getLogger(__name__)
class TweakableText(tk.Text):
"""Allows intercepting Text commands at Tcl-level"""
def __init__(self, master=None, cnf={}, read_only=False, suppress_events=False, **kw):
super().__init__(master=master, cnf=cnf, **kw)
self._read_only = read_only
self._suppress_events = suppress_events
self._edit_count: int = 0
self._last_operation_time: float = time.time()
self._original_widget_name = self._w + "_orig"
self.tk.call("rename", self._w, self._original_widget_name)
self.tk.createcommand(self._w, self._dispatch_tk_operation)
self._tk_proxies = {}
self._original_insert = self._register_tk_proxy_function("insert", self.intercept_insert)
self._original_delete = self._register_tk_proxy_function("delete", self.intercept_delete)
self._original_mark = self._register_tk_proxy_function("mark", self.intercept_mark)
self.bind("<Control-Tab>", self._redirect_ctrl_tab, False)
def _register_tk_proxy_function(self, operation, function):
self._tk_proxies[operation] = function
setattr(self, operation, function)
def original_function(*args):
self.tk.call((self._original_widget_name, operation) + args)
return original_function
def _dispatch_tk_operation(self, operation, *args):
f = self._tk_proxies.get(operation)
try:
if f:
return f(*args)
else:
return self.tk.call((self._original_widget_name, operation) + args)
except TclError as e:
# Some Tk internal actions (eg. paste and cut) can cause this error
if (
str(e).lower() == '''text doesn't contain any characters tagged with "sel"'''
and operation in ["delete", "index", "get"]
and args in [("sel.first", "sel.last"), ("sel.first",)]
):
pass
# Don't worry about hitting ends of undo/redo stacks
elif (
operation == "edit"
and args in [("undo",), ("redo",)]
and str(e).lower() == "nothing to " + args[0]
):
pass
else:
logger.exception(
"[_dispatch_tk_operation] operation: " + operation + ", args:" + repr(args)
)
# traceback.print_exc()
return "" # Taken from idlelib.WidgetRedirector
except Exception as e:
# Need to catch to avoid the crash?
logger.exception("Exception in _dispatch_tk_operation")
from tkinter import messagebox
messagebox.showerror(
"Internal error",
"Error in _dispatch_tk_operation\n" + str(e),
parent=tkinter._default_root,
)
def set_read_only(self, value):
self._read_only = value
def is_read_only(self):
return self._read_only
def get_edit_count(self) -> int:
return self._edit_count
def get_last_operation_time(self) -> float:
"""Returns the timestamp of the widget creation or time or the last time
the text or cursor location was changed"""
return self._last_operation_time
def set_content(self, chars):
self.direct_delete("1.0", tk.END)
self.direct_insert("1.0", chars)
def set_insertwidth(self, new_width):
"""Change cursor width
NB! Need to be careful with setting text["insertwidth"]!
My first straightforward solution caused unexplainable
infinite loop of insertions and deletions in the text
(Repro: insert a line and a word, select that word and then do Ctrl-Z).
This solution seems safe but be careful!
"""
if self._suppress_events:
return
if self["insertwidth"] != new_width:
old_suppress = self._suppress_events
try:
self._suppress_events = True
self.config(insertwidth=new_width)
finally:
self._suppress_events = old_suppress
def intercept_mark(self, *args):
self.direct_mark(*args)
def intercept_insert(self, index, chars, tags=None, **kw):
assert isinstance(chars, str)
if chars >= "\uf704" and chars <= "\uf70d": # Function keys F1..F10 in Mac cause these
pass
elif self.is_read_only():
self.bell()
else:
self.direct_insert(index, chars, tags, **kw)
def intercept_delete(self, index1, index2=None, **kw):
if index1 == "sel.first" and index2 == "sel.last" and not self.has_selection():
return
if self.is_read_only():
self.bell()
elif self._is_erroneous_delete(index1, index2):
pass
else:
self.direct_delete(index1, index2, **kw)
def _is_erroneous_delete(self, index1, index2):
"""Paste can cause deletes where index1 is sel.start but text has no selection. This would cause errors"""
return index1.startswith("sel.") and not self.has_selection()
def direct_mark(self, *args):
self._original_mark(*args)
self._last_operation_time = time.time()
if args[:2] == ("set", "insert") and not self._suppress_events:
self.event_generate("<<CursorMove>>")
def index_sel_first(self):
# Tk will give error without this check
if self.tag_ranges("sel"):
return self.index("sel.first")
else:
return None
def index_sel_last(self):
if self.tag_ranges("sel"):
return self.index("sel.last")
else:
return None
def has_selection(self):
return len(self.tag_ranges("sel")) > 0
def get_selection_indices(self):
# If a selection is defined in the text widget, return (start,
# end) as Tkinter text indices, otherwise return (None, None)
if self.has_selection():
return self.index("sel.first"), self.index("sel.last")
else:
return None, None
def direct_insert(self, index, chars, tags=None, **kw):
self._original_insert(index, chars, tags, **kw)
self._edit_count += 1
self._last_operation_time = time.time()
if not self._suppress_events:
self.event_generate("<<TextChange>>")
def direct_delete(self, index1, index2=None, **kw):
self._original_delete(index1, index2, **kw)
self._edit_count += 1
self._last_operation_time = time.time()
if not self._suppress_events:
self.event_generate("<<TextChange>>")
def _redirect_ctrl_tab(self, event):
self.winfo_toplevel().event_generate("<<ControlTabInText>>", state=event.state)
return "break"
class EnhancedText(TweakableText):
"""Text widget with extra navigation and editing aids.
Provides more comfortable deletion, indentation and deindentation,
and undo handling. Not specific to Python code.
Most of the code is adapted from idlelib.EditorWindow.
"""
def __init__(
self,
master,
indent_width: int = 4,
tab_width: int = 4,
style="Text",
tag_current_line=False,
cnf={},
**kw,
):
# Parent class shouldn't autoseparate
# TODO: take client provided autoseparators value into account
kw["autoseparators"] = False
self._style = style
self._original_options = kw.copy()
super().__init__(master=master, cnf=cnf, **kw)
self.tab_width = tab_width
self.indent_width = indent_width
self._last_event_kind = None
self._last_key_time = None
self._bind_keypad()
self._bind_editing_aids()
self._bind_movement_aids()
self._bind_selection_aids()
self._bind_undo_aids()
self._bind_mouse_aids()
self._ui_theme_change_binding = self.bind(
"<<ThemeChanged>>", self._reload_theme_options, True
)
self._initial_configuration = self.configure()
self._regular_insertwidth = self["insertwidth"]
self._reload_theme_options()
self._should_tag_current_line = tag_current_line
if tag_current_line:
self.bind("<<CursorMove>>", self._tag_current_line, True)
self.bind("<<TextChange>>", self._tag_current_line, True)
self._tag_current_line()
def should_indent_with_tabs(self):
return True
def _bind_mouse_aids(self):
if _running_on_mac():
self.bind("<Button-2>", self.on_secondary_click)
self.bind("<Control-Button-1>", self.on_secondary_click)
else:
self.bind("<Button-3>", self.on_secondary_click)
def _bind_editing_aids(self):
def if_not_readonly(fun):
def dispatch(event):
if not self.is_read_only():
return fun(event)
else:
return "break"
return dispatch
self.bind("<Control-BackSpace>", if_not_readonly(self.delete_word_left), True)
self.bind("<Control-Delete>", if_not_readonly(self.delete_word_right), True)
self.bind("<Control-d>", self._redirect_ctrld, True)
self.bind("<Control-t>", self._redirect_ctrlt, True)
self.bind("<Control-f>", self._redirect_ctrlf, True)
self.bind("<Shift-Control-space>", self._redirect_shift_control_space, True)
self.bind("<BackSpace>", if_not_readonly(self.perform_smart_backspace), True)
self.bind("<Return>", if_not_readonly(self.perform_return), True)
self.bind("<KP_Enter>", if_not_readonly(self.perform_return), True)
self.bind("<Tab>", if_not_readonly(self.perform_tab), True)
try:
# Is needed on eg. Ubuntu with Estonian keyboard
self.bind("<ISO_Left_Tab>", if_not_readonly(self.perform_tab), True)
except Exception:
pass
if sys.platform == "win32":
self.bind("<KeyPress>", self._insert_untypable_characters_on_windows, True)
def _bind_keypad(self):
"""Remap keypad movement events to non-keypad equivalents"""
# https://github.com/pystart/thonny/issues/1106
kmap = {
"<KP_Left>": "<Left>",
"<KP_Right>": "<Right>",
"<KP_Up>": "<Up>",
"<KP_Down>": "<Down>",
"<KP_Home>": "<Home>",
"<KP_End>": "<End>",
"<KP_Next>": "<Next>",
"<KP_Prior>": "<Prior>",
"<KP_Enter>": "<Return>",
}
for from_key in kmap:
def mfunc(event, key=from_key):
self.event_generate(kmap[key], **{"state": event.state})
return "break"
try:
self.bind(from_key, mfunc)
except TclError:
pass
def _bind_movement_aids(self):
self.bind("<Home>", self.perform_smart_home, True)
self.bind("<Left>", self.move_to_edge_if_selection(0), True)
self.bind("<Right>", self.move_to_edge_if_selection(1), True)
self.bind("<Next>", self.perform_page_down, True)
self.bind("<Prior>", self.perform_page_up, True)
def _bind_selection_aids(self):
self.bind("<Command-a>" if _running_on_mac() else "<Control-a>", self.select_all, True)
def _bind_undo_aids(self):
self.bind("<<Undo>>", self._on_undo, True)
self.bind("<<Redo>>", self._on_redo, True)
self.bind("<<Cut>>", self._on_cut, True)
self.bind("<<Copy>>", self._on_copy, True)
self.bind("<<Paste>>", self._on_paste, True)
self.bind("<FocusIn>", self._on_get_focus, True)
self.bind("<FocusOut>", self._on_lose_focus, True)
self.bind("<Key>", self._on_key_press, True)
self.bind("<1>", self._on_mouse_click, True)
self.bind("<2>", self._on_mouse_click, True)
self.bind("<3>", self._on_mouse_click, True)
if _running_on_x11() or _running_on_mac():
def custom_redo(event):
self.event_generate("<<Redo>>")
return "break"
self.bind("<Control-y>", custom_redo, True)
def _redirect_ctrld(self, event):
# I want to disable the deletion effect of Ctrl-D in the text but still
# keep the event for other purposes
self.event_generate("<<CtrlDInText>>")
return "break"
def _redirect_shift_control_space(self, event):
# I want to disable the effect of Shift-Control-space in the text but still
# keep the event for other purposes
self.event_generate("<<ShiftControlSpaceInText>>")
return "break"
def _redirect_ctrlt(self, event):
# I want to disable the swap effect of Ctrl-T in the text but still
# keep the event for other purposes
self.event_generate("<<CtrlTInText>>")
return "break"
def _redirect_ctrlf(self, event):
# I want to disable the Tk effect of Ctrl-T in the text but still
# keep the event for other purposes
self.event_generate("<<CtrlFInText>>")
return "break"
def tag_reset(self, tag_name):
empty_conf = {key: "" for key in self.tag_configure(tag_name)}
self.tag_configure(empty_conf)
def select_lines(self, first_line, last_line):
self.tag_remove("sel", "1.0", tk.END)
self.tag_add("sel", "%s.0" % first_line, "%s.end" % last_line)
def delete_word_left(self, event):
self.event_generate("<Meta-Delete>")
self.edit_separator()
return "break"
def delete_word_right(self, event):
self.event_generate("<Meta-d>")
self.edit_separator()
return "break"
def perform_smart_backspace(self, event):
self._log_keypress_for_undo(event)
text = self
first, last = self.get_selection_indices()
if first and last:
text.delete(first, last)
text.mark_set("insert", first)
return "break"
# Delete whitespace left, until hitting a real char or closest
# preceding virtual tab stop.
chars = text.get("insert linestart", "insert")
if chars == "":
if text.compare("insert", ">", "1.0"):
# easy: delete preceding newline
text.delete("insert-1c")
else:
text.bell() # at start of buffer
return "break"
if (
chars.strip() != ""
): # there are non-whitespace chars somewhere to the left of the cursor
# easy: delete preceding real char
text.delete("insert-1c")
self._log_keypress_for_undo(event)
return "break"
# Ick. It may require *inserting* spaces if we back up over a
# tab character! This is written to be clear, not fast.
have = len(chars.expandtabs(self.tab_width))
assert have > 0
want = ((have - 1) // self.indent_width) * self.indent_width
# Debug prompt is multilined....
# if self.context_use_ps1:
# last_line_of_prompt = sys.ps1.split('\n')[-1]
# else:
last_line_of_prompt = ""
ncharsdeleted = 0
while 1:
if chars == last_line_of_prompt:
break
chars = chars[:-1]
ncharsdeleted = ncharsdeleted + 1
have = len(chars.expandtabs(self.tab_width))
if have <= want or chars[-1] not in " \t":
break
text.delete("insert-%dc" % ncharsdeleted, "insert")
if have < want:
text.insert("insert", " " * (want - have))
return "break"
def perform_midline_tab(self, event=None):
"autocompleter can put its magic here"
# by default
return self.perform_dumb_tab(event)
def perform_dumb_tab(self, event=None):
self._log_keypress_for_undo(event)
self.insert("insert", "\t")
return "break"
def perform_smart_tab(self, event=None):
self._log_keypress_for_undo(event)
# if intraline selection:
# delete it
# elif multiline selection:
# do indent-region
# else:
# indent one level
first, last = self.get_selection_indices()
if first and last:
if index2line(first) != index2line(last):
return self.indent_region(event)
self.delete(first, last)
self.mark_set("insert", first)
prefix = self.get("insert linestart", "insert")
raw, effective = classifyws(prefix, self.tab_width)
if raw == len(prefix):
# only whitespace to the left
self._reindent_to(effective + self.indent_width)
else:
# tab to the next 'stop' within or to right of line's text:
if self.should_indent_with_tabs():
pad = "\t"
else:
effective = len(prefix.expandtabs(self.tab_width))
n = self.indent_width
pad = " " * (n - effective % n)
self.insert("insert", pad)
self.see("insert")
return "break"
def get_cursor_position(self):
return map(int, self.index("insert").split("."))
def get_line_count(self):
return list(map(int, self.index("end-1c").split(".")))[0]
def perform_return(self, event):
self.insert("insert", "\n")
return "break"
def perform_page_down(self, event):
# if last line is visible then go to last line
# (by default it doesn't move then)
try:
last_visible_idx = self.index("@0,%d" % self.winfo_height())
row, _ = map(int, last_visible_idx.split("."))
line_count = self.get_line_count()
if row == line_count or row == line_count - 1: # otherwise tk doesn't show last line
self.mark_set("insert", "end")
except Exception as e:
logger.exception("Could not perform page down", exc_info=e)
def perform_page_up(self, event):
# if first line is visible then go there
# (by default it doesn't move then)
try:
first_visible_idx = self.index("@0,0")
row, _ = map(int, first_visible_idx.split("."))
if row == 1:
self.mark_set("insert", "1.0")
except Exception as e:
logger.exception("Could not perform page up", exc_info=e)
def compute_smart_home_destination_index(self):
"""Is overridden in shell"""
line = self.get("insert linestart", "insert lineend")
for insertpt in range(len(line)):
if line[insertpt] not in (" ", "\t"):
break
else:
insertpt = len(line)
lineat = int(self.index("insert").split(".")[1])
if insertpt == lineat:
insertpt = 0
return "insert linestart+" + str(insertpt) + "c"
def perform_smart_home(self, event):
if (event.state & 4) != 0 and event.keysym == "Home":
# state&4==Control. If <Control-Home>, use the Tk binding.
return None
dest = self.compute_smart_home_destination_index()
if (event.state & 1) == 0:
# shift was not pressed
self.tag_remove("sel", "1.0", "end")
else:
if not self.index_sel_first():
# there was no previous selection
self.mark_set("my_anchor", "insert")
else:
if self.compare(self.index_sel_first(), "<", self.index("insert")):
self.mark_set("my_anchor", "sel.first") # extend back
else:
self.mark_set("my_anchor", "sel.last") # extend forward
first = self.index(dest)
last = self.index("my_anchor")
if self.compare(first, ">", last):
first, last = last, first
self.tag_remove("sel", "1.0", "end")
self.tag_add("sel", first, last)
self.mark_set("insert", dest)
self.see("insert")
return "break"
def move_to_edge_if_selection(self, edge_index):
"""Cursor move begins at start or end of selection
When a left/right cursor key is pressed create and return to Tkinter a
function which causes a cursor move from the associated edge of the
selection.
"""
def move_at_edge(event):
if (
self.has_selection() and (event.state & 5) == 0
): # no shift(==1) or control(==4) pressed
try:
self.mark_set("insert", ("sel.first+1c", "sel.last-1c")[edge_index])
except tk.TclError:
pass
return move_at_edge
def perform_tab(self, event=None):
self._log_keypress_for_undo(event)
if event.state & 0x0001: # shift is pressed (http://stackoverflow.com/q/32426250/261181)
return self.dedent_region(event)
else:
# check whether there are letters before cursor on this line
index = self.index("insert")
left_text = self.get(index + " linestart", index)
if left_text.strip() == "" or self.has_selection():
return self.perform_smart_tab(event)
else:
return self.perform_midline_tab(event)
def indent_region(self, event=None):
return self._change_indentation(True)
def dedent_region(self, event=None):
return self._change_indentation(False)
def _change_indentation(self, increase=True):
head, tail, chars, lines = self._get_region()
# Text widget plays tricks if selection ends on last line
# and content doesn't end with empty line,
text_last_line = index2line(self.index("end-1c"))
sel_last_line = index2line(tail)
if sel_last_line >= text_last_line:
while not self.get(head, "end").endswith("\n\n"):
self.insert("end", "\n")
for pos in range(len(lines)):
line = lines[pos]
if line:
raw, effective = classifyws(line, self.tab_width)
if increase:
effective = effective + self.indent_width
else:
effective = max(effective - self.indent_width, 0)
lines[pos] = self._make_blanks(effective) + line[raw:]
self._set_region(head, tail, chars, lines)
return "break"
def select_all(self, event):
self.tag_remove("sel", "1.0", tk.END)
self.tag_add("sel", "1.0", tk.END)
def set_read_only(self, value):
if value == self.is_read_only():
return
TweakableText.set_read_only(self, value)
self._reload_theme_options()
if self._should_tag_current_line:
self._tag_current_line()
def _reindent_to(self, column):
# Delete from beginning of line to insert point, then reinsert
# column logical (meaning use tabs if appropriate) spaces.
if self.compare("insert linestart", "!=", "insert"):
self.delete("insert linestart", "insert")
if column:
self.insert("insert", self._make_blanks(column))
def _get_region(self):
first, last = self.get_selection_indices()
if first and last:
head = self.index(first + " linestart")
tail = self.index(last + "-1c lineend +1c")
else:
head = self.index("insert linestart")
tail = self.index("insert lineend +1c")
chars = self.get(head, tail)
lines = chars.split("\n")
return head, tail, chars, lines
def _set_region(self, head, tail, chars, lines):
newchars = "\n".join(lines)
if newchars == chars:
self.bell()
return
self.tag_remove("sel", "1.0", "end")
self.mark_set("insert", head)
self.delete(head, tail)
self.insert(head, newchars)
self.tag_add("sel", head, "insert")
def _log_keypress_for_undo(self, e):
if e is None:
return
# NB! this may not execute if the event is cancelled in another handler
event_kind = self._get_event_kind(e)
if (
event_kind != self._last_event_kind
or e.char in ("\r", "\n", " ", "\t")
or e.keysym in ["Return", "KP_Enter"]
or time.time() - self._last_key_time > 2
):
self.edit_separator()
self._last_event_kind = event_kind
self._last_key_time = time.time()
def _get_event_kind(self, event):
if event.keysym in ("BackSpace", "Delete"):
return "delete"
elif event.char:
return "insert"
else:
# eg. e.keysym in ("Left", "Up", "Right", "Down", "Home", "End", "Prior", "Next"):
return "other_key"
def _make_blanks(self, n):
# Make string that displays as n leading blanks.
if self.should_indent_with_tabs():
ntabs, nspaces = divmod(n, self.tab_width)
return "\t" * ntabs + " " * nspaces
else:
return " " * n
def _on_undo(self, e):
self._last_event_kind = "undo"
def _on_redo(self, e):
self._last_event_kind = "redo"
def _on_cut(self, e):
self._last_event_kind = "cut"
self.edit_separator()
def _on_copy(self, e):
self._last_event_kind = "copy"
self.edit_separator()
def _on_paste(self, e):
if self.is_read_only():
return
try:
if self.has_selection():
self.direct_delete("sel.first", "sel.last")
except Exception:
pass
self._last_event_kind = "paste"
self.edit_separator()
self.see("insert")
self.after_idle(lambda: self.see("insert"))
def _on_get_focus(self, e):
self._last_event_kind = "get_focus"
self.edit_separator()
def _on_lose_focus(self, e):
self._last_event_kind = "lose_focus"
self.edit_separator()
def _on_key_press(self, e):
return self._log_keypress_for_undo(e)
def _on_mouse_click(self, event):
self.edit_separator()
def _tag_current_line(self, event=None):
self.tag_remove("current_line", "1.0", "end")
# Let's show current line only with readable text
# (this fits well with PyStart debugger,
# otherwise debugger focus box and current line interact in an ugly way)
if self._should_tag_current_line and not self.is_read_only():
# we may be on the same line as with prev event but tag needs extension
lineno = int(self.index("insert").split(".")[0])
self.tag_add("current_line", str(lineno) + ".0", str(lineno + 1) + ".0")
def on_secondary_click(self, event=None):
"Use this for invoking context menu"
self.focus_set()
if event:
self.mark_set("insert", "@%d,%d" % (event.x, event.y))
def _reload_theme_options(self, event=None):
style = ttk.Style()
states = []
if self.is_read_only():
states.append("readonly")
# Following crashes when a combobox is focused
# if self.focus_get() == self:
# states.append("focus")
if "background" not in self._initial_configuration:
background = style.lookup(self._style, "background", states)
if background:
self.configure(background=background)
if "foreground" not in self._initial_configuration:
foreground = style.lookup(self._style, "foreground", states)
if foreground:
self.configure(foreground=foreground)
self.configure(insertbackground=foreground)
def _insert_untypable_characters_on_windows(self, event):
if event.state == 131084: # AltGr or Ctrl+Alt
lang_id = get_keyboard_language()
char = _windows_altgr_chars_by_lang_id_and_keycode.get(lang_id, {}).get(
event.keycode, None
)
if char is not None:
self.insert("insert", char)
def destroy(self):
self.unbind("<<ThemeChanged>>", self._ui_theme_change_binding)
super().destroy()
def direct_insert(self, index, chars, tags=None, **kw):
super().direct_insert(index, chars, tags, **kw)
class TextFrame(tk.Frame):
"Decorates text with scrollbars, listens for theme changes"
def __init__(
self,
master,
indent_width: int = 4,
tab_width: int = 4,
text_class=EnhancedText,
horizontal_scrollbar=True,
vertical_scrollbar=True,
vertical_scrollbar_class=ttk.Scrollbar,
vertical_scrollbar_rowspan=1,
horizontal_scrollbar_class=ttk.Scrollbar,
borderwidth=0,
background=None,
relief="sunken",
**text_options,
):
self._vertical_scrollbar_rowspan = vertical_scrollbar_rowspan
tk.Frame.__init__(
self, master=master, borderwidth=borderwidth, relief=relief, background=background
)
final_text_options = {
"borderwidth": 0,
"insertwidth": 2,
"spacing1": 0,
"spacing3": 0,
"highlightthickness": 0,
"inactiveselectbackground": "gray",
"padx": 5,
"pady": 5,
}
final_text_options.update(text_options)
self.text = text_class(self, **final_text_options)
if vertical_scrollbar:
self._vbar = vertical_scrollbar_class(self, orient=tk.VERTICAL)
self._vbar["command"] = self._vertical_scroll
self.text["yscrollcommand"] = self._vertical_scrollbar_update
from pystart.ui_utils import check_create_aqua_scrollbar_stripe
self._vbar_stripe = check_create_aqua_scrollbar_stripe(self)
else:
self._vbar = None
self._vbar_stripe = None
if horizontal_scrollbar:
self._hbar = horizontal_scrollbar_class(self, orient=tk.HORIZONTAL)
self._hbar["command"] = self._horizontal_scroll
self.text["xscrollcommand"] = self._horizontal_scrollbar_update
else:
self._hbar = None
self.grid_main_widgets()
self.columnconfigure(1, weight=1)
self.rowconfigure(0, weight=1)
self._ui_theme_change_binding = self.bind(
"<<ThemeChanged>>", self._reload_theme_options, True
)
# TODO: add context menu?
self._reload_theme_options(None)
def grid_main_widgets(self):
self.text.grid(row=0, column=1, sticky=tk.NSEW)
if self._vbar:
self._vbar.grid(
row=0, column=2, sticky=tk.NSEW, rowspan=self._vertical_scrollbar_rowspan
)
if self._vbar_stripe:
self._vbar_stripe.grid(row=0, column=2, sticky="nse")
self._vbar_stripe.tkraise()
if self._hbar:
self._hbar.grid(row=1, column=0, sticky=tk.NSEW, columnspan=2)
def focus_set(self):
self.text.focus_set()
def _vertical_scrollbar_update(self, *args):
if not hasattr(self, "_vbar"):
return
self._vbar.set(*args)
self.text.event_generate("<<VerticalScroll>>")
def _horizontal_scrollbar_update(self, *args):
self._hbar.set(*args)
def _vertical_scroll(self, *args):
self.text.yview(*args)
self.text.event_generate("<<VerticalScroll>>")
def _horizontal_scroll(self, *args):
self.text.xview(*args)
def _reload_theme_options(self, event=None):
pass
def destroy(self):
self.unbind("<<ThemeChanged>>", self._ui_theme_change_binding)
super().destroy()
class EnhancedTextFrame(TextFrame):
"Adds line numbers and print margin"
def __init__(
self,
master,
indent_width: int = 4,
tab_width: int = 4,
line_numbers=False,
line_length_margin=0,
first_line_number=1,
text_class=EnhancedText,
horizontal_scrollbar=True,
vertical_scrollbar=True,
vertical_scrollbar_class=ttk.Scrollbar,
horizontal_scrollbar_class=ttk.Scrollbar,
borderwidth=0,
relief="sunken",
gutter_background="#e0e0e0",
gutter_foreground="#999999",
**text_options,
):
self._gutter = None
super().__init__(
master,
indent_width=indent_width,
tab_width=tab_width,
text_class=text_class,
horizontal_scrollbar=horizontal_scrollbar,
vertical_scrollbar=vertical_scrollbar,
vertical_scrollbar_class=vertical_scrollbar_class,
horizontal_scrollbar_class=horizontal_scrollbar_class,
borderwidth=borderwidth,
relief=relief,
**text_options,
)
self._recommended_line_length = line_length_margin
self._gutter = tk.Text(
self,
width=5,
padx=0,
pady=5,
highlightthickness=0,
bd=0,
takefocus=False,
font=self.text["font"],
background="#e0e0e0",
foreground=gutter_foreground,
selectbackground=gutter_background,
selectforeground=gutter_foreground,
cursor="arrow",
state="disabled",
undo=False,
wrap="none",
)
if "height" in text_options:
self._gutter.configure(height=text_options["height"])
self._gutter_is_gridded = False
self._gutter.bind("<Double-Button-1>", self.on_gutter_double_click, True)
self._gutter.bind("<ButtonRelease-1>", self.on_gutter_click, True)
self._gutter.bind("<Button-1>", self.on_gutter_click, True)
self._gutter.bind("<Button1-Motion>", self.on_gutter_motion, True)
self._gutter["yscrollcommand"] = self._gutter_scroll
# need tags for justifying and rmargin
self._gutter.tag_configure("content", justify="right", rmargin=3)
# gutter will be gridded later
assert first_line_number is not None
self._first_line_number = first_line_number
self.set_gutter_visibility(line_numbers)
margin_line_color = ttk.Style().lookup("Gutter", "background", default="LightGray")
self._margin_line = tk.Canvas(
self.text,
borderwidth=0,
width=1,
height=2000,
highlightthickness=0,
background=margin_line_color,
)
self.update_margin_line()
self.text.bind("<<TextChange>>", self._text_changed, True)
self.text.bind("<<CursorMove>>", self._cursor_moved, True)