Skip to content

Commit 698f796

Browse files
committed
Shortened KEYMAP to savev space for WiPy; Tabbify by Tab/Backtab on col 1 (full version)
1 parent e249311 commit 698f796

File tree

4 files changed

+100
-112
lines changed

4 files changed

+100
-112
lines changed

pe.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ class Editor:
1414
b"\x1b[5~": 0x17,
1515
b"\x1b[6~": 0x19,
1616
b"\x11" : 0x03,
17-
b"\x03" : 0x03,
1817
b"\r" : 0x0a,
19-
b"\n" : 0x0a,
2018
b"\x7f" : 0x08,
21-
b"\x08" : 0x08,
2219
b"\x1b[3~": 0x1f,
20+
b"\x1b[Z" : 0x15,
21+
b"\x1b[3;5~": 0x18,
22+
b"\x03" : 0x03,
23+
b"\n" : 0x0a,
24+
b"\x08" : 0x08,
2325
b"\x13" : 0x13,
2426
b"\x06" : 0x06,
2527
b"\x0e" : 0x0e,
@@ -28,12 +30,10 @@ class Editor:
2830
b"\x1a" : 0x1a,
2931
b"\x09" : 0x09,
3032
b"\x15" : 0x15,
31-
b"\x1b[Z" : 0x15,
33+
b"\x12" : 0x12,
3234
b"\x18" : 0x18,
33-
b"\x1b[3;5~": 0x18,
3435
b"\x16" : 0x16,
3536
b"\x04" : 0x04,
36-
b"\x12" : 0x12,
3737
b"\x1b[M" : 0x1b,
3838
b"\x01" : 0x01,
3939
b"\x14" : 0x02,
@@ -57,10 +57,10 @@ def __init__(self, tab_size, undo_limit):
5757
self.content = [""]
5858
self.undo = []
5959
self.undo_limit = max(undo_limit, 0)
60-
self.yank_buffer = []
61-
self.lastkey = 0
6260
self.case = "n"
6361
self.autoindent = "y"
62+
self.yank_buffer = []
63+
self.lastkey = 0
6464
self.replc_pattern = ""
6565
self.write_tabs = "n"
6666
if sys.platform == "pyboard":
@@ -164,7 +164,7 @@ def get_input(self):
164164
return 0x1c
165165
else:
166166
return 0x1b
167-
elif input[0] >= 0x20:
167+
elif len(input) == 1:
168168
return input[0]
169169
def display_window(self):
170170
self.cur_line = min(self.total_lines - 1, max(self.cur_line, 0))
@@ -280,8 +280,7 @@ def handle_cursor_keys(self, key):
280280
self.cursor_down()
281281
self.col = 0
282282
elif key == 0x10:
283-
ns = self.spaces(self.content[self.cur_line])
284-
self.col = ns if self.col != ns else 0
283+
self.col = self.spaces(self.content[self.cur_line]) if self.col == 0 else 0
285284
elif key == 0x11:
286285
self.col = len(self.content[self.cur_line])
287286
elif key == 0x17:
@@ -356,7 +355,7 @@ def handle_edit_key(self, key):
356355
ni = 0
357356
if self.autoindent == "y":
358357
ni = min(self.spaces(l), self.col)
359-
r = self.content[self.cur_line].partition("\x23")[0].rstrip()
358+
r = l.partition("\x23")[0].rstrip()
360359
if r and r[-1] == ':' and self.col >= len(r):
361360
ni += self.tab_size
362361
self.cur_line += 1
@@ -390,15 +389,29 @@ def handle_edit_key(self, key):
390389
self.changed = '*'
391390
elif key == 0x09:
392391
self.undo_add(self.cur_line, [l], key)
393-
ni = self.tab_size - self.col % self.tab_size
394-
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
395-
self.col += ni
392+
if False: pass
393+
else:
394+
ns = self.spaces(l)
395+
if self.col == 0 and self.col != ns:
396+
self.content[self.cur_line] = ' ' * (self.tab_size - ns % self.tab_size) + l
397+
self.cursor_down()
398+
else:
399+
ni = self.tab_size - self.col % self.tab_size
400+
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
401+
self.col += ni
396402
self.changed = '*'
397403
elif key == 0x15:
398404
self.undo_add(self.cur_line, [l], key)
399-
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col))
400-
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
401-
self.col -= ni
405+
if False: pass
406+
else:
407+
ns = self.spaces(l)
408+
if self.col == 0 and ns > 0:
409+
self.content[self.cur_line] = l[(ns - 1) % self.tab_size + 1:]
410+
self.cursor_down()
411+
else:
412+
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col))
413+
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
414+
self.col -= ni
402415
self.changed = '*'
403416
elif key == 0x12:
404417
count = 0

