Charles.Forsyth20070905-203875a8a1a19 years agoHistory
1588 lines · 23.2 KB · Brainfuck
RawDownload
## Editor# implement Editor; include "sys.m";   sys: Sys;include "draw.m";include "bufio.m";	bufio: Bufio;	Iobuf: import bufio;include "regex.m";	regex: Regex;	Re: import regex;include "sh.m";	sh: Sh; Editor: module {	init: fn(nil: ref Draw->Context, args: list of string);}; FNSIZE: con 128;		# file name LBSIZE: con 4096;		# max line size BLKSIZE: con 4096;		# block size in temp file NBLK: con 8191;		# max size of temp file ESIZE: con 256;			# max size of reg exp GBSIZE: con 256;		# max size of global command MAXSUB: con 9;		# max number of sub reg exp ESCFLG: con 16rFFFF;	# escape Rune - user defined code EOF: con -1;BytesPerRune: con 2;RunesPerBlock: con BLKSIZE / BytesPerRune; APPEND_GETTTY, APPEND_GETSUB, APPEND_GETCOPY, APPEND_GETFILE: con iota; Subexp: adt {	rsp, rep: int;}; Globp: adt {	s: string;	isnil: int;}; addr1: int;addr2: int;anymarks: int;col: int;count: int;dol: int;dot: int;fchange: int;file: string;genbuf := array[LBSIZE] of int;given: int;globp: Globp;iblock: int;ichanged: int;io: ref Sys->FD;iobuf: ref Iobuf;lastc: int;line := array [70] of byte;linebp := -1;linebuf := array [LBSIZE] of int;listf: int;listn: int;loc1: int;loc2: int;names := array [26] of int;oblock: int;oflag: int;pattern: Re;peekc: int;pflag: int;rescuing: int;rhsbuf := array [LBSIZE/2] of int;savedfile: string;subnewa: int;subolda: int;subexp: array of Subexp;tfname: string;tline: int;waiting: int;wrapp: int;zero: array of int;drawctxt: ref Draw->Context; Q: con "";T: con "TMP";WRERR: con "WRITE ERROR";bpagesize := 20;hex: con "0123456789abcdef";linp: int;nlall := 128;tfile: ref Sys->FD;vflag := 1; debug(s: string){	sys->print("%s", s);} init(ctxt: ref Draw->Context, args: list of string){	drawctxt = ctxt; 	sys = load Sys Sys->PATH;	bufio = load Bufio Bufio->PATH;	if (bufio == nil) {		sys->fprint(sys->fildes(2), "can't load %s\n", Bufio->PATH);		return;	}	regex = load Regex Regex->PATH;	if (regex == nil) {		sys->fprint(sys->fildes(2), "can't load %s\n", Regex->PATH);		return;	} #	notify(notifyf); 	if (args != nil)		args = tl args; 	if (args != nil && hd args == "-o") {		oflag = 1;		vflag = 0;		args = tl args;	} 	if (args != nil && hd args == "-") {		vflag = 0;		args = tl args;	} 	if (oflag) {		savedfile = "/fd/1";		globp = ("a", 0);	} else if (args != nil) {		savedfile = hd args;		globp = ("r", 0);	}	else		globp = (nil, 1);	zero = array [nlall + 5] of int;	tfname = mktemp("/tmp/eXXXXX");#	debug(sys->sprint("tfname %s\n", tfname));	_init();	for(;;){		{			commands();			quit();		}exception{		"savej" =>			;		}	}} casee(c: int){	setnoaddr();	if(vflag && fchange) {		fchange = 0;		error(Q);	}	filename(c);	_init();	addr2 = 0;	caseread();} casep(){	newline();	printcom();} caseq(){	setnoaddr();	newline();	quit();} caseread(){#debug("caseread " + file);	if((io=sys->open(file, Sys->OREAD)) == nil) {		lastc = '\n';		error(file);	}	iobuf = bufio->fopen(io, Sys->OREAD);	setwide();	squeeze(0);	c := 0 != dol;	append(APPEND_GETFILE, addr2);	exfile(Sys->OREAD); 	fchange = c;} commands(){	a1: int;	c, temp: int;	lastsep: int; 	for(;;) {		if(pflag) {			pflag = 0;			addr1 = addr2 = dot;			printcom();		}		c = '\n';		for(addr1 = -1;;) {			lastsep = c;			a1 = address();			c = getchr();			if(c != ',' && c != ';')				break;			if(lastsep == ',')				error(Q);			if(a1 < 0) {				a1 = 1;				if(a1 > dol)					a1--;			}			addr1 = a1;			if(c == ';')				dot = a1;		}		if(lastsep != '\n' && a1 < 0)			a1 = dol;		if((addr2=a1) < 0) {			given = 0;			addr2 = dot;			} else			given = 1;		if(addr1 < 0)			addr1 = addr2;#debug(sys->sprint("%d,%d %c\n", addr1, addr2, c));		case c {		'a' =>			add(0);			continue; 		'b' =>			nonzero();			browse();			continue; 		'c' =>			nonzero();			newline();			rdelete(addr1, addr2);			append(APPEND_GETTTY, addr1-1);			continue; 		'd' =>			nonzero();			newline();			rdelete(addr1, addr2);			continue; 		'E' =>			fchange = 0;			c = 'e';			casee(c);			continue; 		'e' =>			casee(c);			continue; 		'f' =>			setnoaddr();			filename(c);			putst(savedfile);			continue; 		'g' =>			global(1);			continue; 		'i' =>			add(-1);			continue; 		'j' =>			if(!given)				addr2++;			newline();			join();			continue; 		'k' =>			nonzero();			c = getchr();			if(c < 'a' || c > 'z')				error(Q);			newline();			names[c-'a'] = zero[addr2] & ~16r1;			anymarks |= 16r1;			continue; 		'm' =>			move(0);			continue; 		'n' =>			listn++;			newline();			printcom();			continue; 		'\n' =>			if(a1 < 0) {				a1 = dot+1;				addr2 = a1;				addr1 = a1;			}			if(lastsep==';')				addr1 = a1;			printcom();			continue; 		'l' =>			listf++;			casep();			continue; 		'p' or 'P' =>			casep();			continue; 		'Q' =>			fchange = 0;			caseq();			continue; 		'q' =>			caseq();			continue; 		'r' =>			filename(c);			caseread();			continue; 		's' =>			nonzero();			substitute(!globp.isnil);			continue; 		't' =>			move(1);			continue; 		'u' =>			nonzero();			newline();			if((zero[addr2]&~8r01) != subnewa)				error(Q);			zero[addr2] = subolda;			dot = addr2;			continue; 		'v' =>			global(0);			continue; 		'W' or 'w' =>			if (c == 'W')				wrapp++;			setwide();			squeeze(dol>0);			temp = getchr();			if(temp != 'q' && temp != 'Q') {				peekc = temp;				temp = 0;			}			filename(c);			if(!wrapp ||			  ((io = sys->open(file, Sys->OWRITE)) == nil) ||			  ((sys->seek(io, big 0, Sys->SEEKEND)) < big 0))				if((io = sys->create(file, Sys->OWRITE, 8r0666)) == nil)					error(file);			iobuf = bufio->fopen(io, Sys->OWRITE);			wrapp = 0;			if(dol > 0)				putfile();			exfile(Sys->OWRITE);			if(addr1<=1 && addr2==dol)				fchange = 0;			if(temp == 'Q')				fchange = 0;			if(temp)				quit();			continue; 		'=' =>			setwide();			squeeze(0);			newline();			count = addr2 - 0;			putd();			putchr('\n');			continue; 		'!' =>			callunix();			continue; 		EOF =>			return; 		}		error(Q);	}} printcom(){	a1: int; 	nonzero();	a1 = addr1;	do {		if(listn) {			count = a1-0;			putd();			putchr('\t');		}		putshst(getline(zero[a1++]));	} while(a1 <= addr2);	dot = addr2;	listf = 0;	listn = 0;	pflag = 0;}  address(): int{	sign, a, opcnt, nextopand, b, c: int; 	nextopand = -1;	sign = 1;	opcnt = 0;	a = dot;	do {		do {			c = getchr();		} while(c == ' ' || c == '\t');		if(c >= '0' && c <= '9') {			peekc = c;			if(!opcnt)				a = 0;			a += sign*getnum();		} else		case c {		'$' or '.' =>			if (c == '$')				a = dol;			if(opcnt)				error(Q); 		'\'' =>			c = getchr();			if(opcnt || c < 'a' || c > 'z')				error(Q);			a = 0;			do {				a++;			} while(a <= dol && names[c-'a'] != (zero[a] & ~8r01)); 		'?' or '/' =>			if (c == '?')				sign = -sign;			compile(c);			b = a;			for(;;) {				a += sign;				if(a <= 0)					a = dol;				if(a > dol)					a = 0;				if(match(a))					break;				if(a == b)					error(Q);			}			break; 		* =>			if(nextopand == opcnt) {				a += sign;				if(a < 0 || dol < a)					continue;       # error(Q); 

Lines 1–500 of 1588.