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 25452 invoked from network); 21 Dec 2021 00:57:43 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 21 Dec 2021 00:57:43 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1640048263; b=YsVK0N+3xksfc+ht32AHPQpAP4asFAFBDHDqSL8VV9KSFZiL1AeeqHGSlFazOV709CYbN0Rhy5 SdTmgqEXv/lirUpG2xkDwiAat8sPCg1o5Qp+6c2PWaHzuBu+GZqdxRdoGtBMT8wiyMHn3G5FbP Oa3ah06Of6Jk90ssKxG0GUrQwhnxmzUTCDbw1xKo8x+8grfGQ8xLZFKvvfui3i1uNLFav1oR1U f87cPLmakVGd3E0DsLIC/slrTbr/zMJOM/9FOBgvv22FuLwUThYHZYu2Ey4rRGNYMrYRrlkh2n DZ0mnh0NbL6PCUaWD1xbKYGgfmK+7Lk1rjiJVUMpKGmcZg==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-wr1-f43.google.com) smtp.remote-ip=209.85.221.43; dkim=pass header.d=gmail.com header.s=20210112 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-20210803; t=1640048263; bh=GO+R2VVYTrlLJk8WIOhFkIeOQ75gun8SHbf4BUHiCS4=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:Cc:To:Subject: Message-ID:Date:From:In-Reply-To:References:MIME-Version:DKIM-Signature: DKIM-Signature; b=C/BpYIovvI45CqS+vl4CddZ8BbzFUqJ7URM57Go8HmT7VNuPfxYFFiVKeAe8VyDjLqSKtWL2wA cHQr9211bUR64JRRPq1LIrxIyUMmiNM17eguULjO3aBt7ZqLY4gCTlwLrCBg7k9YvU+rvB+SON C1rC/i6fiULA/YG5uO8wXVTQAJjzSZBx8krYsNT4Qn0OQh2g4DJ8gyD5oaL6XBMc/GOleLCkv4 C71Z0ZY3G8OKk906JqNzM4rKs+fjgGyD3FCDw2YLgWzMWiRjMI557A68sDF2hE92Mc17hLz8xK Di3LsYdfr6UyZKWL/YiRXiJ8Hxi9FLulgJf+yBX4V5ZWZg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=sHLby/c+Tq+IyE8igxbwI+FvHEGSR93pkDwdPmkuHqQ=; b=VlQsRUSpa2Sat08pGqppzYf4so lglDccNtsWTH/eFCPfNaO2xeiq9/LZU54OMWX2/Ptqdl2SUkMgqRpn/kLF0Ubv8zqQAP7pxwXnltl 5prKAmOOs8jezrJrC+lW3pFh3huNKLNbfCscZn3qj5TlPusjdWRt8NjLBf+PVdw5QOubOPjt+1Yqy Wm1KcEGChIYFfKIdUtF8d9fu63vdWFhag5ud+PU4hstUAo/ImGk0aGj9P0OC2FIlYn8n0mPGppPmP 7Z2JBZB1IZ0AubwpwHmckLNjy5sii+drtrsGJjb9uEovxkvUtt0soMrvJT53BiVXCk3Xnw8MrYJmh L6hhZYGg==; Received: from authenticated user by zero.zsh.org with local id 1mzTTK-000IoG-8X; Tue, 21 Dec 2021 00:57:42 +0000 Authentication-Results: zsh.org; iprev=pass (mail-wr1-f43.google.com) smtp.remote-ip=209.85.221.43; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-wr1-f43.google.com ([209.85.221.43]:45606) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1mzTSL-000I39-IR; Tue, 21 Dec 2021 00:56:42 +0000 Received: by mail-wr1-f43.google.com with SMTP id v7so16151946wrv.12 for ; Mon, 20 Dec 2021 16:56:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=sHLby/c+Tq+IyE8igxbwI+FvHEGSR93pkDwdPmkuHqQ=; b=JrMGsmX3rbIDDSXm8LrAUXCMvIu7u2p/BgKeOdUxxnON/l12dLhUQKIoI9Rw95OJz8 qZaum9uUrsIHYIFSFiKM61LoGLAtpZVHQfm58Fds64eE1Vmo6wmoP/FmwxGahnvnVAtG LrVwwCKwID1zcK+e80CPIDQZVYDnULNdYej3hTgAMp2Ah5lD23uiFYkoLIf/td9zlAvc wc/co90FZo+bn8LNSXkifFvp21Y4FwU1WFS8gzc0CihlBcGgzepdHrLcPFb5I+2xXiBw Uq9xwwxHOmcNn3KtADPAPxJ8fwYi/staGPT0GnHawH9VuplzV1iROWmTjwFUzr2wgmq/ 94GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=sHLby/c+Tq+IyE8igxbwI+FvHEGSR93pkDwdPmkuHqQ=; b=BnAklCGiGsMPdX7b/jlrwJAauDUkECMn2PhUbjOjj2JEyzUpPf2AUJ0nWGubjdcWV6 m54EP0DKTMDXx4ffSQt69IEnG811/dRLH5qBv3fnhQDGFhPLlXuj4pLQRuHUL0SuQo74 wJUHE8T9rDv3S+mkPovuRtxMbqy3HIDzzVJPvT/HST5B6SChZ5mZv5KmooDRpQ/MwM9Q 9+0n2wtNfbvovemKnDZLmB+L0Kv5t8OZ+3JNju8BgJJ//3q1jW5j4qS3dcKtI0DPgPln B40FYijVV3+so4pc2/lBDdMA2dQIf1RGr1w4Yv9bLyXVFhlx6iCjPDtLBlM9kXhlHvQU +DUQ== X-Gm-Message-State: AOAM5330csAcBVXaOIPI1j3new1zzSwv/tTyTfZjjw82MkXE7iuXmwUe VZZWXALXcWCKFpAyva/mx/8WlgF8A3vtLlKK+sL0CUrHmrU= X-Google-Smtp-Source: ABdhPJzm4emAROZnhVu7vw2TxzaEYqEVQNQxX4IfMNWnlSjwULe8/7w+2UfEn6EX2dvkgCLZ+yBPZrBoBc+3ypQnvcw= X-Received: by 2002:a05:6000:144c:: with SMTP id v12mr519845wrx.266.1640048200761; Mon, 20 Dec 2021 16:56:40 -0800 (PST) MIME-Version: 1.0 References: <64ee9965-6517-481f-9fb5-683bd48cf220@www.fastmail.com> In-Reply-To: <64ee9965-6517-481f-9fb5-683bd48cf220@www.fastmail.com> From: Zach Riggle Date: Mon, 20 Dec 2021 18:56:29 -0600 Message-ID: Subject: Re: Global Aliases, but as a function? To: Daniel Shahaf Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Seq: 27431 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Where can I read in the documentation about the preexec wrapper and specifically how to modify the command being executed? Zach Riggle On Mon, Dec 20, 2021 at 12:42 PM Daniel Shahaf wro= te: > > Bart Schaefer wrote on Sat, 18 Dec 2021 22:40 +00:00: > > On Sat, Dec 18, 2021 at 2:38 AM Zach Riggle wrot= e: > >> > >> Is there any way to define something similar to a "global alias" but > >> which can modify the original command? > >> > >> There's a lot of use-cases for this, but the one that I'm wondering > >> about specifically is to pass anything with "--help" in the command > >> line into a pager that does a best-effort colorization IFF the output > >> is not already colored. > > > > The only way to approximate this is by hooking into the ZLE editor to > > modify $BUFFER before passing it to the lexer/parser. (Aliases modify > > the single substituted word, during lexical analysis.) > > > > One could have preexec() install wrapper functions that shadow the > command words of any simple command that has a =C2=AB--help=C2=BB argumen= t. > > I'm not sure whether or not that's preferable to the zle approaches. > > Cheers, > > Daniel > > > There are a bunch of ways to do this, but mainly there are three > > choices, any/all of which involve creating a custom widget: > > > > 1/ Explicitly invoke your widget with a key binding. This gives you > > the most flexibility because you can edit the result or undo it. (In > > this case, though, you might instead want to consider using the > > run-help binding with a suitable function.) > > 2/ Override the accept-line widget (or the keybindings for it) with > > your custom widget (and then invoke the ".accept-line" widget at the > > end). This may be tricky to get right, and to avoid having it > > interfere with other plugins etc. Be prepared to handle $PREBUFFER > > and other multi-line-input situations. > > 3/ Add your widget to the zle-line-finish hooks. This is more likely > > to play well with plugin managers and you don't need .accept-line, but > > you still need to handle multi-line input. > > > > All of these (except run-help) end up putting the whole command > > including the appended pipeline into the history. If you want to > > avoid that, look at zshaddhistory hooks. > > > > Naive/incomplete example of #3: > > > > page-the-help() { > > if [[ -z $PREBUFFER && $BUFFER =3D *--help* ]] > > then RBUFFER+=3D" | less" > > fi > > } > > zle -N page-the-help > > add-zle-hook-widget zle-line-finish page-the-help