From 3a36d4f149c769ba5251eba0cfc6947f3a1409eb Mon Sep 17 00:00:00 2001 From: moculus Date: Tue, 10 Jul 2007 18:55:40 +0000 Subject: [PATCH] new pipe command, and grep command improvements --HG-- branch : pmacs2 --- application.py | 3 ++- method.py | 38 ++++++++++++++++++++++++-------------- regex.py | 3 +++ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/application.py b/application.py index 1cd2b06..09ec245 100755 --- a/application.py +++ b/application.py @@ -546,7 +546,8 @@ class Application(object): if w.margins_visible: for (limit, shade) in w.margins: - if limit <= self.x: + #if limit <= self.x: + if limit < self.x: for j in range(0, slot.height): char = self.win.inch(j + slot.offset, limit) & 255 attr = color.build('default', shade, 'bold') diff --git a/method.py b/method.py index d6cfe4d..6ff5c9e 100644 --- a/method.py +++ b/method.py @@ -1405,12 +1405,23 @@ class RegisterRestore(Method): name = name[0:self.MAX_REG] + '...' w.set_error('Restored %r from register %r' % (text2, name2)) -class Grep(Method): +class Pipe(Method): '''help here''' - args = [Argument('pattern', datatype="str", prompt="Pattern: ")] + args = [Argument('cmd', datatype="str", prompt="Command: ")] + def _parse(self, w, **vargs): + m = regex.shell_command.match(vargs['cmd']) + if m: + prog = m.group(0) + return (prog, vargs['cmd']) + else: + return (None, None) + def _execute(self, w, **vargs): - cmd = ("/usr/bin/grep", '-E', '-n', vargs['pattern'], '-') - pipe = popen2.Popen3(cmd, capturestderr=True) + (prog, cmd) = self._parse(w, **vargs) + if prog is None: + return + + pipe = popen2.Popen4(cmd) pid = pipe.pid indata = w.buffer.make_string() @@ -1418,16 +1429,15 @@ class Grep(Method): pipe.tochild.close() outdata = pipe.fromchild.read() - errdata = pipe.childerr.read() status = pipe.wait() >> 8 - if status == 0: - num = len(outdata.split('\n')) - w.application.data_buffer("*Grep*", outdata, switch_to=True) - w.application.set_error("Grep: %d matches found" % num) - elif status == 1: - w.application.set_error("Grep: no matches found") - else: - w.application.data_buffer("*Grep*", outdata, switch_to=True) - w.set_error("Grep: error (exited with status %d)" % status) + bufname = '*%s*' % self.name.title() + w.application.data_buffer(bufname, outdata, switch_to=True) + w.set_error("%s exited with status %d" % (prog, status)) + +class Grep(Pipe): + '''help here''' + args = [Argument('pattern', datatype="str", prompt="Pattern: ")] + def _parse(self, w, **vargs): + return ('grep', ('/usr/bin/grep', '-E', '-n', vargs['pattern'])) \ No newline at end of file diff --git a/regex.py b/regex.py index a923726..2febf12 100644 --- a/regex.py +++ b/regex.py @@ -3,6 +3,9 @@ import re # meta regexes meta_chars = re.compile(r'([\.\^\$\*\+\?\{\}\(\)\[\]\|\\])') +# shell +shell_command = re.compile(r'^[^ ]+') + # whitespace regexes leading_whitespace = re.compile('^ *') trailing_whitespace = re.compile(' *$')