This file is indexed.

/usr/lib/lazarus/0.9.30.4/ide/buildfiledlg.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
unit BuildFileDlg;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, LCLType, Forms, Controls, Graphics, ComCtrls,
  Dialogs, Buttons, ExtCtrls, StdCtrls, BasicCodeTools, FileUtil, IDEProcs,
  InputHistory, LazarusIDEStrConsts, EnvironmentOpts, TransferMacros,
  IDEContextHelpEdit, ButtonPanel;

type
  TIDEDirective = (
    idedNone,
    idedBuildCommand,  // Filename plus params to build the file
                       //   default is '$(CompPath) $(EdFile)'
    idedBuildWorkingDir,// Working directory for building. Default is the
                       //   directory of the file
    idedBuildScan,     // Flags controlling what messages should be scanned for
                       //   during building. See TIDEDirBuildScanFlag.
    idedRunCommand,    // Filename plus params to run the file
                       //   default is '$NameOnly($(EdFile))'
    idedRunWorkingDir, // Working directory for building. Default is the
                       //   directory of the file
    idedRunFlags       // Flags for run. See TIDEDirRunFlag
    );
  TIDEDirectives = set of TIDEDirective;

  TIDEDirBuildScanFlag = (
    idedbsfNone,
    idedbsfFPC, // scan for FPC messages. FPC+ means on (default) and FPC- off.
    idedbsfMake // scan for MAKE messages. MAKE- means on (default) and MAKE- off.
    );
  TIDEDirBuildScanFlags = set of TIDEDirBuildScanFlag;
  
  TIDEDirRunFlag = (
    idedrfNone,
    idedrfBuildBeforeRun // BUILD+ means on (default), BUILD- means off
    );
  TIDEDirRunFlags = set of TIDEDirRunFlag;


  { TMacroSelectionBox }
  
  TMacroSelectionBox = class(TGroupBox)
    procedure ListBoxClick(Sender: TObject);
  private
    FMacroList: TTransferMacroList;
    FOnAddMacro: TNotifyEvent;
    ListBox: TListBox;
    AddButton: TButton;
    procedure AddButtonClick(Sender: TObject);
    procedure SetMacroList(const AValue: TTransferMacroList);
    procedure FillListBox;
  public
    constructor Create(TheOwner: TComponent); override;
    function GetSelectedMacro(var MacroAsCode: string): TTransferMacro;
    property MacroList: TTransferMacroList read FMacroList write SetMacroList;
    property OnAddMacro: TNotifyEvent read FOnAddMacro write FOnAddMacro;
  end;


  { TBuildFileDialog }

  TBuildFileDialog = class(TForm)
    AlwaysCompileFirstCheckbox: TCHECKBOX;
    BuildBrowseWorkDirButton: TBUTTON;
    BuildCommandGroupbox: TGROUPBOX;
    BuildCommandMemo: TMEMO;
    BuildScanForFPCMsgCheckbox: TCHECKBOX;
    BuildScanForMakeMsgCheckbox: TCHECKBOX;
    BuildWorkDirCombobox: TCOMBOBOX;
    BuildWorkingDirGroupbox: TGROUPBOX;
    BuildPage: TTabSheet;
    ButtonPanel: TButtonPanel;
    GeneralPage: TTabSheet;
    Notebook1: TPageControl;
    OverrideBuildProjectCheckbox: TCHECKBOX;
    OverrideRunProjectCheckbox: TCHECKBOX;
    RunBrowseWorkDirButton: TBUTTON;
    RunCommandGroupbox: TGROUPBOX;
    RunCommandMemo: TMEMO;
    RunPage: TTabSheet;
    RunWorkDirCombobox: TCOMBOBOX;
    RunWorkDirGroupbox: TGROUPBOX;
    WhenFileIsActiveGroupbox: TGROUPBOX;
    BuildMacroSelectionBox: TMacroSelectionBox;
    RunMacroSelectionBox: TMacroSelectionBox;
    procedure BuildBrowseWorkDirButtonCLICK(Sender: TObject);
    procedure BuildFileDialogCREATE(Sender: TObject);
    procedure BuildFileDialogKEYDOWN(Sender: TObject; var Key: Word;
                                     Shift: TShiftState);
    procedure BuildMacroSelectionBoxAddMacro(Sender: TObject);
    procedure HelpButtonClick(Sender: TObject);
    procedure OkButtonClick(Sender: TObject);
    procedure RunMacroSelectionBoxAddMacro(Sender: TObject);
  private
    FDirectiveList: TStrings;
    FFilename: string;
    FMacroList: TTransferMacroList;
    function GetBuildFileIfActive: boolean;
    function GetRunFileIfActive: boolean;
    procedure SetBuildFileIfActive(const AValue: boolean);
    procedure SetDirectiveList(const AValue: TStrings);
    procedure SetFilename(const AValue: string);
    procedure SetMacroList(const AValue: TTransferMacroList);
    procedure SetRunFileIfActive(const AValue: boolean);
    procedure UpdateCaption;
    procedure ReadDirectiveList;
    procedure WriteDirectiveList;
  public
    property DirectiveList: TStrings read FDirectiveList write SetDirectiveList;
    property BuildFileIfActive: boolean read GetBuildFileIfActive write SetBuildFileIfActive;
    property RunFileIfActive: boolean read GetRunFileIfActive write SetRunFileIfActive;
    property Filename: string read FFilename write SetFilename;
    property MacroList: TTransferMacroList read FMacroList write SetMacroList;
  end;

