Discussion:
[9fans] acme: send dot to the stdin of a more complicated command
(too old to reply)
Rudolf Sykora
2009-06-08 12:51:46 UTC
Permalink
Hello everyone,

when I want to process a dot's contents in acme I can use the '>' syntax, e.g.

have
awk '{print}'
in a window, select it, and then 2-1 click on Edit in the window with
my dot. That works.
But what shall I do when the awk script is more complicated, in the
simplest case like
awk '
{print}
'

When I select it all and do the same as before, it doesn't work
(probably because of the new lines?)...

Thanks
Ruda
Lyndon Nerenberg
2009-06-08 16:07:27 UTC
Permalink
Post by Rudolf Sykora
But what shall I do when the awk script is more complicated, in the
simplest case
Put the awk code into a file and execute '> awk -f foo' in acme.
Rudolf Sykora
2009-06-08 16:36:59 UTC
Permalink
Post by Lyndon Nerenberg
Put the awk code into a file and execute '> awk -f foo' in acme.
well, I hoped this would be the last way... It makes me create files I
don't actually need.
Is it the newlines that causes troubles?

Thanks
Ruda
andrey mirtchovski
2009-06-08 17:01:10 UTC
Permalink
Post by Rudolf Sykora
well, I hoped this would be the last way... It makes me create files I
don't actually need.
remember, this is plan9 and everything is a file. chances are your
"script" is already available in some filesystem and you don't need to
write it out:: create a new window inside acme, type your awk script
and then issue ">awk -f /mnt/wsys/X/body" where X is the ID of your
window.

you'll need to figure out what the new window's ID is, but that's not
too difficult, simply middle-click something like this after you've
created the script window:

grep -l foo /mnt/wsys/[0-9]*/body

where 'foo' is some unique part of your script.
Rudolf Sykora
2009-06-08 17:32:43 UTC
Permalink
Post by andrey mirtchovski
remember, this is plan9 and everything is a file. chances are your
"script" is already available in some filesystem and you don't need to
write it out:: create a new window inside acme, type your awk script
and then issue ">awk -f /mnt/wsys/X/body" where X is the ID of your
window.
well, though an inspiring idea, it doesn't sound to be much practical:
1) I usually have a special window in which I have many commands. I
then select the one needed and chord it to the appropriate window
(i.e. I don't use the whole contents of a window).
2) sometimes I have more such windows.

The very question for me now is: why it behaves how it behaves, i.e
why newlines (if it's them) are problematic.

Ruda
andrey mirtchovski
2009-06-08 17:37:51 UTC
Permalink
Post by Rudolf Sykora
The very question for me now is: why it behaves how it behaves, i.e
why newlines (if it's them) are problematic.
and you want somebody do look through the code and figure it out for you?
Rudolf Sykora
2009-06-08 17:47:27 UTC
Permalink
Post by andrey mirtchovski
and you want somebody do look through the code and figure it out for you?
not really. I wanted to know whether
1) somebody thought about it (knowing the system has been around for
some time I'd expect somebody must have had the same problem)
2) there is any good reason why it behaves so.

Ruda
yy
2009-06-08 17:45:05 UTC
Permalink
Post by Rudolf Sykora
The very question for me now is: why it behaves how it behaves, i.e
why newlines (if it's them) are problematic.
Ruda
They are the only way Edit has to separate commands. You will notice
that you cannot use something like i/A/a/W/ (or i/A/;a/W/, for
example). However, you can chord something like
i/A/
a/W/
When you chord your example, acme calls rc with: rc -c 'awk '', so it
does not work. rc is who interprets those multiple-line commands. You
could use rc functions if that makes you feel better than with plain
files, just remember to prepend the function name with a semi-colon,
to force rc to interpret the command. At least that is how I remember
it, please somebody correct me if I am wrong.
--
- yiyus || JGL .
Russ Cox
2009-06-08 18:18:28 UTC
Permalink
Post by Rudolf Sykora
1) I usually have a special window in which I have many commands. I
then select the one needed and chord it to the appropriate window
(i.e. I don't use the whole contents of a window).
2) sometimes I have more such windows.
This is an interesting usage model. I've never seen it before.
The power of acme is that you can extend it with external
programs. The script below implements this usage; I called it Run.
You can type and select your command in one window, with a name matching
pattern, and then in the other window's tag execute >Run pattern.
Run finds the window with a title matching pattern, pulls out the
selected text, and runs it through rc.

See Loading Image... for an illustration.

Russ


#!/bin/rc

if(! ~ $#* 1) {
echo 'usage: Run title' >[1=2]
exit usage
}

id=`{awk -v 'pat='$1 '$6 ~ pat {print $1}' /mnt/wsys/index}
if(~ $#id 0) {
echo 'no match for pattern' >[1=2]
exit none
}
if(! ~ $#id 1) {
echo 'ambiguous pattern' >[1=2]
exit ambiguous
}
if(~ `{wc -w /mnt/wsys/$id/rdsel} 0) {
echo 'no command selected' >[1=2]
exit missing
}
exec rc /mnt/wsys/$id/rdsel
Dan Cross
2009-06-08 19:01:48 UTC
Permalink
Post by Russ Cox
The script below implements this usage; I called it Run.
You can type and select your command in one window, with a name matching
pattern, and then in the other window's tag execute >Run pattern.
Run finds the window with a title matching pattern, pulls out the
selected text, and runs it through rc.
See http://swtch.com/~rsc/acme-Run.png for an illustration.
Wow, cool. Could you copy this into /acme/bin on sources?

