2007-10-12 22:58:17 -04:00
|
|
|
import string
|
2007-10-21 20:55:29 -04:00
|
|
|
import color, mode
|
2007-10-21 20:52:48 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule
|
2007-10-15 15:35:06 -04:00
|
|
|
from method import Method, Argument
|
2007-10-21 20:50:11 -04:00
|
|
|
from point import Point
|
2007-10-12 16:26:17 -04:00
|
|
|
|
|
|
|
class HexGrammar(Grammar):
|
|
|
|
rules = [
|
|
|
|
PatternRule(r'zero', r"00"),
|
|
|
|
PatternRule(r'byte', r'[0-f][0-f]'),
|
|
|
|
]
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class Hex(mode.Fundamental):
|
2007-10-18 17:07:35 -04:00
|
|
|
modename = 'Hex'
|
|
|
|
grammar = HexGrammar
|
|
|
|
colors = {
|
2007-10-12 16:26:17 -04:00
|
|
|
'zero': ('magenta', 'default'),
|
|
|
|
'byte': ('white', 'default'),
|
|
|
|
}
|
|
|
|
def __init__(self, w):
|
2007-10-21 20:55:29 -04:00
|
|
|
mode.Fundamental.__init__(self, w)
|
2007-10-12 22:58:17 -04:00
|
|
|
|
2007-10-15 15:35:06 -04:00
|
|
|
self.add_action_and_bindings(GotoWord(), ('M-g',))
|
2007-10-15 16:17:26 -04:00
|
|
|
self.add_action(FindStrings())
|
|
|
|
self.add_action(WhichWord())
|
2007-10-15 15:35:06 -04:00
|
|
|
|
2007-10-12 22:58:17 -04:00
|
|
|
# create all the insert actions for the basic text input
|
|
|
|
for c in string.letters + string.digits + string.punctuation:
|
|
|
|
if c in string.hexdigits:
|
|
|
|
self.add_binding('overwrite-char-%s' % c.lower(), c)
|
|
|
|
else:
|
|
|
|
self.del_binding(c)
|
|
|
|
|
2007-10-15 15:35:06 -04:00
|
|
|
class GotoWord(Method):
|
|
|
|
'''Jump to the specified line number'''
|
|
|
|
args = [Argument("wordno", type=type(0), prompt="Goto word: ")]
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
n = vargs["wordno"]
|
|
|
|
if n < 0:
|
|
|
|
w.set_error("Negative word counts not supported.")
|
|
|
|
try:
|
2007-10-15 16:17:26 -04:00
|
|
|
x = (n % w.buffer.numwords) * (w.buffer.wordsize + 1)
|
|
|
|
y = n / w.buffer.numwords
|
|
|
|
p = Point(x, y)
|
2007-10-15 15:35:06 -04:00
|
|
|
w.goto(p)
|
|
|
|
except:
|
|
|
|
w.goto_end()
|
2007-10-15 16:17:26 -04:00
|
|
|
|
|
|
|
class WhichWord(Method):
|
|
|
|
'''Show the current word number'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
cursor = w.logical_cursor()
|
|
|
|
n = cursor.y * w.buffer.numwords
|
|
|
|
n += cursor.x / (w.buffer.wordsize + 1)
|
|
|
|
w.set_error("Currently in word %s (%s)" % (hex(n), n))
|
|
|
|
|
|
|
|
class FindStrings(Method):
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
newlines = []
|
|
|
|
lastline = ''
|
|
|
|
i = 0
|
|
|
|
for line in w.buffer.lines:
|
|
|
|
lastc = None
|
|
|
|
newline = ""
|
|
|
|
for c in line:
|
|
|
|
if c not in '0123456789abcdefABCDEF':
|
|
|
|
lastc = None
|
|
|
|
elif lastc is None:
|
|
|
|
lastc = c
|
|
|
|
else:
|
|
|
|
char = chr(int(lastc + c, 16))
|
|
|
|
lastc = None
|
|
|
|
if char in string.whitespace:
|
|
|
|
newline += ' '
|
|
|
|
elif char in string.letters + string.digits + string.punctuation:
|
|
|
|
newline += char
|
|
|
|
else:
|
|
|
|
newline += '.'
|
|
|
|
if lastc is not None:
|
|
|
|
newline += '.'
|
|
|
|
if i == 3:
|
|
|
|
newlines.append(lastline)
|
|
|
|
lastline = ''
|
|
|
|
else:
|
|
|
|
lastline += newline
|
|
|
|
i = (i + 1) % 4
|
|
|
|
w.application.data_buffer("*Strings*", '\n'.join(newlines), switch_to=True)
|
2007-10-19 02:41:33 -04:00
|
|
|
|
|
|
|
install = Hex.install
|