home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / Runimage / Delphi50 / Demos / ToolsAPI / Editor Keybinding / basebindings.pas next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  12.1 KB  |  345 lines

  1. unit BaseBindings;
  2.  
  3. interface
  4.  
  5. uses SysUtils, Classes, ToolsAPI;
  6.  
  7. const
  8.   kfImplicits = kfImplicitShift or kfImplicitModifier or kfImplicitKeypad;
  9.  
  10. type
  11.   TBaseBindings = class(TNotifierObject, IUnknown, IOTANotifier)
  12.   protected
  13.     { Utility functions }
  14.     function MarkWord(const Context: IOTAKeyContext): IOTAEditBlock;
  15.     procedure SlideBlock(const Context: IOTAKeyContext; Backward: Boolean);
  16.     { Default binding implementations }
  17.     procedure AddWatchAtCursor(const Context: IOTAKeyContext; KeyCode: TShortcut;
  18.       var BindingResult: TKeyBindingResult);
  19.     procedure AutoCodeInsight(const Context: IOTAKeyContext; KeyCode: TShortcut;
  20.       var BindingResult: TKeyBindingResult);
  21.     procedure BrowseSymbolAtCursor(const Context: IOTAKeyContext; KeyCode: TShortcut;
  22.       var BindingResult: TKeyBindingResult);
  23.     procedure BlockSave(const Context: IOTAKeyContext; KeyCode: TShortcut;
  24.       var BindingResult: TKeyBindingResult);
  25.     procedure ClassNavigate(const Context: IOTAKeyContext; KeyCode: TShortcut;
  26.       var BindingResult: TKeyBindingResult);
  27.     procedure ClassComplete(const Context: IOTAKeyContext; KeyCode: TShortcut;
  28.       var BindingResult: TKeyBindingResult);
  29.     procedure ClipClear(const Context: IOTAKeyContext; KeyCode: TShortcut;
  30.       var BindingResult: TKeyBindingResult);
  31.     procedure ClipCopy(const Context: IOTAKeyContext; KeyCode: TShortcut;
  32.       var BindingResult: TKeyBindingResult);
  33.     procedure ClipCut(const Context: IOTAKeyContext; KeyCode: TShortcut;
  34.       var BindingResult: TKeyBindingResult);
  35.     procedure ClipPaste(const Context: IOTAKeyContext; KeyCode: TShortcut;
  36.       var BindingResult: TKeyBindingResult);
  37.     procedure CodeTemplate(const Context: IOTAKeyContext; KeyCode: TShortcut;
  38.       var BindingResult: TKeyBindingResult);
  39.     procedure CodeCompletion(const Context: IOTAKeyContext; KeyCode: TShortcut;
  40.       var BindingResult: TKeyBindingResult);
  41.     procedure DebugInspect(const Context: IOTAKeyContext; KeyCode: TShortcut;
  42.       var BindingResult: TKeyBindingResult);
  43.     procedure GotoLine(const Context: IOTAKeyContext; KeyCode: TShortcut;
  44.       var BindingResult: TKeyBindingResult);
  45.     procedure HelpKeyword(const Context: IOTAKeyContext; KeyCode: TShortcut;
  46.       var BindingResult: TKeyBindingResult);
  47.     procedure IncrementalSearch(const Context: IOTAKeyContext; KeyCode: TShortcut;
  48.       var BindingResult: TKeyBindingResult);
  49.     procedure InsertCompilerOptions(const Context: IOTAKeyContext; KeyCode: TShortcut;
  50.       var BindingResult: TKeyBindingResult);
  51.     procedure InsertNewGUID(const Context: IOTAKeyContext; KeyCode: TShortcut;
  52.       var BindingResult: TKeyBindingResult);
  53.     procedure NullCmd(const Context: IOTAKeyContext; KeyCode: TShortcut;
  54.       var BindingResult: TKeyBindingResult);
  55.     procedure OpenFileAtCursor(const Context: IOTAKeyContext; KeyCode: TShortcut;
  56.       var BindingResult: TKeyBindingResult);
  57.     procedure OpenLine(const Context: IOTAKeyContext; KeyCode: TShortcut;
  58.       var BindingResult: TKeyBindingResult);
  59.     procedure Print(const Context: IOTAKeyContext; KeyCode: TShortcut;
  60.       var BindingResult: TKeyBindingResult);
  61.     procedure SetBlockStyle(const Context: IOTAKeyContext; KeyCode: TShortcut;
  62.       var BindingResult: TKeyBindingResult);
  63.     procedure SearchAgain(const Context: IOTAKeyContext; KeyCode: TShortcut;
  64.       var BindingResult: TKeyBindingResult);
  65.     procedure SearchFind(const Context: IOTAKeyContext; KeyCode: TShortcut;
  66.       var BindingResult: TKeyBindingResult);
  67.     procedure SearchReplace(const Context: IOTAKeyContext; KeyCode: TShortcut;
  68.       var BindingResult: TKeyBindingResult);
  69.     procedure SwapCPPHeader(const Context: IOTAKeyContext; KeyCode: TShortcut;
  70.       var BindingResult: TKeyBindingResult);
  71.     procedure ViewExplorer(const Context: IOTAKeyContext; KeyCode: TShortcut;
  72.       var BindingResult: TKeyBindingResult);
  73.   end;
  74.  
  75. resourcestring
  76.   sNoBlockMarked = 'No block marked';  
  77.  
  78. implementation
  79.  
  80. { TBaseBindings }
  81.  
  82. procedure TBaseBindings.AddWatchAtCursor(const Context: IOTAKeyContext;
  83.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  84. begin
  85.   (Context.EditBuffer.TopView as IOTAEditActions).AddWatchAtCursor;
  86.   BindingResult := krHandled;
  87. end;
  88.  
  89. procedure TBaseBindings.AutoCodeInsight(const Context: IOTAKeyContext;
  90.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  91. var
  92.   EP: IOTAEditPosition;
  93.   EA: IOTAEditActions;
  94.   AChar: Char;
  95. begin
  96.   EP := Context.EditBuffer.EditPosition;
  97.   EA := Context.EditBuffer.TopView as IOTAEditActions;
  98.   AChar := Char(Byte(Context.Context));
  99.   EP.InsertCharacter(AChar);
  100.   case AChar of
  101.     '.', '>': EA.CodeCompletion(csCodeList);
  102.     '(': EA.CodeCompletion(csParamList);
  103.   end;
  104.   BindingResult := krHandled;  
  105. end;
  106.  
  107. procedure TBaseBindings.BlockSave(const Context: IOTAKeyContext;
  108.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  109. begin
  110.   Context.EditBuffer.EditBlock.SaveToFile('');
  111.   BindingResult := krHandled;
  112. end;
  113.  
  114. procedure TBaseBindings.BrowseSymbolAtCursor(const Context: IOTAKeyContext;
  115.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  116. begin
  117.   (Context.EditBuffer.TopView as IOTAEditActions).BrowseSymbolAtCursor;
  118.   BindingResult := krHandled;
  119. end;
  120.  
  121. procedure TBaseBindings.ClassComplete(const Context: IOTAKeyContext;
  122.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  123. begin
  124.   (Context.EditBuffer.TopView as IOTAEditActions).ClassComplete;
  125.   BindingResult := krHandled;
  126. end;
  127.  
  128. procedure TBaseBindings.ClassNavigate(const Context: IOTAKeyContext;
  129.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  130. begin
  131.   (Context.EditBuffer.TopView as IOTAEditActions).ClassNavigate(0);
  132.   BindingResult := krHandled;
  133. end;
  134.  
  135. procedure TBaseBindings.ClipClear(const Context: IOTAKeyContext;
  136.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  137. begin
  138.   Context.EditBuffer.EditBlock.Delete;
  139.   BindingResult := krHandled;
  140. end;
  141.  
  142. procedure TBaseBindings.ClipCopy(const Context: IOTAKeyContext;
  143.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  144. begin
  145.   Context.EditBuffer.EditBlock.Copy(False);
  146.   BindingResult := krHandled;
  147. end;
  148.  
  149. procedure TBaseBindings.ClipCut(const Context: IOTAKeyContext;
  150.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  151. begin
  152.   Context.EditBuffer.EditBlock.Cut(False);
  153.   BindingResult := krHandled;
  154. end;
  155.  
  156. procedure TBaseBindings.ClipPaste(const Context: IOTAKeyContext;
  157.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  158. begin
  159.   Context.EditBuffer.EditPosition.Paste;
  160.   BindingResult := krHandled;
  161. end;
  162.  
  163. procedure TBaseBindings.CodeCompletion(const Context: IOTAKeyContext;
  164.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  165. begin
  166.   (Context.EditBuffer.TopView as IOTAEditActions).CodeCompletion(Byte(Context.Context));
  167.   BindingResult := krHandled;
  168. end;
  169.  
  170. procedure TBaseBindings.CodeTemplate(const Context: IOTAKeyContext;
  171.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  172. begin
  173.   (Context.EditBuffer.TopView as IOTAEditActions).CodeTemplate;
  174.   BindingResult := krHandled;
  175. end;
  176.  
  177. procedure TBaseBindings.DebugInspect(const Context: IOTAKeyContext;
  178.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  179. begin
  180.   (Context.EditBuffer.TopView as IOTAEditActions).InspectAtCursor;
  181.   BindingResult := krHandled;
  182. end;
  183.  
  184. procedure TBaseBindings.GotoLine(const Context: IOTAKeyContext;
  185.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  186. begin
  187.   Context.EditBuffer.EditPosition.GotoLine(0);
  188.   BindingResult := krHandled;
  189. end;
  190.  
  191. procedure TBaseBindings.HelpKeyword(const Context: IOTAKeyContext;
  192.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  193. begin
  194.   (Context.EditBuffer.TopView as IOTAEditActions).HelpKeyword;
  195.   BindingResult := krHandled;
  196. end;
  197.  
  198. procedure TBaseBindings.IncrementalSearch(const Context: IOTAKeyContext;
  199.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  200. begin
  201.   (Context.EditBuffer.TopView as IOTAEditActions).IncrementalSearch;
  202.   BindingResult := krHandled;
  203. end;
  204.  
  205. procedure TBaseBindings.InsertCompilerOptions(const Context: IOTAKeyContext; KeyCode: TShortcut;
  206.   var BindingResult: TKeyBindingResult);
  207. begin
  208.   (Context.EditBuffer.TopView as IOTAEditActions).InsertCompilerOptions;
  209.   BindingResult := krHandled;
  210. end;
  211.  
  212. procedure TBaseBindings.InsertNewGUID(const Context: IOTAKeyContext;
  213.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  214. begin
  215.   (Context.EditBuffer.TopView as IOTAEditActions).InsertNewGUID;
  216.   BindingResult := krHandled;
  217. end;
  218.  
  219. function TBaseBindings.MarkWord(const Context: IOTAKeyContext): IOTAEditBlock;
  220. var
  221.   EP: IOTAEditPosition;
  222.   EB: IOTAEditBlock;
  223. begin
  224.   EP := Context.EditBuffer.EditPosition;
  225.   EB := Context.EditBuffer.EditBlock;
  226.   if EP.IsWordCharacter then
  227.     EP.MoveCursor(mmSkipLeft or mmSkipWord);
  228.   if EP.IsWhiteSpace then
  229.     EP.MoveCursor(mmSkipRight or mmSkipWhite);
  230.   if not EP.IsWhiteSpace then
  231.   begin
  232.     if not EP.IsWordCharacter then
  233.       EP.MoveCursor(mmSkipRight or mmSkipNonWord);
  234.     if EP.IsWordCharacter then
  235.     begin
  236.       EB.Reset;
  237.       EB.Style := btNonInclusive;
  238.       EB.BeginBlock;
  239.       EP.MoveCursor(mmSkipRight or mmSkipWord);
  240.       EB.EndBlock;
  241.     end;
  242.   end;
  243.   Result := EB;
  244. end;
  245.  
  246. procedure TBaseBindings.NullCmd(const Context: IOTAKeyContext;
  247.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  248. begin
  249.   BindingResult := krHandled;
  250. end;
  251.  
  252. procedure TBaseBindings.OpenFileAtCursor(const Context: IOTAKeyContext;
  253.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  254. begin
  255.   (Context.EditBuffer.TopView as IOTAEditActions).OpenFileAtCursor;
  256.   BindingResult := krHandled;
  257. end;
  258.  
  259. procedure TBaseBindings.OpenLine(const Context: IOTAKeyContext;
  260.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  261. var
  262.   EP: IOTAEditPosition;
  263. begin
  264.   EP := Context.EditBuffer.EditPosition;
  265.   EP.Save;
  266.   try
  267.     EP.InsertCharacter(#10);
  268.   finally
  269.     EP.Restore;
  270.   end;
  271.   BindingResult := krHandled;
  272. end;
  273.  
  274. procedure TBaseBindings.Print(const Context: IOTAKeyContext;
  275.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  276. begin
  277.   Context.EditBuffer.Print;
  278.   BindingResult := krHandled;
  279. end;
  280.  
  281. procedure TBaseBindings.SearchAgain(const Context: IOTAKeyContext;
  282.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  283. begin
  284.   Context.EditBuffer.EditPosition.RepeatLastSearchOrReplace;
  285.   BindingResult := krHandled;
  286. end;
  287.  
  288. procedure TBaseBindings.SearchFind(const Context: IOTAKeyContext;
  289.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  290. begin
  291.   Context.EditBuffer.EditPosition.Search;
  292.   BindingResult := krHandled;
  293. end;
  294.  
  295. procedure TBaseBindings.SearchReplace(const Context: IOTAKeyContext;
  296.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  297. begin
  298.   Context.EditBuffer.EditPosition.Replace;
  299.   BindingResult := krHandled;
  300. end;
  301.  
  302. procedure TBaseBindings.SetBlockStyle(const Context: IOTAKeyContext;
  303.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  304. var
  305.   Style: TOTABlockType;
  306. begin
  307.   Style := TOTABlockType(Context.Context);
  308.   Assert((Style >= btInclusive) and (Style < btUnknown), 'Invalid block style'); // do not localize
  309.   Context.EditBuffer.EditBlock.Style := Style;
  310.   BindingResult := krHandled;
  311. end;
  312.  
  313. procedure TBaseBindings.SlideBlock(const Context: IOTAKeyContext;
  314.   Backward: Boolean);
  315. var
  316.   EB: IOTAEditBlock;
  317.   IndentAmount: Integer;
  318. begin
  319.   EB := Context.EditBuffer.EditBlock;
  320.   if EB.Size <> 0 then
  321.   begin
  322.     IndentAmount := Context.KeyboardServices.EditorServices.EditOptions.BlockIndent;
  323.     if Backward then
  324.       IndentAmount := IndentAmount * -1;
  325.     EB.Indent(IndentAmount);
  326.   end else
  327.     Context.EditBuffer.TopView.SetTempMsg(sNoBlockMarked);
  328. end;
  329.  
  330. procedure TBaseBindings.SwapCPPHeader(const Context: IOTAKeyContext;
  331.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  332. begin
  333.   (Context.EditBuffer.TopView as IOTAEditActions).SwapCPPHeader;
  334.   BindingResult := krHandled;
  335. end;
  336.  
  337. procedure TBaseBindings.ViewExplorer(const Context: IOTAKeyContext;
  338.   KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
  339. begin
  340.   (Context.EditBuffer.TopView as IOTAEditActions).ViewExplorer;
  341.   BindingResult := krHandled;
  342. end;
  343.  
  344. end.
  345.