ed_stc.py
70.7 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
###############################################################################
# Name: ed_stc.py #
# Purpose: Editra's styled editing buffer #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2008 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
This is the main component of the editor that manages all the information
of the on disk file that it represents in memory. It works with the StyleManager
and SyntaxManager to provide an editing pane that auto detects and configures
itself for type of file that is in buffer to do highlighting and other language
specific options such as commenting code.
@summary: Editra's main text buffer class
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: ed_stc.py 73712 2013-03-23 14:46:09Z CJP $"
__revision__ = "$Revision: 73712 $"
#-------------------------------------------------------------------------#
# Imports
import os
import wx, wx.stc
# Local Imports
import ed_event
import ed_glob
from profiler import Profile_Get as _PGET
from syntax import syntax
import util
import ed_basestc
import ed_marker
import ed_msg
import ed_mdlg
import ed_txt
from ed_keyh import KeyHandler, ViKeyHandler
import eclib
import ebmlib
import ed_thread
#-------------------------------------------------------------------------#
# Globals
_ = wx.GetTranslation
# EOL Constants
EDSTC_EOL_CR = ed_glob.EOL_MODE_CR
EDSTC_EOL_LF = ed_glob.EOL_MODE_LF
EDSTC_EOL_CRLF = ed_glob.EOL_MODE_CRLF
# Character sets
SPACECHARS = " \t\r\n"
NONSPACE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
OPERATORS = "./\?[]{}<>!@#$%^&*():=-+\"';,"
#-------------------------------------------------------------------------#
def jumpaction(func):
"""Decorator method to notify clients about jump actions"""
def WrapJump(*args, **kwargs):
"""Wrapper for capturing before/after pos of a jump action"""
try:
stc = args[0]
pos = stc.GetCurrentPos()
line = stc.GetCurrentLine()
func(*args, **kwargs)
cpos = stc.GetCurrentPos()
cline = stc.GetCurrentLine()
fname = stc.GetFileName()
mdata = dict(fname=fname,
prepos=pos, preline=line,
lnum=cline, pos=cpos)
tlw = stc.TopLevelParent
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_POS_JUMPED, mdata, tlw.Id)
except wx.PyDeadObjectError:
pass
WrapJump.__name__ = func.__name__
WrapJump.__doc__ = func.__doc__
return WrapJump
#-------------------------------------------------------------------------#
class EditraStc(ed_basestc.EditraBaseStc):
"""Defines a styled text control for editing text
@summary: Subclass of wx.stc.StyledTextCtrl and L{ed_style.StyleMgr}.
Manages the documents display and input.
"""
def __init__(self, parent, id_,
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, use_dt=True):
"""Initializes a control and sets the default objects for
Tracking events that occur in the control.
@param parent: Parent Window
@param id_: Control ID
@keyword pos: Control position
@keyword size: Control size
@keyword style: Control style bitmask
@keyword use_dt: whether to use a drop target or not
"""
super(EditraStc, self).__init__(parent, id_, pos, size, style)
self.SetModEventMask(wx.stc.STC_PERFORMED_UNDO | \
wx.stc.STC_PERFORMED_REDO | \
wx.stc.STC_MOD_DELETETEXT | \
wx.stc.STC_MOD_INSERTTEXT)
self.CmdKeyAssign(ord('-'), wx.stc.STC_SCMOD_CTRL, \
wx.stc.STC_CMD_ZOOMOUT)
self.CmdKeyAssign(ord('+'), wx.stc.STC_SCMOD_CTRL | \
wx.stc.STC_SCMOD_SHIFT, wx.stc.STC_CMD_ZOOMIN)
#---- Drop Target ----#
if use_dt and hasattr(parent, 'OnDrop'):
self.SetDropTarget(util.DropTargetFT(self, None, parent.OnDrop))
# Attributes
self.LOG = wx.GetApp().GetLog()
self._loading = None
self.key_handler = KeyHandler(self)
self._backup_done = True
self._bktimer = wx.Timer(self)
self._dwellsent = False
# Macro Attributes
self._macro = list()
self.recording = False
# Command/Settings Attributes
self._config = dict(autocomp=_PGET('AUTO_COMP'),
autoindent=_PGET('AUTO_INDENT'),
brackethl=_PGET('BRACKETHL'),
folding=_PGET('CODE_FOLD'),
highlight=_PGET('SYNTAX'),
autobkup=_PGET('AUTOBACKUP'))
# Set Default Styles used by all documents
self.Configure()
self.UpdateBaseStyles()
# Other Settings
self.SetMouseDwellTime(900)
self.UsePopUp(False)
#self.Bind(wx.stc.EVT_STC_MACRORECORD, self.OnRecordMacro)
self.Bind(wx.stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
self.Bind(wx.stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
self.Bind(wx.stc.EVT_STC_USERLISTSELECTION, self.OnUserListSel)
self.Bind(wx.stc.EVT_STC_DWELLSTART, self.OnDwellStart)
self.Bind(wx.stc.EVT_STC_DWELLEND, self.OnDwellEnd)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_CHAR, self.OnChar)
self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_TIMER, self.OnBackupTimer)
# Async file load events
self.Bind(ed_txt.EVT_FILE_LOAD, self.OnLoadProgress)
#---- End Init ----#
__name__ = u"EditraTextCtrl"
#---- Protected Member Functions ----#
def _BuildMacro(self):
"""Constructs a macro script from items in the macro
record list.
@status: in limbo
"""
if not len(self._macro):
return
# Get command mappings
cmds = list()
for x in dir(wx.stc):
if x.startswith('STC_CMD_'):
cmds.append(x)
cmdvals = [getattr(wx.stc, x) for x in cmds]
cmds = [x.replace('STC_CMD_', u'') for x in cmds]
# Get the commands names used in the macro
named = list()
for x in self._macro:
if x[0] in cmdvals:
named.append(cmds[cmdvals.index(x[0])])
code = list()
stc_dict = wx.stc.StyledTextCtrl.__dict__
for cmd in named:
for attr in stc_dict:
if attr.upper() == cmd:
code.append(attr)
break
code_txt = u''
for fun in code:
code_txt += " ctrl.%s()\n" % fun
code_txt += " print \"Executed\"" #TEST
code_txt = "def macro(ctrl):\n" + code_txt
self.Parent.NewPage()
self.Parent.GetCurrentPage().SetText(code_txt)
self.Parent.GetCurrentPage().FindLexer('py')
# code = compile(code_txt, self.__module__, 'exec')
# exec code in self.__dict__ # Inject new code into this namespace
def _MacHandleKey(self, k_code, shift_down, alt_down, ctrl_down, cmd_down):
"""Handler for mac specific actions"""
handled = False
if alt_down:
return handled
if cmd_down and not ctrl_down:
# Jump line/start end are special on OSX and need to be
# white space aware.
line = self.GetCurrentLine()
if k_code == wx.WXK_RIGHT:
handled = True
pos = self.GetLineStartPosition(line)
# end pos is absolute end which includes NUL term
lpos = self.GetLineEndPosition(line) - 1
endsSpace = False
if lpos > 0:
endsSpace = unichr(self.GetCharAt(lpos)).isspace()
if not shift_down:
self.LineEnd()
if endsSpace:
self.WordLeftEnd()
else:
self.LineEndExtend()
if endsSpace:
self.WordLeftEndExtend()
elif k_code == wx.WXK_LEFT:
handled = True
cpos = self.GetCurrentPos()
self.GotoIndentPos(line)
if shift_down:
self.SetSelection(cpos, self.CurrentPos)
return handled
#---- Public Member Functions ----#
def AddBookmark(self, line=-1):
"""Add a bookmark and return its handle
Sends notifications for bookmark added
@keyword line: if < 0 bookmark will be added to current line
"""
rval = self.AddMarker(ed_marker.Bookmark(), line)
mdata = dict(stc=self, added=True, line=line, handle=rval)
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_BOOKMARK, mdata)
return rval
def RemoveBookmark(self, line):
"""Remove the book mark from the given line
Sends notifications for bookmark removal.
@param line: int
"""
self.RemoveMarker(ed_marker.Bookmark(), line)
mdata = dict(stc=self, added=False, line=line)
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_BOOKMARK, mdata)
def RemoveAllBookmarks(self):
"""Remove all the bookmarks in the buffer
Sends notifications for bookmark removal.
"""
self.RemoveAllMarkers(ed_marker.Bookmark())
mdata = dict(stc=self, added=False, line=-1)
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_BOOKMARK, mdata)
def PlayMacro(self):
"""Send the list of built up macro messages to the editor
to be played back.
@postcondition: the macro of this control has been played back
"""
self.BeginUndoAction()
for msg in self._macro:
if msg[0] == 2170:
self.AddText(msg[2])
elif msg[0] == 2001:
self.AddText(self.GetEOLChar() + u' ' * (msg[1] - 1))
else:
self.SendMsg(msg[0], msg[1], msg[2])
self.EndUndoAction()
#---- Begin Function Definitions ----#
def Bookmark(self, action):
"""Handles bookmark actions
@param action: An event ID that describes what is to be done
@return: None
"""
lnum = self.GetCurrentLine()
mark = -1
if action == ed_glob.ID_ADD_BM:
if self.MarkerGet(lnum):
self.RemoveBookmark(lnum)
else:
self.AddBookmark(lnum)
elif action == ed_glob.ID_DEL_ALL_BM:
self.RemoveAllBookmarks()
elif action == ed_glob.ID_NEXT_MARK:
if self.MarkerGet(lnum):
lnum += 1
mark = self.MarkerNext(lnum, 1)
if mark == -1:
mark = self.MarkerNext(0, 1)
elif action == ed_glob.ID_PRE_MARK:
if self.MarkerGet(lnum):
lnum -= 1
mark = self.MarkerPrevious(lnum, 1)
if mark == -1:
mark = self.MarkerPrevious(self.GetLineCount(), 1)
if mark != -1:
self.GotoLine(mark)
# TODO: DO NOT use these methods anywhere new they will be removed soon
def ShowCommandBar(self):
"""Open the command bar"""
self.TopLevelParent.GetEditPane().ShowCommandControl(ed_glob.ID_COMMAND)
def ShowFindBar(self):
"""Open the quick-find bar"""
self.TopLevelParent.GetEditPane().ShowCommandControl(ed_glob.ID_QUICK_FIND)
# END TODO
def GetBookmarks(self):
"""Gets a list of all lines containing bookmarks
@return: list of line numbers
"""
MarkIsSet = ed_marker.Bookmark.IsSet
return [line for line in range(self.GetLineCount())
if MarkIsSet(self, line)]
def DoBraceHighlight(self):
"""Perform a brace matching highlight
@note: intended for internal use only
"""
brace_at_caret, brace_opposite = self.GetBracePair()
# CallAfter necessary to reduce CG warnings on Mac
if brace_at_caret != -1 and brace_opposite == -1:
wx.CallAfter(self.BraceBadLight, brace_at_caret)
else:
wx.CallAfter(self.BraceHighlight, brace_at_caret, brace_opposite)
def GetBracePair(self, pos=-1):
"""Get a tuple of the positions in the buffer where the brace at the
current caret position and its match are. if a brace doesn't have a
match it will return -1 for the missing brace.
@keyword pos: -1 to use current cursor pos, else use to specify brace pos
@return: tuple (brace_at_caret, brace_opposite)
"""
brace_at_caret = -1
brace_opposite = -1
char_before = None
if pos < 0:
# use current position
caret_pos = self.GetCurrentPos()
else:
caret_pos = pos
if caret_pos > 0:
char_before = self.GetCharAt(caret_pos - 1)
# check before
if char_before and unichr(char_before) in "[]{}()<>":
brace_at_caret = caret_pos - 1
# check after
if brace_at_caret < 0:
char_after = self.GetCharAt(caret_pos)
if char_after and chr(char_after) in "[]{}()<>":
brace_at_caret = caret_pos
if brace_at_caret >= 0:
brace_opposite = self.BraceMatch(brace_at_caret)
return (brace_at_caret, brace_opposite)
def Configure(self):
"""Configures the editors settings by using profile values
@postcondition: all profile dependent attributes are configured
"""
# self.SetControlCharSymbol(172)
self.SetWrapMode(_PGET('WRAP', 'bool'))
self.SetViewWhiteSpace(_PGET('SHOW_WS', 'bool'))
self.SetUseAntiAliasing(_PGET('AALIASING'))
self.SetUseTabs(_PGET('USETABS'))
self.SetBackSpaceUnIndents(_PGET('BSUNINDENT'))
self.SetCaretLineVisible(_PGET('HLCARETLINE'))
self.SetIndent(_PGET('INDENTWIDTH', 'int'))
self.SetTabWidth(_PGET('TABWIDTH', 'int'))
# self.SetTabIndents(True) # Add option for this too?
self.SetIndentationGuides(_PGET('GUIDES'))
self.SetEOLMode(_PGET('EOL_MODE'))
self.SetViewEOL(_PGET('SHOW_EOL'))
self.SetAutoComplete(_PGET('AUTO_COMP'))
self.FoldingOnOff(_PGET('CODE_FOLD'))
self.ToggleAutoIndent(_PGET('AUTO_INDENT'))
self.ToggleBracketHL(_PGET('BRACKETHL'))
self.ToggleLineNumbers(_PGET('SHOW_LN'))
self.SetViEmulationMode(_PGET('VI_EMU'), _PGET('VI_NORMAL_DEFAULT'))
self.SetViewEdgeGuide(_PGET('SHOW_EDGE'))
self.EnableAutoBackup(_PGET('AUTOBACKUP'))
self.SetEndAtLastLine(not _PGET('VIEWVERTSPACE', default=False))
self.SetCaretWidth(_PGET('CARETWIDTH', default=1))
def ConvertCase(self, upper=False):
"""Converts the case of the selected text to either all lower
case(default) or all upper case.
@keyword upper: Flag whether conversion is to upper case or not.
"""
sel = self.GetSelectedText()
if upper:
sel = sel.upper()
else:
sel = sel.lower()
self.ReplaceSelection(sel)
def EnableAutoBackup(self, enable):
"""Enable automatic backups
@param enable: bool
"""
if enable:
# TODO: make backup interval configurable
if not self._bktimer.IsRunning():
self._bktimer.Start(30000) # every 30 seconds
else:
if self._bktimer.IsRunning():
self._bktimer.Stop()
def InvertCase(self):
"""Invert the case of the selected text
@postcondition: all text in selection has case inverted
"""
text = self.GetSelectedText()
if len(text):
self.BeginUndoAction()
self.ReplaceSelection(text.swapcase())
self.EndUndoAction()
def GetAutoIndent(self):
"""Returns whether auto-indent is being used
@return: whether autoindent is active or not
"""
return self._config['autoindent']
def GetLineStartPosition(self, line):
"""Get the starting position of the given line
@param line: int
@return: int
"""
if line > 0:
spos = self.GetLineEndPosition(line-1)
if self.GetLine(line).endswith("\r\n"):
spos += 2
else:
spos += 1
else:
spos = 0
return spos
def GetLastVisibleLine(self):
"""Return what the last visible line is
@return: int
"""
return self.GetFirstVisibleLine() + self.LinesOnScreen() - 1
def GetMiddleVisibleLine(self):
"""Return the number of the line that is in the middle of the display
@return: int
"""
fline = self.GetFirstVisibleLine()
if self.LinesOnScreen() < self.GetLineCount():
mid = (fline + (self.LinesOnScreen() / 2))
else:
mid = (fline + (self.GetLineCount() / 2))
return mid
def GotoBraceMatch(self):
"""Jump the caret to the brace opposite of the one the caret is
currently at. If there is no match or the caret currently is not next
to a brace no action is taken.
@return: bool
"""
cbrace, brace_opposite = self.GetBracePair()
if -1 in (cbrace, brace_opposite):
return False
else:
self.GotoPos(brace_opposite)
return True
def GotoColumn(self, column):
"""Move caret to column of current line
@param column: Column to move to
"""
cline = self.GetCurrentLineNum()
lstart = self.PositionFromLine(cline)
lend = self.GetLineEndPosition(cline)
linelen = lend - lstart
if column > linelen:
column = linelen
self.GotoPos(lstart + column)
@jumpaction
def GotoLine(self, line):
"""Move caret to beginning given line number
@param line: line to go to (int)
"""
if line > self.GetLineCount():
line = self.GetLineCount()
elif line < 0:
line = 0
else:
pass
self.SetYCaretPolicy(wx.stc.STC_CARET_STRICT, 0)
super(EditraStc, self).GotoLine(line)
self.SetYCaretPolicy(wx.stc.STC_CARET_EVEN, 0)
self.PostPositionEvent()
@jumpaction
def GotoPos(self, pos):
"""Override StyledTextCtrl.GotoPos
@param pos: position in buffer to move caret to (int)
"""
super(EditraStc, self).GotoPos(pos)
self.PostPositionEvent()
def SetCaretPos(self, pos):
"""Set the caret position without posting jump events
@param pos: position to go to
"""
try:
super(EditraStc, self).GotoPos(pos)
self.PostPositionEvent()
except wx.PyAssertionError:
util.Log("[ed_stc][err] SetCaretPos - invalid position %s" % pos)
def GotoIndentPos(self, line=None):
"""Move the caret to the end of the indentation
on the given line.
@param line: line to go to
"""
if line is None:
line = self.GetCurrentLine()
self.GotoPos(self.GetLineIndentPosition(line))
def SetCurrentCol(self, column):
"""Set the current column position on the currently line
extending the selection.
@param column: Column to move to
"""
cline = self.GetCurrentLineNum()
lstart = self.PositionFromLine(cline)
lend = self.GetLineEndPosition(cline)
linelen = lend - lstart
if column > linelen:
column = linelen
self.SetCurrentPos(lstart + column)
def DeleteForward(self):
"""Delete the selection, or if there is no selection, then
delete the character to the right of the cursor.
"""
if self.GetSelectionStart() == self.GetSelectionEnd():
self.SetCurrentPos(self.GetCurrentPos() + 1)
self.DeleteBack()
def EnableKeyProcessor(self, enable=True):
"""Enable specialized key handling
@keyword enable: bool
"""
self.key_handler.EnableProcessing(enable)
def GetAutoComplete(self):
"""Is Autocomplete being used by this instance
@return: whether autocomp is active or not
"""
return self._config['autocomp']
def OnBackupTimer(self, evt):
"""Backup the buffer to a backup file.
@param evt: wx.TimerEvent
"""
fname = self.GetFileName()
# If the file is loading or is over 5MB don't do automatic backups.
if self.IsLoading() or ebmlib.GetFileSize(fname) > 5242880:
return
# If the file is different than the last save point make the backup.
suffix = _PGET('AUTOBACKUP_SUFFIX', default=u'.edbkup')
bkupmgr = ebmlib.FileBackupMgr(None, u"%s" + suffix)
path = _PGET('AUTOBACKUP_PATH', default=u"")
if path and os.path.exists(path):
bkupmgr.SetBackupDirectory(path)
if not self._backup_done and \
(not bkupmgr.HasBackup(fname) or bkupmgr.IsBackupNewer(fname)):
msg = _("File backup performed: %s") % fname
idval = self.Id
target = self.TopLevelParent
def BackupJob(fobj, text):
writer = bkupmgr.GetBackupWriter(fobj)
try:
writer(text)
except Exception, msg:
return
nevt = ed_event.StatusEvent(ed_event.edEVT_STATUS, idval,
msg, ed_glob.SB_INFO)
wx.PostEvent(target, nevt)
ed_thread.EdThreadPool().QueueJob(BackupJob, self.File, self.GetText())
self._backup_done = True
def OnModified(self, evt):
"""Overrides base modified handler"""
super(EditraStc, self).OnModified(evt)
if not self.IsLoading():
self._backup_done = False
def OnKeyDown(self, evt):
"""Handles keydown events, currently only deals with
auto indentation.
@param evt: event that called this handler
"""
k_code = evt.GetKeyCode()
shift_down = evt.ShiftDown()
alt_down = evt.AltDown()
ctrl_down = evt.ControlDown()
cmd_down = evt.CmdDown()
if self.key_handler.PreProcessKey(k_code, ctrl_down,
cmd_down, shift_down,
alt_down):
return
if wx.Platform == '__WXMAC__' and self._MacHandleKey(k_code, shift_down,
alt_down, ctrl_down,
cmd_down):
pass
elif k_code == wx.WXK_RETURN:
if self._config['autoindent'] and not self.AutoCompActive():
if self.GetSelectedText():
self.CmdKeyExecute(wx.stc.STC_CMD_NEWLINE)
else:
self.AutoIndent()
else:
evt.Skip()
self.CallTipCancel()
elif self.VertEdit.Enabled:
# XXX: handle column mode
self.VertEdit.OnKeyDown(evt)
else:
evt.Skip()
def OnChar(self, evt):
"""Handles Char events that aren't caught by the
KEY_DOWN event.
@param evt: event that called this handler
@todo: autocomp/calltip lookup can be very cpu intensive it may
be better to try and process it on a separate thread to
prevent a slow down in the input of text into the buffer
"""
key_code = evt.GetKeyCode()
cpos = self.GetCurrentPos()
cmpl = self.GetCompleter()
if self.key_handler.ProcessKey(key_code, evt.ControlDown(),
evt.CmdDown(), evt.ShiftDown(),
evt.AltDown()):
# The key handler handled this keypress, we don't need to insert
# the character into the buffer.
pass
elif not self._config['autocomp'] or not cmpl.ShouldCheck(cpos):
evt.Skip()
return
elif key_code in cmpl.GetAutoCompKeys():
self.HidePopups()
uchr = unichr(key_code)
command = self.GetCommandStr() + uchr
self.PutText(uchr)
if self._config['autocomp']:
self.ShowAutoCompOpt(command)
elif key_code in cmpl.GetCallTipKeys():
self.HidePopups()
uchr = unichr(key_code)
command = self.GetCommandStr() + uchr
self.PutText(uchr)
if self._config['autocomp']:
self.ShowCallTip(command)
elif key_code in cmpl.GetCallTipCancel():
evt.Skip()
self.CallTipCancel()
# elif key_code == wx.WXK_TAB and \
# True not in (evt.ControlDown(), evt.CmdDown(),
# evt.ShiftDown(), evt.AltDown()):
# self.Tab() # <- So action can be overridden
elif wx.Platform == '__WXMAC__' and evt.CmdDown():
pass # Hack for issue on OSX where char events are received here
else:
# print "IS TAB", key_code, wx.WXK_TAB
evt.Skip()
def DoAutoComplete(self):
"""Attempt to perform an autocompletion event."""
self.HidePopups()
command = self.GetCommandStr()
self.ShowAutoCompOpt(command)
def DoCallTip(self):
"""Attempt to show a calltip for the current cursor position"""
self.HidePopups()
command = self.GetCommandStr()
# TODO: GetCommandStr seems to be inadequate under some cases
self.ShowCallTip(command)
def OnKeyUp(self, evt):
"""Update status bar of window
@param evt: wxEVT_KEY_UP
"""
evt.Skip()
self.PostPositionEvent()
tlw = self.TopLevelParent
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_KEYUP,
(evt.GetPositionTuple(), evt.GetKeyCode()), tlw.Id)
def PostPositionEvent(self):
"""Post an event to update the status of the line/column"""
line, column = self.GetPos()
pinfo = dict(lnum=line, cnum=column)
msg = _("Line: %(lnum)d Column: %(cnum)d") % pinfo
nevt = ed_event.StatusEvent(ed_event.edEVT_STATUS, self.GetId(),
msg, ed_glob.SB_ROWCOL)
tlw = self.TopLevelParent
wx.PostEvent(tlw, nevt)
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_POS_CHANGED, pinfo, tlw.Id)
def OnRecordMacro(self, evt):
"""Records macro events
@param evt: wx.stc.StyledTextEvent
"""
if self.IsRecording():
msg = evt.GetMessage()
if msg == 2170:
lparm = self.GetTextRange(self.GetCurrentPos()-1, \
self.GetCurrentPos())
else:
lparm = evt.GetLParam()
mac = (msg, evt.GetWParam(), lparm)
self._macro.append(mac)
# if mac[0] != 2170:
# self._macro.append(mac)
else:
evt.Skip()
def ParaDown(self): # pylint: disable-msg=W0221
"""Move the caret one paragraph down
@note: overrides the default function to set caret at end
of paragraph instead of jumping to start of next
"""
self.WordPartRight()
super(EditraStc, self).ParaDown()
if self.GetCurrentPos() != self.GetLength():
self.WordPartLeft()
self.GotoPos(self.GetCurrentPos() + len(self.GetEOLChar()))
def ParaDownExtend(self): # pylint: disable-msg=W0221
"""Extend the selection a paragraph down
@note: overrides the default function to set selection at end
of paragraph instead of jumping to start of next so that
extra blank lines don't get swallowed.
"""
self.WordRightExtend()
super(EditraStc, self).ParaDownExtend()
if self.GetCurrentPos() != self.GetLength():
self.WordLeftExtend()
self.SetCurrentPos(self.GetCurrentPos() + len(self.GetEOLChar()))
@jumpaction
def OnLeftUp(self, evt):
"""Set primary selection and inform mainwindow that cursor position
has changed.
@param evt: wx.MouseEvent()
"""
evt.Skip()
# FIXME: there is problems with using the primary selection. Setting
# the primary selection causes anything else on the clipboard
# to get killed.
# stxt = self.GetSelectedText()
# if len(stxt):
# util.SetClipboardText(stxt, primary=True)
self.PostPositionEvent()
def OnLoadProgress(self, evt):
"""Receives file loading events from asynchronous file loading"""
pid = self.TopLevelParent.Id
if evt.GetState() == ed_txt.FL_STATE_READING:
if evt.HasText():
# TODO: get gauge updates working properly
# sb = self.GetTopLevelParent().GetStatusBar()
# gauge = sb.GetGauge()
# gauge.SetValue(evt.GetProgress())
# gauge.Show()
# gauge.ProcessPendingEvents()
# sb.ProcessPendingEvents()
self.SetReadOnly(False)
self.AppendText(evt.GetValue())
self.SetSavePoint()
self.SetReadOnly(True)
# wx.GetApp().Yield(True) # Too slow on windows...
elif evt.GetState() == ed_txt.FL_STATE_END:
self.SetReadOnly(False)
ed_msg.PostMessage(ed_msg.EDMSG_PROGRESS_STATE, (pid, 0, 0))
self.SetSavePoint()
self.SetUndoCollection(True)
del self._loading
self._loading = None
parent = self.GetParent()
if hasattr(parent, 'DoPostLoad'):
parent.DoPostLoad()
elif evt.GetState() == ed_txt.FL_STATE_START:
ed_msg.PostMessage(ed_msg.EDMSG_PROGRESS_SHOW, (pid, True))
ed_msg.PostMessage(ed_msg.EDMSG_PROGRESS_STATE, (pid, 0, self.File.GetSize()))
self.SetReadOnly(True)
self.SetUndoCollection(False)
elif evt.GetState() == ed_txt.FL_STATE_ABORTED:
self.SetReadOnly(False)
self.ClearAll()
def OnUpdateUI(self, evt):
"""Check for matching braces
@param evt: wx.stc.StyledTextEvent
"""
# If disabled just skip the event
if self._config['brackethl']:
self.DoBraceHighlight()
# XXX: handle when column mode is enabled
if self.VertEdit.Enabled:
self.VertEdit.OnUpdateUI(evt)
evt.Skip()
def OnUserListSel(self, evt):
"""Callback hook for userlist selections"""
mdata = dict(ltype=evt.GetListType(),
text=evt.GetText(),
stc=self)
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_USERLIST_SEL, mdata,
context=self.TopLevelParent.Id)
evt.Skip()
def OnDwellStart(self, evt):
"""Callback hook for mouse dwell start"""
# Workaround issue where this event in incorrectly sent
# when the mouse has not dwelled within the buffer area
mpoint = wx.GetMousePosition()
brect = self.GetScreenRect()
if not brect.Contains(mpoint) or \
not self.IsShown() or \
not self.TopLevelParent.IsActive():
return
position = evt.Position
if not self._dwellsent and position >= 0:
dwellword = self.GetWordFromPosition(position)[0]
line_num = self.LineFromPosition(position) + 1
mdata = dict(stc=self, pos=position,
line=line_num,
word=dwellword)
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_DWELL_START, mdata)
tip = mdata.get('rdata', None)
if tip:
self.CallTipShow(position, tip)
else:
# Clients did not need to make use of the calltip
# so check if auto-completion provider has anything to display.
if not self.IsNonCode(position):
endpos = self.WordEndPosition(position, True)
col = self.GetColumn(endpos)
line = self.GetLine(line_num-1)
command = self.GetCommandStr(line, col)
tip = self._code['compsvc'].GetCallTip(command)
if len(tip):
tip_pos = position - (len(dwellword.split('.')[-1]) + 1)
fail_safe = position - self.GetColumn(position)
self.CallTipShow(max(tip_pos, fail_safe), tip)
evt.Skip()
def OnDwellEnd(self, evt):
"""Callback hook for mouse dwell end"""
self._dwellsent = False
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_DWELL_END)
self.CallTipCancel()
evt.Skip()
def OnMarginClick(self, evt):
"""Open and Close Folders as Needed
@param evt: wx.stc.StyledTextEvent
"""
margin_num = evt.GetMargin()
if margin_num == ed_basestc.FOLD_MARGIN:
if evt.GetShift() and \
(evt.GetControl() or (wx.Platform == '__WXMAC__' and evt.GetAlt())):
self.FoldAll()
else:
line_clicked = self.LineFromPosition(evt.GetPosition())
level = self.GetFoldLevel(line_clicked)
if level & wx.stc.STC_FOLDLEVELHEADERFLAG:
# Expand node and all Subnodes
if evt.GetShift():
self.SetFoldExpanded(line_clicked, True)
self.Expand(line_clicked, True, True, 100, level)
elif evt.GetControl() or \
(wx.Platform == '__WXMAC__' and evt.GetAlt()):
# Contract all subnodes of clicked one
# Note: using Alt as Ctrl can not be received for
# clicks on mac (Scintilla Bug).
if self.GetFoldExpanded(line_clicked):
self.SetFoldExpanded(line_clicked, False)
self.Expand(line_clicked, False, True, 0, level)
else:
# Expand all subnodes
self.SetFoldExpanded(line_clicked, True)
self.Expand(line_clicked, True, True, 100, level)
else:
self.ToggleFold(line_clicked)
elif margin_num == ed_basestc.MARK_MARGIN:
# Bookmarks ect...
line_clicked = self.LineFromPosition(evt.GetPosition())
# Hook for client code to interact with margin clicks
data = dict(stc=self, line=line_clicked)
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_MARGIN_CLICK, msgdata=data)
if not data.get('handled', False):
# Default to internal bookmark handling
if ed_marker.Bookmark().IsSet(self, line_clicked):
self.RemoveBookmark(line_clicked)
else:
self.AddBookmark(line_clicked)
def FoldAll(self):
"""Fold Tree In or Out
@postcondition: code tree is folded open or closed
"""
line_count = self.GetLineCount()
expanding = True
# find out if we are folding or unfolding
for line_num in range(line_count):
if self.GetFoldLevel(line_num) & wx.stc.STC_FOLDLEVELHEADERFLAG:
expanding = not self.GetFoldExpanded(line_num)
break
line_num = 0
while line_num < line_count:
level = self.GetFoldLevel(line_num)
if level & wx.stc.STC_FOLDLEVELHEADERFLAG and \
(level & wx.stc.STC_FOLDLEVELNUMBERMASK) == \
wx.stc.STC_FOLDLEVELBASE:
if expanding:
self.SetFoldExpanded(line_num, True)
line_num = self.Expand(line_num, True) - 1
else:
last_child = self.GetLastChild(line_num, -1)
self.SetFoldExpanded(line_num, False)
if last_child > line_num:
self.HideLines(line_num + 1, last_child)
line_num = line_num + 1
def Expand(self, line, do_expand, force=False, vis_levels=0, level=-1):
"""Open the Margin Folder
@postcondition: the selected folder is expanded
"""
last_child = self.GetLastChild(line, level)
line = line + 1
while line <= last_child:
if force:
if vis_levels > 0:
self.ShowLines(line, line)
else:
self.HideLines(line, line)
else:
if do_expand:
self.ShowLines(line, line)
if level == -1:
level = self.GetFoldLevel(line)
if level & wx.stc.STC_FOLDLEVELHEADERFLAG:
if force:
self.SetFoldExpanded(line, vis_levels > 1)
line = self.Expand(line, do_expand, force, vis_levels - 1)
else:
if do_expand:
if self.GetFoldExpanded(line):
self.SetFoldExpanded(line, True)
line = self.Expand(line, do_expand, force, vis_levels - 1)
else:
line = line + 1
return line
def ExpandAll(self):
"""Expand all folded code blocks"""
line_count = self.GetLineCount()
for line_num in xrange(line_count):
if self.GetFoldLevel(line_num) & wx.stc.STC_FOLDLEVELHEADERFLAG:
if not self.GetFoldExpanded(line_num):
self.Expand(line_num, True)
def FindLexer(self, set_ext=u''):
"""Sets Text Controls Lexer Based on File Extension
@param set_ext: explicit extension to use in search
@postcondition: lexer is configured for file
"""
if not self._config['highlight']:
return 2
super(EditraStc, self).FindLexer(set_ext)
# Configure Autocompletion
# NOTE: must be done after syntax configuration
if self._config['autocomp']:
self.ConfigureAutoComp()
return 0
def ControlDispatch(self, evt):
"""Dispatches events caught from the mainwindow to the
proper functions in this module.
@param evt: event that was posted to this handler
"""
e_id = evt.GetId()
e_obj = evt.GetEventObject()
e_map = { ed_glob.ID_COPY : self.Copy, ed_glob.ID_CUT : self.Cut,
ed_glob.ID_PASTE : self.Paste, ed_glob.ID_UNDO : self.Undo,
ed_glob.ID_REDO : self.Redo, ed_glob.ID_INDENT : self.Tab,
ed_glob.ID_REVERT_FILE : self.RevertToSaved,
ed_glob.ID_CUT_LINE : self.LineCut,
ed_glob.ID_DELETE_LINE : self.LineDelete,
ed_glob.ID_COLUMN_MODE : self.ToggleColumnMode,
ed_glob.ID_COPY_LINE : self.LineCopy,
ed_glob.ID_DUP_LINE : self.LineDuplicate,
ed_glob.ID_BRACKETHL : self.ToggleBracketHL,
ed_glob.ID_SYNTAX : self.SyntaxOnOff,
ed_glob.ID_UNINDENT : self.BackTab,
ed_glob.ID_TRANSPOSE : self.LineTranspose,
ed_glob.ID_LINE_MOVE_UP : self.LineMoveUp,
ed_glob.ID_LINE_MOVE_DOWN : self.LineMoveDown,
ed_glob.ID_SELECTALL: self.SelectAll,
ed_glob.ID_FOLDING : self.FoldingOnOff,
ed_glob.ID_SHOW_LN : self.ToggleLineNumbers,
ed_glob.ID_TOGGLECOMMENT : self.ToggleComment,
ed_glob.ID_AUTOINDENT : self.ToggleAutoIndent,
ed_glob.ID_LINE_AFTER : self.AddLine,
ed_glob.ID_TOGGLE_FOLD : self.ToggleFold,
ed_glob.ID_TOGGLE_ALL_FOLDS : self.FoldAll,
ed_glob.ID_TRIM_WS : self.TrimWhitespace,
ed_glob.ID_MACRO_START : self.StartRecord,
ed_glob.ID_MACRO_STOP : self.StopRecord,
ed_glob.ID_MACRO_PLAY : self.PlayMacro,
ed_glob.ID_GOTO_MBRACE : self.GotoBraceMatch,
ed_glob.ID_SHOW_AUTOCOMP : self.DoAutoComplete,
ed_glob.ID_SHOW_CALLTIP : self.DoCallTip
}
e_idmap = { ed_glob.ID_ZOOM_OUT : self.DoZoom,
ed_glob.ID_ZOOM_IN : self.DoZoom,
ed_glob.ID_ZOOM_NORMAL : self.DoZoom,
ed_glob.ID_EOL_MAC : self.ConvertLineMode,
ed_glob.ID_EOL_UNIX : self.ConvertLineMode,
ed_glob.ID_EOL_WIN : self.ConvertLineMode,
ed_glob.ID_SPACE_TO_TAB : self.ConvertWhitespace,
ed_glob.ID_TAB_TO_SPACE : self.ConvertWhitespace,
ed_glob.ID_NEXT_MARK : self.Bookmark,
ed_glob.ID_PRE_MARK : self.Bookmark,
ed_glob.ID_ADD_BM : self.Bookmark,
ed_glob.ID_DEL_ALL_BM : self.Bookmark}
# Hide autocomp popups
self.HidePopups()
if e_obj.GetClassName() == "wxToolBar" or e_id in e_map:
if e_id in e_map:
e_map[e_id]()
return
if e_id in e_idmap:
e_idmap[e_id](e_id)
elif e_id == ed_glob.ID_SHOW_EDGE:
self.SetViewEdgeGuide(not self.GetEdgeMode())
elif e_id == ed_glob.ID_SHOW_EOL:
self.SetViewEOL(not self.GetViewEOL())
elif e_id == ed_glob.ID_PASTE_AFTER:
cpos = self.GetCurrentPos()
self.Paste()
self.SetCurrentPos(cpos)
self.SetSelection(cpos, cpos)
elif e_id == ed_glob.ID_SHOW_WS:
self.SetViewWhiteSpace(not self.GetViewWhiteSpace())
elif e_id == ed_glob.ID_WORD_WRAP:
self.SetWrapMode(not self.GetWrapMode())
elif e_id == ed_glob.ID_JOIN_LINES:
self.LinesJoinSelected()
elif e_id == ed_glob.ID_INDENT_GUIDES:
self.SetIndentationGuides(not bool(self.GetIndentationGuides()))
elif e_id == ed_glob.ID_HLCARET_LINE:
self.SetCaretLineVisible(not self.GetCaretLineVisible())
elif e_id in syntax.SYNTAX_IDS:
f_ext = syntax.GetExtFromId(e_id)
self.LOG("[ed_stc][evt] Manually Setting Lexer to %s" % str(f_ext))
self.FindLexer(f_ext)
elif e_id == ed_glob.ID_AUTOCOMP:
self.SetAutoComplete(not self.GetAutoComplete())
elif e_id == ed_glob.ID_LINE_BEFORE:
self.AddLine(before=True)
elif e_id in [ed_glob.ID_TO_UPPER, ed_glob.ID_TO_LOWER]:
self.ConvertCase(e_id == ed_glob.ID_TO_UPPER)
elif e_id == ed_glob.ID_USE_SOFTTABS:
self.SetUseTabs(not self.GetUseTabs())
else:
evt.Skip()
def CheckEOL(self):
"""Checks the EOL mode of the opened document. If the mode
that the document was saved in is different than the editors
current mode the editor will switch modes to preserve the eol
type of the file, if the eol chars are mixed then the editor
will toggle on eol visibility.
@postcondition: eol mode is configured to best match file
@todo: Is showing line endings the best way to show mixed?
"""
mixed = diff = False
eol_map = {u"\n" : wx.stc.STC_EOL_LF,
u"\r\n" : wx.stc.STC_EOL_CRLF,
u"\r" : wx.stc.STC_EOL_CR}
eol = unichr(self.GetCharAt(self.GetLineEndPosition(0)))
if eol == u"\r":
tmp = unichr(self.GetCharAt(self.GetLineEndPosition(0) + 1))
if tmp == u"\n":
eol += tmp
# Is the eol used in the document the same as what is currently set.
if eol != self.GetEOLChar():
diff = True
# Check the lines to see if they are all matching or not.
LEPFunct = self.GetLineEndPosition
GCAFunct = self.GetCharAt
for line in range(self.GetLineCount() - 1):
end = LEPFunct(line)
tmp = unichr(GCAFunct(end))
if tmp == u"\r":
tmp2 = unichr(GCAFunct(LEPFunct(0) + 1))
if tmp2 == u"\n":
tmp += tmp2
if tmp != eol:
mixed = True
break
if mixed or diff:
if mixed:
# Warn about mixed end of line characters and offer to convert
msg = _("Mixed EOL characters detected.\n\n"
"Would you like to format them to all be the same?")
dlg = ed_mdlg.EdFormatEOLDlg(self.TopLevelParent, msg,
_("Format EOL?"),
eol_map.get(eol, self.GetEOLMode()))
if dlg.ShowModal() == wx.ID_YES:
sel = dlg.GetSelection()
self.ConvertEOLs(sel)
super(EditraStc, self).SetEOLMode(sel)
dlg.Destroy()
else:
# The end of line character is different from the preferred
# user setting for end of line. So change our eol mode to
# preserve that of what the document is using.
mode = eol_map.get(eol, wx.stc.STC_EOL_LF)
super(EditraStc, self).SetEOLMode(mode)
else:
pass
def ConvertLineMode(self, mode_id):
"""Converts all line endings in a document to a specified
format.
@param mode_id: (menu) id of eol mode to set
"""
eol_map = { ed_glob.ID_EOL_MAC : wx.stc.STC_EOL_CR,
ed_glob.ID_EOL_UNIX : wx.stc.STC_EOL_LF,
ed_glob.ID_EOL_WIN : wx.stc.STC_EOL_CRLF
}
self.ConvertEOLs(eol_map[mode_id])
super(EditraStc, self).SetEOLMode(eol_map[mode_id])
def ConvertWhitespace(self, mode_id):
"""Convert whitespace from using tabs to spaces or visa versa
@param mode_id: id of conversion mode
"""
if mode_id not in (ed_glob.ID_TAB_TO_SPACE, ed_glob.ID_SPACE_TO_TAB):
return
tabw = self.GetIndent()
pos = self.GetCurrentPos()
sel = self.GetSelectedText()
if mode_id == ed_glob.ID_TAB_TO_SPACE:
cmd = (u"\t", u" " * tabw)
tabs = False
else:
cmd = (" " * tabw, u"\t")
tabs = True
if sel != wx.EmptyString:
self.ReplaceSelection(sel.replace(cmd[0], cmd[1]))
else:
self.BeginUndoAction()
part1 = self.GetTextRange(0, pos).replace(cmd[0], cmd[1])
tmptxt = self.GetTextRange(pos, self.GetLength()).replace(cmd[0], \
cmd[1])
self.SetText(part1 + tmptxt)
self.GotoPos(len(part1))
self.SetUseTabs(tabs)
self.EndUndoAction()
def GetCurrentLineNum(self):
"""Return the number of the line that the caret is currently at
@return: Line number (int)
"""
return self.LineFromPosition(self.GetCurrentPos())
def GetEOLModeId(self):
"""Gets the id of the eol format. Convenience for updating
menu ui.
@return: id of the eol mode of this document
"""
eol_map = { wx.stc.STC_EOL_CR : ed_glob.ID_EOL_MAC,
wx.stc.STC_EOL_LF : ed_glob.ID_EOL_UNIX,
wx.stc.STC_EOL_CRLF : ed_glob.ID_EOL_WIN
}
return eol_map.get(self.GetEOLMode(), ed_glob.ID_EOL_UNIX)
def IsBracketHlOn(self):
"""Returns whether bracket highlighting is being used by this
control or not.
@return: status of bracket highlight activation
"""
return self._config['brackethl']
def IsFoldingOn(self):
"""Returns whether code folding is being used by this
control or not.
@return: whether folding is on or not
"""
return self._config['folding']
def IsHighlightingOn(self):
"""Returns whether syntax highlighting is being used by this
control or not.
@return: whether syntax highlighting is on or not
"""
return self._config['highlight']
def IsLoading(self):
"""Is a background thread loading the text into the file
@return: bool
"""
# NOTE: keep the getattr check here some cases
# are reporting a yet unexplainable AttributeError here
return getattr(self, '_loading', None) is not None
def IsRecording(self):
"""Returns whether the control is in the middle of recording
a macro or not.
@return: whether recording macro or not
"""
return self.recording
def GetSelectionLineStartEnd(self):
"""Get the start and end positions of the lines in the current
fuzzy selection.
@return: tuple (int, int)
"""
sline = self.LineFromPosition(self.GetSelectionStart())
eline = self.LineFromPosition(self.GetSelectionEnd())
last_line = self.GetLineCount() - 1
eol_len = len(self.GetEOLChar())
if sline < eline:
tstart = self.GetLineStartPosition(sline)
tend = self.GetLineEndPosition(eline)
else:
tstart = self.GetLineStartPosition(eline)
tend = self.GetLineEndPosition(sline)
if eline == last_line and tstart != 0:
tstart -= eol_len
else:
tend += eol_len
return (max(tstart, 0), min(tend, self.GetLength()))
def LineCut(self): # pylint: disable-msg=W0221
"""Cut the selected lines into the clipboard"""
start, end = self.GetSelectionLineStartEnd()
self.BeginUndoAction()
self.BaseSetSelection(start, end)
self.Cut()
self.EndUndoAction()
def LineDelete(self): # pylint: disable-msg=W0221
"""Delete the selected lines without modifying the clipboard"""
start, end = self.GetSelectionLineStartEnd()
self.BeginUndoAction()
self.SetTargetStart(start)
self.SetTargetEnd(end)
self.ReplaceTarget(u'')
self.EndUndoAction()
def LinesJoin(self): # pylint: disable-msg=W0221
"""Join lines in target and compress whitespace
@note: overrides default function to allow for leading
whitespace in joined lines to be compressed to 1 space
"""
sline = self.LineFromPosition(self.GetTargetStart())
eline = self.LineFromPosition(self.GetTargetEnd())
if not eline:
eline = 1
lines = list()
for line in xrange(sline, eline + 1):
if line != sline:
tmp = self.GetLine(line).strip()
else:
tmp = self.GetLine(line)
if not tmp.isspace():
tmp = tmp.rstrip()
else:
tmp = tmp.replace("\n", u'').replace("\r", u'')
if len(tmp):
lines.append(tmp)
self.SetTargetStart(self.PositionFromLine(sline))
self.SetTargetEnd(self.GetLineEndPosition(eline))
self.ReplaceTarget(u' '.join(lines))
def LinesJoinSelected(self):
"""Similar to LinesJoin, but operates on selection
@see: LinesJoin
"""
self.SetTargetStart(self.GetSelectionStart())
self.SetTargetEnd(self.GetSelectionEnd())
self.LinesJoin()
def LineMoveUp(self):
"""Move the current line up"""
linenum = self.GetCurrentLine()
if linenum > 0 :
self.BeginUndoAction()
self.LineTranspose()
self.LineUp()
self.EndUndoAction()
def LineMoveDown(self):
"""Move the current line down"""
linenum = self.GetCurrentLine()
col = self.GetColumn(self.GetCurrentPos())
if linenum < self.GetLineCount() - 1:
self.BeginUndoAction()
self.LineDown()
self.LineTranspose()
self.GotoColumn(col)
self.EndUndoAction()
def LineTranspose(self): # pylint: disable-msg=W0221
"""Switch the current line with the previous one
@note: overrides base stc method to do transpose in single undo action
"""
self.BeginUndoAction()
super(EditraStc, self).LineTranspose()
self.EndUndoAction()
def SetAutoComplete(self, value):
"""Turns Autocompletion on and off
@param value: use autocomp or not
"""
if isinstance(value, bool):
self._config['autocomp'] = value
if value:
self.InitCompleter()
def SetEOLMode(self, mode):
"""Sets the EOL mode from a string description
@param mode: eol mode to set
@note: overrides StyledTextCtrl.SetEOLMode
"""
mode_map = { EDSTC_EOL_CR : wx.stc.STC_EOL_CR,
EDSTC_EOL_LF : wx.stc.STC_EOL_LF,
EDSTC_EOL_CRLF : wx.stc.STC_EOL_CRLF
}
mode = mode_map.get(mode, wx.stc.STC_EOL_LF)
super(EditraStc, self).SetEOLMode(mode)
def SetViEmulationMode(self, use_vi, use_normal=False):
"""Activate/Deactivate Vi emulation mode
@param use_vi: Turn vi emulation on/off
@keyword use_normal: Start in normal mode
"""
self.key_handler.ClearMode()
if use_vi:
self.key_handler = ViKeyHandler(self, use_normal)
else:
self.key_handler = KeyHandler(self)
def SetViewEdgeGuide(self, switch=None):
"""Toggles the visibility of the edge guide
@keyword switch: force a particular setting
"""
if (switch is None and not self.GetEdgeMode()) or switch:
self.SetEdgeColumn(_PGET("EDGE", 'int', 80))
self.SetEdgeMode(wx.stc.STC_EDGE_LINE)
else:
self.SetEdgeMode(wx.stc.STC_EDGE_NONE)
def StartRecord(self): # pylint: disable-msg=W0221
"""Starts recording all events
@return: None
"""
self.recording = True
evt = ed_event.StatusEvent(ed_event.edEVT_STATUS, self.GetId(),
_("Recording Macro") + u"...",
ed_glob.SB_INFO)
wx.PostEvent(self.TopLevelParent, evt)
super(EditraStc, self).StartRecord()
def StopRecord(self): # pylint: disable-msg=W0221
"""Stops the recording and builds the macro script
@postcondition: macro recording is stopped
"""
self.recording = False
super(EditraStc, self).StopRecord()
evt = ed_event.StatusEvent(ed_event.edEVT_STATUS, self.GetId(),
_("Recording Finished"),
ed_glob.SB_INFO)
wx.PostEvent(self.TopLevelParent, evt)
self._BuildMacro()
def TrimWhitespace(self):
"""Trims trailing whitespace from all lines in the document.
@postcondition: all trailing whitespace is removed from document
"""
cpos = self.GetCurrentPos()
cline = self.GetCurrentLine()
cline_len = len(self.GetLine(cline))
epos = cline_len - (self.GetLineEndPosition(cline) - cpos)
# Begin stripping trailing whitespace
self.BeginUndoAction()
for line in xrange(self.GetLineCount()):
eol = u''
tmp = self.GetLine(line)
# Scintilla stores text in utf8 internally so we need to
# encode to utf8 to get the correct length of the text.
try:
tlen = len(tmp.encode('utf-8'))
except:
tlen = len(tmp)
if tlen:
if "\r\n" in tmp:
eol = "\r\n"
elif "\n" in tmp:
eol = "\n"
else:
eol = tmp[-1]
if not eol.isspace():
continue
elif eol in u' \t':
eol = u''
else:
continue
# Strip the extra whitespace from the line
end = self.GetLineEndPosition(line) + len(eol)
start = max(end - tlen, 0)
self.SetTargetStart(start)
self.SetTargetEnd(end)
rtxt = tmp.rstrip() + eol
if rtxt != self.GetTextRange(start, end):
self.ReplaceTarget(tmp.rstrip() + eol)
self.EndUndoAction()
# Restore carat position
cline_len = len(self.GetLine(cline))
end = self.GetLineEndPosition(cline)
if epos >= cline_len:
epos = end
else:
start = max(end - cline_len, 0)
epos += start
if epos != cpos:
self.GotoPos(epos)
def FoldingOnOff(self, switch=None):
"""Turn code folding on and off
@keyword switch: force a particular setting
"""
if (switch is None and not self._config['folding']) or switch:
self.LOG("[ed_stc][evt] Code Folding Turned On")
self._config['folding'] = True
self.SetMarginWidth(ed_basestc.FOLD_MARGIN, 12)
self.SetProperty("fold", "1")
else:
self.LOG("[ed_stc][evt] Code Folding Turned Off")
self._config['folding'] = False
# Ensure all code blocks have been expanded
self.ExpandAll()
self.SetMarginWidth(ed_basestc.FOLD_MARGIN, 0)
self.SetProperty("fold", "0")
def SyntaxOnOff(self, switch=None):
"""Turn Syntax Highlighting on and off
@keyword switch: force a particular setting
"""
if (switch is None and not self._config['highlight']) or switch:
self.LOG("[ed_stc][evt] Syntax Highlighting Turned On")
self._config['highlight'] = True
self.FindLexer()
else:
self.LOG("[ed_stc][evt] Syntax Highlighting Turned Off")
self._config['highlight'] = False
self.SetLexer(wx.stc.STC_LEX_NULL)
self.ClearDocumentStyle()
self.UpdateBaseStyles()
return 0
def Tab(self): # pylint: disable-msg=W0221
"""Override base method to ensure that folded blocks get unfolded
prior to changing the indentation.
"""
# TODO: unfolding of folded blocks during block indent
# lines = list()
# if self.HasSelection():
# sel = self.GetSelection()
# sline = self.LineFromPosition(sel[0])
# eline = self.LineFromPosition(sel[1])
# lines = range(sline, eline+1)
# else:
# cline = self.GetCurrentLine()
# lines = [cline, cline+1]
# for line_num in lines:
# if self.GetFoldLevel(line_num) & wx.stc.STC_FOLDLEVELHEADERFLAG:
# if not self.GetFoldExpanded(line_num):
# self.Expand(line_num, True)
super(EditraStc, self).Tab()
def ToggleAutoIndent(self, switch=None):
"""Toggles Auto-indent On and Off
@keyword switch: force a particular setting
"""
if (switch is None and not self._config['autoindent']) or switch:
self._config['autoindent'] = True
else:
self._config['autoindent'] = False
def ToggleBracketHL(self, switch=None):
"""Toggle Bracket Highlighting On and Off
@keyword switch: force a particular setting
"""
if (switch is None and not self._config['brackethl']) or switch:
self.LOG("[ed_stc][evt] Bracket Highlighting Turned On")
self._config['brackethl'] = True
# Make sure to highlight a brace if next to on when turning it on
self.DoBraceHighlight()
else:
self.LOG("[ed_stc][evt] Bracket Highlighting Turned Off")
self._config['brackethl'] = False
# Make sure that if there was a highlighted brace it gets cleared
wx.CallAfter(self.BraceHighlight, -1, -1)
def ToggleFold(self, lineNum=None):
"""Toggle the fold at the given line number. If lineNum is
None then the fold closest cursors current postions.
@keyword lineNum: int
"""
if lineNum is None:
lineNum = self.GetCurrentLine()
super(EditraStc, self).ToggleFold(lineNum)
@jumpaction
def WordLeft(self): # pylint: disable-msg=W0221
"""Move caret to beginning of previous word
@note: override builtin to include extra characters in word
"""
self.SetWordChars(NONSPACE)
super(EditraStc, self).WordLeft()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos + 1) in SPACECHARS:
super(EditraStc, self).WordLeft()
self.SetWordChars('')
def WordLeftExtend(self): # pylint: disable-msg=W0221
"""Extend selection to beginning of previous word
@note: override builtin to include extra characters in word
"""
self.SetWordChars(NONSPACE)
super(EditraStc, self).WordLeftExtend()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos + 1) in SPACECHARS:
super(EditraStc, self).WordLeftExtend()
self.SetWordChars('')
@jumpaction
def WordPartLeft(self): # pylint: disable-msg=W0221
"""Move the caret left to the next change in capitalization/punctuation
@note: overrides default function to not count whitespace as words
"""
super(EditraStc, self).WordPartLeft()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos + 1) in SPACECHARS:
super(EditraStc, self).WordPartLeft()
def WordPartLeftExtend(self): # pylint: disable-msg=W0221
"""Extend selection left to the next change in c
apitalization/punctuation.
@note: overrides default function to not count whitespace as words
"""
super(EditraStc, self).WordPartLeftExtend()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos + 1) in SPACECHARS:
super(EditraStc, self).WordPartLeftExtend()
@jumpaction
def WordPartRight(self): # pylint: disable-msg=W0221
"""Move the caret to the start of the next word part to the right
@note: overrides default function to exclude white space
"""
super(EditraStc, self).WordPartRight()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos + 1) in SPACECHARS:
super(EditraStc, self).WordPartRight()
@jumpaction
def WordPartRightEnd(self): # pylint: disable-msg=W0221
"""Move caret to end of next change in capitalization/punctuation
@postcondition: caret is moved
"""
super(EditraStc, self).WordPartRight()
super(EditraStc, self).WordPartRight()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos - 1) in SPACECHARS:
self.CharLeft()
def WordPartRightEndExtend(self): # pylint: disable-msg=W0221
"""Extend selection to end of next change in capitalization/punctuation
@postcondition: selection is extended
"""
super(EditraStc, self).WordPartRightExtend()
super(EditraStc, self).WordPartRightExtend()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos - 1) in SPACECHARS:
self.CharLeftExtend()
def WordPartRightExtend(self): # pylint: disable-msg=W0221
"""Extend selection to start of next change in
capitalization/punctuation
@postcondition: selection is extended
"""
super(EditraStc, self).WordPartRightExtend()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos + 1) in SPACECHARS:
super(EditraStc, self).WordPartRightExtend()
@jumpaction
def WordRight(self): # pylint: disable-msg=W0221
"""Move caret to beginning of next word
@note: override builtin to include extra characters in word
"""
self.SetWordChars(NONSPACE)
super(EditraStc, self).WordRight()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos + 1) in SPACECHARS:
super(EditraStc, self).WordRight()
self.SetWordChars('')
@jumpaction
def WordRightEnd(self): # pylint: disable-msg=W0221
"""Move caret to end of next change in word
@note: override builtin to include extra characters in word
"""
self.SetWordChars(NONSPACE)
super(EditraStc, self).WordRightEnd()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos - 1) in SPACECHARS:
super(EditraStc, self).WordRightEnd()
self.SetWordChars('')
def WordRightExtend(self): # pylint: disable-msg=W0221
"""Extend selection to beginning of next word
@note: override builtin to include extra characters in word
"""
self.SetWordChars(NONSPACE)
super(EditraStc, self).WordRightExtend()
cpos = self.GetCurrentPos()
if self.GetTextRange(cpos, cpos + 1) in SPACECHARS:
super(EditraStc, self).WordRightExtend()
self.SetWordChars('')
def LoadFile(self, path):
"""Load the file at the given path into the buffer. Returns
True if no errors and False otherwise. To retrieve the errors
check the last error that was set in the file object returned by
L{GetDocument}.
@param path: path to file
"""
fsize = ebmlib.GetFileSize(path)
if fsize < 1048576: # 1MB
return super(EditraStc, self).LoadFile(path)
else:
ed_msg.PostMessage(ed_msg.EDMSG_FILE_OPENING, path)
self.file.SetPath(path)
self._loading = wx.BusyCursor()
self.file.ReadAsync(self)
return True
def ReloadFile(self):
"""Reloads the current file, returns True on success and
False if there is a failure.
@return: whether file was reloaded or not
"""
cfile = self.GetFileName()
if os.path.exists(cfile):
try:
self.BeginUndoAction()
marks = self.GetBookmarks()
cpos = self.GetCurrentPos()
# TODO: Handle async re-loads of large files
txt = self.File.Read()
self.SetReadOnly(False)
if txt is not None:
if self.File.IsRawBytes() and not ebmlib.IsUnicode(txt):
self.AddStyledText(txt)
self.SetReadOnly(True) # Don't allow editing of raw bytes
else:
self.SetText(txt)
else:
return False, _("Failed to reload: %s") % cfile
self.SetModTime(ebmlib.GetFileModTime(cfile))
for mark in marks:
self.AddBookmark(mark)
self.EndUndoAction()
self.SetSavePoint()
except (UnicodeDecodeError, AttributeError, OSError, IOError), msg:
self.LOG("[ed_stc][err] Failed to Reload %s" % cfile)
return False, msg
else:
self.GotoPos(cpos)
context = self.TopLevelParent.Id
ed_msg.PostMessage(ed_msg.EDMSG_FILE_OPENED,
self.GetFileName(), context)
return True, ''
else:
self.LOG("[ed_stc][err] %s does not exists, cant reload." % cfile)
return False, _("%s does not exist") % cfile
def RevertFile(self):
"""Revert all the changes made to the file since it was opened
@postcondition: undo history is re-wound to initial state and file
is re-saved if it has an on disk file.
"""
with eclib.Freezer(self) as _tmp:
while self.CanUndo():
self.Undo()
fname = self.GetFileName()
if len(fname):
self.SaveFile(fname)
def RevertToSaved(self):
"""Revert the current buffer back to the last save point"""
with eclib.Freezer(self) as _tmp:
while self.CanUndo():
if self.GetModify():
self.Undo()
else:
break
def SaveFile(self, path):
"""Save buffers contents to disk
@param path: path of file to save
@return: whether file was written or not
"""
result = True
try:
tlw_id = self.TopLevelParent.Id
ed_msg.PostMessage(ed_msg.EDMSG_FILE_SAVE,
(path, self.GetLangId()), tlw_id)
self.File.SetPath(path)
self.LOG("[ed_stc][info] Writing file %s, with encoding %s" % \
(path, self.File.GetEncoding()))
if _PGET('AUTO_TRIM_WS', 'bool', False):
self.TrimWhitespace()
if self.File.IsReadOnly():
wx.MessageBox(_("File is Read Only and cannot be saved"),
_("Read Only"),
style=wx.OK|wx.CENTER|wx.ICON_WARNING)
return True
else:
if not self.File.IsRawBytes():
self.File.Write(self.GetText())
else:
nchars = self.GetTextLength()
txt = self.GetStyledText(0, nchars)[0:nchars*2:2]
self.File.Write(txt)
except Exception, msg:
result = False
self.LOG("[ed_stc][err] There was an error saving %s" % path)
self.LOG("[ed_stc][err] ERROR: %s" % str(msg))
if result:
self.SetSavePoint()
self.SetModTime(ebmlib.GetFileModTime(path))
self.File.FireModified()
self.SetFileName(path)
wx.CallAfter(ed_msg.PostMessage,
ed_msg.EDMSG_FILE_SAVED,
(path, self.GetLangId()),
tlw_id)
return result
def ConfigureLexer(self, file_ext):
"""Sets Lexer and Lexer Keywords for the specified file extension
@param file_ext: a file extension to configure the lexer from
"""
super(EditraStc, self).ConfigureLexer(file_ext)
if not self._config['folding']:
self.SetProperty("fold", "0")
# Notify that lexer has changed
pid = self.TopLevelParent.Id
self.LOG("[ed_stc][info] Lexer change notification for context %d" % pid)
ed_msg.PostMessage(ed_msg.EDMSG_UI_STC_LEXER,
(self.GetFileName(), self.GetLangId()), pid)
return True
def HideCaret(self):
if hasattr(self, 'SetCaretStyle'):
self.SetCaretStyle(wx.stc.STC_CARETSTYLE_INVISIBLE)
else:
self.SetCaretWidth(0)
def RestoreCaret(self):
if self.key_handler.BlockMode:
self.SetBlockCaret()
else:
self.SetLineCaret()