home *** CD-ROM | disk | FTP | other *** search
- { MSCMDS.PAS
- MS 4.0
- Copyright (c) 1985, 87 by Borland International, Inc. }
-
- {$I msdirect.inc}
-
- unit MsCmds;
- {-Map keystrokes to commands}
-
- interface
-
- uses
- EscSeq, {Returns text string for extended scan codes}
- MsVars; {Global types and declarations}
-
- {$I MSCMDS.INC} {Defines enumerated type of all editor commands}
-
- type
- CommandString = string[4]; {Longest key sequence for a command}
- CmdMatchType = (Match, NoMatch, PartMatch); {Used in matching key sequences to editor commands}
- CommandPriority = (Primary, Secondary); {Two sequences allowed for each command}
- CmdSet = set of CommandType; {To classify groups of commands}
- LengthArray = array[0..255] of Byte; {Holds length of text representation of keystrokes}
-
- var
- {Keyboard-related}
- CmdBuf : CommandString; {Holds current command until complete command reached}
- CmdPtr : Byte; {Points to last character in current command}
- AbortChar : Char; {Character used to abort operation}
- GlobalCmd : CommandType; {Currently active command used by help system}
- MenuPrime : CommandString; {Primary keystrokes which activate menus}
- MenuSecon : CommandString; {Secondary keystrokes which activate menus}
- TogglePrime : CommandString; {Primary keystrokes which toggle macro recording}
- ToggleSecon : CommandString; {Secondary keystrokes which toggle macro recording}
- AddwinPrime : CommandString; {Keystrokes which open a file in a new window}
- CtrlCharStr : string[10]; {String displayed when control char prefix is entered}
-
- function EdScanCmdList(CmdPtr : Byte; var CmdCode : CommandType) : CmdMatchType;
- {-See if current command buffer matches any installed commands}
- {-Return Match if so, PartMatch if possible match, NoMatch if a loser}
- {-Return cmdcode if matched}
-
- function EdCommandKeys(C : CommandType; P : CommandPriority) : CommandString;
- {-Return the primary or secondary key sequence for command c}
-
- function EdTextRepresentation(Keys : string; var Kptr : Integer;
- var Special : Boolean) : string;
- {-Return a text representation of command keystrokes}
-
- procedure EdSetupKeyLength(var AsciiLength, ExtendedLength : LengthArray);
- {-Define lengths of text key representations}
-
- {==========================================================================}
-
- implementation
-
- const
- CmdListBytes = 1024; {Maximum Number of bytes in installable command list}
- type
- CmdListBuffer = array[0..CmdListBytes] of Char;
- var
- CmdList : ^CmdListBuffer; {Points to legal list of command keystrokes}
-
- {The machine code contains an ID string used by the installation program}
- {It also contains the default command to keystroke mapping}
- {$L MSCMDS}
-
- function EdInitCmdListPtr : Pointer; external;
- {-Return a pointer to the beginning of command list}
-
- function EdScanCmdList(CmdPtr : Byte; var CmdCode : CommandType) : CmdMatchType;
- {-See if current command buffer matches any installed commands}
- {-Return Match if so, PartMatch if possible match, NoMatch if a loser}
- {-Return cmdcode if matched}
- var
- Cpos, Cofs, CmdLen : Integer;
- Done : Boolean;
- Result : CmdMatchType;
-
- begin {EdScancmdlist}
-
- {Initialize}
- CmdCode := CmdLeftChar; {First command in CommandType}
- Cpos := 0;
- CmdLen := Ord(CmdList^[0]);
-
- repeat
- {Offset within this command}
- Cofs := 1;
- Result := PartMatch;
-
- while (Result = PartMatch) and (Cofs <= CmdPtr) and (Cofs <= CmdLen) do
- if CmdBuf[Cofs] <> CmdList^[Cpos+Cofs] then
- Result := NoMatch
- else
- Inc(Cofs);
-
- Done := (Result = PartMatch);
-
- if not(Done) then begin
- {Move to next command}
- Cpos := Cpos+CmdLen+2;
- {Bytes in next command}
- CmdLen := Ord(CmdList^[Cpos]);
- end else if (CmdPtr = CmdLen) then begin
- {The whole command matched}
- Result := Match;
- CmdCode := CommandType(CmdList^[Cpos+Cofs]);
- end;
-
- until Done or (CmdLen = 0);
-
- EdScanCmdList := Result;
- end; {EdScancmdlist}
-
- function EdCommandKeys(C : CommandType; P : CommandPriority) : CommandString;
- {-Return the primary or secondary key sequence for command c}
- var
- Cofs : Integer;
- Cmd : CommandString;
-
- function EdNextCommandKeys(C : CommandType; var Cofs : Integer) : CommandString;
- {-Return the next command sequence matching c, starting at cofs}
- var
- Clen : Byte;
- Cmd : CommandString;
-
- begin {EdNextCommandKeys}
- Cmd := '';
- repeat
- Clen := Byte(CmdList^[Cofs]);
- if Clen <> 0 then begin
- if Char(C) = CmdList^[Succ(Cofs+Clen)] then begin
- Move(CmdList^[Cofs], Cmd, Succ(Clen));
- Inc(Cofs, Clen+2);
- {Force exit}
- Clen := 0;
- end else
- Inc(Cofs, Clen+2);
- end;
- until (Clen = 0);
- EdNextCommandKeys := Cmd;
- end; {EdNextCommandKeys}
-
- begin {EdCommandKeys}
- {Get the primary key sequence}
- Cofs := 0;
- Cmd := EdNextCommandKeys(C, Cofs);
- if (P = Secondary) and (Cmd <> '') then
- {Get the secondary command}
- Cmd := EdNextCommandKeys(C, Cofs);
- EdCommandKeys := Cmd;
- end; {EdCommandKeys}
-
- function EdTextRepresentation(Keys : string; var Kptr : Integer;
- var Special : Boolean) : string;
- {-Return a text representation of command keystrokes}
- var
- Ch : Char;
- Dis, Num : string[20];
-
- begin {EdTextRepresentation}
- Dis := '';
- Special := True;
-
- if Kptr <= Length(Keys) then begin
- Ch := Keys[Kptr];
-
- case Ch of
-
- Null : {Null}
- if (Kptr = Length(Keys)) or not(UseExtendedSequence) then
- {A lone null, or one forced to be interpreted as such}
- Dis := Dis+'<Null>'
- else begin
- {An extended sequence}
- Inc(Kptr);
- Dis := Dis+EscapeSequence(Keys[Kptr]);
- end;
-
- #27 : {Escape}
- Dis := Dis+'<Esc>';
-
- #13 : {Enter}
- Dis := Dis+'<Enter>';
-
- #1..#31 : {Remaining control characters}
- Dis := Dis+'<Ctrl'+Chr(Ord(Ch)+64)+'>';
-
- #127 : {ASCII DEL}
- Dis := Dis+'<CtrlBks>';
-
- #128..#255 : {Extended ASCII}
- begin
- Str(Ord(Ch), Num);
- Dis := Dis+'<#'+Num+'>';
- end;
-
- else
- {Normal characters}
- Dis := Dis+Ch;
- Special := False;
- end;
-
- Inc(Kptr);
- end;
- EdTextRepresentation := Dis;
- end; {EdTextRepresentation}
-
- procedure EdSetupKeyLength(var AsciiLength, ExtendedLength : LengthArray);
- {-Define lengths of text key representations}
- var
- Ch : Char;
-
- begin {EdSetupKeyLength}
- {Default length for ascii}
- FillChar(AsciiLength, SizeOf(AsciiLength), 1);
- {Control chars}
- FillChar(AsciiLength[1], 31, 7);
- {Extended chars}
- FillChar(AsciiLength[128], 128, 6);
- {Special cases}
- AsciiLength[0] := 6;
- AsciiLength[27] := 5;
- AsciiLength[127] := 9;
- {Extended ASCII chars}
- for Ch := #0 to #255 do
- ExtendedLength[Ord(Ch)] := Length(EscapeSequence(Ch));
- end; {EdSetupKeyLength}
-
- function EdGetControlCharStr(CtrlKeys : CommandString) : VarString;
- {-Return the string to display on prompt line when inserting control chars}
- var
- Cmd : VarString;
- I : Integer;
- Ch : Char;
-
- begin {EdGetControlCharStr}
- I := 1;
- Cmd := '';
- while I <= Length(CtrlKeys) do begin
- Ch := CtrlKeys[I];
- if Ch = Null then begin
- {Don't try to interpret extended keystrokes}
- Inc(I);
- Cmd := Cmd+'+';
- end else if Ch < Blank then
- Cmd := Cmd+'^'+Chr(Ord(Ch)+64)
- else
- Cmd := Cmd+Ch;
- Inc(I);
- end;
- EdGetControlCharStr := Cmd;
- end; {EdGetControlCharStr}
-
- procedure InitKeySequences;
- {-Set character used to abort operation}
- var
- AbortString : CommandString;
-
- begin {InitKeySequences}
-
- AbortString := EdCommandKeys(CmdAbort, Primary);
- if AbortString = '' then
- AbortChar := ^U
- else
- AbortChar := AbortString[1];
-
- MenuPrime := EdCommandKeys(CmdMenu, Primary);
- MenuSecon := EdCommandKeys(CmdMenu, Secondary);
- TogglePrime := EdCommandKeys(CmdToggleMacroRecord, Primary);
- ToggleSecon := EdCommandKeys(CmdToggleMacroRecord, Secondary);
- AddwinPrime := EdCommandKeys(CmdAddWindow, Primary);
-
- {Get the string to display on prompt line when inserting control chars}
- CtrlCharStr := EdGetControlCharStr(EdCommandKeys(CmdInsertCtrlChar, Primary));
- end; {InitKeySequences}
-
- begin
- {Initialize pointer to the command list stored in this code segment}
- CmdList := EdInitCmdListPtr;
- {Number of chars currently in command buffer}
- CmdPtr := 0;
-
- {Initialize AbortChar and other required keystroke sequences}
- InitKeySequences;
- end.