packages feed

yi-0.3: Yi/Keymap/Emacs/UnivArgument.hs

{-# LANGUAGE CPP, DeriveDataTypeable, FlexibleContexts, GeneralizedNewtypeDeriving #-}

-- Copyright (c) 2005, 2008 Jean-Philippe Bernardy

module Yi.Keymap.Emacs.UnivArgument where

import Yi.Core
import Yi.Editor
import Yi.Keymap

import Data.Maybe
import Data.Dynamic

import Control.Monad ( replicateM_ )

newtype UniversalArg = UniversalArg (Maybe Int)
    deriving Typeable

-- doing the argument precisely is kind of tedious.
-- read: http://www.gnu.org/software/emacs/manual/html_node/Arguments.html
-- and: http://www.gnu.org/software/emacs/elisp-manual/html_node/elisp_318.html

instance Initializable UniversalArg where
    initial = UniversalArg Nothing


withUnivArg :: YiAction (m ()) () => (Maybe Int -> m ()) -> YiM ()
withUnivArg cmd = do UniversalArg a <- withEditor getDynamic
                     runAction $ makeAction (cmd a)
                     withEditor $ setDynamic $ UniversalArg Nothing

withIntArg :: YiAction (m ()) () => (Int -> m ()) -> YiM ()
withIntArg cmd = withUnivArg $ \arg -> cmd (fromMaybe 1 arg)

repeatingArg :: (Monad m, YiAction (m ()) ()) => m () -> YiM ()
repeatingArg f = withIntArg $ \n -> replicateM_ n f