Re: [dev] switching to rc: filename expansion ?

From: Teodoro Santoni <asbrasbra_AT_gmail.com>
Date: Mon, 25 Nov 2019 22:04:11 +0000

Hi marc!

2019-11-25 21:23 GMT, Marc Chantreux <eiro_AT_phear.org>:
> hello people,
>
> some of the things i really enjoy in zsh comes from rc
> (setopt rcquotes, the ^ expansion, the alternative syntax
> for constrol structures that are close to rc, ...) but
> both the codebase and the binary are very large.
>
> i'm experimenting replacing zsh by rc when the rc
> counterpart is good enough. but it seems i reach the
> limits of rc very soon. example
>
> in an mkfile, i can write things like that
>
> PUBLIC = `{ echo *.md }
> PAGES = ${PUBLIC:%.md=pub/%.html}
>
> in zsh, i can write
>
> ls *.md
> set -- *.md(:r) # (:r) will remove the tailing .md
> pages=( pub/$^_AT_.html )
> print -l $pages
>
> as a result, i get
>
> l'autre.md
> this is.md
> pub/l'autre.html
> pub/this is.html
>
> there is nothing like the ':' modifiers or ${p%.md}
> alike expansions in rc so i try to figure out what
> would be an idiomatic way to do that in rc.
>
> i tried
>
> pages=(`{ ls *.md | sed 's/md$/html/; s/md''/html''/' })
> echo pub/^$pages
>
> which gives me
>
> pub/l'autre.html pub/this pub/is.html
>
> close ... but wrong :) i can add klugdes around it but i
> feel i missed something. Any advice when it comes to applying
> a transformation to a list with rc?
>
> regards
> marc
>
>
>

I forgot, then remembered this old trick from the Rc paper, by Tom Duff [1]:

[dank_AT_meme ~]$ rc
% cd /tmp
% pwd
/tmp
% mkdir pub
% cd pub
% touch 'l''autre.md'
% touch 'this is.md'
% cd ..
% mv pub/*md .
% pages=`{ls *.md | sed 's/\.md$/.html/g'}
% echo pub/^$pages
pub/l'autre.html pub/this pub/is.html
% printf 'pub/%s ' $pages
pub/l'autre.html pub/this pub/is.html %
%
% printf 'pub/%s ' $"pages
pub/l'autre.html this is.html %
% echo $pages(1)
l'autre.html
% echo $pages(2)
this
% # ah yep, i'm dump
% # dumb*
% ifs='
'
% pages=`{ls *.md | sed 's/\.md$/.html/g'}
% echo pub/^$pages
pub/l'autre.html pub/this is.html
%

Simply set ifs to the newline during the expansion of your files and
you're done.

[1]: http://doc.cat-v.org/plan_9/4th_edition/papers/rc
Received on Mon Nov 25 2019 - 23:04:11 CET

This archive was generated by hypermail 2.3.0 : Mon Nov 25 2019 - 23:12:09 CET