From 551589644d266cfb096f75f3f37dd08e29d9942a Mon Sep 17 00:00:00 2001 From: moculus Date: Mon, 16 Mar 2009 14:42:17 +0000 Subject: [PATCH] perl perl perl --HG-- branch : pmacs2 --- application.py | 2 +- mode/perl.py | 107 ++++++++++++++++++++++--------------------------- 2 files changed, 48 insertions(+), 61 deletions(-) diff --git a/application.py b/application.py index 800f953..c91bd3b 100755 --- a/application.py +++ b/application.py @@ -646,7 +646,7 @@ class Application(object): i += 1 # running external programs - def run_pipe(self, args, b, name='*Output*', switch='yes', modename=None): + def run_pipe(self, args, b, name='*Output*', switch=True, modename=None): pipe = Popen(args=args, stdin=PIPE, stdout=PIPE, stderr=STDOUT) pipe.stdin.write(b.make_string()) pipe.stdin.close() diff --git a/mode/perl.py b/mode/perl.py index 2e96ef4..dbcac7e 100644 --- a/mode/perl.py +++ b/mode/perl.py @@ -1,6 +1,6 @@ import os, re, string, sys from subprocess import Popen, PIPE, STDOUT -import buffer, color, commands, completer, context, default, method, mode, regex, tab +import buffer, color, commands, completer, context, method, mode, regex, tab from point import Point from lex import Grammar, PatternRule, ContextPatternRule, RegionRule, \ OverridePatternRule, PatternGroupRule @@ -192,48 +192,48 @@ class PerlTabber2(StackTabber2): class PerlSetLib(Method): '''Set the path(s) to find perl modules''' - args = [Argument("lib", type=type(""), prompt="Location of lib: ", - default=default.build_constant("."))] + args = [method.arg("lib", dt='path', p="Lib: ", dv=lambda w: '.')] def _execute(self, w, **vargs): - w.application.config['perl.lib'] = vargs['lib'] - w.application.config['perl.libs'] = [vargs['lib']] -class PerlAddLib(Method): + w.application.config['perl.libs'] = vargs['lib'].split(':') +class PerlAddLib(PerlSetLib): '''Set the path(s) to find perl modules''' - args = [Argument("lib", type=type(""), prompt="Location of lib: ", - default=default.build_constant("."))] def _execute(self, w, **vargs): w.application.config['perl.libs'].append(vargs['lib']) - -class PerlCheckSyntax(Method): + +class PerlBase(Method): + bname = '*Perl*' + def get_args(self, w, **vargs): + return ['perl', '-e', 'print "hello world\n"'] + def run_pipe(self, w, args, switch, mname=None): + return w.application.run_pipe(args, w.buffer, self.bname, switch, mname) + +class PerlCheckSyntax(PerlBase): '''Check the syntax of a perl file''' + bname = '*Perl-Syntax*' + def get_args(self, w, **vargs): + args = ['perl'] + for l in w.application.config.get('perl.libs', []): + args.extend(('-I', l)) + return args + ['-c', '-'] def _execute(self, w, **vargs): - a = w.application - if a.config.get('perl.libs', None): - args = ['perl'] - for l in a.config.get('perl.libs'): - args.extend(('-I', l)) - args.extend(('-c', '-')) - else: - args = ('perl', '-I', a.config.get('perl.lib', '.'), '-c', '-') - retval = a.run_pipe(args, w.buffer, '*Perl-Syntax*', lambda x: x != 0, - modename='error') - b = a.get_buffer_by_name('*Perl-Syntax*') + args = self.get_args(w, **vargs) + r = self.run_pipe(w, args, lambda x: x != 0, 'error') + b = w.application.get_buffer_by_name(self.bname) b.orig_path = w.buffer.path - if retval == 0: a.set_error("Syntax OK") + if r == 0: w.set_error("Syntax OK") - -class PerldocModule(Method): +class PerldocModule(PerlBase): '''View documentation about this buffer using perldoc''' - prog = 'use Pod::Text;' \ - 'Pod::Text->new(sentence=>0, width=>78)->parse_from_filehandle();' + bname = '*Perldoc*' + prog = 'use Pod::Text; Pod::Text->new()->parse_from_filehandle();'; + def get_args(self, w, **vargs): + return ('perl', '-e', self.prog) def _execute(self, w, **vargs): - app = w.application - args = ('perl', '-e', self.prog) - app.run_pipe(args, w.buffer, '*Perldoc*', True) + self.run_pipe(w, self.get_args(w, **vargs), True) class Perldoc(Method): name_re = re.compile('(?:[a-zA-Z_][a-zA-Z0-9_]*::)*[a-zA-Z_][a-zA-Z0-9_]*') - args = [Argument("name", type(""), "", "Perldoc: ")] + args = [method.arg("name", p="Perldoc: ")] def _execute(self, w, **vargs): name = vargs['name'] if not self.name_re.match(name): @@ -255,42 +255,33 @@ class Perldoc(Method): if data: self._show(w, data, name) else: - w.application.set_error('nothing found for %r' % name) + w.set_error('nothing found for %r' % name) def _try(self, w, name, asfunc=False): - a = w.application if asfunc: - cmd = "perldoc -t -T -f '%s'" % (name,) + cmd = "perldoc -t -T -f '%s'" % name else: - cmd = "perldoc -t -T '%s'" % (name,) + cmd = "perldoc -t -T '%s'" % name - if a.config.get('perl.libs', None): - s = ':'.join(['%r' % x for x in a.config.get('perl.libs')]) - cmd = 'PERL5LIB=%r %s' % (s, cmd) - elif a.config.get('perl.lib', None): - cmd = 'PERL5LIB=%r %s' % (a.config.get('perl.lib'), cmd) + l = w.application.config.get('perl.libs', []) + if l: + cmd = 'PERL5LIB=%r %s' % (':'.join(['%r' % x for x in l]), cmd) - (status, data) = commands.getstatusoutput(cmd) + status, data = commands.getstatusoutput(cmd) if status == 0: return data else: return None def _show(self, w, data, name): w.application.data_buffer("*Perldoc*", data, switch_to=True) - w.application.set_error('displaying documentation for %r' % name) + w.set_error('displaying perldoc for %r' % name) class PerldocWord(Perldoc): '''View documentation about a package or function using perldoc''' args = [] def _execute(self, w, **vargs): - token = w.get_token() - word = token.string - - # make sure that the name is (mostly) valid + word = w.get_token().string if word is None: - w.application.set_error('no word selected') - return - elif ':' in word and '::' not in word: - w.application.set_error('invalid word: %r' % word) + w.set_error('no word selected') return return Perldoc._execute(self, w, name=word) @@ -298,7 +289,7 @@ class PerlInitFunctions(Method): '''Jump to a function defined in this module''' def _execute(self, w, **vargs): w.mode.context.build_name_map() - w.application.set_error("Initialized function map") + w.set_error("Initialized function map") class PerlGotoFunction(Method): '''Jump to a function defined in this module''' @@ -309,7 +300,7 @@ class PerlGotoFunction(Method): if name in functions: w.goto(Point(0, functions[name])) else: - w.application.set_error("Function %r was not found" % name) + w.set_error("Function %r was not found" % name) class PerlListFunctions(Method): '''Show the user all functions defined in this module''' @@ -324,11 +315,11 @@ class PerlWhichFunction(Method): cursor = w.logical_cursor() name = w.mode.context.get_line_name(cursor.y) if name is None: - w.application.set_error("None"); + w.set_error("None"); else: functions = w.mode.context.get_names() i = functions[name] + 1 - w.application.set_error("line %d: %s" % (i, name)) + w.set_error("line %d: %s" % (i, name)) class PerlHashCleanup(Method): '''Correctly align assignment blocks and literal hashes''' @@ -729,10 +720,8 @@ class Perl(mode.Fundamental): 'translate.end': ('magenta', 'default', 'bold'), 'translate.null': ('magenta', 'default', 'bold'), } - config = { - 'perl.lib': 'lib', - 'perl.libs': ['lib'], - } + config = {} + lconfig = {'perl.libs': []} actions = [PerlSetLib, PerlCheckSyntax, PerlHashCleanup, PerldocModule, PerldocWord, Perldoc, PerlWrapParagraph, PerlInitFunctions, PerlGotoFunction, PerlWhichFunction, @@ -788,10 +777,8 @@ class Perl(mode.Fundamental): if a.config.get('perl.libs', None): s = ':'.join(['%s' % x for x in a.config.get('perl.libs')]) cmd = 'PERL5LIB=%r %s' % (s, cmd) - elif a.config.get('perl.lib', None): - cmd = 'PERL5LIB=%r %s' % (a.config.get('perl.lib'), cmd) - (status, data) = commands.getstatusoutput(cmd) + if status != 0: raise Exception, "%r failed" % cmd self.perlinc = data.split('\n')