You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
12 KiB
Python
175 lines
12 KiB
Python
# Finding (and Replacing) Nemo, Version 1.1, Aristide Grange 2006/06/06
|
|
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496783
|
|
|
|
"""
|
|
Finding (and Replacing) Nemo
|
|
|
|
Instant Regular Expressions
|
|
Created by Aristide Grange
|
|
"""
|
|
import re
|
|
import itertools
|
|
|
|
from six.moves.tkinter import (
|
|
Frame,
|
|
Label,
|
|
PhotoImage,
|
|
Scrollbar,
|
|
Text,
|
|
Tk,
|
|
SEL_FIRST,
|
|
SEL_LAST,
|
|
)
|
|
|
|
|
|
windowTitle = "Finding (and Replacing) Nemo"
|
|
initialFind = r"n(.*?)e(.*?)m(.*?)o"
|
|
initialRepl = r"M\1A\2K\3I"
|
|
initialText = """\
|
|
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
|
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
|
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
|
|
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
|
"""
|
|
images = {
|
|
"FIND": "R0lGODlhMAAiAPcAMf/////37//35//n1v97Off///f/9/f37/fexvfOvfeEQvd7QvdrQvdrKfdaKfdSMfdSIe/v9+/v7+/v5+/n3u/e1u/Wxu/Gre+1lO+tnO+thO+Ua+97Y+97Oe97Me9rOe9rMe9jOe9jMe9jIe9aMefe5+fe3ufezuece+eEWudzQudaIedSIedKMedKIedCKedCId7e1t7Wzt7Oxt7Gvd69vd69rd61pd6ljN6UjN6Ue96EY95zY95rUt5rQt5jMd5SId5KIdbn59be3tbGztbGvda1rdaEa9Z7a9Z7WtZzQtZzOdZzMdZjMdZaQtZSOdZSMdZKMdZCKdZCGNY5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C33", "#F42548"]
|
|
fieldParams = {
|
|
"height": 3,
|
|
"width": 70,
|
|
"font": ("monaco", 14),
|
|
"highlightthickness": 0,
|
|
"borderwidth": 0,
|
|
"background": "white",
|
|
}
|
|
textParams = {
|
|
"bg": "#F7E0D4",
|
|
"fg": "#2321F1",
|
|
"highlightthickness": 0,
|
|
"width": 1,
|
|
"height": 10,
|
|
"font": ("verdana", 16),
|
|
"wrap": "word",
|
|
}
|
|
|
|
|
|
class Zone:
|
|
def __init__(self, image, initialField, initialText):
|
|
frm = Frame(root)
|
|
frm.config(background="white")
|
|
self.image = PhotoImage(format='gif', data=images[image.upper()])
|
|
self.imageDimmed = PhotoImage(format='gif', data=images[image])
|
|
self.img = Label(frm)
|
|
self.img.config(borderwidth=0)
|
|
self.img.pack(side="left")
|
|
self.fld = Text(frm, **fieldParams)
|
|
self.initScrollText(frm, self.fld, initialField)
|
|
frm = Frame(root)
|
|
self.txt = Text(frm, **textParams)
|
|
self.initScrollText(frm, self.txt, initialText)
|
|
for i in range(2):
|
|
self.txt.tag_config(colors[i], background=colors[i])
|
|
self.txt.tag_config("emph" + colors[i], foreground=emphColors[i])
|
|
|
|
def initScrollText(self, frm, txt, contents):
|
|
scl = Scrollbar(frm)
|
|
scl.config(command=txt.yview)
|
|
scl.pack(side="right", fill="y")
|
|
txt.pack(side="left", expand=True, fill="x")
|
|
txt.config(yscrollcommand=scl.set)
|
|
txt.insert("1.0", contents)
|
|
frm.pack(fill="x")
|
|
Frame(height=2, bd=1, relief="ridge").pack(fill="x")
|
|
|
|
def refresh(self):
|
|
self.colorCycle = itertools.cycle(colors)
|
|
try:
|
|
self.substitute()
|
|
self.img.config(image=self.image)
|
|
except re.error:
|
|
self.img.config(image=self.imageDimmed)
|
|
|
|
|
|
class FindZone(Zone):
|
|
def addTags(self, m):
|
|
color = next(self.colorCycle)
|
|
self.txt.tag_add(color, "1.0+%sc" % m.start(), "1.0+%sc" % m.end())
|
|
try:
|
|
self.txt.tag_add(
|
|
"emph" + color, "1.0+%sc" % m.start("emph"), "1.0+%sc" % m.end("emph")
|
|
)
|
|
except:
|
|
pass
|
|
|
|
def substitute(self, *args):
|
|
for color in colors:
|
|
self.txt.tag_remove(color, "1.0", "end")
|
|
self.txt.tag_remove("emph" + color, "1.0", "end")
|
|
self.rex = re.compile("") # default value in case of misformed regexp
|
|
self.rex = re.compile(self.fld.get("1.0", "end")[:-1], re.MULTILINE)
|
|
try:
|
|
re.compile("(?P<emph>%s)" % self.fld.get(SEL_FIRST, SEL_LAST))
|
|
self.rexSel = re.compile(
|
|
"%s(?P<emph>%s)%s"
|
|
% (
|
|
self.fld.get("1.0", SEL_FIRST),
|
|
self.fld.get(SEL_FIRST, SEL_LAST),
|
|
self.fld.get(SEL_LAST, "end")[:-1],
|
|
),
|
|
re.MULTILINE,
|
|
)
|
|
except:
|
|
self.rexSel = self.rex
|
|
self.rexSel.sub(self.addTags, self.txt.get("1.0", "end"))
|
|
|
|
|
|
class ReplaceZone(Zone):
|
|
def addTags(self, m):
|
|
s = sz.rex.sub(self.repl, m.group())
|
|
self.txt.delete(
|
|
"1.0+%sc" % (m.start() + self.diff), "1.0+%sc" % (m.end() + self.diff)
|
|
)
|
|
self.txt.insert("1.0+%sc" % (m.start() + self.diff), s, next(self.colorCycle))
|
|
self.diff += len(s) - (m.end() - m.start())
|
|
|
|
def substitute(self):
|
|
self.txt.delete("1.0", "end")
|
|
self.txt.insert("1.0", sz.txt.get("1.0", "end")[:-1])
|
|
self.diff = 0
|
|
self.repl = rex0.sub(r"\\g<\1>", self.fld.get("1.0", "end")[:-1])
|
|
sz.rex.sub(self.addTags, sz.txt.get("1.0", "end")[:-1])
|
|
|
|
|
|
def launchRefresh(_):
|
|
sz.fld.after_idle(sz.refresh)
|
|
rz.fld.after_idle(rz.refresh)
|
|
|
|
|
|
def app():
|
|
global root, sz, rz, rex0
|
|
root = Tk()
|
|
root.resizable(height=False, width=True)
|
|
root.title(windowTitle)
|
|
root.minsize(width=250, height=0)
|
|
sz = FindZone("find", initialFind, initialText)
|
|
sz.fld.bind("<Button-1>", launchRefresh)
|
|
sz.fld.bind("<ButtonRelease-1>", launchRefresh)
|
|
sz.fld.bind("<B1-Motion>", launchRefresh)
|
|
sz.rexSel = re.compile("")
|
|
rz = ReplaceZone("repl", initialRepl, "")
|
|
rex0 = re.compile(r"(?<!\\)\\([0-9]+)")
|
|
root.bind_all("<Key>", launchRefresh)
|
|
launchRefresh(None)
|
|
root.mainloop()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app()
|
|
|
|
__all__ = ['app']
|