Discussion:
[9fans] Acme Edit scriptlets
(too old to reply)
Bence Fábián
2013-03-29 00:38:06 UTC
Permalink
Hi!

I did a quick writeup on little Edit scripts
(well basicly sam(1) scripts)
If anyone have more feel free to contribute.
Maybe someone could put them on the wiki even.

http://bencef.com/blog/4/

bencef
p***@gmail.com
2013-03-29 01:19:38 UTC
Permalink
Post by Bence Fábián
Hi!
I did a quick writeup on little Edit scripts
(well basicly sam(1) scripts)
If anyone have more feel free to contribute.
Maybe someone could put them on the wiki even.
Nice idea...

Here's a simple awk bit I use in Acme for centering text (its name is
the center-line rune: ℄)

#!/bin/rc
# Center text
awk '{l=length();s=int((70-l)/2); printf "%"(s+l)"s\n",$0}'

It doesn't seem to be common practice, but I like to name editing
commands with unicode runes to save room: i.e. ← and → for
indentation (used with code), or ⇐ and ⇒ for indentation + a reformat
(used for natural language next). I find they serve a bit like icons.
Peter A. Cejchan
2013-03-29 08:22:25 UTC
Permalink
Thanks, very nice, mates!
I humbly add some of minebelow.
Best, ++pac

#####
## Latin
ÀëïñöÌÿÄÅËÏÖÜ

## Greek
αβγΎεζηΞλΌΜΟπρστφψωΓΔΘΛΞΠΣΊΚΩ

## select text

:; # select all text
:;25 # select from start to line 25 (inclusive)
:25; # select from line 25 (inclusive) to EOF
Edit /;[ ]*\/\// # select from ; to // comments


## edit text
Edit s/^/ /g # increase indentation
Edit s,^ ,,g # decrease indentation
Edit s/^/\/\/ /g #comment out using //

Edit s/\n\n\n+/\n\n/g # remove redundant newlines, keep max two
Edit s/^[ ]+//g # remove leading whitespace
Edit s/[ ]+$//g # remove trailing whitespace
Edit s/ +/ /g # remove multiple spaces
Edit s/;$//g # remove trailing semicolon
Edit s/\*+\///g # comments
Edit s/\/\*+/\/\//g
Edit s/[\(\)]/ /g # remove ()
Edit s/.*/(&)/g # add ()
Edit s/.*/float64(&)/g # float64()
Edit s/.*/} & {/g # add } {
Edit s/^/\/\/ /g # // comment out
Edit /;[ ]*\/\// Edit s/;// # find and remove semicolon before //
comments
Edit s/\+\+[a-zA-Z]+[0-9a-zA-Z]*/&++/ Edit s/\+\+/d # NOT WORKING prefix
to postfix operator
Edit s/->/./g # struct pointer

Edit ,s/\+\+([A-Za-z]+[A-Za-z0-9]*)/\1++/g# prefix to postfix operator: ++
Edit ,s/\-\-([A-Za-z]+[A-Za-z0-9]*)/\1--/g# prefix to postfix operator: --

# prefix to postfix operator: ++i --> i++
Edit /\+\+[a-zA-Z_]+[0-9a-zA-Z_]*/{
x/\+\+/d
a/++/
}

Edit s/\+\+([A-Za-z]+[A-Za-z0-9]*)/\1++/ # prefix to postfix operator: ++i
--> i++

| 9 sed 's/\(//; s/(.*)\)/\1/' # remove outermost pair of parentheses
Edit s:\((.*)\):\1:g # remove outermost pair of parentheses
Post by p***@gmail.com
Post by Bence Fábián
Hi!
I did a quick writeup on little Edit scripts
(well basicly sam(1) scripts)
If anyone have more feel free to contribute.
Maybe someone could put them on the wiki even.
Nice idea...
Here's a simple awk bit I use in Acme for centering text (its name is
the center-line rune: ℄)
#!/bin/rc
# Center text
awk '{l=length();s=int((70-l)/2); printf "%"(s+l)"s\n",$0}'
It doesn't seem to be common practice, but I like to name editing
commands with unicode runes to save room: i.e. ← and → for
indentation (used with code), or ⇐ and ⇒ for indentation + a reformat
(used for natural language next). I find they serve a bit like icons.
dexen deVries
2013-03-29 08:20:56 UTC
Permalink
Post by Bence Fábián
I did a quick writeup on little Edit scripts
(well basicly sam(1) scripts)
If anyone have more feel free to contribute.
Maybe someone could put them on the wiki even.
stuff i use:

