home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
tptools.zip
/
FIRSTED.ZIP
/
EDMAIN.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1987-12-21
|
18KB
|
572 lines
{ EDMAIN.PAS
ED 4.0
Copyright (c) 1985, 87 by Borland International, Inc. }
{$I eddirect.inc}
unit EdMain;
interface
uses
crt, {Basic video operations - standard unit}
Dos, {DOS calls - standard unit}
Errors, {Runtime error handler}
EdVars, {Global types and declarations}
EdScrn1, {Fast screen writing routines}
EdString, {String primitives}
EdPtrOp, {Primitive pointer operations}
EdCmds, {Maps keystrokes to commands}
int24, {DOS critical error handler}
Message, {Message system}
EdUser, {User keyboard input, line editing and error reporting}
EdMemOp, {Text buffer allocation and deallocation}
EdBack, {Background processes}
EdScrn2, {Editor screen updating}
EdEdit, {Basic editing commands}
EdText, {Text processing commands}
EdBlok, {Block move, copy, delete}
EdFinds, {Find and replace routines}
EdFile; {File I/O routines}
procedure EdInitialize;
{-Initialize editor variables and data structures}
procedure EdMainMenu;
{-Get command line parameters or prompt for initial file}
procedure EdSchedule;
{-Schedule editor}
{==========================================================================}
implementation
var
Argcount : Byte; {Number of DOS command line arguments}
procedure EdInitialize;
{-Initialize editor variables and data structures}
begin {EdInitialize}
EdZapPromptLine; {Empty message line}
{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 window}
Window1 := CurWin;
Window1^.FwdLink := Window1;
Window1^.Backlink := Window1;
{Last cursor position}
LastPosition.Line := CurWin^.TopLine;
LastPosition.Col := 1;
end; {EdInitialize}
procedure EdMainMenu;
{-Get command line parameters or prompt for initial file}
var
CmdString, Arg : String255;
Fcount : Integer;
AddWindStr : CommandString;
begin {EdMainMenu}
AddWindStr := EdCommandKeys(CmdAddWindow, Primary);
if Argcount > 0 then begin
{Take DOS command line for file names, up to 3 windows}
EdClearString(CmdString);
Fcount := 1;
if not(EdStringEmpty(AddWindStr)) then
while (Argcount > 0) and (Fcount <= 3) do begin
Arg := ParamStr(Fcount);
CmdString := CmdString+AddWindStr+Arg+^M;
Inc(Fcount);
if Pos('*', Arg)+Pos('?', Arg) <> 0 then begin
{Bring up noname file if wildcards detected}
CmdString := AddWindStr+^M;
Argcount := 1;
end;
Dec(Argcount);
end;
Argcount := 0;
end else
{Start up with noname file}
CmdString := AddWindStr+^M;
{Push commands onto keyboard buffer}
EdUserPush(CmdString);
end; {EdMainMenu}
procedure EdWriteNamedFile;
{-Get file name, save current text stream to it, change stream names}
var
Fname, Junk : Filepath;
begin {EdWriteNamedFile}
{Get a file name}
EdClearString(Junk);
Fname := EdGetFileName(EdGetMessage(386), DefExtension, 0, Junk);
if Abortcmd or EdStringEmpty(Fname) then
Exit;
if EdExistFile(Fname) then begin
{Prompt to overwrite}
if not(EdYesNo(EdGetMessage(319))) then
Exit;
if Abortcmd then
Exit;
end;
EdFileWrite(Fname, False);
if GotError then
Exit;
{Change the names of all linked streams}
EdChangeStreamName(Fname);
end; {EdWriteNamedFile}
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
EdWriteNamedFile
else
EdFileWrite(Filename, False);
end;
EdJumpMarker(C);
end; {EdSaveFile}
procedure EdSaveQuit;
{-Save file and leave editor if all windows are closed}
begin {EdSaveQuit}
with CurWin^ do begin
if Filename = NoFile then begin
EdWriteNamedFile;
if Abortcmd or (Filename = NoFile) then
Exit;
end else
EdFileWrite(Filename, (Window1^.FwdLink = Window1));
if GotError then
Exit;
EdShutWindow(True);
end;
end; {EdSaveQuit}
{***}
procedure EdFindNext;
{-Process find next command}
begin {EdFindNext}
PromptForInput := False;
case LastSearchOp of
Find : EdFind;
Replace : EdFindReplace;
end;
PromptForInput := True;
end; {EdFindNext}
{***}
procedure EdAddWindow;
{-Add a text window, get a file for it, read it in, and move to it}
var
WindToDivide : Integer;
begin {EdAddWindow}
{Add a window only if allowed}
if (WindowCount >= MaxWindows) then begin
EdErrormsg(127);
Exit;
end;
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}
Exit;
{Move to new window}
EdWindowGoto(Succ(WindToDivide));
end;
{Read the new file into memory}
EdReadFile(EdGetFileName(EdGetMessage(320), DefExtension, 0, LastFileEdit));
if GotError then begin
if WindowCount > 0 then begin
{Didn't get file, remove window just created}
EdWindowGoto(WindToDivide);
EdWindowDelete(Succ(WindToDivide));
end else
{Quit the editor}
EdFlagExit;
end else
Inc(WindowCount);
end; {EdAddwindow}
procedure EdPromptLogDrive;
{-Prompt for and move to new directory}
var
Junk : Filepath;
begin {EdPromptLogDrive}
{Current directory is default}
GetDir(0, Junk);
EdLogDrive(EdGetFileName(EdGetMessage(317), '', 16, Junk));
end; {EdPromptLogDrive}
{***}
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}
CmdCpgotoln : EdPromptGotoLine; {Goto line n}
CmdGotoColumn : EdPromptGotoCol; {Goto column n}
CmdJumpLastPosition : EdJumpMarker(LastPosition); {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}
end;
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; {Find and replace}
CmdFindNext : EdFindNext; {Find next}
CmdAbandonFile : EdAbandonFile(True); {Abandon file and leave editor}
CmdReadBlock : {Read file into window}
EdReadtextfile(EdGetFileName(EdGetMessage(321), DefExtension, 0, LastBlockRead), True);
CmdSaveFile : EdSaveFile; {Save file}
CmdWriteBlock : EdPromptWriteBlock;
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}
CmdWindowDown : EdWindowDown; {Switch windows}
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 EdTextCommands(C : CommandType);
{-Dispatcher for commands which perform word processing functions}
begin {EdTextCommands}
case C of
CmdSysInfo : EdSysInfo; {Show editor version}
CmdShowMem : EdShowMemory; {Show available memory}
CmdToggleInsert : EdToggleBoolean(CurWin^.InsertFlag); {Toggle insert mode}
CmdToggleAutoindent : EdToggleBoolean(CurWin^.AI); {Toggle autoindent mode}
end;
end; {EdTextCommands}
{***}
procedure EdUtilityCommands(C : CommandType);
{-Dispatcher for commands which perform utility functions}
begin {EdUtilityCommands}
case C of
CmdLogDrive : EdPromptLogDrive; {Prompt for and move to new directory}
CmdSetUndoLimit : EdSetUndoLimit; {Set default undo limit}
CmdGetDefaultExtension : EdGetDefaultExtension; {Get a new default extension}
CmdWriteNamedFile : EdWriteNamedFile;
CmdWindowUp : EdWindowUp;
end;
end; {EdUtilityCommands}
begin {EdProcessCommand}
{Clear the command line indicator}
EdResetPromptLine;
{Store the soon to be a previous position}
with CurWin^ do begin
LastCol := ColNo;
LastLine := CurLine;
end;
{Branch based on basic command groups}
case C of
CmdLeftChar..CmdUpPage : EdCursorCommands(C);
CmdWindowTopFile..CmdJumpLastPosition : EdQuickCommands(C);
CmdUndo..CmdDeleteLine : EdDeleteCommands(C);
CmdFind..CmdSaveQuit : EdFileCommands(C);
CmdAddWindow..CmdWindowDown : EdWindowCommands(C);
CmdBlockBegin..CmdJumpMarker9 : EdBlockCommands(C);
CmdSysInfo..CmdToggleAutoindent : EdTextCommands(C);
CmdLogDrive..CmdWindowUp : EdUtilityCommands(C);
end;
{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;
end;
{Buffer line if it changed}
if Blockop or (CurLine <> LastLine) then begin
EdBufferCurrentLine;
Blockop := 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
EdBiosScroll;
{The screen may need to be updated}
UpdateScreen := True;
end;
end;
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);
if EditUsercommandInput = 0 then
{Buffer line if it changed}
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 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;
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;
{Keep window modified flags up to date}
EdCloneModifiedFlags;
if EdKeyInterrupt then
Exit;
{Generate line number and byte count for every window}
EdGenLineNo;
end; {EdBackgroundProcess}
begin {EdSchedule}
Rundown := False;
repeat
if EdKeyPressed then
{Foreground operations - classify keystroke and act accordingly}
EdClassifyInput
else
{Horizontal scroll, paint screen, etc.}
EdBackgroundProcess;
until Rundown;
end; {EdSchedule}
begin
Argcount := ParamCount; {Number of DOS command line parameters}
end.