[dev] [surf] shell script to do bookmarks, history, and other things

From: Peter John Hartman <peterjohnhartman_AT_gmail.com>
Date: Wed, 31 Mar 2010 13:14:26 -0400 (EDT)

Hi Folks,

So, I've cobbled together the attached shell script to do most of what I
want to do with surf, including history, smart prefixes, and bookmarks.
The upshot is that one doesn't need the history patch and the searchengines
patch.

I'd appreciate comments on this, esp.\ with respect to how I can figure
out what the TITLE value is of a new uri. The current technique does
a nasty little loop; and it still sometimes returns odd values for the
TITLE. You can see that in the comments to the code.

In config.h replace the SETPROP line with:

#define SETPROP(p) { .v = (char *[]){ "/bin/sh", "-c", "surf.sh $1 $0", p, winid, NULL } }

And you also bind:

{ MODKEY, GDK_b, spawn, SETPROP("_SURF_BMARK") },

============= surf.sh ===================================================

#!/bin/sh
# TODO add to surf-start.sh a cleanup routine to get rid of all old entries
# $1 = $xid
# $2 = $p = _SURF_FIND _SURF_BMARK _SURF_URI i.e. what SETPROP states in config.h

function s_add() {
         url=$1
         name=$2
         timestamp=`date +%Y%m%d%H%M%S`
         grep "$url" ~/.surf/history >/dev/null || echo -e "$url\t$name\t$timestamp" >> ~/.surf/history
}

function s_get_uri() {
         xid=$1
         xprop -id $xid _SURF_URI | cut -d '"' -f 2
}

function s_get_name() {
         xid=$1
         xprop -id $xid WM_ICON_NAME | cut -d '"' -f 2
}

function s_set_uri() {
         xid=$1
         newuri=$2
         xprop -id $xid -f _SURF_URI 8s -set _SURF_URI "$newuri" 2>/dev/null >/dev/null
}

xid=$1
p=$2

if [[ $p == "_SURF_FIND" ]]; then
         dmenu -b -p ":"
     exit;
elif [[ $p == "_SURF_BMARK" ]]; then
         cur=`s_get_uri $xid`
         name=`s_get_name $xid`
             s_add "$cur" "$name"
         exit;
else
         # display the history PLUS the current uri via dmenu
         cur=`s_get_uri $xid`
         curname=`s_get_name $xid`
             cp ~/.surf/history /tmp/surf.history.$$
             echo -e "$cur\t$curname\t$timestamp" >> /tmp/surf.history.$$
             selection=`tac /tmp/surf.history.$$ | awk 'BEGIN { FS="\t" } {print $1" "$2}' | dmenu -b -l 10`
         rm -f /tmp/surf.history.$$

         # if we hit escape, then exit
         if [[ $selection == "" ]]; then
                 exit;
         fi

         # post "smart" process
         # entries ($selection) will either be:
         # gg foobar foo foo2 foo3 i.e. <SMART ABBV><SPACE><VALUE>
         # or:
         # http://www.google.com/ NAME OF SITE i.e. <URL><SPACE><NAME>
         #

         # this removes trailing whitespace, ie when there is no name
         OPTION=$(echo $selection | sed -e 's/^ *//g;s/ *//g' | awk '{print $1}')

         if [[ $OPTION == "gg" ]]; then
             VALUE=$(echo $selection | sed 's/gg //' | urlencode.sh)
             URL="http://www.google.com/search?q=$VALUE"
             s_set_uri $xid $URL
             s_add "$selection" ""
         elif [[ $OPTION == "ut" ]]; then
             VALUE=$(echo $selection | sed 's/ut //' | urlencode.sh)
             URL="http://search2.library.utoronto.ca/UTL/index?N=0&Ntk=Anywhere&Ntt=$VALUE&Ntx=mode%2Bmatchallpartial&Nu=p_work_normalized&Np=1&formName=search_form_simple"
             s_set_uri $xid $URL
             s_add "$selection" ""
         else
             # otherwise go to the url they enter
             URL=$(echo $selection | awk '{print $1}')
             s_set_uri $xid $URL
             # NASTY little hack to figure out what the REAL TITLE is.
             name="`s_get_name $xid`"
             while echo $name | grep "[*%\]"; do
                     name=`s_get_name $xid`
             done
             s_add $URL "$name"
         fi
fi

Peter Hartman

--
sic dicit magister P.
http://individual.utoronto.ca/peterjh/
Received on Wed Mar 31 2010 - 17:14:26 UTC

This archive was generated by hypermail 2.2.0 : Wed Mar 31 2010 - 17:24:02 UTC