From 7cc2df531863cabcacc8a9a6611c94c6ca2459b0 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 19 Mar 2022 03:54:51 +0100 Subject: [PATCH] Zsh: Put functions in directory and autoload them --- .config/zsh/.zprofile | 3 +- .config/zsh/.zshrc | 6 +-- .config/zsh/functions.zsh | 70 ------------------------- .config/zsh/functions/cat-highlight | 25 +++++++++ .config/zsh/functions/export-emacs | 18 +++++++ .config/zsh/functions/gdb_get_backtrace | 19 +++++++ .config/zsh/functions/mksmol | 18 +++++++ 7 files changed, 85 insertions(+), 74 deletions(-) delete mode 100644 .config/zsh/functions.zsh create mode 100644 .config/zsh/functions/cat-highlight create mode 100644 .config/zsh/functions/export-emacs create mode 100644 .config/zsh/functions/gdb_get_backtrace create mode 100644 .config/zsh/functions/mksmol diff --git a/.config/zsh/.zprofile b/.config/zsh/.zprofile index 9dc8092..bf5dd01 100644 --- a/.config/zsh/.zprofile +++ b/.config/zsh/.zprofile @@ -131,7 +131,8 @@ case $(hostname) in ;; esac -[[ -f ${ZDOTDIR}/zsecrets ]] && source ${ZDOTDIR}/zsecrets +[[ -f "${ZDOTDIR}"/zsecrets ]] && source "${ZDOTDIR}"/zsecrets +[[ -d "${ZDOTDIR}"/functions ]] && fpath+="${ZDOTDIR}"/functions ############################# Daemons ########################################## diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index e336bdb..e151743 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -181,9 +181,9 @@ esac precmd_functions=(${(u)precmd_functions}) preexec_functions=(${(u)preexec_functions}) -[[ -f ${ZDOTDIR}/zkeys ]] && source ${ZDOTDIR}/zkeys -[[ -f ${ZDOTDIR}/zaliases ]] && source ${ZDOTDIR}/zaliases -[[ -f ${ZDOTDIR}/functions.zsh ]] && source ${ZDOTDIR}/functions.zsh +[[ -f "${ZDOTDIR}"/zkeys ]] && source "${ZDOTDIR}"/zkeys +[[ -f "${ZDOTDIR}"/zaliases ]] && source "${ZDOTDIR}"/zaliases +[[ -d "${ZDOTDIR}"/functions ]] && autoload -Uz "${ZDOTDIR}"/functions/* local DEFAULT_USER="${DEFAULT_USER:-tastytea}" [[ -f ${ZDOTDIR}/tastytea.zsh-theme ]] && source ${ZDOTDIR}/tastytea.zsh-theme diff --git a/.config/zsh/functions.zsh b/.config/zsh/functions.zsh deleted file mode 100644 index 84d5ef5..0000000 --- a/.config/zsh/functions.zsh +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/zsh -# Various small functions - -# Get backtrace from gdb. -function gdb_get_backtrace() -{ - local exe=$1 - local core=$2 - - gdb ${exe} \ - --core ${core} \ - --batch \ - --quiet \ - -ex "thread apply all bt full" \ - -ex "quit" -} - -# Export variable to Emacs. -function export-emacs -{ - if [[ "$(emacsclient -e t)" != 't' ]]; then - return 1 - fi - - for name in "${@}"; do - value=$(eval echo \"\$${name}\") - emacsclient -e "(setenv \"${name}\" \"${value}\")" >/dev/null - done -} - -# Highlight cat output with whatever less is using for highlighting. The command -# in $LESSOPEN needs to be able to work with multiple files. -function cat-highlight() -{ - # Check if we are in a terminal and $LESSOPEN is not empty - if [[ -t 1 && -n "${LESSOPEN}" ]]; then - local -a args - local -a files - for arg in $@; do - # Filter out file names - if [[ "${arg:0:1}" != "-" ]]; then - files+="$arg" - else - args+="$arg" - fi - done - - local catopen="${LESSOPEN/|/}" - eval "${catopen/\%s/${files}} | \cat ${args}" - else - \cat $@ - fi -} - -# Makes all files in DIR lower case. Works with Non-ASCII characters. -# Equivalent to `zmv "${dir}/(*)" '${dir}/${1:l}'` -function mksmol() -{ - if [[ ${ARGC} -ne 1 ]]; then - echo "usage: ${0} DIR" >&2 - return 1 - fi - local dir="${1}" - - for file in "${dir}"/*; do - basename="${file##*/}" - newfile="${dir}/${basename:l}" - [[ "${file}" != "${newfile}" ]] && mv "${file}" "${newfile}" || : - done -} diff --git a/.config/zsh/functions/cat-highlight b/.config/zsh/functions/cat-highlight new file mode 100644 index 0000000..2bd550c --- /dev/null +++ b/.config/zsh/functions/cat-highlight @@ -0,0 +1,25 @@ +# -*- mode: shell-script; -*- + +# Highlight cat output with whatever less is using for highlighting. The command +# in $LESSOPEN needs to be able to work with multiple files. +function cat-highlight() +{ + # Check if we are in a terminal and $LESSOPEN is not empty + if [[ -t 1 && -n "${LESSOPEN}" ]]; then + local -a args + local -a files + for arg in "${@}"; do + # Filter out file names + if [[ "${arg[1]}" != "-" ]]; then + files+="${arg}" + else + args+="${arg}" + fi + done + + local catopen="${LESSOPEN/|/}" + eval "${catopen/\%s/${files}} | \cat ${args}" + else + \cat "${@}" + fi +} diff --git a/.config/zsh/functions/export-emacs b/.config/zsh/functions/export-emacs new file mode 100644 index 0000000..d6c159d --- /dev/null +++ b/.config/zsh/functions/export-emacs @@ -0,0 +1,18 @@ +# -*- mode: shell-script; -*- + +# Export variable to Emacs. +function export-emacs +{ + if [[ ARGC -eq 0 ]]; then + print -u 2 "Usage: ${0} …" + return 1 + fi + + # Fail if we can't communicate with the daemon + [[ "$(emacsclient -e t)" == 't' ]] || return 1 + + for name in "${@}"; do + value=$(eval print \"\$${name}\") + emacsclient -e "(setenv \"${name}\" \"${value}\")" >/dev/null + done +} diff --git a/.config/zsh/functions/gdb_get_backtrace b/.config/zsh/functions/gdb_get_backtrace new file mode 100644 index 0000000..4f733bb --- /dev/null +++ b/.config/zsh/functions/gdb_get_backtrace @@ -0,0 +1,19 @@ +# -*- mode: shell-script; -*- + +# Get backtrace from gdb +function gdb_get_backtrace() +{ + if [[ ${ARGC} -ne 2 ]]; then + print -u 2 "Usage: ${0} " + return 1 + fi + local exe="${1}" + local core="${2}" + + gdb "${exe}" \ + --core "${core}" \ + --batch \ + --quiet \ + -ex "thread apply all bt full" \ + -ex "quit" +} diff --git a/.config/zsh/functions/mksmol b/.config/zsh/functions/mksmol new file mode 100644 index 0000000..14f5939 --- /dev/null +++ b/.config/zsh/functions/mksmol @@ -0,0 +1,18 @@ +# -*- mode: shell-script; -*- + +# Makes all files in lower case. Works with Non-ASCII characters. +# Equivalent to `zmv "${dir}/(*)" '${dir}/${1:l}'` +function mksmol() +{ + if [[ ${ARGC} -ne 1 ]]; then + print -u 2 "Usage: ${0} " >&2 + return 1 + fi + local dir="${1}" + + for file in "${dir}"/*; do + basename="${file##*/}" + newfile="${dir}/${basename:l}" + [[ "${file}" != "${newfile}" ]] && mv "${file}" "${newfile}" || : + done +}