MODULE edit; (* A small full-screen editor, Turbo-Pascal style: a window containing a multi-line editor with its own status row, plus a File menu (New / Save / Quit). Arrows, Home/End, PgUp/PgDn move; Ins toggles insert/overwrite; Tab indents; the mouse positions the cursor and the wheel scrolls. It loads and saves "tvision-edit.txt". *) FROM InOut IMPORT WriteString, WriteLn; FROM SYSTEM IMPORT ADR, CAST; IMPORT tv; CONST mNew = 100; mSave = 101; mQuit = 102; mModula2 = 110; mPascal = 111; mOberon = 112; mSize = 120; mMax = 121; VAR ed : tv.EditorData; edp : tv.EditorDataPtr; w, edView, bar : tv.ViewPtr; e : tv.Event; quit : BOOLEAN; cmd : INTEGER; sender : tv.ViewPtr; status : ARRAY [0..95] OF CHAR; s1, s2, tmp : ARRAY [0..31] OF CHAR; iW, iH, nw, nh : INTEGER; PROCEDURE ToInt (s : ARRAY OF CHAR) : INTEGER; VAR i, n : INTEGER; neg : BOOLEAN; BEGIN i := 0; n := 0; neg := FALSE; IF s[0] = '-' THEN neg := TRUE; i := 1 END; WHILE (i <= VAL(INTEGER, HIGH(s))) AND (s[i] >= '0') AND (s[i] <= '9') DO n := n * 10 + VAL(INTEGER, ORD(s[i]) - ORD('0')); i := i + 1 END; IF neg THEN n := 0 - n END; RETURN n END ToInt; PROCEDURE Sample; BEGIN tv.EditorInit(edp); tv.EditorAddLine(edp, "MODULE hello; (* sample for tvision-m2 *)"); tv.EditorAddLine(edp, ""); tv.EditorAddLine(edp, "FROM InOut IMPORT WriteString, WriteLn;"); tv.EditorAddLine(edp, ""); tv.EditorAddLine(edp, "BEGIN"); tv.EditorAddLine(edp, " WriteString('Hello, world.'); WriteLn"); tv.EditorAddLine(edp, "END hello."); ed.dirty := FALSE END Sample; PROCEDURE Refresh; BEGIN IF tv.EditorDirty(edp) THEN tv.SetText(bar, " Ins/arrows/Tab edit F10: menu * modified") ELSE tv.SetText(bar, " Ins/arrows/Tab edit F10: menu saved") END; tv.Finish END Refresh; BEGIN IF NOT tv.Init() THEN WriteString("not a tty"); WriteLn; HALT END; edp := CAST(tv.EditorDataPtr, ADR(ed)); tv.MenuBarInit; tv.MenuAdd("File"); tv.MenuItemAdd("New", mNew); tv.MenuItemAdd("Save", mSave); tv.MenuSep; tv.MenuItemAdd("Quit", mQuit); tv.MenuAdd("Window"); tv.MenuItemAdd("Size...", mSize); tv.MenuItemAdd("Maximize", mMax); tv.MenuAdd("Syntax"); tv.MenuItemAdd("Modula-2", mModula2); tv.MenuItemAdd("Pascal", mPascal); tv.MenuItemAdd("Oberon", mOberon); bar := tv.NewView(tv.VText, 0, tv.Rows() - 1, tv.Cols(), 1); tv.AddView(tv.Desk(), bar); w := tv.NewView(tv.VWindow, 1, 1, tv.Cols() - 2, tv.Rows() - 3); tv.SetText(w, "Editor"); tv.AddView(tv.Desk(), w); IF NOT tv.EditorLoad(edp, "tvision-edit.txt") THEN Sample END; tv.EditorSetLanguage(edp, tv.langModula2); tv.EditorSetIndent(edp, 2); edView := tv.NewView(tv.VEditor, 2, 2, tv.Cols() - 4, tv.Rows() - 5); edView^.flags := edView^.flags + tv.vfExpand; (* grow with the window *) tv.EditorAttach(edView, CAST(tv.EditorDataPtr, ADR(ed))); tv.AddView(w, edView); tv.FocusView(edView); Refresh; quit := FALSE; WHILE NOT quit DO IF tv.Resized() THEN Refresh END; IF tv.ReadEvent(e) THEN IF (e.kind = tv.evKey) AND (e.key = tv.kEsc) THEN quit := TRUE ELSE cmd := tv.HandleEvent(e); sender := tv.Sender(); IF cmd = mQuit THEN quit := TRUE ELSIF cmd = mNew THEN tv.EditorInit(edp); tv.EditorSetLanguage(edp, tv.langModula2); tv.EditorSetIndent(edp, 2); Refresh ELSIF cmd = mSave THEN IF tv.EditorSave(edp, "tvision-edit.txt") THEN tv.Message("Save", "Saved tvision-edit.txt.") ELSE tv.Message("Save", "Could not save the file.") END; Refresh ELSIF cmd = mSize THEN tv.IntToText(s1, w^.rect.w, 0); tv.IntToText(s2, w^.rect.h, 0); tv.Dialog("Window size", 34, 9); iW := tv.DlgInput("Width:", s1); iH := tv.DlgInput("Height:", s2); tv.DlgButton("OK", tv.cmOK); tv.DlgButton("Cancel", tv.cmCancel); IF tv.DlgRun() = tv.cmOK THEN tv.DlgTextAt(iW, s1); tv.DlgTextAt(iH, s2); nw := ToInt(s1); nh := ToInt(s2); tv.WindowResize(w, nw, nh); Refresh END ELSIF cmd = mMax THEN tv.WindowResize(w, tv.Cols() - 2, tv.Rows() - 3); Refresh ELSIF cmd = mModula2 THEN tv.EditorSetLanguage(edp, tv.langModula2); Refresh ELSIF cmd = mPascal THEN tv.EditorSetLanguage(edp, tv.langPascal); Refresh ELSIF cmd = mOberon THEN tv.EditorSetLanguage(edp, tv.langOberon); Refresh ELSIF cmd = tv.cmClose THEN quit := TRUE END END; IF NOT quit THEN Refresh END END END; tv.Done END edit.