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

From: Peter John Hartman <peterjohnhartman_AT_gmail.com>
Date: Thu, 1 Apr 2010 21:41:48 -0400 (EDT)

The code looks even better. Here's some more additions and some
subtractions.

* I didn't like the "tag" business with the bmarks. It complicates with
little payoff.

* You never in fact had entries being written to history. I store both
smart prefix entries, e.g., "g foobar foo" and ordinary entries e.g.
"http://www.foobar.org"

* I still need to re-implement the code to verify that the entered url in
fact resolved before storing that entry into the history file. That's in
the old script.

* I have a history to the find as well.

================ surf.sh ==============================================
#!/bin/sh
# v1.1
# AUTHORS: wart_ (Peter John Hartman), pancake, nibbles
# v1.0
# did stuff
# v1.1
# * added find history
# * removed the tags (useless, right?)
# * actually store stuff in history (and not only when you bookmark)
# * added an "info" option for debugging maybe other reasons.
# * make sure you don't add stuff twice check.
#
# TODO: It'd be nice to have a sane way to also store the name or title of the page
#
# $1 = $xid
# $2 = $p = _SURF_FIND _SURF_BMARK _SURF_URI (what SETPROP sets in config.h)
#
# // replace default setprop with this one
# #define SETPROP(p) { .v = (char *[]){ "/bin/sh", "-c", "surf.sh $1 $0", p, winid, NULL } }
#
# // fix shift+slash keybinding in spanish keyboard (f.example)
# { MODKEY, GDK_s, spawn, SETPROP("_SURF_FIND") },
# { MODKEY, GDK_b, spawn, SETPROP("_SURF_BMARK") },
# { MODKEY|GDK_SHIFT_MASK, GDK_g, spawn, SETPROP("_SURF_URI_RAW") },

font='-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*'
normbgcolor='#181818'
normfgcolor='#e9e9e9'
selbgcolor='#dd6003'
selfgcolor='#e9e9e9'
hist=~/.surf/history.txt
ffile=~/.surf/find.txt

xid=$1
p=$2
uri=`xprop -id $xid _SURF_URI | cut -d '"' -f 2`
#name=`xprop -id $xid WM_ICON_NAME | cut -d '"' -f 2`
dmenu="dmenu -e $xid -fn $font -nb $normbgcolor -nf $normfgcolor \
     -sb $selbgcolor -sf $selfgcolor -b"

s_set_uri() { # uri
     xprop -id $xid -f _SURF_URI 8s -set _SURF_URI "$1"
}

s_write_s_history() { # uri/sel e.g. g foobar or http://www.foobar.com
         grep -e "$1" -e "https?://$1" $hist >/dev/null 2>&1 || echo -e "$1" >> $hist
}

s_write_f_history() { # find
         grep "$1" $ffile >/dev/null 2>&1 || echo "$1" >> $ffile
}

case "$p" in
"_SURF_INFO")
         xprop -id $xid | sed 's/\t/ /g' | $dmenu -b -l 20
         ;;
"_SURF_FIND")
     find="`tac $ffile 2>/dev/null | $dmenu -p find:`"
     xprop -id $xid -f _SURF_FIND 8s -set _SURF_FIND "$find"
         s_write_f_history "$find"
     ;;
"_SURF_BMARK")
         s_write_s_history "$uri"
     ;;
"_SURF_URI_RAW")
     uri="`echo | $dmenu -p uri:`"
     s_set_uri "$uri"
     ;;
"_SURF_URI")
     sel=`tac $hist 2> /dev/null | $dmenu -l 5 -p "uri :"`
     # if we hit escape, then exit
     [ -z "$sel" ] && exit
     opt=$(echo $sel | cut -d ' ' -f 1)
     arg=$(echo $sel | cut -d ' ' -f 2-)
     case "$opt" in
     "d") # del.icio.us
         uri="http://del.icio.us/save?url=$uri"
         ;;
     "g") # google for it
         uri="http://www.google.com/search?q=$arg"
         ;;
     "t") # tinyurl
         [ -n "$uri" ] && uri="http://tinyurl.com/create.php?url=$uri"
         ;;
     "w") # wikipedia
         uri="http://wikipedia.org/wiki/$arg"
         ;;
     "u") # utoronto
                 uri="http://search2.library.utoronto.ca/UTL/index?N=0&Ntk=Anywhere&Ntt=$arg&Ntx=mode%2Bmatchallpartial&Nu=p_work_normalized&Np=1&formName=search_form_simple"
                 ;;
     "y") # youtube
         uri="http://www.youtube.com/results?search_query=$arg&aq=f"
         ;;
     *)
         uri="$sel"
         ;;
     esac
     s_set_uri "$uri"
     s_write_s_history "$sel"
     ;;
*)
     echo Unknown xprop
     ;;
esac

Peter (wart_)

--
sic dicit magister P.
http://individual.utoronto.ca/peterjh/
On Thu, 1 Apr 2010, pancake wrote:
> nibble and me cleaned up a bit more my refactored script and
> now supports youtube and does not depends on awk, the code
> is now much cleaner and easy to ready/modify.
>
> The last script depends on dmenu from mercurial, so it uses
> -e $xid to embed.
>
> http://surf.suckless.org/files/bmarks
>
> enjoy :)
>
> btw nibble will probably change the ^G to run $EDITOR to
> edit the url. any other idea? feedback?
>
> pancake wrote:
>>  Conceptually looks nice, but the script uses some bashisms and linuxsms
>>  and
>>  depends on urlencode.sh which is not distributed with the script. it also
>>  has a race condition in the history file on multiuser environments and it
>>  is possible to steal the history information because of the cp
>>    hack: (while : ; chmod 000 /tmp/history* ; done)
>>
>>  I think I can simplify it a bit and fix these issues:
>>
>>    http://lolcathost.org/b/surf.sh
>>
>>  Some functionalities has changed and some other has been added. actually
>>  it supports:
>>
>>   - bookmarking with tags ^b
>>   - sorted by bookmark date
>>   - autocompletion of bookmarked uris with ^g
>>   - open uri without autocomplete ^G
>>   - fix search keybinding in spanish keyboard ^f (can this be added in
>>   mainstream?)
>>   - uri handlers:
>>     - google (g food)
>>     - wikipedia (w foo bar)
>>     - del.icio.us (d )
>>     - tinyurl (t )
>>     - open url matching tags (h web editor)
>>
>>  it can be improved, but at least it works with no dependencies or
>>  linuxisms.
>>
>>  Let me know if you find any issue.
>>
>>  --pancake
>>
>>  On Wed, 31 Mar 2010 13:14:26 -0400 (EDT)
>>  Peter John Hartman <peterjohnhartman_AT_gmail.com> wrote:
>>
>> 
>> >  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 Fri Apr 02 2010 - 01:41:48 UTC

This archive was generated by hypermail 2.2.0 : Fri Apr 02 2010 - 01:48:02 UTC