-
Notifications
You must be signed in to change notification settings - Fork 6
/
smart_text_edit.py
195 lines (149 loc) · 5.93 KB
/
smart_text_edit.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
################################################################################
### Copyright © 2013 BlackDragonHunt
###
### This file is part of the Super Duper Script Editor.
###
### The Super Duper Script Editor is free software: you can redistribute it
### and/or modify it under the terms of the GNU General Public License as
### published by the Free Software Foundation, either version 3 of the License,
### or (at your option) any later version.
###
### The Super Duper Script Editor is distributed in the hope that it will be
### useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################
from PyQt4 import Qt, QtGui
from PyQt4.QtGui import QTextCursor
from spellcheck_edit import SpellCheckEdit
import re
################################################################################
### CONSTANTS
################################################################################
APOSTROPHE = u"'"
QUOTE = u"\""
LEFT_APOST = u"‘"
RIGHT_APOST = u"’"
LEFT_QUOTE = u"“"
RIGHT_QUOTE = u"”"
DEFAULT_REPL = [(u"--", u"—"),
(u"—-", u"–"),
]
CLT_BEGIN = u"<"
CLT_END = u">"
RE_QUICK_CLT = re.compile(ur"\<(?P<CLT_INDEX>\d+)?\>", re.UNICODE)
RE_FULL_CLT = re.compile(ur"\<CLT(\s+(?P<CLT_INDEX>\d+))?\>", re.UNICODE)
################################################################################
### SmartTextEdit Class
################################################################################
class SmartTextEdit(SpellCheckEdit):
def __init__(self, parent):
super(SmartTextEdit, self).__init__(parent)
self.changing_text = False
self.textChanged.connect(self.changed_text)
self.enable_replacements = True
self.enable_smart_quotes = True
self.enable_quick_clt = True
self.replacements = DEFAULT_REPL
def changed_text(self):
# So we don't get caught in infinite death if we have a recursive replacement
if self.changing_text:
return
self.changing_text = True
cursor = self.textCursor()
if self.enable_smart_quotes:
cursor = self.smart_quotes(cursor)
if self.enable_quick_clt:
cursor = self.quick_clt(cursor)
if self.enable_replacements:
cursor = self.handle_replacements(cursor)
self.setTextCursor(cursor)
self.changing_text = False
def smart_quotes(self, cursor):
# Nothing to do at the start of a block.
if cursor.atBlockStart():
return cursor
pos = cursor.positionInBlock()
text = unicode(cursor.block().text().toUtf8(), "UTF-8")
char = text[pos - 1 : pos]
if not char in [APOSTROPHE, QUOTE]:
return cursor
repl_with = None
if pos == 1:
prev_char = None
else:
prev_char = text[pos - 2 : pos - 1]
# It's a left quote/apost if it comes after a space, a tab,
# a left-bracket-type thing or the left of the opposite type of quote,
# or the right of same type of quote.
if prev_char in [None, u" ", u"\t", u"(", u"[", u"{", u"<", u">", u"\uFFFC", LEFT_APOST if char == QUOTE else LEFT_QUOTE, RIGHT_QUOTE if char == QUOTE else RIGHT_APOST]:
repl_with = LEFT_QUOTE if char == QUOTE else LEFT_APOST
else:
repl_with = RIGHT_QUOTE if char == QUOTE else RIGHT_APOST
cursor.setPosition(cursor.position())
cursor.setPosition(cursor.position() - 1, QTextCursor.KeepAnchor)
cursor.insertText(repl_with)
return cursor
def quick_clt(self, cursor):
# Nothing to do at the start of a block.
if cursor.atBlockStart():
return cursor
pos = cursor.position()
text = unicode(cursor.document().toPlainText().toUtf8(), "UTF-8")
char = text[pos - 1 : pos]
if not char == CLT_END:
return cursor
# Find the first < *before* this >
clt_begin = text.rfind(CLT_BEGIN, 0, pos)
clt_end = pos
# Get the requested CLT
match = RE_QUICK_CLT.match(text, clt_begin, clt_end)
if not match or not (match.start() == clt_begin and match.end() == clt_end):
return cursor
clt = int(match.groupdict(default = "0")["CLT_INDEX"])
# Now look for an existing, open CLT before this.
prev_clt_begin = text.rfind(CLT_BEGIN, 0, clt_begin)
prev_clt = 0
match = RE_FULL_CLT.match(text, prev_clt_begin, clt_begin)
if match:
prev_clt = match.group("CLT_INDEX")
if prev_clt == None:
prev_clt = 0
else:
prev_clt = int(prev_clt)
left = ""
right = ""
if not prev_clt == clt:
if not prev_clt == 0:
left = "<CLT>"
# right = "<CLT %02d>" % prev_clt
# else:
# right = "<CLT>"
if not clt == 0:
left = left + "<CLT %02d>" % clt
# right = "<CLT>" + right
cursor.setPosition(cursor.position())
cursor.setPosition(cursor.position() - (clt_end - clt_begin), QTextCursor.KeepAnchor)
cursor.insertText(left + right)
cursor.setPosition(clt_begin + len(left))
return cursor
def handle_replacements(self, cursor):
# Nothing to do at the start of a block.
if cursor.atBlockStart():
return cursor
pos = cursor.positionInBlock()
text = unicode(cursor.block().text().toUtf8(), "UTF-8")
for (repl, repl_with) in self.replacements:
if pos < len(repl):
continue
if text[pos - len(repl) : pos] == repl:
cursor.setPosition(cursor.position())
cursor.setPosition(cursor.position() - len(repl), QTextCursor.KeepAnchor)
cursor.insertText(repl_with)
break
return cursor
### EOF ###