Skip to content

Commit

Permalink
Fixing an inconsistency in the Save command
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-hh committed Dec 29, 2015
1 parent 6fe3489 commit caa0434
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 568 deletions.
Binary file modified Pyboard Editor.doc
Binary file not shown.
Binary file modified Pyboard Editor.pdf
Binary file not shown.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ c) expandtabs() and packtabs() with a second argument for tabsize (not for pye,

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

**1.1** Same function set, but simplified keyboard mapping.
**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.
- 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.
Expand All @@ -61,16 +61,16 @@ c) expandtabs() and packtabs() with a second argument for tabsize (not for pye,
- 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, 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.
- 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 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).
- 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.

Expand Down Expand Up @@ -99,7 +99,7 @@ c) expandtabs() and packtabs() with a second argument for tabsize (not for pye,
**1.8** Clean Copy & Paste, Indent, Un-Indent
- Added a Mark Line key for Line Delete, Line Copy, Indent and Un-Indent
- Changed Line Delete, Line Copy and Buffer Insert into a cleaner Copy & Paste mode
- Added a cleaner Indent and Un-Indent method; for WiPy too
- Added a cleaner Indent and Un-Indent method; for WiPy too
- Removed the attempt to recover from out-of-memory situations: did not work.
- Still runs on WiPy, but really at it's limit

Expand All @@ -121,8 +121,15 @@ c) expandtabs() and packtabs() with a second argument for tabsize (not for pye,
- Lazy screen update: defer screen update, until all chars from the keyboard are processed. Not provided for WiPY, even if needed there most. WiPy has no way to tell if more chars are waiting in the input or at least a read with timeout.

**1.12** Bracket Match and Minor changes
- Ctrl-K causes the cursor set to the matching bracket, if any. Pretty raw, not elegant. Brackets in comments and strings are counting as well.
- Ctrl-K causes the cursor set to the matching bracket, if any. Pretty raw, not elegant.
Brackets in comments and strings are counting as well.
- On Copy the mark will be cleared, since it is assumed that the just copied lines will not be overwritten.
- High level try/except catching internal errors (mostly coding errors)
- Separate cpp options for including scroll optimization, replace or bracket match into the minimal version. Changes in strip.sh script to generate the minimal wipye version too.
- Some editorial changes and fixign of tyops.
- Some editorial changes and fixing of typos.


**1.12b** Fixing a inconsistency in the Save command
- Fixing a inconsistency in the Save command, which caused the change flag being reset when writing just a block
- Squeezing a few lines out of the source code

142 changes: 67 additions & 75 deletions pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def wr(self,s):
res = self.serialcomm.write(s[ns:])
if res != None:
ns += res
def not_pending(self):
return not self.serialcomm.any()
def rd_any(self):
return self.serialcomm.any()
def rd(self):
while not self.serialcomm.any():
pass
Expand Down Expand Up @@ -215,7 +215,7 @@ def line_edit(self, prompt, default):
elif key == 0x7f:
self.wr('\b \b' * len(res))
res = ''
elif key >= 0x20:
elif 0x20 <= key < 0xfff0:
if len(prompt) + len(res) < self.width - 2:
res += chr(key)
self.wr(chr(key))
Expand Down Expand Up @@ -286,22 +286,17 @@ def handle_cursor_keys(self, key):
elif key == 0x07:
line = self.line_edit("Goto Line: ", "")
if line:
try:
self.cur_line = int(line) - 1
self.row = self.height >> 1
except:
pass
self.cur_line = int(line) - 1
self.row = self.height >> 1
elif key == 0x01:


self.autoindent = 'y' if self.autoindent != 'y' else 'n'
self.autoindent = 'y' if self.autoindent != 'y' else 'n'
pat = self.line_edit("Case Sensitive Search {}, Autoindent {}, Tab Size {}, Write Tabs {}: ".format(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]:
try: self.tab_size = int(res[2])
except: pass
if res[2]: self.tab_size = int(res[2])
if res[3]: self.write_tabs = 'y' if res[3][0] == 'y' else 'n'
except:
pass
Expand Down Expand Up @@ -341,8 +336,7 @@ def handle_cursor_keys(self, key):
for c in range(pos, len(self.content[i])):
if self.content[i][c] == match:
if level == 0:
self.cur_line = i
self.col = c
self.cur_line, self.col = i, c
return True
else:
level -= 1
Expand All @@ -358,8 +352,7 @@ def handle_cursor_keys(self, key):
for c in range(pos, -1, -1):
if self.content[i][c] == match:
if level == 0:
self.cur_line = i
self.col = c
self.cur_line, self.col = i, c
return True
else:
level -= 1
Expand Down Expand Up @@ -391,23 +384,23 @@ def delete_lines(self, yank):
self.total_lines = len(self.content)
self.cur_line = lrange[0]
self.mark = None
def handle_edit_key(self, key):
from os import rename, unlink
def handle_edit_keys(self, key):
l = self.content[self.cur_line]
if key == 0x0a:
if key == 0x7f:
if self.mark != None:
self.delete_lines(False)
elif self.col < len(l):
self.undo_add(self.cur_line, [l], 0x7f)
self.content[self.cur_line] = l[:self.col] + l[self.col + 1:]
elif (self.cur_line + 1) < self.total_lines:
self.undo_add(self.cur_line, [l, self.content[self.cur_line + 1]], 0)
self.content[self.cur_line] = l + self.content.pop(self.cur_line + 1)
self.total_lines -= 1
elif 0x20 <= key < 0xfff0:
self.mark = None
self.undo_add(self.cur_line, [l], 0, 2)
self.content[self.cur_line] = l[:self.col]
ni = 0
if self.autoindent == "y":
ni = min(self.spaces(l), self.col)
r = l.partition("\x23")[0].rstrip()
if r and r[-1] == ':' and self.col >= len(r):
ni += self.tab_size
self.cur_line += 1
self.content[self.cur_line:self.cur_line] = [' ' * ni + l[self.col:]]
self.total_lines += 1
self.col = ni
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:]
self.col += 1
elif key == 0x08:
if self.mark != None:
self.delete_lines(False)
Expand All @@ -421,16 +414,20 @@ def handle_edit_key(self, key):
self.content[self.cur_line - 1] += self.content.pop(self.cur_line)
self.cur_line -= 1
self.total_lines -= 1
elif key == 0x7f:
if self.mark != None:
self.delete_lines(False)
elif self.col < len(l):
self.undo_add(self.cur_line, [l], 0x7f)
self.content[self.cur_line] = l[:self.col] + l[self.col + 1:]
elif (self.cur_line + 1) < self.total_lines:
self.undo_add(self.cur_line, [l, self.content[self.cur_line + 1]], 0)
self.content[self.cur_line] = l + self.content.pop(self.cur_line + 1)
self.total_lines -= 1
elif key == 0x0a:
self.mark = None
self.undo_add(self.cur_line, [l], 0, 2)
self.content[self.cur_line] = l[:self.col]
ni = 0
if self.autoindent == "y":
ni = min(self.spaces(l), self.col)
r = l.partition("\x23")[0].rstrip()
if r and r[-1] == ':' and self.col >= len(r):
ni += self.tab_size
self.cur_line += 1
self.content[self.cur_line:self.cur_line] = [' ' * ni + l[self.col:]]
self.total_lines += 1
self.col = ni
elif key == 0x09:
if self.mark != None:
lrange = self.line_range()
Expand Down Expand Up @@ -520,28 +517,16 @@ def handle_edit_key(self, key):
if self.mark != None:
fname = self.line_edit("Save Mark: ", "")
lrange = self.line_range()
self.put_file(fname, lrange[0], lrange[1])
else:
fname = self.fname
if fname == None:
fname = ""
fname = self.line_edit("Save File: ", fname)
lrange = (0, self.total_lines)
if fname:
try:
with open("tmpfile.pye", "w") as f:
for l in self.content[lrange[0]:lrange[1]]:
if self.write_tabs == 'y':
f.write(self.packtabs(l) + '\n')
else:
f.write(l + '\n')
try: unlink(fname)
except: pass
rename("tmpfile.pye", fname)
self.put_file(fname, 0, self.total_lines)
self.changed = ' '
self.undo_zero = len(self.undo)
self.fname = fname
except Exception as err:
self.message = 'Could not save {}, {!r}'.format(fname, err)
elif key == 0x1a:
if len(self.undo) > 0:
action = self.undo.pop(-1)
Expand All @@ -558,24 +543,25 @@ def handle_edit_key(self, key):
self.total_lines = len(self.content)
self.changed = ' ' if len(self.undo) == self.undo_zero else '*'
self.mark = None
elif key >= 0x20:
self.mark = None
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:]
self.col += 1
def edit_loop(self):
if self.content == []:
self.content = [""]
self.total_lines = len(self.content)
self.set_screen_parms()
self.mouse_reporting(True)
self.scroll_region(self.height)
key = 0x05
while True:
if self.not_pending():
self.display_window()
key = self.get_input()
self.message = ''
try:
if key == 0x05:
self.set_screen_parms()
self.row = min(self.height - 1, self.row)
self.scroll_region(self.height)
if sys.implementation.name == "micropython":
gc.collect()
self.message = "{} Bytes Memory available".format(gc.mem_free())
if not self.rd_any():
self.display_window()
key = self.get_input()
self.message = ''
if key == 0x11:
if self.changed != ' ':
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
Expand All @@ -586,17 +572,11 @@ def edit_loop(self):
self.goto(self.height, 0)
self.clear_to_eol()
return None
elif key == 0x05:
self.set_screen_parms()
self.row = min(self.height - 1, self.row)
if sys.implementation.name == "micropython":
gc.collect()
self.message = "{} Bytes Memory available".format(gc.mem_free())
elif self.handle_cursor_keys(key):
pass
else: self.handle_edit_key(key)
else: self.handle_edit_keys(key)
except Exception as err:
self.message = "Internal error: {}".format(err)
self.message = "{}".format(err)
def packtabs(self, s):
from _io import StringIO
sb = StringIO()
Expand All @@ -618,6 +598,18 @@ def get_file(self, fname):
for i in range(len(content)):
content[i] = expandtabs(content[i].rstrip('\r\n\t '))
return (content, "")
def put_file(self, fname, start, stop):
from os import rename, unlink
if fname:
with open("tmpfile.pye", "w") as f:
for l in self.content[start:stop]:
if self.write_tabs == 'y':
f.write(self.packtabs(l) + '\n')
else:
f.write(l + '\n')
try: unlink(fname)
except: pass
rename("tmpfile.pye", fname)
def expandtabs(s):
from _io import StringIO
if '\t' in s:
Expand Down
Loading

0 comments on commit caa0434

Please sign in to comment.