Skip to content

Commit

Permalink
changed default row for find and goto to the middle row; fixed bug in…
Browse files Browse the repository at this point in the history
… re-replace
  • Loading branch information
robert-hh committed Aug 29, 2015
1 parent 8c395e8 commit ce7de0d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
23 changes: 13 additions & 10 deletions pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ def find_in_file(self, pattern, pos):
spos = 0
else:
self.message = pattern + " not found"
return False
return 0
self.col = match + spos
self.cur_line = line
self.message = ' '
return True
return len(pattern)
def handle_cursor_keys(self, key):
if key == 0x4002:
self.cur_line += 1
Expand All @@ -253,14 +253,17 @@ def handle_cursor_keys(self, key):
pat = self.line_edit("Find: ", self.find_pattern)
if pat:
self.find_in_file(pat, self.col)
self.row = int(self.height / 2)
elif key == 0x4014:
if self.find_pattern:
self.find_in_file(self.find_pattern, self.col + 1)
self.row = int(self.height / 2)
elif key == 0x4011:
line = self.line_edit("Goto Line: ", "")
if line:
try:
self.cur_line = int(line) - 1
self.row = int(self.height / 2)
except:
pass
elif key == 0x401b:
Expand All @@ -286,6 +289,7 @@ def handle_cursor_keys(self, key):
self.cur_line = 0
elif key == 0x4013:
self.cur_line = self.total_lines - 1
self.row = int(self.height / 2)
self.message = ' '
else:
return False
Expand Down Expand Up @@ -390,7 +394,8 @@ def handle_edit_key(self, key):
self.replc_pattern = rpat
q = ''
while True:
if self.find_in_file(pat, self.col):
ni = self.find_in_file(pat, self.col)
if ni:
found = True
if q != 'a':
self.display_window()
Expand All @@ -402,7 +407,7 @@ def handle_edit_key(self, key):
if q == 'q' or key == 0x4009:
break
elif q in ('a','y'):
self.content[self.cur_line] = self.content[self.cur_line][:self.col] + rpat + self.content[self.cur_line][self.col + len(pat):]
self.content[self.cur_line] = self.content[self.cur_line][:self.col] + rpat + self.content[self.cur_line][self.col + ni:]
self.col += len(rpat)
count += 1
else:
Expand Down Expand Up @@ -469,13 +474,11 @@ def init_tty(self, device, baud):

self.wr(b'\x1b[999;999H\x1b[6n')
pos = b''
while True:
char = self.rd()
while char != b'R':
pos += char
char = self.rd()
if char == b'R':
break
if char != b'\x1b' and char != b'[':
pos += char
(self.height, self.width) = [int(i, 10) for i in pos.split(b';')]
(self.height, self.width) = [int(i, 10) for i in pos[2:].split(b';')]
self.height -= 1
self.wr(b'\x1b[?9h')
def deinit_tty(self):
Expand Down
17 changes: 9 additions & 8 deletions pemin.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ def find_in_file(self, pattern, pos):
spos = 0
else:
self.message = pattern + " not found"
return False
return 0
self.col = match + spos
self.cur_line = line
self.message = ' '
return True
return len(pattern)
def handle_cursor_keys(self, key):
if key == 0x4002:
self.cur_line += 1
Expand All @@ -237,14 +237,17 @@ def handle_cursor_keys(self, key):
pat = self.line_edit("Find: ", self.find_pattern)
if pat:
self.find_in_file(pat, self.col)
self.row = int(self.height / 2)
elif key == 0x4014:
if self.find_pattern:
self.find_in_file(self.find_pattern, self.col + 1)
self.row = int(self.height / 2)
elif key == 0x4011:
line = self.line_edit("Goto Line: ", "")
if line:
try:
self.cur_line = int(line) - 1
self.row = int(self.height / 2)
except:
pass
elif key == 0x401b:
Expand Down Expand Up @@ -343,13 +346,11 @@ def init_tty(self, device, baud):

self.wr(b'\x1b[999;999H\x1b[6n')
pos = b''
while True:
char = self.rd()
while char != b'R':
pos += char
char = self.rd()
if char == b'R':
break
if char != b'\x1b' and char != b'[':
pos += char
(self.height, self.width) = [int(i, 10) for i in pos.split(b';')]
(self.height, self.width) = [int(i, 10) for i in pos[2:].split(b';')]
self.height -= 1
self.wr(b'\x1b[?9h')
def deinit_tty(self):
Expand Down
32 changes: 17 additions & 15 deletions pye.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
##
## Small python text editor based on the:
## Very simple VT100 terminal text editor widget
## Copyright (c) 2015 Paul Sokolovsky
## Distributed under MIT License
Expand All @@ -14,9 +15,6 @@
import sys
import gc
import _io

