summaryrefslogtreecommitdiff
path: root/examples/apps/ide/src/idemacros.pas
blob: deee65075b145f1cca48e3cf230282d6b2048fb4 (plain)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
unit idemacros;

{$mode objfpc}{$H+}

interface

uses
  Classes
  ,SysUtils
  ,fpg_base
  ,fpg_main
  ;

type
  TIDEMacro = class(TObject)
  public
    Name: string;
    Value: string;
    Description: string;
    constructor Create(AName, AValue, ADescription: TfpgString);
  end;


  TIDEMacroList = class(TObject)
  private
    FItems: TList;
    function    GetItems(AIndex: integer): TIDEMacro;
    procedure   SetItems(AIndex: integer; const AValue: TIDEMacro);
    procedure   AddDefaults;
    procedure   LoadSavedValues;
  public
    constructor Create;
    destructor  Destroy; override;
    function    Count: integer;
    function    FindByName(const MacroName: TfpgString): TIDEMacro;
    function    StrHasMacros(const s: TfpgString): boolean;
    function    ExpandMacro(const s: TfpgString): TfpgString;
    procedure   Add(NewMacro: TIDEMacro);
    procedure   Clear;
    procedure   Delete(AIndex: integer);
    procedure   SetValue(const MacroName, NewValue: TfpgString);
    procedure   ResetToDefaults;
    property    Items[AIndex: integer]: TIDEMacro read GetItems write SetItems; default;
  end;


// lazy-man singleton of IDE Macros
function GMacroList: TIDEMacroList;


implementation

uses
  ideconst
  ,fpg_iniutils;

var
  uIDEMacroList: TIDEMacroList;


function GMacroList: TIDEMacroList;
begin
  if not Assigned(uIDEMacroList) then
    uIDEMacroList := TIDEMacroList.Create;
  Result := uIDEMacroList;
end;


{ TIDEMacro }

constructor TIDEMacro.Create(AName, AValue, ADescription: TfpgString);
begin
  Name := AName;
  Value := AValue;
  Description := ADescription;
end;


{ TIDEMacroList }

function TIDEMacroList.GetItems(AIndex: integer): TIDEMacro;
begin
  Result := TIDEMacro(FItems[AIndex]);
end;

procedure TIDEMacroList.SetItems(AIndex: integer; const AValue: TIDEMacro);
begin
  FItems[AIndex] := AValue;
end;

procedure TIDEMacroList.AddDefaults;
var
  o: TIDEMacro;
begin
  o := TIDEMacro.Create(cMacro_FPCSrcDir, '', 'FPC source directory');
  Add(o);
  o := TIDEMacro.Create(cMacro_FPGuiDir, '', 'fpGUI root directory');
  Add(o);
  o := TIDEMacro.Create(cMacro_FPGuiLibDir, cMacro_FPGuiDir+'lib/'+cMacro_Target+'/', 'fpGUI compiled library directory');
  Add(o);
  o := TIDEMacro.Create(cMacro_SyntaxDefDir, cMacro_FPGuiDir+'examples/apps/fpgide/syntaxdefs/', 'Editor syntax highlighter definitions');
  Add(o);
  o := TIDEMacro.Create(cMacro_TemplateDir, cMacro_FPGuiDir+'examples/apps/fpgide/templates/', 'Project template directory');
  Add(o);
  o := TIDEMacro.Create(cMacro_Compiler, '', 'FPC Compiler to use');
  Add(o);
  o := TIDEMacro.Create(cMacro_Debugger, 'gdb', 'Location of GDB debugger');
  Add(o);
  o := TIDEMacro.Create(cMacro_ExeExt, {$IFDEF MSWINDOWS} '.exe' {$ENDIF} {$IFDEF UNIX} '' {$ENDIF}, 'Default executable extension');
  Add(o);
  o := TIDEMacro.Create(cMacro_Target, CPUTarget+'-'+OSTarget, 'Default target');
  Add(o);
end;

procedure TIDEMacroList.LoadSavedValues;
var
  s: TfpgString;
