dotfiles/.zshrc

160 lines
5.5 KiB
Bash

#!/bin/zsh
### DEFAULT DIRECTORY STRUCTURE
#
# ~/
# - .zshrc > Initial setup, should not need to be changed
# - .bashrc > Bash setup (empty for my dotfiles)
# - .Xresources > Initial Xresources setup, redirects to .config/.Xresources.d/
# - .tmux > Tmux setup (would love to put this into .config/ )
# - .config/ > all other (program) configuration should go in this dir
# --------- .zshrc.d/ > The actual zsh setup files, see their configuration below
# --------- .Xresources.d/ > The actual Xresources setup files
# --------- .zgen/ > The zsh plugin manager
# - .dotfiles/ > the version control of dotfiles
#
####
## Show debug information
#
# ZSH_SETUP_SHOW_DEBUG="true"
# Uncomment the above line to get debug information during the script setup.
print_dbg() {
if [ -z $ZSH_SETUP_SHOW_DEBUG ]; then
return
fi
printf $@
}
# Clone zgen if you haven't already
check_zgen_installation() {
if [[ -z "$ZGEN_PARENT_DIR" ]]; then
ZGEN_PARENT_DIR=${${XDG_CONFIG_DIR}:="$HOME/.config/"}
ZGEN_DIR="$ZGEN_PARENT_DIR/zgen"
fi
if [[ ! -f $ZGEN_DIR/zgen.zsh ]]; then
if [[ ! -d "$ZGEN_PARENT_DIR" ]]; then
mkdir -p "$ZGEN_PARENT_DIR"
fi
pushd $ZGEN_PARENT_DIR
git clone https://github.com/tarjoilija/zgen.git $ZGEN_DIR
popd
fi
source $ZGEN_DIR/zgen.zsh
unset ZGEN_PARENT_DIR
}
load_config_files() {
if [ -n "$(/bin/ls $1)" ]; then
# load the files in number range provided
GLOB=(${1}/[${2}][0123456789-]*)
for dotfile in $GLOB; do
if [ -r "${dotfile}" ]; then
print_dbg "Loading $dotfile\n"
source "${dotfile}"
fi
done
unset GLOB
fi
}
#### ZSHrc - Setting up the Shell
##
## Almost all actual setup is being done with the help of individual files
## in the .config/zsh directory. This file just loads them. They follow this order:
##
## 0 are bootstrapping the zsh environment, setting aliases and sane
## shell defaults which should need no further changes.
## 1 allow custom bootstrapping of things that need to be set before any
## zsh plugins are loaded (e.g., theme environment vars, etc)
## 2 are run *during* zgen setup and can include plugins to be loaded
## 3 script & plugin configurations without *external* dependencies (i.e. can run with
## only what the dotfiles supply) They are run after plugins are loaded and can be configured
## in whatever way you wish. If you change the plugin setup, you may need to reconfigure here.
## 5 commands *with* external script dependencies (e.g. python, mpv, etc.). Scripts should
## check for their dependencies before enabling their commands. It makes scripts more readable
## to begin with their depency in the name, i.e. '5-python-mpv-spawn-unique-player-with-umpv'
## 6 Aliasing.
## 7,8 No default behavior, use however you like.
## 9 anything which has to run at the very end
##
## The running order can further be specified by having a second number, which will then run through
## them in the numbered order.
##
## Most of them are documented, and they try to have sensible naming.
## If you want to supply your own directory of zsh config files you can do so
## by setting the ZSH_CONFIG_DIR environment variable. You can also drop
## configuration files into the config dir without any numbers prepended, they
## will be run after numbered ones (in a random order) if you uncomment the
## corresponding line below.
# Set ZSH_CONFIG_DIR to default to ~/.config/zsh, or let user override
# Exit early if no configuration directory has been found
if [ ! -d ${ZSH_CONFIG_DIR:="${XDG_CONFIG_DIR:-$HOME/.config}/zsh"} ]; then
# If the user explicitly overrode the path give him an error
if [ ! $ZSH_CONFIG_DIR = "${XDG_CONFIG_DIR:-$HOME/.config/}/zsh" ]; then
printf "\$ZSH_CONFIG_DIR=$ZSH_CONFIG_DIR/ does not exist.\n"
fi
PS1="$HOME $ "
return
fi
if [[ -f $HOME/.profile ]]; then
source $HOME/.profile
fi
# //TODO: Prints error when -no- files are found
# Load bootstrap settings (00-19) before plugin initialization
print_dbg "Starting bootstrap (00-19)...\n"
load_config_files $ZSH_CONFIG_DIR 01
# set up zgen - load external zsh plugins
# If you need to reload your plugins use 'zgen reset' and restart
# your shell
if [ -n "$(/bin/ls $ZSH_CONFIG_DIR/)" ]; then
print_dbg "Starting plugin system...\n" #//DEBUG
check_zgen_installation
if ! zgen saved; then
print_dbg "Applying plugins (20-29)...\n"
load_config_files $ZSH_CONFIG_DIR 2
# Save it all to init script.
zgen save
else
print_dbg "Plugins already applied. If you want to re-apply run 'zgen reset' and open new terminal.\n"
fi
fi
# Load additional settings (30-99) after plugin initialization
print_dbg "Setting up remaining scripts (30-99)\n"
load_config_files $ZSH_CONFIG_DIR 3456789
# Uncomment this section to enable execution of arbitrarily named config files
# if [ -n "$(/bin/ls $ZSH_CONFIG_DIR/)" ]; then
# GLOB=($ZSH_CONFIG_DIR/[:alpha:]*)
# for dotfile in $GLOB; do
# print_dbg "Alphabetic: $dotfile\n" #//DEBUG
# if [ -r "${dotfile}" ]; then
# source "${dotfile}"
# fi
# done
# unset GLOB
# fi
# put personal scripts on the PATH to be callable
export PATH="$(du $ZSH_CONFIG_DIR/scripts/ | cut -f2 | tr '\n' ':')$PATH"
unset ZSH_CONFIG_DIR
# unset ZGEN_DIR -- Needs to remain set for some plugins to function
unset ZGEN_PARENT_DIR
unset ZSH_SETUP_SHOW_DEBUG
unfunction print_dbg
unfunction load_config_files
unfunction check_zgen_installation
# //FIXME this needs to get the hell out of here -> put it in an env loading file
export GOPATH="$HOME/Code/go"
export PATH="$PATH:$GOPATH/bin"