重新启动时XMonad确认

时间:2012-04-03 13:09:54

标签: haskell xmonad

我是Haskell初学者,我正在使用xmonad。我试图让它在退出之前提示我,因为我偶然偶然地击中mod + q。我找到了两种方法,但我必须做错事,因为它们都不适合我: https://bbs.archlinux.org/viewtopic.php?id=120298 http://comments.gmane.org/gmane.comp.lang.haskell.xmonad/11699

这是我的xmonad.hs:

import XMonad
import XMonad.Config.Gnome
import XMonad.Actions.Plane
import XMonad.Util.EZConfig
import XMonad.Util.Run(spawnPipe)
import qualified Data.Map as M
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.UrgencyHook
import System.IO(Handle, hPutStrLn)
import System.Exit
import Control.Monad
import XMonad
import XMonad.Util.EZConfig
import XMonad.Util.Dmenu
import XMonad.Util.Run

workspaces' = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

quit_confirm :: X ()
quit_confirm = do
  let m = "confirm restart"
  s <- dmenu [m]
  when (m == s) (spawn "xmonad --restart")

conf_quit = do
  response <- runProcessWithInput "dmenu" ["-p", "Quit?"] "yes\nno\n"
  when (response == "yes") (spawn "xmonad --restart")

main = do
        dzen2Pipe <- spawnPipe "dzen2 -w 1200 -xs 1 -ta l -fn '-*-terminus-*-*-*-*-16-*-*-*-*-*-*-*' -bg black -fg #d3d7cf "
        dzen2Right <- spawnPipe "~/.xmonad/status-dzen.sh"
        startupProgs <- spawnPipe "~/.xmonad/startups.sh"
        xmonad $ defaultConfig
             {
             workspaces = workspaces'
             , manageHook = manageHook' <+> manageHook defaultConfig
             , modMask = mod4Mask
             , terminal = "gnome-terminal"
     , layoutHook = layoutHook'
         , logHook = logHook' dzen2Pipe
             }
            `additionalKeysP`
               [ -- Lock Screen
                ("M-S-l",    spawn "gnome-screensaver-command -l")
                -- Sleep
                , ("M-S-;",   spawn "gnome-screensaver-command -l; pmi action suspend")
                -- wireless
                , ("M-S-C-w", spawn "~/.xmonad/wireless.sh")
                -- 1 screen reconf
                , ("M-S-C-1", spawn "~/.xmonad/1-screen.sh")
                -- 2 screen reconf
                , ("M-S-C-2", spawn "~/.xmonad/2-screen.sh")
                -- confirm for quit
                , ("M-q", conf_quit)
                ]
             `additionalKeys`
             M.toList (planeKeys mod4Mask GConf Finite)

layoutHook' = avoidStruts $ layoutHook defaultConfig

logHook' = dynamicLogWithPP . dzenPP'

dzenPP' h = defaultPP
            {
            ppOutput = hPutStrLn h
            }

manageHook' = composeAll
  [ className =? "Pidgin" --> doShift "1" ,
    className =? "Firefox" --> doShift "2" ,
    className =? "Thunderbird" --> doShift "3" ,
    className =? "OpenOffice.org 3.2" --> doShift "4",
    manageDocks ]

谢谢!

编辑:显示菜单,但是生成过程似乎不起作用。

4 个答案:

答案 0 :(得分:2)

其他人给出的答案错过了dmenu似乎在它返回的字符串末尾添加了一个额外的空格。您只需要执行以下操作,它对我有用:

confirm :: String -> X () -> X ()
confirm m f = do
  result <- dmenu [m]
  when (init result == m) f

然后,正如@mariop建议使用

confirm "Restart" $ spawn "xmonad --recompile && xmonad --restart"

confirm "Exit" $ io (exitWith ExitSuccess)

答案 1 :(得分:1)

将键绑定更改为

("M-q", confirm "Confirm restart?" $ restart "xmonad" True)

添加使用dmenu的通用确认操作

confirm :: String -> X () -> X ()
confirm msg f = do
    a <- dmenu [msg,"y","n"]
    when (a=="y") f

答案 2 :(得分:0)

您说您想要退出确认,但您的代码会重新启动确认。 如果这是你想要的重启,我测试了quit_confirm

您最有可能错过的是重新编译xmonad以查看新配置。 试试这个:

quit_confirm :: X ()
quit_confirm = do
  let m = "confirm restart"
  s <- dmenu [m]
  when (m == s) (spawn "xmonad recompile && xmonad --restart")

答案 3 :(得分:0)

我编写了以下模块,不需要安装dmenu

-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Prompt
-- Copyright   :  (C) 2015 Antoine Beaupré
-- License     :  BSD3
--
-- Maintainer  :  Antoine Beaupré <anarcat@debian.org>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A module for setting up simple confirmation prompts for keybindings.
--
-----------------------------------------------------------------------------
module XMonad.Prompt.ConfirmPrompt (confirmPrompt
                                    -- * Usage
                                    -- $usage
                                    , module XMonad.Prompt
                                    -- * Use case: confirming exit
                                    -- $tip
                                    , EnterPrompt
                                    ) where

import XMonad (X)
import XMonad.Prompt (XPConfig, XPrompt, showXPrompt, mkXPrompt, mkComplFunFromList)

{- $usage

This module makes it easy to add a confirmation prompt for specific
actions. Instead of just running the action, a simple confirmation
prompt will be created using 'XMonad.Prompt' primitives. The action
will then run normally if the user confirms.
-}

{- $tip
This should be used something like this:

> ...
> , ((modm , xK_l), confirmPrompt defaultXPConfig "exit" $ io (exitWith ExitSuccess))
> ...
-}

{- | Customized 'XPrompt' prompt that will ask to confirm the given string -}
data EnterPrompt = EnterPrompt String
instance XPrompt EnterPrompt where
    showXPrompt (EnterPrompt n) = "Confirm " ++ n ++ " (esc/ENTER)"

{- | Prompt the user to confirm a given action. We offer no completion
     and simply ask to confirm (ENTER) or cancel (ESCAPE). The actual key
     handling is done by mkXPrompt.-}
confirmPrompt :: XPConfig -> String -> X() -> X()
confirmPrompt config app func = mkXPrompt (EnterPrompt app) config (mkComplFunFromList []) $ const func

将其安装在~/.xmonad/lib/XMonad/Prompt目录中并享受!

(我submitted这是XMonad邮件列表,我正在等待反馈......)