pemin.py

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,11 @@ class Editor:
1414
b"\x1b[5~": 0x17,
1515
b"\x1b[6~": 0x19,
1616
b"\x11" : 0x03,
17-
b"\x03" : 0x03,
1817
b"\r" : 0x0a,
19-
b"\n" : 0x0a,
2018
b"\x7f" : 0x08,
21-
b"\x08" : 0x08,
2219
b"\x1b[3~": 0x1f,
23-
b"\x13" : 0x13,
24-
b"\x06" : 0x06,
25-
b"\x0e" : 0x0e,
26-
b"\x07" : 0x07,
27-
b"\x05" : 0x05,
28-
b"\x1a" : 0x1a,
29-
b"\x09" : 0x09,
30-
b"\x15" : 0x15,
3120
b"\x1b[Z" : 0x15,
32-
b"\x18" : 0x18,
3321
b"\x1b[3;5~": 0x18,
34-
b"\x16" : 0x16,
35-
b"\x04" : 0x04,
36-
b"\x12" : 0x12,
37-
b"\x1b[M" : 0x1b,
38-
b"\x01" : 0x01,
39-
b"\x14" : 0x02,
40-
b"\x02" : 0x14,
41-
b"\x1b[1;5H": 0x02,
42-
b"\x1b[1;5F": 0x14,
43-
b"\x0f" : 0x1e,
4422
}
4523
def __init__(self, tab_size, undo_limit):
4624
self.top_line = 0
@@ -57,10 +35,10 @@ def __init__(self, tab_size, undo_limit):
5735
self.content = [""]
5836
self.undo = []
5937
self.undo_limit = max(undo_limit, 0)
60-
self.yank_buffer = []
61-
self.lastkey = 0
6238
self.case = "n"
6339
self.autoindent = "y"
40+
self.yank_buffer = []
41+
self.lastkey = 0
6442
if sys.platform == "pyboard":
6543
@staticmethod
6644
def wr(s):
@@ -146,7 +124,7 @@ def get_input(self):
146124
c = self.KEYMAP[input]
147125
if c != 0x1b:
148126
return c
149-
elif input[0] >= 0x20:
127+
elif len(input) == 1:
150128
return input[0]
151129
def display_window(self):
152130
self.cur_line = min(self.total_lines - 1, max(self.cur_line, 0))
@@ -260,8 +238,7 @@ def handle_cursor_keys(self, key):
260238
self.cursor_down()
261239
self.col = 0
262240
elif key == 0x10:
263-
ns = self.spaces(self.content[self.cur_line])
264-
self.col = ns if self.col != ns else 0
241+
self.col = self.spaces(self.content[self.cur_line]) if self.col == 0 else 0
265242
elif key == 0x11:
266243
self.col = len(self.content[self.cur_line])
267244
elif key == 0x17:
@@ -303,7 +280,7 @@ def handle_edit_key(self, key):
303280
ni = 0
304281
if self.autoindent == "y":
305282
ni = min(self.spaces(l), self.col)
306-
r = self.content[self.cur_line].partition("\x23")[0].rstrip()
283+
r = l.partition("\x23")[0].rstrip()
307284
if r and r[-1] == ':' and self.col >= len(r):
308285
ni += self.tab_size
309286
self.cur_line += 1
@@ -330,15 +307,19 @@ def handle_edit_key(self, key):
330307
self.changed = '*'
331308
elif key == 0x09:
332309
self.undo_add(self.cur_line, [l], key)
333-
ni = self.tab_size - self.col % self.tab_size
334-
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
335-
self.col += ni
310+
if False: pass
311+
else:
312+
ni = self.tab_size - self.col % self.tab_size
313+
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
314+
self.col += ni
336315
self.changed = '*'
337316
elif key == 0x15:
338317
self.undo_add(self.cur_line, [l], key)
339-
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col))
340-
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
341-
self.col -= ni
318+
if False: pass
319+
else:
320+
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col))
321+
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
322+
self.col -= ni
342323
self.changed = '*'
343324
elif key == 0x18:
344325
self.undo_add(self.cur_line, [l], 0, 0)
@@ -397,8 +378,6 @@ def handle_edit_key(self, key):
397378
self.total_lines = len(self.content)
398379
if len(self.undo) == 0:
399380
self.changed = self.sticky_c
400-
elif key < 0x20:
401-
self.message = "Sorry, command not supported"
402381
elif key >= 0x20:
403382
self.undo_add(self.cur_line, [l], 0x20 if key == 0x20 else 0x41)
404383
self.content[self.cur_line] = l[:self.col] + chr(key) + l[self.col:]

