From 1a6c020ee6a4b65ad8a45bef4a86f40fbf3ba133 Mon Sep 17 00:00:00 2001 From: gwern0 Date: Thu, 20 Dec 2007 18:40:01 +0100 Subject: Search.hs: cleanup and refactor darcs-hash:20071220174001-f7719-3e10f70144f41e02c0e51b434c561c0a366a49dc.gz --- XMonad/Util/Search.hs | 60 ++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'XMonad/Util/Search.hs') diff --git a/XMonad/Util/Search.hs b/XMonad/Util/Search.hs index 03e6992..31b0496 100644 --- a/XMonad/Util/Search.hs +++ b/XMonad/Util/Search.hs @@ -15,28 +15,23 @@ --------------------------------------------------------------------------- -} module XMonad.Util.Search ( -- * Usage -- $usage - google, - googleSelection, - googleSearch, - wayback, - waybackSelection, - waybackSearch, - wikipedia, - wikipediaSelection, - wikipediaSearch, + google, googleSelection, googleSearch, + wayback, waybackSelection, waybackSearch, + wikipedia, wikipediaSelection, wikipediaSearch, promptSearch, search ) where +import Control.Monad.Trans (MonadIO()) -- for select's type signature +import Data.Char (chr, ord, isAlpha, isMark, isDigit) +import Numeric (showIntAtBase) import XMonad (io, X()) -import XMonad.Util.Run (safeSpawn) -import XMonad.Prompt.Shell (getShellCompl) import XMonad.Prompt (XPrompt(showXPrompt), mkXPrompt, XPConfig()) +import XMonad.Prompt.Shell (getShellCompl) +import XMonad.Util.Run (safeSpawn) import XMonad.Util.XSelection (getSelection) -import Data.Char (chr, ord, isAlpha, isMark, isDigit) -import Numeric (showIntAtBase) --- A customized prompt +-- A customized prompt. data Search = Search instance XPrompt Search where showXPrompt Search = "Search: " @@ -46,14 +41,12 @@ instance XPrompt Search where -- but then that'd be hard enough to copy-and-paste we'd need to depend on 'network'. escape :: String -> String escape = escapeURIString (\c -> isAlpha c || isDigit c || isMark c) - where + where -- Copied from Network.URI. escapeURIString :: - (Char -> Bool) -- ^ a predicate which returns 'False' - -- if the character should be escaped - -> String -- ^ the string to process - -> String -- ^ the resulting URI string + (Char -> Bool) -- a predicate which returns 'False' if should escape + -> String -- the string to process + -> String -- the resulting URI string escapeURIString p s = concatMap (escapeURIChar p) s - escapeURIChar :: (Char->Bool) -> Char -> String escapeURIChar p c | p c = [c] @@ -73,31 +66,40 @@ escape = escapeURIString (\c -> isAlpha c || isDigit c || isMark c) search :: String -> FilePath -> String -> IO () search site browser query = safeSpawn browser $ site ++ escape query -promptSearch :: (String -> String -> IO ()) -> String -> XPConfig -> X () -promptSearch engine browser config = mkXPrompt Search config (getShellCompl []) $ io . (engine browser) - -- The engines googleSearch, waybackSearch, wikipediaSearch :: String -> String -> IO () googleSearch = search "http://www.google.com/search?num=100&q=" wikipediaSearch = search "https://secure.wikimedia.org/wikipedia/en/wiki/Special:Search?go=Go&search=" +waybackSearch = search "http://web.archive.org/" {- This doesn't seem to work, but nevertheless, it seems to be the official method at to get the latest backup. -} -waybackSearch = search "http://web.archive.org/" + +-- | Like 'search', but in this case, the string is not specified but grabbed +-- from the user's response to a prompt. +promptSearch :: (String -> String -> IO ()) -> String -> XPConfig -> X () +promptSearch searchEngine browser config = mkXPrompt Search config (getShellCompl []) $ io . (searchEngine browser) -- | Search the particular site; these are suitable for binding to a key. Use them like this: -- -- > , ((modm, xK_g ), google "firefox" defaultXPConfig) -- --- First argument is the browser you want to use, the second the prompt configuration +-- First argument is the browser you want to use, the second the prompt configuration. google, wayback, wikipedia :: String -> XPConfig -> X () google = promptSearch googleSearch wikipedia = promptSearch wikipediaSearch wayback = promptSearch waybackSearch --- | See previous. Like google\/wikipedia, but one less argument - the query is +-- | Like search, but for use with the X selection; it grabs the selection, +-- passes it to a given searchEngine and opens it in a browser. The various +-- *Selection functions specialize this to a particular search engine to make +-- things easier. +select :: (Control.Monad.Trans.MonadIO m) => (t -> String -> IO a) -> t -> m a +select browser searchEngine = io $ browser searchEngine =<< getSelection + +-- | Like the google\/wikipedia functions, but one less argument - the query is -- extracted from the copy-paste buffer of X Windows. googleSelection, waybackSelection, wikipediaSelection :: String -> X () -googleSelection browser = io $ googleSearch browser =<< getSelection -wikipediaSelection browser = io $ wikipediaSearch browser =<< getSelection -waybackSelection browser = io $ waybackSearch browser =<< getSelection +googleSelection = select googleSearch +wikipediaSelection = select wikipediaSearch +waybackSelection = select waybackSearch -- cgit v1.2.3