#!/usr/local/plan9/bin/rc # 2017/08/06 update # 2021/07/13 update, added -e option # edit might be useless for the OS except MacOS # TextEdit.app of MacOS is too annoying # so I must execute # xattr -c file # in prior to open the file rfork e PATH=/usr/local/plan9/bin:$PATH usage='usage: edit [-e editor] [file]' # on Mac use "open" for editor if you don't want EDITOR value below 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 -c $1 # maybe better #xattr -d $a $1 open -a $EDITOR $1 || echo $usage # back ground job. don't rm $1 }} fn med2 {a=(){ # usage: med2 file $EDITOR $1 & }} fn netcp { # to be used for copy back u=uuidgen cp $1 $2-$u if(~ $status ?*){ echo 'could not cp from',$1,'to',$2-$u exit } mv $2-$u $1 if(~ $status ?*){ echo 'netcp failed',$1,$2 exit } } 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 -X $1 $T$1 # -X is Mac special, which means "Do not copy Extended Attributes" $EDIT $T$1 }} fn dir1 { echo $1 | awk -F / '{print $2}' } # usage: dir1 path # dir1 returns the first element of absolute path # dir1 /aaa/bbb --> aaa # dir1 aaa/bbb --> bbb fn confirm {a=(){ echo $1'?(y/n)' >[1=2] while(! ~ $a y n) a=`{read} echo $a }} T=$home/tmp/edit # working directory for edit. # $T is automatically created, if absent, with chmod 0700 if(! test -d $T){ mkdir -p $T chmod 0700 $T } switch($os){ case Darwin atom = /Applications/Atom.app texshop=/Applications/TeX/TeXShop.app/Contents/MacOS/TeXShop itext=/Applications/'iText Express.app' textedit=/Applications/TextEdit.app # too restrictive, but simple mi=/Applications/mi.app xcode=/Applications/Xcode.app # too big! EDITORLIST=(textedit atom mi itext texshop xcode) # used for usage EDITOR=mi EDIT=med # med is defined in this file case Linux FreeBSD # nano is preinstalled EDITORLIST=(nano jove) EDITOR=jove # small editor with emacs like key binding EDIT=med2 case * echo unsuppoted OS exit } while(~ $1 -*){ switch($1){ case -e shift EDITOR=$1 shift case -* echo $usage exit }} if(! ~ $EDITOR $EDITORLIST){ echo 'select from:' $EDITORLIST exit } 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 ifs=' ' e=`{cleanname $e} # NB: then $e is the absolute path of $1 # $T/$e is wrong. use $T$e # we assume names for server begins with /n # maybe better: if(! ~ `{owner $e} `{whoami}) if(~ `{dir1 $e} n || ~ `{owner $e} root){ ided $e a=`{confirm save} if(~ $a y Y){ if(~ `{dir1 $e} n) cp -X $T$e $e if not sudo cp -X $T$e $e } exit } $EDIT $e