## import re ## if rex-search is used

##
#ifdef LINUX
if sys.platform in ("linux", "darwin"):
Expand Down Expand Up @@ -358,11 +356,11 @@ def find_in_file(self, pattern, pos):
spos = 0
else:
self.message = pattern + " not found"
return False
return 0
self.col = match + spos
self.cur_line = line
self.message = ' ' ## force status once
return True
return len(pattern)

def handle_cursor_keys(self, key): ## keys which move, sanity checks later
if key == KEY_DOWN:
Expand All @@ -389,14 +387,17 @@ def handle_cursor_keys(self, key): ## keys which move, sanity checks later
pat = self.line_edit("Find: ", self.find_pattern)
if pat:
self.find_in_file(pat, self.col)
self.row = int(self.height / 2)
elif key == KEY_FIND_AGAIN:
if self.find_pattern:
self.find_in_file(self.find_pattern, self.col + 1)
self.row = int(self.height / 2)
elif key == KEY_GOTO: ## goto line
line = self.line_edit("Goto Line: ", "")
if line:
try:
self.cur_line = int(line) - 1
self.row = int(self.height / 2)
except:
pass
elif key == KEY_MOUSE: ## Set Cursor
Expand All @@ -423,6 +424,7 @@ def handle_cursor_keys(self, key): ## keys which move, sanity checks later
self.cur_line = 0
elif key == KEY_LAST: ## last line
self.cur_line = self.total_lines - 1
self.row = int(self.height / 2)
self.message = ' ' ## force status once
#endif
else:
Expand Down Expand Up @@ -536,7 +538,8 @@ def handle_edit_key(self, key): ## keys which change content
self.replc_pattern = rpat
q = ''
while True:
if self.find_in_file(pat, self.col):
ni = self.find_in_file(pat, self.col)
if ni:
found = True
if q != 'a':
self.display_window()
Expand All @@ -548,7 +551,7 @@ def handle_edit_key(self, key): ## keys which change content
if q == 'q' or key == KEY_QUIT:
break
elif q in ('a','y'):
self.content[self.cur_line] = self.content[self.cur_line][:self.col] + rpat + self.content[self.cur_line][self.col + len(pat):]
self.content[self.cur_line] = self.content[self.cur_line][:self.col] + rpat + self.content[self.cur_line][self.col + ni:]
self.col += len(rpat)
count += 1
else: ## everything else is no
Expand Down Expand Up @@ -633,13 +636,11 @@ def init_tty(self, device, baud):
## Set cursor far off and ask for reporting its position = size of the window.
self.wr(b'\x1b[999;999H\x1b[6n')
pos = b''
while True:
char = self.rd() ## expect ESC[yyy;xxxR
while char != b'R':
pos += char
char = self.rd()
if char == b'R':
break
if char != b'\x1b' and char != b'[':
pos += char
(self.height, self.width) = [int(i, 10) for i in pos.split(b';')]
(self.height, self.width) = [int(i, 10) for i in pos[2:].split(b';')]
self.height -= 1
self.wr(b'\x1b[?9h') ## enable mouse reporting

Expand Down Expand Up @@ -734,6 +735,7 @@ def pye(content = None, tab_size = 4, device = 0, baud = 115200):
##
## This is the regex version of find. Standard search is up north
def find_in_file(self, pattern, pos):
import re
self.find_pattern = pattern ## remember it
if self.case != "y":
pattern = pattern.lower()
Expand All @@ -753,13 +755,13 @@ def find_in_file(self, pattern, pos):
spos = 0
else:
self.message = pattern + " not found"
return False
return 0
## micropython does not support span(), therefore a second simple find on the target line
if self.case == "y":
self.col = max(self.content[line][spos:].find(match.group(0)), 0) + spos
else:
self.col = max(self.content[line][spos:].lower().find(match.group(0)), 0) + spos
self.cur_line = line
self.message = ' ' ## force status once
return True
return len(match.group(0))
#endif

0 comments on commit ce7de0d

Please sign in to comment.