6 from SearchDialogBase
import SearchDialogBase
8 def grep(text
, io
=None, flist
=None):
10 engine
= SearchEngine
.get(root
)
11 if not hasattr(engine
, "_grepdialog"):
12 engine
._grepdialog
= GrepDialog(root
, engine
, flist
)
13 dialog
= engine
._grepdialog
14 searchphrase
= text
.get("sel.first", "sel.last")
15 dialog
.open(text
, searchphrase
, io
)
17 class GrepDialog(SearchDialogBase
):
19 title
= "Find in Files Dialog"
23 def __init__(self
, root
, engine
, flist
):
24 SearchDialogBase
.__init
__(self
, root
, engine
)
26 self
.globvar
= StringVar(root
)
27 self
.recvar
= BooleanVar(root
)
29 def open(self
, text
, searchphrase
, io
=None):
30 SearchDialogBase
.open(self
, text
, searchphrase
)
32 path
= io
.filename
or ""
35 dir, base
= os
.path
.split(path
)
36 head
, tail
= os
.path
.splitext(base
)
39 self
.globvar
.set(os
.path
.join(dir, "*" + tail
))
41 def create_entries(self
):
42 SearchDialogBase
.create_entries(self
)
43 self
.globent
= self
.make_entry("In files:", self
.globvar
)
45 def create_other_buttons(self
):
48 btn
= Checkbutton(f
, anchor
="w",
50 text
="Recurse down subdirectories")
51 btn
.pack(side
="top", fill
="both")
54 def create_command_buttons(self
):
55 SearchDialogBase
.create_command_buttons(self
)
56 self
.make_button("Search Files", self
.default_command
, 1)
58 def default_command(self
, event
=None):
59 prog
= self
.engine
.getprog()
62 path
= self
.globvar
.get()
66 from OutputWindow
import OutputWindow
69 sys
.stdout
= OutputWindow(self
.flist
)
70 self
.grep_it(prog
, path
)
74 def grep_it(self
, prog
, path
):
75 dir, base
= os
.path
.split(path
)
76 list = self
.findfiles(dir, base
, self
.recvar
.get())
79 pat
= self
.engine
.getpat()
80 print "Searching %r in %s ..." % (pat
, path
)
90 block
= f
.readlines(100000)
98 sys
.stdout
.write("%s: %s: %s\n" % (fn
, lineno
, line
))
105 print "Found", hits
, "hit%s." % s
106 print "(Hint: right-click to open locations.)"
110 def findfiles(self
, dir, base
, rec
):
112 names
= os
.listdir(dir or os
.curdir
)
113 except os
.error
, msg
:
119 fn
= os
.path
.join(dir, name
)
120 if os
.path
.isdir(fn
):
123 if fnmatch
.fnmatch(name
, base
):
126 for subdir
in subdirs
:
127 list.extend(self
.findfiles(subdir
, base
, rec
))
130 def close(self
, event
=None):
132 self
.top
.grab_release()