Permalink
Browse files

Added support for `word forward' with 'w'. Works only on alnums curre…

…ntly. Need to add punct support.
  • Loading branch information...
1 parent ff97967 commit d31b8ba414efde70a723d602913840255ca495de @michaelfeathers committed Aug 15, 2009
Showing with 24 additions and 2 deletions.
  1. +23 −2 editbuffer.hs
  2. +1 −0 vih.hs
View
@@ -12,10 +12,13 @@ module EditBuffer
, moveLeft, moveRight, moveUp, moveDown
, moveToHome, moveToEnd
, moveToLineStart, moveToLineEnd
+ , wordForward
, showRepresentation
)
where
+import Char
+
type Location = (Int, Int)
data EditBuffer = EditBuffer Location String deriving (Eq,Show)
@@ -47,12 +50,12 @@ deleteChar buffer@(EditBuffer location@(x,y) contents)
insertLineAfter :: EditBuffer -> EditBuffer
insertLineAfter (EditBuffer _ "") = EditBuffer (0,1) "\n"
insertLineAfter (EditBuffer (_,y) contents) = EditBuffer (0,y+1) newContents
- where newContents = unlines [transform numberedLine | numberedLine <- zip (lines contents) [0..]]
+ where newContents = unlines [transform numberedLine | numberedLine <- numberedLines contents]
transform (line, pos) = if pos == y then line ++ "\n" else line
deleteLine :: EditBuffer ->EditBuffer
deleteLine (EditBuffer location@(_,y) contents) = forceLocation (EditBuffer location newContents)
- where newContents = unlines [ line | (line, pos) <- zip (lines contents) [0..], pos /= y]
+ where newContents = unlines [ line | (line, pos) <- numberedLines contents, pos /= y]
moveLeft, moveRight, moveUp, moveDown :: EditBuffer -> EditBuffer
moveLeft = saturate (-1, 0)
@@ -74,6 +77,12 @@ moveToLineEnd :: EditBuffer -> EditBuffer
moveToLineEnd buffer@(EditBuffer (_,y) contents) =
satX 0 $ (EditBuffer ((currentLineLength buffer), y) contents)
+wordForward :: EditBuffer -> EditBuffer
+wordForward buffer@(EditBuffer _ contents) =
+ case dropWhile (\(x,_) -> isSpace x) . dropWhile (\(x,_) -> isAlphaNum x) . dropWhile (\(_,pos) -> pos < absPosition buffer) . numberedElements $ contents of
+ [] -> buffer
+ ((_,pos) : _) -> EditBuffer (locationFromPosition pos buffer) contents
+
showRepresentation :: EditBuffer -> String
showRepresentation (EditBuffer location contents) =
show location ++ " " ++ show contents
@@ -98,6 +107,13 @@ absPosition :: EditBuffer -> Int
absPosition (EditBuffer (x, y) contents) =
(x+) . length . unlines . take y . lines $ contents
+locationFromPosition :: Int -> EditBuffer -> Location
+locationFromPosition pos (EditBuffer _ contents) =
+ let wayPoints = takeWhile (<= pos) . scanl1 (+) . map ((+1).length) . lines $ contents
+ in case wayPoints of
+ [] -> (pos, 0)
+ xs -> (pos - (last xs), length wayPoints)
+
saturate :: (Int,Int) -> EditBuffer -> EditBuffer
saturate (adjX,adjY) = satX adjX . satY adjY
@@ -116,4 +132,9 @@ saturateValue bound value
| value >= bound = bound - 1
| otherwise = value
+numberedElements :: [a] -> [(a,Int)]
+numberedElements = (flip zip) [0..]
+
+numberedLines :: String -> [(String,Int)]
+numberedLines = numberedElements . lines
View
1 vih.hs
@@ -45,6 +45,7 @@ mainLoop mode buffer@(EditBuffer location contents) =
'0' -> mainLoop mode (moveToLineStart buffer)
'$' -> mainLoop mode (moveToLineEnd buffer)
'o' -> mainLoop Insert (insertLineAfter buffer)
+ 'w' -> mainLoop mode (wordForward buffer)
'x' -> mainLoop mode (deleteChar buffer)
_ -> mainLoop mode buffer

0 comments on commit d31b8ba

Please sign in to comment.