-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathconque.py
1152 lines (789 loc) · 34.4 KB
/
conque.py
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
# FILE: autoload/conque_term/conque.py
# AUTHOR: Nico Raffo <[email protected]>
# WEBSITE: http://conque.googlecode.com
# MODIFIED: 2011-09-02
# VERSION: 2.3, for Vim 7.0
# LICENSE:
# Conque - Vim terminal/console emulator
# Copyright (C) 2009-2011 Nico Raffo
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
Vim terminal emulator.
This class is the main interface between Vim and the terminal application. It
handles both updating the Vim buffer with new output and accepting new keyboard
input from the Vim user.
Although this class was originally designed for a Unix terminal environment, it
has been extended by the ConqueSole class for Windows.
Usage:
term = Conque()
term.open('/bin/bash', {'TERM': 'vt100'})
term.write("ls -lha\r")
term.read()
term.close()
"""
import vim
import re
import math
class Conque:
# screen object
screen = None
# subprocess object
proc = None
# terminal dimensions and scrolling region
columns = 80 # same as $COLUMNS
lines = 24 # same as $LINES
working_columns = 80 # can be changed by CSI ? 3 l/h
working_lines = 24 # can be changed by CSI r
# top/bottom of the scroll region
top = 1 # relative to top of screen
bottom = 24 # relative to top of screen
# cursor position
l = 1 # current cursor line
c = 1 # current cursor column
# autowrap mode
autowrap = True
# absolute coordinate mode
absolute_coords = True
# tabstop positions
tabstops = []
# enable colors
enable_colors = True
# color changes
color_changes = {}
# color history
color_history = {}
# color highlight cache
highlight_groups = {}
# prune terminal colors
color_pruning = True
# don't wrap table output
unwrap_tables = True
# wrap CUF/CUB around line breaks
wrap_cursor = False
# do we need to move the cursor?
cursor_set = False
# current character set, ascii or graphics
character_set = 'ascii'
# used for auto_read actions
read_count = 0
# input buffer, array of ordinals
input_buffer = []
def open(self):
""" Start program and initialize this instance.
Arguments:
command -- Command string to execute, e.g. '/bin/bash --login'
options -- Dictionary of environment vars to set and other options.
"""
# get arguments
command = vim.eval('command')
options = vim.eval('options')
# create terminal screen instance
self.screen = ConqueScreen()
# int vars
self.columns = vim.current.window.width
self.lines = vim.current.window.height
self.working_columns = vim.current.window.width
self.working_lines = vim.current.window.height
self.bottom = vim.current.window.height
# offset first line to make room for startup messages
if int(options['offset']) > 0:
self.l = int(options['offset'])
# init color
self.enable_colors = options['color'] and not CONQUE_FAST_MODE
# init tabstops
self.init_tabstops()
# open command
self.proc = ConqueSubprocess()
self.proc.open(command, {'TERM': options['TERM'], 'CONQUE': '1', 'LINES': str(self.lines), 'COLUMNS': str(self.columns)})
# send window size signal, in case LINES/COLUMNS is ignored
self.update_window_size(True)
def write(self, input, set_cursor=True, read=True):
""" Write a unicode string to the subprocess.
set_cursor -- Position the cursor in the current buffer when finished
read -- Check program for new output when finished
"""
# write and read
self.proc.write(input)
# read output immediately
if read:
self.read(1, set_cursor)
def write_ord(self, input, set_cursor=True, read=True):
""" Write a single character to the subprocess, using an unicode ordinal. """
if CONQUE_PYTHON_VERSION == 2:
self.write(unichr(input), set_cursor, read)
else:
self.write(chr(input), set_cursor, read)
def write_expr(self, expr, set_cursor=True, read=True):
""" Write the value of a Vim expression to the subprocess. """
if CONQUE_PYTHON_VERSION == 2:
try:
val = vim.eval(expr)
self.write(unicode(val, CONQUE_VIM_ENCODING, 'ignore'), set_cursor, read)
except:
pass
else:
try:
# XXX - Depending on Vim to deal with encoding, sadly
self.write(vim.eval(expr), set_cursor, read)
except:
pass
def write_latin1(self, input, set_cursor=True, read=True):
""" Write latin-1 string to conque. Very ugly, shood be removed. """
# XXX - this whole method is a hack, to be removed soon
if CONQUE_PYTHON_VERSION == 2:
try:
input_unicode = input.decode('latin-1', 'ignore')
self.write(input_unicode.encode('utf-8', 'ignore'), set_cursor, read)
except:
return
else:
self.write(input, set_cursor, read)
def write_buffered_ord(self, chr):
""" Add character ordinal to input buffer. In case we're not allowed to modify buffer a time of input. """
self.input_buffer.append(chr)
def read(self, timeout=1, set_cursor=True, return_output=False, update_buffer=True):
""" Read new output from the subprocess and update the Vim buffer.
Arguments:
timeout -- Milliseconds to wait before reading input
set_cursor -- Set the cursor position in the current buffer when finished
return_output -- Return new subprocess STDOUT + STDERR as a string
update_buffer -- Update the current Vim buffer with the new output
This method goes through the following rough steps:
1. Get new output from subprocess
2. Split output string into control codes, escape sequences, or plain text
3. Loop over and process each chunk, updating the Vim buffer as we go
"""
output = ''
# this may not actually work
try:
# read from subprocess and strip null characters
output = self.proc.read(timeout)
if output == '':
return
# for bufferless terminals
if not update_buffer:
return output
# strip null characters. I'm still not sure why they appear
output = output.replace(chr(0), '')
# split input into individual escape sequences, control codes, and text output
chunks = CONQUE_SEQ_REGEX.split(output)
# if there were no escape sequences, skip processing and treat entire string as plain text
if len(chunks) == 1:
self.plain_text(chunks[0])
# loop through and process escape sequences
else:
for s in chunks:
if s == '':
continue
# Check for control character match
if CONQUE_SEQ_REGEX_CTL.match(s[0]):
nr = ord(s[0])
if nr in CONQUE_CTL:
getattr(self, 'ctl_' + CONQUE_CTL[nr])()
else:
pass
# check for escape sequence match
elif CONQUE_SEQ_REGEX_CSI.match(s):
if s[-1] in CONQUE_ESCAPE:
csi = self.parse_csi(s[2:])
getattr(self, 'csi_' + CONQUE_ESCAPE[s[-1]])(csi)
else:
pass
# check for title match
elif CONQUE_SEQ_REGEX_TITLE.match(s):
self.change_title(s[2], s[4:-1])
# check for hash match
elif CONQUE_SEQ_REGEX_HASH.match(s):
if s[-1] in CONQUE_ESCAPE_HASH:
getattr(self, 'hash_' + CONQUE_ESCAPE_HASH[s[-1]])()
else:
pass
# check for charset match
elif CONQUE_SEQ_REGEX_CHAR.match(s):
if s[-1] in CONQUE_ESCAPE_CHARSET:
getattr(self, 'charset_' + CONQUE_ESCAPE_CHARSET[s[-1]])()
else:
pass
# check for other escape match
elif CONQUE_SEQ_REGEX_ESC.match(s):
if s[-1] in CONQUE_ESCAPE_PLAIN:
getattr(self, 'esc_' + CONQUE_ESCAPE_PLAIN[s[-1]])()
else:
pass
# else process plain text
else:
self.plain_text(s)
# set cusor position
if set_cursor:
self.screen.set_cursor(self.l, self.c)
# we need to set the cursor position
self.cursor_set = False
except:
pass
if return_output:
if CONQUE_PYTHON_VERSION == 3:
return output
else:
return output.encode(CONQUE_VIM_ENCODING, 'replace')
def auto_read(self):
""" Poll program for more output.
Since Vim doesn't have a reliable event system that can be triggered when new
output is available, we have to continually poll the subprocess instead. This
method is called many times a second when the terminal buffer is active, so it
needs to be very fast and efficient.
The feedkeys portion is required to reset Vim's timer system. The timer is used
to execute this command, typically set to go off after 50 ms of inactivity.
"""
# process buffered input if any
if len(self.input_buffer):
for chr in self.input_buffer:
self.write_ord(chr, set_cursor=False, read=False)
self.input_buffer = []
self.read(1)
# check subprocess status, but not every time since it's CPU expensive
if self.read_count % 32 == 0:
if not self.proc.is_alive():
vim.command('call conque_term#get_instance().close()')
return
if self.read_count > 512:
self.read_count = 0
# trim color history occasionally if desired
if self.enable_colors and self.color_pruning:
self.prune_colors()
# ++
self.read_count += 1
# read output
self.read(1)
# reset timer
if self.c == 1:
vim.command('call feedkeys("\<right>\<left>", "n")')
else:
vim.command('call feedkeys("\<left>\<right>", "n")')
# stop here if cursor doesn't need to be moved
if self.cursor_set:
return
# check if window size has changed
if not CONQUE_FAST_MODE:
self.update_window_size()
# otherwise set cursor position
try:
self.set_cursor(self.l, self.c)
except:
pass
self.cursor_set = True
def plain_text(self, input):
""" Write text output to Vim buffer.
This method writes a string of characters without any control characters or escape sequences
to the Vim buffer. In simple terms, it writes the input string to the buffer starting at the
current cursor position, wrapping the text to a new line if needed. It also triggers the
terminal coloring methods if needed.
"""
# translate input into graphics character set if needed
if self.character_set == 'graphics':
old_input = input
input = u('')
for i in range(0, len(old_input)):
chrd = ord(old_input[i])
try:
if chrd > 255:
input = input + old_input[i]
else:
input = input + uchr(CONQUE_GRAPHICS_SET[chrd])
except:
pass
# get current line from Vim buffer
current_line = self.screen[self.l]
# pad current line with spaces, if it's shorter than cursor position
if len(current_line) < self.c:
current_line = current_line + ' ' * (self.c - len(current_line))
# if line is wider than screen
if self.c + len(input) - 1 > self.working_columns:
# Table formatting hack
if self.unwrap_tables and CONQUE_TABLE_OUTPUT.match(input):
self.screen[self.l] = current_line[:self.c - 1] + input + current_line[self.c + len(input) - 1:]
self.apply_color(self.c, self.c + len(input))
self.c += len(input)
return
diff = self.c + len(input) - self.working_columns - 1
# if autowrap is enabled
if self.autowrap:
self.screen[self.l] = current_line[:self.c - 1] + input[:-1 * diff]
self.apply_color(self.c, self.working_columns)
self.ctl_nl()
self.ctl_cr()
remaining = input[-1 * diff:]
self.plain_text(remaining)
else:
self.screen[self.l] = current_line[:self.c - 1] + input[:-1 * diff - 1] + input[-1]
self.apply_color(self.c, self.working_columns)
self.c = self.working_columns
# no autowrap
else:
self.screen[self.l] = current_line[:self.c - 1] + input + current_line[self.c + len(input) - 1:]
self.apply_color(self.c, self.c + len(input))
self.c += len(input)
def apply_color(self, start, end, line=0):
""" Apply terminal colors to buffer for a range of characters in a single line.
When a text attribute escape sequence is encountered during input processing, the
attributes are recorded in the dictionary self.color_changes. After those attributes
have been applied, the changes are recorded in a second dictionary self.color_history.
This method inspects both dictionaries to calculate any syntax highlighting
that needs to be executed to render the text attributes in the Vim buffer.
"""
# stop here if coloration is disabled
if not self.enable_colors:
return
# allow custom line nr to be passed
if line:
buffer_line = line
else:
buffer_line = self.get_buffer_line(self.l)
# check for previous overlapping coloration
to_del = []
if buffer_line in self.color_history:
for i in range(len(self.color_history[buffer_line])):
syn = self.color_history[buffer_line][i]
if syn['start'] >= start and syn['start'] < end:
vim.command('syn clear ' + syn['name'])
to_del.append(i)
# outside
if syn['end'] > end:
self.exec_highlight(buffer_line, end, syn['end'], syn['highlight'])
elif syn['end'] > start and syn['end'] <= end:
vim.command('syn clear ' + syn['name'])
to_del.append(i)
# outside
if syn['start'] < start:
self.exec_highlight(buffer_line, syn['start'], start, syn['highlight'])
# remove overlapped colors
if len(to_del) > 0:
to_del.reverse()
for di in to_del:
del self.color_history[buffer_line][di]
# if there are no new colors
if len(self.color_changes) == 0:
return
# build the color attribute string
highlight = ''
for attr in self.color_changes.keys():
highlight = highlight + ' ' + attr + '=' + self.color_changes[attr]
# execute the highlight
self.exec_highlight(buffer_line, start, end, highlight)
def exec_highlight(self, buffer_line, start, end, highlight):
""" Execute the Vim commands for a single syntax highlight """
syntax_name = 'ConqueHighLightAt_%d_%d_%d_%d' % (self.proc.pid, self.l, start, len(self.color_history) + 1)
syntax_options = 'contains=ALLBUT,ConqueString,MySQLString,MySQLKeyword oneline'
syntax_region = 'syntax match %s /\%%%dl\%%>%dc.\{%d}\%%<%dc/ %s' % (syntax_name, buffer_line, start - 1, end - start, end + 1, syntax_options)
# check for cached highlight group
hgroup = 'ConqueHL_%d' % (abs(hash(highlight)))
if hgroup not in self.highlight_groups:
syntax_group = 'highlight %s %s' % (hgroup, highlight)
self.highlight_groups[hgroup] = hgroup
vim.command(syntax_group)
# link this syntax match to existing highlight group
syntax_highlight = 'highlight link %s %s' % (syntax_name, self.highlight_groups[hgroup])
vim.command(syntax_region)
vim.command(syntax_highlight)
# add syntax name to history
if not buffer_line in self.color_history:
self.color_history[buffer_line] = []
self.color_history[buffer_line].append({'name': syntax_name, 'start': start, 'end': end, 'highlight': highlight})
def prune_colors(self):
""" Remove old syntax highlighting from the Vim buffer
The kind of syntax highlighting required for terminal colors can make
Conque run slowly. The prune_colors() method will remove old highlight definitions
to keep the maximum number of highlight rules within a reasonable range.
"""
buffer_line = self.get_buffer_line(self.l)
ks = list(self.color_history.keys())
for line in ks:
if line < buffer_line - CONQUE_MAX_SYNTAX_LINES:
for syn in self.color_history[line]:
vim.command('syn clear ' + syn['name'])
del self.color_history[line]
###############################################################################################
# Control functions
def ctl_nl(self):
""" Process the newline control character. """
# if we're in a scrolling region, scroll instead of moving cursor down
if self.lines != self.working_lines and self.l == self.bottom:
del self.screen[self.top]
self.screen.insert(self.bottom, '')
elif self.l == self.bottom:
self.screen.append('')
else:
self.l += 1
self.color_changes = {}
def ctl_cr(self):
""" Process the carriage return control character. """
self.c = 1
self.color_changes = {}
def ctl_bs(self):
""" Process the backspace control character. """
if self.c > 1:
self.c += -1
def ctl_soh(self):
""" Process the start of heading control character. """
pass
def ctl_stx(self):
pass
def ctl_bel(self):
""" Process the bell control character. """
vim.command('call conque_term#bell()')
def ctl_tab(self):
""" Process the tab control character. """
# default tabstop location
ts = self.working_columns
# check set tabstops
for i in range(self.c, len(self.tabstops)):
if self.tabstops[i]:
ts = i + 1
break
self.c = ts
def ctl_so(self):
""" Process the shift out control character. """
self.character_set = 'graphics'
def ctl_si(self):
""" Process the shift in control character. """
self.character_set = 'ascii'
###############################################################################################
# CSI functions
def csi_font(self, csi):
""" Process the text attribute escape sequence. """
if not self.enable_colors:
return
# defaults to 0
if len(csi['vals']) == 0:
csi['vals'] = [0]
# 256 xterm color foreground
if len(csi['vals']) == 3 and csi['vals'][0] == 38 and csi['vals'][1] == 5:
self.color_changes['ctermfg'] = str(csi['vals'][2])
self.color_changes['guifg'] = '#' + self.xterm_to_rgb(csi['vals'][2])
# 256 xterm color background
elif len(csi['vals']) == 3 and csi['vals'][0] == 48 and csi['vals'][1] == 5:
self.color_changes['ctermbg'] = str(csi['vals'][2])
self.color_changes['guibg'] = '#' + self.xterm_to_rgb(csi['vals'][2])
# 16 colors
else:
for val in csi['vals']:
if val in CONQUE_FONT:
# ignore starting normal colors
if CONQUE_FONT[val]['normal'] and len(self.color_changes) == 0:
continue
# clear color changes
elif CONQUE_FONT[val]['normal']:
self.color_changes = {}
# save these color attributes for next plain_text() call
else:
for attr in CONQUE_FONT[val]['attributes'].keys():
if attr in self.color_changes and (attr == 'cterm' or attr == 'gui'):
self.color_changes[attr] += ',' + CONQUE_FONT[val]['attributes'][attr]
else:
self.color_changes[attr] = CONQUE_FONT[val]['attributes'][attr]
def csi_clear_line(self, csi):
""" Process the line clear escape sequence. """
# this escape defaults to 0
if len(csi['vals']) == 0:
csi['val'] = 0
# 0 means cursor right
if csi['val'] == 0:
self.screen[self.l] = self.screen[self.l][0:self.c - 1]
# 1 means cursor left
elif csi['val'] == 1:
self.screen[self.l] = ' ' * (self.c) + self.screen[self.l][self.c:]
# clear entire line
elif csi['val'] == 2:
self.screen[self.l] = ''
# clear colors
if csi['val'] == 2 or (csi['val'] == 0 and self.c == 1):
buffer_line = self.get_buffer_line(self.l)
if buffer_line in self.color_history:
for syn in self.color_history[buffer_line]:
vim.command('syn clear ' + syn['name'])
def csi_cursor_right(self, csi):
""" Process the move cursor right escape sequence. """
# we use 1 even if escape explicitly specifies 0
if csi['val'] == 0:
csi['val'] = 1
if self.wrap_cursor and self.c + csi['val'] > self.working_columns:
self.l += int(math.floor((self.c + csi['val']) / self.working_columns))
self.c = (self.c + csi['val']) % self.working_columns
return
self.c = self.bound(self.c + csi['val'], 1, self.working_columns)
def csi_cursor_left(self, csi):
""" Process the move cursor left escape sequence. """
# we use 1 even if escape explicitly specifies 0
if csi['val'] == 0:
csi['val'] = 1
if self.wrap_cursor and csi['val'] >= self.c:
self.l += int(math.floor((self.c - csi['val']) / self.working_columns))
self.c = self.working_columns - (csi['val'] - self.c) % self.working_columns
return
self.c = self.bound(self.c - csi['val'], 1, self.working_columns)
def csi_cursor_to_column(self, csi):
""" Process the move cursor to column escape sequence. """
self.c = self.bound(csi['val'], 1, self.working_columns)
def csi_cursor_up(self, csi):
""" Process the move cursor up escape sequence. """
self.l = self.bound(self.l - csi['val'], self.top, self.bottom)
self.color_changes = {}
def csi_cursor_down(self, csi):
""" Process the move cursor down escape sequence. """
self.l = self.bound(self.l + csi['val'], self.top, self.bottom)
self.color_changes = {}
def csi_clear_screen(self, csi):
""" Process the clear screen escape sequence. """
# default to 0
if len(csi['vals']) == 0:
csi['val'] = 0
# 2 == clear entire screen
if csi['val'] == 2:
self.l = 1
self.c = 1
self.screen.clear()
# 0 == clear down
elif csi['val'] == 0:
for l in range(self.bound(self.l + 1, 1, self.lines), self.lines + 1):
self.screen[l] = ''
# clear end of current line
self.csi_clear_line(self.parse_csi('K'))
# 1 == clear up
elif csi['val'] == 1:
for l in range(1, self.bound(self.l, 1, self.lines + 1)):
self.screen[l] = ''
# clear beginning of current line
self.csi_clear_line(self.parse_csi('1K'))
# clear coloration
if csi['val'] == 2 or csi['val'] == 0:
buffer_line = self.get_buffer_line(self.l)
for line in self.color_history.keys():
if line >= buffer_line:
for syn in self.color_history[line]:
vim.command('syn clear ' + syn['name'])
self.color_changes = {}
def csi_delete_chars(self, csi):
self.screen[self.l] = self.screen[self.l][:self.c] + self.screen[self.l][self.c + csi['val']:]
def csi_add_spaces(self, csi):
self.screen[self.l] = self.screen[self.l][: self.c - 1] + ' ' * csi['val'] + self.screen[self.l][self.c:]
def csi_cursor(self, csi):
if len(csi['vals']) == 2:
new_line = csi['vals'][0]
new_col = csi['vals'][1]
else:
new_line = 1
new_col = 1
if self.absolute_coords:
self.l = self.bound(new_line, 1, self.lines)
else:
self.l = self.bound(self.top + new_line - 1, self.top, self.bottom)
self.c = self.bound(new_col, 1, self.working_columns)
if self.c > len(self.screen[self.l]):
self.screen[self.l] = self.screen[self.l] + ' ' * (self.c - len(self.screen[self.l]))
def csi_set_coords(self, csi):
if len(csi['vals']) == 2:
new_start = csi['vals'][0]
new_end = csi['vals'][1]
else:
new_start = 1
new_end = vim.current.window.height
self.top = new_start
self.bottom = new_end
self.working_lines = new_end - new_start + 1
# if cursor is outside scrolling region, reset it
if self.l < self.top:
self.l = self.top
elif self.l > self.bottom:
self.l = self.bottom
self.color_changes = {}
def csi_tab_clear(self, csi):
# this escape defaults to 0
if len(csi['vals']) == 0:
csi['val'] = 0
if csi['val'] == 0:
self.tabstops[self.c - 1] = False
elif csi['val'] == 3:
for i in range(0, self.columns + 1):
self.tabstops[i] = False
def csi_set(self, csi):
# 132 cols
if csi['val'] == 3:
self.csi_clear_screen(self.parse_csi('2J'))
self.working_columns = 132
# relative_origin
elif csi['val'] == 6:
self.absolute_coords = False
# set auto wrap
elif csi['val'] == 7:
self.autowrap = True
self.color_changes = {}
def csi_reset(self, csi):
# 80 cols
if csi['val'] == 3:
self.csi_clear_screen(self.parse_csi('2J'))
self.working_columns = 80
# absolute origin
elif csi['val'] == 6:
self.absolute_coords = True
# reset auto wrap
elif csi['val'] == 7:
self.autowrap = False
self.color_changes = {}
###############################################################################################
# ESC functions
def esc_scroll_up(self):
self.ctl_nl()
self.color_changes = {}
def esc_next_line(self):
self.ctl_nl()
self.c = 1
def esc_set_tab(self):
if self.c <= len(self.tabstops):
self.tabstops[self.c - 1] = True
def esc_scroll_down(self):
if self.l == self.top:
del self.screen[self.bottom]
self.screen.insert(self.top, '')
else:
self.l += -1
self.color_changes = {}
###############################################################################################
# HASH functions
def hash_screen_alignment_test(self):
self.csi_clear_screen(self.parse_csi('2J'))
self.working_lines = self.lines
for l in range(1, self.lines + 1):
self.screen[l] = 'E' * self.working_columns
###############################################################################################
# CHARSET functions
def charset_us(self):
self.character_set = 'ascii'
def charset_uk(self):
self.character_set = 'ascii'
def charset_graphics(self):
self.character_set = 'graphics'
###############################################################################################
# Random stuff
def set_cursor(self, line, col):
""" Set cursor position in the Vim buffer.
Note: the line and column numbers are relative to the top left corner of the