#!/bin/rc # set up a bunch of funtions in sam # run with ! at the beginning of your sam session # !b cmd # run a command, not a fn fn b{builtin $*} # ^+ [n] # increase indent fn +{ if(~ $%dot(2) $%dot(3)) echo , if(~ $1 [1-9]) echo 'x/^/i/'^`''{seq $1 | tr -d \xa | tr 1-9 ' '} if not echo 'x/^/i/ /' } # ^- [n] # decrease indent fn -{ if(~ $%dot(2) $%dot(3)) echo , if(~ $1 [1-9]) echo 'x/^'^`''{seq 9 | tail -$1 | tr -d \xa | tr 1-9 ' '}^/d if not echo 'x/^ /d' } # ^c [args] # test build and run for C programs fn c{ rfork e if(! ~ $% *.[ch]){ echo !echo not a c file! exit } echo w eval `{grep '^O=.$' /$objtype/mkfile} targ = $O.out d = `{basename -d $%} cd $d if(! mk -n $targ >/dev/null >[2=1]) targ = `{echo $% | sed 's•(.*/)?(.*)\.c$•'^$O^'.\2•'} b w c $d ';' mk $targ '&&' $targ $* } # |calc # inline arithmetic fn calc{pc -n | tr -d $nl} # ^d [file] # save unwritten changes and dump sam state to file fn d{ rfork e switch($#*){ case 0 dump = /tmp/sam.dump case 1 dump = $1 case * echo !u d exit } d = `{pwd} echo 'X/^''/w' echo !rm -f $dump echo 'Y/^..\./!echo `{cleanname -d' $d '$%}^'':#''^$%dot(2)^'',#''^$%dot(3) >>' $dump echo 'X/^..\./!echo `{cleanname -d' $d '$%}^'':#''^$%dot(2)^'',#''^$%dot(3) >>' $dump } # _f # jump to definition/print info for selected C function fn f{ rfork e name = `{read} if(! ~ $#name 1){ echo !echo select a function name first! exit } cd `{basename -d $%} match = `{g '^'^$name^'\(' | sed '1{s/: .*//;q;}'} if(~ $match ''){ echo !man -w 2 $name '| sed ''s†.*/(.*)†\1(2)†''' echo !sig $name } if not plumb $match } # !l [file] # load dump file fn l{ switch($#*){ case 0 xargs -n1 plumb ' if not echo 'x/^/i/'^$"* } # ^rn from to # rename C identifier fn rn{ rfork e if(! ~ $#* 2){ echo 'usage: ^rn from to' exit } if(~ $%dot(2) $%dot(3)) echo , charconst='''[^'']*''' strconst='"([^"]|\\")*"' name='[a-zA-Z_¡-￿][a-zA-Z_0-9¡-￿]*' partial=`{ echo -n $1 | awk '{for(i = 0; i < length($1) + 1; i++) printf "."}' } echo 'y/'$charconst'/ y/'$strconst'/ x/'$name'/ g/'$1'/ v/'$partial'/ c/'$2 } # !u [script...] # print sam script usage fn u{ if(~ $#* 0) sed -n 's/^# ([_!^|><])/\1/p' /bin/s if not for(i) sed -n 's/^# ([_!^|><]'^$i^' )/\1/p' /bin/s } # ^w [cmd] # write buffer, open a rio window in base dir & run a cmd (eg. mk) fn w{ echo w b w c `{basename -d $%} ';' $* } l