1
0
Fork 0

Zsh: Put functions in directory and autoload them

This commit is contained in:
tastytea 2022-03-19 03:54:51 +01:00
parent 512682aa01
commit 7cc2df5318
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
7 changed files with 85 additions and 74 deletions

View File

@ -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 ##########################################

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -0,0 +1,18 @@
# -*- mode: shell-script; -*-
# Export variable to Emacs.
function export-emacs
{
if [[ ARGC -eq 0 ]]; then
print -u 2 "Usage: ${0} <variable> …"
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
}

View File

@ -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} <executable> <core dump>"
return 1
fi
local exe="${1}"
local core="${2}"
gdb "${exe}" \
--core "${core}" \
--batch \
--quiet \
-ex "thread apply all bt full" \
-ex "quit"
}

View File

@ -0,0 +1,18 @@
# -*- mode: shell-script; -*-
# Makes all files in <directory> 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} <directory>" >&2
return 1
fi
local dir="${1}"
for file in "${dir}"/*; do
basename="${file##*/}"
newfile="${dir}/${basename:l}"
[[ "${file}" != "${newfile}" ]] && mv "${file}" "${newfile}" || :
done
}