2
0

fptemplt.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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.2 2001-08-05 02:01:48 peter
  258. * FVISION define to compile with fvision units
  259. Revision 1.1 2001/08/04 11:30:24 peter
  260. * ide works now with both compiler versions
  261. Revision 1.1 2000/07/13 09:48:36 michael
  262. + Initial import
  263. Revision 1.10 2000/06/22 09:07:12 pierre
  264. * Gabor changes: see fixes.txt
  265. Revision 1.9 2000/05/02 08:42:28 pierre
  266. * new set of Gabor changes: see fixes.txt
  267. Revision 1.8 1999/06/25 00:33:40 pierre
  268. * avoid lost memory on duplicate Template Items
  269. Revision 1.7 1999/03/08 14:58:11 peter
  270. + prompt with dialogs for tools
  271. Revision 1.6 1999/03/01 15:42:03 peter
  272. + Added dummy entries for functions not yet implemented
  273. * MenuBar didn't update itself automatically on command-set changes
  274. * Fixed Debugging/Profiling options dialog
  275. * TCodeEditor converts spaces to tabs at save only if efUseTabChars is
  276. set
  277. * efBackSpaceUnindents works correctly
  278. + 'Messages' window implemented
  279. + Added '$CAP MSG()' and '$CAP EDIT' to available tool-macros
  280. + Added TP message-filter support (for ex. you can call GREP thru
  281. GREP2MSG and view the result in the messages window - just like in TP)
  282. * A 'var' was missing from the param-list of THelpFacility.TopicSearch,
  283. so topic search didn't work...
  284. * In FPHELP.PAS there were still context-variables defined as word instead
  285. of THelpCtx
  286. * StdStatusKeys() was missing from the statusdef for help windows
  287. + Topic-title for index-table can be specified when adding a HTML-files
  288. Revision 1.5 1999/02/18 13:44:35 peter
  289. * search fixed
  290. + backward search
  291. * help fixes
  292. * browser updates
  293. Revision 1.4 1999/02/16 17:13:56 pierre
  294. + findclose added for FPC
  295. Revision 1.3 1999/01/21 11:54:24 peter
  296. + tools menu
  297. + speedsearch in symbolbrowser
  298. * working run command
  299. Revision 1.2 1998/12/28 15:47:52 peter
  300. + Added user screen support, display & window
  301. + Implemented Editor,Mouse Options dialog
  302. + Added location of .INI and .CFG file
  303. + Option (INI) file managment implemented (see bottom of Options Menu)
  304. + Switches updated
  305. + Run program
  306. Revision 1.2 1998/12/22 10:39:51 peter
  307. + options are now written/read
  308. + find and replace routines
  309. }