2007-10-21 20:55:29 -04:00
|
|
|
import commands, dirutil, grp, method, mode, os.path, pwd, re
|
2009-06-09 02:12:07 -04:00
|
|
|
import util
|
2008-10-29 10:58:06 -04:00
|
|
|
import buffer, buffer.fs
|
2009-03-29 20:50:27 -04:00
|
|
|
from window import Window
|
2009-03-28 22:20:37 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule, PatternMatchRule
|
2007-10-21 20:50:11 -04:00
|
|
|
from point import Point
|
2009-07-09 16:23:13 -04:00
|
|
|
from method import Method, Argument, arg
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
class PermGrammar(Grammar):
|
|
|
|
rules = [
|
2009-06-11 00:00:33 -04:00
|
|
|
PatternRule('sticky', '[tT]'),
|
|
|
|
PatternRule('setid', '[sS]'),
|
|
|
|
PatternRule('read', 'r'),
|
|
|
|
PatternRule('write', 'w'),
|
|
|
|
PatternRule('exec', 'x'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
2009-03-28 22:20:37 -04:00
|
|
|
ds = r'([^ ]+)( +)([^ ]+)( +)([^ ]+)( +)([A-Za-z]{3} [ 0-9]{2} [0-9]{2}:[0-9]{2})( +)([^\n]+)'
|
2007-07-21 11:40:53 -04:00
|
|
|
class PathGrammar(Grammar):
|
|
|
|
rules = [
|
2009-06-11 00:00:33 -04:00
|
|
|
RegionRule('perm', '(?<=^.)', PermGrammar, ' '),
|
|
|
|
PatternMatchRule('x', ds, 'dir.owner', 'spaces', 'dir.group', 'spaces',
|
|
|
|
'dir.size', 'spaces', 'dir.mtime', 'spaces',
|
|
|
|
'name', 'spaces'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
class DirGrammar(Grammar):
|
|
|
|
rules = [
|
2009-06-11 00:00:33 -04:00
|
|
|
RegionRule('dir.file', '^-', PathGrammar, r'\n'),
|
|
|
|
RegionRule('dir.blk', '^b', PathGrammar, r'\n'),
|
|
|
|
RegionRule('dir.chr', '^c', PathGrammar, r'\n'),
|
|
|
|
RegionRule('dir.dir', '^d', PathGrammar, r'\n'),
|
|
|
|
RegionRule('dir.lnk', '^l', PathGrammar, r'\n'),
|
|
|
|
RegionRule('dir.fifo', '^p', PathGrammar, r'\n'),
|
|
|
|
RegionRule('dir.sock', '^s', PathGrammar, r'\n'),
|
|
|
|
RegionRule('dir.unk', '^\?', PathGrammar, r'\n'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
2009-03-29 20:50:27 -04:00
|
|
|
class FsSettingBase(Method):
|
|
|
|
msg = 'Settings have changed'
|
|
|
|
def _doit(self, w, **vargs): pass
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
self._doit(w, **vargs)
|
|
|
|
w.buffer.reload()
|
|
|
|
w.goto_beginning()
|
|
|
|
w.set_error(self.msg)
|
|
|
|
|
|
|
|
class HideDotFiles(FsSettingBase):
|
|
|
|
msg = "Dotfiles are hidden"
|
|
|
|
def _doit(self, w, **vargs): w.buffer.settings['hide-dot'] = True
|
|
|
|
class ShowDotFiles(FsSettingBase):
|
|
|
|
msg = "Dotfiles are visible"
|
|
|
|
def _doit(self, w, **vargs): w.buffer.settings['hide-dot'] = False
|
|
|
|
|
|
|
|
class SortType(FsSettingBase):
|
|
|
|
msg = "Sorting files by type, name"
|
|
|
|
def _doit(self, w, **vargs): w.buffer.settings['type-sort'] = True
|
|
|
|
class SortName(FsSettingBase):
|
|
|
|
msg = "Sorting files by name, type"
|
|
|
|
def _doit(self, w, **vargs): w.buffer.settings['type-sort'] = False
|
|
|
|
|
2009-07-09 16:23:13 -04:00
|
|
|
class OpenArchive(Method):
|
|
|
|
args = [arg('path', dt="path", p="Open Archive: ", h="archive to open")]
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
path = vargs['path']
|
|
|
|
a = w.application
|
|
|
|
b = a.get_buffer_by_path(path)
|
|
|
|
if not b:
|
|
|
|
name = a.make_name(os.path.basename(path))
|
|
|
|
b = buffer.fs.ArchiveBuffer(path, name)
|
|
|
|
b.open()
|
|
|
|
Window(b, a, height=0, width=0, mode_name='dir')
|
|
|
|
a.add_buffer(b)
|
2009-07-11 21:57:41 -04:00
|
|
|
|
2009-07-09 16:23:13 -04:00
|
|
|
a.methods['switch-buffer'].execute(w, buffername=b.name())
|
2009-07-11 21:57:41 -04:00
|
|
|
if b.btype != 'archive': return
|
|
|
|
|
2009-07-09 16:23:13 -04:00
|
|
|
names = [x for x in b.get_names() if x != '.' and x != '..']
|
|
|
|
if len(names) == 1:
|
|
|
|
path2 = os.path.join(b.path, names[0])
|
2009-07-11 21:57:41 -04:00
|
|
|
if os.path.isdir(path2):
|
|
|
|
a.methods['open-file'].execute(w, filename=path2)
|
2009-07-09 16:23:13 -04:00
|
|
|
|
2009-06-10 23:53:51 -04:00
|
|
|
class DirRefresh(Method):
|
2007-07-21 11:40:53 -04:00
|
|
|
def _execute(self, w, **vargs):
|
2009-07-09 16:23:13 -04:00
|
|
|
t = None
|
|
|
|
try:
|
|
|
|
t = dirutil.resolve_token(w)
|
|
|
|
s = t.string
|
|
|
|
except:
|
|
|
|
pass
|
2007-07-21 11:40:53 -04:00
|
|
|
w.buffer.reload()
|
2009-07-09 16:23:13 -04:00
|
|
|
if t: dirutil.find_name(w, s)
|
2009-06-10 23:53:51 -04:00
|
|
|
class DirOpen(Method):
|
2007-07-21 11:40:53 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
path = dirutil.resolve_path(w)
|
|
|
|
w.set_error("opening %r" % path)
|
|
|
|
w.application.methods['open-file'].execute(w, filename=path)
|
2007-07-22 13:53:32 -04:00
|
|
|
class DirGrep(Method):
|
|
|
|
args = [Argument('pattern', datatype="str", prompt="Pattern: ")]
|
|
|
|
def _execute(self, w, **vargs):
|
2009-06-09 02:12:07 -04:00
|
|
|
cmd = 'grep -rEl "%s" %r' % (vargs['pattern'], w.buffer.path)
|
2007-07-22 13:53:32 -04:00
|
|
|
(status, output) = commands.getstatusoutput(cmd)
|
2009-06-09 02:12:07 -04:00
|
|
|
|
|
|
|
paths = []
|
|
|
|
suffixes = w.application.config['ignore_suffix']
|
|
|
|
for path in output.split('\n'):
|
|
|
|
if not path or util.should_ignore_path(path, suffixes):
|
|
|
|
continue
|
|
|
|
paths.append(path)
|
|
|
|
|
2007-07-22 13:53:32 -04:00
|
|
|
bufname = '*%s*' % self.name.title()
|
2008-10-29 10:58:06 -04:00
|
|
|
b = buffer.fs.PathListBuffer(bufname, paths)
|
2007-07-22 13:53:32 -04:00
|
|
|
b.modename = 'dir'
|
|
|
|
b.open()
|
2009-03-29 20:50:27 -04:00
|
|
|
Window(b, w.application, height=0, width=0)
|
2007-07-22 13:53:32 -04:00
|
|
|
w.application.add_buffer(b)
|
|
|
|
w.application.switch_buffer(b)
|
|
|
|
w.set_error("grep exited with %d" % status)
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
class DirCmd(Method):
|
|
|
|
def _make_cmd(self, w, path, **vargs):
|
|
|
|
return ''
|
|
|
|
def _run(self, w, **vargs):
|
|
|
|
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))
|
|
|
|
dirutil.find_name(w, basename)
|
|
|
|
|
2009-06-10 23:53:51 -04:00
|
|
|
class DirChmod(DirCmd):
|
2007-07-21 11:40:53 -04:00
|
|
|
args = [Argument('mode', type=type(''), prompt="New Mode: ")]
|
|
|
|
octal_re = re.compile('^[0-7]{1,4}$')
|
|
|
|
symbolic_re = re.compile('(?:[ugoa]*(?:[-+=](?:[rwxXst]*|[ugo]))+ *,?)+')
|
|
|
|
def _make_cmd(self, w, path, **vargs):
|
|
|
|
return 'chmod %r %r' % (vargs['mode'], path)
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
if self.octal_re.match(vargs['mode']):
|
|
|
|
pass
|
|
|
|
elif self.symbolic_re.match(vargs['mode']):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
w.set_error("Not a valid mode: %r" % vargs['mode'])
|
|
|
|
self._run(w, **vargs)
|
2009-06-10 23:53:51 -04:00
|
|
|
class DirChown(DirCmd):
|
2007-07-21 11:40:53 -04:00
|
|
|
args = [Argument('owner', type=type(''), prompt="New Owner: ")]
|
|
|
|
def _make_cmd(self, w, path, **vargs):
|
|
|
|
return 'chown %r %r' % (vargs['owner'], path)
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
fields = vargs['owner'].split(':')
|
|
|
|
if len(fields) == 1:
|
|
|
|
(owner, group) = (fields[0], None)
|
|
|
|
elif len(fields) == 2:
|
|
|
|
(owner, group) = fields
|
|
|
|
else:
|
|
|
|
w.set_error("Malformed 'owner' argument: %r" % vargs['owner'])
|
|
|
|
return
|
|
|
|
if not dirutil.valid_owner(owner):
|
|
|
|
w.set_error('User %r does not exist' % owner)
|
|
|
|
return
|
|
|
|
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)
|
2009-06-10 23:53:51 -04:00
|
|
|
class DirChgrp(DirCmd):
|
2007-07-21 11:40:53 -04:00
|
|
|
args = [Argument('group', type=type(''), prompt="New Group: ")]
|
|
|
|
def _make_cmd(self, w, path, **vargs):
|
|
|
|
return 'chgrp %r %r' % (vargs['group'], path)
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
if not dirutil.valid_group(vargs['group']):
|
|
|
|
w.set_error('Group %r does not exist' % group)
|
|
|
|
return
|
|
|
|
self._run(w, **vargs)
|
|
|
|
|
2009-06-10 23:53:51 -04:00
|
|
|
class DirTouch(Method):
|
2007-07-21 11:40:53 -04:00
|
|
|
args = [Argument('filename', datatype="path", prompt="Touch File: ")]
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
basename = vargs['filename']
|
|
|
|
path = os.path.join(w.buffer.path, basename)
|
|
|
|
retval = os.system('touch %r' % path)
|
2009-06-10 23:53:51 -04:00
|
|
|
w.application.methods['dir-refresh'].execute(w, filename=path)
|
2007-07-21 11:40:53 -04:00
|
|
|
dirutil.find_name(w, basename)
|
|
|
|
if retval != 0:
|
|
|
|
w.set_error("touch %r failed (exit %d)" % (path, retval))
|
2009-06-10 23:53:51 -04:00
|
|
|
class DirRemove(Method):
|
2007-07-21 11:40:53 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
self._old_window = 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)
|
|
|
|
def _callback(self, v):
|
|
|
|
a = self._old_window.application
|
|
|
|
if v == 'yes':
|
|
|
|
self._doit()
|
|
|
|
a.close_mini_buffer()
|
|
|
|
return
|
|
|
|
if v == 'no':
|
|
|
|
a.close_mini_buffer()
|
|
|
|
return
|
|
|
|
a.open_mini_buffer(self._prompt, self._callback)
|
|
|
|
a.set_error('Please type "yes" or "no"')
|
|
|
|
def _doit(self):
|
|
|
|
w = self._old_window
|
|
|
|
path = self._old_path
|
|
|
|
try:
|
|
|
|
w.application.methods['previous-line'].execute(w)
|
|
|
|
os.remove(path)
|
|
|
|
w.set_error("deleted %r " % path)
|
2009-06-10 23:53:51 -04:00
|
|
|
w.application.methods['dir-refresh'].execute(w, filename=path)
|
2007-07-21 11:40:53 -04:00
|
|
|
except:
|
|
|
|
w.set_error("failed to delete %r" % path)
|
2007-10-19 02:41:33 -04:00
|
|
|
|
2008-04-18 23:32:08 -04:00
|
|
|
class Dir(mode.Fundamental):
|
2009-03-17 15:24:10 -04:00
|
|
|
name = 'Dir'
|
2008-04-18 23:32:08 -04:00
|
|
|
grammar = DirGrammar()
|
2009-03-17 15:24:10 -04:00
|
|
|
colors = {
|
2009-06-11 00:00:33 -04:00
|
|
|
'dir.blk.start': ('cyan', 'default', 'bold'),
|
|
|
|
'dir.blk.name': ('cyan', 'default', 'bold'),
|
|
|
|
'dir.chr.start': ('yellow', 'default', 'bold'),
|
|
|
|
'dir.chr.name': ('yellow', 'default', 'bold'),
|
|
|
|
'dir.dir.start': ('blue', 'default', 'bold'),
|
|
|
|
'dir.dir.name': ('blue', 'default', 'bold'),
|
|
|
|
'dir.lnk.start': ('green', 'default', 'bold'),
|
|
|
|
'dir.lnk.name': ('green', 'default', 'bold'),
|
|
|
|
'dir.fifo.start': ('red', 'default', 'bold'),
|
|
|
|
'dir.fifo.name': ('red', 'default', 'bold'),
|
|
|
|
'dir.sock.start': ('red', 'default', 'bold'),
|
|
|
|
'dir.sock.name': ('red', 'default', 'bold'),
|
|
|
|
'dir.unk.start': ('magenta', 'default', 'bold'),
|
|
|
|
'dir.unk.name': ('magenta', 'default', 'bold'),
|
|
|
|
|
|
|
|
'perm.setid': ('yellow', 'default', 'bold'),
|
|
|
|
'perm.sticky': ('yellow', 'default', 'bold'),
|
|
|
|
'perm.read': ('magenta', 'default', 'bold'),
|
|
|
|
'perm.write': ('magenta', 'default', 'bold'),
|
|
|
|
'perm.exec': ('magenta', 'default', 'bold'),
|
|
|
|
|
|
|
|
'dir.owner': ('cyan', 'default', 'bold'),
|
|
|
|
'dir.group': ('cyan', 'default', 'bold'),
|
|
|
|
'dir.size': ('yellow', 'default', 'bold'),
|
|
|
|
'dir.mtime': ('green', 'default', 'bold'),
|
2008-04-18 23:32:08 -04:00
|
|
|
}
|
2009-06-10 23:53:51 -04:00
|
|
|
actions = [DirRefresh, DirOpen, DirGrep, DirChmod, DirChown, DirChgrp,
|
|
|
|
DirTouch, DirRemove, HideDotFiles, ShowDotFiles, SortName,
|
2009-07-09 16:23:13 -04:00
|
|
|
SortType,
|
|
|
|
OpenArchive]
|
2008-04-18 23:32:08 -04:00
|
|
|
def __init__(self, w):
|
|
|
|
mode.Fundamental.__init__(self, w)
|
2009-06-10 23:53:51 -04:00
|
|
|
self.add_bindings('dir-refresh', ('C-c r',))
|
|
|
|
self.add_bindings('dir-open', ('RETURN',))
|
2008-04-18 23:32:08 -04:00
|
|
|
self.add_bindings('dir-grep', ('C-c G',))
|
2009-06-10 23:53:51 -04:00
|
|
|
self.add_bindings('dir-chmod', ('C-c m',))
|
|
|
|
self.add_bindings('dir-chown', ('C-c o',))
|
|
|
|
self.add_bindings('dir-chgrp', ('C-c g',))
|
|
|
|
self.add_bindings('dir-touch', ('C-c t',))
|
|
|
|
self.add_bindings('dir-remove', ('DELETE', 'BACKSPACE', 'C-d'))
|
2008-04-18 23:32:08 -04:00
|
|
|
|
2007-10-19 02:41:33 -04:00
|
|
|
install = Dir.install
|