const
  IDEDirDefaultBuildCommand = '$(CompPath) $(EdFile)';
  IDEDirBuildScanFlagDefValues = [idedbsfFPC,idedbsfMake];
  IDEDirDefaultRunCommand = '$MakeExe($(EdFile))';
  IDEDirRunFlagDefValues = [idedrfBuildBeforeRun];

  IDEDirectiveNames: array[TIDEDirective] of string = (
    '',
    'BuildCommand',
    'BuildWorkingDir',
    'BuildScan',
    'RunCommand',
    'RunWorkingDir',
    'RunFlags'
    );
  IDEDirBuildScanFlagNames: array[TIDEDirBuildScanFlag] of string = (
    '',
    'FPC',
    'MAKE'
    );
  IDEDirRunFlagNames: array[TIDEDirRunFlag] of string = (
    '',
    'BUILD'
    );

var
  IDEDirectiveSpecialChars: string;

function IndexOfIDEDirective(DirectiveList: TStrings;
                             const DirectiveName: string): integer;
function GetIDEStringDirective(DirectiveList: TStrings;
                             const DirectiveName, DefaultValue: string): string;
function GetIDEDirectiveFlag(const DirectiveValue, FlagName: string;
                            DefaultValue: boolean): boolean;
procedure SetIDEDirective(DirectiveList: TStrings; const DirectiveName: string;
                          const NewValue, DefaultValue: string);
function StringToIDEDirectiveValue(const s: string): string;
function IDEDirectiveValueToString(const s: string): string;
function IDEDirectiveNameToDirective(const DirectiveName: string
  ): TIDEDirective;
  
// build scan flags
function IDEDirBuildScanFlagNameToFlag(const FlagName: string
  ): TIDEDirBuildScanFlag;
function GetIDEDirBuildScanFromString(const s: string): TIDEDirBuildScanFlags;
function GetIDEDirBuildScanStrFromFlags(Flags: TIDEDirBuildScanFlags): string;

// run flags
function IDEDirRunFlagNameToFlag(const FlagName: string
  ): TIDEDirRunFlag;
function GetIDEDirRunFlagFromString(const s: string): TIDEDirRunFlags;
function GetIDEDirRunFlagStrFromFlags(Flags: TIDEDirRunFlags): string;

  

implementation

{$R *.lfm}

procedure AddFlagStr(var FlagStr: string; const FlagName: string;
  Value: boolean);
var
  s: String;