begin
  // we don't unnecessarily override the defaults setup in AddDefaults()
  SetValue(cMacro_FPCSrcDir, gINI.ReadString(cEnvironment, 'FPCSrcDir', ''));
  SetValue(cMacro_FPGuiDir, gINI.ReadString(cEnvironment, 'FPGuiDir', ''));
  s := gINI.ReadString(cEnvironment, 'FPGuiLibDir', '');
  if s <> '' then
    SetValue(cMacro_FPGuiLibDir, s);
  s := gINI.ReadString(cEnvironment, 'SyntaxDefDir', '');
  if s <> '' then
    SetValue(cMacro_SyntaxDefDir, s);
  s := gINI.ReadString(cEnvironment, 'TemplateDir', '');
  if s <> '' then
    SetValue(cMacro_TemplateDir, s);
  SetValue(cMacro_Compiler, gINI.ReadString(cEnvironment, 'Compiler', ''));
  s := gINI.ReadString(cEnvironment, 'Debugger', '');
  if s <> '' then
    SetValue(cMacro_Debugger, s);
  s := gINI.ReadString(cEnvironment, 'ExeExt', '');
  if s <> '' then
    SetValue(cMacro_ExeExt, s);
  s := gINI.ReadString(cEnvironment, 'Target', '');
  if s <> '' then
    SetValue(cMacro_Target, s);
end;

constructor TIDEMacroList.Create;
begin
  inherited Create;
  FItems := TList.Create;
  AddDefaults;
  LoadSavedValues;
end;

destructor TIDEMacroList.Destroy;
begin
  Clear;
  FItems.Free;
  inherited Destroy;
end;

function TIDEMacroList.Count: integer;
begin
  Result := FItems.Count;
end;

function TIDEMacroList.FindByName(const MacroName: TfpgString): TIDEMacro;
var
  l: Integer;
  r: Integer;
  m: Integer;
  cmp: Integer;
begin
  l := 0;
  r := FItems.Count-1;
  m := 0;
  while l <= r do
  begin
    m := (l+r) shr 1;
    Result := Items[m];
    cmp := AnsiCompareText(MacroName,Result.Name);
    if cmp < 0 then
      r := m-1
    else if cmp > 0 then
      l := m+1
    else
      exit;
  end;
  Result := nil;
end;

function TIDEMacroList.StrHasMacros(const s: TfpgString): boolean;
// search for ${
var
  p: Integer;
  Len: Integer;
begin
  Result := false;
  p := 1;
  Len := length(s);
  while (p < Len) do
  begin
    if s[p] = '$' then
    begin
      inc(p);
      if (p<Len) and (s[p]<>'$') then
      begin
        // skip macro function name
        while (p<Len) and (s[p]<>'{') do inc(p);
        if (p<Len) then
        begin
          Result:=true;
          exit;
        end;
      end
      else
      begin
        // $$ is not a macro
        inc(p);
      end;
    end else
      inc(p);
  end;
end;

function TIDEMacroList.ExpandMacro(const s: TfpgString): TfpgString;
var
  sub: TfpgString;
  pstart: integer;
  pend: integer;
  len: integer;
  m: TIDEMacro;
  r: TfpgString;
begin
  r := s;
  pstart := Pos('${', r);
  while (pstart > 0) do
  begin
    len := Length(r);
    pend := pstart + 2;
    while pend < len do
    begin
      if r[pend] = '}' then
        break
      else
        inc(pend);
    end;
    sub := Copy(r, pstart, (pend-pstart)+1);
    m := FindByName(sub);
    if not Assigned(m) then
      raise Exception.CreateFmt('The macro <%s> is not defined.', [sub]);
    r := StringReplace(r, sub, m.Value, [rfReplaceAll, rfIgnoreCase]);
    pstart := Pos('${', r);
  end;
  Result := r;
end;

procedure TIDEMacroList.Add(NewMacro: TIDEMacro);
var
  l: Integer;
  r: Integer;
  m: Integer;
  cmp: Integer;
begin
  l := 0;
  r := FItems.Count-1;
  m := 0;
  while l <= r do
  begin
    m := (l+r) shr 1;
    cmp := AnsiCompareText(NewMacro.Name, Items[m].Name);
    if cmp < 0 then
      r := m-1
    else if cmp > 0 then
      l := m + 1
    else
      break;
  end;
  if (m < FItems.Count) and (AnsiCompareText(NewMacro.Name, Items[m].Name) > 0) then
    inc(m);
  FItems.Insert(m, NewMacro);
end;

procedure TIDEMacroList.Clear;
var
  i: integer;
begin
  for i := 0 to FItems.Count-1 do Items[i].Free;
  FItems.Clear;
end;

procedure TIDEMacroList.Delete(AIndex: integer);
begin
  Items[AIndex].Free;
  FItems.Delete(AIndex);
end;

procedure TIDEMacroList.SetValue(const MacroName, NewValue: TfpgString);
var
  lMacro: TIDEMacro;
begin
  lMacro := FindByName(MacroName);
  if lMacro <> nil then
    lMacro.Value := NewValue;
end;

procedure TIDEMacroList.ResetToDefaults;
begin
  Clear;
  AddDefaults;
  LoadSavedValues;
end;


initialization
  uIDEMacroList := nil;

finalization
  uIDEMacroList.Free;

end.