From 7b9400b9453a63477d17ad212afba5b5644ae6d3 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 18 Oct 2024 19:43:11 +0200 Subject: refactor(modules/legacy/conf/zsh): Parameterize and move to new `by-name` --- modules/by-name/zs/zsh/config/command_not_found.sh | 64 +++++ .../zs/zsh/config/command_not_found_insult.sh | 298 +++++++++++++++++++++ modules/by-name/zs/zsh/config/custom_cursor.zsh | 42 +++ modules/by-name/zs/zsh/config/zsh-init.zsh | 42 +++ 4 files changed, 446 insertions(+) create mode 100644 modules/by-name/zs/zsh/config/command_not_found.sh create mode 100644 modules/by-name/zs/zsh/config/command_not_found_insult.sh create mode 100644 modules/by-name/zs/zsh/config/custom_cursor.zsh create mode 100644 modules/by-name/zs/zsh/config/zsh-init.zsh (limited to 'modules/by-name/zs/zsh/config') diff --git a/modules/by-name/zs/zsh/config/command_not_found.sh b/modules/by-name/zs/zsh/config/command_not_found.sh new file mode 100644 index 00000000..fb21b676 --- /dev/null +++ b/modules/by-name/zs/zsh/config/command_not_found.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env dash + +# This was taken from the +# `${pkgs.nix-index}/etc/profile.d/command-not-found.sh` file on 2024-02-28 + +# for bash 4 +# this will be called when a command is entered +# but not found in the user’s path + environment +command_not_found_handle() { + # taken from http://www.linuxjournal.com/content/bash-command-not-found + # - do not run when inside Midnight Commander or within a Pipe + if [ -n "${MC_SID-}" ] || ! [ -t 1 ]; then + >&2 echo "$1: command not found" + return 127 + fi + + toplevel=nixpkgs # nixpkgs should always be available even in NixOS + cmd="$1" + attrs=$(nix-locate --minimal --no-group --type x --type s --top-level --whole-name --at-root "/bin/$cmd") + len=$(if [ -n "$attrs" ]; then echo "$attrs" | wc -l; else echo 0; fi) + + case "$len" in + 0) + eprintln "$cmd: command not found" + ;; + 1) + # If only one package provides this, then we can invoke it + # without asking the user. + + # These will not return 127 if they worked correctly. + + >&2 cat <&2 cat <&2 cat <&2 + fi +} + +function_exists() { + # Zsh returns 0 even on non existing functions with -F so use -f + declare -f "$1" >/dev/null + return $? +} + +# +# The idea below is to copy any existing handlers to another function +# name and insert the message in front of the old handler in the +# new handler. By default, neither bash or zsh has has a handler function +# defined, so the default behaviour is replicated. +# +# Also, ensure the handler is only copied once. If we do not ensure this +# the handler would add itself recursively if this file happens to be +# sourced multiple times in the same shell, resulting in a neverending +# stream of messages. +# + +# +# Zsh +# +if function_exists command_not_found_handler; then + if ! function_exists orig_command_not_found_handler; then + eval "orig_$(declare -f command_not_found_handler)" + fi +else + orig_command_not_found_handler() { + printf "zsh: command not found: %s\n" "$1" >&2 + return 127 + } +fi + +command_not_found_handler() { + print_message + orig_command_not_found_handler "$@" +} + +# +# Bash +# +if function_exists command_not_found_handle; then + if ! function_exists orig_command_not_found_handle; then + eval "orig_$(declare -f command_not_found_handle)" + fi +else + orig_command_not_found_handle() { + printf "%s: %s: command not found\n" "$0" "$1" >&2 + return 127 + } +fi + +command_not_found_handle() { + print_message + orig_command_not_found_handle "$@" +} diff --git a/modules/by-name/zs/zsh/config/custom_cursor.zsh b/modules/by-name/zs/zsh/config/custom_cursor.zsh new file mode 100644 index 00000000..37390c1c --- /dev/null +++ b/modules/by-name/zs/zsh/config/custom_cursor.zsh @@ -0,0 +1,42 @@ +#!/usr/bin/env zsh + +# Change cursor shape for different vi modes. +function zle-keymap-select { + if [[ ${KEYMAP} == vicmd ]] || + [[ $1 = 'block' ]]; then + echo -ne '\e[1 q' + elif [[ ${KEYMAP} == main ]] || + [[ ${KEYMAP} == viins ]] || + [[ ${KEYMAP} = '' ]] || + [[ $1 = 'beam' ]]; then + echo -ne '\e[5 q' + fi +} +zle -N zle-keymap-select + +# ci", ci', ci`, di", etc +autoload -U select-quoted +zle -N select-quoted +for m in visual viopp; do + for c in {a,i}{\',\",\`}; do + bindkey -M "$m" "$c" select-quoted + done +done + +# ci{, ci(, ci<, di{, etc +autoload -U select-bracketed +zle -N select-bracketed +for m in visual viopp; do + for c in {a,i}${(s..)^:-'()[]{}<>bB'}; do + bindkey -M $m $c select-bracketed + done +done + +zle-line-init() { + zle -K viins # initiate `vi insert` as keymap (can be removed if `bindkey -V` has been set elsewhere) + echo -ne "\e[5 q" +} +zle -N zle-line-init + +echo -ne '\e[5 q' # Use beam shape cursor on startup. +precmd() { echo -ne '\e[5 q' ;} # Use beam shape cursor for each new prompt. diff --git a/modules/by-name/zs/zsh/config/zsh-init.zsh b/modules/by-name/zs/zsh/config/zsh-init.zsh new file mode 100644 index 00000000..cd8d34a9 --- /dev/null +++ b/modules/by-name/zs/zsh/config/zsh-init.zsh @@ -0,0 +1,42 @@ +#!/usr/bin/env zsh +# If not running interactively, don't do anything +[[ $- != *i* ]] && return + +# Flex on the ubuntu users +#[ "$NVIM" ] || hyfetch +[ "$NVIM" ] || task next +#loginctl show-session $XDG_SESSION_ID + +## Enable colors and change prompt: +#autoload -Uz colors && colors +#autoload -Uz compinit && compinit -u +## Edit line in vim buffer ctrl-v +autoload -Uz edit-command-line +zle -N edit-command-line +## Enter vim buffer from normal mode +#autoload -Uz edit-command-line && zle -N edit-command-line +bindkey "^V" edit-command-line + +## zstyles +#zstyle ':completion:*' menu select +## Auto complete with case insensitivity +#zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' + +#zmodload zsh/complist +#fpath+=/home/dt/.config/zsh/comp +#compinit +#_comp_options+=(globdots) # Include hidden files. +# +## Source configs +#source "${ZDOTDIR}/ali.sh" +#source "${ZDOTDIR}/prompt.sh" +#source "${ZDOTDIR}/hotkeys.sh" +#source "./${path_custom_cursor}" +#source ~/.local/lib/shell/lib +# +## Load zsh-syntax-highlighting +#source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh +## Suggest aliases for commands +#source /usr/share/zsh/plugins/zsh-you-should-use/you-should-use.plugin.zsh +# +##eval "$(lua ~/scripts/z.lua --init zsh enhanced)" -- cgit 1.4.1