pye.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,16 @@ class Editor:
104104
b"\x1b[5~": KEY_PGUP,
105105
b"\x1b[6~": KEY_PGDN,
106106
b"\x11" : KEY_QUIT, ## Ctrl-Q
107-
b"\x03" : KEY_QUIT, ## Ctrl-C
108107
b"\r" : KEY_ENTER,
109-
b"\n" : KEY_ENTER,
110108
b"\x7f" : KEY_BACKSPACE, ## Ctrl-? (127)
111-
b"\x08" : KEY_BACKSPACE,
112109
b"\x1b[3~": KEY_DELETE,
110+
b"\x1b[Z" : KEY_BACKTAB, ## Shift Tab
111+
b"\x1b[3;5~": KEY_YANK, ## Ctrl-Del
112+
#ifndef BASIC
113+
## keys mapped onto themselves
114+
b"\x03" : KEY_QUIT, ## Ctrl-C
115+
b"\n" : KEY_ENTER,
116+
b"\x08" : KEY_BACKSPACE,
113117
b"\x13" : KEY_WRITE, ## Ctrl-S
114118
b"\x06" : KEY_FIND, ## Ctrl-F
115119
b"\x0e" : KEY_FIND_AGAIN, ## Ctrl-N
@@ -118,19 +122,19 @@ class Editor:
118122
b"\x1a" : KEY_UNDO, ## Ctrl-Z
119123
b"\x09" : KEY_TAB,
120124
b"\x15" : KEY_BACKTAB, ## Ctrl-U
121-
b"\x1b[Z" : KEY_BACKTAB, ## Shift Tab
125+
b"\x12" : KEY_REPLC, ## Ctrl-R
122126
b"\x18" : KEY_YANK, ## Ctrl-X
123-
b"\x1b[3;5~": KEY_YANK, ## Ctrl-Del
124127
b"\x16" : KEY_ZAP, ## Ctrl-V
125128
b"\x04" : KEY_DUP, ## Ctrl-D
126-
b"\x12" : KEY_REPLC, ## Ctrl-R
129+
##
127130
b"\x1b[M" : KEY_MOUSE,
128131
b"\x01" : KEY_TOGGLE, ## Ctrl-A
129132
b"\x14" : KEY_FIRST, ## Ctrl-T
130133
b"\x02" : KEY_LAST, ## Ctrl-B
131134
b"\x1b[1;5H": KEY_FIRST,
132135
b"\x1b[1;5F": KEY_LAST,
133136
b"\x0f" : KEY_GET, ## Ctrl-O
137+
#endif
134138
}
135139

