{**************************************************************************} {** Text Editor Written By Franz Ayestaran 25 Mar 92 Pascal Assignment 2 **} {** **} program TextEditor; uses crt, graph, dos; type List = array[1..85,1..35] of char; type List2 = array[1..85,1..35] of char; const device = 'lpt1'; var row1, colm1, GraphDriver, GraphMode, FileIndexX, FileIndexY, FoundCR, VerifyChar, CurrentCursorXPos, CurrentCursorYPos, size, count, color, RowTemp, ColmTemp, TextColor, PageReadWrite, GraphicWrite, BufferReadWrite, DisplayChar, BufferIndexX, BufferIndexY, CurrentIndexX, CurrentIndexY, IndexStartX, IndexEndX, IndexStartY, IndexEndY, PageIndexX, PageIndexY, PageLength, PageStart, LMargin, LMarkerX, LMarkerY, RMargin, RMarkerX, RMarkerY : Integer; FileChar : text; Filename : string[20]; Command : string[127]; charactor,charactor1,char1 : char; var Page : List; var Buffer : List2; {---------------------------------------------------------------} Procedure InitGraphics; Var Gd, Gm : integer; begin Gd := Detect; InitGraph(Gd, Gm, ''); if GraphResult <> grOK then Halt(1); end; {---------------------------------------------------------------} Procedure Title; begin SetBkColor(1); color:=(GetMaxColor); SetTextStyle(DefaultFont,HorizDir,1); SetColor(8); OutTextXY(30,3,' TEXT EDITOR WRITTEN BY FRANZ AYESTARAN 25th MARCH 1992'); end; {--------------------------------------------------------------------------} Procedure Menu; begin SetColor(4); OutTextXY(5,GetMaxY-30,'F1[ ] F2[ ] F3[ ]'); SetColor(1); OutTextXY(5,GetMaxY-30,' New Load Save'); SetColor(4); OutTextXY(235,GetMaxY-30,'F4[ ] F5[ ] F6[ ] F7[ ]'); SetColor(1); OutTextXY(235,GetMaxY-30,' L Mar - L Mar + R Mar - R Mar +'); SetColor(4); OutTextXY(5,GetMaxY-15,'F8[ ] F9[ ] ESC[ ]'); SetColor(1); OutTextXY(5,GetMaxY-15,' Print Dos Quit'); SetColor(4); OutTextXY(235,GetMaxY-15,''); SetColor(1); OutTextXY(235,GetMaxY-15,''); end; {--------------------------------------------------------------------------} Procedure Border; begin SetColor(7); RecTangle(0,0,GetMaxX,38); FloodFill(2,2,7); SetColor(7); RecTangle(0,GetMaxY-40,GetMaxX,GetMaxY); FloodFill(1,GetMaxY-39,7); SetColor(9); RecTangle(0,GetMaxY-39,GetMaxX,GetMaxY); RecTangle(1,GetMaxY-38,GetMaxX-1,GetMaxY-1); end; {--------------------------------------------------------------------------} Procedure Ruler; begin SetColor(6); RecTangle(0,13,GetMaxX+1,38); RecTangle(1,14,GetMaxX,37); SetColor(5); OutTextXY(0,16,'1 10 20 30 40'); OutTextXY(312,16,' 50 60 70 80'); CurrentCursorXPos := -1; for count :=1 to 81 do begin SetColor(9); OutTextXY(CurrentCursorXPos,21,'.'); CurrentCursorXPos := CurrentCursorXPos + 8; end; CurrentCursorXPos := -7; for count := 1 to 10 do begin SetColor(6); OutTextXY(CurrentCursorXPos,31,'I'); CurrentCursorXPos := CurrentCursorXPos + 80; end; end; {--------------------------------------------------------------------------} Procedure LMarginMarker; begin SetColor(white); OutTextXY(LMarkerX,LMarkerY,chr(31)); OutTextXY(LMarkerX,GetMaxY-90,chr(30)); end; {--------------------------------------------------------------------------} Procedure RMarginMarker; begin SetColor(white); OutTextXY(RMarkerX,RMarkerY,chr(31)); OutTextXY(RMarkerX,GetMaxY-90,chr(30)); end; {--------------------------------------------------------------------------} Procedure EditPage; begin BufferIndexX := 1; BufferIndexY := 1; FoundCr := 0; for PageIndexY := IndexStartY to IndexEndY do begin for PageIndexX := IndexStartX to IndexEndX do begin if PageReadWrite = 0 then charactor1 := Page[PageIndexX,PageIndexY]; if BufferReadWrite = 0 then charactor1 := Buffer[BufferIndexX,BufferIndexY]; if GraphicWrite = 1 then begin SetColor(0); for count := 1 to 7 do begin RecTangle(row1,colm1,row1+count,colm1+count); end; SetColor(yellow); end; if BufferReadWrite = 1 then Buffer[BufferIndexX,BufferIndexY] := charactor1; if PageReadWrite = 1 then Page[PageIndexX,PageIndexY] := charactor1; if DisplayChar = 1 then begin SetColor(TextColor); if charactor1 = chr(13) then SetColor(2); if charactor1 = chr(10) then SetColor(7); OutTextXY(row1,colm1,charactor1); end; if VerifyChar = 1 then begin if Charactor1 = chr(10) then begin CurrentIndexX := PageIndexX; CurrentIndexY := PageIndexY; CurrentCursorXPos := row1; CurrentCursorYPos := colm1; FoundCr := 1; end; end; row1 := row1 + 8; BufferIndexX := BufferIndexX + 1; end; row1 := rowTemp; BufferIndexX := 1; colm1 := colm1 + 11; BufferIndexY := BufferIndexY + 1; end; GraphicWrite := 0; end; {--------------------------------------------------------------------------} Procedure PutCursor; begin SetColor(10); RecTangle(CurrentCursorXPos+1,CurrentCursorYPos+9, CurrentCursorXPos+7,CurrentCursorYPos+9); end; {--------------------------------------------------------------------------} Procedure EraseCursor; begin SetColor(8); RecTangle(CurrentCursorXPos+1,CurrentCursorYPos+9, CurrentCursorXPos+7,CurrentCursorYPos+9); end; {--------------------------------------------------------------------------} Procedure NewPage; begin IndexStartX := 1; IndexStartY := 1; IndexEndX := 80; IndexEndY := 35; row1 := 0; colm1 := 11; RowTemp := row1; ColmTemp := colm1; setcolor(0); ClearViewPort; PageReadWrite := 1; BufferReadWrite := 1; GraphicWrite := 0; DisplayChar := 1; VerifyChar := 0; charactor1 := chr(255); EditPage; LMarginMarker; RMarginMarker; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure ShowPage; begin IndexStartX := PageStart; IndexStartY := 1; IndexEndX := 80; IndexEndY := PageLength; row1 := 0; colm1 := 11; RowTemp := row1; ColmTemp := colm1; SetColor(yellow); ClearViewPort; PageReadWrite := 0; BufferReadWrite := 3; GraphicWrite := 0; DisplayChar := 1; VerifyChar := 0; TextColor := 14; EditPage; PutCursor; LMarginMarker; RMarginMarker; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure SetUpGraphics; begin SetViewPort(0,0,GetMaxX,GetMaxY,clipon); Border; Title; Ruler; Menu; SetViewPort(0,40,GetMaxX,GetMaxY-40,clipon); SetBKColor(8); end; {--------------------------------------------------------------------------} Procedure SetUpPage; begin PageStart := 1; PageLength := 1; LMargin := 1; RMargin := 75; CurrentCursorYPos := 11; CurrentIndexX := LMargin; CurrentIndexY := 1; LMarkerX := 0; LMarkerY := 1; RMarkerX := GetMaxX - 8 * 5; RMarkerY := 1; CurrentCursorXPos := LMarkerX; NewPage; LMarginMarker; RMarginMarker; PutCursor; end; {--------------------------------------------------------------------------} Procedure ExecComm; begin CloseGraph; repeat write('Enter Dos Command: '); readln(Command); if Command <> '' then begin SwapVectors; Exec(GetEnv('COMSPEC'), '/C ' + Command); SwapVectors; if DosError <> 0 then writeln('Could not execute COMMAND.COM'); writeln; end; until Command = ''; InitGraphics; SetUpGraphics; ShowPage; CurrentCursorXPos := LMarkerX; CurrentCursorYPos := 11; CurrentIndexX := LMargin; CurrentIndexY := 1; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure OpenFile; Begin CloseGraph; Write('Please Enter File To Open: '); Readln(Filename); InitGraphics; SetupGraphics; NewPage; assign (FileChar, Filename); reset (FileChar); FileIndexX := LMargin; FileIndexY := 1; while not eof (FileChar) do begin read(FileChar,char1); Page[FileIndexX,FileIndexY] := char1; if char1 = chr(10) then begin FileIndexX := LMargin-1; FileIndexY := FileIndexY + 1; end; FileIndexX := FileIndexX + 1; end; Close(FileChar); Page[1,FileIndexY] := chr(32); PageLength := FileIndexY; ShowPage; CurrentCursorXPos := LMarkerX; CurrentCursorYPos := 11; CurrentIndexX := LMargin; CurrentIndexY := 1; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure SaveFile; Begin CloseGraph; Write('Please Enter File To Save: '); Readln(Filename); InitGraphics; SetupGraphics; assign (FileChar, Filename); rewrite(FileChar); FileIndexX := 1; FileIndexY := 1; repeat begin char1 := Page[FileIndexX,FileIndexY]; FoundCr := 0; if char1 = chr(10) then begin write(FileChar,char1); FileIndexX := 0; FileIndexY := FileIndexY + 1; FoundCr := 1; end; if char1 = chr(255) then begin write(FileChar,chr(13)); write(FileChar,chr(10)); FileIndexX := 0; FileIndexY := FileIndexY + 1; FoundCr := 1; end; if FoundCr = 0 then write(FileChar,char1); FileIndexX := FileIndexX + 1; end; until FileIndexY = PageLength; close(FileChar); ShowPage; CurrentIndexX := LMargin; CurrentCursorXPos := LMarkerX; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure PrintText; Begin assign (FileChar,device); rewrite(FileChar); FileIndexX := 1; FileIndexY := 1; repeat begin char1 := Page[FileIndexX,FileIndexY]; FoundCr := 0; if char1 = chr(10) then begin write(FileChar,char1); FileIndexX := 0; FileIndexY := FileIndexY + 1; FoundCr := 1; end; if FileIndexX > RMargin then begin write(FileChar,chr(13)); write(FileChar,chr(10)); FileIndexX := 0; FileIndexY := FileIndexY + 1; FoundCr := 1; end; if char1 = chr(255) then begin write(FileChar,chr(13)); write(FileChar,chr(10)); FileIndexX := 0; FileIndexY := FileIndexY + 1; FoundCr := 1; end; if FoundCr = 0 then write(FileChar,char1); FileIndexX := FileIndexX + 1; end; until FileIndexY = PageLength; close(FileChar); ShowPage; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure NextLine; begin EraseCursor; CurrentCursorXPos := LMarkerX; CurrentCursorYPos := CurrentCursorYPos + 11; CurrentIndexX := LMargin; CurrentIndexY := CurrentIndexY + 1; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure ReadCurrentPageChar; begin row1 := CurrentCursorXPos; Colm1 := CurrentCursorYPos; PageReadWrite := 0; BufferReadWrite := 3; GraphicWrite := 0; DisplayChar := 0; VerifyChar := 0; EditPage; end; {--------------------------------------------------------------------------} Procedure CursorLeft; begin EraseCursor; IndexStartX := CurrentIndexX - 1; IndexEndX := CurrentIndexX - 1; IndexStartY := CurrentIndexY; IndexEndY := CurrentIndexY; ReadCurrentPageChar; EraseCursor; if CurrentIndexX > LMargin then begin CurrentCursorXPos := CurrentCursorXPos - 8; CurrentIndexX := CurrentIndexX - 1; end; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure CursorRight; begin EraseCursor; IndexStartX := CurrentIndexX + 1; IndexEndX := CurrentIndexX + 1; IndexStartY := CurrentIndexY; IndexEndY := CurrentIndexY; ReadCurrentPageChar; if charactor1 = chr(13) then begin EraseCursor; CurrentIndexX := LMargin - 1; CurrentIndexY := CurrentIndexY + 1; CurrentCursorXPos := -8; CurrentCursorYPos := CurrentCursorYPos + 11; end; if (CurrentIndexX < RMargin) and (charactor1 < chr(255)) then begin CurrentCursorXPos := CurrentCursorXPos + 8; CurrentIndexX := CurrentIndexX + 1; end; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure CursorUp; begin IndexStartX := CurrentIndexX; IndexEndX := CurrentIndexX; IndexStartY := CurrentIndexY - 1; IndexEndY := CurrentIndexY - 1; ReadCurrentPageChar; if Charactor1 < chr(255) then begin EraseCursor; if CurrentIndexY > PageStart then begin CurrentCursorYPos := CurrentCursorYPos - 11; CurrentIndexY := CurrentIndexY - 1; end; end; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure CursorDown; begin IndexStartX := CurrentIndexX; IndexEndX := CurrentIndexX; IndexStartY := CurrentIndexY + 1; IndexEndY := CurrentIndexY + 1; ReadCurrentPageChar; EraseCursor; if (CurrentIndexY <= PageLength) and (Charactor1 < chr(255)) then begin CurrentCursorYPos := CurrentCursorYPos + 11; CurrentIndexY := CurrentIndexY + 1; end; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure ShowChar; begin if CurrentIndexY < 34 then begin SetColor(0); for count := 1 to 7 do begin RecTangle(CurrentCursorXPos,CurrentCursorYPos+count, CurrentCursorXPos+count,CurrentCursorYPos); end; EraseCursor; SetColor(yellow); if Charactor = chr(10) then SetColor(7); if Charactor = chr(13) then SetColor(2); Page[CurrentIndexX,CurrentIndexY] := charactor; charactor := Page[CurrentIndexX,CurrentIndexY]; outtextXY(CurrentCursorXPos,CurrentCursorYPos,charactor); CurrentCursorXPos := CurrentCursorXPos + 8; CurrentIndexX := CurrentIndexX + 1; PutCursor; Page[CurrentIndexX,CurrentIndexY] := chr(32); end; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure ScanLineForCR; Begin Row1 := 0; PageReadWrite := 0; BufferReadWrite := 3; GraphicWrite := 0; DisplayChar := 0; IndexStartX := 1; IndexEndX := 80; VerifyChar := 1; EditPage; end; {--------------------------------------------------------------------------} Procedure CR; begin if CurrentIndexY < 34 then begin EraseCursor; Colm1 := CurrentCursorYPos; IndexStartY := CurrentIndexY; IndexEndY := CurrentIndexY; ScanLineForCR; if FoundCR = 1 then begin CurrentIndexX := LMargin; CurrentIndexY := CurrentIndexY + 1; CurrentCursorXPos := LMarkerX; CurrentCursorYPos := CurrentCursorYPos + 11; PutCursor; end; if FoundCr = 0 then begin ShowChar; Charactor := chr(10); Page[CurrentIndexX,CurrentIndexY] := charactor; ShowChar; EraseCursor; CurrentCursorXPos := LMarkerX; CurrentIndexX := LMargin; CurrentCursorYPos := CurrentCursorYPos + 11; CurrentIndexY := CurrentIndexY + 1; PutCursor; Page[CurrentIndexX,CurrentIndexY] := chr(32); PageLength := PageLength + 1; end; end; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure BackSpace; begin EraseCursor; SetColor(0); CurrentIndexX := CurrentIndexX - 1; CurrentCursorXPos := CurrentCursorXPos - 8; if (CurrentCursorXPos < LMarkerX) and ( CurrentCursorYPos > 11) then begin Colm1 := CurrentCursorYPos - 11; IndexStartY := CurrentIndexY - 1; IndexEndY := CurrentIndexY - 1; ScanLineForCr; Page[CurrentIndexX,CurrentIndexY] := chr(32); SetColor(0); for count := 1 to 7 do begin RecTangle(CurrentCursorXPos,CurrentCursorYPos+count, CurrentCursorXPos+count,CurrentCursorYPos); end; CurrentIndexX := CurrentIndexX - 1; CurrentCursorXPos := CurrentCursorXPos - 8; FoundCR := 1 end; Page[CurrentIndexX,CurrentIndexY] := chr(32); SetColor(0); for count := 1 to 7 do begin RecTangle(CurrentCursorXPos,CurrentCursorYPos+count, CurrentCursorXPos+count,CurrentCursorYPos); end; if FoundCr = 1 then begin Page[1,CurrentIndexY + 1] := chr(0); PageLength := CurrentIndexY; end; SetColor(7); PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure Delete; begin EraseCursor; SetColor(0); CurrentIndexX := CurrentIndexX + 1; CurrentCursorXPos := CurrentCursorXPos + 8; Page[CurrentIndexX,CurrentIndexY] := chr(32); for count := 1 to 7 do begin RecTangle(CurrentCursorXPos,CurrentCursorYPos+count, CurrentCursorXPos+count,CurrentCursorYPos); end; SetColor(7); PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure StartLineStop; begin EraseCursor; CurrentCursorXPos := LMarkerX; CurrentIndexX := LMargin; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure CHome; begin EraseCursor; CurrentCursorXPos := LMarkerX; CurrentIndexX := LMargin; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure CEnd; begin EraseCursor; CurrentCursorXpos := RMarkerX; CurrentIndexX := RMargin; PutCursor; Charactor := chr(0); end; {--------------------------------------------------------------------------} Procedure GetChar; Begin repeat charactor := readkey; if CurrentIndexX < LMargin then StartLineStop; if CurrentIndexX > RMargin + 1 then Charactor := chr(13); case charactor of chr(8) : BackSpace; chr(83) : Delete; chr(13) : CR; chr(75) : CursorLeft; chr(77) : CursorRight; chr(72) : CursorUp; chr(80) : CursorDown; chr(60) : OpenFile; chr(61) : SaveFile; chr(32) : showchar; chr(71) : CHome; chr(79) : CEnd; chr(73) : ShowPage; chr(81) : ShowPage; chr(59) : SetUpPage; chr(66) : PrintText; chr(67) : ExecComm; chr(62) : begin if LMarkerX > 0 then begin SetColor(0); OutTextXY(LMarkerX,LMarkerY,chr(31)); OutTextXY(LMarkerX,GetMaxY-90,chr(30)); LMarkerX := LMarkerX -8; LMargin := LMargin -1; CurrentIndexX := LMargin; EraseCursor; CurrentCursorXPos := LMarkerX; PutCursor; LMarginMarker; end; Charactor := chr(0); end; chr(63) : begin if LMarkerX < 8 * 19 then begin SetColor(0); OutTextXY(LMarkerX,LMarkerY,chr(31)); OutTextXY(LMarkerX,GetMaxY-90,chr(30)); LMarkerX := LMarkerX +8; LMargin := LMargin +1; CurrentIndexX := LMargin; EraseCursor; CurrentCursorXPos := LMarkerX; PutCursor; LMarginMarker; end; Charactor := chr(0); end; chr(64) : begin if RMarkerX > 8 * 59 then begin SetColor(0); OutTextXY(RMarkerX,RMarkerY,chr(31)); OutTextXY(RMarkerX,GetMaxY-90,chr(30)); RMarkerX := RMarkerX -8; RMargin := RMargin -1; RMarginMarker; end; Charactor := chr(0); end; chr(65) : begin if RMarkerX < GetMaxX-8 then begin SetColor(0); OutTextXY(RMarkerX,RMarkerY,chr(31)); OutTextXY(RMarkerX,GetMaxY-90,chr(30)); RMarkerX := RMarkerX +8; RMargin := RMargin +1; RMarginMarker; end; Charactor := chr(0); end; end; if (charactor > chr(0)) and (charactor <> chr(27)) then ShowChar; until charactor = chr(27); end; {--------------------------------------------------------------------------} { ******************** } { *** Main Program *** } begin InitGraphics; SetupGraphics; SetUpPage; GetChar; CloseGraph; end.