tsurf.sh - scripts - random scripts
HTML git clone https://git.parazyd.org/scripts
DIR Log
DIR Files
DIR Refs
---
tsurf.sh (4469B)
---
1 #!/bin/sh
2 # v. 2.0 - upgrade based on surf 4.0
3 # Creative Commons License. Peter John Hartman (http://individual.utoronto.ca/peterjh)
4 # Much thanks to nibble and pancake who have a different surf.sh script available which
5 # doesn't do the history bit.
6 #
7 # this script does:
8 # * stores history of: (1) successful uri entries; (2) certain smart prefix entries, e.g., "g foobar"; (3) find entries
9 # * direct bookmark (via ^b)
10 # * information debug (via ^I)
11 # * smart prefixes e.g. g for google search, t for tinyurl, etc.
12 # * delete (with smart prefix x)
13 #
14 # $1 = $xid
15 # $2 = $p = _SURF_FIND _SURF_BMARK _SURF_URI (what SETPROP sets in config.h)
16 #
17 # // replace default setprop with this one
18 # #define SETPROP(p) { .v = (char *[]){ "/bin/sh", "-c", "surf.sh $0 $1 $2", p, q, winid, NULL } }
19 #
20 # { MODKEY, GDK_b, spawn, SETPROP("_SURF_BMARK") },
21 # { MODKEY|GDK_SHIFT_MASK, GDK_i, spawn, SETPROP("_SURF_INFO") },
22 # { MODKEY|GDK_SHIFT_MASK, GDK_g, spawn, SETPROP("_SURF_URI_RAW") },
23
24 #font='-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*'
25 font='Terminus:pixelsize=14'
26 normbgcolor='#181818'
27 normfgcolor='#e9e9e9'
28 selbgcolor=$normbgcolor
29 selfgcolor='#dd6003'
30 bmarks=~/.surf/history.txt
31 ffile=~/.surf/find.txt
32
33 pid=$1
34 fid=$2
35 xid=$3
36
37 dmenu="dmenu -nb $normbgcolor -nf $normfgcolor \
38 -sb $selbgcolor -sf $selfgcolor"
39
40 s_get_prop() { # xprop
41 xprop -id $xid $1 | cut -d '"' -f 2
42 }
43
44 s_set_prop() { # xprop value
45 [ -n "$2" ] && xprop -id $xid -f $1 8s -set $1 "$2"
46 }
47
48 s_write_f() { # file value
49 [ -n "$2" ] && (sed -i "\|$2|d" $1; echo "$2" >> $1)
50 #grep "$uri" $bmarks >/dev/null 2>&1 || echo "$uri" >> $bmarks
51 }
52
53 s_set_write_proper_uri() { # uri
54 # TODO: (xprop -spy _SURF_URI ... | while read name __ value; do echo $value; done works quite nice for eventloops)
55 # input is whatever the use inputed, so don't store that!
56 # first, clear the name field because surf doesn't sometimes
57 #s_set_prop WM_ICON_NAME ""
58 # set the uri
59 s_set_prop _SURF_GO "$1"
60 # get the new name
61 name=`s_get_prop WM_ICON_NAME`
62 # loop until the [10%] stuff is finished and we have a load (is this necessary?)
63 #while echo $name | grep "[*%\]" >/dev/null 2>&1; do
64 # name=`s_get_prop WM_ICON_NAME`
65 #done
66 # bail on error and don't store
67 #if [[ $name != "Error" ]]; then
68 # uri=`s_get_prop _SURF_URI`
69 # store to the bmarks file the OFFICIAL url (with http://whatever)
70 s_write_f $bmarks "$1"
71 #grep "$uri" $bmarks >/dev/null 2>&1 || echo "$uri" >> $bmarks
72 #fi
73 }
74
75 case "$pid" in
76 "_SURF_INFO")
77 xprop -id $xid | sed 's/\t/ /g' | $dmenu -fn "$font" -b -l 20
78 ;;
79 "_SURF_FIND")
80 find="`tac $ffile 2>/dev/null | $dmenu -fn "$font" -b -p find:`"
81 s_set_prop _SURF_FIND "$find"
82 s_write_f $ffile "$find"
83 ;;
84 "_SURF_BMARK")
85 uri=`s_get_prop _SURF_URI`
86 s_write_f $bmarks "$uri"
87 ;;
88 "_SURF_URI_RAW")
89 uri=`echo $(s_get_prop _SURF_URI) | $dmenu -fn "$font" -b -p "uri:"`
90 s_set_prop _SURF_GO "$uri"
91 ;;
92 "_SURF_URI")
93 sel=`tac $bmarks 2> /dev/null | $dmenu -fn "$font" -b -l 5 -p "uri [dgtwuy*]:"`
94 [ -z "$sel" ] && exit
95 opt=$(echo $sel | cut -d ' ' -f 1)
96 arg=$(echo $sel | cut -d ' ' -f 2-)
97 save=0
98 case "$opt" in
99 "d") # del.icio.us
100 uri="http://del.icio.us/save?url=`s_get_prop _SURF_URI`"
101 ;;
102 "g") # google for it
103 uri="http://www.google.com/search?q=$arg"
104 save=1
105 ;;
106 "t") # tinyurl
107 uri="http://tinyurl.com/create.php?url=`s_get_prop _SURF_URI`"
108 ;;
109 "w") # wikipedia
110 uri="http://wikipedia.org/wiki/$arg"
111 save=1
112 ;;
113 "u") # utoronto
114 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"
115 save=1
116 ;;
117 "y") # youtube
118 uri="http://www.youtube.com/results?search_query=$arg&aq=f"
119 save=1
120 ;;
121 "x") # delete
122 sed -i "\|$arg|d" $bmarks
123 exit;
124 ;;
125 *)
126 uri="$sel"
127 save=2
128 ;;
129 esac
130
131 # only set the uri; don't write to file
132 [ $save -eq 0 ] && s_set_prop _SURF_GO "$uri"
133 # set the url and write exactly what the user inputed to the file
134 [ $save -eq 1 ] && (s_set_prop _SURF_GO "$uri"; s_write_f $bmarks "$sel")
135 # try to set the uri only if it is a success
136 [ $save -eq 2 ] && s_set_write_proper_uri "$uri"
137 ;;
138 *)
139 echo Unknown xprop
140 ;;
141 esac