dotfiles/zsh/.zsh/functions.zsh

72 lines
1.8 KiB
Bash

# intellegently extract archives based on extension.
# web_search from terminal
function web_search() {
emulate -L zsh
# define search engine URLS
typeset -A urls
urls=(
ddg "https://www.duckduckgo.com/?q="
github "https://github.com/search?q="
)
# check whether the search engine is supported
if [[ -z "$urls[$1]" ]]; then
echo "Search engine $1 not supported."
return 1
fi
# search or go to main page depending on number of arguments passed
if [[ $# -gt 1 ]]; then
# build search url:
# join arguments passed with '+', then append to search engine URL
url="${urls[$1]}${(j:+:)@[2,-1]}"
else
# build main page url:
# split by '/', then rejoin protocol (1) and domain (2) parts with '//'
url="${(j://:)${(s:/:)urls[$1]}[1,2]}"
fi
open_command "$url"
}
#use generalized open command
function open_command() {
emulate -L zsh
setopt shwordsplit
local open_cmd
# define the open command
case "$OSTYPE" in
darwin*) open_cmd='open' ;;
cygwin*) open_cmd='cygstart' ;;
linux*) open_cmd='xdg-open' ;;
msys*) open_cmd='start ""' ;;
*) echo "Platform $OSTYPE not supported"
return 1
;;
esac
# don't use nohup on OSX
if [[ "$OSTYPE" == darwin* ]]; then
$open_cmd "$@" &>/dev/null
else
nohup $open_cmd "$@" &>/dev/null
fi
}
# Show dots while waiting for tab-completion
expand-or-complete-with-dots() {
# toggle line-wrapping off and back on again
[[ -n "$terminfo[rmam]" && -n "$terminfo[smam]" ]] && echoti rmam
print -Pn "%{%F{red}......%f%}"
[[ -n "$terminfo[rmam]" && -n "$terminfo[smam]" ]] && echoti smam
zle expand-or-complete
zle redisplay
}
zle -N expand-or-complete-with-dots
bindkey "^I" expand-or-complete-with-dots