#!/bin/ksh # # e # # AUTHOR: # Dan Harkless # # COPYRIGHT: # This file is Copyright (C) 1999 by Dan Harkless, and is released under the # GNU General Public License . # # USAGE: # % e [-wait] [...] # # DESCRIPTION: # gnuclient is a wonderful tool if you're like me and leave an emacs / xemacs # invocation running at all times. However, a raw call to gnuclient won't # work in certain situations, hence this wrapper script. # # gnuclient works great if you run it on the same machine emacs is running on, # but when the machines are different, things get tougher. gnuclient has a -h # parameter (equivalent to the $GNU_HOST environment variable) which is # supposed to attach to a gnuserv process running on a different machine, but # I have always had problems getting this mechanism to work. In any case, I # sometimes log onto machines that don't even have gnuclient installed. # # The solution this script takes is to remsh to the machine emacs is running # on and execute gnuclient there. How do we determine which machine that is? # Well, if $GNUSERV_HOST (not $GNU_HOST!) is set, we use that. If not, we use # the machine specified by $DISPLAY (people with real X workstations generally # run emacs on their local machines). # # One thing a naked gnuclient doesn't take into account is the situation where # you are logged onto a remote machine and want to edit a file in a directory # that isn't available on your emacs machine. For instance, you might want to # edit a file in /tmp, a directory that's local to each machine. If your file # is in a partition that is typically not NFS-mounted, this script will copy # the file to your ~/tmp directory (please create one if you don't have one # already), have emacs edit it there, and then move it onto the original if # you actually modified it. # # One last feature of this script is that if $DISPLAY isn't set, we assume # you're not logging in from an X terminal, and instead of calling gnuclient, # we run 'emacs -nw'. # # DATE MODIFICATION # ========== ================================================================== # 1999-04-22 Changed name of required ~/temp directory to more standard ~/tmp. # 1999-04-13 HP-UX's ksh doesn't support -e for file existence check -- use -a. # 1999-04-05 If you're on an X terminal or are running emacs remotely, $HOST is # not the emacs host. If $GNUSERV_HOST is set, use that instead. # 1998-07-28 Added -wait: don't exit until gnuclient does. # 1996-09-19 Original. wait_for_gnuclient_exit=0 if [[ $1 = -wait ]]; then wait_for_gnuclient_exit=1 shift fi if [[ -z $DISPLAY ]]; then exec emacs -nw "$@" elif [[ -z $GNUSERV_HOST ]]; then if [[ $DISPLAY = *:* ]]; then # $DISPLAY is of the form "[]:0.0". GNUSERV_HOST=${DISPLAY%%:*} if [[ -z $GNUSERV_HOST ]]; then # $DISPLAY is of the form ":0.0". GNUSERV_HOST=$HOST fi else # $DISPLAY is of the form "". GNUSERV_HOST=$DISPLAY fi fi if [[ $HOST = $GNUSERV_HOST ]]; then if [[ $wait_for_gnuclient_exit = 1 ]]; then gnuclient "$@" else (gnuclient "$@" &) fi else e=${0##*/} # get the root name in case we need to display errors orig_wd=$PWD for f_path in "$@"; do f=${f_path##*/} if [[ $f != $f_path ]]; then f_dir=`dirname "$f_path"` # can't do this with a single substr. op. if [[ -d $f_dir ]]; then cd $f_dir else echo "$e: Directory '$f_dir' does not exist!" continue # do the next iteration of the for fi else cd $orig_wd fi partition=/`pwd | cut -d/ -f2` # can't do this with a single substr. op. case $partition in / | /etc | /opt | /tmp | /usr | /var) if [[ -a ~/tmp/$f ]]; then echo "$e: Can't edit $f_path because ~/tmp/$f exists!" echo "$e: Aborting." exit 1 else cp -p $f_path ~/tmp fi # In this situation we always have to behave like -wait was used # so we can check whether the file was changed or not. remsh $GNUSERV_HOST "gnuclient ~/tmp/'$f'" if cmp -s ~/tmp/"$f" "$f_path"; then # File is unchanged. rm -f ~/tmp/"$f" else # File was modified: move the ~/tmp copy onto the original. mv ~/tmp/"$f" "$f_path" if [[ $? != 0 ]]; then print -n "$e: Delete ~/tmp/$f [Ny]? " read answer [[ $answer = y* ]] && rm -f ~/tmp/"$f" fi fi ;; *) if [[ $wait_for_gnuclient_exit = 1 ]]; then remsh $GNUSERV_HOST "gnuclient '$PWD/$f'" else (remsh $GNUSERV_HOST -n "(gnuclient '$PWD/$f' &)" &) fi esac done fi