zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: zsh-users@zsh.org
Cc: "Sam B." <me@rmz.io>
Subject: Re: Disable most shell expansion to function or script
Date: Thu, 4 Jan 2024 20:16:34 +0100	[thread overview]
Message-ID: <CAHYJk3TcGphoKMmH1n3hSVobpD7qtooyfGKCLtBhGM+kPYpMmA@mail.gmail.com> (raw)
In-Reply-To: <a2fkj5bgzxlsumj6a67fnff2yjhbiecch2hecbpoqll3fr6zmq@ysnu4kgkr76c>

On 1/4/24, Sam B. <me@rmz.io> wrote:
> Hello,
>
> I'm looking for a way to disable most shell expansion for the arguments
> to a script or function and treat everything up to a newline char as
> verbatim text, i.e. as if quoted.
>
> For example, given a function/script "todo", I'd like
>
>      todo Text inc. $var, *glob?, events!, <redirs, >(sub) & more
>
> to behave more like it was quoted:
>
>      todo 'Text inc. $var, *glob?, events!, <redirs, >(sub) & more'
>
> I know of noglob to disable glob chars, and can have a look at histchars
> to see if events can be disabled. But I'm not sure if any of the other
> expansion can possibly disabled.
>
> Mostly I'm interested joting down some plain text which could include
> '?!&' and have "todo" receive it all without the shell having done some
> magic beforehand.
>
> Any ideas?

function _accept_line () {
  if [[ $CONTEXT = vared ]] || [[ $zsh_eval_context != shfunc ]]; then
    zle .$WIDGET
    return
  fi
  if [[ "$BUFFER" = todo\ * ]]; then
    BUFFER="todo ${(q)BUFFER[6,-1]}"
    zle .$WIDGET
    return # not needed in this example but if you handle other stuff below
  fi
}

zle -N accept-line _accept_line
zle -N accept-line-and-down-history _accept_line
zle -N accept-and-hold _accept_line

Note that the quoted version will be saved in the history, and if you
recall the event, it will be quoted again by the above code, so there
is room for improvement / other ideas.

For example, make a function that takes no arguments, reads a single
line, and runs todo $REPLY for you. (todo itself could handle this if
given no arguments too?)

runquoted() {
  local cmd=( ${@:-todo} ) REPLY
  IFS= read -r
  $cmd $REPLY
}

% runquoted print -rl
asoeu a-;ua;,. ua-h,.uch <- my input
asoeu a-;ua;,. ua-h,.uch
% runquoted
aosot
zsh: command not found: todo

-- 
Mikael Magnusson


  parent reply	other threads:[~2024-01-04 19:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-04 18:09 Sam B.
2024-01-04 18:55 ` Stephane Chazelas
2024-01-04 19:16 ` Mikael Magnusson [this message]
2024-01-04 23:38 ` Bart Schaefer
2024-01-05 17:53   ` Sam B.
2024-01-14 23:28     ` Sam B.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHYJk3TcGphoKMmH1n3hSVobpD7qtooyfGKCLtBhGM+kPYpMmA@mail.gmail.com \
    --to=mikachu@gmail.com \
    --cc=me@rmz.io \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).