From 5abba77067f4042cdfa77b2f96a95283e7c6f544 Mon Sep 17 00:00:00 2001 From: moculus Date: Mon, 6 Oct 2008 16:32:18 +0000 Subject: [PATCH] awk mode --HG-- branch : pmacs2 --- mode/awk.py | 71 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/mode/awk.py b/mode/awk.py index be21b9c..5f10646 100644 --- a/mode/awk.py +++ b/mode/awk.py @@ -1,4 +1,5 @@ import commands, os +from subprocess import Popen, PIPE, STDOUT import color, default, mode, tab from lex import Grammar, PatternRule, RegionRule from mode.python import StringGrammar2 @@ -66,24 +67,68 @@ class AwkTabber(StackTabber2): class AwkFilterFile(Exec): show_success = True - args = [arg('path', dt="path", p="Filter File: ", dv=default.path_dirname, - ld=True, h="file to open")] + args = [arg('path', dt="path", p="Filter File: ", dv=default.path_dirname, ld=True, h="file to open")] def _execute(self, w, **vargs): if not hasattr(w.buffer, 'path'): - w.set_error("Buffer has no program") + w.set_error("Buffer %r has no program" % w.buffer.name()) return elif w.buffer.changed(): - w.set_error("Buffer is not saved") + w.set_error("Buffer %r is not saved" % w.buffer.name()) return - else: - self._doit(w, w.buffer.path, w.application.config['awk.filter-cmd'], - cmdname='awk', bufname='*Awk-Output*', - opts={'data': vargs['path']}) + elif not os.path.exists(vargs['path']): + w.set_error("File %r not found" % vargs['path']) + return + + cmd = 'awk -f %r %r' % (w.buffer.path, vargs['path']) + pipe = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT) + outdata = pipe.stdout.read() + status = pipe.wait() >> 8 + w.application.data_buffer('*Awk-Output*', outdata, switch_to=True) + w.set_error("awk exited with status %d" % status) class AwkFilterBuffer(Pipe): - pass + args = [arg('name', dt="buffer", p="Filter Buffer: ", h="name of the buffer to switch to")] + def _execute(self, w, **vargs): + if not hasattr(w.buffer, 'path'): + w.set_error("Buffer %r has no program" % w.buffer.name()) + return + elif w.buffer.changed(): + w.set_error("Buffer %r is not saved" % w.buffer.name()) + return + elif not w.application.has_buffer_name(vargs['name']): + w.set_error("Buffer %r not found" % vargs['name']) + return + + b = w.application.bufferlist.get_buffer_by_name(vargs['name']) + opts = w.application.config['awk.cmd-opts'] + cmd = 'awk %s -f %r' % (opts, w.buffer.path) + pipe = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) + pipe.stdin.write(b.make_string()) + pipe.stdin.close() + output = pipe.stdout.read() + status = pipe.wait() >> 8 + w.application.data_buffer('*Awk-Output*', output, switch_to=True) + w.set_error("awk exited with status %d" % status) + class AwkFilterInput(Method): - pass + args = [arg('input', p="Data To Filter: ", h="data to filter")] + def _execute(self, w, **vargs): + if not hasattr(w.buffer, 'path'): + w.set_error("Buffer %r has no program" % w.buffer.name()) + return + elif w.buffer.changed(): + w.set_error("Buffer %r is not saved" % w.buffer.name()) + return + + opts = w.application.config['awk.cmd-opts'] + cmd = 'awk %s -f %r' % (opts, w.buffer.path) + pipe = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) + pipe.stdin.write(vargs['input']) + pipe.stdin.close() + output = pipe.stdout.read() + status = pipe.wait() >> 8 + w.application.data_buffer('*Awk-Output*', output, switch_to=True) + w.set_error("awk exited with status %d" % status) class Awk(mode.Fundamental): tabbercls = AwkTabber @@ -100,11 +145,7 @@ class Awk(mode.Fundamental): 'awk_regex.data': ('cyan', 'default', 'bold'), 'awk_regex.end': ('cyan', 'default', 'bold'), } - config = { - 'awk.filter-cmd': "awk -f %(path)r %(data)r", - } + config = {'awk.cmd-opts': ""} actions = [AwkFilterFile, AwkFilterBuffer, AwkFilterInput] - #format = "%(flag)s %(bname)-18s (%(mname)s) %(indent)s %(cursor)s/%(mark)s %(perc)s [%(func)s]" - install = Awk.install