xmonad-contrib-0.7: Third party extensions for xmonadSource codeContentsIndex
Use case: searching with a submap

A module for easily running Internet searches on web sites through xmonad. Modeled after the handy Surfraw CLI search tools at https://secure.wikimedia.org/wikipedia/en/wiki/Surfraw.

Additional sites welcomed.

search :: MonadIO m => Browser -> SearchEngine -> Query -> m ()
simpleEngine :: Query -> SearchEngine
promptSearch :: XPConfig -> Browser -> SearchEngine -> X ()
selectSearch :: MonadIO m => Browser -> SearchEngine -> m ()
google :: SearchEngine

This module is intended to allow easy access to databases on the Internet through xmonad's interface. The idea is that one wants to run a search but the query string and the browser to use must come from somewhere. There are two places the query string can come from - the user can type it into a prompt which pops up, or the query could be available already in the X Windows copy/paste buffer (perhaps you just highlighted the string of interest).

Thus, there are two main functions: promptSearch, and selectSearch (implemented using the more primitive search). To each of these is passed an engine function; this is a function that knows how to search a particular site.

For example, the google function knows how to search Google, and so on. You pass promptSearch and selectSearch the engine you want, the browser you want, and anything special they might need; this whole line is then bound to a key of you choosing in your xmonad.hs. For specific examples, see each function. This module is easily extended to new sites by using simpleEngine.

The currently available search engines are:

  • amazon -- Amazon keyword search.
  • google -- basic Google search.
  • hoogle -- Hoogle, the Haskell libraries search engine.
  • imdb -- the Internet Movie Database.
  • maps -- Google maps.
  • mathworld -- Wolfram MathWorld search.
  • scholar -- Google scholar academic search.
  • wayback -- the Wayback Machine.
  • wikipedia -- basic Wikipedia search.

Feel free to add more!

search :: MonadIO m => Browser -> SearchEngine -> Query -> m ()Source
Given a browser, a search engine, and a search term, perform the requested search in the browser.
simpleEngine :: Query -> SearchEngineSource

Given a base URL, create the SearchEngine that escapes the query and appends it to the base. You can easily define a new engine locally using simpleEngine without needing to modify Search.hs:

 newEngine = simpleEngine "http://site.com/search="

The important thing is that the site has a interface which accepts the query string as part of the URL. Alas, the exact URL to feed simpleEngine varies from site to site, often considerably. Generally, examining the resultant URL of a search will allow you to reverse-engineer it if you can't find the necessary URL already described in other projects such as Surfraw.

promptSearch :: XPConfig -> Browser -> SearchEngine -> X ()Source

Like search, but in this case, the string is not specified but grabbed from the user's response to a prompt. Example:

 , ((modm, xK_g), promptSearch greenXPConfig "firefox" google)
selectSearch :: MonadIO m => Browser -> SearchEngine -> m ()Source

Like search, but for use with the X selection; it grabs the selection, passes it to a given searchEngine and opens it in the given browser. Example:

 , ((modm .|. shiftMask, xK_g), selectSearch "firefox" google)
google :: SearchEngineSource
Use case: searching with a submap

In combination with XMonad.Actions.Submap you can create a powerful and easy way to search without adding a whole bunch of bindings.

First import the necessary modules:

 import qualified XMonad.Prompt         as P
 import qualified XMonad.Actions.Submap as SM
 import qualified XMonad.Actions.Search as S

Then add the following to your key bindings:

 -- Search commands
 , ((modm, xK_s), SM.submap $ searchEngineMap $ S.promptSearch P.defaultXPConfig)
 , ((modm .|. shiftMask, xK_s), SM.submap $ searchEngineMap $ S.selectSearch)


 searchEngineMap method = M.fromList $
       [ ((0, xK_g), method \"firefox\" S.google)
       , ((0, xK_h), method \"firefox\" S.hoogle)
       , ((0, xK_w), method \"firefox\" S.wikipedia)

Make sure to set firefox to open new pages in a new window instead of in a new tab: Firefox -> Edit -> Preferences -> Tabs -> New pages should be opened in...

Now mod-s + g/h/w prompts you for a search string, then opens a new firefox window that performs the search on Google, Hoogle or Wikipedia respectively.

If you select something in whatever application and hit mod-shift-s + g/h/w it will search the selected string with the specified engine.

Happy searching!

Produced by Haddock version 2.3.0