Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!necntc!ncoast!allbery From: tcjones@watdragon.waterloo.edu (Crocodile Dundee) Newsgroups: comp.sources.misc Subject: e - the friendly interface to vi. version 1.1 Message-ID: <4912@ncoast.UUCP> Date: Tue, 20-Oct-87 23:02:13 EDT Article-I.D.: ncoast.4912 Posted: Tue Oct 20 23:02:13 1987 Date-Received: Fri, 23-Oct-87 06:58:41 EDT Sender: allbery@ncoast.UUCP Organization: U. of Waterloo, Ontario Lines: 1345 Approved: allbery@ncoast.UUCP X-Archive: comp.sources.misc/8710/15 Here is version 1.1 of e - the friendly interface to vi. This version fixes a bug with extra long argument lists, adds a very useful capability to do fast cross-directory e'ing using an environment variable VIPATH, and will now compile ok on a SUN. Terry Jones #!/bin/sh # This is a shell archive. Remove anything before the previous line, # then unpack it by saving it in a file and typing "sh file". # # Wrapped by watdragon!tcjones on Sat Oct 17 22:24:29 EDT 1987 # Contents: README edoc Makefile e.h e.c Manifest echo x - README sed 's/^@//' > "README" <<'@//E*O*F README//' Basically e is a little thingamy to let you get into vi with more ease. I find it useful - even given a shell with history. It keeps a short history of the files that have been e'ed most recently in each directory. Thus it is nice to be able to do cd somewhere e and get straight into the file you were last working on in the directory "somewhere". It also allows quick cross-directory editing and trys to help out with spelling. There is some documentation and examples in the file edoc. Before making you should change the #define VI "/usr/ucb/vi" line in e.h if your vi is located elsewhere. and the DEST line in Makefile. This will run just fine on BSD4.[23]. It appears to work on a SUN as well, just #define SUN in e.h Other versions will have little trouble. The readdir/opendir stuff of BSD is used for spelling corrections and cross-directory e'ing. I modified e.h to work on unix v8 in about 15 minutes. I think all that was required was to #include instead of dir.h and to change the modes on the read() calls. This appears free of major bugs (famous last words) - I'd really like to hear about anything that goes wrong and/or fixes/enhancements. Comments on what is wrong and how it could be better/faster are also welcome... ------------------------------------------------------------------------------- Terry Jones, Department Of Computer Science, University Of Waterloo Waterloo Ontario Canada N2L 3G1 {ihnp4,allegra,decvax,utzoo,utcsri,clyde}!watmath!watdragon!tcjones tcjones@dragon.waterloo.{cdn,edu} tcjones@WATER.bitnet tcjones%watdragon@waterloo.csnet [from oz, tcjones@dragon.waterloo.cdn@munnari] ------------------------------------------------------------------------------- @//E*O*F README// chmod u=rw,g=rx,o=rx README echo x - edoc sed 's/^@//' > "edoc" <<'@//E*O*F edoc//' e version 1.1 1/10/87 ===================== About e ======= e is an interface to vi that maintains a history of the most recently e'ed files for each directory. Its invocation syntax is (almost) a superset of that of vi. A file called .e is kept in each directory. This contains the history and is kept to a small length (<10 lines). Spelling corrections are suggested for simple mistakes (omitted character, interchanged characters, extra character). The history file is rearranged with each use to place the last e'ed file at the end of the list. Duplicate entries are removed. If set, the environment variable VIPATH is read and the directories it contains are searched for an existing file with the same name before a new file is created. Thus one can vi across directories with ease - it's nice to type "e .cshrc" or "e inode.h" etc from anywhere - albeit (for those that have it) at the loss of filename completion. Command line syntax =================== A list of the command line variations is given below. Those options that are reminiscent of vi syntax have a "*" after their descriptions. [x] indicates that "x" is optional. "cmd" means a vi command. e (Invokes vi on the last file that was e'ed in this directory) e - (Prints the history for this directory and allows selection of a previous file - or a new one) e . (Prints the history for this directory without asking for input) e -t tag (Invokes vi -t tag)* e -r (Invokes vi -r)* e -r file (Invokes vi -r file)* e -pat (vi's the last file that was e'ed with the string "pat" on the command line) e +[cmd] (vi's the last file that was e'ed in this directory but executes command "cmd" on the way into vi) e [+[cmd]] file (vi's the file and adds it to the history list. minor spelling corrections are suggested if "file" does not exist but is close (in spelling) to some file that does) * e [+[cmd]] files (vi's the files and adds them as a single entry to the history) * "Features". ========== When using "e -" the terminal is put into cbreak mode. If the first character typed is a digit (in the acceptable range of history items) then you will get that history item without further ado. Thus if you have a file called 4play and you try and "e" it from within a "e -" then you'll probably end up in the wrong place. Also the history length must be less than or equal to 9 (the code sets it to 8 at present (HIST_LINES)). This, I find, is enough but you may want more. The problem with having more is that with "e -" you go into cbreak and the first digit entered is taken to mean "I want the nth last file". This saves the need for hitting return. Do what you will, but I like it this way. With the history being kept as a most recently used list, 8 file names should be enough. The numbering on "e -" and "e ." is slightly different. The "e -" option is meant so that you can look at the number next to the file name and hit it easily. With "e ." you see what files you would get if you did an "e -3" (etc) and so it makes sense that "e -1" should give the second last file. That way you can pop in and out of two files by repeated "e -1"'s. Once again, that's the way it is... Examples. ========= With a ".e" file containing fred.c [5]: fred.c jane [4]: jane alison "e ." will give [3]: alison +/main pete.c [2]: +/main pete.c bigmac [1]: bigmac fries juice [0]: fries juice "e" - will get you fries and then juice. "e -" - will present the above and ask for a number (RETURN=0). "e -ali" - will get you alison. "e +/ketchup" - will get you fries and juice, searching for ketchup in fries. "e bigamc" - will ask if you want to correct to bigmac. n or N will not do the correction, q or Q will quit, anything else will go ahead. VIPATH ====== With an environment variable VIPATH set to "$HOME /usr/include/sys" "e inode.h" will prompt you with /usr/include/sys/inode.h if no file called inode.h exists in the current directory or your home directory. You can say y, n or q to accept, reject or quit. If you accept then the file is put into the history. Spelling corrcetions are *not* suggested across directories. There is (of course) no need to put "." in your VIPATH. Doing so will just slow things down and cannot possibly be of help. To be added? ============ * Allow backspacing on a select line. * Make .exrc files be inherited when e'ing across directories. * Spelling corrections across directories. * Plenty of fancy things. * A man page. * Keep a single .e file in $HOME and then it's easy to use the history in unwriteable directories etc. do this nicely with hashing/binary search etc. (suggested by ian! allen) e version 1.1 changes - 17/10/87 ================================ * Added VIPATH environment variable. * Fixed problem with ARG_LENGTH. * #ifdefs that made it work on a SUN3/50. * Moved #defines into e.h ------------------------------------------------------------------------------- terry jones, department of computer science, university of waterloo waterloo ontario canada N2L 3G1 {ihnp4,allegra,decvax,utzoo,utcsri,clyde}!watmath!watdragon!tcjones tcjones@dragon.waterloo.{cdn,edu} tcjones@WATER.bitnet tcjones%watdragon@waterloo.csnet [from oz, tcjones@dragon.waterloo.cdn@munnari] ------------------------------------------------------------------------------- @//E*O*F edoc// chmod u=rw,g=rx,o=rx edoc echo x - Makefile sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//' CC = /bin/cc CFLAGS = -O DEST = /u/tcjones/bin/e SOURCE = e.c $(DEST) : $(SOURCE) $(DEST): $(CC) $(CFLAGS) -o $(DEST) $(SOURCE) strip $(DEST) @//E*O*F Makefile// chmod u=rw,g=rx,o=rx Makefile echo x - e.h sed 's/^@//' > "e.h" <<'@//E*O*F e.h//' /* * e.h for e version 1.1 * ===================== * *------------------------------------------------------------------------------ * terry jones, department of computer science, university of waterloo * waterloo ontario canada N2L 3G1 * {ihnp4,allegra,decvax,utzoo,utcsri,clyde}!watmath!watdragon!tcjones * tcjones@dragon.waterloo.{cdn,edu} tcjones@WATER.bitnet * tcjones%watdragon@waterloo.csnet *------------------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #ifndef SUN #include #endif #define VI "/usr/ucb/vi" #define HIST ".e" #define HIST_LINES 8 #define HIST_CHARS 1024 #define ARG_CHARS 1024 #define MAX_ARGS 100 #define MAX_PATH 1024 #define RUBOUT '\077' #define BELL '\007' #define O_READ 00004 #define G_READ 0004 #ifdef SUN #define IREAD 00400 #define IWRITE 00200 #endif @//E*O*F e.h// chmod u=rw,g=rx,o=rx e.h echo x - e.c sed 's/^@//' > "e.c" <<'@//E*O*F e.c//' /* * e.c - a friendly interface to vi * version 1.1 * * Terry Jones * Department of Computer Science * University of Waterloo * Waterloo, Ontario, Canada. N2L 3G1 * * {ihnp4,allegra,decvax,utzoo,utcsri,clyde}!watmath!watdragon!tcjones * tcjones@dragon.waterloo.{cdn,edu} tcjones@WATER.bitnet * tcjones%watdragon@waterloo.csnet * * version 1.1 changes 17/10/87 * * - Added VIPATH environment variable * - Fixed bug with argument lists that exceeded ARG_CHARS characters. * in multiple(). * - Added #ifdef statements to enable compilation on a SUN. * - Put #defines etc into e.h as they were getting in the way. */ #include "e.h" /* * globals. all in the name of ease and perhaps speed. */ char history[HIST_CHARS]; char arg[ARG_CHARS]; char *hist[HIST_LINES]; char temp[HIST_CHARS]; char *tmp_file="._vihistXXXXXX"; void do_vi(thing) char *thing; { /* split the arguments in 'thing' up and exec vi on them */ char *args[MAX_ARGS]; char *this,*next; register i; args[0]=VI; args[1]=thing; i=1; while (*thing!='\0'&&(thing=index(thing,' '))!=NULL){ *thing++='\0'; if (*thing!='\0'){ args[++i]=thing; } } args[++i]=NULL; if (execvp(VI,args)==-1){ perror(VI); exit(1); } } main(c,v) int c; char **v; { char *last_file(); extern clean_up(); /* make sure we reset the terminal on our way out if we get interrupted */ if (signal(SIGINT, SIG_IGN) != SIG_IGN){ signal(SIGINT, clean_up); } switch (c){ case 1: /* just go and vi the last file that was vi'ed */ last_file(); do_vi(arg); break; case 2: switch ((*++v)[0]){ case '-': if ((c=(*v)[1])=='\0'){ /* this is a select from history, ask what they want */ ask_hist(); do_vi(arg); } else if (isdigit(c)){ /* get the nth last file from the history and vi it */ nth_hist(c-'0'); do_vi(arg); } else if (c=='t'&&(*v)[2]=='\0'){ /* this is an empty tag - ignore it */ do_vi(*v); } else if (c=='r'&&(*v)[2]=='\0'){ /* a recover, just pass it to vi and don't interfere */ do_vi(*v); } else{ /* this is a pattern - try to match it */ find_match(++*v); do_vi(arg); } break; case '+': /* a command, put it before the last file name etc */ insert_command(*v); do_vi(arg); break; case '.': /* just give a history list if there is only a dot */ if ((*v)[1]=='\0'){ register ct; register i; read_hist(); ct=split_hist(); for (i=0;icount-1){ if (count>1){ fprintf(stderr,"Only %d history items exist.\n",count); } else{ fprintf(stderr,"Only one history item exists.\n"); } exit(1); } sprintf(arg,"%s",hist[n]); /* rebuild the history with the selected name at the bottom */ reconstruct(n,count); } ask_hist() { /* ask the outside world which of the files in the history is wanted. set the terminal to cbreak. */ register i; register count; char *last; register option; struct sgttyb blk; /* read and split the history file */ read_hist(); count=split_hist(); /* print the history */ for (i=0;i "); /* set the terminal up */ set_term(); /* get their response */ option=getc(stdin); /* make the terminal 'safe' again */ unset_term(); /* process the option and put the appropriate file name into the arg variable. */ if (option=='\n'){ /* they want the last file of the list */ fprintf(stderr,"%s\n",hist[0]); sprintf(arg,"%s",hist[0]); return; } else if (option==RUBOUT){ /* they want to leave */ fprintf(stderr,"\n"); exit(1); } else if (option>='1'&&option<='0'+count){ /* they have requested a file by it's number */ option=option-'0'; fprintf(stderr,"%s\n",hist[option-1]); sprintf(arg,"%s",hist[option-1]); } else{ /* looks like they want to name a specific file. echo the characters back to the screen. */ fprintf(stderr,"%c",option); arg[0]=option; i=1; while ((arg[i]=getc(stdin))!='\n'){ i++; } arg[i]='\0'; option=count-1; /* a kludge for the history re-make to follow */ /* seeing as they typed in the name, try and help with spelling */ if (!spell_help()){ find(); } /* if it is in the history then reconstruct and return */ for (i=0;i=length){ if (!strncmp(argument++,pattern,length)){ return(1); } } return(0); } find_match(pattern) char *pattern; { /* find the name in the history list that contains the 'pattern'. if it exists then put it into the 'arg' variable and otherwise announce that a match couldn't be found and leave. */ register count; register i; /* read and split the history file */ read_hist(); count=split_hist(); /* try for a match with each file in turn (note that we are working from most-recently-used backwards - probably a good thing) */ for (i=0;i=0;i--){ if (i!=except){ fprintf(tv,"%s\n",hist[i]); } } /* put in the new line from 'arg' */ fprintf(tv,"%s\n",arg); /* rename the temporary to be the new history file */ close_temp(tv); } normal(string) char *string; { /* a normal filename was found, put it into arg. first of all if there is a history and the file is already in it (which means they could have gotten to this file in other ways), then reconstruct the history as though they had. also offer spelling help. */ register count; register i; /* put it into 'arg' */ sprintf(arg,"%s",string); /* if there is a history file */ if (got_vi()){ /* read it and split it up */ read_hist(); count=split_hist(); /* if it is in the history then reconstruct and return */ for (i=0;i=size){ /* add one for a space */ /* * If you are running e and you find that this condition occurs, * the solution is to simply increase the value of the #define * line for ARG_CHARS near the top. */ fprintf(stderr, "%c%c%cWarning! Argument list too long, truncated after \"%s\".\n", BELL,BELL,BELL,*args); sleep(2); /* give the user some chance to see what happened */ break; } strcat(arg,*++args); if (number>1){ strcat(arg," "); } } /* now if there is a history file and we can find an identical line then reconstruct with that line at the bottom. */ if (got_vi()){ read_hist(); count=split_hist(); for (i=0;id_ino&&entry->d_namlen>=len-1&&entry->d_namlen<=len+1){ /* get the 'distance' between what we want and this file name */ new_dist=sp_dist(entry->d_name,arg); /* if this name is close enough and better than our current best */ if (new_dist<=dist&&new_dist!=3){ if (!new_dist){ /* if the dist is 0 then they are identical */ closedir(dp); return(1); } /* remember the new name and distance */ strcpy(new,entry->d_name); dist=new_dist; } } } /* close up. if we got no suitable result then simply return */ closedir(dp); if (dist==3){ return(0); } /* offer them "new" */ set_term(); fprintf(stderr,"correct to %s [y]? ",new); /* process the reply */ switch (getc(stdin)){ case 'N': case 'n': fprintf(stderr,"no\n"); unset_term(); return(0); case 'q': case 'Q': fprintf(stderr,"quit\n"); unset_term(); exit(0); default : fprintf(stderr,"yes\n"); unset_term(); strcpy(arg,new); return(1); } } sp_dist(s,t) char *s; char *t; { /* stolen from the same place as spell_help() above. work out the distance between the strings 's' and 't' according to the rough metric that identical = 0 interchanged characters = 1 wrong character/extra character/missing character = 2 forget it = 3 */ while (*s++==*t){ if (*t++=='\0'){ /* identical */ return(0); } } if (*--s){ if (*t){ if (s[1]&&t[1]&&*s==t[1]&&*t==s[1]&&!strcmp(s+2,t+2)){ /* interchanged chars */ return(1); } if (!strcmp(s+1,t+1)){ /* wrong char */ return(2); } } if (!strcmp(s+1,t)){ /* extra char in 't' */ return(2); } } if (!strcmp(s,t+1)){ /* extra char in 's' */ return(2); } /* forget it */ return(3); } find() { extern char *getenv(); extern char *getwd(); char *p; char path[MAX_PATH]; char *dir; char *space; char *us; char wd[MAXPATHLEN]; char what[ARG_CHARS]; p=getenv("VIPATH"); if (!p||!strlen(p)) return; if (strlen(p)>=MAX_PATH){ fprintf(stderr,"Could not get VIPATH from environment.\n"); exit(1); } strcpy(path,p); strcpy(what,arg); if (!(us=getwd(wd))){ fprintf(stderr,"%s\n",wd); exit(1); } dir=path; while ((space=index(dir,' '))!=NULL){ *space='\0'; if (check(what,dir)){ /* offer them dir/what */ set_term(); fprintf(stderr,"%s/%s [y]? ",dir,what); /* process the reply */ switch (getc(stdin)){ case 'N': case 'n': fprintf(stderr,"no\n"); break; case 'q': case 'Q': fprintf(stderr,"quit\n"); unset_term(); exit(0); break; default : fprintf(stderr,"yes\n"); unset_term(); if (chdir(us)==-1){ perror("chdir"); exit(1); } sprintf(arg,"%s/%s",dir,what); return(1); } unset_term(); } dir=space+1; } if (check(what,dir)){ /* offer them dir/what */ set_term(); fprintf(stderr,"%s/%s [y]? ",dir,what); /* process the reply */ switch (getc(stdin)){ case 'N': case 'n': fprintf(stderr,"no\n"); break; case 'q': case 'Q': fprintf(stderr,"quit\n"); unset_term(); exit(0); break; default : fprintf(stderr,"yes\n"); unset_term(); if (chdir(us)==-1){ perror("chdir"); exit(1); } sprintf(arg,"%s/%s",dir,what); return(1); } unset_term(); } if (chdir(us)==-1){ perror("chdir"); exit(1); } return(0); } check(target,dir) char *target; char *dir; { DIR *dp, *opendir(); struct direct *readdir(); struct direct *entry; struct stat buf; if ((dp=opendir(dir))==NULL){ /* watch here for the chance that they are sitting in someone else's directory which may be unreadable - if not, complain. */ if (strcmp(dir,".")){ fprintf(stderr,"Cannot open \"%s\"\n",dir); } return; } for (entry=readdir(dp);entry!=NULL;entry=readdir(dp)){ if (!strcmp(entry->d_name,target)){ if (chdir(dir)==-1){ perror("chdir"); return; } if (stat(entry->d_name,&buf)==-1){ perror("stat"); return; } /* if it is not a directory and EITHER you own it and can read it OR you don't own it and it is readable by others, OR you are in the group of the owner and it's group readable - then this is it. */ if ( !(buf.st_mode&S_IFDIR) && ( (buf.st_uid==getuid() && buf.st_mode&S_IREAD) || (buf.st_gid==getgid() && buf.st_mode&G_READ) || (buf.st_uid!=getuid() && buf.st_mode&O_READ) ) ) { return(1); } } } return(0); } @//E*O*F e.c// chmod u=rw,g=rx,o=rx e.c echo x - Manifest sed 's/^@//' > "Manifest" <<'@//E*O*F Manifest//' The shar should come with six files 146 Makefile 117 Manifest 1724 README 20563 e.c 1049 e.h 5442 edoc @//E*O*F Manifest// chmod u=rw,g=,o= Manifest echo Inspecting for damage in transit... temp=/tmp/shar$$; dtemp=/tmp/.shar$$ trap "rm -f $temp $dtemp; exit" 0 1 2 3 15 cat > $temp <<\!!! 43 249 1733 README 152 899 5727 edoc 10 23 146 Makefile 45 103 1049 e.h 1023 2756 25469 e.c 9 19 148 Manifest 1282 4049 34272 total !!! wc README edoc Makefile e.h e.c Manifest | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp if [ -s $dtemp ] then echo "Ouch [diff of wc output]:" ; cat $dtemp else echo "No problems found." fi exit 0