Skip to content

Commit

Permalink
Shortened KEYMAP to savev space for WiPy; Tabbify by Tab/Backtab on c…
Browse files Browse the repository at this point in the history
…ol 1 (full version)
  • Loading branch information
robert-hh committed Nov 1, 2015
1 parent e249311 commit 698f796
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 112 deletions.
49 changes: 31 additions & 18 deletions pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class Editor:
b"\x1b[5~": 0x17,
b"\x1b[6~": 0x19,
b"\x11" : 0x03,
b"\x03" : 0x03,
b"\r" : 0x0a,
b"\n" : 0x0a,
b"\x7f" : 0x08,
b"\x08" : 0x08,
b"\x1b[3~": 0x1f,
b"\x1b[Z" : 0x15,
b"\x1b[3;5~": 0x18,
b"\x03" : 0x03,
b"\n" : 0x0a,
b"\x08" : 0x08,
b"\x13" : 0x13,
b"\x06" : 0x06,
b"\x0e" : 0x0e,
Expand All @@ -28,12 +30,10 @@ class Editor:
b"\x1a" : 0x1a,
b"\x09" : 0x09,
b"\x15" : 0x15,
b"\x1b[Z" : 0x15,
b"\x12" : 0x12,
b"\x18" : 0x18,
b"\x1b[3;5~": 0x18,
b"\x16" : 0x16,
b"\x04" : 0x04,
b"\x12" : 0x12,
b"\x1b[M" : 0x1b,
b"\x01" : 0x01,
b"\x14" : 0x02,
Expand All @@ -57,10 +57,10 @@ def __init__(self, tab_size, undo_limit):
self.content = [""]
self.undo = []
self.undo_limit = max(undo_limit, 0)
self.yank_buffer = []
self.lastkey = 0
self.case = "n"
self.autoindent = "y"
self.yank_buffer = []
self.lastkey = 0
self.replc_pattern = ""
self.write_tabs = "n"
if sys.platform == "pyboard":
Expand Down Expand Up @@ -164,7 +164,7 @@ def get_input(self):
return 0x1c
else:
return 0x1b
elif input[0] >= 0x20:
elif len(input) == 1:
return input[0]
def display_window(self):
self.cur_line = min(self.total_lines - 1, max(self.cur_line, 0))
Expand Down Expand Up @@ -280,8 +280,7 @@ def handle_cursor_keys(self, key):
self.cursor_down()
self.col = 0
elif key == 0x10:
ns = self.spaces(self.content[self.cur_line])
self.col = ns if self.col != ns else 0
self.col = self.spaces(self.content[self.cur_line]) if self.col == 0 else 0
elif key == 0x11:
self.col = len(self.content[self.cur_line])
elif key == 0x17:
Expand Down Expand Up @@ -356,7 +355,7 @@ def handle_edit_key(self, key):
ni = 0
if self.autoindent == "y":
ni = min(self.spaces(l), self.col)
r = self.content[self.cur_line].partition("\x23")[0].rstrip()
r = l.partition("\x23")[0].rstrip()
if r and r[-1] == ':' and self.col >= len(r):
ni += self.tab_size
self.cur_line += 1
Expand Down Expand Up @@ -390,15 +389,29 @@ def handle_edit_key(self, key):
self.changed = '*'
elif key == 0x09:
self.undo_add(self.cur_line, [l], key)
ni = self.tab_size - self.col % self.tab_size
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
self.col += ni
if False: pass
else:
ns = self.spaces(l)
if self.col == 0 and self.col != ns:
self.content[self.cur_line] = ' ' * (self.tab_size - ns % self.tab_size) + l
self.cursor_down()
else:
ni = self.tab_size - self.col % self.tab_size
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
self.col += ni
self.changed = '*'
elif key == 0x15:
self.undo_add(self.cur_line, [l], key)
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col))
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
self.col -= ni
if False: pass
else:
ns = self.spaces(l)
if self.col == 0 and ns > 0:
self.content[self.cur_line] = l[(ns - 1) % self.tab_size + 1:]
self.cursor_down()
else:
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col))
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
self.col -= ni
self.changed = '*'
elif key == 0x12:
count = 0
Expand Down
51 changes: 15 additions & 36 deletions pemin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,11 @@ class Editor:
b"\x1b[5~": 0x17,
b"\x1b[6~": 0x19,
b"\x11" : 0x03,
b"\x03" : 0x03,
b"\r" : 0x0a,
b"\n" : 0x0a,
b"\x7f" : 0x08,
b"\x08" : 0x08,
b"\x1b[3~": 0x1f,
b"\x13" : 0x13,
b"\x06" : 0x06,
b"\x0e" : 0x0e,
b"\x07" : 0x07,
b"\x05" : 0x05,
b"\x1a" : 0x1a,
b"\x09" : 0x09,
b"\x15" : 0x15,
b"\x1b[Z" : 0x15,
b"\x18" : 0x18,
b"\x1b[3;5~": 0x18,
b"\x16" : 0x16,
b"\x04" : 0x04,
b"\x12" : 0x12,
b"\x1b[M" : 0x1b,
b"\x01" : 0x01,
b"\x14" : 0x02,
b"\x02" : 0x14,
b"\x1b[1;5H": 0x02,
b"\x1b[1;5F": 0x14,
b"\x0f" : 0x1e,
}
def __init__(self, tab_size, undo_limit):
self.top_line = 0
Expand All @@ -57,10 +35,10 @@ def __init__(self, tab_size, undo_limit):
self.content = [""]
self.undo = []
self.undo_limit = max(undo_limit, 0)
self.yank_buffer = []
self.lastkey = 0
self.case = "n"
self.autoindent = "y"
self.yank_buffer = []
self.lastkey = 0
if sys.platform == "pyboard":
@staticmethod
def wr(s):
Expand Down Expand Up @@ -146,7 +124,7 @@ def get_input(self):
c = self.KEYMAP[input]
if c != 0x1b:
return c
elif input[0] >= 0x20:
elif len(input) == 1:
return input[0]
def display_window(self):
self.cur_line = min(self.total_lines - 1, max(self.cur_line, 0))
Expand Down Expand Up @@ -260,8 +238,7 @@ def handle_cursor_keys(self, key):
self.cursor_down()
self.col = 0
elif key == 0x10:
ns = self.spaces(self.content[self.cur_line])
self.col = ns if self.col != ns else 0
self.col = self.spaces(self.content[self.cur_line]) if self.col == 0 else 0
elif key == 0x11:
self.col = len(self.content[self.cur_line])
elif key == 0x17:
Expand Down Expand Up @@ -303,7 +280,7 @@ def handle_edit_key(self, key):
ni = 0
if self.autoindent == "y":
ni = min(self.spaces(l), self.col)
r = self.content[self.cur_line].partition("\x23")[0].rstrip()
r = l.partition("\x23")[0].rstrip()
if r and r[-1] == ':' and self.col >= len(r):
ni += self.tab_size
self.cur_line += 1
Expand All @@ -330,15 +307,19 @@ def handle_edit_key(self, key):
self.changed = '*'
elif key == 0x09:
self.undo_add(self.cur_line, [l], key)
ni = self.tab_size - self.col % self.tab_size
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
self.col += ni
if False: pass
else:
ni = self.tab_size - self.col % self.tab_size
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
self.col += ni
self.changed = '*'
elif key == 0x15:
self.undo_add(self.cur_line, [l], key)
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col))
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
self.col -= ni
if False: pass
else:
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col))
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
self.col -= ni
self.changed = '*'
elif key == 0x18:
self.undo_add(self.cur_line, [l], 0, 0)
Expand Down Expand Up @@ -397,8 +378,6 @@ def handle_edit_key(self, key):
self.total_lines = len(self.content)
if len(self.undo) == 0:
self.changed = self.sticky_c
elif key < 0x20:
self.message = "Sorry, command not supported"
elif key >= 0x20:
self.undo_add(self.cur_line, [l], 0x20 if key == 0x20 else 0x41)
self.content[self.cur_line] = l[:self.col] + chr(key) + l[self.col:]
Expand Down
61 changes: 39 additions & 22 deletions pye.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ class Editor:
b"\x1b[5~": KEY_PGUP,
b"\x1b[6~": KEY_PGDN,
b"\x11" : KEY_QUIT, ## Ctrl-Q
b"\x03" : KEY_QUIT, ## Ctrl-C
b"\r" : KEY_ENTER,
b"\n" : KEY_ENTER,
b"\x7f" : KEY_BACKSPACE, ## Ctrl-? (127)
b"\x08" : KEY_BACKSPACE,
b"\x1b[3~": KEY_DELETE,
b"\x1b[Z" : KEY_BACKTAB, ## Shift Tab
b"\x1b[3;5~": KEY_YANK, ## Ctrl-Del
#ifndef BASIC
## keys mapped onto themselves
b"\x03" : KEY_QUIT, ## Ctrl-C
b"\n" : KEY_ENTER,
b"\x08" : KEY_BACKSPACE,
b"\x13" : KEY_WRITE, ## Ctrl-S
b"\x06" : KEY_FIND, ## Ctrl-F
b"\x0e" : KEY_FIND_AGAIN, ## Ctrl-N
Expand All @@ -118,19 +122,19 @@ class Editor:
b"\x1a" : KEY_UNDO, ## Ctrl-Z
b"\x09" : KEY_TAB,
b"\x15" : KEY_BACKTAB, ## Ctrl-U
b"\x1b[Z" : KEY_BACKTAB, ## Shift Tab
b"\x12" : KEY_REPLC, ## Ctrl-R
b"\x18" : KEY_YANK, ## Ctrl-X
b"\x1b[3;5~": KEY_YANK, ## Ctrl-Del
b"\x16" : KEY_ZAP, ## Ctrl-V
b"\x04" : KEY_DUP, ## Ctrl-D
b"\x12" : KEY_REPLC, ## Ctrl-R
##
b"\x1b[M" : KEY_MOUSE,
b"\x01" : KEY_TOGGLE, ## Ctrl-A
b"\x14" : KEY_FIRST, ## Ctrl-T
b"\x02" : KEY_LAST, ## Ctrl-B
b"\x1b[1;5H": KEY_FIRST,
b"\x1b[1;5F": KEY_LAST,
b"\x0f" : KEY_GET, ## Ctrl-O
#endif
}