begin
  s:=FlagName;
  if FlagStr<>'' then s:=' '+s;
  if Value then
    s:=s+'+'
  else
    s:=s+'-';
  FlagStr:=FlagStr+s;
end;

function IndexOfIDEDirective(DirectiveList: TStrings;
                             const DirectiveName: string): integer;
var
  i: Integer;
  CurDirective: string;
  DirectiveNameLen: Integer;
begin
  Result:=-1;
  if (DirectiveList=nil) or (DirectiveName='') then exit;
  DirectiveNameLen:=length(DirectiveName);
  for i:=0 to DirectiveList.Count-1 do begin
    CurDirective:=DirectiveList[i];
    if length(CurDirective)>4+DirectiveNameLen then begin
      if CompareText(@CurDirective[3],DirectiveNameLen,
                     @DirectiveName[1],DirectiveNameLen,
                     false)=0
      then begin
        Result:=i;
        exit;
      end;
    end;
  end;
end;

function GetIDEStringDirective(DirectiveList: TStrings;
  const DirectiveName, DefaultValue: string): string;
var
  CurDirective: string;
  DirectiveNameLen: Integer;
  Index: Integer;
begin
  Result:=DefaultValue;
  Index:=IndexOfIDEDirective(DirectiveList,DirectiveName);
  if Index<0 then exit;
  DirectiveNameLen:=length(DirectiveName);
  CurDirective:=DirectiveList[Index];
  Result:=IDEDirectiveValueToString(copy(CurDirective,4+DirectiveNameLen,
               length(CurDirective)-4-DirectiveNameLen));
end;

function GetIDEDirectiveFlag(const DirectiveValue,
  FlagName: string; DefaultValue: boolean): boolean;
// Example: 'FPC+ Make off   BUILD  on  FPC-'

  function ReadNextWord(var ReadPos: integer;
    var WordStart, WordEnd: integer): boolean;
  begin
    Result:=false;
    // skip space
    while (ReadPos<=length(DirectiveValue))
    and (DirectiveValue[ReadPos]=' ') do
      inc(ReadPos);
    // read word
    WordStart:=ReadPos;
    while (ReadPos<=length(DirectiveValue))
    and (DirectiveValue[ReadPos]in ['a'..'z','A'..'Z']) do
      inc(ReadPos);
    WordEnd:=ReadPos;
    Result:=WordStart<WordEnd;
  end;

var
  ReadPos: Integer;
  WordStart, WordEnd,ValueStart, ValueEnd: integer;
  CurValue: Boolean;
begin
  Result:=DefaultValue;
  if (FlagName='') or (DirectiveValue='') then exit;
  ReadPos:=1;
  repeat
    if not ReadNextWord(ReadPos,WordStart,WordEnd) then exit;
    // read value
    if ReadPos>length(DirectiveValue) then begin
      // missing value
      exit;
    end;
    case DirectiveValue[ReadPos] of
    '+','-':
      begin
        CurValue:=DirectiveValue[ReadPos]='+';
        inc(ReadPos);
      end;
    ' ':
      begin
        if not ReadNextWord(ReadPos,ValueStart,ValueEnd) then exit;
        if CompareText(@DirectiveValue[ValueStart],ValueEnd-ValueStart,
                       'ON',2,false)=0
        then
          CurValue:=true
        else if CompareText(@DirectiveValue[ValueStart],ValueEnd-ValueStart,
                            'OFF',3,false)=0
        then
          CurValue:=false
        else
          // syntax error
          exit;
      end;
    else
      // syntax error
      exit;
    end;
    if CompareText(@DirectiveValue[WordStart],WordEnd-WordStart,
                   @FlagName[1],length(FlagName),false)=0
    then begin
      Result:=CurValue;
      exit;
    end;
  until false;
end;

procedure SetIDEDirective(DirectiveList: TStrings; const DirectiveName: string;
  const NewValue, DefaultValue: string);
var
  Index: Integer;
  NewEntry: String;
