Skip to content

Commit

Permalink
Some documentation for Yi.FuzzyOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
ethercrow committed Sep 29, 2012
1 parent 2d9e4f5 commit eb44a59
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions yi-contrib/src/Yi/FuzzyOpen.hs
@@ -1,3 +1,31 @@

{- This file aims to provide (the essential subset of) the same functionality
that vim plugins ctrlp and command-t provide.
Setup:
Add something like this to your config:
(ctrlCh 'p' ?>>! fuzzyOpen)
Usage:
<C-p> (or whatever mapping user chooses) starts fuzzy open dialog.
Typing something filters filelist.
<Enter> opens currently selected file
in current (the one that fuzzyOpen was initited from) window.
<C-t> opens currently selected file in a new tab.
<C-s> opens currently selected file in a split.
<KUp> and <C-p> moves selection up
<KDown> and <C-n> moves selection down
Readline shortcuts <C-a> , <C-e>, <C-u> and <C-k> work as usual.
-}

module Yi.FuzzyOpen module Yi.FuzzyOpen
( fuzzyOpen ( fuzzyOpen
) where ) where
Expand All @@ -23,6 +51,7 @@ fuzzyOpen = do
withEditor $ spawnMinibufferE "" $ const $ localKeymap bufRef fileList withEditor $ spawnMinibufferE "" $ const $ localKeymap bufRef fileList
return () return ()


-- shamelessly stolen from Chapter 9 of Real World Haskell
getRecursiveContents :: FilePath -> IO [FilePath] getRecursiveContents :: FilePath -> IO [FilePath]
getRecursiveContents topdir = do getRecursiveContents topdir = do
names <- getDirectoryContents topdir names <- getDirectoryContents topdir
Expand Down Expand Up @@ -61,12 +90,16 @@ localKeymap bufRef fileList =
updatingB bufAction = withBuffer bufAction >> update updatingB bufAction = withBuffer bufAction >> update
updatingE editorAction = withEditor editorAction >> update updatingE editorAction = withEditor editorAction >> update


insertChar :: Keymap
insertChar = textChar >>= write . insertB

showFileList :: [FilePath] -> String showFileList :: [FilePath] -> String
showFileList = concat . intersperse "\n" . map (" " ++) showFileList = concat . intersperse "\n" . map (" " ++)


{- Implementation detail:
The index of selected file is stored as vertical cursor position.
Asterisk position is always synchronized with cursor position.
TODO: store index of selected file explicitly to make things more obvious.
-}

updateMatchList :: BufferRef -> [FilePath] -> YiM () updateMatchList :: BufferRef -> [FilePath] -> YiM ()
updateMatchList bufRef fileList = do updateMatchList bufRef fileList = do
needle <- withBuffer elemsB needle <- withBuffer elemsB
Expand Down Expand Up @@ -102,6 +135,7 @@ openRoutine preOpenAction bufRef = do
preOpenAction preOpenAction
discard $ editFile chosenFile discard $ editFile chosenFile


-- TODO: This function looks like it belongs to Yi.Buffer.Misc
getLine :: BufferM String getLine :: BufferM String
getLine = do getLine = do
moveToSol moveToSol
Expand All @@ -110,19 +144,23 @@ getLine = do
p1 <- pointB p1 <- pointB
nelemsB (fromPoint p1 - fromPoint p0) p0 nelemsB (fromPoint p1 - fromPoint p0) p0


-- stolen from Yi.Keymap.Vim
-- | Parse any character that can be inserted in the text. -- | Parse any character that can be inserted in the text.
textChar :: KeymapM Char textChar :: KeymapM Char
textChar = do textChar = do
Event (KASCII c) [] <- anyEvent Event (KASCII c) [] <- anyEvent
return c return c


moveSelectionUp :: BufferRef -> EditorM () insertChar :: Keymap
insertChar = textChar >>= write . insertB

moveSelectionUp :: BufferRef -> EditorM ()
moveSelectionUp bufRef = withGivenBuffer0 bufRef $ do moveSelectionUp bufRef = withGivenBuffer0 bufRef $ do
replaceCurrentChar ' ' replaceCurrentChar ' '
lineUp lineUp
replaceCurrentChar '*' replaceCurrentChar '*'

moveSelectionDown :: BufferRef -> EditorM () moveSelectionDown :: BufferRef -> EditorM ()
moveSelectionDown bufRef = withGivenBuffer0 bufRef $ do moveSelectionDown bufRef = withGivenBuffer0 bufRef $ do
replaceCurrentChar ' ' replaceCurrentChar ' '
lineDown lineDown
Expand Down

0 comments on commit eb44a59

Please sign in to comment.