#! /usr/local/bin/perl -w use strict; # Time-stamp: ## This is my cheesy $EDITOR replacement ## If you set $EDITORS as a list of colon separated options, it will try them one at a time ## until something works. ## If you want to use gnuclient-ssh, make sure you have tramp autoloaded in ## emacs, or you will mightily confuse emacs my $return_code = 1; my @trylist; if ( defined( $ENV{ 'EDITORS' } ) ) { @trylist = split( /:/ , $ENV{ 'EDITORS' } ); } else { @trylist = ( 'gnuclient-ssh' , 'emacstty' , 'vim' , 'vi' ); } my $test = $ENV{ 'DISPLAY' }; ## We should never leave this for loop for my $c ( 0 .. $#trylist ) { if ( $return_code == 0 ) { exit( 0 ); } else { $return_code = Try_It( $trylist[ $c ] ); } } ### If we got passed the for loop, then error print "None of your editors succeeded. Sorry\n"; exit( 1 ); sub Try_It { my $editor = shift; print "Starting attempt for $editor\n"; my %vars = ( 'username' => $ENV{ 'USER' } , 'gnu_server' => Clean( $ENV{ 'SSH_CLIENT' } ) , 'display' => $ENV{ 'DISPLAY' } , 'gnu_client' => `hostname --fqdn` , ); chomp $vars{ 'gnu_client' }; my %editors = ( 'gnuclient-ssh' => { 'command' => "gnuclient -h $vars{ 'gnu_server' } /r:$vars{ 'username' }\@$vars{ 'gnu_client' }:@ARGV" , 'vars' => [ 'gnu_client' , 'gnu_server' , 'username' , 'display' ] } , 'gnuclient-local' => { 'command' => "gnuclient @ARGV" , 'vars' => [ 'display' ] } , 'xemacs' => { 'command' => "xemacs @ARGV" , 'vars' => [ 'display' ] } , 'xemacstty' => { 'command' => "xemacs -nw @ARGV" , 'vars' => [] } , 'emacs' => { 'command' => "emacs @ARGV" , 'vars' => [ 'display' ] } , 'emacstty' => { 'command' => "emacs -nw @ARGV" , 'vars' => [ ] } , 'vim' => { 'command' => "vim @ARGV" , 'vars' => [ ] } , 'vi' => { 'command' => "vi @ARGV" , 'vars' => [ ] } , 'ed' => { 'command' => "ed @ARGV" , 'vars' => [ ] } , 'joe' => { 'command' => "joe @ARGV" , 'vars' => [ ] } , 'pico' => { 'command' => "pico @ARGV" , 'vars' => [ ] } ); ## First check vars my @list = @{ $editors{ $editor }{ 'vars' } }; for (my $i; $i < scalar(@list); $i++) { my $var_to_test = $vars{ $list[ $i ] }; # print "Now testing $list[ $i ] which is $vars{ $list[ $i ] } \n"; if (!defined $vars{ $list[ $i ] } or $vars{ $list[ $i ] } eq '' ) { print "The variable $list[ $i ] is $var_to_test and has failed, we cannot use $editor\n"; return( 1 ); } } print "Now trying to exec $editors{ $editor }{ 'command' }\n"; my $return = system( $editors{ $editor }{ 'command' } ); print "I am returning from $editors{ $editor }{ 'command' } with $return \n"; return( $return ); } sub Clean { my $data = shift; my ( $host , $stuff ) = split( /\s+/ , $data ); return ( $host ); }