7 Nov 2004 09:06
:map and :unmap
Ok, here's an initial vi-style :map and :unmap (nullary macros). This
probably works for emacs too (?)
To set a key mapping (ie. :map f jjjj) we execute the current lexer with
just "jjjj" as input, returning a list of [Action] that would occur if
that input had been entered. We then concat those actions with (>>),
getting a single action that is used to generate a new lexer (the `bind'
below). Finally, we add the new binding to the current lexer with >||<.
eval_map :: ViMode -> [Char] -> [Char] -> ViMode
eval_map cmdm lhs rhs = cmdm >||< bind
where
(as,_,_) = execLexer cmdm (rhs, (St [] cmdm))
bind = string lhs `action` \_ -> Just (foldl1 (>>) as)
Now, to unmap, we execute the original lexer with the binding (i.e. "f")
getting back any action that 'f' was previously bound to. We then rebind
'f' to that old action, if any. Ideally we need to store a stack of
previous lexers so we could have stacked bindings, but for now we just
execute the original lexer.
eval_unmap :: ViMode -> [Char] -> ViMode
eval_unmap cmdm lhs = cmdm >||< bind
where
(as,_,_) = execLexer cmd_mode{-original-} (lhs, (St [] cmd_mode))
bind = case as of
[] -> string lhs `action` \_ -> Just nopE -- wasn't bound prior
[a]-> string lhs `action` \_ -> Just a -- bound to just one
_ -> string lhs `action` \_ -> Just nopE
(Continue reading)
RSS Feed