# clear whole window -- usefull with +Errors
Edit ,d

# decrease TAB indentation of selection
Edit s,^TAB,,g


# increase TAB indentation of selection
# the ^. part ensures we indent only lines with content
# and leave empty lines undisturbed
Edit s,^.,TAB&,g
--
dexen deVries

[[[↓][→]]]
Peter A. Cejchan
2013-03-29 08:25:47 UTC
Permalink
Maybe it's time for an Acme wiki page?
Also, could you share the plumbing rules you use (for my
inspiration/learning)?

Happy Easter, folks!
++pac
Post by dexen deVries
Post by Bence Fábián
I did a quick writeup on little Edit scripts
(well basicly sam(1) scripts)
If anyone have more feel free to contribute.
Maybe someone could put them on the wiki even.
# clear whole window -- usefull with +Errors
Edit ,d
# decrease TAB indentation of selection
Edit s,^TAB,,g
# increase TAB indentation of selection
# the ^. part ensures we indent only lines with content
# and leave empty lines undisturbed
Edit s,^.,TAB&,g
--
dexen deVries
[[[↓][→]]]
dexen deVries
2013-03-29 08:48:06 UTC
Permalink
Post by Peter A. Cejchan
Also, could you share the plumbing rules you use (for my
inspiration/learning)?
1) re-format PHP's strange error mesages into standard
FILE_PATHNAME:LINE_NUMBER


# ... called in FILE_PATHNAME on line LINE_NUMBER and defined in FILE_PATHNAME
on line LINE_NUMBER

data matchesmultiline '.*rror.*called in ([^ ]+) on line ([0-9]+) and defined
in ([^ ]+) on line ([0-9]+).*'
arg isfile $1
data set $file
attr add addr=$2
type is text
plumb to edit


#file / line in PHP format
data matchesmultiline '(.+) on line ([0-9]+).*'
arg isfile $1
data set $file
attr add addr=$2
type is text
plumb to edit


2) display php's function prototypes on right-click on a function name with an
opening parenthesis. the `W' script greps a flat text file list of functions
(with arguments and return types) and outputs to +Errors window.


type is text
data matches '[a-zA-Z_][a-zA-Z_0-9]*[(]'
plumb start W --wdir $wdir $data


* * *

a half-hearted support for displaying SQL table schema; again, `Wtable' is a
script outputting definition of indicated table.

type is text
data matches '.*(FROM|JOIN)[ ]+([^ ]+).*'
data set $2
plumb start Wtable --wdir $wdir $data
--
dexen deVries

[[[↓][→]]]
Richard Miller
2013-03-29 09:50:51 UTC
Permalink
Post by dexen deVries
# increase TAB indentation of selection
# the ^. part ensures we indent only lines with content
# and leave empty lines undisturbed
Edit s,^.,TAB&,g
Very nice.
Skip Tavakkolian
2013-03-29 16:20:12 UTC
Permalink
this also works:

# indent
Edit ,x/^./ y/./ c/ /

# outdent
Edit ,x/^ / c//

-Skip
Post by Richard Miller
Post by dexen deVries
# increase TAB indentation of selection
# the ^. part ensures we indent only lines with content
# and leave empty lines undisturbed
Edit s,^.,TAB&,g
Very nice.
Mark van Atten
2013-04-04 10:19:23 UTC
Permalink
Post by Bence Fábián
I did a quick writeup on little Edit scripts
Many thanks, this thread is very useful.

There is also Jason Catena's list of Edit idioms at
https://raw.github.com/catenate/acme-fonts/master/test/1/acme/Edit/sam

