branch : pmacs2
This commit is contained in:
moculus 2007-07-21 14:37:02 +00:00
parent 22cf59cd0b
commit ba70cca295
4 changed files with 91 additions and 59 deletions

50
dirutil.py Normal file
View File

@ -0,0 +1,50 @@
import grp, os, pwd
from point2 import Point
def resolve_token(w):
c = w.logical_cursor()
p = Point(0, c.y)
return w.get_next_token_by_type(p, 'name')
def resolve_name(w):
t = resolve_token(w)
return t.string
def resolve_path(w):
name = resolve_name(w)
path = os.path.join(w.buffer.path, name)
return path
def find_name(w, s):
found = False
w.goto(Point(0, 0))
c = w.logical_cursor()
while not found and c.y < len(w.buffer.lines):
t = resolve_token(w)
if t.string == s:
found = True
break
w.goto(Point(c.x, c.y + 1))
c = w.logical_cursor()
if not found:
w.goto(Point(0, 0))
def valid_owner(owner):
if not owner:
return False
elif owner.isdigit():
return True
try:
pwd.getpwnam(owner)
return True
except:
return False
def valid_group(group):
if not group:
return False
elif group.isdigit():
return True
try:
grp.getgrnam(group)
return True
except:
return False

View File

@ -1,5 +1,5 @@
import os, commands, popen2, re, sets
import buffer2, default, regex, util, window2
import buffer2, default, dirutil, regex, util, window2
from point2 import Point
WHITESPACE = [' ', '\n']
@ -1470,6 +1470,31 @@ class Grep(Pipe):
def _parse(self, w, **vargs):
return ('grep', ('/usr/bin/grep', '-E', '-n', vargs['pattern']))
class Exec(Method):
args = [Argument('cmd', datatype="str", prompt="Exec: ")]
def _doit(self, w, path, cmd):
try:
cmd = cmd % path
except:
w.set_error("Malformed command: %r" % cmd)
return
(status, output) = commands.getstatusoutput(cmd)
bufname = '*%s*' % self.name.title()
w.application.data_buffer(bufname, output, switch_to=True)
w.set_error("Shell exited with %d" % status)
def _execute(self, w, **vargs):
if w.buffer.btype == 'dir':
name = dirutil.resolve_name(w)
path = dirutil.resolve_path(w)
self._doit(w, path, vargs['cmd'])
dirutil.find_name(w, name)
elif hasattr(w.buffer, 'path'):
path = w.buffer.path
self._doit(w, path, vargs['cmd'])
else:
w.set_error("Don't know how to exec: %r" % w.buffer)
return
class TokenComplete(Method):
'''Complete token names based on other tokens in the buffer'''
name_overrides = {}

View File

@ -159,6 +159,10 @@ class Fundamental(Handler):
self.add_bindings('set-mode', ('C-x m',))
self.add_bindings('cancel', ('C-]',))
self.add_bindings('exec', ('C-c e',))
self.add_bindings('grep', ('C-c g',))
self.add_bindings('pipe', ('C-c p',))
# unbound actions
self.add_action(method.GetToken())

View File

@ -1,4 +1,4 @@
import commands, grp, method, mode2, os.path, pwd, re
import commands, dirutil, grp, method, mode2, os.path, pwd, re
from lex3 import Grammar, PatternRule, RegionRule, PatternGroupRule
from point2 import Point
from method import Method, Argument
@ -74,41 +74,15 @@ class Dir(mode2.Fundamental):
def name(self):
return "Dir"
def _resolve_token(w):
c = w.logical_cursor()
p = Point(0, c.y)
return w.get_next_token_by_type(p, 'name')
def _resolve_name(w):
t = _resolve_token(w)
return t.string
def _resolve_path(w):
name = _resolve_name(w)
path = os.path.join(w.buffer.path, name)
return path
def _find_name(w, s):
found = False
w.goto(Point(0, 0))
c = w.logical_cursor()
while not found and c.y < len(w.buffer.lines):
t = _resolve_token(w)
if t.string == s:
found = True
break
w.goto(Point(c.x, c.y + 1))
c = w.logical_cursor()
if not found:
w.goto(Point(0, 0))
class RefreshView(Method):
def _execute(self, w, **vargs):
t = _resolve_token(w)
t = dirutil.resolve_token(w)
s = t.string
w.buffer.reload()
_find_name(w, s)
dirutil.find_name(w, s)
class OpenPath(Method):
def _execute(self, w, **vargs):
path = _resolve_path(w)
path = dirutil.resolve_path(w)
w.set_error("opening %r" % path)
w.application.methods['open-file'].execute(w, filename=path)
@ -116,14 +90,14 @@ class DirCmd(Method):
def _make_cmd(self, w, path, **vargs):
return ''
def _run(self, w, **vargs):
basename = _resolve_name(w)
basename = dirutil.resolve_name(w)
path = os.path.join(w.buffer.path, basename)
cmd = self._make_cmd(w, path, **vargs)
(status, output) = commands.getstatusoutput(cmd)
if status != 0:
w.set_error("%s failed (exit %d)" % (self.name, status))
w.application.methods['refresh-view'].execute(w, filename=path)
_find_name(w, basename)
dirutil.find_name(w, basename)
class Chmod(DirCmd):
args = [Argument('mode', type=type(''), prompt="New Mode: ")]
@ -140,27 +114,6 @@ class Chmod(DirCmd):
w.set_error("Not a valid mode: %r" % vargs['mode'])
self._run(w, **vargs)
def _valid_owner(owner):
if not owner:
return False
elif owner.isdigit():
return True
try:
pwd.getpwnam(owner)
return True
except:
return False
def _valid_group(group):
if not group:
return False
elif group.isdigit():
return True
try:
grp.getgrnam(group)
return True
except:
return False
class Chown(DirCmd):
args = [Argument('owner', type=type(''), prompt="New Owner: ")]
def _make_cmd(self, w, path, **vargs):
@ -174,10 +127,10 @@ class Chown(DirCmd):
else:
w.set_error("Malformed 'owner' argument: %r" % vargs['owner'])
return
if not _valid_owner(owner):
if not dirutil.valid_owner(owner):
w.set_error('User %r does not exist' % owner)
return
if group is not None and not _valid_group(group):
if group is not None and not dirutil.valid_group(group):
w.set_error('Group %r does not exist' % group)
return
self._run(w, **vargs)
@ -186,7 +139,7 @@ class Chgrp(DirCmd):
def _make_cmd(self, w, path, **vargs):
return 'chgrp %r %r' % (vargs['group'], path)
def _execute(self, w, **vargs):
if not _valid_group(vargs['group']):
if not dirutil.valid_group(vargs['group']):
w.set_error('Group %r does not exist' % group)
return
self._run(w, **vargs)
@ -198,13 +151,13 @@ class TouchPath(Method):
path = os.path.join(w.buffer.path, basename)
retval = os.system('touch %r' % path)
w.application.methods['refresh-view'].execute(w, filename=path)
_find_name(w, basename)
dirutil.find_name(w, basename)
if retval != 0:
w.set_error("touch %r failed (exit %d)" % (path, retval))
class RemovePath(Method):
def _execute(self, w, **vargs):
self._old_window = w
self._old_path = _resolve_path(w)
self._old_path = dirutil.resolve_path(w)
basename = os.path.basename(self._old_path)
self._prompt = "Do you want to delete %r? " % basename
w.application.open_mini_buffer(self._prompt, self._callback)