home *** CD-ROM | disk | FTP | other *** search
- { MSMAIN.PAS
- MS 4.0
- Copyright (c) 1985, 87 by Borland International, Inc. }
-
- {$I msdirect.inc}
-
- unit MsMain;
- {-Top level routines for MicroStar}
-
- interface
-
- uses
- Crt, {Basic video operations - standard unit}
- Dos, {DOS interface - standard unit}
- Errors, {Runtime error handler}
- MsVars, {Global types and declarations}
- MsScrn1, {Fast screen writing routines}
- MsString, {String primitives}
- MsPtrOp, {Pointer primitives}
- EscSeq, {Returns text string for extended scan codes}
- MsCmds, {Maps keystrokes to commands}
- Int24, {DOS critical error handler}
- Message, {Message system}
- MsUser, {User keyboard input, line edit, error report, help}
- MsMemOp, {Text buffer allocation and deallocation}
- MsBack, {Background processes}
- MsScrn2, {Editor screen updating}
- MsMenu, {Pulldown and custom menu system}
- MsDir, {Popup directory picker}
- MsEdit, {Basic editing commands}
- MsText, {Text processing commands}
- MsTabs, {Tab commands}
- MsBlok, {Block move, copy, delete}
- MsFind, {Find, replace routines}
- MsFile, {File I/O routines}
- MsMacro, {Macro editing and processing}
- MsSet, {Setup and installation}
- MsPrtM, {Print job setup}
- MsSpell, {Spelling checker}
- Invoke, {General purpose DOS shell}
- MsInvoke; {DOS shell specialized for MicroStar}
-
- procedure EdInitialize;
- {-Initialize editor variables and data structures}
-
- procedure EdMainMenu;
- {-Get command line parameters and set up for initial file}
-
- procedure EdSchedule;
- {-Schedule editor}
-
- {==========================================================================}
-
- implementation
-
- var
- Argcount : Byte; {Number of DOS command line arguments}
-
- procedure EdProcessMacroChars(CurrentUserChars : Integer);
- forward;
- {-Processes characters pushed in a Find and Macro command}
-
- procedure EdInitialize;
- {-Initialize editor variables and data structures}
- var
- HelpFile : Filepath; {Pathname to indexed help file}
- MacroFile : Filepath; {Pathname to default macro file}
- PrtDefFile : Filepath; {Pathname to default printer definition}
-
- procedure EdInitPromptLine;
- {-Set up the prompt line to display help for the menu system}
- var
- MenuStr : VarString;
- Kptr : Integer;
- Special : Boolean;
-
- begin {EdInitPromptLine}
- FillChar(PromptLine[1], PhyScrCols, Blank);
- PromptCol := 1;
- if ShowMenuHelp then begin
- Kptr := 1;
- EdClearString(MenuStr);
- while Kptr <= Length(MenuPrime) do
- MenuStr := MenuStr+EdTextRepresentation(MenuPrime, Kptr, Special);
- MenuStr := MenuStr+EdGetMessage(394);
- MaxPromptChars := Pred(PhyScrCols-Length(MenuStr));
- MenuHelpPos := Succ(PhyScrCols-Length(MenuStr));
- FillChar(HelpPromptLine[1], PhyScrCols, Blank);
- Move(MenuStr[1], HelpPromptLine[MenuHelpPos], Length(MenuStr));
- Move(HelpPromptLine[1], PromptLine[1], PhyScrCols);
- end else
- MaxPromptChars := PhyScrCols;
- end; {EdInitPromptLine}
-
- function EdReturnFullPath(Path : Filepath) : Filepath;
- {-Return default directory if path is empty}
-
- begin {EdReturnFullPath}
- if Length(Path) = 0 then
- GetDir(0, Path);
- EdReturnFullPath := EdAddTrailingBackslash(Path);
- end; {EdReturnFullPath}
-
- begin {EdInitialize}
-
- if EdStringEmpty(AddwinPrime) then begin
- {If the add window command is installed as nothing, we can't do much}
- WriteLn;
- WriteLn(EdGetMessage(62));
- Halt(1);
- end;
-
- {Initialize the first window}
- Curwin :=
- EdAllocateWindow
- (LogtopScr, {Physical top of window}
- LogscrRows, {Length of window}
- Line1, {Initial position in window}
- Col1, {Initial position in window}
- NoFile {Editing no file}
- );
-
- {Define top and only window}
- Window1 := Curwin;
- Window1^.Fwdlink := Window1;
- Window1^.Backlink := Window1;
-
- {Set initial zoom state}
- Zoomed := False;
- if SaveInitZoomState then
- EdZoomWindow(False);
-
- {Last cursor position}
- LastPosition.Line := Curwin^.TopLine;
- LastPosition.Col := 1;
-
- {Define the support path}
- SupportPath := EdReturnFullPath(SaveSupportPath);
-
- {Build path names for support files}
- HelpFile := SupportPath+ProgName+'.'+HelpFileExt;
- MacroFile := SupportPath+ProgName+'.'+MacroFileExt;
- PrtDefFile := SaveDeviceName;
-
- {See whether help is available}
- HelpAvailable := EdExistFile(HelpFile);
- if HelpAvailable then begin
- {Open help file and leave it open}
- Assign(Helpf, HelpFile);
- Reset(Helpf, 1);
- end;
-
- {Get the default macro file if found}
- if EdExistFile(MacroFile) then
- EdReadMacroFile(MacroFile);
-
- {Initialize printer definition}
- EdPrintSetDefaults(PrtDefFile);
-
- {Initialize the prompt line to show menu help}
- EdInitPromptLine;
-
- end; {EdInitialize}
-
- procedure EdMainMenu;
- {-Get command line arguments and determine initial keystrokes of editor}
- var
- CmdString, Arg : String255;
- Fcount : Integer;
-
- begin {EdMainMenu}
- if Argcount > 0 then begin
- {Take DOS command line for file names, up to 3 windows}
- EdClearString(CmdString);
- Fcount := 1;
- while (Argcount > 0) and (Fcount <= 3) do begin
- Arg := ParamStr(Fcount);
- CmdString := CmdString+AddwinPrime+Arg+^M;
- if EdHasWildCards(Arg) then begin
- {Display menu system if first parameter had wildcards}
- if Fcount = 1 then
- CmdString := MenuPrime+'FO'+Arg+^M;
- {Stop taking arguments after one containing wildcards}
- Argcount := 1;
- end;
- Inc(Fcount);
- Dec(Argcount);
- end;
- Argcount := 0;
- end else if OpeningMenu then
- {Display menu for File Open}
- CmdString := MenuPrime+'FO'
- else
- {Start up with noname file}
- CmdString := AddwinPrime+^X^M;
-
- {Push commands into keyboard buffer}
- EdUserPush(CmdString);
- end; {EdMainMenu}
-
- procedure EdFindReplace(ApplyMacro : Boolean);
- {-Process find and replace command}
- label
- ExitPoint;
- const
- Xmin = 5;
- XSize = 70;
- YSize = 5;
- var
- Ch : Char;
- NotFound, ShowUpdates, NoReplace,
- Done, HaveWindow, CursorState : Boolean;
- Ymin, Count, Fcol : Integer;
- P : PlineDesc;
- M : BlockMarker;
- W : WindowPtr;
- CmdPrompt, Prompt : VarString;
-
- procedure RestoreScreen;
- {-Get rid of the prompt window if appropriate}
-
- begin {RestoreScreen}
- if HaveWindow then begin
- {Remove window}
- EdRestoreWindow(W, Xmin, Ymin, XSize, YSize);
- {Restore cursor}
- SolidCursor := CursorState;
- EdSetCursorOff;
- if EdPtrIsNil(CurrMenu) then begin
- EdShowMenuHelp;
- EdUpdateCursor;
- end;
- end;
- end; {RestoreScreen}
-
- procedure EdReplacestring(StartCol : Integer);
- {-Perform string replacement}
- var
- EndOfSearch, EndOfReplace : Integer;
- LenDiff : Integer;
-
- begin {EdReplacestring}
- with Curwin^ do begin
-
- Modified := True;
- EndOfSearch := StartCol+Length(SearchStr);
- EndOfReplace := StartCol+Length(ReplaceStr);
- LenDiff := Length(ReplaceStr)-Length(SearchStr);
-
- if LenDiff < 0 then begin
- {Line getting shorter}
- with Curline^ do begin
- Move(Txt^[EndOfSearch], Txt^[EndOfReplace], Succ(Bufflen-EndOfSearch));
- FillChar(Txt^[Succ(Bufflen)+LenDiff], -LenDiff, Blank);
- end;
- {Fix up markers}
- EdFixBlockInsertedSpace(Curline, StartCol, LenDiff);
- EdCheckNoMarker;
- EdFixMarkInsertedSpace(Curline, StartCol, LenDiff);
-
- end else if LenDiff > 0 then begin
- {Line getting longer}
- if not EdSizeline(Curline, Curline^.Bufflen+LenDiff, True) then begin
- EdErrormsg(35);
- {Stop searching}
- Done := True;
- Exit;
- end;
- with Curline^ do
- Move(Txt^[EndOfSearch], Txt^[EndOfReplace], Succ(Bufflen-EndOfReplace));
- {Fix up markers}
- EdFixBlockInsertedSpace(Curline, StartCol, LenDiff);
- EdFixMarkInsertedSpace(Curline, StartCol, LenDiff);
-
- end;
-
- Move(ReplaceStr[1], Curline^.Txt^[StartCol], Length(ReplaceStr));
- EdResetPageLine(Curwin);
- end;
- end; {EdReplacestring}
-
- procedure EdModifyString(StartCol : Integer);
- {-Select between replace and macro application}
- var
- CurrentUserChars : Integer;
- LfindUpper : Boolean;
- LfindBackward : Boolean;
- LfindWholeWord : Boolean;
- Lpreview : Boolean;
- Lglobal : Boolean;
- LblockFind : Boolean;
- LsearchStr : VarString;
-
- begin {EdModifyString}
- case LastSearchOp of
- Replace :
- EdReplacestring(StartCol);
-
- RunMacro :
- begin
-
- {Store number of characters already pushed (nested macros)}
- CurrentUserChars := EditUsercommandInput;
-
- {Store global search parameters in case changed by macro}
- LsearchStr := SearchStr;
- LfindUpper := Findupper;
- LfindBackward := Findbackward;
- LfindWholeWord := Findwholeword;
- Lpreview := Preview;
- Lglobal := Global;
- LblockFind := Blockfind;
-
- {Push macro onto typeahead buffer}
- EdInsertMacro(Macronum, 1);
-
- {Process characters}
- {Special handler to avoid problems if a macro calls EdFind}
- Recurring := True;
- EdProcessMacroChars(CurrentUserChars);
- Recurring := False;
-
- {Restore search parameters}
- SearchStr := LsearchStr;
- Findupper := LfindUpper;
- Findbackward := LfindBackward;
- Findwholeword := LfindWholeWord;
- Preview := Lpreview;
- Global := Lglobal;
- Blockfind := LblockFind;
- LastSearchOp := RunMacro;
-
- {Guarantee abort checking is active again}
- AbortEnable := True;
- end;
- end;
- end; {EdModifyString}
-
- procedure EdAdjustEOLmatch;
- {-Adjust replace string when searching for ^M^J}
- var
- EolPos : Integer;
-
- begin {EdAdjustEOLmatch}
- EolPos := Pos(^M^J, SearchStr);
- if (Length(ReplaceStr) > 1) and (EolPos = Pred(Length(SearchStr))) then begin
- EolPos := Pos(^M^J, ReplaceStr);
- if EolPos = Pred(Length(ReplaceStr)) then
- {Truncate ^M^J from replace string}
- ReplaceStr := Copy(ReplaceStr, 1, Length(ReplaceStr)-2);
- end;
- end; {EdAdjustEOLmatch}
-
- begin {EdFindReplace}
-
- {Protect against recursive find-and-macro calls}
- if Recurring then begin
- EdErrormsg(73);
- Exit;
- end;
-
- AbortEnable := True;
- LastSearchOp := None;
-
- if PromptForInput and (EditUsercommandInput = 0) then begin
- {Set up a prompt box}
- EdEraseMenuHelp;
- EdUpdateCmdLine;
- HaveWindow := True;
- with Curwin^ do
- Ymin := (Firstlineno+Lastlineno) shr 1;
- if Ymin > 20 then
- Ymin := 20;
- if EdPtrNotNil(CurrMenu) then
- if Ymin < 10 then
- Ymin := 10;
- if ApplyMacro then
- if Ymin > 10 then
- Ymin := 10;
- W := EdSetupWindow(Border, Xmin, Ymin, Pred(Xmin+XSize), Pred(Ymin+YSize), NormalBox);
- CursorState := SolidCursor;
- SolidCursor := False;
- end else
- HaveWindow := False;
-
- if PromptForInput then begin
- if HaveWindow then begin
- {Display the prompt}
- Prompt := EdGetMessage(323);
- EdFastWrite(Prompt, Ymin, Xmin+(XSize-Length(Prompt)) shr 1, ScreenAttr[MfColor]);
- end;
- EdGetSearchString(Xmin, Succ(Ymin), XSize, XSize-3, HaveWindow, SearchStr);
- end;
- if Abortcmd or EdStringEmpty(SearchStr) then begin
- RestoreScreen;
- Exit;
- end;
-
- if ApplyMacro then begin
-
- {Find out which macro to use}
- if PromptForInput then begin
- if HaveWindow then begin
- {Display the prompt}
- Prompt := EdGetMessage(315);
- EdFastWrite(Prompt, Ymin, Xmin+(XSize-Length(Prompt)) shr 1, ScreenAttr[MfColor]);
- end;
- EdGetMacroNumber(304, Macronum);
- end;
- if Abortcmd or not(Macronum in [0..MaxMacro]) then begin
- RestoreScreen;
- Exit;
- end;
- if HaveWindow then
- if EdPtrNotNil(Macronames[Macronum]) then
- EdFastWrite(Macronames[Macronum]^, Ymin+2, Succ(Xmin), ScreenAttr[MnColor]);
- EdClearString(ReplaceStr);
- CmdPrompt := EdGetMessage(330);
- LastSearchOp := RunMacro;
-
- end else begin
-
- if PromptForInput then begin
- if HaveWindow then begin
- {Redraw border}
- EdDrawBox(Border, Xmin, Ymin, XSize, 5, NormalBox);
- {Display the prompt}
- Prompt := EdGetMessage(338);
- EdFastWrite(Prompt, Ymin, Xmin+(XSize-Length(Prompt)) shr 1, ScreenAttr[MfColor]);
- end;
- EdGetSearchString(Xmin, Ymin+2, XSize, XSize-3, HaveWindow, ReplaceStr);
- end;
- if Abortcmd then begin
- RestoreScreen;
- Exit;
- end;
-
- {Set up command prompt}
- CmdPrompt := EdGetMessage(339);
- LastSearchOp := Replace;
-
- end;
-
- {Ask for options}
- if HaveWindow then begin
- {Redraw border}
- EdDrawBox(Border, Xmin, Ymin, XSize, 5, NormalBox);
- {Draw new prompt}
- Prompt := EdGetMessage(337);
- EdFastWrite(Prompt, Ymin, Xmin+(XSize-Length(Prompt)) shr 1, ScreenAttr[MfColor]);
- end;
- EdGetOptions(Xmin, Ymin+3, XSize, 6, HaveWindow);
- Count := FindCount;
-
- {Get rid of prompt box}
- RestoreScreen;
-
- if Abortcmd or Goterror then
- Exit;
-
- if Blockfind then begin
- EdBlockInit;
- if Goterror then
- Exit;
- end else if Global then
- EdGlobalInit;
-
- {Adjust for end of line searches}
- EdAdjustEOLmatch;
-
- {Search for the pattern}
- NotFound := True;
- ShowUpdates := True;
- Done := False;
- {Set cursor to proper start position to avoid repeated finds}
- Fcol := EdSetStartCol(Curwin^.Colno);
-
- with Curwin^ do
- repeat
-
- if ShowUpdates then begin
- EdWritePromptLine(EdGetMessage(326));
- {Update entire screen at least once}
- EdUpdateScreen;
- {Show further updates only if previewing}
- ShowUpdates := Preview;
- end else if not(EdKeyInterrupt) then
- {Update the prompt line only if no keys are pending}
- EdWritePromptLine(EdGetMessage(327));
-
- {Find the next match}
- P := EdScanpattern(Curline, SearchStr, Fcol);
-
- if Abortcmd then
- goto ExitPoint;
-
- if EdPtrIsNil(P) then
-
- {No match, force exit}
- Done := True
-
- else begin
-
- {Found at least one occurrence of the pattern}
- NotFound := False;
-
- {Move cursor to the position found}
- M.Line := P;
- if PositionFindAtStart or Findbackward then
- M.Col := Fcol
- else
- M.Col := Fcol+Length(SearchStr);
- EdJumpMarker(M);
-
- {Assume the replacement will be done}
- NoReplace := False;
-
- {Optional preview before replacement}
- if Preview then begin
- EdWritePromptLine('');
- {Show the found string clearly}
- EdHighlightScreen(Fcol, Pred(Fcol+Length(SearchStr)), ScreenAttr[BordColor], False);
- with Curwin^ do
- if Firsttextno+Lineno < 5 then
- Ymin := Succ(Firsttextno+Lineno)
- else
- Ymin := Firsttextno+Lineno-4;
- {Prompt for action}
- EdDisplayPromptWindow(CmdPrompt+EdGetMessage(329), Ymin,
- [^Y, ^N, ^A, ^Q], Ch, NormalBox);
- if Abortcmd then
- goto ExitPoint;
-
- case Ch of
- ^Y :
- EdModifyString(Fcol);
-
- ^N :
- {Flag that replacement was not done}
- NoReplace := True;
-
- ^A :
- begin
- {Modify string and do the rest without asking}
- EdModifyString(Fcol);
- Preview := False;
- end;
-
- ^Q :
- begin
- Done := True;
- NoReplace := True;
- end;
- end;
-
- end else
- {Modify without asking}
- EdModifyString(Fcol);
-
- if not(Done or Blockfind or Global) then begin
- Dec(Count);
- if Count <= 0 then
- Done := True;
- end;
-
- if Done then begin
- {Move the cursor past the last string found or replaced}
- if not(ApplyMacro or Findbackward or PositionFindAtStart) then begin
- if NoReplace then
- Colno := Fcol+Length(SearchStr)
- else
- Colno := Fcol+Length(ReplaceStr);
- end;
- end else begin
- {Proceed to the next instance}
- if Findbackward then begin
- Dec(Colno);
- if Colno < Length(SearchStr) then
- {No hope of further matches on this line}
- if EdPtrIsNil(Curline^.Backlink) then
- {Force exit}
- Done := True
- else begin
- EdUpLine;
- EdRightLine;
- end;
- end else begin
- {Advance over the replace string to prevent left recursive search/replace}
- if not(ApplyMacro) then
- if NoReplace and EdStringEmpty(ReplaceStr) then
- {Don't get stuck in place}
- Colno := Succ(Fcol)
- else
- Colno := Fcol+Length(ReplaceStr);
- if Colno+Length(SearchStr) > Curline^.Bufflen then
- {No hope of further matches on this line}
- if EdPtrIsNil(Curline^.Fwdlink) then begin
- {Force exit}
- Done := True;
- EdRightLine;
- end else begin
- EdDownLine;
- Colno := 1;
- end;
- end;
- if Blockfind then
- if not(EdCursorInBlock(Curline, Colno, True)) then
- {Cursor moved outside of block - force exit}
- Done := True;
- {Set "find" column for next pass}
- Fcol := Colno;
- end;
- end;
-
- until Done;
-
- if NotFound then
- {Pattern not found}
- EdErrormsg(38);
-
- ExitPoint:
- ExitMenu := True;
- EdRealign;
-
- end; {EdFindReplace}
-
- procedure EdSaveFile;
- {-Save file and return to same position for further editing}
- var
- C : BlockMarker;
-
- begin {EdSaveFile}
- with Curwin^, C do begin
- Line := Curline;
- Col := Colno;
- if Filename = NoFile then begin
- if EdWriteNamedFile then
- ;
- end else
- EdFileWrite(Filename, False);
- end;
- EdJumpMarker(C);
- end; {EdSaveFile}
-
- procedure EdSaveQuit;
- {-Save file and leave editor if all windows are closed}
-
- begin {EdSaveQuit}
- repeat
- with Curwin^ do begin
- if Modified then begin
- {Only save if modified}
- if Filename = NoFile then begin
- {Get a filename before writing}
- if not(EdWriteNamedFile) then
- exit;
- end else
- EdFileWrite(Filename, (WindowCount = 1));
- if Goterror then
- Exit;
- end;
- EdShutWindow(True);
- if WindowCount >= 1 then
- {Give some visual feedback while saving multiple files}
- EdUpdateScreen;
- end;
- until WindowCount < 1;
- end; {EdSaveQuit}
-
- procedure EdAbandonAllFiles;
- {-Abandon all open windows and exit to DOS}
-
- begin {EdAbandonAllFiles}
- repeat
- EdAbandonFile(True);
- if AbortCmd then
- exit;
- if WindowCount >= 1 then
- EdUpdateScreen;
- until WindowCount < 1;
- end; {EdAbandonAllFiles}
-
- procedure EdFindNext;
- {-Process find next command}
-
- begin {EdFindNext}
- PromptForInput := False;
- case LastSearchOp of
- Find : EdFind;
- Replace : EdFindReplace(False);
- RunMacro : EdFindReplace(True);
- end;
- PromptForInput := True;
- end; {EdFindNext}
-
- procedure EdNewFile;
- {-Read a new file into an existing window}
- var
- Fname : Filepath;
-
- begin {EdNewFile}
-
- {See if current window is modified, and if so, prompt to save it}
- if not CheckCurwinModified then
- exit;
-
- if WindowCount > 0 then
- if EdLinkedWindow(Curwin) then begin
- {Create a new text stream for the window}
- if not(EdNewTextStream(Curwin)) then
- Exit;
- Curwin^.Filename := NoFile;
- end else begin
- {Clearing the text stream from memory may take a while}
- EdWait;
- EdDeleteAllText(Curwin);
- EdResetWindow(Curwin);
- end;
-
- if WindowCount <= 1 then
- {No files currently open, indicate by blank screen}
- ClrScr
- else
- EdUpdateScreen;
-
- repeat
- Abortcmd := False;
- Goterror := False;
- {Read the new file into memory}
- Fname := EdGetFileName(EdGetMessage(320), DefExtension, EdYcenterWindow(3), 0, LastFileEdit, True);
- if Abortcmd then begin
- {Shut the window or enter the menu system}
- Abortcmd := False;
- Goterror := False;
- EdShutWindow(False);
- end else if not(Goterror) then
- EdReadFile(Fname);
- until not(Goterror);
-
- end; {EdNewFile}
-
- procedure EdAddWindow;
- {-Add a text window, get a file for it, read it in, and move to it}
- label
- ExitPoint;
- var
- WindToDivide : Integer;
- Rezoom : Boolean;
-
- begin {EdAddWindow}
-
- {Add a window only if allowed}
- if (WindowCount >= MaxWindows) then begin
- EdErrormsg(127);
- Exit;
- end;
-
- {Undo zoom if currently on}
- Rezoom := Zoomed;
- if Rezoom then
- EdZoomWindow(False);
-
- if WindowCount > 0 then begin
- {Get most likely window to divide into two}
- WindToDivide := EdGetWindowToDivide;
-
- {Divide window if possible}
- EdWindowCreate(WindToDivide);
- if Goterror then
- {Could not create window, message already displayed}
- goto ExitPoint;
-
- {Move to new window}
- EdWindowGoto(Succ(WindToDivide));
- end;
-
- {Read the new file into memory}
- EdReadFile(EdGetFileName(EdGetMessage(320), DefExtension, EdYcenterWindow(3), 0, LastFileEdit, True));
-
- if Goterror then begin
- if WindowCount > 0 then begin
- {Didn't get file, remove window just created}
- EdWindowGoto(WindToDivide);
- EdWindowDelete(Succ(WindToDivide));
- ExitMenu := True;
- end else begin
- {Clear any text from first window, stay in menu system}
- EdDeleteAllText(Curwin);
- EdResetWindow(Curwin);
- ExitMenu := False;
- end;
- end else begin
- Inc(WindowCount);
- ExitMenu := True;
- end;
-
- ExitPoint:
- {Redo zoom if we started that way}
- if Rezoom then
- EdZoomWindow(False);
-
- end; {EdAddwindow}
-
- procedure EdSaveAndNewFile;
- {-Save current file and prompt for a new one in the same window}
- var
- Fname : Filepath;
-
- begin {EdSaveAndNewFile}
-
- {Save the file}
- with Curwin^ do begin
- if Filename = NoFile then begin
- {Get a filename and save stream to it}
- if not(EdWriteNamedFile) then
- Exit;
- end else
- EdFileWrite(Filename, False);
- if Goterror then
- Exit;
- end;
-
- {Clear the window for a new file}
- if EdLinkedWindow(Curwin) then begin
- {Create a new text stream for the window}
- if not(EdNewTextStream(Curwin)) then
- Exit;
- Curwin^.Filename := NoFile;
- end else begin
- {Clearing the text stream from memory may take a while}
- EdWait;
- EdDeleteAllText(Curwin);
- EdResetWindow(Curwin);
- end;
- if WindowCount <= 1 then
- {No files currently open, indicate by blank screen}
- ClrScr
- else
- EdUpdateScreen;
-
- {Get a new file}
- repeat
- {Reset from previous errors}
- Abortcmd := False;
- Goterror := False;
- {Read the new file into memory}
- Fname := EdGetFileName(EdGetMessage(320), DefExtension, EdYcenterWindow(3), 0, LastFileEdit, True);
- if Abortcmd then begin
- {Shut the window or enter the menu system}
- Abortcmd := False;
- Goterror := False;
- EdShutWindow(False);
- end else if not(Goterror) then
- EdReadFile(Fname);
- until not(Goterror);
-
- end; {EdSaveAndNewFile}
-
- procedure EdWriteOrAppendBlock;
- {-If a block is marked, write or append it to a file}
- var
- Fname : Filepath;
- Exists, Appending : Boolean;
- WhichItem : Integer;
-
- begin {EdWriteOrAppendBlock}
-
- {Make sure a block is marked}
- if EdNoBlock then begin
- EdErrormsg(26);
- Exit;
- end;
-
- {Get a file name to write to}
- Fname := EdGetFileName(EdGetMessage(310), DefExtension, EdYcenterWindow(3), 0, LastBlockWrite, False);
- if Abortcmd or EdStringEmpty(Fname) then
- Exit;
-
- {See if file already exists}
- Exists := EdExistFile(Fname);
- if Goterror then
- Exit;
-
- if Exists then begin
-
- {Return 0=Abort, 1=Append, 2=Overwrite}
- EdChooseAppending(WhichItem);
-
- if Abortcmd or (WhichItem = 0) then
- Exit;
-
- Appending := (WhichItem = 1);
- if not(Appending) then begin
- {Create a .BAK file}
- EdMakeBakFile(Fname);
- if Goterror then
- Exit;
- end;
-
- end else
- Appending := False;
-
- {Write the block}
- EdWriteBlock(Fname, Exists, Appending);
-
- end; {EdWriteOrAppendBlock}
-
- procedure EdPromptLogDrive;
- {-Prompt for and move to new directory}
- var
- CurDir : Filepath;
-
- begin {EdPromptLogDrive}
- {Current directory is default}
- GetDir(0, CurDir);
- EdLogDrive(EdGetFileName(EdGetMessage(317), '', EdYcenterWindow(3), 16, CurDir, False));
- end; {EdPromptLogDrive}
-
- procedure EdPrintFile;
- {-Initialize for printing, or abort current print}
-
- begin {EdPrintFile}
- if Printing then begin
- {Should printing be stopped?}
- if EdYesNo(EdGetMessage(388)) then
- EdPrintExit;
- end else if EdPrintSetup then begin
- {Initialize the print job}
- EdInitPrintState;
- {Start printing if no error during initialization}
- Printing := not(Goterror);
- end;
- end; {EdPrintFile}
-
- procedure EdProcessCommand(C : CommandType);
- {-Top level dispatcher for all editor commands}
- var
- Lastline : PlineDesc;
- Lastcol : Integer;
-
- procedure EdCursorCommands(C : CommandType);
- {-Dispatcher for cursor movement commands}
-
- begin {EdCursorCommands}
- case C of
- CmdLeftChar : EdLeftChar; {Left character}
- CmdRightChar : EdRightChar; {Right character}
- CmdLeftWord : EdLeftWord; {Left lexeme}
- CmdRightWord : EdRightWord; {Right lexeme}
- CmdUpLine : {Up line}
- begin
- EdUpLine;
- FullScroll := FullScroll+TempScroll;
- end;
- CmdDownLine : {Down line}
- begin
- EdDownLine;
- FullScroll := FullScroll+TempScroll;
- end;
- CmdScrollUp : EdScrollUp; {Scroll up}
- CmdScrollDown : EdScrollDown; {Scroll down}
- CmdDownPage : EdDownPage; {Down page}
- CmdUpPage : EdUpPage; {Up page}
- end;
- end; {EdCursorCommands}
-
- procedure EdQuickCommands(C : CommandType);
- {-Dispatcher for quick movement commands}
-
- begin {EdQuickCommands}
- case C of
- CmdWindowTopFile : EdWindowTopFile; {Top of window}
- CmdWindowBottomFile : EdWindowBottomFile; {Bottom of window}
- CmdLeftLine : EdLeftLine; {Cursor to left side}
- CmdRightLine : EdRightLine; {Cursor to right side}
- CmdTopScreen : EdTopScreen; {Top of screen}
- CmdBottomScreen : EdBottomScreen; {Bottom of screen}
- CmdMoveToBegin : EdMoveToBegin; {Move to previous BEGIN line}
- CmdMoveToEnd : EdMoveToEnd; {Move to previous END line}
- CmdCpgotoln : EdPromptGotoLine; {Goto line n}
- CmdGotoColumn : EdPromptGotoCol; {Goto column n}
- CmdGotoWindow : EdPromptGotoWindow; {Goto window n}
- CmdJumpLastPosition : EdJumpLastPosition; {Previous cursor position}
- end;
- end; {EdQuickCommands}
-
- procedure EdDeleteCommands(C : CommandType);
- {-Dispatcher for commands which delete or insert text}
-
- begin {EdDeleteCommands}
- case C of
- CmdUndo : EdUndo; {Undo last deletion}
- CmdRestoreCurrentLine : EdRestoreCurrentLine; {Restore line as on entry}
- CmdTab : EdTab; {Tab}
- CmdInsertCtrlChar : EdInsertCtrlChar; {Insert control character into text}
- CmdNewLine : EdNewLine; {New line in text buffer}
- CmdInsertLine : EdInsertLine; {Insert line}
- CmdDeleteRightChar : EdDeleteRightChar; {Delete current character}
- CmdDeleteLeftChar : EdDeleteLeftChar; {Delete left character}
- CmdDeleteRightWord : EdDeleteRightWord; {Delete right lexeme}
- CmdDeleteLineRight : EdDeleteLineRight; {Delete line right}
- CmdDeleteLine : EdDeleteLine; {Delete line}
- CmdDelLineNoRecourse : EdDeleteLineNoRecourse;
- end;
-
- {All of these commands may affect pagination}
- EdResetPageLine(Curwin);
-
- end; {EdDeleteCommands}
-
- procedure EdFileCommands(C : CommandType);
- {-Dispatcher for commands which read and write files}
-
- begin {EdFileCommands}
- case C of
- CmdFind : EdFind; {Find pattern}
- CmdFindReplace : EdFindReplace(False); {Find and replace}
- CmdFindAndMacro : EdFindReplace(True); {Search and apply macro at position}
- CmdFindNext : EdFindNext; {Find next}
- CmdInvokeDOS : EdInvokeDOS; {Invoke a DOS shell}
- CmdBackTab : EdBackTab; {Backwards tab}
- CmdNewFile : EdNewFile; {Edit a new file in current window}
- CmdAbandonFile : EdAbandonAllFiles; {Abandon all open files}
- CmdReadBlock : {Read file into window}
- EdReadtextfile(EdGetFileName(EdGetMessage(321), DefExtension,
- EdYcenterWindow(3), 0, LastBlockRead, True),
- True);
- CmdSaveFile : EdSaveFile; {Save file}
- CmdWriteBlock : EdWriteOrAppendBlock; {Write marked block to file}
- CmdSaveQuit : EdSaveQuit; {Save file and exit}
- end;
- end; {EdFileCommands}
-
- procedure EdWindowCommands(C : CommandType);
- {-Dispatcher for commands which manipulate windows}
-
- begin {EdWindowCommands}
- case C of
- CmdAddWindow : EdAddWindow; {Add another window}
- CmdSizeWindow : EdSizeWindow; {Size current window}
- CmdDoneFile : EdSaveAndNewFile; {Save and get another file in the window}
- CmdWindowDown : EdWindowDown; {Switch windows}
- CmdZoomWindow : EdZoomWindow(True); {Current window fills screen}
- CmdHelpSummary : EdHelpWindow(CmdHelpHelp); {Write a help summary}
- end;
- end; {EdWindowCommands}
-
- procedure EdBlockCommands(C : CommandType);
- {-Dispatcher for block operations and markers}
-
- begin {EdBlockCommands}
- case C of
- CmdBlockBegin : EdBlockBegin; {Begin block}
- CmdBlockEnd : EdBlockEnd; {End block}
- CmdJumpTopOfBlock : EdJumpMarker(Blockfrom); {Top of block}
- CmdJumpBottomBlock : EdJumpMarker(Blockto); {Bottom of block}
- CmdBlockCopy : EdBlockCopy; {Copy block}
- CmdBlockMove : EdBlockMove; {Move block}
- CmdBlockDelete : EdBlockDelete; {Delete block}
- CmdBlockHide : EdBlockHide; {Hide/display toggle block}
- CmdBlockWord : EdBlockWord; {Mark current word as block}
- CmdToggleTextMarker : EdToggleTextMarker; {Toggle text marker display}
- CmdSetMarker0..CmdSetMarker9 :
- EdSetMarker(Ord(C)-Ord(CmdSetMarker0)); {Set marker}
- CmdJumpMarker0..CmdJumpMarker9 :
- EdJumpMarker(Marker[Ord(C)-Ord(CmdJumpMarker0)]); {Jump marker}
- end;
-
- {Assure that current line buffer is updated}
- Blockop := True;
- end; {EdBlockCommands}
-
- procedure EdMacroCommands(C : CommandType);
- {-Dispatcher for commands which manipulate and insert macros}
- var
- Junk : Filepath;
-
- begin {EdMacroCommands}
- case C of
- CmdReadMacroFile :
- begin
- {Load a set of macros from disk}
- Junk := '*.'+MacroFileExt;
- EdReadMacroFile(
- EdGetFileName(EdGetMessage(307), MacroFileExt, EdYcenterWindow(3), 0, Junk, True));
- end;
- CmdWriteMacroFile :
- begin
- {Write current macros to disk}
- EdClearString(Junk);
- EdWriteMacroFile(
- EdGetFileName(EdGetMessage(308), MacroFileExt, EdYcenterWindow(3), 0, Junk, False));
- end;
- CmdToggleMacroRecord : EdToggleMacroRecord; {Toggle macro record}
- CmdInsertScrapPrompted : EdInsertMacro(0, 0); {Insert scrap macro n times (prompted)}
- CmdInsertScrap1..CmdInsertScrap9 : {Insert scrap macro 1..9 times}
- EdInsertMacro(0, Succ(Ord(C)-Ord(CmdInsertScrap1)));
- CmdInsertMacro1..CmdInsertMacro9 : {Insert Macro n once}
- EdInsertMacro(Succ(Ord(C)-Ord(CmdInsertMacro1)), 1);
- end;
- end; {EdMacroCommands}
-
- procedure EdTextCommands(C : CommandType);
- {-Dispatcher for commands which perform word processing functions}
-
- begin {EdTextCommands}
- case C of
- CmdSysInfo : EdSysInfo; {Show info about system and editor}
- CmdShowMem : EdShowMemory; {Show available memory}
- CmdToggleInsert : EdToggleBoolean(Curwin^.Insertflag); {Toggle insert mode}
- CmdToggleAutoindent : EdToggleBoolean(Curwin^.AI); {Toggle autoindent mode}
- CmdToggleCase : EdChangeCase(Toggle); {Toggle case of character(s)}
- CmdLowerCase : EdChangeCase(ToLower); {Lower case character(s)}
- CmdUpperCase : EdChangeCase(ToUpper); {Upper case character(s)}
- CmdSetRightMargin : EdSetRightMargin; {Set a new right margin}
- CmdReformParagraph : EdReformParagraph; {Reformat a single paragraph}
- CmdToggleWordWrap : EdToggleWordWrap; {Toggle the state of word wrap}
- CmdSetLeftMargin : EdSetLeftMargin; {Set a new left margin}
- CmdToggleTabLine : EdToggleTabLine; {Toggle display of the tab line}
- CmdInsertUndoBuffer : EdInsertUndoBuffer; {Insert the undo buffer into the text}
- CmdToggleJustify : EdToggleJustify; {Toggle state of right justification}
- CmdTogglePaginate : EdTogglePaginate; {Toggle Pagination display}
- CmdToggleAttribute : EdToggleBoolean(Curwin^.AT); {Toggle on-screen attribute display}
- CmdCenterLine : EdCenterLine; {Center the current line in margins}
- CmdSetColors : EdSetColors; {Support installation of editor colors}
- CmdSaveDefaults : EdSaveDefaults; {Save editor defaults to disk}
- end;
- end; {EdTextCommands}
-
- procedure EdUtilityCommands(C : CommandType);
- {-Dispatcher for commands which perform utility functions}
-
- begin {EdUtilityCommands}
- case C of
- CmdPrintFile : EdPrintFile; {Start printing a file}
- CmdFlushUndo : EdFlushUndo; {Flush the undo buffer}
- CmdLogDrive : EdPromptLogDrive; {Prompt for and move to new directory}
- CmdDirectory : EdDirectory; {display a file directory}
- CmdGotoPage : EdPromptGotoPage; {Go to specified page}
- CmdSetTopMargin : EdSetTopMargin; {Set default top margin}
- CmdSetBotMargin : EdSetBotMargin; {Set default bottom margin}
- CmdSetPageLength : EdSetPageLength; {Set default page length}
- CmdSetUndoLimit : EdSetUndoLimit; {Set default undo limit}
- CmdToggleTabExpansion : EdToggleBoolean(ReadExpandTabs); {Toggle tab expansion on read-in}
- CmdGetDefaultExtension : EdGetDefaultExtension; {Get a new default extension}
- CmdSetTabLine : EdSetTabLine; {Set tabs based on current template}
- CmdSetTabSize : EdSetTabSize; {Set default tab size}
- CmdSaveTabLine : EdSaveTabLine; {Write current tab line into text}
- CmdEditTabLine : EdEditTabLine; {Interactively edit the tabs}
- CmdSetTempMargin : EdSetTempMargin; {Set temporary margin for word wrap}
- CmdReformBlock : EdReformBlock; {Reformat marked block}
- CmdCloseTillLast : EdAbandonFile(False); {Close windows but stay in editor}
- end;
- end; {EdUtilityCommands}
-
- procedure EdRareCommands(C : CommandType);
- {-Dispatcher for commands rarely used}
-
- begin {EdRareCommands}
- case C of
- CmdPromptSetMarker : EdPromptSetMarker; {Prompt for a marker number to set}
- CmdPromptJumpMarker : EdPromptJumpMarker; {Prompt for a marker to jump to}
- CmdPromptInsertMacro : EdPromptMacroInsert; {Prompt for a macro to insert}
- CmdToggleTabMode : EdToggleFixedTabs; {Toggle fixed tabs}
- CmdSetTempAtCursor : EdSetTempAtCursor; {Set temp margin at cursor}
- CmdSetSupportPath : EdSetSupportPath; {Set a new home directory for the editor}
- CmdToggleStripMode : EdToggleBoolean(SaveStripMode); {Toggle high bit stripping}
- CmdEditMacro : EdEditMacro; {Edit a macro definition}
- CmdWriteNamedFile : {Write current window to a new file}
- if EdWriteNamedFile then
- ;
- CmdToggleKeyHelp : EdToggleBoolean(SaveKeyHelpMode); {Toggle display of keystroke help in menus}
- CmdWindowUp : EdWindowUp; {Move to previous window on screen}
- CmdNextSentence : EdNextSentence; {Move cursor to start of next sentence}
- CmdPrevSentence : EdPrevSentence; {Move cursor to start of previous sentence}
- CmdRestoreDefaultTabs : EdSetEvenTabs(Curwin^.Tabs); {Put tabs on even boundaries}
- CmdWhatFont : EdWhatFont; {What font is cursor on}
- CmdChooseBold : EdInsertPrintFormat(^B); {Insert Bold toggle}
- CmdChooseDbl : EdInsertPrintFormat(^D); {Insert Doublestrike toggle}
- CmdChooseUnd : EdInsertPrintFormat(^S); {Insert Underscore toggle}
- CmdChooseSup : EdInsertPrintFormat(^T); {Insert Superscript toggle}
- CmdChooseSub : EdInsertPrintFormat(^V); {Insert Subscript toggle}
- CmdChooseAlt1 : EdInsertPrintFormat(^A); {Insert Alt1 font toggle}
- CmdChooseAlt2 : EdInsertPrintFormat(^N); {Insert Alt2 font toggle}
- CmdToggleRetraceMode : EdToggleRetraceMode; {Toggle snow control}
- CmdToggleSolidCursor : EdToggleSolidCursor; {Toggle display of block cursor}
- CmdToggleEga43Line : EdToggleEga43Line; {Toggle use of 25 and 43 line screen modes}
- CmdMarginRelease : EdToggleBoolean(MarginRelease); {Reset or release margin}
- CmdSpellCheck : EdSpellingCheck; {Check document for spelling}
- CmdToggleWriteTabs : EdToggleBoolean(WriteCompressTabs); {Toggle writing of tabs on output}
- CmdToggleCompressWrap : EdToggleCompressWrap; {Toggle compression of lines prior to wrap}
- CmdToggleInitZoomState : EdToggleInitZoomState; {Change initial zoom state}
- CmdHelpMenu : EdHelpMenu; {Put up a menu of help topics}
- CmdHelpHelp..CmdFunctionKeyHelp : EdHelpWindow(C); {Select a help subsection}
- end;
- end; {EdRareCommands}
-
- begin {EdProcessCommand}
-
- {Clear the command line indicator}
- EdResetPromptLine;
-
- repeat
-
- {See if menu command}
- if C = CmdMenu then begin
- {Get command from menu system}
- EdUpdateCmdLine;
- EdGetMenuChoice(C, ExitMenu);
- end else
- ExitMenu := True;
-
- {Store the soon to be a previous position}
- with Curwin^ do begin
- Lastcol := Colno;
- Lastline := Curline;
- end;
-
- {Store a global record of current command for the help system}
- GlobalCmd := C;
-
- {Branch based on basic command groups}
- case C of
- CmdLeftChar..CmdUpPage : EdCursorCommands(C);
- CmdWindowTopFile..CmdJumpLastPosition : EdQuickCommands(C);
- CmdUndo..CmdDelLineNoRecourse : EdDeleteCommands(C);
- CmdFind..CmdSaveQuit : EdFileCommands(C);
- CmdAddWindow..CmdZoomWindow : EdWindowCommands(C);
- CmdBlockBegin..CmdJumpMarker9 : EdBlockCommands(C);
- CmdReadMacroFile..CmdInsertScrap9 : EdMacroCommands(C);
- CmdSysInfo..CmdSaveDefaults : EdTextCommands(C);
- CmdPrintFile..CmdCloseTillLast : EdUtilityCommands(C);
- CmdPromptSetMarker..CmdFunctionKeyHelp : EdRareCommands(C);
- end;
-
- if not(ExitMenu) then
- {Loop within menu system}
- C := CmdMenu
- else if EdPtrNotNil(CurrMenu) then
- {Remove menus from screen}
- EdEraseMenus
- else
- {Assure menu help is on-screen}
- EdShowMenuHelp;
-
- {Reset from any effect of the called command}
- EdZapPromptLine;
- Abortcmd := False;
- Goterror := False;
- AbortEnable := False;
-
- if WindowCount > 0 then begin
-
- with LastPosition, Curwin^ do begin
- if (Curline <> Lastline) or (Colno <> Lastcol) then begin
- {Store the previous position}
- Line := Lastline;
- Col := Lastcol;
- {Reset the temporary left margin as needed}
- if WW then
- EdResetTempMargin(Curwin, False);
- end;
-
- {Buffer line if it changed}
- if Blockop or (Curline <> Lastline) then begin
- EdBufferCurrentLine;
- Blockop := False;
- {Turn off margin release}
- MarginRelease := False;
- end;
- end;
-
- {Don't update the screen inside of macros}
- if (EditUsercommandInput = 0) then begin
- {Special handling of single line scrolls for CGA speed}
- if FullScroll <> 0 then
- if Recurring then
- FullScroll := 0
- else
- EdBiosScroll;
- {The screen may need to be updated in the background}
- UpdateScreen := True;
- end;
-
- end;
-
- until ExitMenu;
-
- end; {EdProcessCommand}
-
- procedure EdClassifyInput;
- {-Route keyboard input to text or command handlers}
- var
- Ch : Char;
- CmdCode : CommandType;
-
- begin {EdClassifyInput}
-
- {Read a character}
- Ch := EdGetInput;
-
- if (CmdPtr = 0) and (Ch >= #32) and (Ch <> #127) then begin
-
- {A normal character}
- {Store previous position}
- with LastPosition, Curwin^ do begin
- Col := Colno;
- Line := Curline;
- end;
-
- {Process the character}
- EdProcessText(Ch, True);
-
- if EditUsercommandInput = 0 then
- {Buffer line if it changed due to word wrap}
- if Curwin^.Curline <> LastPosition.Line then
- EdBufferCurrentLine;
-
- end else begin
-
- {Potentially a command}
- Inc(CmdPtr);
- if (CmdPtr > 1) and (CmdBuf[Pred(CmdPtr)] <> Null) then
- {Accept lower/upper/control chars as equivalent for second char of command}
- CmdBuf[CmdPtr] := EdControlFilter(Ch)
- else
- CmdBuf[CmdPtr] := Ch;
-
- case EdScanCmdList(CmdPtr, CmdCode) of
-
- Match :
- {Process the command}
- EdProcessCommand(CmdCode);
-
- NoMatch :
- {Reset and ignore}
- EdResetPromptLine;
-
- PartMatch :
- {Leave char in CmdBuf to complete match}
- {Indicate via command line that a partial command has been entered}
- EdDisplayCommandBuffer;
-
- end; {Case}
- end; {Potential command}
-
- end; {EdClassifyInput}
-
- procedure EdProcessMacroChars {(CurrentUserChars : integer)} ;
- {-Processes characters pushed in a Find and Macro command}
-
- begin {EdProcessMacroChars}
- {Process the macro characters}
- while EditUsercommandInput > CurrentUserChars do
- EdClassifyInput;
- end; {EdProcessMacroChars}
-
- procedure EdSchedule;
- {-Schedule editor}
-
- procedure EdBackgroundProcess;
- {-Process background functions}
-
- begin {EdBackgroundProcess}
-
- {Align windows horizontally}
- EdHscroll;
-
- {Update the cursor}
- if UpdateCursor then
- EdUpdateCursor;
-
- {Update the screen if something's new}
- if UpdateScreen then begin
- {Redraw the screen}
- EdUpdateScreen;
- {Assure the cursor is always off when solidcursor is used}
- EdSetCursorOff;
- end;
- if EdKeyInterrupt then
- Exit;
-
- {Mark any new lines in block}
- EdMarkblock;
- if EdKeyInterrupt then
- Exit;
-
- {Update the screen again if attributes changed}
- if UpdateScreen then begin
- EdUpdateScreen;
- if EdKeyInterrupt then
- Exit;
- end;
-
- {Show on-screen font attributes}
- EdSetAttributes;
- if EdKeyInterrupt then
- Exit;
-
- {Update the screen again if attributes changed}
- if UpdateScreen then begin
- EdUpdateScreen;
- if EdKeyInterrupt then
- Exit;
- end;
-
- {Print files}
- if Printing then begin
- EdPrintNext(PrintChars);
- if EdKeyInterrupt then
- Exit;
- end;
-
- {Keep window modified flags up to date}
- EdCloneModifiedFlags;
- if EdKeyInterrupt then
- Exit;
-
- {Generate page numbers for every window}
- EdRepaginate;
- if EdKeyInterrupt then
- Exit;
-
- {Generate line number and byte count for every window}
- EdGenLineNo;
-
- {Print until interrupted}
- if Printing then
- EdPrintNext(MaxInt);
-
- end; {EdBackgroundProcess}
-
- begin {EdSchedule}
- Rundown := False;
- repeat
- if EdKeyPressed then
- {Foreground operations - classify keystrokes and process commands}
- EdClassifyInput
- else
- {Horizontal scroll, paint screen, etc.}
- EdBackgroundProcess;
- until Rundown;
- end; {EdSchedule}
-
- begin
- Argcount := ParamCount; {Number of DOS command line parameters}
- end.