File: text_utils.py

package info (click to toggle)
gnat-gps 18-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,716 kB
  • sloc: ada: 362,679; python: 31,031; xml: 9,597; makefile: 1,030; ansic: 917; sh: 264; java: 17
file content (1469 lines) | stat: -rw-r--r-- 48,621 bytes parent folder | download
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
"""Defines editor-specific functions

YOU SHOULD ALMOST ALWAYS LOAD THIS FILE

This script defines a number of python functions and GPS actions that can
be used inside GPS editors. These can be used to move the cursor around or
edit the text.
They are often programmed so that they emulate the Emacs editor, but they
are independent of the Emacs mode and do not override any key shortcut. As
a result you can define your own shortcuts for the actions defined in this
package.
See also emacs.xml
"""


############################################################################
# No user customization below this line
############################################################################

import GPS
from gi.repository import Gtk
from gps_utils import interactive, filter_text_actions, with_save_excursion, \
    in_ada_file, get_focused_widget, make_interactive, hook
import six

should_extend_selection = False
# Whether the selection should be extended when moving the cursor

transient_mark_pref = GPS.Preference("Src-Editor-Transient-Mark")

SUBPROGRAM_BLOCKS = set(["CAT_PROCEDURE", "CAT_FUNCTION", "CAT_ENTRY",
                         "CAT_PROTECTED", "CAT_TASK", "CAT_PACKAGE"])
# The block_types that are considered to be "subprogram" blocks


def parse_parentheses(editor, begin=None, end=None):
    """
    Parse parentheses of editor
    range: begin to end inclusive.
    Returns the parenthesis stack. Each element is parentheses: location int
    """
    # set the default begin and end if not provided
    if begin is None:
        begin = editor.beginning_of_buffer()
    if end is None:
        end = editor.end_of_buffer()

    pairs = {"(": ")", "[": "]", "{": "}"}
    h = pairs.keys()
    t = pairs.values()
    stack = []
    source = editor.get_chars(begin, end).rstrip("\n").splitlines()
    last = end.line()-1

    # parse all parentheses, find open parentheses
    for i in range(0, len(source)):
        if source[i].lstrip(" ").startswith("#"):
            source[i] = ""
        for j, c in enumerate(source[i]):
            if c in h:
                stack.append((i, j))
            elif c in t:
                    if stack is []:
                        continue
                    elif pairs[source[stack[-1][0]][stack[-1][1]]] == c:
                        # when parenthesis is closed, remember its line number
                        last = stack.pop()[0]

    closed = (len(stack) == 0)
    # get the last char of parsed text
    tail = editor.at(end.line(), end.end_of_line().column()-1).get_char()

    # if the parsed text is ending a parenthesis -->
    # last char is a closing parentheses, then the cursor should
    # return to where the openning counterparts's line start
    if tail in t and closed:
        tmp = source[last]
        start = len(tmp) - len(tmp.lstrip(" "))
        stack.append((last, start-1))
    return (stack, closed)


def forward_until(loc, pred,
                  skip_first_char=False,
                  stop_at_eol=False,
                  backwards=False,
                  give_up=True):

    step = -1 if backwards else 1
    cur_loc = loc

    if skip_first_char:
        cur_loc = cur_loc.forward_char(step)

    while not pred(cur_loc.get_char()):
        if cur_loc.get_char() == "\n" and stop_at_eol:
            if give_up:
                return loc
            return cur_loc

        if cur_loc == cur_loc.forward_char(step):
            if give_up:
                return loc
            return cur_loc

        cur_loc = cur_loc.forward_char(step)
    return cur_loc


def replace(frm, to, text):
    """Replace a part of the buffer by the given text"""
    frm.buffer().delete(frm, to)
    frm.buffer().insert(frm, text)


def goto_subprogram_start(cursor):
    """
    Return an EditorLocation corresponding to the subprogram in which
    cursor is
    """

    if cursor.block_type() == "CAT_UNKNOWN":
        return None

    min = cursor.buffer().beginning_of_buffer()
    cursor = cursor.block_start()
    while cursor.block_type() not in SUBPROGRAM_BLOCKS and cursor > min:
        cursor = cursor.block_start() - 1

    if cursor >= min:
        return cursor
    else:
        return None


def get_local_vars(subprogram):
    """
    Return a list of GPS.Entity that are variables local to the subprogram. It
    might not work accurately with nested subprograms
    """
    result = []
    if subprogram:
        locFile = subprogram.body().file()
        locFrom = subprogram.body().line()
        locTo = subprogram.end_of_scope().line()

        for e in locFile.entities(local=True):
            decl = e.declaration()
            if not e.is_type() \
               and decl.file() == locFile \
               and decl.line() >= locFrom \
               and decl.line() <= locTo:
                result.append(e)

    return result


def delete_until_char(char, buffer=None):
    """
    Delete all characters forward from the current cursor position, until CHAR
    is seen. CHAR itself is also deleted.  If the current character is CHAR, it
    is skipped and the next occurrences of CHAR is searched.
    """
    if not buffer:
        buffer = GPS.EditorBuffer.get()

    start = buffer.current_view().cursor()
    end = start + 1
    while end.get_char() != char:
        end = end + 1
    buffer.delete(start, end)


@interactive("Editor", name="zap to char")
class Zap_To_Char(GPS.CommandWindow):

    """
    Deletes all characters from the cursor position up to and including the
    next occurrence of a character. The character is queried interactively
    """

    def __init__(self):
        GPS.CommandWindow.__init__(
            self,
            prompt="Zap to char:",
            on_changed=self.on_changed)

    @with_save_excursion
    def on_changed(self, input, cursor_pos):
        delete_until_char(char=input)
        self.hide()


