* Something like noglob to inhibit brace expansion? @ 2003-10-04 23:40 Lloyd Zusman 2003-10-05 10:50 ` DervishD 2003-10-05 16:21 ` Bart Schaefer 0 siblings, 2 replies; 8+ messages in thread From: Lloyd Zusman @ 2003-10-04 23:40 UTC (permalink / raw) To: zsh-users Is there something akin to the noglob precommand modifier that I can use to inhibit brace expansion? % alias pr='noglob print' % pr * * % pr {a,b,c,*} a b c * What I'd like to see is {a,b,c,*} I know that 'setopt ignorebraces' handles this in general, but it's not a precommand modifier, so I don't see how I can use it like this within an alias. Does anyone know of any tricks to get this functionality within an alias? Thanks in advance. -- Lloyd Zusman ljz@asfast.com ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Something like noglob to inhibit brace expansion? 2003-10-04 23:40 Something like noglob to inhibit brace expansion? Lloyd Zusman @ 2003-10-05 10:50 ` DervishD 2003-10-05 16:21 ` Bart Schaefer 1 sibling, 0 replies; 8+ messages in thread From: DervishD @ 2003-10-05 10:50 UTC (permalink / raw) To: Lloyd Zusman; +Cc: zsh-users Hi Lloyd :) * Lloyd Zusman <ljz@asfast.com> dixit: [IGNORE_BRACES] > Does anyone know of any tricks to get this functionality within > an alias? The best solution I can think of is to use the preexec function and set the option only for that command, then use precmd to reset the option. The problem is that I think that the command line gets expanded before calling preexec :((( Any zle widget, maybe? Raúl Núñez de Arenas Coronado -- Linux Registered User 88736 http://www.pleyades.net & http://raul.pleyades.net/ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Something like noglob to inhibit brace expansion? 2003-10-04 23:40 Something like noglob to inhibit brace expansion? Lloyd Zusman 2003-10-05 10:50 ` DervishD @ 2003-10-05 16:21 ` Bart Schaefer 2003-10-06 14:12 ` Lloyd Zusman 1 sibling, 1 reply; 8+ messages in thread From: Bart Schaefer @ 2003-10-05 16:21 UTC (permalink / raw) To: zsh-users On Oct 4, 7:40pm, Lloyd Zusman wrote: } } % pr {a,b,c,*} } a b c * } } What I'd like to see is {a,b,c,*} } } Does anyone know of any tricks to get this functionality within } an alias? It's a little ugly, but: alias pr='setopt noglob ignorebraces && _pr' _pr() { print $*; setopt glob braceexpand } You can get a little fancier if you want to preserve the exit status of 'print', but you get the idea. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Something like noglob to inhibit brace expansion? 2003-10-05 16:21 ` Bart Schaefer @ 2003-10-06 14:12 ` Lloyd Zusman 2003-10-06 15:43 ` Anthony R Iano-Fletcher 2003-10-06 17:01 ` Bart Schaefer 0 siblings, 2 replies; 8+ messages in thread From: Lloyd Zusman @ 2003-10-06 14:12 UTC (permalink / raw) To: zsh-users Bart Schaefer <schaefer@brasslantern.com> writes: > On Oct 4, 7:40pm, Lloyd Zusman wrote: > } > } % pr {a,b,c,*} > } a b c * > } > } What I'd like to see is {a,b,c,*} > } > } Does anyone know of any tricks to get this functionality within > } an alias? > > It's a little ugly, but: > > alias pr='setopt noglob ignorebraces && _pr' > _pr() { print $*; setopt glob braceexpand } > > You can get a little fancier if you want to preserve the exit status of > 'print', but you get the idea. Aha! Thank you. And I think that the following will take care of the exit code: alias pr='setopt noglob ignorebraces && _pr' _pr() { print $*; local rc=$?; setopt glob braceexpand; return $rc } Hmm ... or what about this as an even uglier alternative (one long line in real life)? alias pr='eval "preexec() { setopt braceexpand; preexec(){} }" && setopt ignorebraces && noglob print' There's even probably some sort of way to make a generic alias or function for this, which can then be applied to any command to generate an alias for it without command-line expansion. Next time I have trouble sleeping at night, I'll work on that one. :) Feature suggestion: how about adding an 'ignorebraces' precommand modifier in a future version of zsh? Or perhaps even something which would pass in the completely unglobbed, unexpanded, un-everything'ed command line. Or even more radically, how about an 'ignore' precommand modifier which takes options that can be grouped together, such as -b for braces, -g for glob, -v for variable expansion, etc.? -- Lloyd Zusman ljz@asfast.com ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Something like noglob to inhibit brace expansion? 2003-10-06 14:12 ` Lloyd Zusman @ 2003-10-06 15:43 ` Anthony R Iano-Fletcher 2003-10-06 17:01 ` Bart Schaefer 1 sibling, 0 replies; 8+ messages in thread From: Anthony R Iano-Fletcher @ 2003-10-06 15:43 UTC (permalink / raw) To: zsh-users Hello Lloyd Zusman wrote: > Feature suggestion: how about adding an 'ignorebraces' precommand > modifier in a future version of zsh? Or perhaps even something which > would pass in the completely unglobbed, unexpanded, un-everything'ed > command line. Or even more radically, how about an 'ignore' precommand > modifier which takes options that can be grouped together, such as -b > for braces, -g for glob, -v for variable expansion, etc.? I too have a much beloved command for which I would like to turn off any shell expansions, subsitutions, etc.. The 'ignore' suggestion sounds like a nice idea. Anthony. -- Anthony R Iano-Fletcher Anthony.Iano-Fletcher@nih.gov http://cbel.cit.nih.gov/~arif CBEL, CIT, NIH, Bethesda, MD, USA. Phone: (+1) 301 402 1741. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Something like noglob to inhibit brace expansion? 2003-10-06 14:12 ` Lloyd Zusman 2003-10-06 15:43 ` Anthony R Iano-Fletcher @ 2003-10-06 17:01 ` Bart Schaefer 2003-10-06 17:19 ` Lloyd Zusman 1 sibling, 1 reply; 8+ messages in thread From: Bart Schaefer @ 2003-10-06 17:01 UTC (permalink / raw) To: zsh-users On Oct 6, 10:12am, Lloyd Zusman wrote: } } Hmm ... or what about this as an even uglier alternative (one long } line in real life)? } } alias pr='eval "preexec() { setopt braceexpand; preexec(){} }" && } setopt ignorebraces && noglob print' No, this won't work -- preexec is executed once before each interactive command _line_ is executed, not before every individual command in any pipeline or list. So in that example, preexec won't be defined until after it would have executed. It'll execute before the _next_ command, but by that time it's too late -- parsing of that next command is over, and won't have used braceexpand. You could do it with precmd instead of preexec. } There's even probably some sort of way to make a generic alias or } function for this, which can then be applied to any command You could always do something like this -- modeled on "ttyctl": function optctl { case $1 in -f) if [[ -o localoptions ]] then print -u2 optctl: cannot freeze with LOCAL_OPTIONS set return 1 fi zmodload -i zsh/parameter || return 1 typeset -agH _optctl_opts _optctl_opts=( ${(kv)options[@]} ) ;; -u) unset _optctl_opts ;; '') print options are ${${_optctl_opts:+frozen}:-not frozen} ;; *) print -u2 optctl: too many arguments return 1 ;; esac } precmd() { [[ -n ${_optctl_opts:-} ]] && options=( ${_optctl_opts[@]} ) # ... whatever you usually have in precmd ... } With the caveat, of course, that precmd itself can't use LOCAL_OPTIONS if you want this to work. (The extra :- there is so that NO_UNSET does not cause zsh to complain.) } Feature suggestion: how about adding [...] an 'ignore' precommand } modifier which takes options that can be grouped together, such as -b } for braces, -g for glob, -v for variable expansion, etc.? That's an interesting idea, though it may be very difficult to manage it for those expansions that don't already have options. Expansions introduced with '$' in particular are tied to parsing at several levels, because the presence of '${' or '$(' changes the parse of everything that follows, up to the matching '}' or ')'. E.g., if command substitution is turned off but globbing isn't, '$(...)' ought to become interpretable as a glob with qualifiers; but by the time zsh gets around to discovering that the 'noglob' et al. builtins are on the command line, it's already been parsed as a command list. It's easy to turn a glob pattern back into a plain string, but quite difficult to turn a command list into a glob pattern. That's also why ignorebraces is an option rather than a precommand; the parser has to be changed much earlier. We'd have to implement "ignore" as a reserved word like "nocorrect", in which case it can't take options. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Something like noglob to inhibit brace expansion? 2003-10-06 17:01 ` Bart Schaefer @ 2003-10-06 17:19 ` Lloyd Zusman 2003-10-06 19:05 ` Bart Schaefer 0 siblings, 1 reply; 8+ messages in thread From: Lloyd Zusman @ 2003-10-06 17:19 UTC (permalink / raw) To: zsh-users Bart Schaefer <schaefer@brasslantern.com> writes: > On Oct 6, 10:12am, Lloyd Zusman wrote: > } > } Hmm ... or what about this as an even uglier alternative (one long > } line in real life)? > } > } alias pr='eval "preexec() { setopt braceexpand; preexec(){} }" && > } setopt ignorebraces && noglob print' > > No, this won't work -- preexec is executed once before each interactive > command _line_ is executed, not before every individual command in any > pipeline or list. So in that example, preexec won't be defined until > after it would have executed. It'll execute before the _next_ command, > but by that time it's too late -- parsing of that next command is over, > and won't have used braceexpand. That's the idea. I want it to execute before the _next_ command (in other words, _after_ the specified command executes), because it turns braceexpand back _on_ after it was turned off for that one, single command. > You could do it with precmd instead of preexec. > > } There's even probably some sort of way to make a generic alias or > } function for this, which can then be applied to any command > > [ ... etc. ... ] Thank you. I'm going to play with this. Fun! > [ ... ] > > } Feature suggestion: how about adding [...] an 'ignore' precommand > } modifier which takes options that can be grouped together, such as -b > } for braces, -g for glob, -v for variable expansion, etc.? > > That's an interesting idea, though it may be very difficult to manage > it for those expansions that don't already have options. Expansions > introduced with '$' in particular are tied to parsing at several levels, > because the presence of '${' or '$(' changes the parse of everything > that follows, up to the matching '}' or ')'. > > [ ... ] I understand your cogent explanation. So maybe we could just have a precommand modifier called 'raw' or something similar, which just passes in the command line with no expansions whatsoever. The program or function could do its own parsing in that case. Hmm ... isn't one of the three arguments passed to preexec already the raw, unexpanded command? If so, is there a way to use preexec to modify the command line that is about to be executed? -- Lloyd Zusman ljz@asfast.com ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Something like noglob to inhibit brace expansion? 2003-10-06 17:19 ` Lloyd Zusman @ 2003-10-06 19:05 ` Bart Schaefer 0 siblings, 0 replies; 8+ messages in thread From: Bart Schaefer @ 2003-10-06 19:05 UTC (permalink / raw) To: zsh-users On Oct 6, 1:19pm, Lloyd Zusman wrote: } } Hmm ... isn't one of the three arguments passed to preexec already the } raw, unexpanded command? If so, is there a way to use preexec to modify } the command line that is about to be executed? There is not. We considered that, but my experience with a similar hook in the zmail scripting language led me to recommend against it. It's much too easy to get yourself into a state where the shell doesn't work at all because a typo in preexec breaks or disables everything. However, you could do this: preexec() { setopt localoptions noksharrays typeset -gH _DONE_=0 local -a cmd cmd=( ${(z)1} ) if [[ $cmd[1] == raw ]] then if [[ $cmd[2] == '{' && $cmd[-1] == '}' ]] then _DONE_=1 eval $cmd[3] "${(@q)cmd[4,-2]}" elif [[ -z ${(M)cmd#([;|&]|&&|\{|\|\|)} ]] then _DONE_=1 eval $cmd[2] "${(@q)cmd[3,-1]}" else print -u2 raw: syntax error: all bets are off fi fi } alias raw='((_DONE_)) ||' This allows you to write stuff like raw echo this must be a simple $(command) but is *not* expanded raw { echo this must parse OK but is $(not expanded); echo see? } But note that echo raw has no effect ; raw echo in the middle like this ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2003-10-06 19:05 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2003-10-04 23:40 Something like noglob to inhibit brace expansion? Lloyd Zusman 2003-10-05 10:50 ` DervishD 2003-10-05 16:21 ` Bart Schaefer 2003-10-06 14:12 ` Lloyd Zusman 2003-10-06 15:43 ` Anthony R Iano-Fletcher 2003-10-06 17:01 ` Bart Schaefer 2003-10-06 17:19 ` Lloyd Zusman 2003-10-06 19:05 ` Bart Schaefer
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).