fpcodtmp.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. unit FPCodTmp; { Code Templates }
  2. interface
  3. uses Objects,
  4. WUtils;
  5. type
  6. PCodeTemplate = ^TCodeTemplate;
  7. TCodeTemplate = object(TObject)
  8. constructor Init(const AShortCut: string; AText: PUnsortedStringCollection);
  9. function GetShortCut: string;
  10. procedure GetText(AList: PUnsortedStringCollection);
  11. destructor Done; virtual;
  12. private
  13. ShortCut: PString;
  14. Text: PUnsortedStringCollection;
  15. end;
  16. PCodeTemplateCollection = ^TCodeTemplateCollection;
  17. TCodeTemplateCollection = object(TSortedCollection)
  18. function Compare(Key1, Key2: Pointer): sw_Integer; virtual;
  19. function SearchByShortCut(const ShortCut: string): PCodeTemplate; virtual;
  20. end;
  21. const CodeTemplates : PCodeTemplateCollection = nil;
  22. function FPTranslateCodeTemplate(const Shortcut: string; ALines: PUnsortedStringCollection): boolean;
  23. procedure InitCodeTemplates;
  24. procedure DoneCodeTemplates;
  25. implementation
  26. constructor TCodeTemplate.Init(const AShortCut: string; AText: PUnsortedStringCollection);
  27. procedure CopyIt(P: PString); {$ifndef FPC}far;{$endif}
  28. begin
  29. Text^.Insert(NewStr(GetStr(P)));
  30. end;
  31. begin
  32. inherited Init;
  33. ShortCut:=NewStr(AShortCut);
  34. New(Text, Init(10,10));
  35. if Assigned(AText) then
  36. AText^.ForEach(@CopyIt);
  37. end;
  38. function TCodeTemplate.GetShortCut: string;
  39. begin
  40. GetShortCut:=GetStr(ShortCut);
  41. end;
  42. procedure TCodeTemplate.GetText(AList: PUnsortedStringCollection);
  43. procedure CopyIt(P: PString); {$ifndef FPC}far;{$endif}
  44. begin
  45. AList^.Insert(NewStr(GetStr(P)));
  46. end;
  47. begin
  48. if Assigned(AList) then
  49. Text^.ForEach(@CopyIt);
  50. end;
  51. destructor TCodeTemplate.Done;
  52. begin
  53. if Assigned(ShortCut) then DisposeStr(ShortCut); ShortCut:=nil;
  54. if Assigned(Text) then Dispose(Text, Done); Text:=nil;
  55. inherited Done;
  56. end;
  57. function TCodeTemplateCollection.Compare(Key1, Key2: Pointer): sw_Integer;
  58. var K1: PCodeTemplate absolute Key1;
  59. K2: PCodeTemplate absolute Key2;
  60. R: Sw_integer;
  61. S1,S2: string;
  62. begin
  63. S1:=UpCaseStr(K1^.GetShortCut);
  64. S2:=UpCaseStr(K2^.GetShortCut);
  65. if S1<S2 then R:=-1 else
  66. if S1>S2 then R:=1 else
  67. R:=0;
  68. Compare:=R;
  69. end;
  70. function TCodeTemplateCollection.SearchByShortCut(const ShortCut: string): PCodeTemplate;
  71. var T: TCodeTemplate;
  72. Index: sw_integer;
  73. P: PCodeTemplate;
  74. begin
  75. T.Init(ShortCut,nil);
  76. if Search(@T,Index)=false then P:=nil else
  77. P:=At(Index);
  78. T.Done;
  79. SearchByShortCut:=P;
  80. end;
  81. function FPTranslateCodeTemplate(const Shortcut: string; ALines: PUnsortedStringCollection): boolean;
  82. var OK: boolean;
  83. P: PCodeTemplate;
  84. begin
  85. OK:=Assigned(CodeTemplates);
  86. if OK then
  87. begin
  88. P:=CodeTemplates^.SearchByShortCut(ShortCut);
  89. OK:=Assigned(P);
  90. if OK then
  91. P^.GetText(ALines);
  92. end;
  93. {$ifdef GABOR}
  94. {
  95. this is for testing purposes only. once the user front-end for defining
  96. CodeTemplates is implemented, this can be removed - Gabor
  97. }
  98. if (OK=false) and (UpCaseStr(ShortCut)='CASES') then
  99. begin
  100. OK:=true;
  101. ALines^.Insert(NewStr('case of'));
  102. ALines^.Insert(NewStr(' : ;'));
  103. ALines^.Insert(NewStr(' : ;'));
  104. ALines^.Insert(NewStr('end;'));
  105. end else
  106. if (OK=false) and (UpCaseStr(ShortCut)='CASEE') then
  107. begin
  108. OK:=true;
  109. ALines^.Insert(NewStr('case of'));
  110. ALines^.Insert(NewStr(' : ;'));
  111. ALines^.Insert(NewStr(' : ;'));
  112. ALines^.Insert(NewStr('else ;'));
  113. ALines^.Insert(NewStr('end;'));
  114. end else
  115. ;
  116. {$endif}
  117. FPTranslateCodeTemplate:=OK;
  118. end;
  119. procedure InitCodeTemplates;
  120. begin
  121. if Assigned(CodeTemplates) then Exit;
  122. New(CodeTemplates, Init(10,10));
  123. end;
  124. procedure DoneCodeTemplates;
  125. begin
  126. if Assigned(CodeTemplates) then Dispose(CodeTemplates, Done);
  127. CodeTemplates:=nil;
  128. end;
  129. END.