简体   繁体   English

重新启动时XMonad确认

[英]XMonad confirmation when restarting

I'm a Haskell beginner and I'm using xmonad. 我是Haskell初学者,我正在使用xmonad。 I'm trying to make it prompt me before quitting as I occasionally hit mod+q by accident. 我试图让它在退出之前提示我,因为我偶然偶然地击中mod + q。 I've found two ways of doing that, but I must be doing something wrong because neither of them work for me: https://bbs.archlinux.org/viewtopic.php?id=120298 http://comments.gmane.org/gmane.comp.lang.haskell.xmonad/11699 我找到了两种方法,但我必须做错事,因为它们都不适合我: https ://bbs.archlinux.org/viewtopic.php id = 120298 http://comments.gmane。组织/ gmane.comp.lang.haskell.xmonad / 11699

Here's my xmonad.hs: 这是我的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 ]

Thanks! 谢谢!

Edit: the menu is displayed, but spawn process doesn't seem to work. 编辑:显示菜单,但生成过程似乎不起作用。

The answers others gave missed that dmenu seems to add an extra whitespace to the end of the string it returns. 其他人给出的答案错过了dmenu似乎在它返回的字符串的末尾添加了一个额外的空格。 You just need to do the following and it works for me: 你只需要做以下事情就可以了:

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

And then, as @mariop suggested use either 然后,正如@mariop建议使用的那样

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

or 要么

confirm "Exit" $ io (exitWith ExitSuccess)

Change the key binding to 将密钥绑定更改为

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

And add a generic confirmation action that uses dmenu 并添加使用dmenu的通用确认操作

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

I wrote the following module, which doesn't require dmenu to be installed: 我编写了以下模块,它不需要安装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

Install this in a ~/.xmonad/lib/XMonad/Prompt directory and enjoy! 将其安装在~/.xmonad/lib/XMonad/Prompt目录中即可享受!

(I submitted this to the XMonad mailing list and I'm waiting for feedback...) (我将其提交给XMonad邮件列表,我正在等待反馈......)

You say you want a quit confirm but your code does a restart confirm. 您说您想要退出确认,但您的代码会重新启动确认。 If this is restart that you want, I tested and quit_confirm works. 如果这是你想要的重启,我测试了并且quit_confirm有效。

What you most likely miss is the recompilation of xmonad to see your new configuration. 您最有可能错过的是重新编译xmonad以查看新配置。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM