#!/usr/local/plan9/bin/rc rfork e PATH=/usr/local/plan9/bin:$PATH usage='usage: edit [file]' wd=`{pwd} os=`{uname} fn owner { ls -l $1 | awk '{print $4}' } fn med {a=(){ # usage: med file a=`{xattr $1} # extended attribute if(~ $a ?*) xattr -d $a $1 open -a $EDITOR $1 || echo $usage # back ground job. don't rm $1 }} fn ided {b=() d=(){ # indirect edit in $T # usage: ided file # the file is absolute path b=`{basename $1} d=`{basename -d $1} if(! test -e $T$d) mkdir -p $T$d cp $1 $T$1 $EDIT $T$1 }} fn confirm {a=(){ echo $1'?(y/n)' >[1=2] while(! ~ $a y n) a=`{read} echo $a }} switch($os){ case Darwin EDITOR=TextEdit.app #EDITOR=Xcode.app EDIT=med case Linux FreeBSD EDIT=nano case * echo unsuppoted OS exit } T=$home/tmp/edit # working directory for edit. # $T is automatically created, if absent, with chmod 0700 while(~ $1 -*){ switch($1){ case -* echo $usage exit }} if(! test -d $T){ mkdir -p $T chmod 0700 $T } if(~ $#* 0){ # usage1: edit # usage2: command | edit # second case will work only for GUI based editor such as TextEdit.app t=$pid # identifier f=$T/$t if(u test -t 0) # input is a tty not a pipe touch $f if not cat > $f p=`{echo -n NXNAXAMXMEXE | tr X '\010'} if(grep -s $p $f){ # we guess man output cat $f | col -b > $f^m mv $f^m $f } $EDIT $f #rm $f exit } if(! test -e $1){ a=`{confirm create} if(~ $a y Y) touch $1 if not exit } e=$1 if(! ~ $e /*) e=$wd/$e e=`{cleanname $e} # NB: then $e is the absolute path of $1 # $T/$e is wrong. use $T$e # maybe better: if(! ~ `{owner $e} `{whoami}) if(~ `{owner $e} root){ ided $e a=`{confirm save} if(~ $a y Y) echo sudo cp -X $T$e $e exit } $EDIT $e