begin
  if (DirectiveName='') or (DirectiveList=nil) then exit;
  //DebugLn(['SetIDEDirective ',DirectiveName,' NewValue="',NewValue,'" DefaultValue="',DefaultValue,'"']);
  Index:=IndexOfIDEDirective(DirectiveList,DirectiveName);
  if NewValue=DefaultValue then begin
    // value is default -> remove entry
    while Index>=0 do begin
      DirectiveList.Delete(Index);
      Index:=IndexOfIDEDirective(DirectiveList,DirectiveName);
    end;
    exit;
  end else begin
    // value is not default
    NewEntry:='{%'+DirectiveName+' '+StringToIDEDirectiveValue(NewValue)+'}';
    if Index<0 then
      Index:=DirectiveList.Add(NewEntry)
    else
      DirectiveList[Index]:=NewEntry;
  end;
end;

function StringToIDEDirectiveValue(const s: string): string;
var
  NewLength: Integer;
  i: Integer;
  ResultPos: Integer;
  SpecialIndex: Integer;
begin
  NewLength:=length(s);
  for i:=1 to length(s) do
    if Pos(s[i],IDEDirectiveSpecialChars)>0 then
      inc(NewLength);
  if NewLength=length(s) then begin
    Result:=s;
    exit;
  end;
  SetLength(Result,NewLength);
  ResultPos:=1;
  for i:=1 to length(s) do begin
    SpecialIndex:=Pos(s[i],IDEDirectiveSpecialChars);
    if SpecialIndex>0 then begin
      Result[ResultPos]:='%';
      inc(ResultPos);
      Result[ResultPos]:=chr(ord('0')+SpecialIndex);
      inc(ResultPos);
    end else begin
      Result[ResultPos]:=s[i];
      inc(ResultPos);
    end;
  end;
  if ResultPos<>NewLength+1 then
    RaiseException('Internal error');
end;

function IDEDirectiveValueToString(const s: string): string;
var
  NewLength: Integer;
  i: Integer;
  ResultPos: Integer;
  SpecialIndex: Integer;
begin
  NewLength:=length(s);
  for i:=1 to length(s) do
    if (s[i]='%') and (i<length(s)) then
      dec(NewLength);
  if NewLength=length(s) then begin
    Result:=s;
    exit;
  end;
  SetLength(Result,NewLength);
  ResultPos:=1;
  i:=1;
  while i<=length(s) do begin
    if (s[i]='%') and (i<length(s)) then begin
      inc(i);
      SpecialIndex:=ord(s[i])-ord('0');
      inc(i);
      if (SpecialIndex<1) or (SpecialIndex>length(IDEDirectiveSpecialChars))
      then
        Result[ResultPos]:='?'
      else
        Result[ResultPos]:=IDEDirectiveSpecialChars[SpecialIndex];
      inc(ResultPos);
    end else begin
      Result[ResultPos]:=s[i];
      inc(ResultPos);
      inc(i);
    end;
  end;
  if ResultPos<>NewLength+1 then
    RaiseException('Internal error');
end;

function IDEDirectiveNameToDirective(const DirectiveName: string
  ): TIDEDirective;
begin
  for Result:=Low(TIDEDirective) to High(TIDEDirective) do
    if AnsiCompareText(IDEDirectiveNames[Result],DirectiveName)=0 then exit;
  Result:=idedNone;
end;

function IDEDirBuildScanFlagNameToFlag(const FlagName: string
  ): TIDEDirBuildScanFlag;
begin
  for Result:=Low(TIDEDirBuildScanFlag) to High(TIDEDirBuildScanFlag) do
    if AnsiCompareText(IDEDirBuildScanFlagNames[Result],FlagName)=0 then
      exit;
  Result:=idedbsfNone;
end;

function GetIDEDirBuildScanFromString(const s: string): TIDEDirBuildScanFlags;
var
  f: TIDEDirBuildScanFlag;
begin
  Result:=[];
  for f:=Low(TIDEDirBuildScanFlag) to High(TIDEDirBuildScanFlag) do begin
    if f=idedbsfNone then continue;
    if GetIDEDirectiveFlag(s,IDEDirBuildScanFlagNames[f],
                           f in IDEDirBuildScanFlagDefValues)
    then
      Include(Result,f);
  end;
