[9fans] Acme Edit scriptlets

905 views
Skip to first unread message

Bence Fábián

unread,
Mar 28, 2013, 8:38:06 PM3/28/13
to
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.


bencef

phinea...@gmail.com

unread,
Mar 28, 2013, 9:19:38 PM3/28/13
to
> 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

unread,
Mar 29, 2013, 4:20:56 AM3/29/13
to
On Friday 29 of March 2013 01:38:06 Bence Fábián wrote:
> 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

unread,
Mar 29, 2013, 4:22:25 AM3/29/13
to
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

Peter A. Cejchan

unread,
Mar 29, 2013, 4:25:47 AM3/29/13
to
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

dexen deVries

unread,
Mar 29, 2013, 4:48:06 AM3/29/13
to
On Friday 29 of March 2013 09:25:47 Peter A. Cejchan wrote:
> 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

unread,
Mar 29, 2013, 5:50:51 AM3/29/13
to
> # 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

unread,
Mar 29, 2013, 12:20:12 PM3/29/13
to
this also works:

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

# outdent
Edit ,x/^ / c//

-Skip

Mark van Atten

unread,
Apr 4, 2013, 6:19:23 AM4/4/13
to
On Friday, 29 March 2013 01:38:06 UTC+1, Bence Fábián wrote:

> 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

dexen deVries

unread,
Apr 4, 2013, 8:08:02 AM4/4/13
to
On Thursday 04 of April 2013 10:19:23 Mark van Atten wrote:
> On Friday, 29 March 2013 01:38:06 UTC+1, Bence Fábián wrote:
> > 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.

Bence Fábián

unread,
Apr 4, 2013, 8:16:10 AM4/4/13
to
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"
}'




2013/4/4 Mark van Atten <vanatt...@gmail.com>

erik quanstrom

unread,
Apr 4, 2013, 8:28:14 AM4/4/13
to
On Thu Apr 4 08:17:13 EDT 2013, beg...@gmail.com wrote:

> 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

unread,
Apr 4, 2013, 9:04:38 AM4/4/13
to
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

unread,
Apr 4, 2013, 9:01:31 AM4/4/13
to
whoa. nice job.



2013/4/4 erik quanstrom <quan...@quanstro.net>

Bence Fábián

unread,
Apr 4, 2013, 9:17:38 AM4/4/13
to
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


2013/4/4 dexen deVries <dexen....@gmail.com>

Martin Kühl

unread,
Apr 5, 2013, 8:35:58 AM4/5/13
to
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
}

David Arroyo

unread,
Apr 5, 2013, 8:32:27 AM4/5/13
to
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]

dexen deVries

unread,
Apr 5, 2013, 8:50:25 AM4/5/13
to
thanks, this version supports using two Acmes, each in separate namespace :-)


--
dexen deVries

[[[↓][→]]]

Reply all
Reply to author
Forward
0 new messages