@interactive("Editor",  name="toggle wrapping")
def toggle_editor_wrapping():
    """Toggle word wrapping in the current editor"""

    buffer = GPS.EditorBuffer.get()
    v = buffer.current_view()
    from pygps import get_widgets_by_type
    text_view = get_widgets_by_type(Gtk.TextView, v.pywidget())[0]
    if text_view.get_wrap_mode() == Gtk.WrapMode(0):
        text_view.set_wrap_mode(Gtk.WrapMode(2))
    else:
        text_view.set_wrap_mode(Gtk.WrapMode(0))


@interactive("Editor", in_ada_file, name="subprogram box")
@with_save_excursion
def add_subprogram_box():
    """
    Search backward for the first subprogram or package declaration. Before
    the start of this declaration, insert a comment box containing the name of
    the subprogram. This provides helpful separations between subprograms, and
    is similar to the style used in the GNAT compiler or GPS themselves
    """

    buffer = GPS.EditorBuffer.get()
    loc = goto_subprogram_start(buffer.current_view().cursor())
    if loc:
        name = loc.block_name()
        loc = loc.block_start().beginning_of_line()
        dashes = '-' * (len(name) + 6)
        box = dashes + "\n" + "-- " + name + " --\n" + dashes + "\n\n"
        buffer.insert(loc, box)
        buffer.indent(loc, loc.forward_line(3))


@interactive("Editor", "Source editor", name="select line")
def select_line():
    """
    Select the current line in the current editor, including trailing newline
    This moves the cursor to the end of the line
    """
    buffer = GPS.EditorBuffer.get()
    loc = buffer.current_view().cursor()
    buffer.select(loc.beginning_of_line(), loc.end_of_line() + 1)


@interactive("Editor", "Source editor", name="select subprogram")
def select_enclosing_block():
    """
    Select the subprogram which contains the current selection.

    If there is no selection, select the block that contains the cursor.
    If there is a selection, select the subprogram which contains the first
    line of the selection - so you can call this action multiple times in
    a row to select parent subprograms.
    """
    b = GPS.EditorBuffer.get()
    sel_start = b.selection_start()

    # the first line is already selected? There isn't an "enclosing"
    # subprogram, so let's select everything.
    if sel_start.line() == 1:
        b.select()
        return

    # Find the enclosing subprogram
    loc = sel_start

    while (loc.block_type() not in SUBPROGRAM_BLOCKS and
           loc.line() > 1) or (sel_start == loc.block_start()):
        start = loc.block_start()
        if start.line() == 1:
            loc = start
            break
        else:
            loc = b.at(start.line() - 1, 1)

    if loc.line() <= 1:
        b.select()
        return

    b.select(loc.block_start(), loc.block_end())


def get_selection_or_buffer(buffer=None):
    """
    If a selection exists, returns its beginning and end. Otherwise return the
    beginning and end of buffer.  The buffer is returned as the first field of
    the tuple.
    """
    if not buffer:
        buffer = GPS.EditorBuffer.get()
    start = buffer.selection_start()
    end = buffer.selection_end()
    if start == end:
        return (buffer, buffer.beginning_of_buffer(), buffer.end_of_buffer())
    else:
        return (buffer, start, end)


def get_selection_or_word(buffer=None):
    """
    If a selection exists, returns its beginning and end. Otherwise return the
    beginning and end of the current word..  The buffer is returned as the
    first field of the tuple
    """
    if not buffer:
        buffer = GPS.EditorBuffer.get()
    start = buffer.selection_start()
    end = buffer.selection_end()
    if start == end:
        loc = buffer.current_view().cursor()
        return (buffer, goto_word_start(loc), goto_word_end(loc))
    else:
        return (buffer, start, end)


def get_selection_or_line(buffer, location):
    """
    If a selection exists, returns its beginning and end. Otherwise return the
    beginning and end of line.  The buffer is returned as the first field of
    the tuple
    """

    if isinstance(location, GPS.FileLocation):
        location = buffer.at(location.line(), location.column())

    buffer = location.buffer()
    start = buffer.selection_start()
    end = buffer.selection_end()
    if start == end:
        return (buffer, location.beginning_of_line(), location.end_of_line())
    else:
        return (buffer, start, end)


@interactive("Editor", "Source editor", name="Move block right")
def move_block(chars=1):
    """
    Move the current selection chars characters to the right. If chars is
    negative, moves to the left. If there is no selection, indent the current
    line.
    """

    buffer = GPS.EditorBuffer.get()
    tab_width = 8

    # Determine extents of the selection
    start_line = buffer.selection_start().line()
    end_line = buffer.selection_end().line()

    beg_loc = buffer.selection_start().beginning_of_line()
    end_loc = buffer.selection_end().end_of_line()

    had_selection = not (buffer.selection_start() == buffer.selection_end())

    if not had_selection:
        cursor_loc = buffer.current_view().cursor()
        cursor_line = cursor_loc.line()
        cursor_col = cursor_loc.column()

    end_loc = end_loc.forward_char(-1)

    text = buffer.get_chars(beg_loc, end_loc)

    newtext = []
    for line in text.split('\n'):
        if chars > 0:
            # Insert x chars at the beginning of the line
            newtext += [" " * chars + line]
        else:
            # ... remove x blanks from the beginning of the text ...

            for c in range(-chars):
                if line == "":
                    break
                if line[0] == '\t':
                    line = " " * (tab_width - 1) + line[1:]
                elif line[0] == ' ':
                    line = line[1:]
                else:
                    break
            newtext += [line]

    with buffer.new_undo_group():
        buffer.delete(beg_loc, end_loc)
        buffer.insert(buffer.at(start_line, 1), "\n".join(newtext))

    if had_selection:
        # Reselect the range of lines
        start_loc = buffer.at(start_line, 1)
        end_loc = buffer.at(end_line, 1).end_of_line()
        buffer.select(start_loc, end_loc)
    else:
        # Replace the cursor
        buffer.current_view().goto(
            buffer.at(
                cursor_line,
                max(0, cursor_col + chars)))


