{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, PatternGuards #-}
module XMonad.Layout.WorkspaceDir (
workspaceDir,
changeDir,
WorkspaceDir,
Chdir(Chdir),
) where
import System.Directory ( setCurrentDirectory, getCurrentDirectory )
import XMonad.Prelude ( when )
import XMonad hiding ( focus )
import XMonad.Prompt ( XPConfig )
import XMonad.Prompt.Directory ( directoryPrompt )
import XMonad.Layout.LayoutModifier
import XMonad.StackSet ( tag, currentTag )
newtype Chdir = Chdir String
instance Message Chdir
newtype WorkspaceDir a = WorkspaceDir String deriving ( ReadPrec [WorkspaceDir a]
ReadPrec (WorkspaceDir a)
ReadS [WorkspaceDir a]
forall a. ReadPrec [WorkspaceDir a]
forall a. ReadPrec (WorkspaceDir a)
forall a. Int -> ReadS (WorkspaceDir a)
forall a. ReadS [WorkspaceDir a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [WorkspaceDir a]
$creadListPrec :: forall a. ReadPrec [WorkspaceDir a]
readPrec :: ReadPrec (WorkspaceDir a)
$creadPrec :: forall a. ReadPrec (WorkspaceDir a)
readList :: ReadS [WorkspaceDir a]
$creadList :: forall a. ReadS [WorkspaceDir a]
readsPrec :: Int -> ReadS (WorkspaceDir a)
$creadsPrec :: forall a. Int -> ReadS (WorkspaceDir a)
Read, Int -> WorkspaceDir a -> ShowS
forall a. Int -> WorkspaceDir a -> ShowS
forall a. [WorkspaceDir a] -> ShowS
forall a. WorkspaceDir a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WorkspaceDir a] -> ShowS
$cshowList :: forall a. [WorkspaceDir a] -> ShowS
show :: WorkspaceDir a -> String
$cshow :: forall a. WorkspaceDir a -> String
showsPrec :: Int -> WorkspaceDir a -> ShowS
$cshowsPrec :: forall a. Int -> WorkspaceDir a -> ShowS
Show )
instance LayoutModifier WorkspaceDir Window where
modifyLayout :: forall (l :: * -> *).
LayoutClass l Window =>
WorkspaceDir Window
-> Workspace String (l Window) Window
-> Rectangle
-> X ([(Window, Rectangle)], Maybe (l Window))
modifyLayout (WorkspaceDir String
d) Workspace String (l Window) Window
w Rectangle
r = do String
tc <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> i
currentTagforall b c a. (b -> c) -> (a -> b) -> a -> c
.XState -> WindowSet
windowset)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (String
tc forall a. Eq a => a -> a -> Bool
== forall i l a. Workspace i l a -> i
tag Workspace String (l Window) Window
w) forall a b. (a -> b) -> a -> b
$ String -> X ()
scd String
d
forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace String (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout Workspace String (l Window) Window
w Rectangle
r
handleMess :: WorkspaceDir Window
-> SomeMessage -> X (Maybe (WorkspaceDir Window))
handleMess (WorkspaceDir String
_) SomeMessage
m
| Just (Chdir String
wd) <- forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m = do String
wd' <- String -> X String
cleanDir String
wd
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. String -> WorkspaceDir a
WorkspaceDir String
wd'
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
workspaceDir :: LayoutClass l a => String -> l a
-> ModifiedLayout WorkspaceDir l a
workspaceDir :: forall (l :: * -> *) a.
LayoutClass l a =>
String -> l a -> ModifiedLayout WorkspaceDir l a
workspaceDir String
s = forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout (forall a. String -> WorkspaceDir a
WorkspaceDir String
s)
cleanDir :: String -> X String
cleanDir :: String -> X String
cleanDir String
x = String -> X ()
scd String
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO String
getCurrentDirectory
scd :: String -> X ()
scd :: String -> X ()
scd String
x = forall (m :: * -> *). MonadIO m => IO () -> m ()
catchIO forall a b. (a -> b) -> a -> b
$ String -> IO ()
setCurrentDirectory String
x
changeDir :: XPConfig -> X ()
changeDir :: XPConfig -> X ()
changeDir XPConfig
c = XPConfig -> String -> (String -> X ()) -> X ()
directoryPrompt XPConfig
c String
"Set working directory: " (forall a. Message a => a -> X ()
sendMessage forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Chdir
Chdir)