zsh: Autoload big functions

This commit is contained in:
2020-09-22 00:28:28 +02:00
parent 16896efa02
commit 0788926c8c
5 changed files with 155 additions and 143 deletions

View File

@@ -308,41 +308,6 @@ function histgrep() {
grep "$@" "${HISTFILE:-$HOME/.zsh_history}"
}
function clang-format() {
local idx=${@[(I)-style*]}
if (( ! idx )); then
# No style flag given
command clang-format "$@"
return
fi
local style="${@[$idx]#-style}" prefix=""
if [ -n "$style" ]; then
# Flag was given in form -style=<style>
style="${style#=}"
prefix="-style="
else
# Flag was given in form -style <style>
(( idx++ ))
style="${@[$idx]}"
fi
if [ ! -e "$style" ]; then
# Argument is not a file and thus probably a valid style string that can
# be passes to clang-format
command clang-format "$@"
return
fi
# Delete all empty lines (not counting whitespace) and comments and join all
# lines with commas.
style="$(sed -E '/^\s*($|#)/d' "$style" | tr '\n' ',')"
style="{${style%,}}"
# Overwrite the argument of the style flag with the parsed format file.
set -- "${@[1, $idx - 1]}" "${prefix}${style}" "${@[$idx + 1, -1]}"
command clang-format "$@"
}
function urlenc() {
python3 -c "from urllib import parse; print(parse.quote('$@'), end='')"
}
@@ -351,112 +316,6 @@ function urldec() {
python3 -c "from urllib import parse; print(parse.unquote('$@'), end='')"
}
glog() {
# Return if not in git repo
git rev-parse || return
# extendedglob is necessary for the expansion of the binds array
emulate -L zsh -o extendedglob
# One line format for fzf list view
# abbreviated commit hash (yellow), title and ref names
local formatshort='--pretty=format:%C(yellow)%h %Creset%s%C(auto)%d'
# Verbose format for the preview window on the right
# This array is stitched together with newlines later
local format=(
'--pretty=format:%C(yellow)' # newline created by this eaten by %-
'%-commit: %H%C(auto)' # yellow commit hash
'%-D%Cblue' # auto colored ref names (if any)
'Author: %aN %aE%Cred' # blue author mail
'AuthorDate: %ad%Cblue' # red author date
'Commit: %cN %cE%Cred' # blue commiter mail
'CommitDate: %cd%Creset%C(bold)' # red commit date
''
' %s%Creset' # bold white subject
' ' # space is here so that the empty line is not eaten when empty body
'%-b' # body
'--------------------------------------------------'
''
)
# Before being able to operate on the string itself we need to remove all
# ansi color escape sequences to not confuse sed. (see git show below)
local del_ansi='s/\[[0-9]{0,2}m//g'
# Ignore the graph part at the beginning, then capture the commit hash and
# throw away the rest of the line.
local commit_hash='s/^[ */\\|]*([a-z0-9]*).*$/\1/'
local dateshort='--date=format:%F' # year
local date='--date=format:%F %T %z' # year time timezone
local colors='--color=always'
local -A binds=(
'ctrl-space' 'toggle-preview'
'ctrl-alt-j' 'preview-down'
'ctrl-alt-k' 'preview-up'
)
local -a fzf_args=(
# Understand ansi color escape sequences
"--ansi"
# Expand the binds array in the format key1:value1,key2:value2
"--bind" "${(@kj:,:)binds/(#m)*/$MATCH:$binds[$MATCH]}"
# Execute git show on the commit as preview
"--preview" "
out=\"\$(echo {} | sed -Ee \"$del_ansi\" -e \"$commit_hash\")\"
if [ \"\$out\" ]; then
git show \"${(j:%n:)format}\" \"$date\" $colors \"\$out\"
fi
"
# Reverse the layout so that the newest commit is at the top
"--reverse"
# Do not sort when typing to maintain the sorting by date
"--no-sort"
)
# Display an ascii graph of the commits in the above format and pipe that
# into fzf.
commit="$(\
git log "$formatshort" --graph "$dateshort" "$colors" \
| fzf "${fzf_args[@]}"
)"
# If fzf exits successfully, put the abbreviated commit hash into the
# clipboard and write it into stdout.
if ! (( $? )); then
commit="$(sed -Ee "$del_ansi" -e "$commit_hash" <<<"$commit")"
if command -v xclip &>/dev/null; then
echo -n "$commit" | xclip -selection clip
fi
echo "$commit"
fi
}
git-submodule-rm() {
# Exit if not in git repo
local toplevel="$(git rev-parse --show-toplevel)" || return
# Exit if no arguements were given
[ $# -gt 0 ] || return
local separator=""
for arg in "$@"; do
printf "$separator"
# argument relative from git toplevel
local arg_from_git="${${arg:A}##$toplevel/}"
# argument has to exist
[ -e "$arg" ] || continue
# argument has to exist in repo
[ -e "$toplevel/$arg_from_git" ] || continue
# has to be a submodule
[ -e "$toplevel/.git/modules/$arg_from_git" ] || continue
git submodule deinit -f "$arg"
echo "command rm -rf \"$toplevel/.git/modules/$arg_from_git\""
command rm -rf "$toplevel/.git/modules/$arg_from_git"
git rm -f "$arg"
separator="\n"
done
}
safe-remove() {
[ $# -gt 0 ] || return 1
[ -e "$1" ] || return 1