From 964e5b65c6290fc52367cc49a7c1513d49efd3ff Mon Sep 17 00:00:00 2001 From: Morningstar202604 <320475654+Morningstar202604@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:45:29 +0800 Subject: [PATCH] fix(vi-mode): fix stale clipboard retry after failed push (#14141) --- plugins/vi-mode/README.md | 9 ++++++++ plugins/vi-mode/vi-mode.plugin.zsh | 33 ++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/plugins/vi-mode/README.md b/plugins/vi-mode/README.md index bc78cfa3d..16135223c 100644 --- a/plugins/vi-mode/README.md +++ b/plugins/vi-mode/README.md @@ -39,6 +39,15 @@ plugins=(... vi-mode) - `VI_MODE_DISABLE_CLIPBOARD`: If set, disables clipboard integration on yank/paste +Clipboard integration keeps put widgets (`p`/`P`) in sync with the system +clipboard, but only while the last copy to the clipboard actually succeeded. +If that copy failed — no `xclip`/`xsel`, Wayland without `wl-clipboard`, a dead +X forwarding over SSH — the clipboard no longer mirrors what you killed inside +the line editor, so puts use the internal kill buffer instead of the stale +clipboard contents. After a failed push, puts keep using the kill buffer only +while the clipboard still holds exactly what it held when the push failed; a +new external copy is picked up again immediately. + ## Mode indicators *Normal mode* is indicated with a red `<<<` mark at the right prompt, when it diff --git a/plugins/vi-mode/vi-mode.plugin.zsh b/plugins/vi-mode/vi-mode.plugin.zsh index 2704fb05e..7c057b788 100644 --- a/plugins/vi-mode/vi-mode.plugin.zsh +++ b/plugins/vi-mode/vi-mode.plugin.zsh @@ -118,6 +118,15 @@ bindkey '^s' history-incremental-search-forward bindkey '^a' beginning-of-line bindkey '^e' end-of-line +# Snapshot of the clipboard taken when the most recent push to it failed. +# While the marker is set, put widgets compare the live clipboard against the +# snapshot: unchanged means the clipboard is stale (it never received the +# kill), so the internal CUTBUFFER is used instead. As soon as the clipboard +# differs again - e.g. an external copy - it is trusted immediately, so +# external copies are never lost for longer than the failure itself. +typeset -g _OMZ_VI_CLIPBOARD_STALE="" +typeset -g _OMZ_VI_CLIPBOARD_STALE_SET="" + function wrap_clipboard_widgets() { # NB: Assume we are the first wrapper and that we only wrap native widgets # See zsh-autosuggestions.zsh for a more generic and more robust wrapper @@ -132,13 +141,33 @@ function wrap_clipboard_widgets() { eval " function ${wrapped_name}() { zle .${widget} - printf %s \"\${CUTBUFFER}\" | clipcopy 2>/dev/null || true + # On a failed push, snapshot the clipboard so put widgets can tell + # stale content (which never received this kill) apart from later + # external copies. + if printf %s \"\${CUTBUFFER}\" | clipcopy 2>/dev/null; then + _OMZ_VI_CLIPBOARD_STALE_SET=\"\" + else + _OMZ_VI_CLIPBOARD_STALE=\"\$(clippaste 2>/dev/null)\" + _OMZ_VI_CLIPBOARD_STALE_SET=1 + fi } " else eval " function ${wrapped_name}() { - CUTBUFFER=\"\$(clippaste 2>/dev/null || echo \$CUTBUFFER)\" + if [[ -n \"\${_OMZ_VI_CLIPBOARD_STALE_SET:-}\" ]]; then + # Last push failed. If the clipboard still matches the snapshot + # taken at the failure, it is stale: keep the internal CUTBUFFER. + # If it differs, an external copy arrived since - trust it again + # and clear the stale state. + local _clip=\"\$(clippaste 2>/dev/null)\" + if [[ \"\$_clip\" != \"\${_OMZ_VI_CLIPBOARD_STALE}\" ]]; then + CUTBUFFER=\"\$_clip\" + _OMZ_VI_CLIPBOARD_STALE_SET=\"\" + fi + else + CUTBUFFER=\"\$(clippaste 2>/dev/null || echo \$CUTBUFFER)\" + fi zle .${widget} } "