home *** CD-ROM | disk | FTP | other *** search
- /* This file is MAIN.CC */
- #include "em.h"
- //FILE*debug;
- int gethex(int&i,char*&f){char c; int j=0,k=0,s=1;
- while((c=*f++)==' '?:c=='\t'); if(c=='-') {s=-1; c=*f++;}
- A: if(c>='0') if(c<='9') {j=j*16+c-'0'; c=*f++; k++; goto A;}
- if(c>='A') if(c<='F') {j=j*16+c+10-'A'; c=*f++; k++; goto A;}
- if(c>='a') if(c<='f') {j=j*16+c+10-'a'; c=*f++; k++; goto A;} i=j*s; return k;}
- /*-----*/
- char DIR[64],helpfile[128],bighelpfile[128],infofile[128],*dictname=0;
- char *altnames[256],*altshortnames[256]; val keynames[13]; char rept;
- macrinfo basemi,*mi=&basemi; int nscreenmodes,oldmx,oldmy;
- c_short_addr graphicalscreen(0xa0000); jmp_buf Exit;
- /*-----*//* remove null args */
- //static void dustman(int&n,char**arg){reg i,j; reg char*s;
- //for(i=j=0;i<n;i++) if(s=arg[i]) arg[j++]=s; n=j;}
- /*-----*/
- char Getchar(){int i; A: i=get_key(); if(i<0) goto A; printch(i); return i;}
- /*-----*/
- void Gets(char*s){int i; char*t=s;
- A: i=Getchar(); *t++=i; if(i!=10) if(i!=13) goto A; t[-1]=0;}
- /*-----*/
- void myhandler(){MOAN("out of store");}
- /*-----*/
- void*myalloc(int i){void*x=malloc(i); if(!x) myhandler(); return x;}
- /*-----*/
- main(int nargs,char**Arg){
- c_mem Dustbin(512); c_short_addr dustbin(Dustbin); Subr*S; char*s,**info=0;
- int i,j,k,m,n,w; jmp_buf emacserror; buffer*C=0; char *T,**arg;
- char goodmode[]={0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0};
- Jerry.range(2048,2048); Jerry.mc=0;
- arg=(char**)myalloc(nargs*sizeof(char*)); for(i=0;i<nargs;i++) arg[i]=Arg[i];
- if(nargs>1) if(!strcmp(arg[1],"/help")) {
- ps("in AAEMACS, type 'ctrl-_ ?' to find how to get help.\n"); exit(0);}
- gp_Cols=ScreenCols(); gp_Rows=ScreenRows(); clock();
- /**** These 2 addresses are specific for Gnu C's virtual store system:- ****/
- /* gp_Cols=*(uns short*)0xe000044a; gp_Rows=*(byte*)0xe0000484+1; */
- for(i=0;s=(S=&subrname[i])->name;i++) S->n=strlen(s); i=0;
- keynames[i++]=val("null",0);
- keynames[i++]=val("tab",'I'-64);
- keynames[i++]=val("ret",CR);
- keynames[i++]=val("esc",'['-64);
- keynames[i++]=val("space",' ');
- keynames[i++]=val("ctrlleftsqbrkt",'['-64);
- keynames[i++]=val("ctrlbackslash",'\\'-64);
- keynames[i++]=val("ctrlrightsqbrkt",']'-64);
- keynames[i++]=val("ctrlcircumflex",'^'-64);
- keynames[i++]=val("ctrlunderline",'_'-64);
- keynames[i++]=val("char127",127);
- keynames[i++]=val("char255",255);
- keynames[i++]=val(0,0);
- for(i=0;i<255;i++) {to__upper[i]=to__lower[i]=i; chtype[i]=0;}
- for(i='a';i<='z';i++) {to__upper[i]-=32; chtype[i]|=c_alpha|c_alnum;}
- for(i='A';i<='Z';i++) {to__lower[i]+=32; chtype[i]|=c_alpha|c_alnum;}
- for(i='0';i<='9';i++) chtype[i]|=c_alnum; chtype['_']|=c_alnum;
- chtype[' ']|=c_white; chtype[9]|=c_white; chtype[LF]|=c_white;
- set_new_handler(&out_of_store); Moan=Display=0;
- lastyank=region(); Found=region(); for(i=0;i<16;i++) killring[i]=Text();
- if(T=getenv("AAEMACS")) {strcpy(DIR,T); goto DIR_FOUND;}
- wr("parameter AAEMACS not set in AUTOEXEC.BAT\n");
- if(nargs>0) {strcpy(DIR,arg[0]); n=strlen(DIR);
- for(i=0;i<n;i++) DIR[i]=DIR[i]=='/'?'\\':to__upper[DIR[i]&255];
- if(!strcmp(DIR,"EMACS")) goto ASK_FOR_DIR;
- if(n>=6) if(!strcmp(DIR+n-6,"\\EMACS")) {DIR[n-6]=0;
- pr(CW,"is your AAEMACS on directory '%s'?",DIR);
- if(Yesno(CW)) goto DIR_FOUND;}}
- ASK_FOR_DIR: wr("what directory is your AAEMACS on?"); Gets(DIR);
- DIR_FOUND: pr(helpfile,"%s\\"HELPFILE,DIR); n=strlen(DIR);
- for(i=0;i<n;i++) DIR[i]=DIR[i]=='/'?'\\':to__upper[DIR[i]&255];
- pr(bighelpfile,"%s\\"BIGHELPFILE,DIR); pr(infofile,"%s\\INFO.DAT",DIR);
- if(!(info=readtext(infofile))) {ps("I can't open file %s\n",infofile); exit(1);}
- m=gp_mode(); *graphicalscreen=16; _ax=(*graphicalscreen).val();
- if(m<20 ? !goodmode[m] : _ax==16 ?: ScreenCols()<80 ?: ScreenRows()<20) {
- /* if(outportb(0x3c0,0x10),inportb(0x3c1)&3 *//* eliminate mono & graphic */
- ps("this screen mode (%1x hex) is not suitable for AAEMACS\n",m); exit(1);}
- if(ScreenCols()<80) {ps("this screen mode (%1x hex) has < 80 chars/line,\
- so is not suitable for AAEMACS\n",m); exit(1);}
- pr(CW,"%s\\"DICTFILE,DIR); dictname=copyof(CW);
- for(i=0;i<strsize;i++) CW[i]=0;
- for(i=0;i<256;i++) altnames[i]=altshortnames[i]=0;
- for(j=0;s=info[j];j++) {w=strlen(s); if(w) if(s[w-1]==LF) s[--w]=0;
- i=atoi(s); for(k=0;k<w;k++) if(s[k]==' ') s[k]=0;
- if(!s[4]?1:!s[21]) {ps("bad line in %s\n",infofile); exit(1);}
- i&=255; altnames[i]=copyof(s+4); altshortnames[i]=copyof(s+21);}
- delete info[0]; delete info; info=0;
- bad=&emacserror; if(setjmp(*bad)){
- clearscreen(); ps("\n%s\n",Moan?:"AAEMACS aborted"); exit(1);}
- clearscreen(); line0=new line(); line0->next=line0; thisstep.f=kf(&_idle);
- B=buf0=new buffer(); B->initbuffer(); laststep.f=kf(&_idle);
- B->row1=0; B->nrows=gp_Rows; B->ncols=gp_Cols; B->lastrow=B->row1+B->nrows-1;
- for(i=0;i<256;i++) {Sl[i].lp=Sl[i].nlp=0;
- Sl[i].buf=B; Sl[i].l=line0; Sl[i].ok=0; Sl[i].sa=dustbin;}
- for(i=0;i<gp_Rows;i++) Sl[i].sa=screen+i*gp_Cols; /* screen address of line */
- /* bind the keys:- (printing chars are unbound and unbindable) */
- keys.subarray(); keys[0].subarray();
- keys[0][alt_0]=alt0;keys[0][alt_1]=alt1;keys[0][alt_2]=alt2;keys[0][alt_3]=alt3;
- keys[0][alt_4]=alt4;keys[0][alt_5]=alt5;keys[0][alt_6]=alt6;keys[0][alt_7]=alt7;
- keys[0][alt_8]=alt8;keys[0][alt_9]=alt9;keys[0][alt_minus]=altminus;
- keys[0][alt_A]=skipsentb;
- keys[0][alt_B]=skipwordb;
- keys[0][alt_C]=capitalize;
- keys[0][alt_D]=killwordf;
- keys[0][alt_E]=skipsentf;
- keys[0][alt_F]=skipwordf;
- keys[0][alt_G]=gotoline;
- keys[0][alt_H]=killwordb;
- keys[0][alt_I]=checkspelling;
- keys[0][alt_J]=finalcr;
- keys[0][alt_K]=killsent;
- keys[0][alt_L]=lwcasewords;
- keys[0][alt_M]=modemenu;
- keys[0][alt_N]=skipparaf;
- keys[0][alt_O]=gotodir;
- keys[0][alt_P]=skipparab;
- keys[0][alt_Q]=formatpara;
- keys[0][alt_R]=repl;
- keys[0][alt_S]=replask;
- keys[0][alt_T]=twiddlewords;
- keys[0][alt_U]=upcasewords;
- keys[0][alt_V]=page_up;
- keys[0][alt_W]=copykill;
- keys[0][alt_X]=callsubr;
- keys[0][alt_Y]=replyank;
- keys[0][alt_Z]=showregion;
- keys[0][pageup]=page_up; keys[0][pagedown]=page_down;
- keys[0][home]=gotobof; keys[0][end_]=gotoeof;
- keys[0][del_]=delet; keys[0][ins]=insertspaces;
- keys[0][leftarrow]=goleft; keys[0][rightarrow]=goright;
- keys[0][uparrow]=goup; keys[0][downarrow]=godown;
- keys[0][ctrl_uparrow]=gouppart; keys[0][ctrl_downarrow]=godownpart;
- keys[0][alt_home]=markbof; keys[0][alt_end]=markeof;
- keys[0][alt_downarrow]=skipsentf; keys[0][alt_uparrow]=skipsentb;
- keys[0][alt_leftarrow]=skipwordb; keys[0][alt_rightarrow]=skipwordf;
- keys[0][alt_del]=killwordf; keys[0][alt_bksp]=killwordb;
- keys[0][alt_insert]=openline; keys[0][alt_equals]=menu;
- keys[0][mbutton]=menu2; keys[0][rbutton]=menu2;
- keys[0][lbutton]=mouse_mode; keys[0][ctrl_2]=cbrackets;
- keys[0][mbuttond]=_idle; keys[0][rbuttond]=_idle;
- keys[0][lbuttond]=setolddot; keys[0][mousemove]=mouse_move;
- keys[0][alt_sharp]=macromenu;
- keys['A'-64]=gotobol;
- keys['B'-64]=goleft;
- keys['D'-64]=delet;
- keys['E'-64]=gotoeol;
- keys['F'-64]=goright;
- keys['G'-64]=whoa;
- keys['H'-64]=backspace;
- keys['I'-64]=tabulate;
- keys['J'-64]=newline;
- keys['K'-64]=kill_to_eol;
- keys['L'-64]=refresh_display;
- keys['M'-64]=newline;
- keys['N'-64]=godown;
- keys['O'-64]=openline;
- keys['P'-64]=goup;
- keys['Q'-64]=literalchar;
- keys['R'-64]=findb;
- keys['S'-64]=findf;
- keys['T'-64]=twiddle;
- keys['U'-64]=times4;
- keys['V'-64]=page_down;
- keys['W'-64]=killregion;
- keys['X'-64].subarray(); keys['X'-64][0].subarray();
- keys['X'-64]['B'-64]=buffermenu;
- keys['X'-64]['D'-64]=cstrchr;
- keys['X'-64]['E'-64]=calldos;
- keys['X'-64]['F'-64]=openfile;
- keys['X'-64]['I'-64]=insertfile;
- keys['X'-64]['L'-64]=lwcaseregion;
- keys['X'-64]['O'-64]=delblanklines;
- keys['X'-64]['Q'-64]=formatinsetregion;
- keys['X'-64]['S'-64]=savebuffer;
- keys['X'-64]['T'-64]=expandtabs;
- keys['X'-64]['U'-64]=upcaseregion;
- keys['X'-64]['W'-64]=writebuffer;
- keys['X'-64]['X'-64]=swopmark;
- keys['X'-64]['Y'-64]=maketabs;
- keys['X'-64]['Z'-64]=finish;
- keys['X'-64]['(']=beginmacro;
- keys['X'-64][')']=endmacro;
- keys['X'-64]['=']=showinfo;
- keys['X'-64]['1']=onewindow;
- keys['X'-64]['2']=splitwindow;
- keys['X'-64]['D']=cchrstr;
- keys['X'-64]['E']=obey;
- keys['X'-64]['F']=setrightmargin;
- keys['X'-64]['N']=renamebuffer;
- keys['X'-64]['R']=incrfindb;
- keys['X'-64]['S']=incrfindf;
- keys['X'-64]['X']=gotonbuf;
- keys['Y'-64]=yank;
- keys['Z'-64]=repeat;
- keys[esc].subarray(); keys[esc][0].subarray();
- keys[esc][0][alt_K]=bindkeybuf;
- keys[esc][0][alt_R]=replword;
- keys[esc][0][alt_S]=replwordask;
- keys[esc]['/']=leaveonewhite;
- keys[esc]['0']=esc0;keys[esc]['1']=esc1;keys[esc]['2']=esc2;keys[esc]['3']=esc3;
- keys[esc]['4']=esc4;keys[esc]['5']=esc5;keys[esc]['6']=esc6;keys[esc]['7']=esc7;
- keys[esc]['8']=esc8;keys[esc]['9']=esc9;keys[esc]['-']=escminus;
- keys[esc]['F'-64]=restorebuf;
- keys[esc]['K'-64]=unbindkey;
- keys[esc]['N'-64]=namemacro;
- keys[esc]['R'-64]=findwordb;
- keys[esc]['S'-64]=findwordf;
- keys[esc][' ']=setmark; keys[esc]['.']=pushmark; keys[esc][',']=popmark;
- keys[esc]['#']=remmark;
- keys[esc]['C']=copytext;
- keys[esc]['G']=gotocol;
- keys[esc]['J']=nofinalcr;
- keys[esc]['K']=bindkeymacro;
- keys[esc]['M']=movetext;
- keys[esc]['N']=nextwindow;
- keys[esc]['P']=prevwindow;
- keys[esc]['Q']=formatregion;
- keys[esc]['T']=twiddlelines;
- keys[esc]['X']=displayhex;
- keys[esc]['\\']=deletewhite;
- keys['_'-64]=help;
- Jerry.setup(); Jerry.settrap(0x7f,&event); /* releases & movement */
- clearscreen(); T=nargs>=2?arg[1]:".";
- n=strlen(T); for(i=0;i<n;i++) if(T[i]=='['?:T[i]==']') {T="."; break;}
- /* attrib acts odd if there is a [ or ] in the filename */
- if(setjmp(*bad)) {mi=&basemi; thisstep.f=&_idle; goto BEG;}
- fullfilename(Fn.s,T); Fn.n=strlen(Fn.s); i=attrib(Fn.s);
- if(i==3*256) { /* can't find directory, so give menu of current directory */
- strcpy(CW,"."); fullfilename(Fn.s,CW); Fn.n=strlen(Fn.s); i=attrib(Fn.s);}
- if(i&16){findbuf(val("(no file)",9))->go_to();
- window[0]=B; filefromdirmenu(Fn,"file:");}
- Macro=new macro(); Macro->name=CMN; basemi.prevstep=&laststep; /* so Macro!=0 */
- findbuf(Fn)->go_to(); laststep.f=kf(&_idle);
- window[0]=B; Moan="AAEMACS (version of "VERSION"): for help, type ctrl-_";
- BEG: set_new_handler(myhandler); if(setjmp(Exit)) goto END;
- LOOP: bad=&emacserror; if(!rept) laststep=thisstep;
- thisstep.clear(); macdepth=0; refreshscreen();
- Jerry.range(2048,2048); oldmy=1024;
- oldmx=B->longlines ? linewhere(B->dot)%gp_Cols : B->dot.r->to_tabexp(B->dot.c);
- Jerry.move(oldmy,oldmx); if(C!=B) B->olddot=B->dot; C=B; showmoan();
- gp_cursor(cursor); basemi.rec=1; T1t.s=T1w; T2t.s=T2w; Fn.s=Filename;
- B->dot2=B->dot1; B->dot1=B->dot; rept=0; Obey();
- if(!rept) if(basemi.rec) if(record) (*record)+=thisstep.copy(); goto LOOP;
- END:;}
- /*-----*//* show displays and moans */
- void showmoan(){int j=B->lastrow; if(Display)display(Display,Moan?j-1:j,0,Cyan);
- Display=0; if(Moan) display(Moan,j,0,Red); Moan=0;}
- /*-----*/
- void(*cf[3])(int,byte)={&insert_,&overlay_,&nomove_};
- subr*cff[3]={&insert,&overlay,&nomove};
- /*-----*//* obey keysequence 'c,c2' (-1 == to be read). If D!=0, arg==N */
- void Obey(val N/*=val(1,0)*/){int c,j,z=1; val Key; line*L=B->dot.r;
- char*C; keyarray *keytable,*kt[33]; prevobtype=obtype; obtype=ob_other;
- B->col.Reg=region(mark(0,0),mark(0,0)); c=getkey(); kt[1]=keytable=&keys;
- if(B->dotcc>=0) {B->dotcc2=B->dotcc; B->dotcc=-1;} thisstep.N=N;
- ARRAY: if(c<0) {keyseqc[z++]=0; keyseqc[z++]=-c; Key=(*keytable)[0][-c];}
- else {keyseqc[z++]=c; Key=(*keytable)[c];} keyseqc[0]=z;
- switch(Key.n){ /* = type of key binding. N.i==repeat count, usually */
- case _unbound: if(c>=32) if(z==2) {cf[B->overlay](int(N.i),c); /* character */
- /* auto newline:- */
- if(B->wrap) if(L->to_tabexp(B->dot.c)>=B->rmargin-1) L->split_if_long();
- thisstep.f=kf(cff[B->overlay]); thisstep.T1=char(c); break;}
- C=keyseqc+keyseqc[0]; /* unbound esc letter treated as alt-letter:- */
- if(C[-2]=='['-64) if(C[-3]) if(isalpha(j=to_upper(c))){
- c=-esc_alt[j-'A']; keytable=kt[z-=2]; goto ARRAY;}
- pr(CW,"%k not defined. For help type ctrl-_",&keyseq);basemi.rec=0;MOAN(CW);
- case _subr: thisstep.f=Key; Key.f(N,val(),val()); break;
- case _macro: thisstep.f=Key; if(N.n) thisstep.N=N; (*Key.m)(N); break;
- case _keyarray: pr(CW,"%k typed: more please",&keyseq);
- display(CW,B->lastrow,0,Cyan); c=getkey(); B->redraw_info();
- if(c>0) c=to_upper(c); kt[z]=keytable=Key.k; goto ARRAY;
- case _buffer: thisstep.f=kf(&buffer_);
- thisstep.T1=copyof(Key.b->name?:"(no file)");
- (*Key.b)(int(N.i)); break; default: break;} mi=&basemi;}
- /*-----*/
- int Yesno(char*s) {char c,d; do{ps("%s",s);
- c=d=Getchar(); while(d!=LF) d=Getchar();
- if(c=='Y'?1:c=='y') return 1; if(c=='N'?1:c=='n') return 0;
- ps("(answer 'y' or 'n') ");} while(1);}
- /*-----*/
- #define _(name,args) {&name,0,#name,args}
- #define __(name,Name,args) {&name,0,Name,args}
- /* initial _adinf == 'do not evaluate this func call at compile time' */
- short AS[]={_adinf,_string,0},
- A[]={_adinf,0,0},
- III[]={_int,_int,_int,0},
- II[]={_int,_int,0},
- IK[]={_int,_keyseq,0},
- IL[]={_int,_label,0},
- IMS[]={_int,_magic,_string,0},
- IM[]={_int,_magic,0},
- IF[]={_int,666,666,0},
- ISK[]={_int,_string,_keyseq,0},
- ISS[]={_int,_string,_string,0},
- IS[]={_int,_string,0},
- I[]={_int,0},
- KCA[]={_keyseq,_char,_adinf},
- AIS[]={_adinf,_int,_string,0}; /* allowed arg patterns */
- Func funcname[]= {
- __(_andthen,"%O,",A),
- _(currentbuffer,AS),
- __(keyseq_,"keyseq",KCA),
- _(lastkill,AS),
- __(_yesno,"yesno",AIS),
- {0,0,0,0}};
- Subr subrname[]= {_(_idle,I), _(accentedletters,I), _(alt0,I), _(alt1,I),
- _(alt2,I), _(alt3,I), _(alt4,I), _(alt5,I), _(alt6,I), _(alt7,I), _(alt8,I),
- _(alt9,I), _(altminus,I), _(backspace,I), _(beginmacro,ISK), _(bindkeybuf,IK),
- _(bindkeymacro,IK), _(bindkeysubr,ISK), __(buffer_,"buffer",ISK),
- _(buffermenu,I), _(calldos,IS), _(callsubr,IS), _(capitalize,I), _(casemode,I),
- _(cbrackets,I), _(cccmode,I), _(cchrstr,I), _(char_from_menu,I), _(checkhelp,I),
- _(checkspelling,I), _(copybufmodes,IS), _(copydir,I), _(copykill,I),
- _(copytext,I), _(cstrchr,I), _(delblanklines,I), _(delbuf,I),
- __(delet,"delete",I), _(deletewhite,I), __(dis_play,"display",IS),
- _(displayhex,I), _(emptyappendix,I), _(endmacro,I), _(esc0,I), _(esc1,I),
- _(esc2,I), _(esc3,I), _(esc4,I), _(esc5,I), _(esc6,I), _(esc7,I), _(esc8,I),
- _(esc9,I), _(escminus,I), _(expandtabs,I), _(finalcr,I), _(findb,IM),
- _(findf,IM), _(findwordb,IS), _(findwordf,IS), _(finish,I),
- _(formatinsetregion,ISS), _(formatpara,I), _(formatregion,I), _(getappendix,I),
- _(godown,I), _(godownpart,I), _(goleft,I), _(goright,I), _(gotobof,I),
- _(gotobol,I), _(gotocol,I), _(gotodir,I), _(gotoeof,I), _(gotoeol,I),
- _(gotoline,I), _(gotonbuf,I), _(goup,I), _(gouppart,I), __(go_to,"goto",IL),
- _(help,IK), __(If,"if",IF), _(incrfindb,I), _(incrfindf,I), _(insert,IS),
- _(insertfile,IS), _(insertspaces,I), _(kill_to_eol,I), _(killregion,I),
- _(killsent,I), _(killwordb,I), _(killwordf,I), _(leaveonewhite,I),
- _(linefeed,I), _(literalchar,I), _(longlinesmode,I), _(lwcaseregion,I),
- _(lwcasewords,I), _(macromenu,I), _(maketabs,I), _(markbof,I), _(markeof,I),
- _(menu,I), _(mergefile,IS), _(modemenu,I), _(mouse_mode,I), _(mouse_move,I),
- _(movetext,I), _(namemacro,ISK), _(newline,I), _(nextwindow,I), _(nofinalcr,I),
- _(nomove,IS), _(obey,IK), _(onewindow,I), _(openfile,IS), _(openline,I),
- _(overlay,IS), _(overlaymode,I), _(page_down,I), _(page_up,I), _(popmark,I),
- _(prbindings,I), _(prbuffers,I), _(prevwindow,I), _(prkeynames,I), _(prkeys,I),
- _(prmacro,I), _(prmacros,I), _(prsubrnames,I), _(pushmark,I), _(putappendix,I),
- _(readmacros,IS), _(refresh_display,I), _(remmark,I), _(renamebuffer,IS),
- _(repeat,I), _(repl,IMS), _(replask,IMS), _(replword,ISS), _(replwordask,ISS),
- _(replyank,I), _(restorebuf,I), _(savebuffer,I), _(setmark,I), _(setolddot,I),
- _(setrightmargin,I), _(setsortcol,I), _(showinfo,I), _(showregion,I),
- _(skipafterwordmode,I), _(skipparab,I), _(skipparaf,I), _(skipsentb,I),
- _(skipsentf,I), _(skipwordb,I), _(skipwordf,I), _(sortlines,I),
- _(splitwindow,IS), _(swopmark,I), _(tabmode,I), _(tabulate,I), _(times4,I),
- _(twiddle,I), _(twiddlelines,I), _(twiddlewords,I), _(twodmode,I),
- _(unbindkey,IK), _(upcaseregion,I), _(upcasewords,I), _(version,I), _(whoa,I),
- _(wrapmode,I), _(writebuffer,IS), _(yank,I), {0,0,0,0}};
- #undef _
-