From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 22397 invoked from network); 21 Apr 2021 11:38:12 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 21 Apr 2021 11:38:12 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1619005092; b=beqTUS2pPeJrkCjOL7BelzoXOTDchLWTNDbeZe1erDb2ajOAUUAIc8oqu67uSX5hvznaSyuooG qRzXmLbSoEz3WVJxz7KCwjv/cAgo2+n/aVnyw0Mpy0vyecvVG/NXSZ9eVdyhMVVdWtBvatHuKX W/4sI7mIqg8YoED/kkBR5+8RZl2J+mCLamiUfQt+Iw1sXps1UnWh6Gl802O76VssNCXTs477iy rkNIqebA7gJVL4y0OSsDHbM25PIclS6nUi+H2KmpMbOB70xOUQ6u8LwquX+/c+4rOQSg8s2GTJ +d1tiPPrpyWErwZmjEPXjf7ASo30FJ9CCyS6bAztisfKmw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-lf1-f43.google.com) smtp.remote-ip=209.85.167.43; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1619005092; bh=EjOA0mvK11+8jojUocf9j/GoE0e0kOYeZn8xTHQb00Y=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:References:To:Cc:In-Reply-To:Date:Subject:MIME-Version: Content-Type:Message-ID:From:DKIM-Signature:DKIM-Signature; b=vvrASRwW0+R1CBTJNyi5RZ7FgWCa133/752pdlKp87nebl9xIPZ9jzIP8pp+JpfRzddY3Fmjqb 3FhHK+tkSlmlvZSHT1C1/b4KCGd11dCpCvChVJ7DSM4Tfir5/ES+CuvyY30OKD+vqlj9+9ddto omOtLD5iizZVDUq6YtvWm0qik3vHmnUF7W9Ecx5fKB5ueE4hXW5YbzyzAJws6yu54VcLwbtSa2 Zi4uYSZE8UgRWikQh3pfYs8s9qjLSeQxJwLFt3LFQSDG7GH0hPeSSOnzA27dkx9dFx1XyQkiGB BJK+0wz7ZfenukqQ9mMQGPIISRT7KBiCOr5J/qPuHlvw6g==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:References:To:Cc:In-Reply-To:Date: Subject:Mime-Version:Content-Type:Message-Id:From:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=Zw63+1a6rwUGv51g4BzA9c1Jd0eVVX3f1jpGaMDDkzQ=; b=s4iyo0mN8KmmZb1ztuOYeS6grC nfHfCRPURTmdBCpGwYXOo859kgMDPgYhJoD/US3uxrQ6wPsPw+/UatkP6MXaglP/HnqPZXpt5yOWc 3T7VMheBBx37aqj6k1NsBKRjry4BSXio/ybGpp0gf++NYYdhJWtungryQ2qfqGIPoWemsYdfTBOPK zDJpaybgX4z1ObsiX2wTcmGyEkK8x5m4ddAIyS9LEwRoKKLvnPpxLhn9UfOk8YfKMF7OSm+blo6me lpQwn3J2K0TOaEQbpiYYDKC4w3TLrrtr/jKXTerdlI6GmFl6qPH32ihJNugVFJxc9V+245B7aC+ce 3XbWYnyw==; Received: from authenticated user by zero.zsh.org with local id 1lZBBJ-0005u0-Ce; Wed, 21 Apr 2021 11:38:09 +0000 Authentication-Results: zsh.org; iprev=pass (mail-lf1-f43.google.com) smtp.remote-ip=209.85.167.43; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-lf1-f43.google.com ([209.85.167.43]:45708) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1lZBAh-0005e5-Gt; Wed, 21 Apr 2021 11:37:36 +0000 Received: by mail-lf1-f43.google.com with SMTP id g8so66423116lfv.12 for ; Wed, 21 Apr 2021 04:37:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=Zw63+1a6rwUGv51g4BzA9c1Jd0eVVX3f1jpGaMDDkzQ=; b=Yv88KXY4nZk7KrF+ybb4aYdv4NrQ7B98FUe7aCGH+EywpugGqf0PvRQmMEn9RSsmJB z+rhIMHuisKlAWzNdQjqLU/yNsRVI+wDRf7tg/+n+1M3otpZerKvMu+Vkz48Np0mUX1q WOztoloC23tR9fuZKIMh5ZzyxsmNtSo/fkyTxXdKaTlswrx1HFRwjcIrFAeYCjDiKQci eCMOW3uue0voLRBtsqzJdNqFr5P8D0iRbeZlca4Lm4S83ppM4dpD+jiWLsTDZNm2WVlr 6R0XnP+iIZrJETK8SG/cKnxhaQ9xD2f2J24/+TLyiQJY9k82UX4E1a2n4/i1RJzekBs+ nHWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=Zw63+1a6rwUGv51g4BzA9c1Jd0eVVX3f1jpGaMDDkzQ=; b=pnW91kyb4CyMJWjdkbZ1puM4z34FSnOH5Y8RM7zml98jLn/UuyiV4ilTQAuGJiKwjV oDBezgzl9HFPhjpj4hA+X590BPD/8nnF3jRw8BZBZkdk9lNVjGl2jU5peNmhq1avIfpK Do9lIgo0AfnY556gQDl4m/XaBtJpfleTnvKRdg6fXqls8plaR/FZU0c8beLKNsliNI4w 1ejMxQeGE3hGsrp1hKFAtiEPt7aATID9Y3k1zZPEhImDhCUbglhnGWjhARC7QFjFqQdc Jb1Fg/mS4mPnEeHs4kWbQpUyAjBt8Aaq7IQczoEW1wlHj3IL6inuGltZn8XpnrUnLJIl VYDA== X-Gm-Message-State: AOAM530aRDYnAAH/b92Aa0X5HfIoCc3y9AZnzwvKw3VBsFiVATNU+jVi 5C8FPzD+TWja5PZTvZeFKMCmNC8X3vvZ5g== X-Google-Smtp-Source: ABdhPJwSY/usnI8mFOSxyxEFo+44jK69IS60KkHrd5ADmPkfhIyTwLUFRZG11o8XIFTTiQDDd4mmOQ== X-Received: by 2002:a05:6512:3189:: with SMTP id i9mr19394701lfe.613.1619005050803; Wed, 21 Apr 2021 04:37:30 -0700 (PDT) Received: from [192.168.1.15] (hag-tkubng22-50df3e-132.dhcp.inet.fi. [80.223.62.132]) by smtp.gmail.com with ESMTPSA id v11sm182689ljp.63.2021.04.21.04.37.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Apr 2021 04:37:30 -0700 (PDT) From: Marlon Richert Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_0D3178CF-CDE4-40FF-8BD1-958750DAA90D" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) Subject: [PATCH] Add execute-command() widget function (was Re: [RFC][PATCH] Add change-directory() widget function) Date: Wed, 21 Apr 2021 14:37:28 +0300 In-Reply-To: Cc: Zsh hackers list To: Bart Schaefer , Daniel Shahaf References: <95CDA630-4EE5-4003-8D9C-CCCB9A47F109@gmail.com> <4D587C0C-EB5F-4A58-A0AE-D45E43F432CD@gmail.com> X-Mailer: Apple Mail (2.3654.60.0.2.21) X-Seq: 48642 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: --Apple-Mail=_0D3178CF-CDE4-40FF-8BD1-958750DAA90D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On 21 Apr 2021, at 00:32, Bart Schaefer = wrote: > My only remark about this as-is, is that it would have potentially > dangerous side-effects if invoked at a PS2 or PS3 prompt. >=20 > Potential fixes - begin the widget function with: > [[ ${(%):-%_} =3D select ]] && return 1 > [[ -n "$PREBUFFER$BUFFER" ]] && zle push-input On 21 Apr 2021, at 06:46, Bart Schaefer = wrote: > Oh, I missed this: >=20 >> On Tue, Apr 20, 2021 at 1:13 PM Marlon Richert = wrote: >>> @@ -0,0 +1,29 @@ >>> +zle .push-line-or-edit >=20 > That's still wrong, because in the event there's a PS2 prompt, you'll > never get past this line. You need .push-input here. My suggested > test for $PREBUFFER is probably not actually needed. Thank you both (Bart & Daniel) for your input. I added better safeguards = against the non-PS1 cases, rewrote the function to be more generic and = added comments. New patch attached. --Apple-Mail=_0D3178CF-CDE4-40FF-8BD1-958750DAA90D Content-Disposition: attachment; filename=0001-Add-execute-command-widget-function.txt Content-Type: text/plain; x-unix-mode=0644; name="0001-Add-execute-command-widget-function.txt" Content-Transfer-Encoding: quoted-printable =46rom 83da04aba0d07112f650a98fafc3f28daf75dcd1 Mon Sep 17 00:00:00 2001 From: Marlon Richert Date: Wed, 21 Apr 2021 14:33:27 +0300 Subject: [PATCH] Add execute-command() widget function --- Doc/Zsh/contrib.yo | 26 +++++++++++++++++ Functions/Zle/execute-command | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Functions/Zle/execute-command diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo index 8bf1a208e..df02fc4d9 100644 --- a/Doc/Zsh/contrib.yo +++ b/Doc/Zsh/contrib.yo @@ -2502,6 +2502,32 @@ arguments: =20 example(zstyle :zle:edit-command-line editor gvim -f) ) +tindex(execute-command) +item(tt(execute-command))( +This function lets you implement widgets that can execute arbitrary = commands=20 +without losing the current command line, in a fashion similar to the=20 +tt(run-help) and tt(which-command) widgets (see=20 +ifzman(the subsection Miscellaneous in zmanref(zshzle))\ +ifnzman(noderef(Miscellaneous))). More precisely, it=20 +enumeration( +myeit() pushes the buffer onto the buffer stack,=20 +myeit() executes the supplied arguments, then=20 +myeit() lets the ZLE pop the buffer off the top of the buffer stack and = load=20 + it into the editing buffer. +) + +You can use this, for example, to create key bindings that let you = instantly=20 +change directories, even while in the middle of typing another command:=20= + +example(autoload -Uz execute-command +setopt autopushd pushdminus pushdsilent +zle -N cd-upward ; cd-upward() { execute-command cd .. } +zle -N cd-backward; cd-backward() { execute-command pushd -1 } +zle -N cd-forward ; cd-forward() { execute-command pushd +0 } +bindkey '^[^[[A' cd-upward; bindkey '^[^[OA' cd-upward +bindkey '^[-' cd-backward +bindkey '^[=3D' cd-forward) +) tindex(expand-absolute-path) item(tt(expand-absolute-path))( Expand the file name under the cursor to an absolute path, resolving diff --git a/Functions/Zle/execute-command = b/Functions/Zle/execute-command new file mode 100644 index 000000000..04fccf176 --- /dev/null +++ b/Functions/Zle/execute-command @@ -0,0 +1,54 @@ +# This function lets you implement widgets that can execute arbitrary = commands +# without losing the current command line, in a fashion similar to the +# 'run-help' and 'which-command' widgets. You can use this, for = example, to +# create key bindings that let you instantly change directories, even = while in +# the middle of typing another command: +# +# autoload -Uz execute-command +# setopt autopushd pushdminus pushdsilent +# zle -N cd-upward ; cd-upward() { execute-command cd .. } +# zle -N cd-backward; cd-backward() { execute-command pushd -1 } +# zle -N cd-forward ; cd-forward() { execute-command pushd +0 } +# bindkey '^[^[[A' cd-upward; bindkey '^[^[OA' cd-upward +# bindkey '^[-' cd-backward +# bindkey '^[=3D' cd-forward +# + +case $CONTEXT in + ( start ) # PS1 + ;; + ( cont ) # PS2 + # Add a one-time hook that will re-run this widget at the top-level = prompt. + autoload -Uz add-zle-hook-widget + local hook=3Dline-init + local func=3D:$hook:$WIDGET + eval "$func() { + # Make sure we don't run twice. + add-zle-hook-widget -d $hook $func + + # Don't leave anything behind. + zle -D $func + unfunction $func + + zle $WIDGET + }" + add-zle-hook-widget $hook $func + + # Move the entire current multiline construct into the editor = buffer. This + # function is then aborted and we return to the top-level prompt, = which + # triggers the hook above. + zle .push-line-or-edit + return # Not actually necessary, but for clarity's sake + ;; + ( * ) + # We don't want this to be used in a select loop or in vared. + return 1 + ;; +esac + +# Push the current buffer onto the buffer stack and clear the buffer. = The ZLE +# will auto-restore it at the next top-level prompt. +zle .push-line + +BUFFER=3D"$*" +zle .accept-line --=20 2.31.1 --Apple-Mail=_0D3178CF-CDE4-40FF-8BD1-958750DAA90D Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii --Apple-Mail=_0D3178CF-CDE4-40FF-8BD1-958750DAA90D--