9 from SearchDialogBase
import SearchDialogBase
11 def grep(text
, io
=None, flist
=None):
13 engine
= SearchEngine
.get(root
)
14 if not hasattr(engine
, "_grepdialog"):
15 engine
._grepdialog
= GrepDialog(root
, engine
, flist
)
16 dialog
= engine
._grepdialog
19 class GrepDialog(SearchDialogBase
):
21 title
= "Find in Files Dialog"
25 def __init__(self
, root
, engine
, flist
):
26 SearchDialogBase
.__init
__(self
, root
, engine
)
28 self
.globvar
= StringVar(root
)
29 self
.recvar
= BooleanVar(root
)
31 def open(self
, io
=None):
32 SearchDialogBase
.open(self
, None)
34 path
= io
.filename
or ""
37 dir, base
= os
.path
.split(path
)
38 head
, tail
= os
.path
.splitext(base
)
41 self
.globvar
.set(os
.path
.join(dir, "*" + tail
))
43 def create_entries(self
):
44 SearchDialogBase
.create_entries(self
)
45 self
.globent
= self
.make_entry("In files:", self
.globvar
)
47 def create_other_buttons(self
):
50 btn
= Checkbutton(f
, anchor
="w",
52 text
="Recurse down subdirectories")
53 btn
.pack(side
="top", fill
="both")
56 def create_command_buttons(self
):
57 SearchDialogBase
.create_command_buttons(self
)
58 self
.make_button("Search Files", self
.default_command
, 1)
60 def default_command(self
, event
=None):
61 prog
= self
.engine
.getprog()
64 path
= self
.globvar
.get()
68 from OutputWindow
import OutputWindow
71 sys
.stdout
= OutputWindow(self
.flist
)
72 self
.grep_it(prog
, path
)
76 def grep_it(self
, prog
, path
):
77 dir, base
= os
.path
.split(path
)
78 list = self
.findfiles(dir, base
, self
.recvar
.get())
81 pat
= self
.engine
.getpat()
82 print "Searching %s in %s ..." % (`pat`
, path
)
92 block
= f
.readlines(100000)
100 sys
.stdout
.write("%s: %s: %s\n" % (fn
, lineno
, line
))
107 print "Found", hits
, "hit%s." % s
108 print "(Hint: right-click to open locations.)"
112 def findfiles(self
, dir, base
, rec
):
114 names
= os
.listdir(dir or os
.curdir
)
115 except os
.error
, msg
:
121 fn
= os
.path
.join(dir, name
)
122 if os
.path
.isdir(fn
):
125 if fnmatch
.fnmatch(name
, base
):
128 for subdir
in subdirs
:
129 list.extend(self
.findfiles(subdir
, base
, rec
))
132 def close(self
, event
=None):
134 self
.top
.grab_release()