shithub: rc

ref: 4028a0b5f197066ef36281f45c8efbaaaf3dbe37
dir: /bin/s/

View raw version
#!/bin/rc
# sam functions

# ^r [cmd]     # run command in cwd and send output to jam.err window
fn r{
	echo '^T'
	f=$%
	if(~ $#* 0){
		w
	}
	if not{
		c=''''^($*)^''''
		echo !'{' '@{' $c '}' '}'
		echo B /tmp/jam.err
		echo e
		echo b $f
		echo b ~~jam~~
	}
}

# ^R [cmd]     # run command and send output to jam.err window
fn R{
	if(~ $% jam.err */jam.err){
		echo nope >[1=2]
		exit
	}
	echo '^T'
	f=$%
	d=`{basename -d $f}
	if(~ $#* 0){
		W
	}
	if not{
		c=''''^($*)^''''
		echo !'{' cd $d '&&' '@{' $c '}' '}'
		echo B /tmp/jam.err
		echo e
		echo b $f
		echo b ~~jam~~
	}
}

# ^T	# write to disk any modified files
fn T{
	echo X/^''''/w
}

# !o name expr # save a sam command
fn o{
	# beware of free carets between the überquotes
	echo fn $1 '{' echo '''' $*(2-) '''' '}' >/env/'fn#'^$1
}
# ^O expr      # run any rc function
fn O{
	fun $*
}

# ^w [cmd]     # open window in cwd
fn w{
	echo !exec window -m -minx 0 -miny 0 -dx 600 -dy 600 $*
}

# from kvik's rc/rwin
# ^W [cmd]     # open window in current file's directory
fn W{
	if(~ $% jam.err */jam.err){
		echo nope >[1=2]
		exit
	}
	echo '^T'
	echo !exec window -minx 0 -miny 0 -dx 600 -dy 600 -m -cd `{basename -d $%} $*
}

# ^dump		# print all files in menu
fn dump{
	rfork e
	d = `{pwd}
	echo 'Y/^..\./!echo `{cleanname -d' $d '$%}^'':#''^$%dot(2)^'',#''^$%dot(3) >>/tmp/dump.'$pid
	echo 'X/^..\./!echo `{cleanname -d' $d '$%}^'':#''^$%dot(2)^'',#''^$%dot(3) >>/tmp/dump.'$pid
	echo r '/tmp/dump.'$pid
	echo !rm '/tmp/dump.'$pid
}

# from umbraticus' rc/s/u
# !u [fn...]   # print sam fn usage
fn U{
	if(~ $#* 0) sed -n 's/^# ([_!^|><])/\1/p' /bin/s
	if not for(i) sed -n 's/^# ([_!^|><]'^$i^' )/\1/p' /bin/s
}
fn u{
	window -m -dx 636 rc -c 'U; read'
}

# ^+ [n]       # increase indent
fn +{
	t='	'
	if(! ~ $#* 0)
		for(i in `{seq 2 1 $1}) t=$t^'	'
	echo 's/^/'^$t^'/g'
}
# ^- [n]       # decrease indent
fn -{
	t='	'
	if(! ~ $#* 0)
		for(i in `{seq 2 1 $1}) t=$t^'	'
	echo 's/^'^$t^'//g'
}

# ^swap        # swap two strings specified as regular expressions
fn swap{
	echo s/''^$"1^''/‡‡'&'‡‡/g
	echo s/''^$"2^''/''^$"1^''/g
	echo s/‡‡''^$"1^''‡‡/''^$"2^''/g
}

# from umbraticus' rc/s
# |pc          # inline arithmetic
fn pc{
	/bin/pc | tr -d '
'
}
# _sig           # print function signature
fn sig{
	sed '1s/.*/!sig &/;q'
}
# _i           # find information about selected C function/struct or command
fn i{
	rfork e
	name = `{read}
	if(! ~ $#name 1){
		echo !echo select a function/struct/command name first!
		exit
	}
	cd `{basename -d $%}
	match = `{g '^([*a-zA-Z]+[ 	]+)*'^$name^'[ 	]*(\([^;]*)?$' | sed '1{s/: .*//;q;}'}
	if(~ $#match 1)	# local function definition
		plumb $match
	if not{
		match = `{g 'struct[ 	]+'^$name^'[ 	]*({|$)' | sed '1{s/: .*//;q;}'}
		if(~ $#match 1)	# local struct definition
			plumb $match
		if not{
			match = `{struct $name | sed 1q}
			if(~ $#match 1)	# system struct definition
				plumb $match
			if not{
				match = `{man -w $name >[2]/dev/null | sed 's†.*/([0-9])/(.*)†\2(\1)†'}
				if(! ~ $#match 0){	# system function or command
					echo !echo ''''^$"match^''''
					echo !sig $name
				}
				if not{	# getting really desperate now...
					match = `{lookman $name | sed 's/.*#//'}
					if(~ $#match 0)
						echo !echo no information found for $name
					if not
						echo !echo ''''^$"match^''''
				}
			}
		}
	}
}
# _m           # plumb man page for selected name
fn m{
	sed '1s/.*/!plumb &''(0)''/;q'
}
# ^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
}
# ^cc          # c comment
fn cc{
	echo '#'^$%dot(3)^'a/*\//'
	echo '#'^$%dot(2)^'i/\/*/'
}
# ^uc          # uncomment c/rc code
fn uc{
	echo 'x/^(#|\/\/) ?/d'
}

# source common menu items
if(~ $#radarappearsjammed 0 && test -f $home/lib/jam)
	. $home/lib/jam
status=''

radarappearsjammed=1