def __init__(self, tab_size, undo_limit):
Expand All @@ -148,10 +152,10 @@ def __init__(self, tab_size, undo_limit):
self.content = [""]
self.undo = []
self.undo_limit = max(undo_limit, 0)
self.yank_buffer = []
self.lastkey = 0
self.case = "n"
self.autoindent = "y"
self.yank_buffer = []
self.lastkey = 0
#ifndef BASIC
self.replc_pattern = ""
self.write_tabs = "n"
Expand Down Expand Up @@ -348,7 +352,7 @@ def get_input(self): ## read from interface/keyboard one byte each and match ag
else:
return KEY_MOUSE ## do nothing but set the cursor
#endif
elif input[0] >= 0x20: ## but only if no Ctrl-Char
elif len(input) == 1: ## but only if a single char
return input[0]

def display_window(self): ## Update window and status line
Expand Down Expand Up @@ -478,8 +482,7 @@ def handle_cursor_keys(self, key): ## keys which move, sanity checks later
self.cursor_down()
self.col = 0
elif key == KEY_HOME:
ns = self.spaces(self.content[self.cur_line])
self.col = ns if self.col != ns else 0
self.col = self.spaces(self.content[self.cur_line]) if self.col == 0 else 0
elif key == KEY_END:
self.col = len(self.content[self.cur_line])
elif key == KEY_PGUP:
Expand Down Expand Up @@ -558,7 +561,7 @@ def handle_edit_key(self, key): ## keys which change content
ni = 0
if self.autoindent == "y": ## Autoindent
ni = min(self.spaces(l), self.col) ## query indentation
r = self.content[self.cur_line].partition("\x23")[0].rstrip() ## \x23 == #
r = l.partition("\x23")[0].rstrip() ## \x23 == #
if r and r[-1] == ':' and self.col >= len(r): ## look for : as the last non-space before comment
ni += self.tab_size
self.cur_line += 1
Expand Down Expand Up @@ -594,15 +597,33 @@ def handle_edit_key(self, key): ## keys which change content
self.changed = '*'
elif key == KEY_TAB:
self.undo_add(self.cur_line, [l], key)
ni = self.tab_size - self.col % self.tab_size ## determine spaces to add
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
self.col += ni
if False: pass
#ifndef BASIC
else:
ns = self.spaces(l)
if self.col == 0 and self.col != ns:
self.content[self.cur_line] = ' ' * (self.tab_size - ns % self.tab_size) + l
self.cursor_down()
#endif
else:
ni = self.tab_size - self.col % self.tab_size ## determine spaces to add
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
self.col += ni
self.changed = '*'
elif key == KEY_BACKTAB:
self.undo_add(self.cur_line, [l], key)
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col)) ## determine spaces to drop
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
self.col -= ni
if False: pass
#ifndef BASIC
else:
ns = self.spaces(l)
if self.col == 0 and ns > 0:
self.content[self.cur_line] = l[(ns - 1) % self.tab_size + 1:]
self.cursor_down()
#endif
else:
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col)) ## determine spaces to drop
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
self.col -= ni
self.changed = '*'
#ifndef BASIC
elif key == KEY_REPLC:
Expand Down Expand Up @@ -706,10 +727,6 @@ def handle_edit_key(self, key): ## keys which change content
self.total_lines = len(self.content) ## brute force
if len(self.undo) == 0: ## test changed flag
self.changed = self.sticky_c
#ifdef BASIC
elif key < 0x20:
self.message = "Sorry, command not supported"
#endif
elif key >= 0x20: ## character to be added
self.undo_add(self.cur_line, [l], 0x20 if key == 0x20 else 0x41)
self.content[self.cur_line] = l[:self.col] + chr(key) + l[self.col:]
Expand Down
Loading

0 comments on commit 698f796

Please sign in to comment.