[Xmonad] ShellPrompt completion like dmenus

Andrea Rossato mailing_list at istitutocolli.org
Thu Sep 20 10:53:51 EDT 2007


On Thu, Sep 20, 2007 at 03:03:05PM +0200, Tobias Hauth wrote:
> Hi,
> 
> Is it possible to have ShellPrompt complete like dmenu does? For example
> if I type "fire" it should also show mozilla-firefox and not only
> everything starting with fire... .

Hi,

there is a huge difference between dmenu and ShellPrompt, afaik:
ShellPrompt gets the completions from bash (or readline in version
0.3), whereas in demnu you need to provide them when launching dmenu,
right?

Now, having ShellPrompt complete "mozilla-firefox" for "fire" requires
loading ALL binaries' names present in you path and search them. While
feasible I think it would be just too much resource consuming.

BTW you can try it yourself. Attached there's a version of ShellPrompt
that does what you want (it doesn't require readline): give it a try
and, please, report back you impressions.

Andrea
-------------- next part --------------
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonadContrib.ShellPrompt
-- Copyright   :  (C) 2007 Andrea Rossato
-- License     :  BSD3
-- 
-- Maintainer  :  andrea.rossato at unibz.it
-- Stability   :  unstable
-- Portability :  unportable
--
-- A shell prompt for XMonad
--
-----------------------------------------------------------------------------

module XMonadContrib.ShellPrompt (
                             -- * Usage
                             -- $usage
                             shellPrompt
                             , rmPath
                             , split
                              ) where

import XMonad
import XMonadContrib.XPrompt

import Control.Monad
import Data.List
import System.Directory
import System.Environment

-- $usage
--
-- 1. In Config.hs add:
--
-- > import XMonadContrib.XPrompt
-- > import XMonadContrib.ShellPrompt
--
-- 2. Add XMonadContrib.XPromptLib to the "other-modules" list in xmonad.cabal:
--
-- > other-modules:      Config Operations StackSet XMonad XMonadContrib.XPromptLib
--
-- 3. In your keybindings add something like:
--
-- >   , ((modMask .|. controlMask, xK_x), shellPrompt defaultXPConfig)
--

-- %import XMonadContrib.XPrompt
-- %import XMonadContrib.ShellPrompt
-- %keybind , ((modMask .|. controlMask, xK_x), shellPrompt defaultXPConfig)

data Shell = Shell

instance XPrompt Shell where
    showXPrompt Shell = "Run:   "


shellPrompt :: XPConfig -> X ()
shellPrompt c = mkXPrompt Shell c getShellCompl spawn

getShellCompl :: String -> IO [String]
getShellCompl s 
    | s /= "" && last s /= ' ' = do
  fl <- getDirectoryContents s `catch` \_ -> return []
  c <- commandCompletionFunction s
  return $ sort . nub $ fl ++ c
    | otherwise = return []

commandCompletionFunction :: String -> IO [String]
commandCompletionFunction str 
    | '/' `elem` str = return []
    | otherwise = do
  p <- getEnv "PATH"
  cl p
    where
      cl = liftM (nub . rmPath . concat) . mapM fCF . split ':'  
      fCF s = liftM (filter (str `isInfixOf`)) $ getDirectoryContents  s `catch` \_ -> return []

rmPath :: [String] -> [String]
rmPath s = 
    map (reverse . fst . break  (=='/') . reverse) s

split :: Eq a => a -> [a] -> [[a]]
split _ [] = []
split e l =
    f : split e (rest ls)
        where 
          (f,ls) = span (/=e) l
          rest s | s == [] = []
                 | otherwise = tail s



More information about the Xmonad mailing list