home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / tptools.zip / FIRSTED.ZIP / EDCMDS.PAS < prev    next >
Pascal/Delphi Source File  |  1987-12-21  |  5KB  |  160 lines

  1. {                          EDCMDS.PAS
  2.                              ED 4.0
  3.              Copyright (c) 1985, 87 by Borland International, Inc.            }
  4.  
  5. {$I eddirect.inc}
  6.  
  7. unit EdCmds;
  8.   {-Map keystrokes to commands}
  9.  
  10. interface
  11.  
  12. uses
  13.   Dos,                       {DOS calls - standard unit}
  14.   Errors;                    {Runtime error handler}
  15.  
  16.   {Defines CommandType - enumerated type of all editor commands}
  17.   {$I edcmds.inc}
  18.  
  19. type
  20.   CommandString = string[4]; {Longest key sequence for a command}
  21.   CmdMatchType = (Match, NoMatch, PartMatch); {Used in matching key sequences to editor commands}
  22.   CommandPriority = (Primary, Secondary); {Two sequences allowed for each command}
  23.  
  24. var
  25.   {Keyboard-related}
  26.   CmdBuf : CommandString;    {Holds current command until complete command reached}
  27.   CmdPtr : Byte;             {Points to last character in current command}
  28.   AbortChar : Char;          {Character used to abort operation}
  29.  
  30. function EdScanCmdList(CmdPtr : Byte; var CmdCode : CommandType) : CmdMatchType;
  31.   {-See if current command buffer matches any installed commands}
  32.   {-Return Match if so, PartMatch if possible match, NoMatch if a loser}
  33.   {-Return CmdCode if matched}
  34.  
  35. function EdCommandKeys(C : CommandType; P : CommandPriority) : CommandString;
  36.   {-Return the primary or secondary key sequence for command c}
  37.  
  38.   {==========================================================================}
  39.  
  40. implementation
  41.  
  42. const
  43.   CmdListBytes = 512;        {Maximum Number of bytes in installable command list}
  44. type
  45.   CmdListBuffer = array[0..CmdListBytes] of Char;
  46. var
  47.   CmdList : ^CmdListBuffer;  {Points to legal list of command keystrokes}
  48.  
  49.   {The machine image contains an ID string used by the installation program}
  50.   {It also contains the default command to keystroke mapping}
  51.   {$L EDCMDS}
  52.  
  53.   function EdInitCmdListPtr : Pointer; external;
  54.   {-Return a pointer to the beginning of command list}
  55.  
  56.   function EdScanCmdList(CmdPtr : Byte; var CmdCode : CommandType) : CmdMatchType;
  57.     {-See if current command buffer matches any installed commands}
  58.     {-Return Match if so, PartMatch if possible match, NoMatch if a loser}
  59.     {-Return cmdcode if matched}
  60.   var
  61.     Cpos, Cofs, CmdLen : Integer;
  62.     Done : Boolean;
  63.     Result : CmdMatchType;
  64.  
  65.   begin                      {EdScancmdlist}
  66.  
  67.     {Initialize}
  68.     CmdCode := CmdLeftChar;  {First command in CommandType}
  69.     Cpos := 0;
  70.     CmdLen := Ord(CmdList^[0]);
  71.  
  72.     repeat
  73.       {Offset within this command}
  74.       Cofs := 1;
  75.       Result := PartMatch;
  76.  
  77.       while (Result = PartMatch) and (Cofs <= CmdPtr) and (Cofs <= CmdLen) do
  78.         if CmdBuf[Cofs] <> CmdList^[Cpos+Cofs] then
  79.           Result := NoMatch
  80.         else
  81.           Inc(Cofs);
  82.  
  83.       Done := (Result = PartMatch);
  84.  
  85.       if not(Done) then begin
  86.         {Move to next command}
  87.         Cpos := Cpos+CmdLen+2;
  88.         {Bytes in next command}
  89.         CmdLen := Ord(CmdList^[Cpos]);
  90.       end else if (CmdPtr = CmdLen) then begin
  91.         {The whole command matched}
  92.         Result := Match;
  93.         CmdCode := CommandType(CmdList^[Cpos+Cofs]);
  94.       end;
  95.  
  96.     until Done or (CmdLen = 0);
  97.  
  98.     EdScanCmdList := Result;
  99.   end;                       {EdScancmdlist}
  100.  
  101.   function EdCommandKeys(C : CommandType; P : CommandPriority) : CommandString;
  102.     {-Return the primary or secondary key sequence for command c}
  103.   var
  104.     Cofs : Integer;
  105.     Cmd : CommandString;
  106.  
  107.     function EdNextCommandKeys(C : CommandType; var Cofs : Integer) : CommandString;
  108.       {-Return the next command sequence matching c, starting at cofs}
  109.     var
  110.       Clen : Integer;
  111.       Cmd : CommandString;
  112.  
  113.     begin                    {EdNextCommandKeys}
  114.       Cmd := '';
  115.       repeat
  116.         Clen := Ord(CmdList^[Cofs]);
  117.         if Clen <> 0 then
  118.           if Ord(C) = Ord(CmdList^[Succ(Cofs+Clen)]) then begin
  119.             Move(CmdList^[Cofs], Cmd, Succ(Clen));
  120.             Cofs := Cofs+Clen+2;
  121.             {Force exit}
  122.             Clen := 0;
  123.           end else
  124.             Cofs := Cofs+Clen+2;
  125.       until (Clen = 0);
  126.       EdNextCommandKeys := Cmd;
  127.     end;                     {EdNextCommandKeys}
  128.  
  129.   begin                      {EdCommandKeys}
  130.     {Get the primary key sequence}
  131.     Cofs := 0;
  132.     Cmd := EdNextCommandKeys(C, Cofs);
  133.     if (P = Secondary) and (Cmd <> '') then
  134.       {Get the secondary command}
  135.       Cmd := EdNextCommandKeys(C, Cofs);
  136.     EdCommandKeys := Cmd;
  137.   end;                       {EdCommandKeys}
  138.  
  139.   procedure SetAbortChar;
  140.     {-Set character used to abort operation}
  141.   var
  142.     AbortString : CommandString;
  143.  
  144.   begin                      {SetAbortChar}
  145.     AbortString := EdCommandKeys(CmdAbort, Primary);
  146.     if AbortString = '' then
  147.       AbortChar := ^U
  148.     else
  149.       AbortChar := AbortString[1];
  150.   end;                       {SetAbortChar}
  151.  
  152. begin
  153.   {Initialize pointer to the command list stored in this code segment}
  154.   CmdList := EdInitCmdListPtr;
  155.   {Number of chars currently in command buffer}
  156.   CmdPtr := 0;
  157.   {Initialize AbortChar}
  158.   SetAbortChar;
  159. end.
  160.