Skip to content

Commit

Permalink
vi r command
Browse files Browse the repository at this point in the history
  • Loading branch information
Atsuo Ishimoto committed Jan 2, 2014
1 parent 4feb3f6 commit 1bf4074
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 23 deletions.
6 changes: 6 additions & 0 deletions kaa/commands/editmodecommand.py
Expand Up @@ -16,6 +16,12 @@ def editmode_insertmode(self, wnd):
kaa.app.messagebar.set_message(
'You are in insert mode now. Type `ESC` to return command mode.')

@commandid('editmode.replace')
def editmode_replacemode(self, wnd):
wnd.document.mode.editmode_replace(wnd)
kaa.app.messagebar.set_message(
'You are in replace mode now. Type `ESC` to return command mode.')

@commandid('editmode.visual')
def editmode_visualmode(self, wnd):
wnd.screen.selection.clear()
Expand Down
2 changes: 1 addition & 1 deletion kaa/commands/editorcommand.py
Expand Up @@ -487,7 +487,7 @@ def delete_word(self, wnd):
def delete_line(self, wnd):
pos = wnd.cursor.pos
nextpos = wnd.cursor.adjust_nextpos(
pos, wnd.document.find_newline(pos))
pos, wnd.document.get_line_to(pos))
if pos < nextpos:
wnd.document.mode.delete_string(wnd, pos, nextpos)

Expand Down
24 changes: 24 additions & 0 deletions kaa/commands/vicommand.py
@@ -0,0 +1,24 @@
import itertools
import re
import unicodedata
import kaa
from kaa.command import Commands, commandid, is_enable, norec, norerun
from kaa import document
from kaa.filetype.default import modebase
from kaa import doc_re


class ViCommands(Commands):

@commandid('edit.replace-char')
@norec
@norerun
def replace_char(self, wnd):
wnd.editmode.install_keyhook(self.hook_replacechar)

def hook_replacechar(self, wnd, keyevent):
s = keyevent.key[0]
if s >= ' ':
wnd.document.mode.put_string(wnd, s, overwrite=True)
return None
return keyevent
2 changes: 1 addition & 1 deletion kaa/cursor.py
Expand Up @@ -268,7 +268,7 @@ def first_letter(self, pos):
self.savecol()

def eol(self, pos):
eol = self.wnd.document.find_newline(pos)
eol = self.wnd.document.get_line_to(pos)
nextpos = self.adjust_nextpos(self.pos, eol)
self.wnd.screen.locate(nextpos, middle=True)
self.setpos(nextpos)
Expand Down
2 changes: 1 addition & 1 deletion kaa/document.py
Expand Up @@ -254,7 +254,7 @@ def gettol(self, pos):
tol = self.buf.rfindchr('\n', 0, pos)
return 0 if tol == -1 else tol + 1

def find_newline(self, pos):
def get_line_to(self, pos):
eol = self.buf.findchr('\n', pos, len(self.buf))
if eol == -1:
eol = len(self.buf)
Expand Down
23 changes: 20 additions & 3 deletions kaa/editmode.py
Expand Up @@ -9,23 +9,35 @@ class EditMode:
repeat = None
repeat_str = ''

_replace_str = False
_key_hook = None

def __init__(self):
self.pending_keys = []
self.last_command_keys = []

def install_keyhook(self, hook):
self._key_hook = hook

def activated(self, wnd):
pass

def flush_pending_str(self, wnd):
if self.pending_str:
pending = self.pending_str
self.pending_str = ''
wnd.document.mode.on_str(wnd, pending)
# if not wnd.closed:
# wnd.update_window()
wnd.document.mode.on_str(wnd, pending, self._replace_str)
return True

def on_keyevent(self, wnd, event):
if self._key_hook:
f = self._key_hook
self._key_hook = None
event = f(wnd, event)

if not event:
return

