-
Notifications
You must be signed in to change notification settings - Fork 0
/
rain
executable file
·37 lines (28 loc) · 1.16 KB
/
rain
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
#!/usr/bin/env python3
import os
import sys
import re
# Kak buffer contents will arrive over stdin
buffer_contents = sys.stdin.readlines()
# make a regex for opening delimiters (, [, and {
opening_delimiters = re.compile('[\[({]')
# make a regex for closing delimiters ), ], and }
closing_delimiters = re.compile('[\])}]')
delimiter_level = 0
delimiter_ranges = []
def kak_range(line_number, char_number, delimiter_level):
return str(line_number) + '.' + str(char_number) + '+1|delimiter' + str(delimiter_level)
def kak_range_list(delimiter_ranges):
return '\'' + '\' \''.join(delimiter_ranges) + '\''
for li, line in enumerate(buffer_contents):
for ci, char in enumerate(line):
# line and char numbers in Kak are 1-indexed
line_number = li + 1
char_number = ci + 1
if opening_delimiters.match(char):
delimiter_ranges.append(kak_range(line_number, char_number, delimiter_level))
delimiter_level += 1
elif closing_delimiters.match(char):
delimiter_level -= 1
delimiter_ranges.append(kak_range(line_number, char_number, delimiter_level))
print(kak_range_list(delimiter_ranges))