make_interactive(lambda: move_block(-1),
                 category="Editor", filter="Source editor",
                 name="Move block left")


@interactive("Editor", "Source editor")
@with_save_excursion
def untabify():
    """
    Replace tab characters in the current selection (or the whole buffer) with
    the correct amount of spaces. The tab stops are every n columns where n is
    specified by a preference in the Preferences dialog.
    """

    tab_width = 8
    buffer, start, end = get_selection_or_buffer()
    while start < end:
        start = start.search("\t", dialog_on_failure=False)
        if not start:
            break
        size = tab_width - ((start[0].column() - 1) % tab_width)
        replace(start[0], start[1] - 1, " " * size)
        start = start[1]


def lines_with_digit(buffer, loc, max=None):
    """
    Return an EditorLocation pointing to the last line adjacent to loc that
    contains a digit in the same column as loc. See description of serialize ()
    for an example. The search can be limited to a specific max location
    """

    if max:
        max = max.end_of_line()
    else:
        max = buffer.end_of_buffer()

    col = loc.column() - 1
    loc2 = loc.end_of_line() + 1  # to beginning of next line
    while loc2 < max:
        eol = loc2.end_of_line()
        check = loc2 + col
        if check > eol or not buffer.get_chars(check, check).isdigit():
            return loc2 - 1
        loc2 = eol + 1  # to beginning of next line

    return max


@interactive("Editor", "Source editor")
@with_save_excursion
def serialize(increment=1):
    """
    Increment a set of numbers found on adjacent lines.  The exact behavior
    depends on whether there is a current selection or not.  If there is no
    selection, then the set of lines considered is from the current line on and
    includes all adjacent lines that have at least one digit in the original
    columns. In the following example, | marks the place where the cursor is at
    the beginning:

        AAA |10 AAA
        CCC 34567 CCC
        DDD DDD

    then only the first two lines will be modified, and will become

        AAA 10 AAA
        CCC 11 CCC
        DDD DDD

    If there is a selection, all the lines in the selection are
    modified. For each line, the columns that had digits in the first
    line are modified, no matter what they actually contain. In the
    example above, if you select all three lines, the replacement becomes

        AAA 10 AAA
        CCC 11567 CCC
        DDD 12D

    ie only the fifth and sixth columns are modified since only those
    columns contained digits in the first line. This feature assumes that
    you are selecting a relevant set of lines. But it allows you to
    transform blank lines more easily. For instance, if you have

        AAA 1
        BBB
        CCC

    this is transformed into

        AAA 1
        BBB 2
        CCC 3
    """

    buffer = GPS.EditorBuffer.get()
    start = buffer.selection_start()
    end = buffer.selection_end()
    if start == end:
        has_sel = False
        start = buffer.current_view().cursor()
        end = lines_with_digit(buffer, start)
    else:
        has_sel = True
    loc = start

    # From start .. end, all lines are equal now
    end = end.end_of_line()

    # Find the range of text to replace on each line
    repl = loc
    while buffer.get_chars(repl, repl).isdigit():
        repl = repl + 1

    frm_col = loc.column() - 1    # columns start at 0 on a line
    end_col = (repl - 1).column() - 1

    try:
        value = int(buffer.get_chars(loc, repl - 1)) + increment
    except Exception:
        GPS.Console().write("Cursor must be before a number")
        return

    format = "%0" + str(end_col - frm_col + 1) + "d"

    # And now do the replacement
    repl = loc.end_of_line() + 1  # to beginning of next line
    while repl < end:
        if has_sel:
            # We had a selection: make sure the column range exists on the
            # line, and fill it with the value
            eol = repl.end_of_line()
            if repl + frm_col > eol:
                buffer.insert(eol,
                              " " * ((eol - repl) - frm_col + 2) +
                              format % value)
            else:
                replace(repl + frm_col, min(repl + end_col, eol),
                        format % value)
        else:
            # We had no selection: replace the digit, no matter how many cols
            to = repl + frm_col
            while buffer.get_chars(to, to).isdigit():
                to = to + 1
            replace(repl + frm_col, to - 1, format % value)

        repl = repl.end_of_line() + 1
        value = value + increment


@interactive("Editor", "Writable source editor", name="kill forward")
def delete_forward():
    """Delete the character just after the cursor in the current editor"""
    buffer = GPS.EditorBuffer.get()
    cursor = buffer.current_view().cursor()
    buffer.delete(cursor, cursor)


@interactive("Editor", "Writable source editor", name="Delete Line")
def delete_line():
    """
    Delete the current line and place the cursor on the beginning of the next
    line.
    """
    buffer = GPS.EditorBuffer.get()   # get the current buffer
    view = buffer.current_view()      # get the current view of this buffer
    location = view.cursor()          # get the location of the cursor

    # Get the bounds to delete
    start = location.beginning_of_line()
    end = location.end_of_line()

    # Do the deletion
    with buffer.new_undo_group():
        buffer.delete(start, end)