if event.key == '\x1b' and event.no_trailing_char:
return self.on_esc_pressed(wnd, event)
else:
Expand Down Expand Up @@ -109,6 +121,7 @@ def on_esc_pressed(self, wnd, event):
kaa.app.messagebar.set_message('')
self.pending_keys = []
self.clear_repeat()
self._key_hook = None
del self.last_command_keys[:]

if not wnd.closed:
Expand Down Expand Up @@ -145,6 +158,10 @@ def set_repeat(self, n):
self.repeat = n


class ReplaceMode(EditMode):
MODENAME = 'Replace'
_replace_str = True

class CommandMode(EditMode):
MODENAME = 'Command'

Expand Down
2 changes: 2 additions & 0 deletions kaa/filetype/default/keybind.py
Expand Up @@ -134,6 +134,7 @@
command_mode_keys = {
# editmode change
'i': 'editmode.insert',
'R': 'editmode.replace',
'a': ('editmode.insert', 'cursor.end-of-line'),
'v': ('editmode.visual', 'selection.set-mark'),
# 'V': ('editmode.visual-linewise', 'cursor.home', 'selection.set-mark'),
Expand All @@ -155,6 +156,7 @@
(ctrl, 'f'): 'cursor.pagedown',

# edit
'r': 'edit.replace-char',
'x': 'edit.delete',

# undo/redo
Expand Down
21 changes: 15 additions & 6 deletions kaa/filetype/default/modebase.py
Expand Up @@ -203,14 +203,15 @@ def init_addon_keys(self):
self.keybind_vi_visuallinewisemode.add_keybind(keys)

def init_commands(self):
from kaa.commands import (editorcommand, editmodecommand)
from kaa.commands import (editorcommand, editmodecommand, vicommand)

self.register_commandobj(editorcommand.CursorCommands())
self.register_commandobj(editorcommand.EditCommands())
self.register_commandobj(editorcommand.CodeCommands())
self.register_commandobj(editorcommand.SelectionCommands())
self.register_commandobj(editorcommand.SearchCommands())
self.register_commandobj(editmodecommand.EditModeCommands())
self.register_commandobj(vicommand.ViCommands())

for name in dir(self):
attr = getattr(self, name)
Expand Down Expand Up @@ -252,6 +253,9 @@ def get_command(self, commandid):
def editmode_insert(self, wnd):
wnd.set_editmode(editmode.EditMode())

def editmode_replace(self, wnd):
wnd.set_editmode(editmode.ReplaceMode())

def editmode_visual(self, wnd):
wnd.set_editmode(editmode.VisualMode())

Expand Down Expand Up @@ -313,8 +317,8 @@ def filter_string(self, wnd, s):
def on_edited(self, wnd):
pass

def on_str(self, wnd, s):
self.put_string(wnd, s)
def on_str(self, wnd, s, overwrite=False):
self.put_string(wnd, s, overwrite=overwrite)
wnd.screen.selection.clear()

if kaa.app.macro.is_recording():
Expand Down Expand Up @@ -434,8 +438,8 @@ def replace_rect(self, wnd, repto):
posto += (len(s) - (t - f))
posfrom = wnd.document.geteol(posfrom)

def put_string(self, wnd, s):
s = wnd.document.mode.filter_string(wnd, s)
def put_string(self, wnd, s, overwrite=False):
s = self.filter_string(wnd, s)

if wnd.screen.selection.is_selected():
if wnd.screen.selection.is_rectangular():
Expand All @@ -449,7 +453,12 @@ def put_string(self, wnd, s):
f, t = sel
self.replace_string(wnd, f, t, s)
else:
self.insert_string(wnd, wnd.cursor.pos, s)
if not overwrite:
self.insert_string(wnd, wnd.cursor.pos, s)
else:
eol = wnd.document.get_line_to(wnd.cursor.pos)
posto = min(wnd.cursor.pos+len(s), eol)
self.replace_string(wnd, wnd.cursor.pos, posto, s)

def update_charattr(self, wnd):
if wnd.charattrs:
Expand Down
2 changes: 1 addition & 1 deletion kaa/screen.py
Expand Up @@ -669,7 +669,7 @@ def get_pos_at_cols(self, tol, cols):
return pos
p += charcols

return self.document.find_newline(tol)
return self.document.get_line_to(tol)

def _fillscreen(self):
while True:
Expand Down
2 changes: 1 addition & 1 deletion kaa/ui/fileinfo/fileinfomode.py
Expand Up @@ -119,7 +119,7 @@ def build_doc(self):
def is_cursor_visible(self):
return 0 # hide cursor

def on_str(self, wnd, s):
def on_str(self, wnd, s, overwrite=False):
# does nothing
pass

Expand Down
2 changes: 1 addition & 1 deletion kaa/ui/itemlist/itemlistmode.py
Expand Up @@ -81,7 +81,7 @@ def on_esc_pressed(self, wnd, event):
self.cursel = None
self.selected(wnd)

def on_str(self, wnd, s):
def on_str(self, wnd, s, overwrite=False):
cursel = self.cursel
if cursel is None:
cursel = -1
Expand Down
2 changes: 1 addition & 1 deletion kaa/ui/moveseparator/moveseparatormode.py
Expand Up @@ -52,7 +52,7 @@ def is_cursor_visible(self):
def on_esc_pressed(self, wnd, event):
self.done(wnd)

def on_str(self, wnd, s):
def on_str(self, wnd, s, overwrite=False):
pass

@commandid('moveseparator.prev')
Expand Down
4 changes: 2 additions & 2 deletions kaa/ui/msgbox/msgboxmode.py
Expand Up @@ -33,13 +33,13 @@ def on_add_window(self, wnd):

wnd.CURSOR_TO_MIDDLE_ON_SCROLL = False

def on_str(self, wnd, s):
def on_str(self, wnd, s, overwrite=False):
pass

def on_start(self, wnd):
wnd.cursor.setpos(self.document.endpos() - 1)

def on_str(self, wnd, s):
def on_str(self, wnd, s, overwrite=False):
for c in s:
c = c.lower()
if c in self.shortcuts:
Expand Down
4 changes: 2 additions & 2 deletions kaa/ui/pyconsole/pythonconsolemode.py
Expand Up @@ -87,14 +87,14 @@ def on_esc_pressed(self, wnd, event):
wnd.cursor.setpos(f)
self.document.undo.clear()

def put_string(self, wnd, s):
def put_string(self, wnd, s, overwrite=False):
pos = wnd.cursor.pos
f, t = self.document.marks['current_script']
if not (f <= pos <= t):
wnd.cursor.setpos(t)
wnd.screen.selection.clear()

super().put_string(wnd, s)
super().put_string(wnd, s, overwrite)

def replace_string(self, wnd, pos, posto, s, update_cursor=True):
f, t = self.document.marks['current_script']
Expand Down
2 changes: 1 addition & 1 deletion kaa/ui/pythondebug/pythondebugmode.py
Expand Up @@ -217,7 +217,7 @@ def close(self):
super().close()
self.port = None

def on_str(self, wnd, s):
def on_str(self, wnd, s, overwrite=False):
pass

def build(self, stack):
Expand Down
2 changes: 1 addition & 1 deletion kaa/ui/selectlist/selectlist.py
Expand Up @@ -45,7 +45,7 @@ def init_keybind(self):
super().init_keybind()
self.keybind.add_keybind(selectlist_keys)

def on_str(self, wnd, s):
def on_str(self, wnd, s, overwrite=False):
pass

def calc_height(self, wnd):
Expand Down
2 changes: 1 addition & 1 deletion kaa/ui/viewdiff/viewdiffmode.py
Expand Up @@ -34,7 +34,7 @@ def init_tokenizers(self):
def is_cursor_visible(self):
return 1 # hide cursor

def on_str(self, wnd, s):
def on_str(self, wnd, s, overwrite=False):
# does nothing
pass

Expand Down

0 comments on commit 1bf4074

Please sign in to comment.