<commit_before>-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Prompt.Man
-- Copyright   :  (c) 2007 <NAME>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  <EMAIL>
-- Stability   :  unstable
-- Portability :  non-portable (uses \"manpath\" and \"bash\")
--
-- A manual page prompt for XMonad window manager.
--
-- TODO
--
--   * narrow completions by section number, if the one is specified
--     (like @\/etc\/bash_completion@ does)
--
--   * test with QuickCheck
-----------------------------------------------------------------------------

module XMonad.Prompt.Man (
                                -- * Usage
                                -- $usage
                                manPrompt
                               , getCommandOutput
                               ) where

import XMonad
import XMonad.Prompt
import XMonad.Util.Run
import XMonad.Prompt.Shell (split)

import System.Directory
import System.Process
import System.IO

import qualified Control.Exception as E
import Control.Monad
import Data.List
import Data.Maybe

-- $usage
-- 1. In Config.hs add:
--
-- > import XMonad.Prompt
-- > import XMonad.Prompt.Man
--
-- 2. In your keybindings add something like:
--
-- >     , ((modMask, xK_F1), manPrompt defaultXPConfig) -- mod-f1 %! Query for manual page to be displayed

-- %import XMonad.Prompt
-- %import XMonad.Prompt.Man
-- %keybind , ((modMask, xK_F1), manPrompt defaultXPConfig)

data Man = Man

instance XPrompt Man where
    showXPrompt Man = "Manual page: "

-- | Query for manual page to be displayed.
manPrompt :: XPConfig -> X ()
manPrompt c = mkXPrompt Man c manCompl $ runInTerm . (++) "man "

manCompl :: String -> IO [String]
manCompl str | '/' `elem` str = do
  -- XXX It may be better to use readline instead of bash's compgen...
  lines `fmap` getCommandOutput ("bash -c 'compgen -A file " ++ str ++ "'")
             | otherwise      = do
  mp <- getCommandOutput "manpath -g 2>/dev/null" `E.catch` \_ -> return []
  let sects    = ["man" ++ show n | n <- [1..9 :: Int]]
      dirs     = [d ++ "/" ++ s | d <- split ':' mp, s <- sects]
      stripExt = reverse . drop 1 . dropWhile (/= '.') . reverse
  mans <- forM dirs $ \d -> do
            exists <- doesDirectoryExist d<commit_msg>Prompt/Man.hs: remove docstring.<commit_after>-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Prompt.Man
-- Copyright   :  (c) 2007 <NAME>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  <EMAIL>
-- Stability   :  unstable
-- Portability :  non-portable (uses \"manpath\" and \"bash\")
--
-- A manual page prompt for XMonad window manager.
--
-- TODO
--
--   * narrow completions by section number, if the one is specified
--     (like @\/etc\/bash_completion@ does)
--
--   * test with QuickCheck
-----------------------------------------------------------------------------

module XMonad.Prompt.Man (
                                -- * Usage
                                -- $usage
                                manPrompt
                               , getCommandOutput
                               ) where

import XMonad
import XMonad.Prompt
import XMonad.Util.Run
import XMonad.Prompt.Shell (split)

import System.Directory
import System.Process
import System.IO

import qualified Control.Exception as E
import Control.Monad
import Data.List
import Data.Maybe

-- $usage
-- 1. In Config.hs add:
--
-- > import XMonad.Prompt
-- > import XMonad.Prompt.Man
--
-- 2. In your keybindings add something like:
--
-- >     , ((modMask, xK_F1), manPrompt defaultXPConfig)

-- %import XMonad.Prompt
-- %import XMonad.Prompt.Man
-- %keybind , ((modMask, xK_F1), manPrompt defaultXPConfig)

data Man = Man

instance XPrompt Man where
    showXPrompt Man = "Manual page: "

-- | Query for manual page to be displayed.
manPrompt :: XPConfig -> X ()
manPrompt c = mkXPrompt Man c manCompl $ runInTerm . (++) "man "

manCompl :: String -> IO [String]
manCompl str | '/' `elem` str = do
  -- XXX It may be better to use readline instead of bash's compgen...
  lines `fmap` getCommandOutput ("bash -c 'compgen -A file " ++ str ++ "'")
             | otherwise      = do
  mp <- getCommandOutput "manpath -g 2>/dev/null" `E.catch` \_ -> return []
  let sects    = ["man" ++ show n | n <- [1..9 :: Int]]
      dirs     = [d ++ "/" ++ s | d <- split ':' mp, s <- sects]
      stripExt = reverse . drop 1 . dropWhile (/= '.') . reverse
  mans <- forM dirs $ \d -> do
            exists <- doesDirectoryExist d
{'fraction_non_alphanumeric': 0.1955952900130833, 'fraction_numerical': 0.005887483645878762, 'mean_word_length': 1.6438040345821325, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1735934', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1363, 'n_words': 421}