def kill_line(location=None, count=1):
    """
    Kills the end of the line on which LOCATION is. If LOCATION is unspecified,
    the current cursor location in the current editor is used.  If the line is
    empty or contains only white spaces, the whole line is deleted.  This is a
    better emulation of Emacs's behavior than the one provided by default by
    gtk+, which doesn't handle whitespaces correctly.  When called several
    times from the same line, entries are appended in the clipboard.  Count is
    the number of lines to delete. If greater than 1, then the whole lines are
    deleted, including newline characters.
    """

    if not location:
        location = GPS.EditorBuffer.get().current_view().cursor()

    buffer = location.buffer()
    start = location

    append = GPS.last_command() == "kill line"

    # In case the current location points to a line terminator we just cut it
    if count == 1 and start.get_char() == "\n":
        buffer.cut(start, start, append)
    else:
        bol = start
        for line in range(1, count + 1):
            end = bol.end_of_line()
            str = buffer.get_chars(start, end)
            strip_str = str.rstrip()
            if (count == 1 and
               len(str) > 0 and
               str[len(str) - 1] == '\n' and strip_str != ""):
                end = end.forward_char(-1)
            bol = end + 1

        buffer.cut(start, end, append)

################################################
# Moving the cursor
################################################


@interactive("Editor", "Source editor",
             name="goto beginning of buffer")
def beginning_of_buffer():
    """Move the cursor to the beginning of the buffer"""
    buffer = GPS.EditorBuffer.get()
    buffer.current_view().goto(buffer.beginning_of_buffer(),
                               should_extend_selection)


@interactive("Editor", "Source editor", name="goto end of buffer")
def end_of_buffer():
    """Move the cursor to the end of the buffer"""
    buffer = GPS.EditorBuffer.get()
    buffer.current_view().goto(
        buffer.end_of_buffer(), should_extend_selection)


def _goto_line_bound(beginning, extend_selection):
    """
    Move the cursor in the current focus widget to various places in the line:
    * if beginning is True, move to the beginning of the line
      If the cursor is already in column 1, move to the first non-blank
      character on the line when in a GPS code editor.
    * else move to the end of the line
    """

    from pygps import get_widgets_by_type
    widget = get_focused_widget()

    # When in a standard Gtk_Entry field:

    if isinstance(widget, Gtk.Entry):
        bounds = widget.get_selection_bounds()
        cursor_pos = widget.get_position()
        if bounds:
            start_pos = bounds[0] if bounds[1] == cursor_pos else bounds[1]
        else:
            start_pos = widget.get_position()
        end_pos = 0 if beginning else widget.get_text_length()

        if extend_selection:
            widget.select_region(start_pos, end_pos)
        else:
            widget.set_position(end_pos)

        return

    elif not isinstance(widget, Gtk.TextView):
        # We don't know how to handle these other widgets
        return

    else:
        # We are in a GPS code editor or standard Gtk.TextView

        ed = GPS.EditorBuffer.get(open=False)
        if ed:
            gtk_ed_view = get_widgets_by_type(
                Gtk.TextView, ed.current_view().pywidget())[0]

        if not ed or gtk_ed_view != widget:
            # in a Gtk.TextView, but not a GPS code editor

            b = widget.get_buffer()
            it = b.get_iter_at_mark(b.get_mark("insert"))
            if beginning:
                if it.get_line_index() == 0:
                    # Already at beginning ? move to first non blank
                    while it.get_char() in (u' ', u'\t'):
                        it.forward_char()

                else:
                    b.place_cursor(b.get_iter_at_line_offset(it.get_line(), 0))

            else:
                it.forward_to_line_end()
                b.place_cursor(it)

        else:
            for c in ed.cursors():
                d = c.mark().location()

                if beginning:
                    if d.column() == 1:
                        # Already at beginning ? move to first non blank

                        while d.get_char() in (u' ', u'\t'):
                            d = d.forward_char(1)
                    else:
                        d = d.beginning_of_line()

                else:
                    d = d.end_of_line()

                c.move(d, extend_selection or ed.extend_existing_selection)


@interactive("Editor", filter_text_actions,
             name="goto beginning of line (extend selection)")
def goto_beginning_of_line_ext_sel():
    """
    Move the cursor to the beginning of the line:
    * if the cursor is anywhere within the line, move back to column 1
    * if the cursor is already on column 1, move to the first non-blank
      character of the line.
    This function extends the current selection while moving the cursor.
    """
    _goto_line_bound(beginning=True, extend_selection=True)


@interactive("Editor", filter_text_actions,
             name="goto beginning of line", for_learning=True)
def goto_beginning_of_line():
    """
    Move the cursor to the beginning of the line:
    * if the cursor is anywhere within the line, move back to column 1
    * if the cursor is already on column 1, move to the first non-blank
      character of the line.
    """
    _goto_line_bound(beginning=True, extend_selection=False)


@interactive("Editor", filter_text_actions,
             name="goto end of line (extend selection)")
def goto_end_of_line_ext_sel():
    _goto_line_bound(beginning=False, extend_selection=True)


@interactive("Editor", filter_text_actions, name="goto end of line",
             for_learning=True)
def goto_end_of_line():
    _goto_line_bound(beginning=False, extend_selection=False)


def end_of_line(file, line):
    """Goto to the end of the line in file"""
    buffer = GPS.EditorBuffer.get(GPS.File(file))
    loc = buffer.at(line, 1)
    buffer.current_view().goto(loc.end_of_line() - 1)


