improved hex mode

--HG--
branch : pmacs2
This commit is contained in:
moculus 2007-10-15 19:35:06 +00:00
parent 100947dd36
commit c9998e1b19
4 changed files with 29 additions and 13 deletions

View File

@ -280,7 +280,7 @@ class Application(object):
if cipher is None:
if not os.path.exists(path) or os.path.isfile(path):
if binary:
b = buffer2.BinaryBuffer(path, name=name)
b = buffer2.Binary32Buffer(path, name=name)
else:
b = buffer2.FileBuffer(path, name=name)
elif os.path.isdir(path):
@ -795,7 +795,7 @@ def open_aes_file(path, nl, name=None):
def open_plain_file(path, nl, name=None, binary=False):
if os.path.isfile(path) or not os.path.exists(path):
if binary:
return buffer2.BinaryBuffer(path, nl, name)
return buffer2.Binary32Buffer(path, nl, name)
else:
return buffer2.FileBuffer(path, nl, name)
elif os.path.isdir(path):

View File

@ -484,27 +484,28 @@ class AesBuffer(FileBuffer):
def write_filter(self, data):
return aes.encrypt_data(data, self.password)
class BinaryBuffer(FileBuffer):
btype = 'binfile'
def __init__(self, path, nl='\n', name=None, numbytes=2):
class Binary32Buffer(FileBuffer):
btype = 'bin32file'
wordsize = 4
numwords = 4
def __init__(self, path, nl='\n', name=None):
'''fb = FileBuffer(path)'''
FileBuffer.__init__(self, path, nl, name)
self.numbytes = numbytes
def read_filter(self, data):
lines = []
i = 0
while i < len(data):
j = 0
words = []
while j < self.numbytes * 8 and i + j < len(data):
while j < self.numwords * self.wordsize and i + j < len(data):
nibbles = []
for c in data[i + j:i + j + 8]:
for c in data[i + j:i + j + self.wordsize]:
nibbles.append(string.hexdigits[ord(c) / 16])
nibbles.append(string.hexdigits[ord(c) % 16])
words.append(''.join(nibbles))
j += 8
j += self.wordsize
lines.append(' '.join(words))
i += self.numbytes * 8
i += self.numwords * self.wordsize
return '\n'.join(lines)
def write_filter(self, data):
bytes = []

View File

@ -34,7 +34,7 @@ class Argument:
def coerce_to_type(self, value):
if self.type == type(0):
try:
return int(value)
return int(value, 0)
except:
raise Exception, "expected int; got %s" % (repr(value))
else:

View File

@ -1,11 +1,12 @@
import string
import color, mode2
from lex3 import Grammar, PatternRule, RegionRule
from method import Method, Argument
from point2 import Point
class HexGrammar(Grammar):
rules = [
PatternRule(r'zero', r"00"),
PatternRule(r'printable', r"[2-7][0-9a-f]"),
PatternRule(r'byte', r'[0-f][0-f]'),
]
@ -13,12 +14,13 @@ class Hex(mode2.Fundamental):
grammar = HexGrammar
colors = {
'zero': ('magenta', 'default'),
'printable': ('green', 'default'),
'byte': ('white', 'default'),
}
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
self.add_action_and_bindings(GotoWord(), ('M-g',))
# create all the insert actions for the basic text input
for c in string.letters + string.digits + string.punctuation:
if c in string.hexdigits:
@ -28,3 +30,16 @@ class Hex(mode2.Fundamental):
def name(self):
return "Hex"
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:
p = Point((n % 4) * 9, n / 4)
w.goto(p)
except:
w.goto_end()