From 67fc567939eec10fcea47cd3569d1682698a5724 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Mon, 21 Oct 2024 22:39:32 +0200 Subject: feat(modules/zsh): Nearly completely rewrite New features: - The `vi` mode is now actually useful - The whole history search/suggestion has been integrated into `atuin` - The `edit-command-line` plugin does no longer print useless stuff - and miscellaneous other things. --- .../config/command_not_found/command_not_found.sh | 64 +++++ .../command_not_found/command_not_found_insult.sh | 298 +++++++++++++++++++++ 2 files changed, 362 insertions(+) create mode 100644 modules/by-name/zs/zsh/config/command_not_found/command_not_found.sh create mode 100644 modules/by-name/zs/zsh/config/command_not_found/command_not_found_insult.sh (limited to 'modules/by-name/zs/zsh/config/command_not_found') diff --git a/modules/by-name/zs/zsh/config/command_not_found/command_not_found.sh b/modules/by-name/zs/zsh/config/command_not_found/command_not_found.sh new file mode 100644 index 00000000..fb21b676 --- /dev/null +++ b/modules/by-name/zs/zsh/config/command_not_found/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 "$@" +} -- cgit 1.4.1