Skip to content

Commit

Permalink
Further trimming to suit WiPy better
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-hh committed Oct 25, 2015
1 parent 26a0576 commit c26cf3b
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 231 deletions.
Binary file modified Pyboard Editor.doc
Binary file not shown.
Binary file modified Pyboard Editor.pdf
Binary file not shown.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ The editor works also well in a Linux or MAC terminal environment, with both pyt

**Files:**

- pye.py: Source file with comments and code for both PyBoard and Linux micropython/python3. Runs on PyBoard as well, but the file size is much larger than the stripped down version.
- pye.py: Source file with comments and code for PyBoard, WiPy and Linux micropython/python3. Runs on PyBoard as well, but the file size is much larger than the stripped down version.
- Pyboard Editor.pdf: A short documentation
- README.md: This one
- pe.py: Source file for PyBoard with all functions in a condensed
- pe.py: Condensed source file for PyBoard with all functions
- pemin.py: Condensed source file with a reduced function set for PyBoard
- wipye.py: Condensed source file with a reduced function set for WiPy
- tuning_pye.py: A file with some improved replacements for functions of pye:
Expand All @@ -34,38 +34,44 @@ c) expandtabs() and packtabs() with a second argument for tabsize (not for pye,
**Short Version History**

**1.0** Initial release with all the basic functions

**1.1** Same function set, but simplified keyboard mapping.
- Removed the duplicated definitions for cursor motion keys.
- Allowed both \r and \n for ENTER, and both \x08 and \x7f for BACKSPACE, which avoid some hazzle with terminal settings.
- Removed auto-indent from the minimal version.

**1.2** Mouse support added, as well as some other minor changes.
- Simple Mouse support for scrolling and placing the cursor
- Flags setting for search case, autoindent on/off and statusline on/off
- GOTO line sets cursor to the middle row
- The function pye(..) returns a value now

**1.3** UNDO added. Added a multilevel UNDO (Ctrl-Z) for most functions that change the content. Other changes:
- Backspace at the first non-blank character mimics BackTab, if Auto-indent is enabled
- Added a REDRAW (Ctrl-E) function, which checks for the changed screen size after the window size has been changed.
- Added a line number column on the left side of the screen (can be turned off).
- Improved the scrolling speed, such that it lags less.
- Some code simplification and straightening, such that functions group better and a easier to understand.

**1.4** GET file added. Adding a function GET (Ctrl-O), which inserts the content of a file before the current line. Other changes:
- Both HOME and END stop at start of text is passing by on their way to their destination.
- Flag allowing to replace spaces by Tab when writing the file, complementary to what is done while reading. Tabsize is 8. A tab is inserted whenever possible, even if it replaced a single space character.
- Flag allowing to replace spaces by Tab when writing the file, complementary to what is done while reading. Tabsize is 8. A tab is inserted whenever possible, even if it replaces a single space character.
- Fixed a mild amnesia in UNDO

**1.5** WiPy Port and body shaping:
- Support for WiPy added. WiPy runs only the minimal version.
- Aligned function set of the minimal version, in order to comply with WiPy. Dropped Mouse support, GET file, Line number column, Delete in Line edit mode, and write tabs; but included the buffer functions Yank, Dup & ZAP, Tab and Backtab.
- Aligned function set of the minimal version, in order to comply with WiPy. Dropped Mouse support, GET file, Line number column, and write tabs; but included Tab, Backtab, the buffer functions Yank, Dup & ZAP and scrolling optimization.
- LEFT and RIGHT move to the adjacent line if needed
- When used with Linux **and** CPython, a terminal window resize cause redrawing the screen content. The REDRAW key (Ctrl-E) stays functional and is required for all other use cases, when the window size is changed.
- HOME toggles again between start-of-line and start-of-text. END moves always to end-of-line
- Dropped context sensitive behaviour of Tab, Backtab, Backspace and Delete. Too confusing.
- Dropped the line number column, and made the status line permanent in all modes.
- Rearranged the code such that any platform related sections are grouped together.






**1.6** WiPy fixes and further trimming:
- Making rarely used small functions inline again, which saves some space. Important for WiPy.
- Catch Ctrl-C on WiPy. Not really nice yet, since the next input byte is lost.
- Tab Size can be set with the Ctrl-A command (if available).
- Simplified Linux main(). No calling options any more.
- Always ask when leaving w/o saving after the content was changed.

101 changes: 50 additions & 51 deletions pe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys, gc, _io
import sys
class Editor:
KEYMAP = {
b"\x1b[A" : 0x0b,
Expand All @@ -14,6 +14,7 @@ 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,
Expand Down Expand Up @@ -77,14 +78,14 @@ def rd():
return Editor.serialcomm.read(1)
def init_tty(self, device, baud, fd_tty):
import pyb
if device:
Editor.sdev = device
if Editor.sdev:
Editor.serialcomm = pyb.UART(device, baud)
else:
Editor.serialcomm = pyb.USB_VCP()
Editor.serialcomm.setinterrupt(-1)
Editor.sdev = device
def deinit_tty(self):
if Editor.sdev:
if not Editor.sdev:
Editor.serialcomm.setinterrupt(3)
@staticmethod
def goto(row, col):
Expand All @@ -102,21 +103,9 @@ def cursor(onoff):
def hilite(mode):
if mode == 1:
Editor.wr(b"\x1b[1m")
elif mode == 2:
Editor.wr(b"\x1b[2;7m")
else:
Editor.wr(b"\x1b[0m")
@staticmethod
def get_screen_size():
Editor.wr('\x1b[999;999H\x1b[6n')
pos = b''
char = Editor.rd()
while char != b'R':
if char in b"0123456789;": pos += char
char = Editor.rd()
(height, width) = [int(i, 10) for i in pos.split(b';')]
return (height-1, width)
@staticmethod
def mouse_reporting(onoff):
if onoff:
Editor.wr('\x1b[?9h')
Expand All @@ -139,7 +128,14 @@ def scroll_down(self, scrolling):
self.goto(self.height - 1, 0)
Editor.wr("\x1bD " * scrolling)
def set_screen_parms(self):
(self.height, self.width) = self.get_screen_size()
Editor.wr('\x1b[999;999H\x1b[6n')
pos = b''
char = Editor.rd()
while char != b'R':
if char in b"0123456789;": pos += char
char = Editor.rd()
(self.height, self.width) = [int(i, 10) for i in pos.split(b';')]
self.height -= 1
self.scrbuf = ["\x04"] * self.height
self.scroll_region(self.height)
def get_input(self):
Expand Down Expand Up @@ -195,8 +191,8 @@ def display_window(self):
self.scrbuf[c] = l
i += 1
self.goto(self.height, 0)
self.clear_to_eol()
self.hilite(1)
self.clear_to_eol()
self.wr("[%d] %c Row: %d Col: %d %s" % (self.total_lines, self.changed, self.cur_line + 1, self.col + 1, self.message[:self.width - 25]))
self.hilite(0)
self.cursor(True)
Expand Down Expand Up @@ -254,33 +250,31 @@ def find_in_file(self, pattern, pos):
self.cur_line = line
self.message = ' '
return len(pattern)
def cursor_down(self, set_col = False):
def cursor_down(self):
if self.cur_line < self.total_lines - 1:
self.cur_line += 1
if set_col: self.col = 0
if self.cur_line == self.top_line + self.height:
self.scroll_down(1)
def cursor_up(self, set_col = False):
if self.cur_line > 0:
self.cur_line -= 1
if set_col: self.col = len(self.content[self.cur_line])
if self.cur_line < self.top_line:
self.scroll_up(1)
def handle_cursor_keys(self, key):
if key == 0x0d:
self.cursor_down()
elif key == 0x0b:
self.cursor_up()
if self.cur_line > 0:
self.cur_line -= 1
if self.cur_line < self.top_line:
self.scroll_up(1)
elif key == 0x0c:
if self.col > 0:
self.col -= 1
else:
self.cursor_up(True)
elif self.cur_line > 0:
self.cur_line -= 1
self.col = len(self.content[self.cur_line])
elif key == 0x0f:
if self.col < len(self.content[self.cur_line]):
self.col += 1
else:
self.cursor_down(True)
elif self.cur_line < self.total_lines - 1:
self.cur_line += 1
self.col = 0
elif key == 0x10:
ns = self.spaces(self.content[self.cur_line])
self.col = ns if self.col != ns else 0
Expand Down Expand Up @@ -323,13 +317,16 @@ def handle_cursor_keys(self, key):
self.cur_line = max(self.cur_line, self.top_line)
self.scroll_down(3)
elif key == 0x01:
pat = self.line_edit("Case Sensitive Search %c, Autoindent %c, Write Tabs %c: " %
(self.case, self.autoindent, self.write_tabs), "")
pat = self.line_edit("Case Sensitive Search %c, Autoindent %c, Tab Size %d, Write Tabs %c: " %
(self.case, self.autoindent, self.tab_size, self.write_tabs), "")
try:
res = [i.strip().lower() for i in pat.split(",")]
if res[0]: self.case = 'y' if res[0][0] == 'y' else 'n'
if res[1]: self.autoindent = 'y' if res[1][0] == 'y' else 'n'
if res[2]: self.write_tabs = 'y' if res[2][0] == 'y' else 'n'
if res[2]:
try: self.tab_size = int(res[2])
except: pass
if res[3]: self.write_tabs = 'y' if res[3][0] == 'y' else 'n'
except:
pass
elif key == 0x02:
Expand All @@ -347,12 +344,6 @@ def undo_add(self, lnum, text, key, span = 1):
del self.undo[0]
self.sticky_c = "*"
self.undo.append((lnum, span, text, key, self.col))
def yank_add(self, key, l):
if key == self.lastkey:
self.yank_buffer.append(l)
else:
del self.yank_buffer
self.yank_buffer = [l]
def handle_edit_key(self, key):
l = self.content[self.cur_line]
if key == 0x0a:
Expand All @@ -378,8 +369,7 @@ def handle_edit_key(self, key):
elif self.cur_line > 0:
self.undo_add(self.cur_line - 1, [self.content[self.cur_line - 1], l], 0)
self.col = len(self.content[self.cur_line - 1])
self.content[self.cur_line - 1] += l
del self.content[self.cur_line]
self.content[self.cur_line - 1] += self.content.pop(self.cur_line)
self.cur_line -= 1
self.total_lines -= 1
self.changed = '*'
Expand Down Expand Up @@ -447,17 +437,25 @@ def handle_edit_key(self, key):
self.changed = "*"
elif key == 0x18:
self.undo_add(self.cur_line, [l], 0, 0)
self.yank_add(key, l)
if key == self.lastkey:
self.yank_buffer.append(l)
else:
del self.yank_buffer
self.yank_buffer = [l]
if self.total_lines > 1:
del self.content[self.cur_line]
self.total_lines -= 1
if self.cur_line >= self.total_lines:
self.cursor_up()
self.cur_line -= 1
else:
self.content[self.cur_line] = ''
self.changed = '*'
elif key == 0x04:
self.yank_add(key, l)
if key == self.lastkey:
self.yank_buffer.append(l)
else:
del self.yank_buffer
self.yank_buffer = [l]
self.cursor_down()
elif key == 0x16:
if self.yank_buffer:
Expand All @@ -483,7 +481,7 @@ def handle_edit_key(self, key):
del self.undo[:]
self.fname = fname
except Exception as err:
self.message = 'Could not save %s, Reason: "%s"' % (fname, err)
self.message = 'Could not save %s, Error: %s' % (fname, err)
elif key == 0x1a:
if len(self.undo) > 0:
action = self.undo.pop(-1)
Expand Down Expand Up @@ -513,7 +511,7 @@ def edit_loop(self):
key = self.get_input()
self.message = ''
if key == 0x03:
if self.changed != ' ' and self.fname != None:
if self.changed != ' ':
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
if not res or res[0].upper() != 'Y':
continue
Expand All @@ -532,8 +530,9 @@ def edit_loop(self):
self.lastkey = key
@staticmethod
def expandtabs(s):
from _io import StringIO
if '\t' in s:
sb = _io.StringIO()
sb = StringIO()
pos = 0
for c in s:
if c == '\t':
Expand All @@ -547,7 +546,8 @@ def expandtabs(s):
return s
@staticmethod
def packtabs(s):
sb = _io.StringIO()
from _io import StringIO
sb = StringIO()
for i in range(0, len(s), 8):
c = s[i:i + 8]
cr = c.rstrip(" ")
Expand All @@ -562,7 +562,7 @@ def get_file(fname):
with open(fname) as f:
content = f.readlines()
except Exception as err:
message = 'Could not load %s, Reason: "%s"' % (fname, err)
message = 'Could not load %s, Error: %s' % (fname, err)
return (None, message)
else:
if not content:
Expand All @@ -589,5 +589,4 @@ def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200, fd_t
e.deinit_tty()
content = e.content if (e.fname == None) else e.fname
del e
gc.collect()
return content
Loading

0 comments on commit c26cf3b

Please sign in to comment.