@interactive("Editor", "Writable source editor", name="forward delete")
def forward_delete():
    delete(forward=True)


@interactive("Editor", "Writable source editor", name="backward delete")
def backward_delete():
    e = GPS.EditorBuffer.get()
    cursor = e.selection_start()
    end = e.selection_end()

    # if it's python code and no block is selected

    if e.file().language() == "python":
        if end.line() == cursor.line() and end.column() == cursor.column():

            did = False

            # not include the first position
            if cursor.column() != 1:

                # see if I should forward deletion by 4
                try:
                    did = python_forward_indent(e, cursor)
                except Exception:
                    pass

            # if justice(auto indent by 4) has been done
            if did:
                return

    # otherwise python-indent not done, behave normally
    delete(forward=False)


def python_forward_indent(e, cursor):
    """
       Indent with backspace in the leading white spaces with 4
       * e is EditorBuffer
       * cursor is EditorLocation of cursor
    """
    line = e.get_chars(cursor.beginning_of_line(), cursor.end_of_line())
    spaces_len = len(line) - len(line.lstrip(" "))
    indent = 4 if spaces_len % 4 == 0 else spaces_len % 4
    # if cursor is in the middle of the leading whitespaces

    if spaces_len > 0 and spaces_len >= cursor.column()-1:

        # remove 4 blanks if possible
        e.delete(e.at(cursor.line(), 1), e.at(cursor.line(), indent))

        # adjust cursor position
        e.main_cursor().move(e.at(cursor.line(), cursor.column()-indent))

        return True

    return False


def delete(forward=True):
    """
    Helper for backward_delete/forward_delete actions, to factorize common
    functionality. Will delete the selection if there is one, or one character
    backward/forward depending on the forward parameter
    """
    ed = GPS.EditorBuffer.get()
    mc = ed.main_cursor()

    def do():
        def _delete(s, e):
            s, e = (s, e) if s < e else (e, s)
            ed.delete(s, e.forward_char(-1))

        no_selection = mc.mark().location() == mc.sel_mark().location()
        for c in ed.cursors():
            try:
                if no_selection:
                    start = c.mark().location()
                    end = start.forward_char(1 if forward else -1)
                    if end and start != end:
                        _delete(start, end)
                else:
                    _delete(c.mark().location(), c.sel_mark().location())
            except GPS.Exception:
                return

    if ed.has_slave_cursors():
        with ed.new_undo_group():
            do()
    else:
        do()


def is_space(char):
    return char == ' ' or char == '\t'


def goto_word_start(loc, underscore_is_word=True):
    """
    Move to the beginning of the current word (or leave the cursor where it
    is). This properly handles '_'
    """
    if underscore_is_word:
        while not loc.starts_word():
            loc = loc.forward_word(-1)
        return loc
    else:
        while not loc.starts_word():
            prev = loc
            loc = loc.forward_char(-1)
            c = loc.get_char()
            if c == '_':
                return prev
        return loc


def goto_word_end(loc, underscore_is_word=True):
    if underscore_is_word:
        while True:
            loc = loc.forward_word()
            try:
                if loc.get_char() != '_':
                    return loc.forward_char(-1)
            except Exception:
                return loc.buffer().end_of_buffer()

    else:
        while not loc.ends_word():
            prev = loc
            loc = loc.forward_char(1)
            try:
                if loc.get_char() == '_':
                    return prev
            except Exception:
                # Probably an invalid position.
                return loc.buffer().end_of_buffer()
        return loc


def isword(a):
    # test if a (a char) is a word
    return (a.isalpha() or a.isdigit() or a == "_")


@interactive("Editor", "", name="go to next word")
def move_to_next_word():
    """
    Jump to beginning of the next word / the end of this word
    [word)[word)jump_here[word)...
    par"""
    b = GPS.EditorBuffer.get()
    loc = b.selection_end()
    if isword(loc.get_char()):
        loc = forward_until(loc, lambda x: not isword(x),
                            True, True, False, False)
    else:
        loc = forward_until(loc, lambda x: isword(x) or x in ["\n", "\"", "'"],
                            True, False, False, False)
    if loc.get_char() not in ["\n", "\"", "'"]:
        loc = forward_until(loc, lambda x: x != " ", False, True, False, False)
    b.main_cursor().move(loc)


@interactive("Editor", "", name="go to previous word")
def move_to_previous_word():
    b = GPS.EditorBuffer.get()
    loc = b.selection_start().forward_char(-1)
    loc = forward_until(loc, lambda x: x != " ", False, True, True)
    if loc.get_char() not in ["\n", "\"", "'"]:
        if isword(loc.get_char()):
            loc = forward_until(loc, lambda x: not isword(x),
                                True, True, True, False)
        else:
            loc = forward_until(loc, lambda x: isword(x) or x.isspace(),
                                True, False, True, False)
        loc = loc.forward_char() if loc != b.beginning_of_buffer() else loc
    b.main_cursor().move(loc)


def delete_spaces(backward=True, forward=True, leave_one=False):
    """Delete all spaces around cursor, possibly leaving one"""
    buffer = GPS.EditorBuffer.get()
    start = buffer.current_view().cursor()
    end = start
    if forward:
        max = end.end_of_line()
        while is_space(end.get_char()) and end < max:
            end = end + 1
        end = end - 1
    if backward:
        max = start.beginning_of_line()
        start = start - 1
        while is_space(start.get_char()) and start >= max:
            start = start - 1
        start = start + 1
    if start <= end:
        buffer.delete(start, end)

    if leave_one:
        buffer.insert(start, " ")


