Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.4 KiB

  1. #!/bin/sh
  2. # title: dmenu_websearch <http://efe.kim/dmenu_websearch.html>
  3. # license: CC0
  4. # author: Sunur Efe Vural <efe@efe.kim>
  5. # version: Mar 22, 2019
  6. # dependencies: dmenu, xdotool, hexdump, xprop, setxkbmap, coreutils.
  7. # A browser-independent address bar with bookmark support. When the
  8. # cursor is on a web browser it acts as the address bar of that browser.
  9. engine='https://google.com/search?q=%s'
  10. bookmarks="$HOME/.local/share/bookmarks"
  11. gotourl() {
  12. if [ "$nbrowser" = surf ]
  13. then
  14. xprop -id "$winid" -f _SURF_GO 8s -set _SURF_GO "$choice"
  15. elif [ -n "$winid" ] && [ -z "$nbrowser" ]
  16. then
  17. #change layout to us cuz xdotool spasms with non-latin layouts
  18. layout=$(setxkbmap -query | awk '/^layout:/{ print $2 }')
  19. setxkbmap -layout us
  20. xdotool key --clearmodifiers "$shortcut"\
  21. type --clearmodifiers --delay 2 "$choice"
  22. xdotool key --clearmodifiers Return
  23. setxkbmap -layout "$layout"
  24. elif [ -n "$nbrowser" ]
  25. then
  26. $nbrowser "$choice"
  27. else $BROWSER "$choice"
  28. fi
  29. }
  30. searchweb() {
  31. #convert search query to percent encoding and insert it into url
  32. choice=$(echo "$choice" | hexdump -v -e '/1 " %02x"')
  33. choice=$(echo "$engine" | sed "s/%s/${choice% 0a}/;s/[[:space:]]/%/g")
  34. gotourl
  35. }
  36. xprop -root | grep '^_NET_ACTIVE_WINDOW' && {
  37. winid=$(xprop -root _NET_ACTIVE_WINDOW | sed 's/.*[[:space:]]//')
  38. class=$(xprop -id "$winid" WM_CLASS | awk -F'\"' '{ print $(NF - 1) }')
  39. case "$class" in
  40. Firefox) nbrowser='firefox' ;;
  41. #Firefox) shortcut='ctrl+l' ;; # alternative method, uses xdotool
  42. IceCat) nbrowser='icecat' ;;
  43. Chromium) nbrowser='chromium' ;;
  44. Chrome) nbrowser='chrome' ;;
  45. Opera) nbrowser='opera' ;;
  46. Vivaldi) nbrowser='vivaldi' ;; # not tested
  47. Brave) nbrowser='brave' ;; # not tested
  48. Conkeror) nbrowser='conkeror' ;; # not tested
  49. Palemoon) nbrowser='palemoon' ;; # not tested
  50. Iceweasel) nbrowser='iceweasel' ;; # not tested
  51. qutebrowser) nbrowser='qutebrowser' ;;
  52. Midori) nbrowser='midori' ;; # not that good
  53. Luakit) nbrowser='luakit' ;; # uses the last window instance
  54. Uzbl|Vimb) shortcut='o' ;;
  55. Links) shortcut='g' ;;
  56. Netsurf*|Epiphany|Dillo|Konqueror|Arora) shortcut='ctrl+l' ;;
  57. Surf) nbrowser='surf' ; uricur=$(xprop -id "$winid" _SURF_URI |\
  58. awk -F'\"' '{ print $( NF - 1 ) }') ;;
  59. *) pid=$(xprop -id "$winid" _NET_WM_PID | awk '{ print $3 }')
  60. while pgrep -oP "$pid" >/dev/null
  61. do
  62. pid=$(pgrep -oP "$pid")
  63. done
  64. pname=$(awk '/^Name\:/{ print $NF }' /proc/"$pid"/status) ||
  65. winid="" ;;
  66. esac
  67. [ -n "$pname" ] && case "$pname" in
  68. w3m) shortcut="U" ;;
  69. lynx|elinks|links) shortcut="g" ;;
  70. *) winid="" ;;
  71. esac
  72. }
  73. tmpfile=$(mktemp /tmp/dmenu_websearch.XXXXXX)
  74. trap 'rm "$tmpfile"' 0 1 15
  75. printf '%s\n%s\n' "$uricur" "$1" > "$tmpfile"
  76. cat "$bookmarks" >> "$tmpfile"
  77. sed -i -E '/^(#|$)/d' "$tmpfile"
  78. choice=$(dmenu -i -p "Go:" -w "$winid" < "$tmpfile") || exit 1
  79. # Detect links without protocol (This is WIP)
  80. protocol='^(https?|ftps?|mailto|about|file):///?'
  81. checkurl() {
  82. grep -Fx "$choice" "$tmpfile" &&
  83. choice=$(echo "$choice" | awk '{ print $1 }') && return 0
  84. [ ${#choice} -lt 4 ] && return 1
  85. echo "$choice" | grep -Z ' ' && return 1
  86. echo "$choice" | grep -EiZ "$protocol" && return 0
  87. echo "$choice" | grep -FZ '..' && return 1
  88. prepath=$(echo "$choice" | sed 's/(\/|#|\?).*//')
  89. echo "$prepath" | grep -FvZ '.' && return 1
  90. echo "$prepath" | grep -EZ '^([[:alnum:]~_:-]+\.?){1,3}' && return 0
  91. }
  92. if checkurl
  93. then
  94. echo "$choice" | grep -EivZ "$protocol" &&
  95. choice="http://$choice"
  96. gotourl
  97. else searchweb
  98. fi