xmonad-contrib/XMonad/Util/SpawnNamedPipe.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

75 lines
2.4 KiB
Haskell

-----------------------------------------------------------------------------
-- |
-- Module : XMonad.Util.SpawnNamedPipe
-- Description : A module for spawning a pipe whose handle lives in the XMonad state.
-- Copyright : (c) Christian Wills 2014
-- License : BSD3-style (see LICENSE)
--
-- Maintainer : cwills.dev@gmail.com
-- Stability : unstable
-- Portability : not portable
--
-- A module for spawning a pipe whose 'Handle' lives in the Xmonad state.
--
-----------------------------------------------------------------------------
module XMonad.Util.SpawnNamedPipe (
-- * Usage
-- $usage
spawnNamedPipe
, getNamedPipe
) where
import XMonad
import XMonad.Util.Run
import System.IO
import qualified XMonad.Util.ExtensibleState as XS
import XMonad.Prelude
import qualified Data.Map as Map
-- $usage
-- This module makes it possible to spawn a pipe to Dzen2 in the startupHook
-- and write to it from inside the logHook without the need for global
-- variables.
--
-- > import XMonad.Util.SpawnNamedPipe
-- > import Data.Maybe
-- >
-- > -- StartupHook
-- > startupHook' = spawnNamedPipe "dzen2" "dzenPipe"
-- >
-- > -- LogHook
-- > logHook' = do
-- > mh <- getNamedPipeHandle "dzenPipe"
-- > dynamicLogWithPP $ def {
-- > ppOutput = maybe (\s -> return ()) (hPutStrLn) mh}
-- >
-- > -- Main
-- > main = xmonad $ def { startupHook = startupHook'
-- > , logHook = logHook'}
--
newtype NamedPipes = NamedPipes { pipeMap :: Map.Map String Handle }
deriving (Show)
instance ExtensionClass NamedPipes where
initialValue = NamedPipes Map.empty
-- | When 'spawnNamedPipe' is executed with a command 'String' and a name
-- 'String' respectively. The command string is spawned with 'spawnPipe' (as
-- long as the name chosen hasn't been used already) and the 'Handle' returned
-- is saved in Xmonad's state associated with the name 'String'.
spawnNamedPipe :: String -> String -> X ()
spawnNamedPipe cmd name = do
b <- XS.gets (Map.member name . pipeMap)
unless b $ do
h <- spawnPipe cmd
XS.modify (NamedPipes . Map.insert name h . pipeMap)
-- | Attempts to retrieve a 'Handle' to a pipe previously stored in Xmonad's
-- state associated with the given string via a call to 'spawnNamedPipe'. If the
-- given string doesn't exist in the map stored in Xmonad's state Nothing is
-- returned.
getNamedPipe :: String -> X (Maybe Handle)
getNamedPipe name = XS.gets (Map.lookup name . pipeMap)