diff --git a/.rc.d/fortune.sh b/.rc.d/fortune.sh index 2c63284..a29095d 100755 --- a/.rc.d/fortune.sh +++ b/.rc.d/fortune.sh @@ -1,9 +1,16 @@ -# Display fortune (with cowsay if available) in interactive shells +# Display fortune (with cowsay if available) in top-level interactive shells +# (new iTerm2 tabs/windows, xterm, console, VS Code / IntelliJ terminals). +# Subshells (e.g. typing 'bash' or 'zsh' inside an existing terminal) are skipped. case $- in *i*) ;; *) return 0 2>/dev/null || exit 0 ;; esac +# Skip nested subshells (SHLVL > 1) +if [ "${SHLVL:-1}" -gt 1 ]; then + return 0 2>/dev/null || exit 0 +fi + _fortune_cmd="" if command -v fortune >/dev/null 2>&1; then _fortune_cmd="fortune" diff --git a/.rc.d/python.sh b/.rc.d/python.sh.disabled similarity index 100% rename from .rc.d/python.sh rename to .rc.d/python.sh.disabled diff --git a/.rc.d/qb64.sh b/.rc.d/qb64.sh.disabled similarity index 100% rename from .rc.d/qb64.sh rename to .rc.d/qb64.sh.disabled diff --git a/.rc.d/zoxide.sh b/.rc.d/zoxide.sh index 646a34c..1e9d079 100755 --- a/.rc.d/zoxide.sh +++ b/.rc.d/zoxide.sh @@ -1,12 +1,36 @@ -# TODO: Check if zoxide_path exists. -# TODO: Check shell better. -# -n $zoxide_path -a -n $zoxide_path -function anon() { - zoxide_path=$(which zoxide) - if [ -n $zoxide_path -a $zoxide_path != "zoxide not found" ]; then - if [ "$SHELL" = "/bin/zsh" ]; then - eval "$(zoxide init --cmd cd zsh)" +# Setup zoxide (smarter cd command) for interactive shells (bash and zsh) +_setup_zoxide() { + # Only run in interactive shells + case $- in + *i*) ;; + *) return 0 ;; + esac + + # Ensure zoxide binary is accessible if installed in common locations + if ! command -v zoxide >/dev/null 2>&1; then + for _zoxide_bin_dir in \ + "/opt/homebrew/bin" \ + "/usr/local/bin" \ + "$HOME/.local/bin" \ + "$HOME/bin" + do + if [ -x "$_zoxide_bin_dir/zoxide" ]; then + case ":$PATH:" in + *":$_zoxide_bin_dir:"*) ;; + *) PATH="$_zoxide_bin_dir:$PATH"; export PATH ;; + esac + break + fi + done + fi + + command -v zoxide >/dev/null 2>&1 || return 0 + + if [ -n "$ZSH_VERSION" ]; then + eval "$(zoxide init --cmd cd zsh)" + elif [ -n "$BASH_VERSION" ]; then + eval "$(zoxide init --cmd cd bash)" fi - fi } -unset -f anon +_setup_zoxide +unset -f _setup_zoxide diff --git a/.rc.d/zsh-completions.sh b/.rc.d/zsh-completions.sh deleted file mode 100755 index 37f57f1..0000000 --- a/.rc.d/zsh-completions.sh +++ /dev/null @@ -1,10 +0,0 @@ -if type brew &>/dev/null; then - fpath=( $(brew --prefix)/share/zsh-completions "${fpath[@]}" ) -fi - -# If you receive "zsh compinit: insecure directories" warnings when attempting -# to load these completions, you may need to run these commands: -# chmod go-w '/opt/homebrew/share' -# chmod -R go-w '/opt/homebrew/share/zsh' - -# compinit will load and run after this in .zshrc diff --git a/.rc.d/zz-completions.sh b/.rc.d/zz-completions.sh new file mode 100644 index 0000000..f47703f --- /dev/null +++ b/.rc.d/zz-completions.sh @@ -0,0 +1,70 @@ +# Setup shell completions for Bash and Zsh +# Named with 'zz-' prefix to guarantee late execution after all PATH and tool +# additions in ~/.rc.d/*.sh have completed. + +_setup_completions() { + # Only initialize completions for interactive shells + case $- in + *i*) ;; + *) return 0 ;; + esac + + # ------------------------------------------------------------------------- + # ZSH Autocompletion Setup + # ------------------------------------------------------------------------- + if [ -n "$ZSH_VERSION" ]; then + # Candidate completion directories + for _dir in \ + "/opt/homebrew/share/zsh-completions" \ + "/usr/local/share/zsh-completions" \ + "/usr/share/zsh-completions" \ + "/opt/homebrew/share/zsh/site-functions" \ + "/usr/local/share/zsh/site-functions" \ + "$HOME/.zsh_functions" + do + if [ -d "$_dir" ]; then + case " ${fpath[*]} " in + *" $_dir "*) ;; + *) fpath=("$_dir" "${fpath[@]}") ;; + esac + fi + done + + # If you receive "zsh compinit: insecure directories" warnings, run: + # chmod go-w '/opt/homebrew/share' + # chmod -R go-w '/opt/homebrew/share/zsh' + + # Initialize compinit with 24-hour cache check for fast startup (~2ms) + autoload -Uz compinit + _zcompdump="${ZDOTDIR:-$HOME}/.zcompdump" + if [ -n "$(find "$_zcompdump" -mtime -1 2>/dev/null)" ]; then + compinit -C -d "$_zcompdump" + else + compinit -d "$_zcompdump" + fi + unset _zcompdump _dir + + # ------------------------------------------------------------------------- + # Bash Autocompletion Setup + # ------------------------------------------------------------------------- + elif [ -n "$BASH_VERSION" ]; then + if ! shopt -oq posix; then + for _comp in \ + "/opt/homebrew/etc/profile.d/bash_completion.sh" \ + "/usr/local/etc/profile.d/bash_completion.sh" \ + "/usr/share/bash-completion/bash_completion" \ + "/etc/bash_completion" \ + "$HOME/.local/share/bash-completion/bash_completion" + do + if [ -r "$_comp" ]; then + \. "$_comp" + break + fi + done + unset _comp + fi + fi +} +_setup_completions +unset -f _setup_completions +