aboutsummaryrefslogtreecommitdiffstats
path: root/XMonad/Util/Search.hs
diff options
context:
space:
mode:
authorgwern0 <gwern0@gmail.com>2007-12-13 21:51:59 +0100
committergwern0 <gwern0@gmail.com>2007-12-13 21:51:59 +0100
commit5682d1093eb25f6465300c7c767abca06ebe4bf0 (patch)
treef1d94efd7bf5517b5a7f211871d44bac3350af0f /XMonad/Util/Search.hs
parentf73bde137e9737230932553586f6ad84727544de (diff)
downloadXMonadContrib-5682d1093eb25f6465300c7c767abca06ebe4bf0.tar.gz
XMonadContrib-5682d1093eb25f6465300c7c767abca06ebe4bf0.tar.xz
XMonadContrib-5682d1093eb25f6465300c7c767abca06ebe4bf0.zip
+XMonad.Util.Search: new module
This module is intended to provide helpful functions for easily running web searchs; just hit a bound key, enter your query, and up opens a new tab/browser/window with the search results. In theory anyway; the Wikipedia and Google ones work fine for me, but the Internet Archive's docs on how to do don't necessarily seem to be correct. If you were, like me, previously running shell commands to call Surfraw or similar shell scripts to do the same thing, you can now scrap them and replace them. There aren't too many search engines defined here; new ones would be good, and they're easy to add! darcs-hash:20071213205159-f7719-a0c0378f1e82af4c0b7126382ef1375fbb607dfc.gz
Diffstat (limited to 'XMonad/Util/Search.hs')
-rw-r--r--XMonad/Util/Search.hs78
1 files changed, 78 insertions, 0 deletions
diff --git a/XMonad/Util/Search.hs b/XMonad/Util/Search.hs
new file mode 100644
index 0000000..ec627ab
--- /dev/null
+++ b/XMonad/Util/Search.hs
@@ -0,0 +1,78 @@
+{- |
+ Module : XMonad.Util.Search
+ Copyright : (C) 2007 Gwern Branwen
+ License : None; public domain
+
+ Maintainer : <gwern0@gmail.com>
+ Stability : unstable
+ Portability : unportable
+
+ A module for easily running Internet searches on web sites through XMonad.
+ Modeled after the handy Surfraw CLI search tools
+ <https://secure.wikimedia.org/wikipedia/en/wiki/Surfraw>.
+
+ Additional sites welcomed.
+--------------------------------------------------------------------------- -}
+module XMonad.Util.Search ( -- * Usage
+ -- $usage
+ google,
+ googleSelection,
+ wayback,
+ waybackSelection,
+ wikipedia,
+ wikipediaSelection,
+ promptSearch,
+ search
+ ) where
+
+import Data.Char (isAlpha, isDigit, isMark)
+import XMonad (io, X())
+import XMonad.Util.Run (safeSpawn)
+import Network.URI (escapeURIString)
+import XMonad.Prompt.Shell (getShellCompl)
+import XMonad.Prompt (XPrompt(showXPrompt), mkXPrompt, XPConfig())
+import XMonad.Util.XSelection (getSelection)
+
+-- A customized prompt
+data Search = Search
+instance XPrompt Search where
+ showXPrompt Search = "Search: "
+
+-- | Escape the search string so search engines understand it.
+-- We could just go (const False) and escape anything that even looks at us
+-- funny, but that produces obfuscated search queries. So we merely escape
+-- anything that doesn't look unfunny.
+escape :: String -> String
+escape = escapeURIString (\c -> isAlpha c || isDigit c || isMark c)
+
+-- | Given the base search URL, a browser to use, and the actual query, escape
+-- the query, prepend the base URL, and hand it off to the browser.
+search :: String -> FilePath -> String -> IO ()
+search site browser search = safeSpawn browser $ site ++ escape search
+
+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="
+{- This doesn't seem to work, but nevertheless, it seems to be the official
+ method at <http://web.archive.org/collections/web/advanced.html> to get the
+ latest backup. -}
+waybackSearch = search "http://web.archive.org/"
+
+-- | 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
+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
+-- 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