dotfiles/zsh/.config/functions.zsh

87 lines
2.4 KiB
Bash

#Copyright 2018 TheToric
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
# 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