URL from Surfraw

Description

This script launches dmenu containing all the elvi and bookmarks defined in surfraw, and allows you to use those to search the web. If the user inputs something that resembles a URL, it goes to that url instead. Otherwise, just use Google (or some predefined elvi).

Bash script

#!/bin/bash
#
#
# Default search engine. If we really are at a loss, use this
DEFAULT_SEARCH=google
 
# The location of the surfraw bookmark file
BOOKMARKS="$HOME/.surfraw.bookmarks"
 
# Colors for dmenu
COLORS=" -nb #303030 -nf khaki -sb #CCFFAA -sf #303030"
 
# Prompt for dmenu
PROMPT="Open"
 
# Use surfraw to search for the words
function search()
{
  # Does surfraw know what to do with it?
  url=`surfraw -print $@`
 
  # If not, then use the default search engine
  if [ $? -ne 0 ]
  then
    url=`surfraw -print $DEFAULT_SEARCH $@`
  fi
 
  echo $url
}
 
# We assume that this is a URL
function goto()
{
  case "$1" in
    *:*) echo $1 ;;
    *) echo "http://$1" ;;
  esac
}
 
# Use dmenu to navigate through possible choices
function present_menu()
{
  elvi=`surfraw -elvi | cut -f 1 | tail --lines=+2`
  if [ -r "$BOOKMARKS" ]
  then
    bookmarks=" `cut -f 1 -d ' ' "$BOOKMARKS" 2>/dev/null`"
  else
    bookmarks=""
  fi
 
  echo "${elvi}${bookmarks}" | tr ' ' '\n' | sort | dmenu -p "$PROMPT" -i $COLORS
}
 
present_menu | \
  ( read car cdr
    test -z "$car" && exit 1
    ( test -z "$cdr" && echo $car | fgrep -c '.
:' > /dev/null && goto $car ) || search "$car $cdr" )

Installation

The above script can be bound in your uzbl config in the following ways. To open the selected page in the current window:

@bind o = sh 'uri=`$XDG_DATA_HOME/uzbl/scripts/load_url_from_surfraw.sh` && echo "uri $uri" > $4'

You can also use it to open a new UZBL instance with the new URL (similar to tab-open in vimperator):

@bind t = sh 'uri=`$XDG_DATA_HOME/uzbl/scripts/load_url_from_surfraw.sh` && uzbl --uri $uri'

Todo

what about a 2-step dmenu? first select search engine, then type search query. - Dieter_be

 
url-from-surfraw.txt · Last modified: 2010/01/10 08:28 by mason.larobina
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki