--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-10-06 16:32:18 +00:00
parent 7204c2b29d
commit 5abba77067
1 changed files with 56 additions and 15 deletions

View File

@ -1,4 +1,5 @@
import commands, os import commands, os
from subprocess import Popen, PIPE, STDOUT
import color, default, mode, tab import color, default, mode, tab
from lex import Grammar, PatternRule, RegionRule from lex import Grammar, PatternRule, RegionRule
from mode.python import StringGrammar2 from mode.python import StringGrammar2
@ -66,24 +67,68 @@ class AwkTabber(StackTabber2):
class AwkFilterFile(Exec): class AwkFilterFile(Exec):
show_success = True show_success = True
args = [arg('path', dt="path", p="Filter File: ", dv=default.path_dirname, args = [arg('path', dt="path", p="Filter File: ", dv=default.path_dirname, ld=True, h="file to open")]
ld=True, h="file to open")]
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
if not hasattr(w.buffer, 'path'): 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 return
elif w.buffer.changed(): elif w.buffer.changed():
w.set_error("Buffer is not saved") w.set_error("Buffer %r is not saved" % w.buffer.name())
return return
else: elif not os.path.exists(vargs['path']):
self._doit(w, w.buffer.path, w.application.config['awk.filter-cmd'], w.set_error("File %r not found" % vargs['path'])
cmdname='awk', bufname='*Awk-Output*', return
opts={'data': vargs['path']})
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): 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): 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): class Awk(mode.Fundamental):
tabbercls = AwkTabber tabbercls = AwkTabber
@ -100,11 +145,7 @@ class Awk(mode.Fundamental):
'awk_regex.data': ('cyan', 'default', 'bold'), 'awk_regex.data': ('cyan', 'default', 'bold'),
'awk_regex.end': ('cyan', 'default', 'bold'), 'awk_regex.end': ('cyan', 'default', 'bold'),
} }
config = { config = {'awk.cmd-opts': ""}
'awk.filter-cmd': "awk -f %(path)r %(data)r",
}
actions = [AwkFilterFile, AwkFilterBuffer, AwkFilterInput] actions = [AwkFilterFile, AwkFilterBuffer, AwkFilterInput]
#format = "%(flag)s %(bname)-18s (%(mname)s) %(indent)s %(cursor)s/%(mark)s %(perc)s [%(func)s]"
install = Awk.install install = Awk.install