pmacs3/mode/iperlmini.py

149 lines
5.0 KiB
Python
Raw Permalink Normal View History

import re, string
import buffer, completer, mode.mini, mode.consolemini
from window import Window
from method import Method
from mode import Fundamental
from subprocess import Popen, PIPE, STDOUT
from mode.perl import PerlGrammar
from point import Point
PAD = ' '
LIMIT = 79
class IperlExec(Method):
def _execute(self, w, **vargs):
2008-05-26 23:57:09 -04:00
if w.application.completion_window_is_open():
w.application.close_completion_buffer()
s = w.buffer.make_string()
w.mode.history[-1] = s
w.mode.history.append('')
w.buffer.set_data('')
w.mode.hindex = len(w.mode.history) - 1
a = w.application
2008-05-28 15:22:45 -04:00
b = w.mode.get_iperl()
if a.window().buffer is not b:
a.switch_buffer(b)
p = a.get_mini_buffer_prompt()
b.insert_string(b.get_buffer_end(), p + s + '\n', force=True)
2008-05-26 23:57:09 -04:00
b.pipe.stdin.write("ENTER:%s\n" % s)
b.pipe.stdin.flush()
2008-05-28 18:13:40 -04:00
output = b.pipe_read()
2008-05-30 02:27:42 -04:00
if output:
b.insert_string(b.get_buffer_end(), output, force=True)
class IperlTab(Method):
def execute(self, w, **vargs):
a = w.application
s = w.buffer.make_string()
2008-05-28 15:22:45 -04:00
b = w.mode.get_iperl()
x2 = w.logical_cursor().x
if not s or s[:x2].isspace():
w.insert_string_at_cursor(' ' * w.mode.tabwidth)
return
r = re.compile('^[a-zA-Z0-9_:$@*&%]$')
line = s
x1 = x2
while x1 > 0 and r.match(s[x1 - 1]):
x1 -= 1
word = line[x1:x2]
2008-05-30 02:27:42 -04:00
candidates = b.readline_completions(x1, x2, s)
if candidates:
s = completer.find_common_string(candidates)
w.insert_string_at_cursor(s)
2008-05-30 02:27:42 -04:00
mode.mini.use_completion_window(a, s, [word+s for s in candidates])
class IperlPathStart(Method):
2008-05-28 14:58:55 -04:00
'''Interactively run perl statements in the context of a buffer'''
2008-05-28 18:13:40 -04:00
def _start(self, w, parent, switch=True):
a = w.application
2008-05-28 14:58:55 -04:00
if w.buffer.btype == 'iperl':
b = w.buffer
else:
2008-05-28 14:58:55 -04:00
name = buffer.IperlBuffer.create_name(parent)
if not a.has_buffer_name(name):
b = buffer.IperlBuffer(parent, a)
a.add_buffer(b)
Window(b, a)
2008-05-28 14:58:55 -04:00
else:
b = a.get_buffer_by_name(name)
self.main_buffer = b
2008-05-28 18:13:40 -04:00
if switch and a.window().buffer is not b:
a.switch_buffer(b)
2008-05-28 18:13:40 -04:00
if switch:
w.application.open_mini_buffer('*** ', lambda x: None, self, None, 'iperlmini')
def execute(self, w, switch=True):
self._start(w, w.buffer, switch)
2008-05-28 14:58:55 -04:00
class IperlStart(IperlPathStart):
'''Interactively run perl statements'''
2008-05-28 18:13:40 -04:00
def execute(self, w, switch=True):
self._start(w, None, switch=True)
class IperlPageUp(mode.consolemini.ConsolePageUp):
subbuf = '*IPerl*'
class IperlPageDown(mode.consolemini.ConsolePageDown):
subbuf = '*IPerl*'
class IperlGotoBeginning(mode.consolemini.ConsoleGotoBeginning):
subbuf = '*IPerl*'
class IperlGotoEnd(mode.consolemini.ConsoleGotoEnd):
subbuf = '*IPerl*'
class IperlMini(Fundamental):
name = 'IperlMini'
actions = [IperlExec, IperlTab, IperlStart, IperlPathStart,
IperlPageUp, IperlPageDown, IperlGotoBeginning, IperlGotoEnd]
readre = re.compile('^([A-Z]+):(.*)\n$')
def _readline(self):
2008-05-28 15:22:45 -04:00
b = self.get_iperl()
2008-05-26 23:57:09 -04:00
line = b.pipe.stdout.readline()
m = self.readre.match(line)
if m:
return (m.group(1), m.group(2))
else:
return ('RAW', line.rstrip())
def _read(self):
2008-05-28 15:22:45 -04:00
b = self.get_iperl()
output = []
while True:
(type_, value) = self._readline()
if type_ == 'PROMPT':
2008-05-26 23:57:09 -04:00
b.prompt = value.strip() + ' '
self.window.application.set_mini_buffer_prompt(b.prompt)
break
2008-05-16 09:24:17 -04:00
value.rstrip()
if value:
output.append(value)
2008-05-16 09:24:17 -04:00
if output:
return '\n'.join(output) + '\n'
else:
return ''
2008-05-28 15:22:45 -04:00
def get_iperl(self):
return self.window.buffer.method.main_buffer
2008-05-26 23:57:09 -04:00
def __init__(self, w):
Fundamental.__init__(self, w)
self.history = ['']
self.hindex = 0
2008-05-28 15:22:45 -04:00
b = self.get_iperl()
2008-05-28 14:58:55 -04:00
w.application.set_mini_buffer_prompt(b.prompt)
self.add_bindings('iperl-exec', ('RETURN',))
self.add_bindings('console-clear', ('C-l',))
2008-06-10 14:29:19 -04:00
self.add_bindings('console-cancel', ('C-]', 'C-g'))
self.add_bindings('console-history-prev', ('C-p', 'UP'))
self.add_bindings('console-history-next', ('C-n', 'DOWN'))
self.add_bindings('iperl-tab', ('TAB',))
self.add_bindings('iperl-page-up', ('M-v',))
self.add_bindings('iperl-page-down', ('C-v',))
self.add_bindings('iperl-goto-beginning', ('M-<',))
self.add_bindings('iperl-goto-end', ('M->',))
for c in string.ascii_letters + string.digits + string.punctuation:
self.add_binding('insert-string-%s' % c, c)
install = IperlMini.install