Skip to content

Commit

Permalink
modify mkAbsolutePath to support environment vars
Browse files Browse the repository at this point in the history
If you want more general support, comment on PR xmonad#744.
  • Loading branch information
geekosaur committed Aug 20, 2022
1 parent ee97eec commit 0639b32
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@
- Added `WindowScreen`, which is a type synonym for the specialized `Screen`
type, that results from the `WindowSet` definition in `XMonad.Core`.

- Modified `mkAbsolutePath` to support a leading environment variable, so
things like `$HOME/NOTES` work. If you want more general environment
variable support, comment on [this PR].

* `XMonad.Util.XUtils`

- Added `withSimpleWindow`, `showSimpleWindow`, `WindowConfig`, and
Expand Down
26 changes: 17 additions & 9 deletions XMonad/Prelude.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
--------------------------------------------------------------------
-- |
-- Module : XMonad.Prelude
Expand Down Expand Up @@ -60,6 +61,8 @@ import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Tuple (swap)
import GHC.Stack
import System.Directory (getHomeDirectory)
import System.Environment (getEnv)
import Control.Exception (SomeException, handle)
import qualified XMonad.StackSet as W

-- | Short for 'fromIntegral'.
Expand All @@ -80,7 +83,7 @@ chunksOf i xs = chunk : chunksOf i rest
(!?) xs n | n < 0 = Nothing
| otherwise = listToMaybe $ drop n xs

-- | Multivariant composition.
-- | Multivariable composition.
--
-- > f .: g ≡ (f .) . g ≡ \c d -> f (g c d)
(.:) :: (a -> b) -> (c -> d -> a) -> c -> d -> b
Expand Down Expand Up @@ -108,14 +111,19 @@ safeGetWindowAttributes w = withDisplay $ \dpy -> io . alloca $ \p ->
-- * If it starts with @~\/@, replace that with the actual home
-- * directory.
--
-- * Otherwise, prepend a @\/@ to the path.
-- * If it starts with @$@, read the name of an environment
-- * variable and replace it with the contents of that.
--
-- * Otherwise, prepend the home directory and @\/@ to the path.
mkAbsolutePath :: MonadIO m => FilePath -> m FilePath
mkAbsolutePath ps = do
home <- liftIO getHomeDirectory
pure $ case ps of
'/' : _ -> ps
'~' : '/' : _ -> home <> drop 1 ps
_ -> home <> ('/' : ps)
home <- io getHomeDirectory
case ps of
'/' : _ -> pure ps
'~' : '/' : _ -> pure (home <> drop 1 ps)
'$' : _ -> let (v,ps') = span (`elem` ("_"<>['A'..'Z']<>['a'..'z']<>['0'..'9'])) (drop 1 ps)
in io ((\(_ :: SomeException) -> pure "") `handle` getEnv v) Exports.<&> (<> ps')
_ -> pure (home <> ('/' : ps))
{-# SPECIALISE mkAbsolutePath :: FilePath -> IO FilePath #-}
{-# SPECIALISE mkAbsolutePath :: FilePath -> X FilePath #-}

Expand Down

0 comments on commit 0639b32

Please sign in to comment.