end;

function GetIDEDirBuildScanStrFromFlags(Flags: TIDEDirBuildScanFlags): string;
var
  f: TIDEDirBuildScanFlag;
begin
  Result:='';
  for f:=Low(TIDEDirBuildScanFlag) to High(TIDEDirBuildScanFlag) do begin
    if f=idedbsfNone then continue;
    if (f in Flags)<>(f in IDEDirBuildScanFlagDefValues) then
      AddFlagStr(Result,IDEDirBuildScanFlagNames[f],f in Flags);
  end;
end;

function IDEDirRunFlagNameToFlag(const FlagName: string
  ): TIDEDirRunFlag;
begin
  for Result:=Low(TIDEDirRunFlag) to High(TIDEDirRunFlag) do
    if AnsiCompareText(IDEDirRunFlagNames[Result],FlagName)=0 then
      exit;
  Result:=idedrfNone;
end;

function GetIDEDirRunFlagFromString(const s: string): TIDEDirRunFlags;
var
  f: TIDEDirRunFlag;
begin
  Result:=[];
  for f:=Low(TIDEDirRunFlag) to High(TIDEDirRunFlag) do begin
    if f=idedrfNone then continue;
    if GetIDEDirectiveFlag(s,IDEDirRunFlagNames[f],
                           f in IDEDirRunFlagDefValues)
    then
      Include(Result,f);
  end;
end;

function GetIDEDirRunFlagStrFromFlags(Flags: TIDEDirRunFlags): string;
var
  f: TIDEDirRunFlag;
begin
  Result:='';
  for f:=Low(TIDEDirRunFlag) to High(TIDEDirRunFlag) do begin
    if f=idedrfNone then continue;
    if (f in Flags)<>(f in IDEDirRunFlagDefValues) then
      AddFlagStr(Result,IDEDirRunFlagNames[f],f in Flags);
  end;
end;

{ TBuildFileDialog }