@interactive("Editor", "Writable source editor",
             name="delete horizontal space")
@with_save_excursion
def delete_horizontal_space(backward=1, forward=1):
    """
    Delete all spaces and tabs around the cursor in the current editor.  The
    two parameters can be used to control in what directions white spaces are
    searched for.
    """
    delete_spaces(leave_one=False)


@interactive("Editor", "Writable source editor", name="just one space")
@with_save_excursion
def just_one_space():
    """
    Delete all spaces and tabs around the cursor, leaving one space.  If there
    are no spaces around, a new space is inserted.
    """
    delete_spaces(leave_one=True)


@interactive("Editor", "Writable source editor", name="transpose chars")
def transpose_chars():
    """Transpose characters around cursor, moving forward one character."""
    buffer = GPS.EditorBuffer.get()
    cursor = buffer.current_view().cursor()
    if cursor > buffer.beginning_of_buffer():
        c = cursor.get_char()
        with buffer.new_undo_group():
            buffer.delete(cursor, cursor)
            buffer.insert(cursor - 1, c)
            buffer.current_view().goto(cursor + 1)


@interactive("Editor", "Writable source editor", name="Transpose lines")
def transpose_lines(location=None):
    """
    Transpose the line at LOCATION (or current line) and the previous one,
    leaving the cursor after both.
    """
    if not location:
        location = GPS.EditorBuffer.get().current_view().cursor()
    buffer = location.buffer()
    if location.line() < buffer.lines_count():
        with buffer.new_undo_group():
            start = location.beginning_of_line()
            end = location.end_of_line()
            text = buffer.get_chars(start, end)
            buffer.delete(start, end)
            buffer.insert(start.forward_line(-1), text)
            buffer.current_view().goto(start.end_of_line() + 1)


@interactive("Editor", "Writable source editor", name="open line")
@with_save_excursion
def open_line():
    """Insert a newline and leave cursor at its current place."""
    buffer = GPS.EditorBuffer.get()
    buffer.insert(buffer.current_view().cursor(), "\n")


@interactive("Editor", "Writable source editor", name="Join line")
def join_line():
    """
    Join the current line and the following one, separated by a single space,
    and leaves the cursor on the space
    """
    buffer = GPS.EditorBuffer.get()
    eol = buffer.current_view().cursor().end_of_line()
    with buffer.new_undo_group():
        buffer.current_view().goto(eol)
        buffer.delete(eol, eol)  # Newline character
        delete_spaces(backward=False, forward=True, leave_one=False)
        if not is_space(eol.forward_char(-1).get_char()):
            buffer.insert(eol, " ")


def apply_func_to_word(func, location=None):
    """
    Apply a function to the current word (starting at the current character).
    FUNC takes one argument, the text it replaces, and should return the
    replacement text
    """
    if not location:
        location = GPS.EditorBuffer.get().current_view().cursor()
    buffer = location.buffer()
    with buffer.new_undo_group():
        end = location.forward_word()
        text = func(buffer.get_chars(location, end))
        replace(location, end, text)


@interactive("Editor", "Writable source editor", name="Upper case word")
def upper_case_word(location=None):
    """Upper case the current word (starting at the current character)"""
    apply_func_to_word(str.upper, location)


@interactive("Editor", "Writable source editor", name="Lower case word")
def lower_case_word(location=None):
    """Lower case the current word (starting at the current character)"""
    apply_func_to_word(str.lower, location)


@interactive("Editor", "Writable source editor", name="Capitalize word")
def capitalize_case_word(location=None):
    """Capitalize the current word (starting at the current character)"""
    apply_func_to_word(str.capitalize, location)


@interactive("Editor", "Writable source editor", name="Center line")
def center_line():
    """
    Center the current line on the screen. If a comment line then the text
    inside the comment is centered, the comment markers remain unchanged.
    """
    buffer = GPS.EditorBuffer.get()
    location = buffer.current_view().cursor()
    initial = location.create_mark()

    with buffer.new_undo_group():
        start = location.beginning_of_line()
        end = location.end_of_line()
        text = buffer.get_chars(start, end)
        if text[0:2] == "--" or text[0:2] == "//" or text[0:2] == "##":
            start = start + 2

        if text[-3:] == "--\n" or text[-3:] == "//\n" or text[-3:] == "##\n":
            # Use right comment characters to center the text
            end = end - 3
            text = buffer.get_chars(start, end).strip()
            spaces = end.column() - start.column() + 1 - len(text)
            before = spaces / 2
            after = spaces / 2
            if before + after != spaces:
                after = after + 1
            buffer.delete(start, end)
            buffer.insert(start, ' ' * before + text + ' ' * after)
        else:
            # No right comment characters, use the highlight column to center
            # the text
            col = GPS.Preference("Src-Editor-Highlight-Column").get()
            text = buffer.get_chars(start, end).strip()
            spaces = int(col) - start.column() - len(text)
            before = spaces / 2
            buffer.delete(start, end - 1)
            buffer.insert(start, ' ' * before + text)

        # Move to next line
        buffer.current_view().goto(GPS.EditorLocation
                                   (buffer,
                                    line=initial.location().forward_line(
                                        1).line(),
                                    column=location.column()))


