module Yi.Keymap.Emacs ( keymap
, mkKeymapSet
, defKeymap
, ModeMap(..)
, eKeymap
, completionCaseSensitive
) where
import Control.Applicative (Alternative ((<|>), empty, some))
import Control.Monad (replicateM_, unless, void)
import Control.Monad.State (gets)
import Data.Char (digitToInt, isDigit)
import Data.Maybe (fromMaybe)
import Data.Prototype (Proto (Proto), extractValue)
import Data.Text ()
import Lens.Micro.Platform ((.=), makeLenses, (%=))
import Yi.Buffer
import Yi.Command (shellCommandE)
import Yi.Core
import Yi.Dired (dired)
import Yi.Editor
import Yi.File (fwriteE, fwriteToE)
import Yi.Keymap (Keymap, KeymapSet, YiAction (..), YiM, modelessKeymapSet, write)
import Yi.Keymap.Emacs.KillRing
import Yi.Keymap.Emacs.Utils
import Yi.Keymap.Keys
import Yi.MiniBuffer
import Yi.Misc (adjBlock, adjIndent, placeMark, selectAll)
import Yi.Mode.Buffers (listBuffers)
import Yi.Rectangle
import Yi.Search (isearchFinishWithE, resetRegexE, getRegexE)
import Yi.TextCompletion (resetComplete, wordComplete')
data ModeMap = ModeMap { _eKeymap :: Keymap
, _completionCaseSensitive :: Bool
}
$(makeLenses ''ModeMap)
keymap :: KeymapSet
keymap = mkKeymapSet defKeymap
mkKeymapSet :: Proto ModeMap -> KeymapSet
mkKeymapSet = modelessKeymapSet . _eKeymap . extractValue
defKeymap :: Proto ModeMap
defKeymap = Proto template
where
template self = ModeMap { _eKeymap = emacsKeymap
, _completionCaseSensitive = False }
where
emacsKeymap :: Keymap
emacsKeymap = selfInsertKeymap Nothing isDigit <|> completionKm (_completionCaseSensitive self) <|>
do univArg <- readUniversalArg
selfInsertKeymap univArg (not . isDigit) <|> emacsKeys univArg
selfInsertKeymap :: Maybe Int -> (Char -> Bool) -> Keymap
selfInsertKeymap univArg condition = do
c <- printableChar
unless (condition c) empty
let n = argToInt univArg
write (adjBlock n >> replicateM_ n (insertB c))
completionKm :: Bool -> Keymap
completionKm caseSensitive = do void $ some (meta (char '/') ?>>! wordComplete' caseSensitive)
deprioritize
write resetComplete
deleteB' :: BufferM ()
deleteB' = adjBlock (1) >> deleteN 1
moveE :: TextUnit -> Direction -> EditorM ()
moveE u d = do
getRegexE >>= \case
Nothing -> return ()
_ -> isearchFinishWithE resetRegexE
withCurrentBuffer (moveB u d)
emacsKeys :: Maybe Int -> Keymap
emacsKeys univArg =
choice [
spec KTab ?>>! adjIndent IncreaseCycle
, shift (spec KTab) ?>>! adjIndent DecreaseCycle
, spec KEnter ?>>! repeatingArg newlineB
, spec KDel ?>>! deleteRegionOr deleteForward
, spec KBS ?>>! deleteRegionOr deleteBack
, spec KHome ?>>! repeatingArg moveToSol
, spec KEnd ?>>! repeatingArg moveToEol
, spec KLeft ?>>! repeatingArg $ moveE Character Backward
, spec KRight ?>>! repeatingArg $ moveE Character Forward
, spec KUp ?>>! repeatingArg $ moveE VLine Backward
, spec KDown ?>>! repeatingArg $ moveE VLine Forward
, spec KPageDown ?>>! repeatingArg downScreenB
, spec KPageUp ?>>! repeatingArg upScreenB
, shift (spec KUp) ?>>! repeatingArg (scrollB (1))
, shift (spec KDown) ?>>! repeatingArg (scrollB 1)
, ctrl (spec KLeft) ?>>! repeatingArg prevWordB
, ctrl (spec KRight) ?>>! repeatingArg nextWordB
, ctrl (spec KHome) ?>>! repeatingArg topB
, ctrl (spec KEnd) ?>>! repeatingArg botB
, ctrl (spec KUp) ?>>! repeatingArg (prevNParagraphs 1)
, ctrl (spec KDown) ?>>! repeatingArg (nextNParagraphs 1)
, ctrlCh '@' ?>>! placeMark
, ctrlCh ' ' ?>>! placeMark
, ctrlCh '/' ?>>! repeatingArg undoB
, ctrlCh '_' ?>>! repeatingArg undoB
, ctrlCh 'a' ?>>! repeatingArg (maybeMoveB Line Backward)
, ctrlCh 'b' ?>>! repeatingArg $ moveE Character Backward
, ctrlCh 'd' ?>>! deleteForward
, ctrlCh 'e' ?>>! repeatingArg (maybeMoveB Line Forward)
, ctrlCh 'f' ?>>! repeatingArg $ moveE Character Forward
, ctrlCh 'g' ?>>! setVisibleSelection False
, ctrlCh 'h' ?>> char 'b' ?>>! acceptedInputsOtherWindow
, ctrlCh 'i' ?>>! adjIndent IncreaseOnly
, ctrlCh 'j' ?>>! newlineAndIndentB
, ctrlCh 'k' ?>>! killLine univArg
, ctrlCh 'l' ?>>! (withCurrentBuffer scrollToCursorB >> userForceRefresh)
, ctrlCh 'm' ?>>! repeatingArg (insertB '\n')
, ctrlCh 'n' ?>>! repeatingArg (moveE VLine Forward)
, ctrlCh 'o' ?>>! repeatingArg (insertB '\n' >> leftB)
, ctrlCh 'p' ?>>! repeatingArg (moveE VLine Backward)
, ctrlCh 'q' ?>> insertNextC univArg
, ctrlCh 'r' ?>> isearchKeymap Backward
, ctrlCh 's' ?>> isearchKeymap Forward
, ctrlCh 't' ?>>! repeatingArg swapB
, ctrlCh 'v' ?>>! scrollDownE univArg
, ctrlCh 'w' ?>>! killRegion
, ctrlCh 'y' ?>>! yank
, ctrlCh 'z' ?>>! suspendEditor
, ctrlCh '+' ?>>! repeatingArg (increaseFontSize 1)
, ctrlCh '-' ?>>! repeatingArg (decreaseFontSize 1)
, ctrl (metaCh 'w') ?>>! appendNextKillE
, ctrl (metaCh ' ') ?>>! layoutManagersNextE
, ctrl (metaCh ',') ?>>! layoutManagerNextVariantE
, ctrl (metaCh '.') ?>>! layoutManagerPreviousVariantE
, ctrl (metaCh 'j') ?>>! nextWinE
, ctrl (metaCh 'k') ?>>! prevWinE
, ctrl (meta $ spec KEnter) ?>>! swapWinWithFirstE
, shift (ctrl $ metaCh 'j') ?>>! moveWinNextE
, shift (ctrl $ metaCh 'k') ?>>! moveWinPrevE
, shift (ctrl $ meta $ spec KEnter) ?>>! pushWinToFirstE
, Event (KASCII ' ') [MShift,MCtrl,MMeta] ?>>! layoutManagersPreviousE
, ctrlCh 'x' ?>> ctrlX
, ctrlCh 'c' ?>> ctrlC
, metaCh ' ' ?>>! justOneSep univArg
, metaCh 'v' ?>>! scrollUpE univArg
, metaCh '!' ?>>! shellCommandE
, metaCh '<' ?>>! repeatingArg topB
, metaCh '>' ?>>! repeatingArg botB
, metaCh '%' ?>>! queryReplaceE
, metaCh '^' ?>>! joinLinesE univArg
, metaCh ';' ?>>! commentRegion
, metaCh 'a' ?>>! repeatingArg (moveE unitSentence Backward)
, metaCh 'b' ?>>! repeatingArg prevWordB
, metaCh 'c' ?>>! repeatingArg capitaliseWordB
, metaCh 'd' ?>>! repeatingArg killWordB
, metaCh 'e' ?>>! repeatingArg (moveE unitSentence Forward)
, metaCh 'f' ?>>! repeatingArg nextWordB
, metaCh 'h' ?>>! repeatingArg (selectNParagraphs 1)
, metaCh 'k' ?>>! repeatingArg (deleteB unitSentence Forward)
, metaCh 'l' ?>>! repeatingArg lowercaseWordB
, metaCh 'm' ?>>! firstNonSpaceB
, metaCh 'q' ?>>! withSyntax modePrettify
, metaCh 'r' ?>>! repeatingArg moveToMTB
, metaCh 'u' ?>>! repeatingArg uppercaseWordB
, metaCh 't' ?>>! repeatingArg (transposeB unitWord Forward)
, metaCh 'w' ?>>! killRingSave
, metaCh 'x' ?>>! executeExtendedCommandE
, metaCh 'y' ?>>! yankPopE
, metaCh '.' ?>>! promptTag
, metaCh '{' ?>>! repeatingArg (prevNParagraphs 1)
, metaCh '}' ?>>! repeatingArg (nextNParagraphs 1)
, metaCh '=' ?>>! countWordsRegion
, metaCh '\\' ?>>! deleteHorizontalSpaceB univArg
, metaCh '@' ?>>! repeatingArg markWord
, meta (spec KBS) ?>>! repeatingArg bkillWordB
, metaCh 'g' ?>>
optMod meta (char 'g') >>! (gotoLn . fromDoc :: Int ::: LineNumber -> BufferM Int)
]
where
blockKillring = insertN ""
withUnivArg :: YiAction (m ()) () => (Maybe Int -> m ()) -> YiM ()
withUnivArg cmd = runAction $ makeAction (cmd univArg)
repeatingArg :: (Monad m, YiAction (m ()) ()) => m () -> YiM ()
repeatingArg f = withIntArg $ \n -> replicateM_ n f
withIntArg :: YiAction (m ()) () => (Int -> m ()) -> YiM ()
withIntArg cmd = withUnivArg $ \arg -> cmd (fromMaybe 1 arg)
deleteBack :: YiM ()
deleteBack = repeatingArg $ blockKillring >> adjBlock (1) >> bdeleteB
deleteForward :: YiM ()
deleteForward = repeatingArg $ blockKillring >> deleteB'
deleteRegionOr :: (Show a, YiAction (m a) a) => m a -> YiM ()
deleteRegionOr f = do
b <- gets currentBuffer
r <- withGivenBuffer b getSelectRegionB
if regionSize r == 0
then runAction $ makeAction f
else withGivenBuffer b $ deleteRegionB r
ctrlC = choice [ ctrlCh 'c' ?>>! commentRegion ]
rectangleFunctions = choice [ char 'a' ?>>! alignRegionOn
, char 'o' ?>>! openRectangle
, char 't' ?>>! stringRectangle
, char 'k' ?>>! killRectangle
, char 'y' ?>>! yankRectangle
]
tabFunctions :: Keymap
tabFunctions = choice [ optMod ctrl (char 'n') >>! nextTabE
, optMod ctrl (char 'p') >>! previousTabE
, optMod ctrl (char 't') >>! newTabE
, optMod ctrl (char 'e') >>! findFileNewTab
, optMod ctrl (char 'd') >>! deleteTabE
, charOf id '0' '9' >>=! moveTabE . Just . digitToInt
]
ctrlX = choice [ ctrlCh 'o' ?>>! deleteBlankLinesB
, char '0' ?>>! closeWindowEmacs
, char '1' ?>>! closeOtherE
, char '2' ?>>! splitE
, char 'h' ?>>! selectAll
, char 's' ?>>! askSaveEditor
, ctrlCh 'b' ?>>! listBuffers
, ctrlCh 'c' ?>>! askQuitEditor
, ctrlCh 'f' ?>>! findFile
, ctrlCh 'r' ?>>! findFileReadOnly
, ctrlCh 'q' ?>>!
((withCurrentBuffer (readOnlyA %= not)) :: EditorM ())
, ctrlCh 's' ?>>! fwriteE
, ctrlCh 'w' ?>>! promptFile "Write file:" (void . fwriteToE)
, ctrlCh 'x' ?>>! (exchangePointAndMarkB >>
highlightSelectionA .= True)
, char 'b' ?>>! switchBufferE
, char 'd' ?>>! dired
, char 'e' ?>>
char 'e' ?>>! evalRegionE
, char 'o' ?>>! nextWinE
, char 'k' ?>>! killBufferE
, char 'r' ?>> rectangleFunctions
, char 'u' ?>>! repeatingArg undoB
, optMod ctrl (char 't') >> tabFunctions
]