procedure TBuildFileDialog.BuildFileDialogKEYDOWN(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if Key=VK_ESCAPE then ModalResult:=mrCancel;
end;

procedure TBuildFileDialog.BuildMacroSelectionBoxAddMacro(Sender: TObject);
var
  MacroCode: string;
  Macro: TTransferMacro;
begin
  Macro:=BuildMacroSelectionBox.GetSelectedMacro(MacroCode);
  if Macro=nil then exit;
  BuildCommandMemo.SelText:=MacroCode;
end;

procedure TBuildFileDialog.HelpButtonClick(Sender: TObject);
begin
  ShowContextHelpForIDE(Self);
end;

procedure TBuildFileDialog.OkButtonClick(Sender: TObject);
begin
  WriteDirectiveList;
  ModalResult:=mrOk;
end;

procedure TBuildFileDialog.RunMacroSelectionBoxAddMacro(Sender: TObject);
var
  MacroCode: string;
  Macro: TTransferMacro;
begin
  Macro:=RunMacroSelectionBox.GetSelectedMacro(MacroCode);
  if Macro=nil then exit;
  RunCommandMemo.SelText:=MacroCode;
end;

procedure TBuildFileDialog.BuildFileDialogCREATE(Sender: TObject);
begin
  Notebook1.PageIndex:=0;
  
  BuildMacroSelectionBox:=TMacroSelectionBox.Create(Self);
  with BuildMacroSelectionBox do begin
    Name:='BuildMacroSelectionBox';
    Caption:=lisEdtExtToolMacros;
    OnAddMacro:=@BuildMacroSelectionBoxAddMacro;
    AnchorToNeighbour(akTop,0,BuildScanForMakeMsgCheckbox);
    BorderSpacing.Around:=6;
    Align:=alClient;
    Parent:=BuildPage;
  end;

  RunMacroSelectionBox:=TMacroSelectionBox.Create(Self);
  with RunMacroSelectionBox do begin
    Name:='RunMacroSelectionBox';
    Caption:=lisEdtExtToolMacros;
    OnAddMacro:=@RunMacroSelectionBoxAddMacro;
    AnchorToNeighbour(akTop,0,RunCommandGroupbox);
    BorderSpacing.Around:=6;
    Align:=alClient;
    Parent:=RunPage;
  end;

  GeneralPage.Caption:=dlgFROpts;
  WhenFileIsActiveGroupbox.Caption:=lisBFWhenThisFileIsActiveInSourceEditor;
  OverrideBuildProjectCheckbox.Caption:=
    lisBFOnBuildProjectExecuteTheBuildFileCommandInstead;
  OverrideRunProjectCheckbox.Caption:=
    lisBFOnRunProjectExecuteTheRunFileCommandInstead;

  BuildPage.Caption:=lisBFBuild;
  BuildWorkingDirGroupbox.Caption:=lisBFWorkingDirectoryLeaveEmptyForFilePath;
  BuildCommandGroupbox.Caption:=lisBFBuildCommand;
  BuildScanForFPCMsgCheckbox.Caption:=lisCOScanForFPCMessages;
  BuildScanForMakeMsgCheckbox.Caption:=lisCOScanForMakeMessages;

  RunPage.Caption:=lisBFRun;
  AlwaysCompileFirstCheckbox.Caption:=lisBFAlwaysBuildBeforeRun;
  RunWorkDirGroupbox.Caption:=lisBFWorkingDirectoryLeaveEmptyForFilePath2;
  RunCommandGroupbox.Caption:=lisBFRunCommand;

  ButtonPanel.HelpButton.OnClick := @HelpButtonClick;
  ButtonPanel.OKButton.OnClick := @OKButtonClick;
end;

procedure TBuildFileDialog.BuildBrowseWorkDirButtonCLICK(Sender: TObject);
var
  OpenDialog: TSelectDirectoryDialog;
  NewFilename: String;
  ComboBox: TComboBox;
begin
  OpenDialog:=TSelectDirectoryDialog.Create(Self);
  try
    InputHistories.ApplyFileDialogSettings(OpenDialog);
    if Sender=BuildBrowseWorkDirButton then
      OpenDialog.Title:=lisWorkingDirectoryForBuilding
    else if Sender=RunBrowseWorkDirButton then
      OpenDialog.Title:=lisWorkingDirectoryForRun
    else
      exit;
    OpenDialog.Filename:='';
    OpenDialog.InitialDir:=ExtractFilePath(Filename);
    if OpenDialog.Execute then begin
      NewFilename:=TrimFilename(OpenDialog.Filename);
      if Sender=BuildBrowseWorkDirButton then
        ComboBox:=BuildWorkDirCombobox
      else if Sender=RunBrowseWorkDirButton then
        ComboBox:=RunWorkDirCombobox;
      SetComboBoxText(ComboBox,NewFilename);
    end;
    InputHistories.StoreFileDialogSettings(OpenDialog);
  finally
    OpenDialog.Free;
  end;
end;

procedure TBuildFileDialog.SetDirectiveList(const AValue: TStrings);
begin
  if FDirectiveList=AValue then exit;
  FDirectiveList:=AValue;
  ReadDirectiveList;
end;

procedure TBuildFileDialog.SetFilename(const AValue: string);
begin
  if FFilename=AValue then exit;
  FFilename:=AValue;
  UpdateCaption;
end;

procedure TBuildFileDialog.SetMacroList(const AValue: TTransferMacroList);
begin
  if FMacroList=AValue then exit;
  FMacroList:=AValue;
  BuildMacroSelectionBox.MacroList:=MacroList;
  RunMacroSelectionBox.MacroList:=MacroList;
end;

procedure TBuildFileDialog.SetBuildFileIfActive(const AValue: boolean);
begin
  OverrideBuildProjectCheckbox.Checked:=AValue;
end;

function TBuildFileDialog.GetBuildFileIfActive: boolean;
begin
  Result:=OverrideBuildProjectCheckbox.Checked;
end;

function TBuildFileDialog.GetRunFileIfActive: boolean;
begin
  Result:=OverrideRunProjectCheckbox.Checked;
end;

procedure TBuildFileDialog.SetRunFileIfActive(const AValue: boolean);
begin
  OverrideRunProjectCheckbox.Checked:=AValue;
end;

procedure TBuildFileDialog.UpdateCaption;
begin
  Caption:=Format(lisConfigureBuild, [Filename]);
end;

procedure TBuildFileDialog.ReadDirectiveList;
var
  BuildWorkingDir: String;
  BuildCommand: String;
  BuildScanForFPCMsg: Boolean;
  BuildScanForMakeMsg: Boolean;
  AlwaysBuildBeforeRun: Boolean;
  RunWorkingDir: String;
  RunCommand: String;
  BuildScanStr: String;
  BuildScan: TIDEDirBuildScanFlags;
  RunFlags: TIDEDirRunFlags;
begin
  // get values from directive list
  // build
  BuildWorkingDir:=GetIDEStringDirective(DirectiveList,
                                       IDEDirectiveNames[idedBuildWorkingDir],
                                       '');
  BuildCommand:=GetIDEStringDirective(DirectiveList,
                                    IDEDirectiveNames[idedBuildCommand],
                                    IDEDirDefaultBuildCommand);
  BuildScanStr:=GetIDEStringDirective(DirectiveList,
                                 IDEDirectiveNames[idedBuildScan],'');
  BuildScan:=GetIDEDirBuildScanFromString(BuildScanStr);
  BuildScanForFPCMsg:=idedbsfFPC in BuildScan;
  BuildScanForMakeMsg:=idedbsfMake in BuildScan;

  // run
  RunFlags:=GetIDEDirRunFlagFromString(
               GetIDEStringDirective(DirectiveList,
                                     IDEDirectiveNames[idedRunFlags],''));
  AlwaysBuildBeforeRun:=idedrfBuildBeforeRun in RunFlags;
  RunWorkingDir:=GetIDEStringDirective(DirectiveList,
                                       IDEDirectiveNames[idedRunWorkingDir],'');
  RunCommand:=GetIDEStringDirective(DirectiveList,
                                  IDEDirectiveNames[idedRunCommand],
                                  IDEDirDefaultRunCommand);

  // set values to dialog
  BuildWorkDirCombobox.Text:=BuildWorkingDir;
  BuildCommandMemo.Lines.Text:=BuildCommand;
  BuildScanForFPCMsgCheckbox.Checked:=BuildScanForFPCMsg;
  BuildScanForMakeMsgCheckbox.Checked:=BuildScanForMakeMsg;
  AlwaysCompileFirstCheckbox.Checked:=AlwaysBuildBeforeRun;
  RunWorkDirCombobox.Text:=RunWorkingDir;
  RunCommandMemo.Lines.Text:=RunCommand;
end;

procedure TBuildFileDialog.WriteDirectiveList;
var
  BuildWorkingDir: String;
  BuildCommand: String;
  BuildScanForFPCMsg: Boolean;
  BuildScanForMakeMsg: Boolean;
  BuildScan: TIDEDirBuildScanFlags;
  AlwaysBuildBeforeRun: Boolean;
  RunWorkingDir: String;
  RunCommand: String;
  RunFlags: TIDEDirRunFlags;
begin
  // get values from dialog
  // build
  BuildWorkingDir:=SpecialCharsToSpaces(BuildWorkDirCombobox.Text,true);
  BuildCommand:=SpecialCharsToSpaces(BuildCommandMemo.Lines.Text,true);
  BuildScanForFPCMsg:=BuildScanForFPCMsgCheckbox.Checked;
  BuildScanForMakeMsg:=BuildScanForMakeMsgCheckbox.Checked;
  BuildScan:=[];
  if BuildScanForFPCMsg then Include(BuildScan,idedbsfFPC);
  if BuildScanForMakeMsg then Include(BuildScan,idedbsfMake);

  // run
  AlwaysBuildBeforeRun:=AlwaysCompileFirstCheckbox.Checked;
  RunFlags:=[];
  if AlwaysBuildBeforeRun then Include(RunFlags,idedrfBuildBeforeRun);
  RunWorkingDir:=SpecialCharsToSpaces(RunWorkDirCombobox.Text,true);
  RunCommand:=SpecialCharsToSpaces(RunCommandMemo.Lines.Text,true);
  
  // set values to directivelist
  //DebugLn(['TBuildFileDialog.WriteDirectiveList ']);
  SetIDEDirective(DirectiveList,IDEDirectiveNames[idedBuildWorkingDir],
                  BuildWorkingDir,'');
  SetIDEDirective(DirectiveList,IDEDirectiveNames[idedBuildCommand],
                  BuildCommand,IDEDirDefaultBuildCommand);
  SetIDEDirective(DirectiveList,IDEDirectiveNames[idedBuildScan],
                  GetIDEDirBuildScanStrFromFlags(BuildScan),'');
  SetIDEDirective(DirectiveList,IDEDirectiveNames[idedRunWorkingDir],
                  RunWorkingDir,'');
  SetIDEDirective(DirectiveList,IDEDirectiveNames[idedRunCommand],
                  RunCommand,IDEDirDefaultRunCommand);
  SetIDEDirective(DirectiveList,IDEDirectiveNames[idedRunFlags],
                  GetIDEDirRunFlagStrFromFlags(RunFlags),'');
end;

{ TMacroSelectionBox }

procedure TMacroSelectionBox.ListBoxClick(Sender: TObject);
begin
  AddButton.Enabled:=(Listbox.ItemIndex>=0);
end;

procedure TMacroSelectionBox.AddButtonClick(Sender: TObject);
begin
  if Assigned(OnAddMacro) then OnAddMacro(Self);
end;

procedure TMacroSelectionBox.SetMacroList(const AValue: TTransferMacroList);
begin
  if FMacroList=AValue then exit;
  FMacroList:=AValue;
  FillListBox;
end;

procedure TMacroSelectionBox.FillListBox;
var
  i: Integer;
  Macro: TTransferMacro;
begin
  ListBox.Items.BeginUpdate;
  ListBox.Items.Clear;
  if MacroList=nil then exit;
  for i:=0 to MacroList.Count-1 do begin
    Macro:=MacroList[i];
    if Macro.MacroFunction=nil then begin
      Listbox.Items.Add('$('+Macro.Name+') - '+Macro.Description);
    end else begin
      Listbox.Items.Add('$'+Macro.Name+'() - '+Macro.Description);
    end;
  end;
  ListBox.Items.EndUpdate;
end;

constructor TMacroSelectionBox.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  
  AddButton:=TButton.Create(Self);
  with AddButton do begin
    Name:='AddButton';
    Caption:=lisCodeTemplAdd;
    OnClick:=@AddButtonClick;
    Enabled:=false;
    AutoSize:=true;
    Anchors:=[akTop,akRight];
    Top:=0;
    BorderSpacing.Around := 6;
    AnchorParallel(akTop,0,Self);
    AnchorParallel(akRight,0,Self);
    Parent:=Self;
  end;
  
  ListBox:=TListBox.Create(Self);
  with ListBox do begin
    Name:='ListBox';
    OnClick:=@ListBoxClick;
    Align:=alLeft;
    BorderSpacing.Around := 6;
    AnchorToNeighbour(akRight, 0, AddButton);
    Parent:=Self;
  end;
end;

function TMacroSelectionBox.GetSelectedMacro(
  var MacroAsCode: string): TTransferMacro;
var
  i: integer;
begin
  Result:=nil;
  MacroAsCode:='';
  if MacroList=nil then exit;
  i:=Listbox.ItemIndex;
  if i<0 then exit;
  Result:=MacroList[i];
  if Result.MacroFunction=nil then
    MacroAsCode:='$('+Result.Name+')'
  else
    MacroAsCode:='$'+Result.Name+'()';
end;

initialization
  IDEDirectiveSpecialChars:='{}*%';

end.