pmacs3/mode/hex.py

46 lines
1.3 KiB
Python
Raw Normal View History

2007-10-12 22:58:17 -04:00
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'byte', r'[0-f][0-f]'),
]
class Hex(mode2.Fundamental):
grammar = HexGrammar
colors = {
'zero': ('magenta', 'default'),
'byte': ('white', 'default'),
}
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
2007-10-12 22:58:17 -04:00
self.add_action_and_bindings(GotoWord(), ('M-g',))
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)
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()