fptemplt.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 1998 by Berczi Gabor
  5. Template support routines for the IDE
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit FPTemplt;
  13. interface
  14. uses FPViews;
  15. const
  16. tsDate = '$DATE';
  17. tsDateCustom = '$DATE(';
  18. tsTime = '$TIME';
  19. tsPrompt = '$PROMPT(';
  20. function GetTemplateCount: integer;
  21. function GetTemplateName(Index: integer): string;
  22. function StartTemplate(Index: integer; Editor: PSourceEditor): boolean;
  23. procedure InitTemplates;
  24. procedure DoneTemplates;
  25. implementation
  26. uses
  27. Dos,Objects,
  28. FVConsts,
  29. MsgBox,
  30. WUtils,
  31. WEditor,
  32. FPConst,FPVars,FPString,FPUtils;
  33. type
  34. PTemplate = ^TTemplate;
  35. TTemplate = record
  36. Name : PString;
  37. Path : PString;
  38. end;
  39. PTemplateCollection = ^TTemplateCollection;
  40. TTemplateCollection = object(TSortedCollection)
  41. function At(Index: Integer): PTemplate;
  42. procedure FreeItem(Item: Pointer); virtual;
  43. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  44. end;
  45. const Templates : PTemplateCollection = nil;
  46. function NewTemplate(const Name, Path: string): PTemplate;
  47. var P: PTemplate;
  48. begin
  49. New(P);
  50. FillChar(P^,SizeOf(P^),0);
  51. P^.Name:=NewStr(Name);
  52. P^.Path:=NewStr(Path);
  53. NewTemplate:=P;
  54. end;
  55. procedure DisposeTemplate(P: PTemplate);
  56. begin
  57. if assigned(P) then
  58. begin
  59. if assigned(P^.Name) then
  60. DisposeStr(P^.Name);
  61. if assigned(P^.Path) then
  62. DisposeStr(P^.Path);
  63. Dispose(P);
  64. end;
  65. end;
  66. function TTemplateCollection.At(Index: Integer): PTemplate;
  67. begin
  68. At:=inherited At(Index);
  69. end;
  70. procedure TTemplateCollection.FreeItem(Item: Pointer);
  71. begin
  72. if assigned(Item) then
  73. DisposeTemplate(Item);
  74. end;
  75. function TTemplateCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  76. var R: Sw_integer;
  77. K1: PTemplate absolute Key1;
  78. K2: PTemplate absolute Key2;
  79. begin
  80. if K1^.Name^<K2^.Name^ then R:=-1 else
  81. if K1^.Name^>K2^.Name^ then R:= 1 else
  82. R:=0;
  83. Compare:=R;
  84. end;
  85. function GetTemplateCount: integer;
  86. var Count: integer;
  87. begin
  88. if Templates=nil then Count:=0 else Count:=Templates^.Count;
  89. GetTemplateCount:=Count;
  90. end;
  91. function GetTemplateName(Index: integer): string;
  92. begin
  93. GetTemplateName:=Templates^.At(Index)^.Name^;
  94. end;
  95. function SearchStr(const InS, SubS: string; var P: sw_integer): boolean;
  96. begin
  97. P:=Pos(SubS,InS);
  98. SearchStr:=(P<>0);
  99. end;
  100. procedure ReplaceStr(var S: string; StartP,Len: sw_integer; const NewS: string);
  101. begin
  102. Delete(S,StartP,Len);
  103. Insert(NewS,S,StartP);
  104. end;
  105. function ReadStringPos(const InS: string; StartP: sw_integer; var Expr: string; var EndPos: sw_integer): sw_integer;
  106. const Enclosers : string[2] = '''"';
  107. var OK: boolean;
  108. Encloser: char;
  109. P: sw_integer;
  110. begin
  111. OK:=false; Expr:=''; P:=StartP; EndPos:=-1;
  112. if length(InS)>=P then
  113. begin
  114. P:=Pos(InS[P],Enclosers);
  115. OK:=(P<>0);
  116. if OK then
  117. begin
  118. OK:=false;
  119. Encloser:=Enclosers[P];
  120. P:=StartP;
  121. Inc(P);
  122. while (P<=length(InS)) do
  123. begin
  124. if InS[P]<>Encloser then
  125. Expr:=Expr+InS[P]
  126. else
  127. if (P+1<=length(InS)) and (InS[P+1]=Encloser) then
  128. Expr:=Expr+InS[P]
  129. else
  130. begin
  131. OK:=true;
  132. Break;
  133. end;
  134. Inc(P);
  135. end;
  136. EndPos:=P;
  137. end;
  138. end;
  139. if OK then
  140. ReadStringPos:=length(Expr)
  141. else
  142. ReadStringPos:=-1;
  143. end;
  144. {function ReadString(const InS: string; StartP: sw_integer; var Expr: string): sw_integer;
  145. var P: sw_integer;
  146. begin
  147. ReadString:=ReadStringPos(InS,StartP,Expr,P);
  148. end;}
  149. function ProcessTemplateLine(var S: string): boolean;
  150. var OK: boolean;
  151. P,EndP: sw_integer;
  152. Name,Expr: string;
  153. begin
  154. OK:=true;
  155. repeat
  156. P:=0; Expr:='';
  157. if OK and SearchStr(S,tsPrompt,P) then
  158. if ReadStringPos(S,P+length(tsPrompt),Name,EndP)>=0 then
  159. if copy(S,EndP+1,1)=')' then
  160. begin
  161. OK:=InputBox(dialog_fillintemplateparameter,Name,Expr,255)=cmOK;
  162. if OK then
  163. ReplaceStr(S,P,EndP-P+1+1,Expr);
  164. end;
  165. if OK and SearchStr(S,tsDateCustom,P) then
  166. if ReadStringPos(S,P+length(tsDateCustom),Expr,EndP)>=0 then
  167. if copy(S,EndP+1,1)=')' then
  168. ReplaceStr(S,P,EndP-P+1+1,FormatDateTimeL(Now,Expr));
  169. if OK and SearchStr(S,tsDate,P) then
  170. ReplaceStr(S,P,length(tsDate),FormatDateTimeL(Now,'yyyy/mm/dd'));
  171. if OK and SearchStr(S,tsTime,P) then
  172. ReplaceStr(S,P,length(tsTime),FormatDateTimeL(Now,'hh:nn:ss'));
  173. until P=0;
  174. ProcessTemplateLine:=OK;
  175. end;
  176. function ProcessTemplate(Editor: PSourceEditor): boolean;
  177. var OK: boolean;
  178. I: sw_integer;
  179. S,OrigS: string;
  180. begin
  181. OK:=true;
  182. with Editor^ do
  183. for I:=0 to GetLineCount-1 do
  184. begin
  185. S:=GetDisplayText(I); OrigS:=S;
  186. OK:=ProcessTemplateLine(S);
  187. if OK=false then Break;
  188. if S<>OrigS then
  189. begin
  190. SetDisplayText(I,S);
  191. UpdateAttrs(I,attrAll);
  192. DrawView;
  193. end;
  194. end;
  195. ProcessTemplate:=OK;
  196. end;
  197. function StartTemplate(Index: integer; Editor: PSourceEditor): boolean;
  198. var
  199. T: PTemplate;
  200. OK: boolean;
  201. begin
  202. T:=Templates^.At(Index);
  203. OK:=StartEditor(Editor,T^.Path^);
  204. if OK then
  205. begin
  206. ProcessTemplate(Editor);
  207. end;
  208. StartTemplate:=OK;
  209. end;
  210. {*****************************************************************************
  211. InitTemplates
  212. *****************************************************************************}
  213. procedure InitTemplates;
  214. procedure ScanDir(Dir: PathStr);
  215. var SR: SearchRec;
  216. S: string;
  217. PT : PTemplate;
  218. i : sw_integer;
  219. begin
  220. if copy(Dir,length(Dir),1)<>DirSep then Dir:=Dir+DirSep;
  221. FindFirst(Dir+'*'+TemplateExt,AnyFile,SR);
  222. while (DosError=0) do
  223. begin
  224. S:=NameOf(SR.Name);
  225. S:=LowerCaseStr(S);
  226. S[1]:=Upcase(S[1]);
  227. PT:=NewTemplate(S,FExpand(Dir+SR.Name));
  228. if not Templates^.Search(PT,i) then
  229. Templates^.Insert(PT)
  230. else
  231. DisposeTemplate(PT);
  232. FindNext(SR);
  233. end;
  234. {$ifdef FPC}
  235. FindClose(SR);
  236. {$endif def FPC}
  237. end;
  238. begin
  239. New(Templates, Init(10,10));
  240. ScanDir('.');
  241. ScanDir(IDEDir);
  242. end;
  243. procedure DoneTemplates;
  244. begin
  245. if assigned(Templates) then
  246. begin
  247. Dispose(Templates, Done);
  248. Templates:=nil;
  249. end;
  250. end;
  251. END.
  252. {
  253. $Log$
  254. Revision 1.5 2004-11-08 20:28:26 peter
  255. * Breakpoints are now deleted when removed from source, disabling is
  256. still possible from the breakpoint list
  257. * COMPILER_1_0, FVISION, GABOR defines removed, only support new
  258. FV and 1.9.x compilers
  259. * Run directory added to Run menu
  260. * Useless programinfo window removed
  261. Revision 1.4 2002/09/26 20:45:08 pierre
  262. * fix webbug 2102-C
  263. Revision 1.3 2002/09/07 15:40:45 peter
  264. * old logs removed and tabs fixed
  265. }