When editing and re-editing latex, I regularly pipe selections
through a simple-minded script called `chunk' which does most of
the work for obtaining semantic linebreaks. That goes back to a
recommendation by Kernighan in his paper `Unix for beginners' of
1974; see the quotation, comments and link at [1].



#!/usr/local/plan9/bin/rc
# chunk up (to prepare) for semantic linebreaks

# do not break within \cite
# do not break within $$ math
# break after closing parentheses ),]
# break before an opening parentheses (,[

ssam -e 'x/(^[^%].+\n)+/ y/\\cite[^{]*{(\n|.)*}/ y/\$.*\$/
x/(([^A-Z]\.)|[,;:!?]|\)|\]) | (\(|\[)/ s/ /\n/' \ | 9 fmt -w 60
-j


For batch processing probably something more sophisticated would
be needed to leave various environments unchunked. But I don't use
it that way, and just apply it to selections where I know its use
makes sense. Usually these are areas where I have just been doing
a lot of rewriting.

There's no point in chunking up commented material, and sometimes
it is actually convenient to have a place where I can keep things
unchunked for reference.

The original chunk command in Writer's Workbench [2], for troff not
latex, was based on a parser for English, I think. I find I don't
want that (because I write in other languages as well), and that
even in English I don't need it (because the chunking based on
interpunction is always fine with me, and where I care about the
remaining cases, I prefer to do it myself; but see [3]).

Mark.


[1] http://rhodesmill.org/brandon/2012/one-sentence-per-line/

[2] http://man.cat-v.org/unix_WWB/1/chunk

[3] https://github.com/waldir/semantic-linebreaker
Bence Fábián
2013-04-04 12:16:10 UTC
Permalink
Cool.


Here's a script i use to generate case
insensitive regexes. It turns

FooBar

into

[Ff][Oo][Oo][Bb][Aa][Rr]

term% cat /bin/uncase
#!/bin/rc

exec awk '{
lower = tolower($0)
upper = toupper($0)
len = length($0)

for( i = 1 ; i <= len ; i++ )
printf "[" substr(upper, i, 1) substr(lower, i, 1) "]"
printf "\n"
}'
Post by Mark van Atten
Post by Bence Fábián
I did a quick writeup on little Edit scripts
Many thanks, this thread is very useful.
There is also Jason Catena's list of Edit idioms at
https://raw.github.com/catenate/acme-fonts/master/test/1/acme/Edit/sam
When editing and re-editing latex, I regularly pipe selections
through a simple-minded script called `chunk' which does most of
the work for obtaining semantic linebreaks. That goes back to a
recommendation by Kernighan in his paper `Unix for beginners' of
1974; see the quotation, comments and link at [1].
#!/usr/local/plan9/bin/rc
# chunk up (to prepare) for semantic linebreaks
# do not break within \cite
# do not break within $$ math
# break after closing parentheses ),]
# break before an opening parentheses (,[
ssam -e 'x/(^[^%].+\n)+/ y/\\cite[^{]*{(\n|.)*}/ y/\$.*\$/
x/(([^A-Z]\.)|[,;:!?]|\)|\]) | (\(|\[)/ s/ /\n/' \ | 9 fmt -w 60
-j
For batch processing probably something more sophisticated would
be needed to leave various environments unchunked. But I don't use
it that way, and just apply it to selections where I know its use
makes sense. Usually these are areas where I have just been doing
a lot of rewriting.
There's no point in chunking up commented material, and sometimes
it is actually convenient to have a place where I can keep things
unchunked for reference.
The original chunk command in Writer's Workbench [2], for troff not
latex, was based on a parser for English, I think. I find I don't
want that (because I write in other languages as well), and that
even in English I don't need it (because the chunking based on
interpunction is always fine with me, and where I care about the
remaining cases, I prefer to do it myself; but see [3]).
Mark.
[1] http://rhodesmill.org/brandon/2012/one-sentence-per-line/
[2] http://man.cat-v.org/unix_WWB/1/chunk
[3] https://github.com/waldir/semantic-linebreaker
erik quanstrom
2013-04-04 12:28:14 UTC
Permalink
Post by Bence Fábián
Cool.
Here's a script i use to generate case
insensitive regexes. It turns
FooBar
into
[Ff][Oo][Oo][Bb][Aa][Rr]
see also rune(1), http://9atom.org/magic/man2html/1/rune
which generalizes this idea to all of unicode (rune/case),
and also to diacritical and other markers (rune/fold; rune/unfold).
for the latter also see grep(1)'s -I flag, http://9atom.org/magic/man2html/1/grep

- erik
Bence Fábián
2013-04-04 13:01:31 UTC
Permalink
whoa. nice job.
Post by erik quanstrom
Post by Bence Fábián
Cool.
Here's a script i use to generate case
insensitive regexes. It turns
FooBar
into
[Ff][Oo][Oo][Bb][Aa][Rr]
see also rune(1), http://9atom.org/magic/man2html/1/rune
which generalizes this idea to all of unicode (rune/case),
and also to diacritical and other markers (rune/fold; rune/unfold).
for the latter also see grep(1)'s -I flag,
http://9atom.org/magic/man2html/1/grep
- erik
dexen deVries
2013-04-04 13:04:38 UTC
Permalink
an Edit script, or an Rc script for Acme, to close all windows which names
start with given (literal) prefix.

use case: several files and directories of two projects open in one Acme
instance. want to close all windows related to one of the projects, and leave
the other project's windows open.
--
dexen deVries

[[[↓][→]]]
Bence Fábián
2013-04-04 13:17:38 UTC
Permalink
for (dir in `{grep -l '^'$pattern /mnt/acme/[0-9]*/tag | sed 's/tag//'}){
echo delete >$dir/ctl
}

where $pattern is the pattern you want to match
Post by dexen deVries
an Edit script, or an Rc script for Acme, to close all windows which names
start with given (literal) prefix.
use case: several files and directories of two projects open in one Acme
instance. want to close all windows related to one of the projects, and leave
the other project's windows open.
--
dexen deVries
[[[↓][→]]]
Martin Kühl
2013-04-05 12:35:58 UTC
Permalink
or using 9p(1):

for (num in `{9p ls acme | grep '^[0-9]'}) {
if (9p read acme/$num/tag | grep -s '^'$pattern)
echo delete | 9p write acme/$num/ctl
}
dexen deVries
2013-04-05 12:50:25 UTC
Permalink
Post by Martin Kühl
for (num in `{9p ls acme | grep '^[0-9]'}) {
if (9p read acme/$num/tag | grep -s '^'$pattern)
echo delete | 9p write acme/$num/ctl
}
thanks, this version supports using two Acmes, each in separate namespace :-)
--
dexen deVries

[[[↓][→]]]
David Arroyo
2013-04-05 12:32:27 UTC
Permalink
I took the template.awk script from werc[0] and use it in acme all the
time. I've a collection of template files beginning with

Edit ,|tpl
% var1=val1
% var2=val2
...

I can execute line 1 to generate stuff like Makefiles, man pages, puppet
manifests, etc.[1]

[0]: http://hg.cat-v.org/werc/file/50a9b770bb43/bin/template.awk#l1
[1]: http://aqwari.us/notes/werctpl
Post by Bence Fábián
whoa. nice job.
Post by erik quanstrom
Post by Bence Fábián
Cool.
Here's a script i use to generate case
insensitive regexes. It turns
FooBar
into
[Ff][Oo][Oo][Bb][Aa][Rr]
see also rune(1), http://9atom.org/magic/man2html/1/rune
which generalizes this idea to all of unicode (rune/case),
and also to diacritical and other markers (rune/fold; rune/unfold).
for the latter also see grep(1)'s -I flag,
http://9atom.org/magic/man2html/1/grep
- erik
dexen deVries
2013-04-04 12:08:02 UTC
Permalink
Post by Bence Fábián
I did a quick writeup on little Edit scripts
(p9p specific)

attached is my dirty hack for automagic grepping of $% file or recursively %s
dir or pipe.

a funky goodie:
automatically supplies `.' (dot) between arguments, so for example:

$ G some token here

becomes `grep some.token.here'
--
dexen deVries

[[[↓][→]]]


``we, the humanity'' is the greatest experiment we, the humanity, ever
undertook.
Loading...