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` --- flake/nixosConfigurations/common.nix | 1 + 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 +++ modules/by-name/zs/zsh/module.nix | 94 +++++++ .../conf/zsh/config/command_not_found.sh | 64 ----- .../conf/zsh/config/command_not_found_insult.sh | 298 --------------------- .../home.legacy/conf/zsh/config/custom_cursor.zsh | 42 --- modules/home.legacy/conf/zsh/config/zsh-init.zsh | 42 --- modules/home.legacy/conf/zsh/default.nix | 84 ------ 11 files changed, 541 insertions(+), 530 deletions(-) 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 create mode 100644 modules/by-name/zs/zsh/module.nix delete mode 100644 modules/home.legacy/conf/zsh/config/command_not_found.sh delete mode 100644 modules/home.legacy/conf/zsh/config/command_not_found_insult.sh delete mode 100644 modules/home.legacy/conf/zsh/config/custom_cursor.zsh delete mode 100644 modules/home.legacy/conf/zsh/config/zsh-init.zsh delete mode 100644 modules/home.legacy/conf/zsh/default.nix diff --git a/flake/nixosConfigurations/common.nix b/flake/nixosConfigurations/common.nix index fce2c6a1..7e80980d 100644 --- a/flake/nixosConfigurations/common.nix +++ b/flake/nixosConfigurations/common.nix @@ -34,6 +34,7 @@ programs = { imv.enable = true; zathura.enable = true; + zsh.enable = true; }; sound.enable = true; 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)" diff --git a/modules/by-name/zs/zsh/module.nix b/modules/by-name/zs/zsh/module.nix new file mode 100644 index 00000000..98e0d28a --- /dev/null +++ b/modules/by-name/zs/zsh/module.nix @@ -0,0 +1,94 @@ +{ + config, + pkgs, + lib, + shell_library, + system, + ... +}: let + cfg = config.soispha.programs.zsh; + homeConfig = config.home-manager.users.soispha; +in { + options.soispha.programs.zsh = { + enable = lib.mkEnableOption "zsh"; + }; + + config.home-manager.users.soispha = lib.mkIf cfg.enable { + home.sessionPath = []; + + programs.zsh = { + enable = true; + enableCompletion = true; + autosuggestion.enable = true; + syntaxHighlighting.enable = true; + + autocd = true; + + # Must be relative to the users home directory (for whatever reason) + # Thus no `${homeConfig.xdg.configHome}` + dotDir = ".config/zsh"; + + history = { + extended = true; + ignoreDups = false; + expireDuplicatesFirst = false; + ignoreSpace = false; + + path = "${homeConfig.xdg.dataHome}/zsh/history"; + save = 9000000; # number of lines to save + size = 9000000; # number of lines to keep + share = false; # share between sessions + }; + historySubstringSearch = { + enable = true; + searchDownKey = "^[[B"; # DOWN Arrow key + searchUpKey = "^[[A"; # UP Arrow key + }; + + loginExtra = + "setopt " # The extra space is important + + lib.concatStringsSep "\nsetopt " [ + "AUTO_CD" + "AUTO_PUSHD" + "CHASE_DOTS" + + "ALWAYS_TO_END" + + "EXTENDED_HISTORY" + "HIST_ALLOW_CLOBBER" + "HIST_VERIFY" + "HIST_FCNTL_LOCK" + "APPEND_HISTORY" + + "DVORAK" + "CORRECT" + + "PROMPT_SUBST" + "TRANSIENT_RPROMPT" # maybe? + + "COMBINING_CHARS" + "VI" + ]; + + initExtraFirst = + builtins.readFile ./config/zsh-init.zsh + + '' + SHELL_LIBRARY_VERSION="2.1.2" source ${shell_library.rawLib.${system}} + # This next line buffers the first line of the following item: + + '' + # NOTE: This must be before the insult, as we otherwise override the previous handler <2024-02-28> + + builtins.readFile ./config/command_not_found.sh + + builtins.readFile ./config/command_not_found_insult.sh + + builtins.readFile ./config/custom_cursor.zsh + + builtins.readFile "${pkgs.fzf}/share/fzf/key-bindings.zsh"; + + shellAliases = { + ll = ". ll"; + lm = ". lm"; + + hisea = "history 0 | grep"; + }; + }; + }; +} diff --git a/modules/home.legacy/conf/zsh/config/command_not_found.sh b/modules/home.legacy/conf/zsh/config/command_not_found.sh deleted file mode 100644 index fb21b676..00000000 --- a/modules/home.legacy/conf/zsh/config/command_not_found.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/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/home.legacy/conf/zsh/config/custom_cursor.zsh b/modules/home.legacy/conf/zsh/config/custom_cursor.zsh deleted file mode 100644 index 37390c1c..00000000 --- a/modules/home.legacy/conf/zsh/config/custom_cursor.zsh +++ /dev/null @@ -1,42 +0,0 @@ -#!/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/home.legacy/conf/zsh/config/zsh-init.zsh b/modules/home.legacy/conf/zsh/config/zsh-init.zsh deleted file mode 100644 index cd8d34a9..00000000 --- a/modules/home.legacy/conf/zsh/config/zsh-init.zsh +++ /dev/null @@ -1,42 +0,0 @@ -#!/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)" diff --git a/modules/home.legacy/conf/zsh/default.nix b/modules/home.legacy/conf/zsh/default.nix deleted file mode 100644 index 2af7cdbe..00000000 --- a/modules/home.legacy/conf/zsh/default.nix +++ /dev/null @@ -1,84 +0,0 @@ -{ - config, - pkgs, - lib, - shell_library, - system, - ... -}: { - home.sessionPath = []; - programs.zsh = { - enable = true; - autosuggestion.enable = true; - enableCompletion = true; - syntaxHighlighting.enable = true; - - autocd = true; - - # Must be relative to the users home directory (for whatever reason) - # Thus no `${config.xdg.configHome}` - dotDir = ".config/zsh"; - - history = { - extended = true; - ignoreDups = false; - expireDuplicatesFirst = false; - ignoreSpace = false; - - path = "${config.xdg.dataHome}/zsh/history"; - save = 9000000; # number of lines to save - size = 9000000; # number of lines to keep - share = false; # share between sessions - }; - historySubstringSearch = { - enable = true; - searchDownKey = "^[[B"; # DOWN Arrow key - searchUpKey = "^[[A"; # UP Arrow key - }; - - loginExtra = - "setopt " # The extra space is important - + lib.concatStringsSep "\nsetopt " [ - "AUTO_CD" - "AUTO_PUSHD" - "CHASE_DOTS" - - "ALWAYS_TO_END" - - "EXTENDED_HISTORY" - "HIST_ALLOW_CLOBBER" - "HIST_VERIFY" - "HIST_FCNTL_LOCK" - "APPEND_HISTORY" - - "DVORAK" - "CORRECT" - - "PROMPT_SUBST" - "TRANSIENT_RPROMPT" # maybe? - - "COMBINING_CHARS" - "VI" - ]; - - initExtraFirst = - builtins.readFile ./config/zsh-init.zsh - + '' - SHELL_LIBRARY_VERSION="2.1.2" source ${shell_library.rawLib.${system}} - # This next line buffers the first line of the following item: - - '' - # NOTE: This must be before the insult, as we otherwise override the previous handler <2024-02-28> - + builtins.readFile ./config/command_not_found.sh - + builtins.readFile ./config/command_not_found_insult.sh - + builtins.readFile ./config/custom_cursor.zsh - + builtins.readFile "${pkgs.fzf}/share/fzf/key-bindings.zsh"; - - shellAliases = { - ll = ". ll"; - lm = ". lm"; - - hisea = "history 0 | grep"; - }; - }; -} -- cgit 1.4.1