- Dan C.
Skip Tavakkolian
2009-06-08 19:02:44 UTC
Permalink
Post by Russ Cox
See http://swtch.com/~rsc/acme-Run.png for an illustration.
nice!
Rudolf Sykora
2009-06-09 08:33:58 UTC
Permalink
Post by Russ Cox
See http://swtch.com/~rsc/acme-Run.png for an illustration.
Russ
Thank you much!
This is what I need and now I see how it can be achieved...
:)

Ruda
Rudolf Sykora
2013-08-28 08:05:14 UTC
Permalink
Hello,

I tried to modify Russ' script below to be usable on p9p. I came up with
------------------------------------
#!/usr/local/plan9/bin/rc

if(! ~ $#* 1) {
echo 'usage: Run title' >[1=2]
exit 1
}

id=`{awk -v 'pat='$1 '$6 ~ pat {print $1}' <{9p read acme/index}}
if(~ $#id 0) {
echo 'no match for pattern' >[1=2]
exit 2
}
if(! ~ $#id 1) {
echo 'ambiguous pattern' >[1=2]
exit 3
}
if(~ `{wc -w <{9p read acme/$id/rdsel}} 0) {
echo 'no command selected' >[1=2]
exit 4
}
#exec cat <{9p read acme/$id/rdsel}
exec /usr/local/plan9/bin/rc <{9p read acme/$id/rdsel}
------------------------------------

but the last line doesn't really do what I want, yielding the error:
Run: exit 1
rc: /dev/fd/5:2: token EOF: syntax error

can anybody help me with what is wrong?
The commented-out line with cat prints correctly what I have
selected in the other window (see the description of the original
script), e.g.
awk '{print $2}'
Thank you!
Ruda
Post by Rudolf Sykora
1) I usually have a special window in which I have many commands. I
then select the one needed and chord it to the appropriate window
(i.e. I don't use the whole contents of a window).
2) sometimes I have more such windows.
This is an interesting usage model. I've never seen it before.
The power of acme is that you can extend it with external
programs. The script below implements this usage; I called it Run.
You can type and select your command in one window, with a name matching
pattern, and then in the other window's tag execute >Run pattern.
Run finds the window with a title matching pattern, pulls out the
selected text, and runs it through rc.
See http://swtch.com/~rsc/acme-Run.png for an illustration.
Russ
#!/bin/rc
if(! ~ $#* 1) {
echo 'usage: Run title' >[1=2]
exit usage
}
id=`{awk -v 'pat='$1 '$6 ~ pat {print $1}' /mnt/wsys/index}
if(~ $#id 0) {
echo 'no match for pattern' >[1=2]
exit none
}
if(! ~ $#id 1) {
echo 'ambiguous pattern' >[1=2]
exit ambiguous
}
if(~ `{wc -w /mnt/wsys/$id/rdsel} 0) {
echo 'no command selected' >[1=2]
exit missing
}
exec rc /mnt/wsys/$id/rdsel
dexen deVries
2013-08-28 12:13:49 UTC
Permalink
This post might be inappropriate. Click to display it.
Rudolf Sykora
2013-08-28 12:53:12 UTC
Permalink
Post by dexen deVries
your selection lacks the final LF to make Rc happy ;-)
exec /usr/local/plan9/bin/rc <{9p read acme/$id/rdsel;echo;}
echo foo bar
rc <{9p read acme/$winid/rdsel; echo; }
thanks, this helped, now it works :)
Ruda


P.S.: correction: in my previous e-mail where I write
Post by dexen deVries
awk '{print $2}'
I actually meant just
awk '{print $2}'
(ie. without the initial >)
James A. Robinson
2013-08-28 15:11:42 UTC
Permalink
Post by dexen deVries
your selection lacks the final LF to make Rc happy ;-)
exec /usr/local/plan9/bin/rc <{9p read acme/$id/rdsel;echo;}
echo foo bar
rc <{9p read acme/$winid/rdsel; echo; }
selecting bare `echo foo bar' (without LF) gives syntax error; selecting whole
line works a-OK
A small variation of this would be to allow for
multiple arguments to Run, following the label,
and to shift the label off the args and pass the
rest to the executing command.

For my own copy of this I read the contents
of rdsel into a separate file and execute it
with any following args (I'm using sh, but I
assume similar functionality is available in
rc).

Jim

Loading...