xmonad-contrib/XMonad/Util/ActionCycle.hs
Adam Plaice ca866229f6 Fix most remaining broken inter-module docs links
The links were broken due to:

1. Incorrect quotes (' instead of " for module links and occasionally
vice-versa).

2. Changes in the name of the "target" module not reflected in the
"source" docs.

3. Typos to begin with.

4. Use of `<foo>` in the docs is rendered as just `foo` with a link to
`/foo`.

5. Similarly for `"Foo"` if it starts with a capital letter (and hence
could be a module).

6. Markup inside `@` code blocks still being applied.

e.g. `@M-<arrow-keys>@` is rendered as `M-arrow-keys` with a spurious
hyperlink from arrow-keys to `/arrow-keys`, which is confusing.

Three links from XMonad.Util.Run have been removed outright, since
they're no longer examples of the usage of 'runProcessWithInput'.
WmiiActions has been gone since 2008, while XMonad.Prompt.Directory
and XMonad.Layout.WorkspaceDir haven't been using
'runProcessWithInput' since 2020 and 2012, respectively.

In some cases the `<foo>` were surrounded with @, especially in the
case of key definitions, for consistency.  (This wasn't done
everywhere, because it looks ugly in the source.)

MoreManageHelpers has never been in xmonad-contrib.  ManageHelpers
seems to fill the expected role.

In the case of the module description for X.H.ManageDebug the quotes
were simply removed because none of the likely options to make the
link work were successful.
2022-11-01 19:35:55 +01:00

87 lines
3.1 KiB
Haskell

{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
-----------------------------------------------------------------------------
-- |
-- Module : XMonad.Util.ActionCycle
-- Description : Provides a way to implement cycling actions.
-- Copyright : (c) 2020 Leon Kowarschick
-- License : BSD3-style (see LICENSE)
--
-- Maintainer : Leon Kowarschick. <thereal.elkowar@gmail.com>
-- Stability : unstable
-- Portability : unportable
--
-- This module provides a way to have "cycling" actions.
-- This means that you can define an @X ()@ action that cycles through a list of actions,
-- advancing every time it is executed.
-- This may for exapmle be useful for toggle-style keybindings.
--
-----------------------------------------------------------------------------
module XMonad.Util.ActionCycle
( -- * Usage
-- $usage
cycleAction
, cycleActionWithResult
)
where
import Prelude hiding ((!!))
import Data.Map.Strict as M
import XMonad
import qualified XMonad.Util.ExtensibleState as XS
import qualified Data.List.NonEmpty as NonEmpty
import Data.List.NonEmpty ((!!), NonEmpty((:|)))
-- $usage
-- You can use this module to implement cycling key-bindings by importing "XMonad.Util.ActionCycle"
--
-- > import XMonad.Util.ActionCycle
--
-- and then creating a keybinding as follows:
--
-- > ((mod1Mask, xK_t), cycleAction "cycleActions" [ spawn "commmand1", spawn "command2", spawn "command3" ])
--
-- Note that the name given to cycleAction must be a unique action per cycle.
-- | Generate an @X ()@ action that cycles through a list of actions,
-- advancing every time the action is called.
cycleAction
:: String -- ^ Unique name for this action. May be any arbitrary, unique string.
-> [X ()] -- ^ List of actions that will be cycled through.
-> X ()
cycleAction _ [] = pure ()
cycleAction name (x:xs) = cycleActionWithResult name (x :| xs)
-- | Another version of 'cycleAction' that returns the result of the actions.
-- To allow for this, we must make sure that the list of actions is non-empty.
cycleActionWithResult
:: String -- ^ Unique name for this action. May be any arbitrary, unique string.
-> NonEmpty.NonEmpty (X a) -- ^ Non-empty List of actions that will be cycled through.
-> X a
cycleActionWithResult name actions = do
cycleState <- XS.gets (getActionCycle name)
idx <- case cycleState of
Just x -> do
XS.modify (nextActionCycle name (NonEmpty.length actions))
pure x
Nothing -> do
XS.modify (setActionCycle name 1)
pure 0
actions !! idx
newtype ActionCycleState = ActionCycleState (M.Map String Int)
instance ExtensionClass ActionCycleState where
initialValue = ActionCycleState mempty
getActionCycle :: String -> ActionCycleState -> Maybe Int
getActionCycle name (ActionCycleState s) = M.lookup name s
nextActionCycle :: String -> Int -> ActionCycleState -> ActionCycleState
nextActionCycle name maxNum (ActionCycleState s) = ActionCycleState $ M.update (\n -> Just $ (n + 1) `mod` maxNum) name s
setActionCycle :: String -> Int -> ActionCycleState -> ActionCycleState
setActionCycle name n (ActionCycleState s) = ActionCycleState $ M.insert name n s