class BlockIterator (six.Iterator):

    """
    An iterator for the various sections of an editor.
    Each step in the iteration returns a tuple (start, end) of EditorLocation
    instances for the section.
    The constructor parameter overlay_name can be one of:
        - "":          The whole buffer is returned
        - "selection": The current selection in the buffer is returned
        - "word":      The current word in the buffer is returned
        - overlay name: All sections for which this overlay applies are
                       returned. The name could be one of "comment",
                       "keywords", "string" or "character"
    Example of use:
        buffer = EditorBuffer.get()
        for start, end in BlockIterator (buffer, "comment"):
           ...
    """

    def __init__(self, buffer, overlay_name):
        self.mark = buffer.beginning_of_buffer().create_mark()
        if overlay_name != "" \
                and overlay_name != "selection" \
                and overlay_name != "word":
            self.overlay = buffer.create_overlay(overlay_name)
            self.in_comment = \
                buffer.beginning_of_buffer().has_overlay(self.overlay)
        else:
            self.overlay = None
            self.overlay_name = overlay_name

    def __iter__(self):
        return self

    def __next__(self):
        loc = self.mark.location()
        if not self.overlay:
            if loc < loc.buffer().end_of_buffer():
                self.mark.move(loc.buffer().end_of_buffer())
                if self.overlay_name == "selection":
                    return (loc.buffer().selection_start(),
                            loc.buffer().selection_end())
                elif self.overlay_name == "word":
                    cursor = loc.buffer().current_view().cursor()
                    start = cursor
                    while not start.starts_word():
                        start = start - 1
                    while not cursor.ends_word():
                        cursor = cursor + 1
                    return (start, cursor)
                else:
                    return (loc.buffer().beginning_of_buffer(),
                            loc.buffer().end_of_buffer())
            raise StopIteration
        else:
            # Find beginning of next section
            if not loc.has_overlay(self.overlay):
                loc = loc.forward_overlay(self.overlay)

            if loc >= loc.buffer().end_of_buffer():
                raise StopIteration

            loc2 = loc.forward_overlay(self.overlay)
            self.mark.move(loc2 + 1)
            return (loc, loc2 - 1)


class WordIterator (six.Iterator):

    """
    An iterator for all words in a block. Each iteration returns a tuple
    (start, end) of EditorLocation instances.
    Example of use:
      buffer = EditorBuffer.get()
      for blockStart, blockEnd in BlockIterator (buffer, "comments"):
         for wordStart, wordEnd in WordIterator (blockStart, blockEnd):
            ...
    """

    def __init__(self, start, end):
        self.mark = start.create_mark()
        self.end = end

    def __iter__(self):
        return self

    def starts_at(self, loc):
        self.mark.move(loc)

    def __next__(self):
        loc = self.mark.location()
        while loc < self.end:
            loc2 = loc.forward_word()
            if loc.get_char().isalpha():
                # Use a mark, in case the buffer is modified
                self.mark.move(loc2 + 1)
                return (loc, loc2 - 1)
            else:
                loc = loc + 1
        raise StopIteration


class LineIterator (six.Iterator):

    """
    An iterator for all lines in a block. Each iteration returns a
    tuple (start, end) of EditorLocation instances.
    """

    def __init__(self, start, end):
        self.mark = start.create_mark()
        self.end = end.create_mark()

    def __iter__(self):
        return self

    def __next__(self):
        loc = self.mark.location()
        if loc >= self.end.location():
            raise StopIteration
        loc2 = loc.end_of_line()
        if loc2 >= self.end.location():
            self.mark.move(self.end.location() + 1)
            return (loc, self.end.location())
        else:
            self.mark.move(loc2 + 1)
            return (loc, loc2)


# Emulating Emacs selection:
# In Emacs, one sets the mark first, then when the cursor is moved the
# selection is extended appropriately. This is rather tricky to emulate
# in gtk+.
# There are two implementations: when pygtk is available, we simply
# temporarily override the key bindings so that the selection is always
# extended. This avoids all flickering, has no run-time cost, and is
# certainly the nicest. Not quite perfect though, since other functions
# that move the cursor will not extend the selection, only the basic
# key bindings defined for a gkt.TextView do.
# However, if pygtk is not available, we emulate it by monitoring all
# location changes. The slow down is almost invisible, but since the
# selection is first cancelled by gtk+ when the cursor is moved, and we
# then reselect it, there is some flickering

HOME = 65360
LEFT = 65361
UP = 65362
RIGHT = 65363
DOWN = 65364
PAGE_UP = 65365
PAGE_DOWN = 65366
END = 65367

KP_HOME = 65429
KP_LEFT = 65430
KP_UP = 65431
KP_RIGHT = 65432
KP_DOWN = 65433
KP_PAGE_UP = 65434
KP_PAGE_DOWN = 65435
KP_END = 65436


