File: fptemplt.pas

package info (click to toggle)
fpc 3.0.0%2Bdfsg-11%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 273,420 kB
  • ctags: 106,632
  • sloc: pascal: 2,840,574; xml: 152,225; ansic: 9,635; asm: 8,297; java: 5,346; sh: 3,991; yacc: 3,745; php: 3,281; makefile: 2,635; lex: 2,538; sql: 267; cpp: 145; perl: 134; sed: 132; csh: 34; tcl: 7
file content (296 lines) | stat: -rw-r--r-- 6,964 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
{
    This file is part of the Free Pascal Integrated Development Environment
    Copyright (c) 1998 by Berczi Gabor

    Template support routines for the IDE

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit FPTemplt;

interface

uses FPViews;

const
      tsDate         = '$DATE';
      tsDateCustom   = '$DATE(';
      tsTime         = '$TIME';
      tsPrompt       = '$PROMPT(';

{$ifdef useresstrings}
resourcestring
{$else}
const
{$endif}
      dialog_fillintemplateparameter = 'Fill in template parameter';

function  GetTemplateCount: integer;
function  GetTemplateName(Index: integer): string;
function  StartTemplate(Index: integer; Editor: PSourceEditor): boolean;

procedure InitTemplates;
procedure DoneTemplates;

implementation

uses
  Dos,Objects,
  FVConsts,
  MsgBox,
  WUtils,
  WEditor,
  FPConst,FPVars,FPUtils;

type
    PTemplate = ^TTemplate;
    TTemplate = record
      Name : PString;
      Path : PString;
    end;

    PTemplateCollection = ^TTemplateCollection;
    TTemplateCollection = object(TSortedCollection)
      function  At(Index: Integer): PTemplate;
      procedure FreeItem(Item: Pointer); virtual;
      function  Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
    end;

const Templates : PTemplateCollection = nil;

function NewTemplate(const Name, Path: string): PTemplate;
var P: PTemplate;
begin
  New(P);
  FillChar(P^,SizeOf(P^),0);
  P^.Name:=NewStr(Name);
  P^.Path:=NewStr(Path);
  NewTemplate:=P;
end;

procedure DisposeTemplate(P: PTemplate);
begin
  if assigned(P) then
   begin
     if assigned(P^.Name) then
       DisposeStr(P^.Name);
     if assigned(P^.Path) then
       DisposeStr(P^.Path);
     Dispose(P);
   end;
end;

function TTemplateCollection.At(Index: Integer): PTemplate;
begin
  At:=inherited At(Index);
end;

procedure TTemplateCollection.FreeItem(Item: Pointer);
begin
  if assigned(Item) then
    DisposeTemplate(Item);
end;

function TTemplateCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
var R: Sw_integer;
    K1: PTemplate absolute Key1;
    K2: PTemplate absolute Key2;
begin
  if K1^.Name^<K2^.Name^ then R:=-1 else
  if K1^.Name^>K2^.Name^ then R:= 1 else
  R:=0;
  Compare:=R;
end;

function GetTemplateCount: integer;
var Count: integer;
begin
  if Templates=nil then Count:=0 else Count:=Templates^.Count;
  GetTemplateCount:=Count;
end;

function GetTemplateName(Index: integer): string;
begin
  GetTemplateName:=Templates^.At(Index)^.Name^;
end;

function SearchStr(const InS, SubS: string; var P: sw_integer): boolean;
begin
  P:=Pos(SubS,InS);
  SearchStr:=(P<>0);
end;

procedure ReplaceStr(var S: string; StartP,Len: sw_integer; const NewS: string);
begin
  Delete(S,StartP,Len);
  Insert(NewS,S,StartP);
end;

function ReadStringPos(const InS: string; StartP: sw_integer; var Expr: string; var EndPos: sw_integer): sw_integer;
const Enclosers : string[2] = '''"';
var OK: boolean;
    Encloser: char;
    P: sw_integer;
begin
  OK:=false; Expr:=''; P:=StartP; EndPos:=-1;
  if length(InS)>=P then
  begin
    P:=Pos(InS[P],Enclosers);
    OK:=(P<>0);
    if OK then
    begin
      OK:=false;
      Encloser:=Enclosers[P];
      P:=StartP;
      Inc(P);
      while (P<=length(InS)) do
      begin
        if InS[P]<>Encloser then
          Expr:=Expr+InS[P]
        else
          if (P+1<=length(InS)) and (InS[P+1]=Encloser) then
            Expr:=Expr+InS[P]
          else
            begin
              OK:=true;
              Break;
            end;
        Inc(P);
      end;
      EndPos:=P;
    end;
  end;
  if OK then
    ReadStringPos:=length(Expr)
  else
    ReadStringPos:=-1;
end;

{function ReadString(const InS: string; StartP: sw_integer; var Expr: string): sw_integer;
var P: sw_integer;
begin
  ReadString:=ReadStringPos(InS,StartP,Expr,P);
end;}

function ProcessTemplateLine(var S: string): boolean;
var OK: boolean;
    P,EndP: sw_integer;
    Name,Expr: string;
begin
  OK:=true;
  repeat
    P:=0; Expr:='';
    if OK and SearchStr(S,tsPrompt,P) then
      if ReadStringPos(S,P+length(tsPrompt),Name,EndP)>=0 then
        if copy(S,EndP+1,1)=')' then
         begin
           OK:=InputBox(dialog_fillintemplateparameter,Name,Expr,255)=cmOK;
           if OK then
             ReplaceStr(S,P,EndP-P+1+1,Expr);
         end;
    if OK and SearchStr(S,tsDateCustom,P) then
      if ReadStringPos(S,P+length(tsDateCustom),Expr,EndP)>=0 then
        if copy(S,EndP+1,1)=')' then
           ReplaceStr(S,P,EndP-P+1+1,FormatDateTimeL(Now,Expr));
    if OK and SearchStr(S,tsDate,P) then
      ReplaceStr(S,P,length(tsDate),FormatDateTimeL(Now,'yyyy/mm/dd'));
    if OK and SearchStr(S,tsTime,P) then
      ReplaceStr(S,P,length(tsTime),FormatDateTimeL(Now,'hh:nn:ss'));
  until P=0;
  ProcessTemplateLine:=OK;
end;

function ProcessTemplate(Editor: PSourceEditor): boolean;
var OK: boolean;
    I: sw_integer;
    S,OrigS: string;
begin
  OK:=true;
  with Editor^ do
  for I:=0 to GetLineCount-1 do
  begin
    S:=GetDisplayText(I); OrigS:=S;
    OK:=ProcessTemplateLine(S);
    if OK=false then Break;
    if S<>OrigS then
    begin
      SetDisplayText(I,S);
      UpdateAttrs(I,attrAll);
      DrawView;
     end;
  end;
  ProcessTemplate:=OK;
end;

function StartTemplate(Index: integer; Editor: PSourceEditor): boolean;
var
    T: PTemplate;
    OK: boolean;
begin
  T:=Templates^.At(Index);
  OK:=StartEditor(Editor,T^.Path^);
  if OK then
  begin
    ProcessTemplate(Editor);
  end;
  StartTemplate:=OK;
end;


{*****************************************************************************
                                 InitTemplates
*****************************************************************************}

procedure InitTemplates;

  procedure ScanDir(Dir: PathStr);
  var SR: SearchRec;
      S: string;
      PT : PTemplate;
      i : sw_integer;
  begin
    {$ifdef HASAMIGA}
    if (copy(Dir,length(Dir),1)<>DirSep) and (copy(Dir,length(Dir),1)<>DriveSeparator) then Dir:=Dir+DirSep;
    {$else}
    if copy(Dir,length(Dir),1)<>DirSep then Dir:=Dir+DirSep;
    {$endif}
    FindFirst(Dir+'*'+TemplateExt,AnyFile,SR);
    while (DosError=0) do
    begin
      S:=NameOf(SR.Name);
      S:=LowerCaseStr(S);
      S[1]:=Upcase(S[1]);
      PT:=NewTemplate(S,FExpand(Dir+SR.Name));
      if not Templates^.Search(PT,i) then
        Templates^.Insert(PT)
      else
        DisposeTemplate(PT);
      FindNext(SR);
    end;
    FindClose(SR);
  end;

begin
  New(Templates, Init(10,10));
  ScanDir('.');
  ScanDir(IDEDir);
  ScanDir(SystemIDEDir);
end;


procedure DoneTemplates;
begin
  if assigned(Templates) then
    begin
      Dispose(Templates, Done);
      Templates:=nil;
    end;
end;

END.