From 3c2390aff7ef45a926e4bd4cbcd7608002e0ebbb Mon Sep 17 00:00:00 2001 From: Jeremy List Date: Mon, 23 Apr 2018 17:55:28 +1200 Subject: [PATCH 1/3] Add :next and :previous commands (ignoring difference between :next and :bnext, etc for now) --- yi-keymap-vim/src/Yi/Keymap/Vim/Ex.hs | 2 ++ .../Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs | 28 +++++++++++++++++++ yi-keymap-vim/yi-keymap-vim.cabal | 1 + 3 files changed, 31 insertions(+) create mode 100644 yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs diff --git a/yi-keymap-vim/src/Yi/Keymap/Vim/Ex.hs b/yi-keymap-vim/src/Yi/Keymap/Vim/Ex.hs index e0a7b2bcc..b75d754ed 100644 --- a/yi-keymap-vim/src/Yi/Keymap/Vim/Ex.hs +++ b/yi-keymap-vim/src/Yi/Keymap/Vim/Ex.hs @@ -17,6 +17,7 @@ module Yi.Keymap.Vim.Ex import Yi.Keymap.Vim.Common (EventString) import qualified Yi.Keymap.Vim.Ex.Commands.Buffer as Buffer (parse) +import qualified Yi.Keymap.Vim.Ex.Commands.BufferCycle as BufferCycle (parse) import qualified Yi.Keymap.Vim.Ex.Commands.BufferDelete as BufferDelete (parse) import qualified Yi.Keymap.Vim.Ex.Commands.BufferNew as BufferNew (parse) import qualified Yi.Keymap.Vim.Ex.Commands.Buffers as Buffers (parse) @@ -48,6 +49,7 @@ import Yi.Keymap.Vim.Ex.Types (ExCommand (..), evStrin defExCommandParsers :: [EventString -> Maybe ExCommand] defExCommandParsers = [ Buffer.parse + , BufferCycle.parse , Buffers.parse , BufferDelete.parse , BufferNew.parse diff --git a/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs b/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs new file mode 100644 index 000000000..50cdab871 --- /dev/null +++ b/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE OverloadedStrings #-} +module Yi.Keymap.Vim.Ex.Commands.BufferCycle (parse) where + +import Control.Applicative (Alternative ((<|>))) +import Control.Monad +import Data.List +import qualified Data.Attoparsec.Text as P +import Lens.Micro.Platform +import Yi.Editor +import Yi.Keymap (Action (EditorA)) +import Yi.Window +import Yi.Keymap.Vim.Common (EventString) +import qualified Yi.Keymap.Vim.Ex.Commands.Common as Common +import Yi.Keymap.Vim.Ex.Types (ExCommand (cmdAction, cmdShow)) + +parse :: EventString -> Maybe ExCommand +parse = Common.parse $ do + void (P.char 'b') <|> return () + direction <- (fmap (const False) $ P.string "prev" <|> P.string "previous") <|> fmap (const True) (P.string "next") + return $ Common.pureExCommand { + cmdShow = if direction then "next" else "previous", + cmdAction = EditorA $ currentWindowA %= \window -> let + cl = bufkey window : bufAccessList window + cl' = if direction + then tail cl ++ [head cl] + else last cl : init cl + in window { bufkey = head cl', bufAccessList = tail cl' } + } \ No newline at end of file diff --git a/yi-keymap-vim/yi-keymap-vim.cabal b/yi-keymap-vim/yi-keymap-vim.cabal index 6109256ff..4282b0374 100644 --- a/yi-keymap-vim/yi-keymap-vim.cabal +++ b/yi-keymap-vim/yi-keymap-vim.cabal @@ -77,6 +77,7 @@ library Yi.Keymap.Vim.EventUtils Yi.Keymap.Vim.Ex Yi.Keymap.Vim.Ex.Commands.Buffer + Yi.Keymap.Vim.Ex.Commands.BufferCycle Yi.Keymap.Vim.Ex.Commands.BufferDelete Yi.Keymap.Vim.Ex.Commands.Buffers Yi.Keymap.Vim.Ex.Commands.Cabal From 6199afcc973364b82cc30195bcdb15a0b28d9b70 Mon Sep 17 00:00:00 2001 From: Jeremy List Date: Mon, 23 Apr 2018 19:30:35 +1200 Subject: [PATCH 2/3] Prevent space leak --- .../src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs b/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs index 50cdab871..09a58a1e4 100644 --- a/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs +++ b/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs @@ -24,5 +24,11 @@ parse = Common.parse $ do cl' = if direction then tail cl ++ [head cl] else last cl : init cl - in window { bufkey = head cl', bufAccessList = tail cl' } - } \ No newline at end of file + in forceSpine cl' `seq` window { bufkey = head cl', bufAccessList = tail cl' } + } + +{-# INLINE forceSpine #-} +forceSpine :: [a] -> () +forceSpine = go where + go [] = () + go (_:r) = go r From 5fde11da961308f35233aa85299160a7c59c9a15 Mon Sep 17 00:00:00 2001 From: Jeremy List Date: Mon, 23 Apr 2018 19:35:04 +1200 Subject: [PATCH 3/3] Exchange semantics for :previous and :next (account for how new buffers are actually added to list) --- yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs b/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs index 09a58a1e4..e95c359ca 100644 --- a/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs +++ b/yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/BufferCycle.hs @@ -22,8 +22,8 @@ parse = Common.parse $ do cmdAction = EditorA $ currentWindowA %= \window -> let cl = bufkey window : bufAccessList window cl' = if direction - then tail cl ++ [head cl] - else last cl : init cl + then last cl : init cl + else tail cl ++ [head cl] in forceSpine cl' `seq` window { bufkey = head cl', bufAccessList = tail cl' } }