def override_key_bindings(select):
    """Override the default TextView keybinding to either always force
       the extension the selection, or not"""

    global should_extend_selection

    Gtk.TextView()   # make sure the BindingSet was created
    bind = Gtk.binding_set_find("GtkTextView")

    def override(key, mvt, step):
        # pygobject does not have a binding to
        # gtk_binding_entry_add_signal, which would be more convenient and
        # efficient than going through a string.
        # Gtk.binding_entry_remove(bind, key, modifier)
        subst = (key, mvt, step, 1 if select else 0)
        Gtk.binding_entry_add_signal_from_string(
            bind, 'bind "%s" {"move_cursor" (%s,%s,%s)}' % subst)

    should_extend_selection = select

    override("Right",    "visual-positions", 1)
    override("KP_Right", "visual-positions", 1)
    override("Left",     "visual-positions", -1)
    override("KP_Left",  "visual-positions", -1)

    override("<ctrl>Right", "words", 1)
    override("<ctrl>KP_Right", "words", 1)
    override("<ctrl>Left", "words", -1)
    override("<ctrl>KP_Left", "words", -1)

    override("Up", "display-lines", -1)
    override("KP_Up", "display-lines", -1)
    override("Down", "display-lines", 1)
    override("KP_Down", "display-lines", 1)

    override("<ctrl>Up", "paragraph", -1)
    override("<ctrl>KP_Up", "paragraph", -1)
    override("<ctrl>Down", "paragraph", 1)
    override("<ctrl>KP_Down", "paragraph", 1)

    override("Home", "display-line-ends", -1)
    override("KP_Home", "display-line-ends", -1)
    override("End", "display-line-ends", 1)
    override("KP_End", "display-line-ends", 1)

    override("<ctrl>Home", "buffer-ends", -1)
    override("<ctrl>KP_Home", "buffer-ends", -1)
    override("<ctrl>End", "buffer-ends", 1)
    override("<ctrl>KP_End", "buffer-ends", 1)

    override("Page_Up", "pages", -1)
    override("KP_Page_Up", "pages", -1)
    override("Page_Down", "pages", 1)
    override("KP_Page_Down", "pages", 1)

    override("<ctrl>Page_Up", "horizontal-pages", -1)
    override("<ctrl>KP_Page_Up", "horizontal-pages", -1)
    override("<ctrl>Page_Down", "horizontal-pages", 1)
    override("<ctrl>KP_Page_Down", "horizontal-pages", 1)


prev_char = ''  # To pre-fill the dialog with the last char


@interactive("Editor", "Source editor", name="insert extended character")
def insert_extended_character(location=None):
    """
    Present a dialog asking for a character codepeoint (in decimal), and
    insert the character at the cursor location in the current editor.
    """
    global prev_char
    if location:
        buffer = location.buffer()
    else:
        buffer = GPS.EditorBuffer.get()
        location = buffer.current_view().cursor()

    r = GPS.MDI.input_dialog("Insert Extended Character",
                             "Character code={}".format(prev_char))

    try:
        prev_char = r[0]
        num = int(r[0].strip())
    except Exception:
        GPS.Console().write("Please enter a decimal number")
        return

    buffer.insert(location, unichr(num))


@interactive("Editor", "Source editor", name="set mark command")
def set_mark_command(location=None):
    """
    Set mark at LOCATION (or current cursor if LOCATION is unspecified) This is
    similar to Emacs's behavior: a mark is put at the current cursor position.
    You can then move the cursor elsewhere, and delete the text between this
    mark and the new cursor position. See also the action 'Cancel mark
    command'
    """
    if not location:
        location = GPS.EditorBuffer.get().current_view().cursor()

    location.buffer().extend_existing_selection = True

    location.create_mark("selection_bound")
    override_key_bindings(select=True)


@interactive("Editor", "Source editor", name="Cancel mark command")
def cancel_mark_command(buffer=None):
    """
    Cancel the mark in BUFFER Remove the emacs-emulation mark in the current
    editor. See also the action 'Set mark command'
    """
    if not buffer:
        buffer = GPS.EditorBuffer.get()

    buffer.extend_existing_selection = False

    try:
        buffer.unselect()
        override_key_bindings(select=False)
    except Exception:
        pass  # No such mark


@hook("clipboard_changed")
def __on_clipboard_changed():
    """Called when the contents of the clipboard has changed"""
    if transient_mark_pref.get():
        cancel_mark_command()


@interactive(name='New View Horizontal reuse', category='MDI')
def new_view_horizontal_reuse():
    """
    When on an editor, splits the current notebook into two side-by-side
    windows, so that the two windows show two views of the same file.

    If another window already exists to the side, a new view is created
    inside that existing notebook, rather than create a new one.
    """
    GPS.MDI.current().split(vertically=False, new_view=True, reuse=True)


@interactive(name='New View Horizontal', category='MDI')
def new_view_horizontal():
    """
    When on an editor, splits the current notebook into two side-by-side
    windows, so that the two windows show two views of the same file.
    """
    GPS.MDI.current().split(vertically=False, new_view=True, reuse=False)


@interactive(name='New View Vertical reuse', category='MDI')
def new_view_vertical_reuse():
    """
    When on an editor, splits the current notebook into two windows
    vertically, so that the two windows show two views of the same file.

    If another window already exists above or below, a new view is created
    inside that existing notebook, rather than create a new one
    """
    GPS.MDI.current().split(vertically=True, new_view=True, reuse=True)


@interactive(name='New View Vertical', category='MDI')
def new_view_vertical():
    """
    When on an editor, splits the current notebook into two windows
    vertically, so that the two windows show two views of the same file.
    """
    GPS.MDI.current().split(vertically=True, new_view=True, reuse=False)


GPS.parse_xml("""
   <action name="kill line" output="none" category="Editor">
      <description>
      This is similar to Emacs' kill-line function. It deletes the end of the
      line after the cursor's current column. If the cursor is at the
      end of the line, it deletes the newline character and therefore
      joins the current line and the next.  The text that is deleted is
      copied to the clipboard. If you call this action multiple times
      from the same location, all deleted text is merged into a single
      clipboard, so that a single Paste will put it all back.  When
      this command is executed after a repeat_next command, the whole
      line is deleted to provide a more intuitive behavior.
      </description>
      <filter id="Source editor" />
      <shell lang="python">
if $repeat == 1: text_utils.kill_line(None, $remaining+1)
      </shell>
   </action>
""")