fptemplt.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. {$ifdef FVISION}
  29. FVConsts,
  30. {$else}
  31. Commands,
  32. {$endif}
  33. MsgBox,
  34. WUtils,
  35. WEditor,
  36. FPConst,FPVars,FPString,FPUtils;
  37. type
  38. PTemplate = ^TTemplate;
  39. TTemplate = record
  40. Name : PString;
  41. Path : PString;
  42. end;
  43. PTemplateCollection = ^TTemplateCollection;
  44. TTemplateCollection = object(TSortedCollection)
  45. function At(Index: Integer): PTemplate;
  46. procedure FreeItem(Item: Pointer); virtual;
  47. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  48. end;
  49. const Templates : PTemplateCollection = nil;
  50. function NewTemplate(const Name, Path: string): PTemplate;
  51. var P: PTemplate;
  52. begin
  53. New(P);
  54. FillChar(P^,SizeOf(P^),0);
  55. P^.Name:=NewStr(Name);
  56. P^.Path:=NewStr(Path);
  57. NewTemplate:=P;
  58. end;
  59. procedure DisposeTemplate(P: PTemplate);
  60. begin
  61. if assigned(P) then
  62. begin
  63. if assigned(P^.Name) then
  64. DisposeStr(P^.Name);
  65. if assigned(P^.Path) then
  66. DisposeStr(P^.Path);
  67. Dispose(P);
  68. end;
  69. end;
  70. function TTemplateCollection.At(Index: Integer): PTemplate;
  71. begin
  72. At:=inherited At(Index);
  73. end;
  74. procedure TTemplateCollection.FreeItem(Item: Pointer);
  75. begin
  76. if assigned(Item) then
  77. DisposeTemplate(Item);
  78. end;
  79. function TTemplateCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  80. var R: Sw_integer;
  81. K1: PTemplate absolute Key1;
  82. K2: PTemplate absolute Key2;
  83. begin
  84. if K1^.Name^<K2^.Name^ then R:=-1 else
  85. if K1^.Name^>K2^.Name^ then R:= 1 else
  86. R:=0;
  87. Compare:=R;
  88. end;
  89. function GetTemplateCount: integer;
  90. var Count: integer;
  91. begin
  92. if Templates=nil then Count:=0 else Count:=Templates^.Count;
  93. GetTemplateCount:=Count;
  94. end;
  95. function GetTemplateName(Index: integer): string;
  96. begin
  97. GetTemplateName:=Templates^.At(Index)^.Name^;
  98. end;
  99. function SearchStr(const InS, SubS: string; var P: sw_integer): boolean;
  100. begin
  101. P:=Pos(SubS,InS);
  102. SearchStr:=(P<>0);
  103. end;
  104. procedure ReplaceStr(var S: string; StartP,Len: sw_integer; const NewS: string);
  105. begin
  106. Delete(S,StartP,Len);
  107. Insert(NewS,S,StartP);
  108. end;
  109. function ReadStringPos(const InS: string; StartP: sw_integer; var Expr: string; var EndPos: sw_integer): sw_integer;
  110. const Enclosers : string[2] = '''"';
  111. var OK: boolean;
  112. Encloser: char;
  113. P: sw_integer;
  114. begin
  115. OK:=false; Expr:=''; P:=StartP; EndPos:=-1;
  116. if length(InS)>=P then
  117. begin
  118. P:=Pos(InS[P],Enclosers);
  119. OK:=(P<>0);
  120. if OK then
  121. begin
  122. OK:=false;
  123. Encloser:=Enclosers[P];
  124. P:=StartP;
  125. Inc(P);
  126. while (P<=length(InS)) do
  127. begin
  128. if InS[P]<>Encloser then
  129. Expr:=Expr+InS[P]
  130. else
  131. if (P+1<=length(InS)) and (InS[P+1]=Encloser) then
  132. Expr:=Expr+InS[P]
  133. else
  134. begin
  135. OK:=true;
  136. Break;
  137. end;
  138. Inc(P);
  139. end;
  140. EndPos:=P;
  141. end;
  142. end;
  143. if OK then
  144. ReadStringPos:=length(Expr)
  145. else
  146. ReadStringPos:=-1;
  147. end;
  148. {function ReadString(const InS: string; StartP: sw_integer; var Expr: string): sw_integer;
  149. var P: sw_integer;
  150. begin
  151. ReadString:=ReadStringPos(InS,StartP,Expr,P);
  152. end;}
  153. function ProcessTemplateLine(var S: string): boolean;
  154. var OK: boolean;
  155. P,EndP: sw_integer;
  156. Name,Expr: string;
  157. begin
  158. OK:=true;
  159. repeat
  160. P:=0; Expr:='';
  161. if OK and SearchStr(S,tsPrompt,P) then
  162. if ReadStringPos(S,P+length(tsPrompt),Name,EndP)>=0 then
  163. if copy(S,EndP+1,1)=')' then
  164. begin
  165. OK:=InputBox(dialog_fillintemplateparameter,Name,Expr,255)=cmOK;
  166. if OK then
  167. ReplaceStr(S,P,EndP-P+1+1,Expr);
  168. end;
  169. if OK and SearchStr(S,tsDateCustom,P) then
  170. if ReadStringPos(S,P+length(tsDateCustom),Expr,EndP)>=0 then
  171. if copy(S,EndP+1,1)=')' then
  172. ReplaceStr(S,P,EndP-P+1+1,FormatDateTimeL(Now,Expr));
  173. if OK and SearchStr(S,tsDate,P) then
  174. ReplaceStr(S,P,length(tsDate),FormatDateTimeL(Now,'yyyy/mm/dd'));
  175. if OK and SearchStr(S,tsTime,P) then
  176. ReplaceStr(S,P,length(tsTime),FormatDateTimeL(Now,'hh:nn:ss'));
  177. until P=0;
  178. ProcessTemplateLine:=OK;
  179. end;
  180. function ProcessTemplate(Editor: PSourceEditor): boolean;
  181. var OK: boolean;
  182. I: sw_integer;
  183. S,OrigS: string;
  184. begin
  185. OK:=true;
  186. with Editor^ do
  187. for I:=0 to GetLineCount-1 do
  188. begin
  189. S:=GetDisplayText(I); OrigS:=S;
  190. OK:=ProcessTemplateLine(S);
  191. if OK=false then Break;
  192. if S<>OrigS then
  193. begin
  194. SetDisplayText(I,S);
  195. UpdateAttrs(I,attrAll);
  196. end;
  197. end;
  198. ProcessTemplate:=OK;
  199. end;
  200. function StartTemplate(Index: integer; Editor: PSourceEditor): boolean;
  201. var
  202. T: PTemplate;
  203. OK: boolean;
  204. begin
  205. T:=Templates^.At(Index);
  206. OK:=StartEditor(Editor,T^.Path^);
  207. if OK then
  208. begin
  209. ProcessTemplate(Editor);
  210. end;
  211. StartTemplate:=OK;
  212. end;
  213. {*****************************************************************************
  214. InitTemplates
  215. *****************************************************************************}
  216. procedure InitTemplates;
  217. procedure ScanDir(Dir: PathStr);
  218. var SR: SearchRec;
  219. S: string;
  220. PT : PTemplate;
  221. i : sw_integer;
  222. begin
  223. if copy(Dir,length(Dir),1)<>DirSep then Dir:=Dir+DirSep;
  224. FindFirst(Dir+'*'+TemplateExt,AnyFile,SR);
  225. while (DosError=0) do
  226. begin
  227. S:=NameOf(SR.Name);
  228. S:=LowerCaseStr(S);
  229. S[1]:=Upcase(S[1]);
  230. PT:=NewTemplate(S,FExpand(Dir+SR.Name));
  231. if not Templates^.Search(PT,i) then
  232. Templates^.Insert(PT)
  233. else
  234. DisposeTemplate(PT);
  235. FindNext(SR);
  236. end;
  237. {$ifdef FPC}
  238. FindClose(SR);
  239. {$endif def FPC}
  240. end;
  241. begin
  242. New(Templates, Init(10,10));
  243. ScanDir('.');
  244. ScanDir(IDEDir);
  245. end;
  246. procedure DoneTemplates;
  247. begin
  248. if assigned(Templates) then
  249. begin
  250. Dispose(Templates, Done);
  251. Templates:=nil;
  252. end;
  253. end;
  254. END.
  255. {
  256. $Log$
  257. Revision 1.3 2002-09-07 15:40:45 peter
  258. * old logs removed and tabs fixed
  259. }