edit.mod 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. MODULE edit;
  2. (* A small full-screen editor, Turbo-Pascal style: a window containing a
  3. multi-line editor with its own status row, plus a File menu
  4. (New / Save / Quit).
  5. Arrows, Home/End, PgUp/PgDn move; Ins toggles insert/overwrite; Tab
  6. indents; the mouse positions the cursor and the wheel scrolls. It loads
  7. and saves "tvision-edit.txt". *)
  8. FROM InOut IMPORT WriteString, WriteLn;
  9. FROM SYSTEM IMPORT ADR, CAST;
  10. IMPORT tv;
  11. CONST
  12. mNew = 100;
  13. mSave = 101;
  14. mQuit = 102;
  15. mModula2 = 110;
  16. mPascal = 111;
  17. mOberon = 112;
  18. VAR
  19. ed : tv.EditorData;
  20. edp : tv.EditorDataPtr;
  21. w, edView, bar : tv.ViewPtr;
  22. e : tv.Event;
  23. quit : BOOLEAN;
  24. cmd : INTEGER;
  25. sender : tv.ViewPtr;
  26. status : ARRAY [0..95] OF CHAR;
  27. PROCEDURE Sample;
  28. BEGIN
  29. tv.EditorInit(edp);
  30. tv.EditorAddLine(edp, "MODULE hello; (* sample for tvision-m2 *)");
  31. tv.EditorAddLine(edp, "");
  32. tv.EditorAddLine(edp, "FROM InOut IMPORT WriteString, WriteLn;");
  33. tv.EditorAddLine(edp, "");
  34. tv.EditorAddLine(edp, "BEGIN");
  35. tv.EditorAddLine(edp, " WriteString('Hello, world.'); WriteLn");
  36. tv.EditorAddLine(edp, "END hello.");
  37. ed.dirty := FALSE
  38. END Sample;
  39. PROCEDURE Refresh;
  40. BEGIN
  41. IF tv.EditorDirty(edp) THEN
  42. tv.SetText(bar, " Ins/arrows/Tab edit F10: menu * modified")
  43. ELSE
  44. tv.SetText(bar, " Ins/arrows/Tab edit F10: menu saved")
  45. END;
  46. tv.Finish
  47. END Refresh;
  48. BEGIN
  49. IF NOT tv.Init() THEN
  50. WriteString("not a tty"); WriteLn; HALT
  51. END;
  52. edp := CAST(tv.EditorDataPtr, ADR(ed));
  53. tv.MenuBarInit;
  54. tv.MenuAdd("File");
  55. tv.MenuItemAdd("New", mNew);
  56. tv.MenuItemAdd("Save", mSave);
  57. tv.MenuSep;
  58. tv.MenuItemAdd("Quit", mQuit);
  59. tv.MenuAdd("Syntax");
  60. tv.MenuItemAdd("Modula-2", mModula2);
  61. tv.MenuItemAdd("Pascal", mPascal);
  62. tv.MenuItemAdd("Oberon", mOberon);
  63. bar := tv.NewView(tv.VText, 0, tv.Rows() - 1, tv.Cols(), 1);
  64. tv.AddView(tv.Desk(), bar);
  65. w := tv.NewView(tv.VWindow, 1, 1, tv.Cols() - 2, tv.Rows() - 3);
  66. tv.SetText(w, "Editor");
  67. tv.AddView(tv.Desk(), w);
  68. IF NOT tv.EditorLoad(edp, "tvision-edit.txt") THEN Sample END;
  69. tv.EditorSetLanguage(edp, tv.langModula2);
  70. edView := tv.NewView(tv.VEditor, 2, 2, tv.Cols() - 4, tv.Rows() - 5);
  71. tv.EditorAttach(edView, CAST(tv.EditorDataPtr, ADR(ed)));
  72. tv.AddView(w, edView);
  73. tv.FocusView(edView);
  74. Refresh;
  75. quit := FALSE;
  76. WHILE NOT quit DO
  77. IF tv.Resized() THEN Refresh END;
  78. IF tv.ReadEvent(e) THEN
  79. IF (e.kind = tv.evKey) AND (e.key = tv.kEsc) THEN
  80. quit := TRUE
  81. ELSE
  82. cmd := tv.HandleEvent(e);
  83. sender := tv.Sender();
  84. IF cmd = mQuit THEN
  85. quit := TRUE
  86. ELSIF cmd = mNew THEN
  87. Sample; Refresh
  88. ELSIF cmd = mSave THEN
  89. IF tv.EditorSave(edp, "tvision-edit.txt") THEN
  90. tv.Message("Save", "Saved tvision-edit.txt.")
  91. ELSE
  92. tv.Message("Save", "Could not save the file.")
  93. END;
  94. Refresh
  95. ELSIF cmd = mModula2 THEN
  96. tv.EditorSetLanguage(edp, tv.langModula2); Refresh
  97. ELSIF cmd = mPascal THEN
  98. tv.EditorSetLanguage(edp, tv.langPascal); Refresh
  99. ELSIF cmd = mOberon THEN
  100. tv.EditorSetLanguage(edp, tv.langOberon); Refresh
  101. ELSIF cmd = tv.cmClose THEN
  102. quit := TRUE
  103. END
  104. END;
  105. IF NOT quit THEN Refresh END
  106. END
  107. END;
  108. tv.Done
  109. END edit.