1
0
mirror of https://github.com/tiyn/dotfiles.git synced 2026-05-09 05:51:35 +02:00

Zsh/Nvim: python is now mapped to uv run python using Zsh widgets and the compiler script for Nvim

This commit is contained in:
2026-05-08 02:33:06 +02:00
parent d579220afd
commit b82bfff551
2 changed files with 221 additions and 161 deletions

View File

@@ -2,16 +2,16 @@
# HELPER FUNCTIONS # # HELPER FUNCTIONS #
#################### ####################
precmd_vcs_info() { vcs_info } precmd_vcs_info() { vcs_info; }
precmd_functions+=( precmd_vcs_info ) precmd_functions+=(precmd_vcs_info)
zle-line-init() { zle-line-init() {
zle -K viins zle -K viins
echo -ne "\e[5 q" echo -ne "\e[5 q"
} }
preexec() { echo -ne '\e[5 q' ;} preexec() { echo -ne '\e[5 q'; }
local copy_widgets=( local copy_widgets=(
vi-yank vi-yank-eol vi-delete vi-backward-kill-word vi-change-whole-line vi-yank vi-yank-eol vi-delete vi-backward-kill-word vi-change-whole-line
@@ -22,14 +22,14 @@ local paste_widgets=(
) )
function zle-keymap-select { function zle-keymap-select {
if [[ ${KEYMAP} == vicmd ]] || if [[ ${KEYMAP} == vicmd ]] ||
[[ $1 = 'block' ]]; then [[ $1 = 'block' ]]; then
echo -ne '\e[1 q' echo -ne '\e[1 q'
elif [[ ${KEYMAP} == main ]] || elif [[ ${KEYMAP} == main ]] ||
[[ ${KEYMAP} == viins ]] || [[ ${KEYMAP} == viins ]] ||
[[ ${KEYMAP} == '' ]]; then [[ ${KEYMAP} == '' ]]; then
echo -ne '\e[5 q' echo -ne '\e[5 q'
fi fi
} }
function x11-clip-wrap-widgets() { function x11-clip-wrap-widgets() {
@@ -59,23 +59,23 @@ function +vi-git-st() {
local ahead behind remote local ahead behind remote
local -a gitstatus local -a gitstatus
remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} \ remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} \
--symbolic-full-name 2>/dev/null)/refs\/remotes\/} --symbolic-full-name 2>/dev/null)/refs\/remotes\//}
if [[ -n ${remote} ]] ; then if [[ -n ${remote} ]]; then
# for git prior to 1.7 # for git prior to 1.7
# ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l) # ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l)
ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l) ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
(( $ahead )) && gitstatus+=( " ${c3}+${ahead}${c2}" ) (($ahead)) && gitstatus+=(" ${c3}+${ahead}${c2}")
# for git prior to 1.7 # for git prior to 1.7
# behind=$(git rev-list HEAD..origin/${hook_com[branch]} | wc -l) # behind=$(git rev-list HEAD..origin/${hook_com[branch]} | wc -l)
behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l) behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
(( $behind )) && gitstatus+=( "${c4}-${behind}${c2}" ) (($behind)) && gitstatus+=("${c4}-${behind}${c2}")
hook_com[branch]="${hook_com[branch]} [${remote}${(j:/:)gitstatus}]" hook_com[branch]="${hook_com[branch]} [${remote}${(j:/:)gitstatus}]"
fi fi
} }
function +vi-git-stash() { function +vi-git-stash() {
local -a stashes local -a stashes
if [[ -s ${hook_com[base]}/.git/refs/stash ]] ; then if [[ -s ${hook_com[base]}/.git/refs/stash ]]; then
stashes=$(git stash list 2>/dev/null | wc -l) stashes=$(git stash list 2>/dev/null | wc -l)
hook_com[misc]+=" (${stashes} stashed)" hook_com[misc]+=" (${stashes} stashed)"
fi fi
@@ -86,56 +86,99 @@ function +vi-git-stash() {
################## ##################
mkcd() { mkcd() {
mkdir -p -- "$1" && mkdir -p -- "$1" &&
cd -- "$1" cd -- "$1"
} }
uv() { uv() {
if [[ "$1" == "init" ]]; then if [[ "$1" == "init" ]]; then
shift shift
if [[ "$1" == "--normal" ]]; then if [[ "$1" == "--normal" ]]; then
shift shift
command uv init "$@" command uv init "$@"
else else
command uv init --bare "$@" || return command uv init --bare "$@" || return
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
command uv venv || return command uv venv || return
if [[ -f ".venv/bin/activate" ]]; then if [[ -f ".venv/bin/activate" ]]; then
source .venv/bin/activate source .venv/bin/activate
fi
fi
fi fi
fi else
command uv "$@"
fi fi
else
command uv "$@"
fi
} }
_find_venv_upwards() { find_uv_root() {
local dir="$PWD" local target="$1"
local dir
while [[ "$dir" != "/" ]]; do if [[ -n "$target" && -e "$target" ]]; then
if [[ -f "$dir/.venv/pyvenv.cfg" ]]; then dir="$(cd "$(dirname "$target")" && pwd)"
echo "$dir/.venv" else
return dir="$PWD"
fi fi
dir=$(dirname "$dir")
done while [[ "$dir" != "/" ]]; do
if [[ -f "$dir/.python-version" ]] || [[ -f "$dir/pyproject.toml" ]]; then
echo "$dir"
return 0
fi
dir="$(dirname "$dir")"
done
return 1
}
uvshim() {
local cmd="$1"
shift
local uv_root=""
local first_arg="$1"
uv_root="$(find_uv_root "$first_arg")"
if [[ -n "$uv_root" ]]; then
uv run --project "$uv_root" "$cmd" "$@"
else
command -- "$cmd" "$@"
fi
}
python() { uvshim python "$@"; }
python3() { uvshim python3 "$@"; }
pip() { uvshim pip "$@"; }
pytest() { uvshim pytest "$@"; }
_find_venv_upwards() {
local dir="$PWD"
while [[ "$dir" != "/" ]]; do
if [[ -f "$dir/.venv/pyvenv.cfg" ]]; then
echo "$dir/.venv"
return
fi
dir=$(dirname "$dir")
done
} }
_auto_venv() { _auto_venv() {
local venv_dir local venv_dir
venv_dir=$(_find_venv_upwards) venv_dir=$(_find_venv_upwards)
if [[ -n "$VIRTUAL_ENV" && "$VIRTUAL_ENV" != "$venv_dir" ]]; then if [[ -n "$VIRTUAL_ENV" && "$VIRTUAL_ENV" != "$venv_dir" ]]; then
deactivate 2>/dev/null deactivate 2>/dev/null
fi fi
if [[ -n "$venv_dir" && "$VIRTUAL_ENV" != "$venv_dir" ]]; then if [[ -n "$venv_dir" && "$VIRTUAL_ENV" != "$venv_dir" ]]; then
source "$venv_dir/bin/activate" source "$venv_dir/bin/activate"
fi fi
} }
autoload -U add-zsh-hook autoload -U add-zsh-hook
@@ -144,89 +187,89 @@ _auto_venv
_accept_line() { _accept_line() {
# automatically push to remote # automatically push to remote
if [[ "$BUFFER" == "git push" ]]; then if [[ "$BUFFER" == "git push" ]]; then
local branch local branch
branch=$(git symbolic-ref --short HEAD 2>/dev/null) branch=$(git symbolic-ref --short HEAD 2>/dev/null)
if [[ -n "$branch" ]]; then if [[ -n "$branch" ]]; then
git rev-parse --abbrev-ref --symbolic-full-name @{u} &>/dev/null git rev-parse --abbrev-ref --symbolic-full-name @{u} &>/dev/null
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
BUFFER="git push -u origin $branch" BUFFER="git push -u origin $branch"
fi fi
fi fi
fi
# use keifu instead of git log --graph
if [[ "$BUFFER" =~ ^([a-zA-Z0-9_-]+)[[:space:]]+([a-zA-Z0-9_-]+)(.*)$ ]]; then
local cmd=${match[1]}
local subcmd=${match[2]}
local rest=${match[3]}
# directly via git log --graph
if [[ "$cmd" == "git" && "$subcmd" == "log" && "$rest" == *"--graph"* ]]; then
BUFFER="keifu"
zle accept-line
return
fi fi
# check for git-alias # use keifu instead of git log --graph
if [[ "$cmd" == "git" ]]; then if [[ "$BUFFER" =~ ^([a-zA-Z0-9_-]+)[[:space:]]+([a-zA-Z0-9_-]+)(.*)$ ]]; then
local alias_expansion
alias_expansion=$(git config --get "alias.$subcmd" 2>/dev/null)
if [[ -n "$alias_expansion" && "$alias_expansion" == log*--graph* ]]; then local cmd=${match[1]}
BUFFER="keifu" local subcmd=${match[2]}
zle accept-line local rest=${match[3]}
return
fi
fi
# shell-alias # directly via git log --graph
if alias "$cmd" &>/dev/null; then if [[ "$cmd" == "git" && "$subcmd" == "log" && "$rest" == *"--graph"* ]]; then
local expansion=$(alias "$cmd") BUFFER="keifu"
expansion=${expansion#*=} zle accept-line
expansion=${expansion#\'} return
expansion=${expansion%\'}
if [[ "$expansion" =~ --git-dir=([^[:space:]]+) ]]; then
local gitdir=${match[1]}
if [[ "$subcmd" == "log" && "$rest" == *"--graph"* ]]; then
BUFFER="(cd $gitdir && keifu)"
zle accept-line
return
fi fi
# git-alias within shell-alias # check for git-alias
local alias_expansion if [[ "$cmd" == "git" ]]; then
alias_expansion=$(git --git-dir="$gitdir" config --get "alias.$subcmd" 2>/dev/null) local alias_expansion
alias_expansion=$(git config --get "alias.$subcmd" 2>/dev/null)
if [[ -n "$alias_expansion" && "$alias_expansion" == log*--graph* ]]; then if [[ -n "$alias_expansion" && "$alias_expansion" == log*--graph* ]]; then
BUFFER="(cd $gitdir && keifu)" BUFFER="keifu"
zle accept-line zle accept-line
return return
fi
fi fi
fi
fi
fi
# swap main and master Fix # shell-alias
if [[ $BUFFER == git\ * ]]; then if alias "$cmd" &>/dev/null; then
local has_main=0 local expansion=$(alias "$cmd")
local has_master=0 expansion=${expansion#*=}
git rev-parse --verify main >/dev/null 2>&1 && has_main=1 expansion=${expansion#\'}
git rev-parse --verify master >/dev/null 2>&1 && has_master=1 expansion=${expansion%\'}
if [[ $has_main -eq 1 && $has_master -eq 0 ]]; then
BUFFER=${BUFFER//" master"/" main"}
fi
if [[ $has_master -eq 1 && $has_main -eq 0 ]]; then
BUFFER=${BUFFER//" main"/" master"}
fi
fi
zle accept-line if [[ "$expansion" =~ --git-dir=([^[:space:]]+) ]]; then
local gitdir=${match[1]}
if [[ "$subcmd" == "log" && "$rest" == *"--graph"* ]]; then
BUFFER="(cd $gitdir && keifu)"
zle accept-line
return
fi
# git-alias within shell-alias
local alias_expansion
alias_expansion=$(git --git-dir="$gitdir" config --get "alias.$subcmd" 2>/dev/null)
if [[ -n "$alias_expansion" && "$alias_expansion" == log*--graph* ]]; then
BUFFER="(cd $gitdir && keifu)"
zle accept-line
return
fi
fi
fi
fi
# swap main and master Fix
if [[ $BUFFER == git\ * ]]; then
local has_main=0
local has_master=0
git rev-parse --verify main >/dev/null 2>&1 && has_main=1
git rev-parse --verify master >/dev/null 2>&1 && has_master=1
if [[ $has_main -eq 1 && $has_master -eq 0 ]]; then
BUFFER=${BUFFER//" master"/" main"}
fi
if [[ $has_master -eq 1 && $has_main -eq 0 ]]; then
BUFFER=${BUFFER//" main"/" master"}
fi
fi
zle accept-line
} }
zle -N _accept_line zle -N _accept_line
@@ -275,7 +318,7 @@ setopt prompt_subst
# copy to x11 clipboard # copy to x11 clipboard
x11-clip-wrap-widgets copy $copy_widgets x11-clip-wrap-widgets copy $copy_widgets
x11-clip-wrap-widgets paste $paste_widgets x11-clip-wrap-widgets paste $paste_widgets
# enable colors # enable colors
autoload -U colors && colors autoload -U colors && colors
@@ -343,7 +386,7 @@ fi
eval $(thefuck --alias) eval $(thefuck --alias)
# opam # opam
[[ ! -r "$HOME/.opam/opam-init/init.zsh" ]] || source "$HOME/.opam/opam-init/init.zsh" > /dev/null 2> /dev/null [[ ! -r "$HOME/.opam/opam-init/init.zsh" ]] || source "$HOME/.opam/opam-init/init.zsh" >/dev/null 2>/dev/null
########################## ##########################
# COMMANDS BEFORE PROMPT # # COMMANDS BEFORE PROMPT #

View File

@@ -14,43 +14,60 @@ vipercmd="carbon" #"silicon"
cd "$dir" || exit cd "$dir" || exit
textype() { \ has_uv_project() {
command="pdflatex --shell-escape" dir="$PWD"
( sed 5q "$file" | grep -i -q 'xelatex' ) && command="xelatex" while [ "$dir" != "/" ]; do
$command --output-directory="$dir" "$base" && if [ -f "$dir/.python-version" ] || [ -f "$dir/pyproject.toml" ]; then
grep -i addbibresource "$file" >/dev/null && return 0
biber --input-directory "$dir" "$base" && fi
$command --output-directory="$dir" "$base" && dir=$(dirname "$dir")
done
return 1
}
textype() {
command="pdflatex --shell-escape"
(sed 5q "$file" | grep -i -q 'xelatex') && command="xelatex"
$command --output-directory="$dir" "$base" &&
grep -i addbibresource "$file" >/dev/null &&
biber --input-directory "$dir" "$base" &&
$command --output-directory="$dir" "$base" &&
makeglossaries "$basenodir" && makeglossaries "$basenodir" &&
$command --output-directory="$dir" "$base" $command --output-directory="$dir" "$base"
} }
case "$file" in case "$file" in
*\.[0-9]) refer -PS -e "$file" | groff -mandoc -T pdf > "$base".pdf ;; *\.[0-9]) refer -PS -e "$file" | groff -mandoc -T pdf >"$base".pdf ;;
*\.bash) bash "$file" ;; *\.bash) bash "$file" ;;
*\.go) go run "$file" ;; *\.go) go run "$file" ;;
*\.lua) lua "$file" ;; *\.lua) lua "$file" ;;
*\.md) pandoc "$file" --pdf-engine=xelatex -o "$base".pdf ;; *\.md) pandoc "$file" --pdf-engine=xelatex -o "$base".pdf ;;
*\.mom) refer -PS -e "$file" | groff -mom -kept -T pdf > "$base".pdf ;; *\.mom) refer -PS -e "$file" | groff -mom -kept -T pdf >"$base".pdf ;;
*\.ms) refer -PS -e "$file" | groff -me -ms -kept -T pdf > "$base".pdf ;; *\.ms) refer -PS -e "$file" | groff -me -ms -kept -T pdf >"$base".pdf ;;
*\.py) python3 "$file" ;; *\.py)
*\.rmd) echo "require(rmarkdown); render('$file')" | R -q --vanilla ;; if has_uv_project; then
*\.sent) setsid sent "$file" 2>/dev/null & ;; uv run python "$file"
*config.h) sudo make install ;; else
*\.c) if [ -f Makefile ]; then make run; else cc "$file" -o "$base" && "$base"; fi ;; python3 "$file"
*\.java) java "$file" ;; fi
*\.js) node "$file" ;; ;;
*\.m) octave -qW "$file" ;; *\.rmd) echo "require(rmarkdown); render('$file')" | R -q --vanilla ;;
*\.nim) nim c -r -d:noColors "$file" ;; *\.sent) setsid sent "$file" 2>/dev/null & ;;
*\.r|*\.R) Rscript "$file" ;; *config.h) sudo make install ;;
*\.rs) (cd "$dir" && cargo locate-project >/dev/null 2>&1) \ *\.c) if [ -f Makefile ]; then make run; else cc "$file" -o "$base" && "$base"; fi ;;
&& (cd "$(dirname "$(cd "$dir" && cargo locate-project --message-format plain)")" && cargo run) \ *\.java) java "$file" ;;
|| (rustc "$file" && "./$(basename "${file%.rs}")") ;; *\.js) node "$file" ;;
*\.sh) dash "$file" ;; *\.m) octave -qW "$file" ;;
*\.smt2) z3 "$file" ;; *\.nim) nim c -r -d:noColors "$file" ;;
*\.tex) textype "$file" ;; *\.r | *\.R) Rscript "$file" ;;
*\.vpr) $vipercmd "$file" ;; *\.rs) (cd "$dir" && cargo locate-project >/dev/null 2>&1) &&
*\.zsh) zsh "$file" ;; (cd "$(dirname "$(cd "$dir" && cargo locate-project --message-format plain)")" && cargo run) ||
*\.ly) lilypond "$file" ;; (rustc "$file" && "./$(basename "${file%.rs}")") ;;
*) sed 1q "$file" | grep "^#!/" | sed "s/^#!//" | xargs -r -I % "$file" ;; *\.sh) dash "$file" ;;
*\.smt2) z3 "$file" ;;
*\.tex) textype "$file" ;;
*\.vpr) $vipercmd "$file" ;;
*\.zsh) zsh "$file" ;;
*\.ly) lilypond "$file" ;;
*) sed 1q "$file" | grep "^#!/" | sed "s/^#!//" | xargs -r -I % "$file" ;;
esac esac