136140
def __init__(self, tab_size, undo_limit):
@@ -148,10 +152,10 @@ def __init__(self, tab_size, undo_limit):
148152
self.content = [""]
149153
self.undo = []
150154
self.undo_limit = max(undo_limit, 0)
151-
self.yank_buffer = []
152-
self.lastkey = 0
153155
self.case = "n"
154156
self.autoindent = "y"
157+
self.yank_buffer = []
158+
self.lastkey = 0
155159
#ifndef BASIC
156160
self.replc_pattern = ""
157161
self.write_tabs = "n"
@@ -348,7 +352,7 @@ def get_input(self): ## read from interface/keyboard one byte each and match ag
348352
else:
349353
return KEY_MOUSE ## do nothing but set the cursor
350354
#endif
351-
elif input[0] >= 0x20: ## but only if no Ctrl-Char
355+
elif len(input) == 1: ## but only if a single char
352356
return input[0]
353357

354358
def display_window(self): ## Update window and status line
@@ -478,8 +482,7 @@ def handle_cursor_keys(self, key): ## keys which move, sanity checks later
478482
self.cursor_down()
479483
self.col = 0
480484
elif key == KEY_HOME:
481-
ns = self.spaces(self.content[self.cur_line])
482-
self.col = ns if self.col != ns else 0
485+
self.col = self.spaces(self.content[self.cur_line]) if self.col == 0 else 0
483486
elif key == KEY_END:
484487
self.col = len(self.content[self.cur_line])
485488
elif key == KEY_PGUP:
@@ -558,7 +561,7 @@ def handle_edit_key(self, key): ## keys which change content
558561
ni = 0
559562
if self.autoindent == "y": ## Autoindent
560563
ni = min(self.spaces(l), self.col) ## query indentation
561-
r = self.content[self.cur_line].partition("\x23")[0].rstrip() ## \x23 == #
564+
r = l.partition("\x23")[0].rstrip() ## \x23 == #
562565
if r and r[-1] == ':' and self.col >= len(r): ## look for : as the last non-space before comment
563566
ni += self.tab_size
564567
self.cur_line += 1
@@ -594,15 +597,33 @@ def handle_edit_key(self, key): ## keys which change content
594597
self.changed = '*'
595598
elif key == KEY_TAB:
596599
self.undo_add(self.cur_line, [l], key)
597-
ni = self.tab_size - self.col % self.tab_size ## determine spaces to add
598-
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
599-
self.col += ni
600+
if False: pass
601+
#ifndef BASIC
602+
else:
603+
ns = self.spaces(l)
604+
if self.col == 0 and self.col != ns:
605+
self.content[self.cur_line] = ' ' * (self.tab_size - ns % self.tab_size) + l
606+
self.cursor_down()
607+
#endif
608+
else:
609+
ni = self.tab_size - self.col % self.tab_size ## determine spaces to add
610+
self.content[self.cur_line] = l[:self.col] + ' ' * ni + l[self.col:]
611+
self.col += ni
600612
self.changed = '*'
601613
elif key == KEY_BACKTAB:
602614
self.undo_add(self.cur_line, [l], key)
603-
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col)) ## determine spaces to drop
604-
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
605-
self.col -= ni
615+
if False: pass
616+
#ifndef BASIC
617+
else:
618+
ns = self.spaces(l)
619+
if self.col == 0 and ns > 0:
620+
self.content[self.cur_line] = l[(ns - 1) % self.tab_size + 1:]
621+
self.cursor_down()
622+
#endif
623+
else:
624+
ni = min((self.col - 1) % self.tab_size + 1, self.spaces(l, self.col)) ## determine spaces to drop
625+
self.content[self.cur_line] = l[:self.col - ni] + l[self.col:]
626+
self.col -= ni
606627
self.changed = '*'
607628
#ifndef BASIC
608629
elif key == KEY_REPLC:
@@ -706,10 +727,6 @@ def handle_edit_key(self, key): ## keys which change content
706727
self.total_lines = len(self.content) ## brute force
707728
if len(self.undo) == 0: ## test changed flag
708729
self.changed = self.sticky_c
709-
#ifdef BASIC
710-
elif key < 0x20:
711-
self.message = "Sorry, command not supported"
712-
#endif
713730
elif key >= 0x20: ## character to be added
714731
self.undo_add(self.cur_line, [l], 0x20 if key == 0x20 else 0x41)
715732
self.content[self.cur_line] = l[:self.col] + chr(key) + l[self.col:]

0 commit comments

Comments
 (0)