forked from JamesShaker/SublimeHOL
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_transfer.py
More file actions
162 lines (144 loc) · 6.11 KB
/
text_transfer.py
File metadata and controls
162 lines (144 loc) · 6.11 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
from __future__ import absolute_import, unicode_literals, print_function, division
import re
import sublime_plugin
import sublime
from collections import defaultdict
import tempfile
import binascii
try:
from .sublimehol import manager, SETTINGS_FILE
except (ImportError, ValueError):
from sublimehol import manager, SETTINGS_FILE
def default_sender(repl, text, view=None, repl_view=None):
if repl.apiv2:
repl.write(text, location=repl_view.view.size() - len(text))
else:
repl.write(text)
"""Senders is a dict of functions used to transfer text to repl as a repl
specific load_file action"""
SENDERS = defaultdict(lambda: default_sender)
def sender(external_id,):
def wrap(func):
SENDERS[external_id] = func
return wrap
class HolReplViewWrite(sublime_plugin.TextCommand):
def run(self, edit, external_id, text):
for rv in manager.find_repl(external_id):
rv.append_input_text(text)
break # send to first repl found
else:
sublime.error_message("Cannot find REPL for '{0}'".format(external_id))
class HolReplSend(sublime_plugin.TextCommand):
def run(self, edit, external_id, text, with_auto_postfix=True):
for rv in manager.find_repl(external_id):
if with_auto_postfix:
text += rv.repl.cmd_postfix
if sublime.load_settings(SETTINGS_FILE).get('show_transferred_text'):
rv.append_input_text(text)
rv.adjust_end()
SENDERS[external_id](rv.repl, text, self.view, rv)
break
else:
sublime.error_message("Cannot find REPL for '{}'".format(external_id))
class HolReplTransferCurrent(sublime_plugin.TextCommand):
def run(self, edit, scope="selection", action="send", prepend="", append=""):
if scope == "selection":
lN,cN,text = self.selected_text()
elif scope == "lines":
lN,cN,text = self.selected_lines()
elif scope == "file":
lN,cN,text = self.selected_file()
else:
lN,cN,text = (0,0,"")
#Delete things off front dependent on type of selection sent
#TACTIC HANDLING
if prepend[-2:] == "e(":
#Get rid of leading whitespace and keep track
ntext = text.lstrip()
#Whilst we have leading tactic concatenators
while (ntext[0:2] in [r'>-',r'>|',r'>>',r'\\']) or (ntext[0:5] in ['THEN1','THENL', 'THEN ']):
#get rid of them
if ntext[0:2] in [r'>-',r'>|',r'>>',r'\\']:
ntext = ntext[2:]
if ntext[0:5] in ['THEN1','THENL','THEN ']:
ntext = ntext[5:]
#and any revealed whitespace
ntext = ntext.lstrip()
#Record deletion off front
dL = len(text) - len(ntext)
#Get rid of trailing whitespace
ntext = ntext.rstrip()
#Whilst we have trailing tactic concatenators
while (ntext[-2:] in [r'>-',r'>|',r'>>',r'\\']) or (ntext[-5:] in ['THEN1','THENL']) or (ntext[-4:] == 'THEN'):
#get rid of them
if ntext[-2:] in [r'>-',r'>|',r'>>',r'\\']:
ntext = ntext[:-2]
if ntext[-5:] in ['THEN1','THENL']:
ntext = ntext[:-5]
if ntext[-4:] == 'THEN':
ntext = ntext[:-4]
#and any revealed whitespace
ntext = ntext.rstrip()
elif prepend[-2:] == "g(":
#find goals by either indentifying complete, or half marked terms
matches = re.findall(r'\‘([^’]*?)\’|`([^`]*?)`',text)
possible_goals = [t[0] for t in matches] + [t[1] for t in matches]
if possible_goals == []:
possible_goals = re.split('\‘|\’|`', text)
#take the largest possible goal and find how much deleted to get it
ntext = max(possible_goals,key=len)
dL = text.find(ntext)
#augment prepend and append
prepend += "‘"
append = "’" + append
#OTHER HANDLING
else:
dL = 0
ntext = text
#handle if front stuff deleted
if dL > 0:
#find number of lines and characters deleted
deleted_text = text[:dL]
deleted_lines = deleted_text.split('\n')
nu_lines = len(deleted_lines) - 1
nu_chars = len(deleted_lines[-1])
#Adjust line number and character number based on deletion
if nu_lines > 0:
lN += nu_lines
cN = nu_chars
else:
cN -= nu_chars
#construct location string based on line and character number (accounting for definition syntax)
if ntext[:10] == "Definition":
locString = "(*#loc " + str(lN - 1) + " 0 *)\n"
else:
locString = "(*#loc " + str(lN) + " " + str(cN) + " *)\n"
if prepend != "" and ntext == "" and append == "":
text = locString + prepend
elif prepend != "" or ntext != "" or append != "":
text = prepend + locString + ntext + append
cmd = "hol_repl_" + action
self.view.window().run_command(cmd, {"external_id": self.repl_external_id(), "text": text})
def repl_external_id(self):
return self.view.scope_name(0).split(" ")[0].split(".", 1)[1]
def selected_text(self):
v = self.view
parts = [v.substr(region) for region in v.sel()]
lN,cN = v.rowcol(min([region.a for region in v.sel()]+[region.b for region in v.sel()]))
lN += 1
return (lN,cN,"".join(parts))
def selected_lines(self):
v = self.view
parts = []
region_pts = []
for sel in v.sel():
for line in v.lines(sel):
parts.append(v.substr(line))
region_pts.append(line.a)
region_pts.append(line.b)
lN,cN = v.rowcol(min(region_pts))
lN += 1
return (lN,cN,"\n".join(parts))
def selected_file(self):
v = self.view
return (1,0,v.substr(sublime.Region(0, v.size())))