Skip to content

Commit

Permalink
Merge pull request yi-editor#547 from jetho/gf-Cmds
Browse files Browse the repository at this point in the history
Add Vim's gf, <C-w>gf and <C-w>f commands
  • Loading branch information
ethercrow committed May 24, 2014
2 parents b260f63 + 4e2bbfe commit fd11220
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion yi/src/library/Yi/Keymap/Vim2/NormalMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Yi.Keymap.Vim2.NormalMap
import Control.Monad
import Control.Applicative
import Control.Lens hiding (re)
import System.Directory (doesFileExist)

import Data.Char
import Data.List (group)
Expand All @@ -29,6 +30,9 @@ import Yi.Misc
import Yi.Regex (seInput, makeSearchOptsM)
import Yi.Search (getRegexE, isearchInitE, setRegexE, makeSimpleSearch)
import Yi.Monad
import Yi.Keymap
import Yi.File (editFile)
import Yi.Utils (io)

mkDigitBinding :: Char -> VimBinding
mkDigitBinding c = mkBindingE Normal Continue (char c, return (), mutate)
Expand All @@ -46,6 +50,7 @@ defNormalMap operators =
continuingBindings ++
nonrepeatableBindings ++
jumpBindings ++
fileEditBindings ++
[tabTraversalBinding]

motionBinding :: VimBinding
Expand Down Expand Up @@ -268,6 +273,13 @@ nonrepeatableBindings = fmap (mkBindingE Normal Drop)
, ("<C-w>p", prevWinE, resetCount)
]

fileEditBindings :: [VimBinding]
fileEditBindings = fmap (mkStringBindingY Normal)
[ ("gf", openFileUnderCursor Nothing, resetCount)
, ("<C-w>gf", openFileUnderCursor $ Just newTabE, resetCount)
, ("<C-w>f", openFileUnderCursor $ Just (splitE >> prevWinE), resetCount)
]

setMarkBinding :: VimBinding
setMarkBinding = VimBindingE f
where f _ s | vsMode s /= Normal = NoMatch
Expand All @@ -292,7 +304,6 @@ searchWordE wholeWord dir = do
Left _ -> return ()
else search $ makeSimpleSearch word


searchBinding :: VimBinding
searchBinding = VimBindingE f
where f evs (VimState { vsMode = Normal }) | evs `elem` group "/?"
Expand Down Expand Up @@ -365,10 +376,21 @@ tabTraversalBinding = VimBindingE f
return Drop
f _ _ = NoMatch

openFileUnderCursor :: Maybe (EditorM ()) -> YiM ()
openFileUnderCursor editorAction = do
fileName <- withEditor . withBuffer0 $ readUnitB unitViWORD
fileExists <- io $ doesFileExist fileName
if (not fileExists) then
withEditor . fail $ "Can't find file \"" ++ fileName ++ "\""
else do
maybeM withEditor editorAction
void . editFile $ fileName

-- TODO: withCount name implies that parameter has type (Int -> EditorM ())
-- Is there a better name for this function?
withCount :: EditorM () -> EditorM ()
withCount action = flip replicateM_ action =<< getCountE

withCountOnBuffer0 :: BufferM () -> EditorM ()
withCountOnBuffer0 action = withCount $ withBuffer0 action

0 comments on commit fd11220

Please sign in to comment.