implement Brutusext; # Name: con "Brutus entry"; include "sys.m"; sys: Sys; include "draw.m"; draw: Draw; Context: import draw; include "bufio.m"; bufio: Bufio; Iobuf: import bufio; include "regex.m"; regex: Regex; include "tk.m"; tk: Tk; include "wmlib.m"; wmlib: Wmlib; include "brutus.m"; include "brutusext.m"; init(s: Sys, d: Draw, b: Bufio, t: Tk, w: Wmlib) { sys = s; draw = d; bufio = b; tk = t; wmlib = w; regex = load Regex Regex->PATH; } create(parent: string, t: ref Tk->Toplevel, name, args: string): string { (text, err) := gather(parent, args); if(err != nil) return err; err = tk->cmd(t, "text "+name+" -tabs {1c} -wrap none -font /fonts/pelm/latin1.9.font"); if(len err > 0 && err[0] == '!') return err; (n, maxw) := nlines(text); if(maxw < 40) maxw = 40; if(maxw > 70) maxw = 70; tk->cmd(t, name+" configure -height "+string n+".01h -width "+string maxw+"w"); return tk->cmd(t, name+" insert end '"+text); } gather(parent, args: string): (string, string) { argl := tokenize(args); nargs := len argl; if(nargs == 0) return (nil, "usage: excerpt [start] [end] file"); file := hd argl; argl = tl argl; b := bufio->open(fullname(parent, file), Bufio->OREAD); if(b == nil) return (nil, sys->sprint("can't open %s: %r", file)); start := ""; end := ""; if(argl != nil){ start = hd argl; if(tl argl != nil) end = hd tl argl; } (text, err) := readall(b, start, end); return (text, err); } tokenize(s: string): list of string { l: list of string; i := 0; a := ""; first := 1; while(i < len s){ (a, i) = arg(first, s, i); if(a != "") l = a :: l; first = 0; } rl: list of string; while(l != nil){ rl = hd l :: rl; l = tl l; } return rl; } arg(first: int, s: string, i: int): (string, int) { while(i 1) { n := nleadtab(hd l); if(tabstrip == -1 || n < tabstrip) tabstrip = n; } } # remove tabstrip tabs from each line # and concatenate in reverse order ans := ""; for(l=revlines; l != nil; l = tl l) { s := hd l; if(tabstrip > 0 && len s > 1) s = s[tabstrip:]; ans = s + ans; } return ans; } nleadtab(s: string) : int { slen := len s; for(i:=0; i maxw) maxw = w; w = 0; } else if(s[i] == '\t') w += 5; else w++; } if(len s>0 && s[len s-1]!='\n') { n++; if(w > maxw) maxw = w; } return (n, maxw); } match(pat, line: string, lineno: int): int { if(pat == "") return 1; case pat[0] { '0' to '9' => return int pat <= lineno; '/' => if(len pat < 3 || pat[len pat-1]!='/') return -1; re := compile(pat[1:len pat-1]); if(re == nil) return -1; match := regex->execute(re, line); return match != nil; } return -1; } pats: list of (string, Regex->Re); compile(pat: string): Regex->Re { l := pats; while(l != nil){ (p, r) := hd l; if(p == pat) return r; l = tl l; } (re, nil) := regex->compile(pat, 0); pats = (pat, re) :: pats; return re; } cook(parent: string, nil: int, args: string): (ref Brutusext->Celem, string) { (text, err) := gather(parent, args); if(err != nil) return (nil, err); el1 := ref Brutusext->Celem(Brutusext->Text, text, nil, nil, nil, nil); el2 := ref Brutusext->Celem(Brutus->Type*Brutus->NSIZE+Brutus->Size10, "", el1, nil, nil, nil); el1.parent = el2; ans := ref Brutusext->Celem(Brutus->Example, "", el2, nil, nil, nil); el2.parent = ans; return (ans, ""); } fullname(parent, file: string): string { if(len parent==0 || (len file>0 && (file[0]=='/' || file[0]=='#'))) return file; for(i:=len parent-1; i>=0; i--) if(parent[i] == '/') return parent[0:i+1] + file; return file; }