search (2794B)
1 #!/bin/sh 2 histfile="$HOME/.cache/searchhist" 3 4 if [ $# -lt 1 ]; then 5 targets="$( 6 tail -1 "$histfile"; 7 grep -E '^\[' "${HOME}/doc/bookmark.gph"; 8 grep 'href="htt' "${HOME}/doc/bookmark.html" | 9 sed 's,</.*>,,g; s,.*<a href=",,; s,">, ,; s,</a>,,'; 10 cat "$HOME"/doc/bookmark.txt; 11 dmenu_path)" 12 13 if [ -z "$TERM" -o "$TERM" = linux -o "$TERM" = dumb ]; then 14 c=$(printf '%s' "$targets" | dmenu -i) || exit 1 15 else 16 c=$(printf '%s' "$targets" | fzy) || exit 1 17 fi 18 else 19 c="$*" 20 fi 21 22 if [ "$c" = "r" ]; then 23 c="$(tail -1 "$histfile")" 24 else 25 printf '%s\n' "$c" > "$histfile" 26 fi 27 28 regexmatch() { 29 printf '%s' "$1" | grep -E -q "$2" 30 } 31 32 regeximatch() { 33 printf '%s' "$1" | grep -i -E -q "$2" 34 } 35 36 if command -v "$(printf '%s' "$c" | sed 's/ .*//')" >/dev/null 2>&1; then 37 out="$(eval "$c" 2>&1)" 38 s=$? 39 if [ "$s" -gt 0 ]; then 40 s=" (${s})" 41 else 42 s="" 43 fi 44 if [ -n "$out" ]; then 45 notify "\$ ${c}${s}" "$out" 46 fi 47 exit "$s" 48 fi 49 50 if regeximatch "$c" '^gopher://' || \ 51 regeximatch "$c" '^\[(0|1|3|7|9|g|I|h)\|.*\|.*\|[0-9]+\]' || \ 52 { regexmatch "$c" '^(0|1|3|7|9|g|I|h)' && \ 53 [ "$(printf '%s' "$c" | awk -F'\t' '{print NF}')" -eq 4 ]; }; then 54 plumb "$c" 55 56 elif regeximatch "$c" '^(http|https)://'; then 57 plumb "$(printf '%s' "$c" | awk '{print $1}')" 58 59 elif regeximatch "$c" '!scholar'; then 60 plumb "https://scholar.google.com/scholar?q=$(printf '%s' "$c" | sed 's/ *!scholar *//')" 61 62 elif regeximatch "$c" '!map'; then 63 export SURFFLAGS="-S -I" 64 plumb "https://www.openstreetmap.org/search?query=$(printf '%s' "$c" | sed 's/ *!map *//')" 65 66 elif regeximatch "$c" '!wiki'; then 67 plumb "https://en.wikipedia.org/wiki/Special:Search?search=$(printf '%s' "$c" | sed 's/ *!wiki *//')" 68 69 elif regeximatch "$c" '^rfc[0-9][0-9]*$'; then 70 plumb "ftp://ftp.rfc-editor.org/in-notes/${c}.txt" 71 72 elif regexmatch "$c" '!translate'; then 73 plumb "https://en.bab.la/dictionary/danish-english/$(printf '%s' "$c" | sed 's/ *!translate *//')" 74 75 elif regexmatch "$c" '!yt'; then 76 q="$(printf '%s' "$c" | sed "s/ *!yt *//")" 77 #idiotbox="$HOME/code/idiotbox" 78 #if [ -x "$idiotbox/cgi" ]; then 79 #tmpfile=/tmp/yt.html 80 #QUERY_STRING="q=$(printf '%s' "$q" | tr ' ' '+')" \ 81 #"$idiotbox/cgi" > "$tmpfile" 82 #cp -r "$idiotbox/css" /tmp 83 #plumb "$tmpfile" 84 #sleep 5 85 #rm "$tmpfile" 86 #else 87 #plumb "https://codemadness.org/idiotbox/?q=${q}&o=relevance&m=Dark" 88 #fi 89 plumb "gopher://git.codemadness.org/7/youtube.cgi ${q}" 90 #plumb "https://codemadness.org/idiotbox/?q=${q}&o=relevance&m=Dark" 91 92 elif regexmatch "$c" ' '; then 93 regexmatch "$c" '!i' && export SURFFLAGS="-S -I" 94 plumb "https://duckduckgo.com/lite?q=$(printf '%s' "$c" | sed 's/+/%2b/g;s/&/%26/g;s/ /%20/g')&kad=en_US&kp=-2&kc=-1&kae=d&k1=-1&kk=-1&kam=osm&kaj=m&kv=-1&kak=-1&kax=-1&kaq=-1&kap=-1&kao=-1&kau=-1&kg=p&kd=-1&kac=-1" 95 else 96 plumb "http://${c}" 97 fi