Merge pull request #441 from slotThe/xdg-mess

Revise XDG handling
This commit is contained in:
Tomáš Janoušek 2021-01-07 19:52:23 +01:00 committed by GitHub
commit d0813f0b3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 24 deletions

View File

@ -44,6 +44,17 @@
All of these were now removed. You can use the re-exported `def` from
`Data.Default` instead.
* `XMonad.Hooks.Script`
`execScriptHook` now has an `X` constraint (was: `MonadIO`), due to changes
in how the xmonad core handles XDG directories.
* `XMonad.Prompt`
`historyCompletion` and `historyCompletionP` now both have an `X`
constraint (was: `IO`), due to changes in how the xmonad core handles XDG
directories.
### New Modules
* `XMonad.Util.Hacks`

View File

@ -363,18 +363,16 @@ namedEngine name (SearchEngine _ site) = searchEngineF name site
Prompt's result, passes it to a given searchEngine and opens it in a given
browser. -}
promptSearchBrowser :: XPConfig -> Browser -> SearchEngine -> X ()
promptSearchBrowser config browser (SearchEngine name site) =
mkXPrompt (Search name) config (historyCompletionP ("Search [" `isPrefixOf`)) $ search browser site
promptSearchBrowser config browser (SearchEngine name site) = do
hc <- historyCompletionP ("Search [" `isPrefixOf`)
mkXPrompt (Search name) config hc $ search browser site
{- | Like 'promptSearchBrowser', but only suggest previous searches for the
given 'SearchEngine' in the prompt. -}
promptSearchBrowser' :: XPConfig -> Browser -> SearchEngine -> X ()
promptSearchBrowser' config browser (SearchEngine name site) =
mkXPrompt
(Search name)
config
(historyCompletionP (searchName `isPrefixOf`))
$ search browser site
promptSearchBrowser' config browser (SearchEngine name site) = do
hc <- historyCompletionP (searchName `isPrefixOf`)
mkXPrompt (Search name) config hc $ search browser site
where
searchName = showXPrompt (Search name)

View File

@ -44,7 +44,7 @@ import XMonad
-- @~\/.xmonad\/hooks startup@ will also.
-- | Execute a named script hook
execScriptHook :: MonadIO m => String -> m ()
execScriptHook :: String -> X ()
execScriptHook hook = do
xmonadDir <- getXMonadDir
let script = xmonadDir ++ "/hooks "

View File

@ -543,7 +543,8 @@ mkXPromptImplementation historyKey conf om = do
XConf { display = d, theRoot = rw } <- ask
s <- gets $ screenRect . W.screenDetail . W.current . windowset
numlock <- gets X.numberlockMask
hist <- io readHistory
cachedir <- getXMonadCacheDir
hist <- io $ readHistory cachedir
fs <- initXMF (font conf)
st' <- io $
bracket
@ -562,7 +563,7 @@ mkXPromptImplementation historyKey conf om = do
releaseXMF fs
when (successful st') $ do
let prune = take (historySize conf)
io $ writeHistory $
io $ writeHistory cachedir $
M.insertWith
(\xs ys -> prune . historyFilter conf $ xs ++ ys)
historyKey
@ -1560,21 +1561,21 @@ type History = M.Map String [String]
emptyHistory :: History
emptyHistory = M.empty
getHistoryFile :: IO FilePath
getHistoryFile = fmap (++ "/prompt-history") getXMonadCacheDir
getHistoryFile :: FilePath -> FilePath
getHistoryFile cachedir = cachedir ++ "/prompt-history"
readHistory :: IO History
readHistory = readHist `E.catch` \(SomeException _) -> return emptyHistory
readHistory :: FilePath -> IO History
readHistory cachedir = readHist `E.catch` \(SomeException _) -> return emptyHistory
where
readHist = do
path <- getHistoryFile
let path = getHistoryFile cachedir
xs <- bracket (openFile path ReadMode) hClose hGetLine
readIO xs
writeHistory :: History -> IO ()
writeHistory hist = do
path <- getHistoryFile
let filtered = M.filter (not . null) hist
writeHistory :: FilePath -> History -> IO ()
writeHistory cachedir hist = do
let path = getHistoryFile cachedir
filtered = M.filter (not . null) hist
writeFile path (show filtered) `E.catch` \(SomeException e) ->
hPutStrLn stderr ("error writing history: "++show e)
setFileMode path mode
@ -1667,14 +1668,17 @@ breakAtSpace s
-- | 'historyCompletion' provides a canned completion function much like
-- 'getShellCompl'; you pass it to mkXPrompt, and it will make completions work
-- from the query history stored in the XMonad cache directory.
historyCompletion :: ComplFunction
historyCompletion :: X ComplFunction
historyCompletion = historyCompletionP (const True)
-- | Like 'historyCompletion' but only uses history data from Prompts whose
-- name satisfies the given predicate.
historyCompletionP :: (String -> Bool) -> ComplFunction
historyCompletionP p x = fmap (toComplList . M.filterWithKey (const . p)) readHistory
where toComplList = deleteConsecutive . filter (isInfixOf x) . M.foldr (++) []
historyCompletionP :: (String -> Bool) -> X ComplFunction
historyCompletionP p = do
cd <- getXMonadCacheDir
pure $ \x ->
let toComplList = deleteConsecutive . filter (isInfixOf x) . M.foldr (++) []
in toComplList . M.filterWithKey (const . p) <$> readHistory cd
-- | Sort a list and remove duplicates. Like 'deleteAllDuplicates', but trades off
-- laziness and stability for efficiency.