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.

71 lines
2.1 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. nbrowser=$BROWSER
  37. tmpfile=$(mktemp /tmp/dmenu_websearch.XXXXXX)
  38. trap 'rm "$tmpfile"' 0 1 15
  39. printf '%s\n%s\n' "$uricur" "$1" > "$tmpfile"
  40. cat "$bookmarks" >> "$tmpfile"
  41. sed -i -E '/^(#|$)/d' "$tmpfile"
  42. choice=$(dmenu -i -p "Go:" -w "$winid" < "$tmpfile") || exit 1
  43. # Detect links without protocol (This is WIP)
  44. protocol='^(https?|ftps?|mailto|about|file):///?'
  45. checkurl() {
  46. grep -Fx "$choice" "$tmpfile" &&
  47. choice=$(echo "$choice" | awk '{ print $1 }') && return 0
  48. [ ${#choice} -lt 4 ] && return 1
  49. echo "$choice" | grep -Z ' ' && return 1
  50. echo "$choice" | grep -EiZ "$protocol" && return 0
  51. echo "$choice" | grep -FZ '..' && return 1
  52. prepath=$(echo "$choice" | sed 's/(\/|#|\?).*//')
  53. echo "$prepath" | grep -FvZ '.' && return 1
  54. echo "$prepath" | grep -EZ '^([[:alnum:]~_:-]+\.?){1,3}' && return 0
  55. }
  56. if checkurl
  57. then
  58. echo "$choice" | grep -EivZ "$protocol" &&
  59. choice="http://$choice"
  60. gotourl
  61. else searchweb
  62. fi