aboutsummaryrefslogtreecommitdiffstats
path: root/.config/zsh/functions
diff options
context:
space:
mode:
authorDavid Runge <dave@sleepmap.de>2021-09-28 22:50:56 +0200
committerDavid Runge <dave@sleepmap.de>2021-09-28 22:50:56 +0200
commit3ac8e0f994839f3c972033bcb12421081ebeb683 (patch)
tree2a4f05cac56559686ea5d7aee8703e7511662c47 /.config/zsh/functions
parenta640bb766699706b4975416b85acd30191b79fa3 (diff)
downloaddotfiles-3ac8e0f994839f3c972033bcb12421081ebeb683.tar.gz
dotfiles-3ac8e0f994839f3c972033bcb12421081ebeb683.tar.bz2
dotfiles-3ac8e0f994839f3c972033bcb12421081ebeb683.tar.xz
dotfiles-3ac8e0f994839f3c972033bcb12421081ebeb683.zip
zsh: move includes to XDG compliant locations
.config/zsh/{functions,includes}/*: Move functions and includes to XDG compliant locations. .zshrc: Include functions and other includes from XDG compliant locations. Remove use of prepend-sudo function.
Diffstat (limited to '.config/zsh/functions')
-rw-r--r--.config/zsh/functions/01_editor.zsh124
-rw-r--r--.config/zsh/functions/02_utility.zsh75
-rw-r--r--.config/zsh/functions/0_terminal.zsh74
-rw-r--r--.config/zsh/functions/overrides.zsh4
-rw-r--r--.config/zsh/functions/packaging.zsh133
5 files changed, 410 insertions, 0 deletions
diff --git a/.config/zsh/functions/01_editor.zsh b/.config/zsh/functions/01_editor.zsh
new file mode 100644
index 0000000..1484a92
--- /dev/null
+++ b/.config/zsh/functions/01_editor.zsh
@@ -0,0 +1,124 @@
+# Exposes information about the Zsh Line Editor via the $editor_info associative
+# array.
+function editor-info {
+ # Clean up previous $editor_info.
+ unset editor_info
+ typeset -gA editor_info
+
+ if [[ "$KEYMAP" == 'vicmd' ]]; then
+ zstyle -s ':editor:info:keymap:alternate' format 'REPLY'
+ editor_info[keymap]="$REPLY"
+ else
+ zstyle -s ':editor:info:keymap:primary' format 'REPLY'
+ editor_info[keymap]="$REPLY"
+
+ if [[ "$ZLE_STATE" == *overwrite* ]]; then
+ zstyle -s ':editor:info:keymap:primary:overwrite' format 'REPLY'
+ editor_info[overwrite]="$REPLY"
+ else
+ zstyle -s ':editor:info:keymap:primary:insert' format 'REPLY'
+ editor_info[overwrite]="$REPLY"
+ fi
+ fi
+
+ unset REPLY
+
+ zle reset-prompt
+ zle -R
+}
+zle -N editor-info
+
+# Updates editor information when the keymap changes.
+function zle-keymap-select {
+ zle editor-info
+}
+zle -N zle-keymap-select
+
+# Enables terminal application mode and updates editor information.
+function zle-line-init {
+ # The terminal must be in application mode when ZLE is active for $terminfo
+ # values to be valid.
+ if (( $+terminfo[smkx] )); then
+ # Enable terminal application mode.
+ echoti smkx
+ fi
+
+ # Update editor information.
+ zle editor-info
+}
+zle -N zle-line-init
+
+# Disables terminal application mode and updates editor information.
+function zle-line-finish {
+ # The terminal must be in application mode when ZLE is active for $terminfo
+ # values to be valid.
+ if (( $+terminfo[rmkx] )); then
+ # Disable terminal application mode.
+ echoti rmkx
+ fi
+
+ # Update editor information.
+ zle editor-info
+}
+zle -N zle-line-finish
+
+# Toggles emacs overwrite mode and updates editor information.
+function overwrite-mode {
+ zle .overwrite-mode
+ zle editor-info
+}
+zle -N overwrite-mode
+
+# Enters vi insert mode and updates editor information.
+function vi-insert {
+ zle .vi-insert
+ zle editor-info
+}
+zle -N vi-insert
+
+# Moves to the first non-blank character then enters vi insert mode and updates
+# editor information.
+function vi-insert-bol {
+ zle .vi-insert-bol
+ zle editor-info
+}
+zle -N vi-insert-bol
+
+# Enters vi replace mode and updates editor information.
+function vi-replace {
+ zle .vi-replace
+ zle editor-info
+}
+zle -N vi-replace
+
+# Expands .... to ../..
+function expand-dot-to-parent-directory-path {
+ if [[ $LBUFFER = *.. ]]; then
+ LBUFFER+='/..'
+ else
+ LBUFFER+='.'
+ fi
+}
+zle -N expand-dot-to-parent-directory-path
+
+# Displays an indicator when completing.
+function expand-or-complete-with-indicator {
+ local indicator
+ zstyle -s ':editor:info:completing' format 'indicator'
+ print -Pn "$indicator"
+ zle expand-or-complete
+ zle redisplay
+}
+zle -N expand-or-complete-with-indicator
+
+# Inserts 'sudo ' at the beginning of the line.
+function prepend-sudo {
+ if [[ "$BUFFER" != su(do|)\ * ]]; then
+ BUFFER="sudo $BUFFER"
+ (( CURSOR += 5 ))
+ fi
+}
+zle -N prepend-sudo
+
+# Reset to default key bindings.
+bindkey -d
diff --git a/.config/zsh/functions/02_utility.zsh b/.config/zsh/functions/02_utility.zsh
new file mode 100644
index 0000000..05b77bc
--- /dev/null
+++ b/.config/zsh/functions/02_utility.zsh
@@ -0,0 +1,75 @@
+# Makes a directory and changes to it.
+function mkdcd {
+ [[ -n "$1" ]] && mkdir -p "$1" && builtin cd "$1"
+}
+
+# Changes to a directory and lists its contents.
+function cdls {
+ builtin cd "$argv[-1]" && ls "${(@)argv[1,-2]}"
+}
+
+# Pushes an entry onto the directory stack and lists its contents.
+function pushdls {
+ builtin pushd "$argv[-1]" && ls "${(@)argv[1,-2]}"
+}
+
+# Pops an entry off the directory stack and lists its contents.
+function popdls {
+ builtin popd "$argv[-1]" && ls "${(@)argv[1,-2]}"
+}
+
+# Prints columns 1 2 3 ... n.
+function slit {
+ awk "{ print ${(j:,:):-\$${^@}} }"
+}
+
+# Finds files and executes a command on them.
+function find-exec {
+ find . -type f -iname "*${1:-}*" -exec "${2:-file}" '{}' \;
+}
+
+# Displays user owned processes status.
+function psu {
+ ps -U "${1:-$LOGNAME}" -o 'pid,%cpu,%mem,command' "${(@)argv[2,-1]}"
+}
+
+# Highlights make output.
+function make {
+ if (( $+commands[colormake] )); then
+ colormake "$@"
+ else
+ command make "$@"
+ fi
+}
+
+# Lists the contents of archives.
+function lsarchive {
+ while (( $# > 0 )); do
+ if [[ ! -s "$1" ]]; then
+ print "$0: file not valid: $1" >&2
+ shift
+ continue
+ fi
+ case "$1:l" in
+ (*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;;
+ (*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;;
+ (*.tar.xz|*.txz) tar --xz --help &> /dev/null \
+ && tar --xz -t${verbose:+v}f "$1" \
+ || xzcat "$1" | tar t${verbose:+v}f - ;;
+ (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
+ && tar --lzma -t${verbose:+v}f "$1" \
+ || lzcat "$1" | tar x${verbose:+v}f - ;;
+ (*.tar) tar t${verbose:+v}f "$1" ;;
+ (*.zip) unzip -l${verbose:+v} "$1" ;;
+ (*.rar) unrar &> /dev/null \
+ && unrar ${${verbose:+v}:-l} "$1" \
+ || rar ${${verbose:+v}:-l} "$1" ;;
+ (*.7z) 7za l "$1" ;;
+ (*)
+ print "$0: cannot list: $1" >&2
+ success=1
+ ;;
+ esac
+ shift
+ done
+}
diff --git a/.config/zsh/functions/0_terminal.zsh b/.config/zsh/functions/0_terminal.zsh
new file mode 100644
index 0000000..85be89a
--- /dev/null
+++ b/.config/zsh/functions/0_terminal.zsh
@@ -0,0 +1,74 @@
+# Terminal functions
+#
+
+# Sets the terminal or terminal multiplexer window title.
+function set-window-title {
+ local title_format{,ted}
+ title_format='%s'
+ zformat -f title_formatted "$title_format" "s:$argv"
+
+ if [[ "$TERM" == screen* ]]; then
+ title_format="\ek%s\e\\"
+ else
+ title_format="\e]2;%s\a"
+ fi
+
+ printf "$title_format" "${(V%)title_formatted}"
+}
+
+# Sets the terminal tab title.
+function set-tab-title {
+ local title_format{,ted}
+ title_format='%s'
+ zformat -f title_formatted "$title_format" "s:$argv"
+
+ printf "\e]1;%s\a" ${(V%)title_formatted}
+}
+
+
+# Sets the tab and window titles with a given command.
+function _terminal-set-titles-with-command {
+ emulate -L zsh
+ setopt EXTENDED_GLOB
+
+ # Get the command name that is under job control.
+ if [[ "${2[(w)1]}" == (fg|%*)(\;|) ]]; then
+ # Get the job name, and, if missing, set it to the default %+.
+ local job_name="${${2[(wr)%*(\;|)]}:-%+}"
+
+ # Make a local copy for use in the subshell.
+ local -A jobtexts_from_parent_shell
+ jobtexts_from_parent_shell=(${(kv)jobtexts})
+
+ jobs "$job_name" 2>/dev/null > >(
+ read index discarded
+ # The index is already surrounded by brackets: [1].
+ _terminal-set-titles-with-command "${(e):-\$jobtexts_from_parent_shell$index}"
+ )
+ else
+ # Set the command name, or in the case of sudo or ssh, the next command.
+ local cmd="${${2[(wr)^(*=*|sudo|ssh|-*)]}:t}"
+ local truncated_cmd="${cmd/(#m)?(#c15,)/${MATCH[1,12]}...}"
+ unset MATCH
+
+ set-window-title "$cmd"
+ set-tab-title "$truncated_cmd"
+ fi
+}
+
+
+# Sets the tab and window titles with a given path.
+function _terminal-set-titles-with-path {
+ emulate -L zsh
+ setopt EXTENDED_GLOB
+
+ local absolute_path="${${1:a}:-$PWD}"
+ local abbreviated_path="${absolute_path/#$HOME/~}"
+ local truncated_path="${abbreviated_path/(#m)?(#c15,)/...${MATCH[-12,-1]}}"
+ unset MATCH
+
+ set-window-title "$abbreviated_path"
+ set-tab-title "$truncated_path"
+}
+
+
diff --git a/.config/zsh/functions/overrides.zsh b/.config/zsh/functions/overrides.zsh
new file mode 100644
index 0000000..b320671
--- /dev/null
+++ b/.config/zsh/functions/overrides.zsh
@@ -0,0 +1,4 @@
+# make git auto completion faster by favoring local files
+__git_files () {
+ _wanted files expl 'local files' _files
+}
diff --git a/.config/zsh/functions/packaging.zsh b/.config/zsh/functions/packaging.zsh
new file mode 100644
index 0000000..69d6df4
--- /dev/null
+++ b/.config/zsh/functions/packaging.zsh
@@ -0,0 +1,133 @@
+#!/usr/bin/env zsh
+
+pkg_sign() {
+ # sign one or more packages with the key setup in makepkg.conf
+ local _pkg_list=()
+ local _arg _pkg
+
+ # get GPGKEY from makepkg.conf
+ if [[ -f /etc/makepkg.conf ]]; then
+ source /etc/makepkg.conf 2>/dev/null 1>&2
+ fi
+ if [[ -f "${HOME}/.makepkg.conf" ]]; then
+ source "${HOME}/.makepkg.conf" 2>/dev/null 1>&2
+ fi
+ if [[ -z "${GPGKEY}" ]]; then
+ 1>&2 printf "No GPGKEY is setup in makepkg.conf!\n"
+ return 1
+ fi
+
+ # check whether file(s) exists or globs exist
+ for _arg in "$@"; do
+ if [[ -f "${_arg}" ]] && [[ "${_arg}" == *.pkg.tar.zst ]]; then
+ _pkg_list+=( "${_arg}" )
+ fi
+ if [[ -n *"${_arg}"*.pkg.tar.zst(#qN) ]]; then
+ _pkg_list+=( *"${_arg}"*.pkg.tar.zst )
+ fi
+ done
+
+ # sign package(s)
+ if (( ${#_pkg_list} > 0 )); then
+ for _pkg in "${_pkg_list[@]}"; do
+ gpg --detach-sign --local-user "${GPGKEY}" "$_pkg"
+ done
+ return 0
+ else
+ 1>&2 printf "No package can be found for signing!\n"
+ return 1
+ fi
+}
+
+pkg_add() {
+ # sign one or more packages with the key setup in makepkg.conf
+ local _pkg_list=()
+ local _arg
+
+ # get GPGKEY from makepkg.conf
+ if [[ -f /etc/makepkg.conf ]]; then
+ source /etc/makepkg.conf 2>/dev/null 1>&2
+ fi
+ if [[ -f "${HOME}/.makepkg.conf" ]]; then
+ source "${HOME}/.makepkg.conf" 2>/dev/null 1>&2
+ fi
+ if [[ -z "${GPGKEY}" ]]; then
+ 1>&2 printf "No GPGKEY is setup in makepkg.conf!\n"
+ return 1
+ fi
+
+ # check whether file(s) exists or globs exist
+ for _arg in "$@"; do
+ if [[ -f "${_arg}" ]] && [[ "$_arg" == *.pkg.tar.zst ]]; then
+ _pkg_list+=( "${_arg}" )
+ fi
+ if [[ -n *"${_arg}"*.pkg.tar.zst(#qN) ]]; then
+ _pkg_list+=( *"${_arg}"*.pkg.tar.zst )
+ fi
+ done
+
+ # add package(s) to repo database
+ if (( ${#_pkg_list} > 0 )); then
+ repo-add -R -s -k "${GPGKEY}" "$(dirname "${_pkg_list[1]}")"/*.db.tar.gz "${_pkg_list[@]}"
+ return 0
+ else
+ 1>&2 printf "No packages to add can be found!\n"
+ return 1
+ fi
+}
+
+sshfs_mount() {
+ mkdir -p "${HOME}/mounts/$1"
+ sshfs -C -F "${HOME}/.ssh/config" "$1": "${HOME}/mounts/$1"
+}
+
+sshfs_umount() {
+ fusermount3 -u "${HOME}/mounts/$1"
+}
+
+nvc() {
+ local config
+ if [[ -z "$1" ]]; then
+ 1>&2 printf "A repository name needs to be specified as the first argument.\n"
+ return 1
+ fi
+ config="${HOME}/.config/nvchecker/$1.toml"
+ if [[ ! -f "${config}" ]]; then
+ 1>&2 printf "The configuration does not exist: %s\n" "${config}"
+ return 1
+ fi
+ if ! command -v nvchecker > /dev/null; then
+ 1>&2 printf "The required application 'nvchecker' can not be found.\n"
+ return 1
+ fi
+ nvchecker -c "${config}"
+}
+
+nvt() {
+ local config package
+ if [[ -z "$1" ]]; then
+ 1>&2 printf "A repository name needs to be specified as the first argument.\n"
+ return 1
+ fi
+ config="${HOME}/.config/nvchecker/$1.toml"
+ if [[ ! -f "${config}" ]]; then
+ 1>&2 printf "The configuration does not exist: %s\n" "${config}"
+ return 1
+ fi
+
+ if [[ -z "$2" ]]; then
+ 1>&2 printf "A package name needs to be specified as the second argument.\n"
+ return 1
+ fi
+ package="${2}"
+ if ! grep "${package}" "${config}" > /dev/null; then
+ 1>&2 printf "The package %s can not be found in the configuration: %s\n" "${package}" "${config}"
+ return 1
+ fi
+
+ if ! command -v nvtake > /dev/null; then
+ 1>&2 printf "The required application 'nvtake' can not be found.\n"
+ return 1
+ fi
+ nvtake -c "${config}" "${package}"
+}