2007-03-06 10:05:38 -05:00
|
|
|
#!/usr/bin/env python
|
2007-06-04 03:29:37 -04:00
|
|
|
import curses, curses.ascii, getpass, os, re, string, sets, sys, termios, time
|
2007-03-06 10:05:38 -05:00
|
|
|
import traceback
|
|
|
|
|
2008-03-21 02:08:17 -04:00
|
|
|
import buffer, bufferlist, color, completer, keyinput, method, minibuffer, mode
|
2007-10-21 20:55:29 -04:00
|
|
|
import util, window
|
2007-10-21 20:50:11 -04:00
|
|
|
from point import Point
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
def run(buffers, jump_to_line=None, init_mode=None):
|
|
|
|
# save terminal state so we can restore it when the program exits
|
|
|
|
attr = termios.tcgetattr(sys.stdin)
|
|
|
|
keyinput.disable_control_chars()
|
|
|
|
|
|
|
|
retval = 1
|
|
|
|
try:
|
|
|
|
retval = curses.wrapper(run_app, buffers, jump_to_line, init_mode)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
# restore terminal state
|
|
|
|
termios.tcsetattr(sys.stdin, termios.TCSANOW, attr)
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def run_app(stdscr, buffers, jump_to_line=None, init_mode=None):
|
|
|
|
a = Application(stdscr, buffers, jump_to_line, init_mode)
|
|
|
|
a.run()
|
|
|
|
|
|
|
|
KILL_RING_LIMIT = 128
|
|
|
|
WORD_LETTERS = list(string.letters + string.digits)
|
|
|
|
ERROR_TIMEOUT = -1
|
|
|
|
#ERROR_TIMEOUT = 2
|
|
|
|
|
|
|
|
#DARK_BACKGROUND = False
|
|
|
|
DARK_BACKGROUND = True
|
|
|
|
|
2007-06-13 22:18:41 -04:00
|
|
|
class Application(object):
|
2007-03-06 10:05:38 -05:00
|
|
|
def __init__(self, stdscr, buffers=[], jump_to_line=None, init_mode=None):
|
|
|
|
# initalize curses primitives
|
|
|
|
self.stdscr = stdscr
|
2007-07-03 12:53:14 -04:00
|
|
|
(self.y, self.x) = self.stdscr.getmaxyx()
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# initialize some basic stuff
|
2008-03-16 16:08:37 -04:00
|
|
|
# each highlighted_range contains three things: (window, start_p, end_p)
|
2008-03-20 21:58:30 -04:00
|
|
|
self.config = {}
|
2007-03-06 10:05:38 -05:00
|
|
|
self.highlighted_ranges = []
|
2007-07-03 12:53:14 -04:00
|
|
|
self.mini_active = False
|
|
|
|
self.mini_buffer = None
|
|
|
|
self.mini_prompt = ""
|
|
|
|
self.error_string = ""
|
|
|
|
self.error_timestamp = None
|
|
|
|
self.input = keyinput.Handler()
|
2008-03-16 02:16:41 -04:00
|
|
|
# let's prepopulate some default token colors
|
|
|
|
self.token_colors = {
|
2008-03-16 10:53:35 -04:00
|
|
|
'comment': ('red', 'default'),
|
|
|
|
'comment.start': ('red', 'default'),
|
|
|
|
'comment.null': ('red', 'default'),
|
|
|
|
'comment.end': ('red', 'default'),
|
2008-03-16 11:43:07 -04:00
|
|
|
|
2008-03-16 10:53:35 -04:00
|
|
|
'continuation': ('red', 'default'),
|
2008-03-16 11:43:07 -04:00
|
|
|
|
2008-03-16 10:53:35 -04:00
|
|
|
'string.start': ('green', 'default'),
|
|
|
|
'string.null': ('green', 'default'),
|
2008-03-16 11:43:07 -04:00
|
|
|
'string.hex': ('magenta', 'default'),
|
2008-03-16 10:53:35 -04:00
|
|
|
'string.octal': ('magenta', 'default'),
|
|
|
|
'string.escaped': ('magenta', 'default'),
|
|
|
|
'string.end': ('green', 'default'),
|
2008-03-16 11:43:07 -04:00
|
|
|
|
2008-03-16 16:08:37 -04:00
|
|
|
'char': ('default', 'default'),
|
|
|
|
'integer': ('default', 'default'),
|
|
|
|
'float': ('default', 'default'),
|
2008-03-16 11:43:07 -04:00
|
|
|
|
2008-03-16 10:53:35 -04:00
|
|
|
'label': ('magenta', 'default'),
|
|
|
|
'keyword': ('cyan', 'default'),
|
|
|
|
'function': ('blue', 'default'),
|
|
|
|
'builtin': ('magenta', 'default'),
|
|
|
|
'method': ('cyan', 'default'),
|
|
|
|
'bareword': ('default', 'default'),
|
|
|
|
'delimiter': ('default', 'default'),
|
2008-03-16 02:16:41 -04:00
|
|
|
}
|
2008-03-16 10:53:35 -04:00
|
|
|
self.default_color = ('default', 'default',)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# initialize our colors
|
|
|
|
if curses.has_colors():
|
|
|
|
curses.start_color()
|
|
|
|
try:
|
|
|
|
curses.use_default_colors()
|
|
|
|
color.default_color = True
|
|
|
|
except:
|
|
|
|
# guess we weren't on 2.4
|
|
|
|
color.default_color = False
|
|
|
|
color.init()
|
|
|
|
|
|
|
|
# this is how we can change color settings
|
|
|
|
if curses.can_change_color():
|
|
|
|
#curses.init_color(curses.COLOR_BLUE, 750, 400, 0)
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.set_error("Dynamic color not available")
|
|
|
|
|
|
|
|
# initialize our modes
|
2007-10-18 17:07:35 -04:00
|
|
|
# the first dict stores our modes by (lowercase) name, and the other
|
|
|
|
# dicts all store various ways to "auto-detecting" the correct mode,
|
|
|
|
# in the precedence with which they are used.
|
|
|
|
self.modes = {}
|
|
|
|
self.mode_paths = {}
|
|
|
|
self.mode_basenames = {}
|
|
|
|
self.mode_extensions = {}
|
|
|
|
self.mode_detection = {}
|
|
|
|
|
2007-10-19 03:01:34 -04:00
|
|
|
# ok, now let's load all the "standard" modes
|
2007-10-21 20:55:29 -04:00
|
|
|
mode.install(self)
|
2007-11-06 21:51:57 -05:00
|
|
|
names = ('about', 'blame', 'c', 'console', 'consolemini', 'css', 'diff',
|
|
|
|
'dir', 'elisp', 'hex', 'html', 'java', 'javascript', 'lisp',
|
|
|
|
'make', 'mini', 'mutt', 'nasm', 'ocaml', 'perl', 'python',
|
|
|
|
'replace', 'rst', 'scheme', 'search', 'sh', 'sql', 'tt',
|
2008-01-27 15:10:13 -05:00
|
|
|
'text', 'text2', 'which', 'xml', 'cheetah')
|
2007-11-06 21:51:57 -05:00
|
|
|
for name in names:
|
2007-10-19 03:01:34 -04:00
|
|
|
exec("import mode.%s; mode.%s.install(self)" % (name, name))
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# initialize our methods
|
|
|
|
self.methods = {}
|
2008-03-17 03:16:15 -04:00
|
|
|
names = ('method', 'method.svn', 'method.cvs', 'method.search',
|
|
|
|
'method.buffers', 'method.move', 'method.shell',
|
|
|
|
'method.introspect', 'method.help')
|
|
|
|
for name in names:
|
2008-03-17 02:17:51 -04:00
|
|
|
exec("import %s" % name)
|
|
|
|
mod = eval(name)
|
|
|
|
for mname in dir(mod):
|
|
|
|
cls = eval("%s.%s" % (name, mname))
|
|
|
|
if hasattr(cls, '_is_method') and cls._is_method:
|
|
|
|
self.methods[cls._name()] = cls()
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# create all the insert methods for the character ranges we like
|
|
|
|
for c in string.letters + string.digits + string.punctuation:
|
|
|
|
obj = method.InsertString(c)
|
|
|
|
self.methods[obj.name] = obj
|
2007-10-12 22:58:17 -04:00
|
|
|
obj = method.OverwriteChar(c)
|
|
|
|
self.methods[obj.name] = obj
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# window/slot height/width
|
|
|
|
height = self.y - 2
|
|
|
|
width = self.x - 1
|
|
|
|
|
2007-10-18 11:28:55 -04:00
|
|
|
# run user custom code here
|
2007-10-18 11:48:39 -04:00
|
|
|
self.rcerror = None
|
2007-10-18 11:28:55 -04:00
|
|
|
self.loadrc()
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
# initialize our buffers
|
|
|
|
# note that the first buffer in buffers will be initially visible
|
2007-11-06 21:51:57 -05:00
|
|
|
buffers.append(buffer.AboutBuffer())
|
2007-10-21 20:50:11 -04:00
|
|
|
buffers.append(buffer.ScratchBuffer())
|
|
|
|
buffers.append(buffer.ConsoleBuffer())
|
2007-11-06 21:51:57 -05:00
|
|
|
|
2007-10-19 03:01:34 -04:00
|
|
|
if self.rcerror:
|
2007-10-21 20:50:11 -04:00
|
|
|
buffers.insert(0, buffer.DataBuffer('*RcError*', self.rcerror))
|
2007-10-19 03:01:34 -04:00
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
self.bufferlist = bufferlist.BufferList(height, width)
|
|
|
|
self.active_slot = 0
|
|
|
|
|
|
|
|
# build windows for our buffers
|
|
|
|
for b in buffers:
|
2007-06-24 09:51:43 -04:00
|
|
|
if b.name() == '*Console*':
|
2007-11-06 21:51:57 -05:00
|
|
|
window.Window(b, self)
|
2007-06-24 09:51:43 -04:00
|
|
|
else:
|
2007-11-06 21:51:57 -05:00
|
|
|
window.Window(b, self, mode_name=init_mode)
|
2007-03-06 10:05:38 -05:00
|
|
|
self.bufferlist.add_buffer(b)
|
2007-06-04 03:29:37 -04:00
|
|
|
self.bufferlist.set_slot(0, buffers[0])
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# see if the user has requested that we go to a particular line
|
|
|
|
if jump_to_line:
|
2007-06-04 23:05:33 -04:00
|
|
|
w = self.bufferlist.slots[0].window
|
2008-03-18 11:03:11 -04:00
|
|
|
self.methods['goto-line'].execute(w, lineno=jump_to_line)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# initialize our kill ring and last action
|
2007-07-02 20:29:27 -04:00
|
|
|
self.kill_ring = []
|
|
|
|
self.kill_commands = ['kill', 'kill-region']
|
|
|
|
self.last_action = None
|
|
|
|
self.last_search = None
|
2007-03-06 10:05:38 -05:00
|
|
|
self.last_replace_before = None
|
2007-07-02 20:29:27 -04:00
|
|
|
self.last_replace_after = None
|
|
|
|
self.registers = {}
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# initialize tab handlers
|
2008-03-21 02:08:17 -04:00
|
|
|
method.DATATYPES['path'] = completer.FileCompleter()
|
|
|
|
method.DATATYPES['buffer'] = completer.BufferCompleter(self)
|
|
|
|
method.DATATYPES['command'] = completer.CommandCompleter()
|
|
|
|
method.DATATYPES['shell'] = completer.ShellCompleter()
|
|
|
|
method.DATATYPES['config'] = completer.ConfigCompleter()
|
|
|
|
method.DATATYPES['method'] = completer.MethodCompleter()
|
|
|
|
method.DATATYPES['register'] = completer.RegisterCompleter()
|
|
|
|
method.DATATYPES['mode'] = completer.ModeCompleter()
|
|
|
|
method.DATATYPES['perlfunction'] = completer.PerlFunctionCompleter()
|
|
|
|
method.DATATYPES['pythonfunction'] = completer.PythonFunctionCompleter()
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# set up curses
|
|
|
|
self.win = curses.newwin(self.y, self.x, 0, 0)
|
2007-07-02 20:05:58 -04:00
|
|
|
self.win.leaveok(0)
|
2007-03-06 10:05:38 -05:00
|
|
|
curses.meta(1)
|
2007-08-12 18:07:54 -04:00
|
|
|
curses.cbreak()
|
|
|
|
#curses.halfdelay(5)
|
2007-08-12 18:04:00 -04:00
|
|
|
curses.noecho()
|
|
|
|
curses.nonl()
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-10-19 03:01:34 -04:00
|
|
|
# this sets up a mode, as well as optionally adding information on when to
|
|
|
|
# auto-load the mode
|
2007-10-18 12:05:21 -04:00
|
|
|
def setmode(self, name, cls, paths=[], basenames=[], extensions=[], detection=[]):
|
|
|
|
self.modes[name] = cls
|
|
|
|
for p in paths:
|
|
|
|
self.mode_paths[p] = name
|
|
|
|
for b in basenames:
|
|
|
|
self.mode_basenames[b] = name
|
|
|
|
for e in extensions:
|
|
|
|
self.mode_extensions[e] = name
|
|
|
|
for d in detection:
|
|
|
|
self.mode_detection[d] = name
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
def globals(self):
|
|
|
|
return globals()
|
|
|
|
def locals(self):
|
|
|
|
return locals()
|
|
|
|
|
|
|
|
def add_slot(self):
|
2007-06-04 03:29:37 -04:00
|
|
|
# XYZ
|
2007-06-04 23:05:33 -04:00
|
|
|
b = self.bufferlist.slots[self.active_slot].window.buffer
|
2007-06-04 03:29:37 -04:00
|
|
|
n = self.bufferlist.add_slot()
|
|
|
|
self.bufferlist.set_slot(n, b)
|
|
|
|
def remove_slot(self, n):
|
2007-03-06 10:05:38 -05:00
|
|
|
assert len(self.bufferlist.slots) > 1, "oh no you didn't!"
|
2007-06-04 03:29:37 -04:00
|
|
|
assert n >= 0 and n < len(self.bufferlist.slots), \
|
|
|
|
"invalid slot: %r (%r)" % (n, len(self.bufferlist.slots))
|
|
|
|
self.bufferlist.remove_slot(n)
|
2007-06-14 08:41:55 -04:00
|
|
|
if self.active_slot > n:
|
2007-06-13 22:18:41 -04:00
|
|
|
self.active_slot = max(0, self.active_slot - 1) #XYZ
|
2007-03-06 10:05:38 -05:00
|
|
|
def single_slot(self):
|
|
|
|
while len(self.bufferlist.slots) > 1:
|
|
|
|
if self.active_slot == 0:
|
|
|
|
self.remove_slot(1)
|
|
|
|
else:
|
|
|
|
self.remove_slot(0)
|
|
|
|
|
2007-06-04 03:29:37 -04:00
|
|
|
def get_window_height_width(self, i):
|
|
|
|
assert i >= 0 and i < len(self.bufferlist.slots), \
|
2007-03-06 10:05:38 -05:00
|
|
|
"invalid slot: %r" % slotname
|
2007-06-04 03:29:37 -04:00
|
|
|
slot = self.bufferlist.slots[i]
|
2007-03-06 10:05:38 -05:00
|
|
|
return (slot.height, slot.width)
|
|
|
|
|
2007-07-19 12:38:17 -04:00
|
|
|
# files and stuff
|
2007-10-12 16:26:17 -04:00
|
|
|
def open_path(self, path, binary=False, cipher=None, password=None):
|
2007-07-19 12:38:17 -04:00
|
|
|
path = os.path.abspath(os.path.realpath(util.expand_tilde(path)))
|
|
|
|
b = self.get_buffer_by_path(path)
|
|
|
|
if b is None:
|
|
|
|
name = os.path.basename(path)
|
|
|
|
if self.has_buffer_name(name):
|
|
|
|
i = 1
|
|
|
|
auxname = '%s/%d' % (name, i)
|
|
|
|
while self.has_buffer_name(auxname):
|
|
|
|
i += 1
|
|
|
|
auxname = '%s/%d' % (name, i)
|
|
|
|
name = auxname
|
|
|
|
|
|
|
|
mode_name = None
|
|
|
|
if cipher is None:
|
|
|
|
if not os.path.exists(path) or os.path.isfile(path):
|
2007-10-12 16:26:17 -04:00
|
|
|
if binary:
|
2007-10-21 20:50:11 -04:00
|
|
|
b = buffer.Binary32Buffer(path, name=name)
|
2007-10-12 16:26:17 -04:00
|
|
|
else:
|
2007-10-21 20:50:11 -04:00
|
|
|
b = buffer.FileBuffer(path, name=name)
|
2007-07-19 12:38:17 -04:00
|
|
|
elif os.path.isdir(path):
|
2007-10-21 20:50:11 -04:00
|
|
|
b = buffer.DirBuffer(path, name=name)
|
2007-07-19 14:28:21 -04:00
|
|
|
mode_name = 'dir'
|
2007-07-19 12:38:17 -04:00
|
|
|
else:
|
|
|
|
raise Exception, "not a file or dir: %r" % path
|
|
|
|
elif cipher == 'aes':
|
|
|
|
if not password:
|
|
|
|
raise Exception, "password is required"
|
|
|
|
if not os.path.exists(path) or os.path.isfile(path):
|
2007-10-21 20:50:11 -04:00
|
|
|
b = buffer.AesBuffer(path, password, name=name)
|
2007-07-19 12:38:17 -04:00
|
|
|
else:
|
|
|
|
raise Exception, "not a file or dir: %r" % path
|
|
|
|
b.open()
|
2007-10-21 20:55:29 -04:00
|
|
|
window.Window(b, self, height=0, width=0, mode_name=mode_name)
|
2007-07-19 12:38:17 -04:00
|
|
|
self.add_buffer(b)
|
|
|
|
return b
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
# mini buffer handling
|
|
|
|
def get_mini_buffer(self):
|
|
|
|
return self.mini_buffer
|
|
|
|
def mini_buffer_is_open(self):
|
|
|
|
return self.mini_buffer is not None
|
2007-06-18 14:50:48 -04:00
|
|
|
def open_mini_buffer(self, prompt, callback, method=None, tabber=None, modename=None):
|
2007-03-06 10:05:38 -05:00
|
|
|
if self.mini_buffer_is_open():
|
|
|
|
self.close_mini_buffer()
|
|
|
|
self.mini_prompt = prompt
|
2007-06-04 23:05:33 -04:00
|
|
|
self.mini_buffer = minibuffer.MiniBuffer(callback, method, tabber, modename)
|
2007-06-18 14:50:48 -04:00
|
|
|
try:
|
2007-10-21 20:55:29 -04:00
|
|
|
window.Window(self.mini_buffer, self, height=1,
|
2007-06-18 14:50:48 -04:00
|
|
|
width=self.x-1-len(self.mini_prompt)-1)
|
|
|
|
self.mini_active = True
|
|
|
|
except minibuffer.MiniBufferError:
|
|
|
|
self.mini_buffer = None
|
|
|
|
self.mini_prompt = ''
|
2007-03-06 10:05:38 -05:00
|
|
|
def exec_mini_buffer(self):
|
|
|
|
self.mini_buffer.callback(self.mini_buffer.make_string())
|
|
|
|
self.close_mini_buffer()
|
|
|
|
def close_mini_buffer(self):
|
2007-06-18 14:50:48 -04:00
|
|
|
self.mini_active = False
|
2007-03-06 10:05:38 -05:00
|
|
|
if self.mini_buffer_is_open():
|
|
|
|
self.mini_buffer.close()
|
|
|
|
self.mini_buffer = None
|
|
|
|
self.mini_prompt = ""
|
2007-06-18 14:50:48 -04:00
|
|
|
assert not self.mini_active
|
2007-03-06 10:05:38 -05:00
|
|
|
def get_mini_buffer_prompt(self):
|
|
|
|
return self.mini_prompt
|
|
|
|
def set_mini_buffer_prompt(self, p):
|
|
|
|
self.mini_prompt = p
|
|
|
|
|
|
|
|
# window handling
|
|
|
|
def toggle_window(self):
|
|
|
|
assert 0 <= self.active_slot and self.active_slot < len(self.bufferlist.slots)
|
2007-06-13 22:18:41 -04:00
|
|
|
self.active_slot = (self.active_slot + 1) % len(self.bufferlist.slots) #XYZ
|
2007-03-06 10:05:38 -05:00
|
|
|
def window(self):
|
2007-06-04 23:05:33 -04:00
|
|
|
return self.bufferlist.slots[self.active_slot].window
|
2007-03-06 10:05:38 -05:00
|
|
|
def active_window(self):
|
|
|
|
if self.mini_active:
|
2007-06-04 23:05:33 -04:00
|
|
|
return self.mini_buffer.windows[0]
|
2007-03-06 10:05:38 -05:00
|
|
|
else:
|
|
|
|
assert 0 <= self.active_slot and self.active_slot < len(self.bufferlist.slots), \
|
|
|
|
"0 <= %d < %d" % (self.active_slot, len(self.bufferlist.slots))
|
2007-06-04 03:29:37 -04:00
|
|
|
i = self.active_slot
|
|
|
|
return self.bufferlist.slots[i].window
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# buffer handling
|
|
|
|
def file_buffer(self, path, data, switch_to=True):
|
|
|
|
assert not self.has_buffer_name(path), 'oh no! %r is already open' % path
|
|
|
|
assert not os.path.exists(path), 'oh no! %r already exists in fs' % path
|
|
|
|
f = open(path, 'w')
|
|
|
|
f.write(data)
|
|
|
|
f.close()
|
2007-10-21 20:50:11 -04:00
|
|
|
b = buffer.FileBuffer(path)
|
2007-03-06 10:05:38 -05:00
|
|
|
b.open()
|
2007-10-21 20:55:29 -04:00
|
|
|
window.Window(b, self, height=0, width=0)
|
2007-03-06 10:05:38 -05:00
|
|
|
self.add_buffer(b)
|
|
|
|
if switch_to:
|
|
|
|
self.switch_buffer(b)
|
|
|
|
def data_buffer(self, name, data, switch_to=True, modename=None):
|
|
|
|
if self.has_buffer_name(name):
|
|
|
|
b = self.bufferlist.buffer_names[name]
|
|
|
|
self.remove_buffer(b)
|
2007-10-21 20:50:11 -04:00
|
|
|
b = buffer.DataBuffer(name, data)
|
2007-03-06 10:05:38 -05:00
|
|
|
if modename is not None:
|
|
|
|
b.modename = modename
|
2007-10-21 20:55:29 -04:00
|
|
|
window.Window(b, self, height=0, width=0)
|
2007-03-06 10:05:38 -05:00
|
|
|
self.add_buffer(b)
|
|
|
|
if switch_to:
|
|
|
|
self.switch_buffer(b)
|
|
|
|
def get_buffer_by_path(self, path):
|
|
|
|
return self.bufferlist.get_buffer_by_path(path)
|
|
|
|
def has_buffer_name(self, name):
|
|
|
|
return self.bufferlist.has_buffer_name(name)
|
|
|
|
def get_buffer_by_name(self, name):
|
|
|
|
return self.bufferlist.get_buffer_by_name(name)
|
|
|
|
def has_buffer(self, b):
|
|
|
|
return self.bufferlist.has_buffer(b)
|
|
|
|
def add_buffer(self, b):
|
|
|
|
self.bufferlist.add_buffer(b)
|
|
|
|
def remove_buffer(self, b):
|
|
|
|
assert b.name() is not "*Scratch*", "can't kill the scratch"
|
|
|
|
assert self.bufferlist.has_buffer(b), "can't kill what's not there"
|
|
|
|
assert len(self.bufferlist.buffers) > 1, "can't kill with no other buffers"
|
|
|
|
self.bufferlist.remove_buffer(b)
|
|
|
|
b.close()
|
|
|
|
if self.bufferlist.empty_slot(self.active_slot):
|
|
|
|
b2 = self.bufferlist.hidden_buffers[0]
|
|
|
|
self.bufferlist.set_slot(self.active_slot, b2)
|
|
|
|
def switch_buffer(self, b):
|
|
|
|
assert self.has_buffer_name(b.name()), "buffer %s does not exist" % (b.name())
|
|
|
|
assert 0 <= self.active_slot and self.active_slot < len(self.bufferlist.slots)
|
2007-06-05 00:49:24 -04:00
|
|
|
#self.add_window_to_buffer(b, self.active_slot)
|
2007-03-06 10:05:38 -05:00
|
|
|
self.bufferlist.set_slot(self.active_slot, b)
|
|
|
|
|
|
|
|
def add_window_to_buffer(self, b, slotname):
|
2007-06-05 00:49:24 -04:00
|
|
|
# XYZ
|
2007-03-06 10:05:38 -05:00
|
|
|
if not b.has_window(slotname):
|
|
|
|
slot = self.bufferlist.slots[slotname]
|
2007-10-21 20:55:29 -04:00
|
|
|
window.Window(b, self, height=slot.height, width=slot.width)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# error string handling
|
|
|
|
def set_error(self, s):
|
|
|
|
self.error_string = s
|
|
|
|
self.error_timestamp = time.time()
|
|
|
|
def clear_error(self):
|
|
|
|
self.error_string = ""
|
|
|
|
self.error_timestamp = None
|
|
|
|
def resize_event(self):
|
2007-07-28 23:08:42 -04:00
|
|
|
(self.y, self.x) = self.stdscr.getmaxyx()
|
2007-03-06 10:05:38 -05:00
|
|
|
self.resize_slots()
|
|
|
|
def resize_slots(self):
|
|
|
|
n = len(self.bufferlist.slots)
|
2007-06-04 10:06:04 -04:00
|
|
|
assert n > 0
|
2007-03-06 10:05:38 -05:00
|
|
|
x = self.x - 1
|
|
|
|
y_sum = self.y - 1 - n
|
2007-07-28 23:08:42 -04:00
|
|
|
self.bufferlist.resize(y_sum, x)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# exit
|
|
|
|
def exit(self):
|
|
|
|
self.done = True
|
|
|
|
|
|
|
|
# kill stack manipulation
|
|
|
|
def push_kill(self, s):
|
|
|
|
if s is not None:
|
|
|
|
if self.last_action in self.kill_commands and \
|
2007-06-28 00:33:25 -04:00
|
|
|
len(self.kill_ring):
|
2007-03-06 10:05:38 -05:00
|
|
|
self.kill_ring[-1] = self.kill_ring[-1] + s
|
|
|
|
else:
|
|
|
|
self.kill_ring.append(s)
|
2007-07-02 20:29:27 -04:00
|
|
|
if KILL_RING_LIMIT and len(self.kill_ring) > KILL_RING_LIMIT:
|
2007-03-06 10:05:38 -05:00
|
|
|
self.kill_ring.pop(0)
|
|
|
|
def pop_kill(self):
|
|
|
|
return self.kill_ring.pop(-1)
|
|
|
|
def has_kill(self, i=-1):
|
|
|
|
return len(self.kill_ring) >= abs(i)
|
|
|
|
def get_kill(self, i=-1):
|
|
|
|
return self.kill_ring[i]
|
|
|
|
|
|
|
|
# undo/redo
|
|
|
|
def undo(self):
|
|
|
|
try:
|
2007-06-04 03:29:37 -04:00
|
|
|
self.window().undo()
|
2007-03-06 10:05:38 -05:00
|
|
|
except Exception, e:
|
|
|
|
self.set_error("%s" % (e))
|
|
|
|
def redo(self):
|
|
|
|
try:
|
2007-06-04 03:29:37 -04:00
|
|
|
self.window().redo()
|
2007-03-06 10:05:38 -05:00
|
|
|
except Exception, e:
|
|
|
|
self.set_error("%s" % (e))
|
|
|
|
|
|
|
|
# action creating methods
|
|
|
|
def make_insert_action(self, c):
|
|
|
|
return lambda: self.window().insert_string(c)
|
|
|
|
def make_window_action(self, methodname):
|
|
|
|
f = getattr(self.window(), methodname)
|
|
|
|
f()
|
|
|
|
|
|
|
|
# we are evil
|
|
|
|
def eval(self, s):
|
|
|
|
return eval(s)
|
|
|
|
|
2007-10-18 11:28:55 -04:00
|
|
|
# load user configuration NOW
|
|
|
|
def loadrc(self):
|
|
|
|
path = os.path.join(os.getenv('HOME'), '.pmc', 'conf')
|
|
|
|
if os.path.exists(path):
|
2007-10-18 11:48:39 -04:00
|
|
|
try:
|
2008-03-16 16:08:37 -04:00
|
|
|
f = open(path, 'r')
|
|
|
|
#execfile(path)
|
|
|
|
exec(f)
|
|
|
|
f.close()
|
2007-10-18 11:48:39 -04:00
|
|
|
except Exception, e:
|
2007-10-19 03:01:34 -04:00
|
|
|
s = traceback.format_exc()
|
|
|
|
self.rcerror = 'There was an error during startup:\n\n%s' % s
|
2007-10-18 11:28:55 -04:00
|
|
|
|
2007-07-18 23:06:21 -04:00
|
|
|
# the mighty run-loop!
|
2007-03-06 10:05:38 -05:00
|
|
|
def run(self):
|
|
|
|
self.done = False
|
2007-08-12 18:18:23 -04:00
|
|
|
self.draw()
|
2007-03-06 10:05:38 -05:00
|
|
|
while not self.done:
|
|
|
|
i = self.win.getch()
|
|
|
|
if i == curses.KEY_RESIZE:
|
|
|
|
while i == curses.KEY_RESIZE:
|
|
|
|
i = self.win.getch()
|
|
|
|
self.resize_event()
|
|
|
|
err = ''
|
|
|
|
try:
|
|
|
|
self.input.parse(i)
|
|
|
|
except Exception, e:
|
|
|
|
err = str(e)
|
|
|
|
while len(self.input.tokens):
|
|
|
|
t = self.input.tokens.pop(0)
|
|
|
|
self.active_window().mode.handle_token(t)
|
2007-08-12 18:18:23 -04:00
|
|
|
self.draw(err)
|
2007-07-18 23:06:21 -04:00
|
|
|
|
|
|
|
# clear the error line; it might look confusing to the user
|
|
|
|
try:
|
|
|
|
self.win.addstr(self.y-1, 0, ' ' * self.x)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
self.win.refresh()
|
2007-03-06 10:05:38 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
# highlighting
|
|
|
|
# each highlighted_range contains three things: [window, start_p, end_p]
|
2007-07-05 16:18:09 -04:00
|
|
|
def add_highlighted_range(self, w, p1, p2, fg='default', bg='default'):
|
|
|
|
self.highlighted_ranges.append([w, p1, p2, fg, bg])
|
2007-03-06 10:05:38 -05:00
|
|
|
def clear_highlighted_ranges(self):
|
|
|
|
self.highlighted_ranges = []
|
|
|
|
|
|
|
|
# full screen drawer
|
2007-08-12 18:18:23 -04:00
|
|
|
def draw(self, err=""):
|
2007-07-28 23:08:42 -04:00
|
|
|
try:
|
2007-09-26 11:56:07 -04:00
|
|
|
self.draw_slots()
|
2007-07-28 23:08:42 -04:00
|
|
|
self.draw_input_bar()
|
|
|
|
self.draw_cursor()
|
|
|
|
self.win.noutrefresh()
|
|
|
|
curses.doupdate()
|
2007-11-04 18:23:06 -05:00
|
|
|
except Exception, e:
|
2007-07-28 23:08:42 -04:00
|
|
|
# ok, so there was a problem...
|
|
|
|
# let's see if the screen changed sizes and if so, resize our slots
|
|
|
|
self.resize_event()
|
2007-08-12 18:18:23 -04:00
|
|
|
if err:
|
|
|
|
self.set_error(err)
|
|
|
|
if self.error_timestamp is not None and ERROR_TIMEOUT > 0 and \
|
|
|
|
time.time() - self.error_timestamp > ERROR_TIMEOUT:
|
|
|
|
self.clear_error()
|
|
|
|
(y, x) = self.stdscr.getmaxyx()
|
|
|
|
if y != self.y or x != self.x:
|
|
|
|
self.resize_event()
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-06-28 00:33:25 -04:00
|
|
|
def draw_cursor(self):
|
|
|
|
if self.mini_active:
|
|
|
|
b = self.mini_buffer
|
|
|
|
w = b.windows[0]
|
2007-07-08 19:16:53 -04:00
|
|
|
p = w.logical_cursor()
|
|
|
|
if p.y >= len(b.lines):
|
2007-06-28 00:33:25 -04:00
|
|
|
return
|
2007-07-08 19:16:53 -04:00
|
|
|
(vy, vx) = (self.y - 1, min(p.x + len(self.mini_prompt), self.x - 2))
|
2007-06-28 00:33:25 -04:00
|
|
|
else:
|
|
|
|
slot = self.bufferlist.slots[self.active_slot]
|
|
|
|
w = slot.window
|
|
|
|
if w.active_point is not None and w.point_is_visible(w.active_point):
|
|
|
|
p = w.active_point
|
|
|
|
else:
|
|
|
|
p = w.logical_cursor()
|
|
|
|
count = 0
|
|
|
|
(x, y) = w.first.xy()
|
2007-07-02 20:05:58 -04:00
|
|
|
(vy, vx) = (None, None)
|
2007-06-28 00:33:25 -04:00
|
|
|
while count < slot.height:
|
|
|
|
if p.y == y and p.x >= x and p.x <= x + slot.width:
|
2007-07-02 20:05:58 -04:00
|
|
|
(vy, vx) = (slot.offset + count, p.x - x)
|
2007-06-28 00:33:25 -04:00
|
|
|
break
|
|
|
|
if x + slot.width >= len(w.buffer.lines[y]):
|
|
|
|
x = 0
|
|
|
|
y += 1
|
|
|
|
else:
|
|
|
|
x += slot.width
|
|
|
|
count += 1
|
2007-07-02 20:05:58 -04:00
|
|
|
if vy is None or vx is None:
|
|
|
|
return
|
2007-07-08 19:16:53 -04:00
|
|
|
try:
|
|
|
|
self.win.move(vy, vx)
|
|
|
|
except:
|
|
|
|
raise Exception, "(%d,%d==%r) was illegal (%d,%d)" % \
|
|
|
|
(vx, vy, p, self.x, self.y)
|
2007-06-28 00:33:25 -04:00
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
# sub-drawing methods
|
|
|
|
def draw_slots(self):
|
|
|
|
self.win.erase()
|
|
|
|
for i in range(0, len(self.bufferlist.slots)):
|
|
|
|
slot = self.bufferlist.slots[i]
|
|
|
|
self.draw_slot(i)
|
|
|
|
self.draw_status_bar(i)
|
2007-07-28 23:08:42 -04:00
|
|
|
|
2007-07-05 16:18:09 -04:00
|
|
|
def highlight_char(self, sy, sx, fg='default', bg='default'):
|
2007-06-14 06:10:20 -04:00
|
|
|
junk = self.win.inch(sy, sx)
|
2007-11-04 18:23:06 -05:00
|
|
|
char = chr(junk & 255)
|
2007-07-05 16:18:09 -04:00
|
|
|
attr = color.build(fg, bg)
|
2007-07-08 19:16:53 -04:00
|
|
|
try:
|
2007-11-04 18:23:06 -05:00
|
|
|
self.win.addstr(sy, sx, char, attr)
|
|
|
|
except Exception, e:
|
2007-07-08 19:16:53 -04:00
|
|
|
raise Exception, "(%d, %d, %r, %r) v. (%d, %d)" % \
|
|
|
|
(sy, sx, fg, bg, self.y, self.x)
|
|
|
|
|
2007-07-05 16:18:09 -04:00
|
|
|
def highlight_chars(self, sy, sx1, sx2, fg='default', bg='default'):
|
2007-07-08 19:16:53 -04:00
|
|
|
assert sx2 < self.x, "%d < %d" % (sx2, self.x)
|
2007-06-14 06:10:20 -04:00
|
|
|
for x in range(sx1, sx2):
|
2007-07-05 16:18:09 -04:00
|
|
|
self.highlight_char(sy, x, fg, bg)
|
2007-06-14 06:10:20 -04:00
|
|
|
|
2007-06-04 03:29:37 -04:00
|
|
|
def draw_slot(self, i):
|
2007-06-13 22:18:41 -04:00
|
|
|
assert self.active_slot < len(self.bufferlist.slots), "only two"
|
|
|
|
assert i < len(self.bufferlist.slots), "only three"
|
2007-06-04 03:29:37 -04:00
|
|
|
slot = self.bufferlist.slots[i]
|
|
|
|
if slot.window is None:
|
2007-03-06 10:05:38 -05:00
|
|
|
return
|
2007-06-04 03:29:37 -04:00
|
|
|
w = slot.window
|
2007-06-05 00:49:24 -04:00
|
|
|
modename = w.mode.name()
|
2007-06-04 03:29:37 -04:00
|
|
|
|
2007-06-16 10:41:27 -04:00
|
|
|
if modename in w.buffer.highlights:
|
|
|
|
self._draw_slot_lit(i)
|
|
|
|
else:
|
|
|
|
self._draw_slot_raw(i)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-06-14 06:10:20 -04:00
|
|
|
# highlighted regions
|
2007-07-05 16:18:09 -04:00
|
|
|
for (high_w, p1, p2, fg, bg) in self.highlighted_ranges:
|
2007-07-03 12:53:14 -04:00
|
|
|
if w is high_w and p2 >= w.first and p1 <= w.last:
|
2007-06-14 06:10:20 -04:00
|
|
|
count = 0
|
|
|
|
(x, y) = w.first.xy()
|
|
|
|
px = p1.x
|
|
|
|
while count < slot.height:
|
2007-07-08 19:16:53 -04:00
|
|
|
if p1.y == y and px >= x and px - x < slot.width:
|
|
|
|
if slot.width > p2.x - x:
|
|
|
|
self.highlight_chars(slot.offset + count, px-x, p2.x-x, fg, bg)
|
2007-06-14 06:10:20 -04:00
|
|
|
break
|
|
|
|
else:
|
2007-07-08 19:16:53 -04:00
|
|
|
self.highlight_chars(slot.offset + count, px-x, slot.width, fg, bg)
|
|
|
|
px += slot.width - px + x
|
2007-06-17 10:55:36 -04:00
|
|
|
if x + slot.width >= len(w.buffer.lines[y]):
|
2007-06-14 06:10:20 -04:00
|
|
|
x = 0
|
|
|
|
y += 1
|
|
|
|
else:
|
|
|
|
x += slot.width
|
|
|
|
count += 1
|
|
|
|
|
2007-07-03 12:53:14 -04:00
|
|
|
if w.margins_visible:
|
|
|
|
for (limit, shade) in w.margins:
|
2007-07-10 14:55:40 -04:00
|
|
|
#if limit <= self.x:
|
|
|
|
if limit < self.x:
|
2007-06-04 14:06:07 -04:00
|
|
|
for j in range(0, slot.height):
|
2007-11-04 18:23:06 -05:00
|
|
|
char = chr(self.win.inch(j + slot.offset, limit) & 255)
|
2007-03-06 10:05:38 -05:00
|
|
|
attr = color.build('default', shade, 'bold')
|
2007-11-04 18:23:06 -05:00
|
|
|
self.win.addstr(j + slot.offset, limit, char, attr)
|
2007-06-13 11:44:09 -04:00
|
|
|
|
2007-06-16 10:41:27 -04:00
|
|
|
def _draw_slot_raw(self, i):
|
|
|
|
slot = self.bufferlist.slots[i]
|
|
|
|
w = slot.window
|
|
|
|
modename = w.mode.name()
|
|
|
|
redattr = color.build_attr(color.pairs('red', 'default'))
|
|
|
|
|
|
|
|
(x, y) = w.first.xy()
|
|
|
|
lines = w.buffer.lines
|
|
|
|
count = 0
|
|
|
|
while count < slot.height:
|
|
|
|
if y >= len(lines):
|
|
|
|
self.win.addstr(slot.offset + count, 0, '~', redattr)
|
2007-06-16 10:56:29 -04:00
|
|
|
count += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
line = lines[y]
|
|
|
|
s = line[x:x + slot.width]
|
2007-07-19 14:37:39 -04:00
|
|
|
try:
|
|
|
|
self.win.addstr(slot.offset + count, 0, s)
|
|
|
|
except:
|
|
|
|
self.set_error("addstr(%r + %r, %r, %r)" % (slot.offset, count, 0, s))
|
2007-06-16 10:41:27 -04:00
|
|
|
if x + slot.width >= len(line):
|
|
|
|
x = 0
|
|
|
|
y += 1
|
|
|
|
else:
|
2007-11-04 18:23:06 -05:00
|
|
|
self.win.addstr(slot.offset + count, slot.width, '\\', redattr)
|
2007-06-16 10:41:27 -04:00
|
|
|
x += slot.width
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
def _draw_slot_lit(self, i):
|
|
|
|
slot = self.bufferlist.slots[i]
|
|
|
|
w = slot.window
|
|
|
|
modename = w.mode.name()
|
|
|
|
redattr = color.build_attr(color.pairs('red', 'default'))
|
|
|
|
highlighter = w.buffer.highlights[modename]
|
|
|
|
|
|
|
|
(x, y) = w.first.xy()
|
|
|
|
j = 0
|
|
|
|
count = 0
|
|
|
|
assert len(w.buffer.lines) == len(highlighter.tokens)
|
|
|
|
while count < slot.height:
|
|
|
|
if y < len(w.buffer.lines):
|
|
|
|
while j < len(highlighter.tokens[y]):
|
|
|
|
token = highlighter.tokens[y][j]
|
2007-07-11 15:36:52 -04:00
|
|
|
if token.string.endswith('\n'):
|
|
|
|
tstring = token.string[:-1]
|
|
|
|
else:
|
|
|
|
tstring = token.string
|
|
|
|
|
2007-06-16 10:41:27 -04:00
|
|
|
assert token.y == y, '%d == %d' % (token.y, y)
|
|
|
|
|
|
|
|
s_offset = max(x - token.x, 0)
|
|
|
|
x_offset = max(token.x - x, 0)
|
2007-06-21 17:24:56 -04:00
|
|
|
assert x_offset <= slot.width, '%d <= %d' % (x_offset, slot.width)
|
2007-06-16 10:41:27 -04:00
|
|
|
|
2007-07-11 15:36:52 -04:00
|
|
|
s = tstring[s_offset:]
|
2007-06-16 10:41:27 -04:00
|
|
|
token_done = x_offset + len(s) <= slot.width
|
2007-06-16 10:56:29 -04:00
|
|
|
token_wrap = x_offset + len(s) > slot.width
|
2007-07-18 07:22:12 -04:00
|
|
|
attr = color.build(*token.color)
|
|
|
|
self.win.addstr(slot.offset + count, x_offset, s[:slot.width - x_offset], attr)
|
2007-06-16 10:41:27 -04:00
|
|
|
|
|
|
|
if token_wrap:
|
2007-11-04 18:23:06 -05:00
|
|
|
self.win.addstr(slot.offset + count, slot.width, '\\', redattr)
|
2007-06-16 10:41:27 -04:00
|
|
|
x += slot.width
|
|
|
|
count += 1
|
|
|
|
if token_done:
|
|
|
|
j += 1
|
2007-06-26 15:26:32 -04:00
|
|
|
if count >= slot.height:
|
|
|
|
break
|
2007-06-16 10:41:27 -04:00
|
|
|
|
|
|
|
# we have finished this logical line of tokens
|
2007-07-18 07:22:12 -04:00
|
|
|
j = x = 0
|
2007-06-16 10:41:27 -04:00
|
|
|
y += 1
|
|
|
|
count += 1
|
|
|
|
else:
|
|
|
|
self.win.addstr(slot.offset + count, 0, '~', redattr)
|
|
|
|
count += 1
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
def draw_status_bar(self, slotname):
|
|
|
|
slot = self.bufferlist.slots[slotname]
|
2007-06-04 03:29:37 -04:00
|
|
|
if slot.window is None:
|
2007-03-06 10:05:38 -05:00
|
|
|
return
|
|
|
|
|
2007-07-18 07:22:12 -04:00
|
|
|
w = slot.window
|
|
|
|
b = w.buffer
|
2007-03-06 10:05:38 -05:00
|
|
|
cursor = w.logical_cursor()
|
2007-07-18 07:22:12 -04:00
|
|
|
first = w.first
|
|
|
|
last = w.last
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
if b.readonly():
|
|
|
|
if b.changed():
|
|
|
|
modflag = '%*'
|
|
|
|
else:
|
|
|
|
modflag = '%%'
|
|
|
|
else:
|
|
|
|
if b.changed():
|
|
|
|
modflag = '**'
|
|
|
|
else:
|
|
|
|
modflag = '--'
|
|
|
|
|
|
|
|
if w.mark:
|
|
|
|
mark = w.mark
|
|
|
|
else:
|
2007-06-04 03:29:37 -04:00
|
|
|
mark = Point(-1, -1)
|
2007-03-06 10:05:38 -05:00
|
|
|
name = b.name()
|
|
|
|
|
|
|
|
if w.first_is_visible():
|
|
|
|
perc = "Top"
|
|
|
|
elif w.last_is_visible():
|
|
|
|
perc = "Bot"
|
|
|
|
else:
|
|
|
|
perc = "%2d%%" % (first.y*100 / len(b.lines))
|
|
|
|
|
2007-08-12 18:07:54 -04:00
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
# XYZ: we should actually use more of the 'state' variables
|
2007-07-06 14:34:14 -04:00
|
|
|
format = "%s %-18s (%s)--L%d--C%d--%s"
|
|
|
|
status = format % (modflag, name, w.mode.name(), cursor.y+1, cursor.x+1, perc)
|
|
|
|
#format = "%s %-18s (%s)--L%d--C%d--%s %s %s %s"
|
|
|
|
#status = format % (modflag, name, w.mode.name(), cursor.y+1, cursor.x+1, perc, w.first, cursor, w.last)
|
2007-07-02 20:05:58 -04:00
|
|
|
|
2007-08-12 18:07:54 -04:00
|
|
|
status = status.ljust(slot.width + 1)[:slot.width + 1]
|
|
|
|
self.win.addstr(slot.height + slot.offset, 0, status, curses.A_REVERSE)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# input bar drawing
|
|
|
|
def draw_input_bar(self):
|
|
|
|
if self.error_string:
|
|
|
|
self.draw_error()
|
|
|
|
elif self.mini_buffer_is_open():
|
|
|
|
self.draw_mini_buffer()
|
|
|
|
else:
|
|
|
|
self.draw_nothing()
|
|
|
|
try:
|
|
|
|
# fucking python, fucking curses, fucking fuck
|
2007-11-04 18:23:06 -05:00
|
|
|
self.win.addstr(self.y-1, self.x-1, ' ')
|
2007-03-06 10:05:38 -05:00
|
|
|
except:
|
|
|
|
pass
|
2007-09-26 11:56:07 -04:00
|
|
|
|
|
|
|
def _get_mini_lines(self, s):
|
|
|
|
i = self.x - 1
|
|
|
|
return [s[:i]]
|
|
|
|
|
|
|
|
lines = [s[:i]]
|
|
|
|
while i < len(s):
|
|
|
|
lines.append(s[i:self.x - 1 + i])
|
|
|
|
i += self.x - 1
|
|
|
|
return lines
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
def draw_error(self):
|
2007-09-26 11:56:07 -04:00
|
|
|
lines = self._get_mini_lines(self.error_string)
|
|
|
|
for i in range(0, len(lines)):
|
|
|
|
line = util.pad(lines[i], self.x - 1)
|
|
|
|
self.win.addstr(self.y - len(lines) + i, 0, line)
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
def draw_mini_buffer(self):
|
2007-06-04 23:05:33 -04:00
|
|
|
b = self.mini_buffer
|
2007-09-26 11:56:07 -04:00
|
|
|
lines = self._get_mini_lines(self.mini_prompt + b.lines[0])
|
|
|
|
for i in range(0, len(lines)):
|
|
|
|
line = util.pad(lines[i], self.x - 1)
|
|
|
|
self.win.addstr(self.y - len(lines) + i, 0, line)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
def draw_nothing(self):
|
2007-09-26 11:56:07 -04:00
|
|
|
self.win.addstr(self.y-1, 0, util.pad('', self.x - 1))
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2008-01-19 00:08:39 -05:00
|
|
|
def open_aes_file(path, nl, name=None, binary=False):
|
2007-07-20 10:51:36 -04:00
|
|
|
if os.path.isfile(path) or not os.path.exists(path):
|
2007-07-19 14:37:39 -04:00
|
|
|
p = getpass.getpass("Please enter the AES password: ")
|
2007-10-21 20:50:11 -04:00
|
|
|
return buffer.AesBuffer(path, p, nl, name)
|
2007-07-19 14:37:39 -04:00
|
|
|
else:
|
|
|
|
raise Exception, "can't open %r; unsupported file type" % path
|
2007-10-12 16:26:17 -04:00
|
|
|
def open_plain_file(path, nl, name=None, binary=False):
|
2007-07-20 10:51:36 -04:00
|
|
|
if os.path.isfile(path) or not os.path.exists(path):
|
2007-10-12 16:26:17 -04:00
|
|
|
if binary:
|
2007-10-21 20:50:11 -04:00
|
|
|
return buffer.Binary32Buffer(path, nl, name)
|
2007-10-12 16:26:17 -04:00
|
|
|
else:
|
2007-10-21 20:50:11 -04:00
|
|
|
return buffer.FileBuffer(path, nl, name)
|
2007-07-19 14:37:39 -04:00
|
|
|
elif os.path.isdir(path):
|
2007-10-21 20:50:11 -04:00
|
|
|
return buffer.DirBuffer(path, nl, name)
|
2007-07-19 14:37:39 -04:00
|
|
|
else:
|
|
|
|
raise Exception, "can't open %r; unsupported file type" % path
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2007-07-06 18:27:52 -04:00
|
|
|
ciphers = { 'none': open_plain_file, 'aes': open_aes_file }
|
|
|
|
linetypes = { 'win': '\r\n', 'mac': '\r', 'unix': '\n' }
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
import optparse
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.set_defaults(debug=False)
|
|
|
|
parser.set_defaults(goto=None)
|
|
|
|
parser.set_defaults(mode=None)
|
|
|
|
parser.set_defaults(cipher='none')
|
|
|
|
parser.set_defaults(linetype='unix')
|
2007-10-12 16:26:17 -04:00
|
|
|
parser.set_defaults(binary=False)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-10-12 16:26:17 -04:00
|
|
|
parser.add_option('-b', '--binary', dest='binary', action='store_true',
|
|
|
|
help='open file(s) in hex binary mode')
|
2007-03-06 10:05:38 -05:00
|
|
|
parser.add_option('-d', '--debug', dest='debug', action='store_true',
|
|
|
|
help='run in debug mode')
|
|
|
|
parser.add_option('-e', '--encrypt', dest='cipher', metavar='CIPHER',
|
|
|
|
help='decrypt and encrypt with CIPHER (default: none)')
|
|
|
|
parser.add_option('-g', '--goto', dest='goto', metavar='NUM', type='int',
|
|
|
|
help='jump to line NUM of the first argument')
|
|
|
|
parser.add_option('-l', '--line-end', dest='linetype', metavar='TYPE',
|
|
|
|
help='use TYPE (win,mac,unix) line endings (default: unix)')
|
|
|
|
parser.add_option('-m', '--mode', dest='mode', metavar='MODE',
|
|
|
|
help='open arguments in MODE')
|
|
|
|
|
|
|
|
(opts, args) = parser.parse_args()
|
|
|
|
|
|
|
|
# if debugging, disable error handling to produce backtraces
|
|
|
|
if opts.debug:
|
2007-10-21 20:55:29 -04:00
|
|
|
mode.DEBUG = True
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
# we will support using +19 as the first argument to indicate opening the
|
|
|
|
# first file on line 19 (same as -g 19 or --goto 19)
|
2007-06-23 16:12:00 -04:00
|
|
|
if len(args) > 0 and args[0].startswith('+'):
|
2007-03-06 10:05:38 -05:00
|
|
|
opts.goto = int(args[0][1:])
|
|
|
|
args = args[1:]
|
|
|
|
|
2007-06-04 23:05:33 -04:00
|
|
|
if opts.goto is not None:
|
|
|
|
opts.goto += 1
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
# figure out which kind of line types we're using
|
|
|
|
if opts.linetype not in linetypes:
|
|
|
|
sys.stderr.write('invalid linetype: %r' % opts.linetype)
|
|
|
|
sys.exit(1)
|
|
|
|
nl = linetypes[opts.linetype]
|
|
|
|
|
|
|
|
# figure out what kind of file open function to use
|
|
|
|
if opts.cipher not in ciphers:
|
|
|
|
sys.stderr.write('invalid cipher: %r' % opts.cipher)
|
|
|
|
sys.exit(2)
|
|
|
|
f = ciphers[opts.cipher]
|
|
|
|
|
|
|
|
# open each path using our callback to get a buffer, open that buffer, etc.
|
|
|
|
buffers = []
|
2007-07-18 07:22:12 -04:00
|
|
|
names = sets.Set()
|
|
|
|
paths = sets.Set()
|
2007-11-06 21:51:57 -05:00
|
|
|
#if not args:
|
|
|
|
# args = ['.']
|
2007-03-06 10:05:38 -05:00
|
|
|
for path in args:
|
|
|
|
path = os.path.abspath(os.path.realpath(util.expand_tilde(path)))
|
|
|
|
if path in paths:
|
|
|
|
continue
|
|
|
|
name = os.path.basename(path)
|
|
|
|
if name in names:
|
|
|
|
i = 1
|
|
|
|
auxname = '%s/%d' % (name, i)
|
|
|
|
while auxname in names:
|
|
|
|
i += 1
|
|
|
|
auxname = '%s/%d' % (name, i)
|
|
|
|
name = auxname
|
2007-10-12 19:32:17 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
b = f(path, nl, name, opts.binary)
|
|
|
|
b.open()
|
2007-10-21 20:50:11 -04:00
|
|
|
except buffer.BinaryDataException, e:
|
2007-10-12 19:32:17 -04:00
|
|
|
b = f(path, nl, name, True)
|
|
|
|
b.open()
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
buffers.append(b)
|
|
|
|
paths.add(path)
|
|
|
|
names.add(name)
|
|
|
|
|
|
|
|
# ok, now run our app
|
|
|
|
run(buffers, opts.goto, opts.mode)
|