From 0a23fd605c92b0f1ba8a4ab8f95f2d6f02ec5098 Mon Sep 17 00:00:00 2001 From: Guanran Wang Date: Sat, 14 Oct 2023 17:39:45 +0800 Subject: [PATCH] home,zsh,sudo: use fetchers --- .../dotfiles/config/zsh/sudo/sudo.plugin.zsh | 109 ------------------ .../guanranwang/home-manager/common/home.nix | 11 +- 2 files changed, 10 insertions(+), 110 deletions(-) delete mode 100644 users/guanranwang/home-manager/common/dotfiles/config/zsh/sudo/sudo.plugin.zsh diff --git a/users/guanranwang/home-manager/common/dotfiles/config/zsh/sudo/sudo.plugin.zsh b/users/guanranwang/home-manager/common/dotfiles/config/zsh/sudo/sudo.plugin.zsh deleted file mode 100644 index d36426b..0000000 --- a/users/guanranwang/home-manager/common/dotfiles/config/zsh/sudo/sudo.plugin.zsh +++ /dev/null @@ -1,109 +0,0 @@ -# ------------------------------------------------------------------------------ -# Description -# ----------- -# -# sudo or sudo -e (replacement for sudoedit) will be inserted before the command -# -# ------------------------------------------------------------------------------ -# Authors -# ------- -# -# * Dongweiming -# * Subhaditya Nath -# * Marc Cornellà -# * Carlo Sala -# -# ------------------------------------------------------------------------------ - -__sudo-replace-buffer() { - local old=$1 new=$2 space=${2:+ } - - # if the cursor is positioned in the $old part of the text, make - # the substitution and leave the cursor after the $new text - if [[ $CURSOR -le ${#old} ]]; then - BUFFER="${new}${space}${BUFFER#$old }" - CURSOR=${#new} - # otherwise just replace $old with $new in the text before the cursor - else - LBUFFER="${new}${space}${LBUFFER#$old }" - fi -} - -sudo-command-line() { - # If line is empty, get the last run command from history - [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)" - - # Save beginning space - local WHITESPACE="" - if [[ ${LBUFFER:0:1} = " " ]]; then - WHITESPACE=" " - LBUFFER="${LBUFFER:1}" - fi - - { - # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR - # Else use the default $EDITOR - local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}} - - # If $EDITOR is not set, just toggle the sudo prefix on and off - if [[ -z "$EDITOR" ]]; then - case "$BUFFER" in - sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;; - sudo\ *) __sudo-replace-buffer "sudo" "" ;; - *) LBUFFER="sudo $LBUFFER" ;; - esac - return - fi - - # Check if the typed command is really an alias to $EDITOR - - # Get the first part of the typed command - local cmd="${${(Az)BUFFER}[1]}" - # Get the first part of the alias of the same name as $cmd, or $cmd if no alias matches - local realcmd="${${(Az)aliases[$cmd]}[1]:-$cmd}" - # Get the first part of the $EDITOR command ($EDITOR may have arguments after it) - local editorcmd="${${(Az)EDITOR}[1]}" - - # Note: ${var:c} makes a $PATH search and expands $var to the full path - # The if condition is met when: - # - $realcmd is '$EDITOR' - # - $realcmd is "cmd" and $EDITOR is "cmd" - # - $realcmd is "cmd" and $EDITOR is "cmd --with --arguments" - # - $realcmd is "/path/to/cmd" and $EDITOR is "cmd" - # - $realcmd is "/path/to/cmd" and $EDITOR is "/path/to/cmd" - # or - # - $realcmd is "cmd" and $EDITOR is "cmd" - # - $realcmd is "cmd" and $EDITOR is "/path/to/cmd" - # or - # - $realcmd is "cmd" and $EDITOR is /alternative/path/to/cmd that appears in $PATH - if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \ - || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \ - || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then - __sudo-replace-buffer "$cmd" "sudo -e" - return - fi - - # Check for editor commands in the typed command and replace accordingly - case "$BUFFER" in - $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudo -e" ;; - \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudo -e" ;; - sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "$EDITOR" ;; - sudo\ *) __sudo-replace-buffer "sudo" "" ;; - *) LBUFFER="sudo $LBUFFER" ;; - esac - } always { - # Preserve beginning space - LBUFFER="${WHITESPACE}${LBUFFER}" - - # Redisplay edit buffer (compatibility with zsh-syntax-highlighting) - zle && zle redisplay # only run redisplay if zle is enabled - } -} - -zle -N sudo-command-line - -# Defined shortcut keys: [Esc] [Esc] -bindkey -M emacs '\e\e' sudo-command-line -bindkey -M vicmd '\e\e' sudo-command-line -bindkey -M viins '\e\e' sudo-command-line - diff --git a/users/guanranwang/home-manager/common/home.nix b/users/guanranwang/home-manager/common/home.nix index 3d2ef57..c109a8a 100644 --- a/users/guanranwang/home-manager/common/home.nix +++ b/users/guanranwang/home-manager/common/home.nix @@ -67,8 +67,17 @@ src = pkgs.zsh-fzf-tab; } { + # should I use flake inputs / fetchurl? name = "sudo"; - src = ./dotfiles/config/zsh/sudo; + src = (pkgs.fetchFromGitHub { + owner = "ohmyzsh"; + repo = "ohmyzsh"; + rev = "f8bf8f0029a475831ebfba0799975ede20e08742"; + hash = "sha256-9cJQQycahO+vo/YcAHjF+PVhsWxu7pa4MsK8Dgr69k0="; + sparseCheckout = [ + "plugins/sudo" + ]; + } + "/plugins/sudo"); } ]; initExtra = "zstyle ':fzf-tab:*' fzf-command sk";