This file is indexed.

/usr/lib/lazarus/0.9.30.4/ide/diffdialog.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
{  $Id: diffdialog.pas 25511 2010-05-19 09:40:19Z mattias $  }
{
 /***************************************************************************
                              diffdialog.pas
                              --------------


 ***************************************************************************/

 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code 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.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 ***************************************************************************

  Author: Mattias Gaertner
  
  Abstract:
    The TDiffDlg is the dialog for showing the differences between two files.

}

unit DiffDialog;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Math, Forms, Controls, Buttons, StdCtrls, FileUtil,
  LazarusIDEStrConsts, EditorOptions, IDEWindowIntf, LCLType,
  InputHistory, DiffPatch, ExtCtrls, Dialogs, SynEdit, SynHighlighterDiff, IDEContextHelpEdit,
  SourceEditor;

type

  { TDiffFile }

  TDiffFile = class
  public
    Name: string;
    Editor: TSourceEditor;
    SelectionAvailable: boolean;
    constructor Create(const NewName: string; NewEditor: TSourceEditor;
      NewSelectionAvailable: boolean);
  end;


  { TDiffFiles }

  TDiffFiles = class(TList)
  private
    function GetItems(Index: integer): TDiffFile;
    procedure SetItems(Index: integer; const AValue: TDiffFile);
  public
    procedure Clear; override;
    function Add(DiffFile: TDiffFile): integer;
    function IndexOfName(const Name: string): integer;
  public
    property Items[Index: integer]: TDiffFile read GetItems write SetItems; default;
  end;


  { TDiffDlg }
  
  TDiffDlg = class(TForm)
    HelpButton: TBitBtn;
    CloseButton: TBitBtn;
    DiffSynEdit: TSynEdit;
    OpenInEditorButton: TBitBtn;
    SaveDiffButton: TBitBtn;
    SynDiffSyn1: TSynDiffSyn;
    Text1FileOpenButton: TButton;
    dlgSave: TSaveDialog;
    dlgOpen: TOpenDialog;

    Text2FileOpenButton: TButton;

    // text 1
    Text1GroupBox: TGroupBox;
    Text1Combobox: TComboBox;
    Text1OnlySelectionCheckBox: TCheckBox;
    
    // text 2
    Text2GroupBox: TGroupBox;
    Text2Combobox: TComboBox;
    Text2OnlySelectionCheckBox: TCheckBox;

    // options
    OptionsGroupBox: TCheckGroup;

    procedure FileOpenClick(Sender: TObject);
    procedure HelpButtonClick(Sender: TObject);
    procedure OnChangeFlag(Sender: TObject);
    procedure SaveDiffButtonClick(Sender: TObject);
    procedure Text1ComboboxChange(Sender: TObject);
    procedure Text2ComboboxChange(Sender: TObject);
  private
    fDiffNeedsUpdate: boolean;
    FLockCount: integer;
    procedure SetupComponents;
    procedure UpdateDiff;
  public
    Files: TDiffFiles;
    Text1: TDiffFile;
    Text2: TDiffFile;
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure Init;
    procedure FillTextComboBoxes;
    procedure SetText1Index(NewIndex: integer);
    procedure SetText2Index(NewIndex: integer);
    procedure SaveSettings;
    procedure SetDiffOptions(NewOptions: TTextDiffFlags);
    function GetDiffOptions: TTextDiffFlags;
    procedure BeginUpdate;
    procedure EndUpdate;
  end;
  
function ShowDiffDialog(Text1Index: integer;
  var OpenDiffInEditor: boolean; var Diff: string): TModalResult;

const
  IgnoreCaseCheckBox = 0;
  IgnoreEmptyLineChangesCheckBox = 1;
  IgnoreHeadingSpacesCheckBox = 2;
  IgnoreLineEndsCheckBox = 3;
  IgnoreSpaceCharAmountCheckBox = 4;
  IgnoreSpaceCharsCheckBox = 5;
  IgnoreTrailingSpacesCheckBox = 6;

implementation

{$R *.lfm}

function ShowDiffDialog(Text1Index: integer;
  var OpenDiffInEditor: boolean; var Diff: string): TModalResult;
var
  DiffDlg: TDiffDlg;
  Files: TDiffFiles;
  i: Integer;
  SrcEdit: TSourceEditor;
begin
  Files := TDiffFiles.Create;
  for i:=0 to SourceEditorManager.SourceEditorCount - 1 do begin
    SrcEdit := SourceEditorManager.SourceEditors[i]; // FindSourceEditorWithPageIndex(i);
    Files.Add(TDiffFile.Create(SrcEdit.PageName, SrcEdit, SrcEdit.SelectionAvailable));
  end;

  OpenDiffInEditor:=false;
  DiffDlg:=TDiffDlg.Create(nil);
  DiffDlg.BeginUpdate;
  DiffDlg.Files:=Files;
  DiffDlg.SetText1Index(Text1Index);
  DiffDlg.Init;
  DiffDlg.EndUpdate;
  Result:=DiffDlg.ShowModal;
  DiffDlg.SaveSettings;

  if Result=mrYes then begin
    OpenDiffInEditor:=true;
    Diff:=DiffDlg.DiffSynEdit.Text;
    Result:=mrOk;
  end;

  Files.Free;
  DiffDlg.Free;
end;

{ TDiffDlg }

procedure TDiffDlg.OnChangeFlag(Sender: TObject);
begin
  UpdateDiff;
end;

procedure TDiffDlg.FileOpenClick(Sender: TObject);
begin
  if dlgOpen.Execute then
  begin
    //only add new files
    if Text1ComboBox.Items.IndexOf(dlgOpen.FileName) = -1 then
    begin
      Files.Add(TDiffFile.Create(dlgOpen.FileName,nil,False));
      Text1ComboBox.Items.Add(dlgOpen.FileName);
      Text2ComboBox.Items.Add(dlgOpen.FileName);
    end;
    
    //set the combobox and make the diff
    if TButton(Sender).Name = 'Text1FileOpenButton'then
    with Text1ComboBox do
    begin
      ItemIndex := Items.IndexOf(dlgOpen.FileName);
      SetText1Index(Items.IndexOf(dlgOpen.FileName));
    end
    else
    with Text2ComboBox do
    begin
      ItemIndex := Items.IndexOf(dlgOpen.FileName);
      SetText2Index(Items.IndexOf(dlgOpen.FileName));
    end;
  end;
end;

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

procedure TDiffDlg.SaveDiffButtonClick(Sender: TObject);
begin
  if dlgSave.Execute then
    DiffSynEdit.Lines.SaveToFile(UTF8ToSys(dlgSave.FileName));
end;

procedure TDiffDlg.Text1ComboboxChange(Sender: TObject);
begin
  SetText1Index(Text1Combobox.Items.IndexOf(Text1Combobox.Text));
end;

procedure TDiffDlg.Text2ComboboxChange(Sender: TObject);
begin
  SetText2Index(Text2Combobox.Items.IndexOf(Text2Combobox.Text));
end;

procedure TDiffDlg.SetupComponents;
begin
  // text 1
  Text1GroupBox.Caption:=lisDiffDlgText1;
  Text1OnlySelectionCheckBox.Caption:=lisDiffDlgOnlySelection;
  Text1FileOpenButton.Caption:='...';

  // text 2
  Text2GroupBox.Caption:=lisDiffDlgText2;
  Text2OnlySelectionCheckBox.Caption:=lisDiffDlgOnlySelection;
  Text2FileOpenButton.Caption:='...';

  // options
  with OptionsGroupBox do
  begin
    Caption:=dlgFROpts;
    Items.Add(lisDiffDlgCaseInsensitive);
    Items.Add(lisDiffDlgIgnoreIfEmptyLinesWereAdd);
    Items.Add(lisDiffDlgIgnoreSpacesAtStartOfLine);
    Items.Add(lisDiffDlgIgnoreSpacesAtEndOfLine);
    Items.Add(lisDiffDlgIgnoreIfLineEndCharsDiffe);
    Items.Add(lisDiffDlgIgnoreIfSpaceCharsWereAdd);
    Items.Add(lisDiffDlgIgnoreSpaces);
  end;

  // buttons
  CloseButton.Caption:=lisMenuClose;
  OpenInEditorButton.Caption:=lisDiffDlgOpenDiffInEditor;
  SaveDiffButton.Caption:=lisSave;
  HelpButton.Caption:=lisMenuHelp;

  OpenInEditorButton.LoadGlyphFromStock(idButtonOpen);
  if OpenInEditorButton.Glyph.Empty then
    OpenInEditorButton.LoadGlyphFromLazarusResource('laz_open');
  
  SaveDiffButton.LoadGlyphFromStock(idButtonSave);
  if SaveDiffButton.Glyph.Empty then
    SaveDiffButton.LoadGlyphFromLazarusResource('laz_save');

  // dialogs
  dlgOpen.Title:=lisOpenExistingFile;
  dlgOpen.Filter:=dlgAllFiles+' ('+GetAllFilesMask+')|'+GetAllFilesMask
                 +'|'+lisLazarusUnit+' (*.pas;*.pp)|*.pas;*.pp'
                 +'|'+lisLazarusProject+' (*.lpi)|*.lpi'
                 +'|'+lisLazarusForm+' (*.lfm;*.dfm)|*.lfm;*.dfm'
                 +'|'+lisLazarusPackage+' (*.lpk)|*.lpk'
                 +'|'+lisLazarusProjectSource+' (*.lpr)|*.lpr';
  dlgSave.Title:=lisSaveFileAs;
  dlgSave.Filter:=dlgAllFiles+' ('+GetAllFilesMask+')|'+GetAllFilesMask;

  // diff
  EditorOpts.GetSynEditSettings(DiffSynEdit);
end;

procedure TDiffDlg.UpdateDiff;
var
  Text1Src, Text2Src: string;
  DiffTxt: String;
  dat : TStrings;
begin
  if FLockCount>0 then begin
    fDiffNeedsUpdate:=true;
    exit;
  end;
  fDiffNeedsUpdate:=false;
  if (Text1=nil) or (Text2=nil) then begin
    DiffSynEdit.Lines.Text:='';
  end else begin
    if Text1.Editor = nil then
      begin
        dat := TStringList.Create;
        dat.LoadFromFile(UTF8ToSys(Text1.Name));
        Text1Src := dat.Text;
        dat.Free;
      end
    else begin
      if (Text1.SelectionAvailable and Text1OnlySelectionCheckBox.Checked) then
        Text1Src := Text1.Editor.EditorComponent.SelText
      else
        Text1Src := Text1.Editor.EditorComponent.Lines.Text;
    end;

    if Text2.Editor = nil then
      begin
        dat := TStringList.Create;
        dat.LoadFromFile(UTF8ToSys(Text2.Name));
        Text2Src := dat.Text;
        dat.Free;
      end
    else begin
      if (Text2.SelectionAvailable and Text2OnlySelectionCheckBox.Checked) then
        Text2Src := Text2.Editor.EditorComponent.SelText
      else
        Text2Src := Text2.Editor.EditorComponent.Lines.Text;
    end;

    DiffTxt:=CreateTextDiff(Text1Src,Text2Src,GetDiffOptions,tdoContext);
    
    DiffSynEdit.Lines.Text:=DiffTxt;
  end;
end;

constructor TDiffDlg.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  Caption := lisMenuDiff;
  IDEDialogLayoutList.ApplyLayout(Self,600,500);
  SetupComponents;
end;

destructor TDiffDlg.Destroy;
begin
  inherited Destroy;
end;

procedure TDiffDlg.Init;
var
  LastText2Name: String;
  i: Integer;
begin
  // fill all diff file names
  FillTextComboBoxes;
  
  // get recent Text 2
  LastText2Name:=InputHistories.DiffText2;
  if LastText2Name<>'' then
    i:=Files.IndexOfName(LastText2Name);
  if i<0 then i:=0;
  if i=Files.IndexOf(Text2) then inc(i);
  SetText2Index(i);
  
  // set recent options
  SetDiffOptions(InputHistories.DiffFlags);

  // and action ...
  UpdateDiff;
end;

procedure TDiffDlg.FillTextComboBoxes;
var
  i: Integer;
begin
  // Text 1
  Text1Combobox.Items.BeginUpdate;
  Text1Combobox.Items.Clear;
  for i:=0 to Files.Count-1 do
    Text1Combobox.Items.Add(Files[i].Name);
  Text1Combobox.Items.EndUpdate;

  // Text 2
  Text2Combobox.Items.BeginUpdate;
  Text2Combobox.Items.Clear;
  for i:=0 to Files.Count-1 do
    Text2Combobox.Items.Add(Files[i].Name);
  Text2Combobox.Items.EndUpdate;
end;

procedure TDiffDlg.SetText1Index(NewIndex: integer);
var
  OldText1: TDiffFile;
begin
  OldText1:=Text1;
  if (NewIndex>=0) and (NewIndex<Files.Count) then begin
    Text1:=Files[NewIndex];
    Text1Combobox.Text:=Text1.Name;
    Text1OnlySelectionCheckBox.Enabled:=Text1.SelectionAvailable;
  end else begin
    Text1:=nil;
    Text1Combobox.Text:='';
    Text1OnlySelectionCheckBox.Enabled:=false;
  end;
  if Text1<>OldText1 then UpdateDiff;
end;

procedure TDiffDlg.SetText2Index(NewIndex: integer);
var
  OldText2: TDiffFile;
begin
  OldText2:=Text2;
  if (NewIndex>=0) and (NewIndex<Files.Count) then begin
    Text2:=Files[NewIndex];
    Text2Combobox.Text:=Text2.Name;
    Text2OnlySelectionCheckBox.Enabled:=Text2.SelectionAvailable;
  end else begin
    Text2:=nil;
    Text2Combobox.Text:='';
    Text2OnlySelectionCheckBox.Enabled:=false;
  end;
  if Text2<>OldText2 then UpdateDiff;
end;

procedure TDiffDlg.SaveSettings;
begin
  InputHistories.DiffFlags:=GetDiffOptions;
  if Text2<>nil then begin
    InputHistories.DiffText2:=Text2.Name;
    InputHistories.DiffText2OnlySelection:=Text2OnlySelectionCheckBox.Checked;
  end else begin
    InputHistories.DiffText2:='';
    InputHistories.DiffText2OnlySelection:=false;
  end;
  IDEDialogLayoutList.SaveLayout(Self);
end;

procedure TDiffDlg.SetDiffOptions(NewOptions: TTextDiffFlags);
begin
  OptionsGroupBox.Checked[IgnoreCaseCheckBox]:=tdfIgnoreCase in NewOptions;
  OptionsGroupBox.Checked[IgnoreEmptyLineChangesCheckBox]:=tdfIgnoreEmptyLineChanges in NewOptions;
  OptionsGroupBox.Checked[IgnoreHeadingSpacesCheckBox]:=tdfIgnoreHeadingSpaces in NewOptions;
  OptionsGroupBox.Checked[IgnoreLineEndsCheckBox]:=tdfIgnoreLineEnds in NewOptions;
  OptionsGroupBox.Checked[IgnoreSpaceCharAmountCheckBox]:=tdfIgnoreSpaceCharAmount in NewOptions;
  OptionsGroupBox.Checked[IgnoreSpaceCharsCheckBox]:=tdfIgnoreSpaceChars in NewOptions;
  OptionsGroupBox.Checked[IgnoreTrailingSpacesCheckBox]:=tdfIgnoreTrailingSpaces in NewOptions;
end;

function TDiffDlg.GetDiffOptions: TTextDiffFlags;
begin
  Result:=[];
  if OptionsGroupBox.Checked[IgnoreCaseCheckBox] then
    Include(Result,tdfIgnoreCase);
  if OptionsGroupBox.Checked[IgnoreEmptyLineChangesCheckBox] then
    Include(Result,tdfIgnoreEmptyLineChanges);
  if OptionsGroupBox.Checked[IgnoreHeadingSpacesCheckBox] then
    Include(Result,tdfIgnoreHeadingSpaces);
  if OptionsGroupBox.Checked[IgnoreLineEndsCheckBox] then
    Include(Result,tdfIgnoreLineEnds);
  if OptionsGroupBox.Checked[IgnoreSpaceCharAmountCheckBox] then
    Include(Result,tdfIgnoreSpaceCharAmount);
  if OptionsGroupBox.Checked[IgnoreSpaceCharsCheckBox] then
    Include(Result,tdfIgnoreSpaceChars);
  if OptionsGroupBox.Checked[IgnoreTrailingSpacesCheckBox] then
    Include(Result,tdfIgnoreTrailingSpaces);
end;

procedure TDiffDlg.BeginUpdate;
begin
  inc(FLockCount);
end;

procedure TDiffDlg.EndUpdate;
begin
  dec(FLockCount);
  if (FLockCount=0) and fDiffNeedsUpdate then UpdateDiff;
end;

{ TDiffFile }

constructor TDiffFile.Create(const NewName: string; NewEditor: TSourceEditor;
  NewSelectionAvailable: boolean);
begin
  Name:=NewName;
  Editor:=NewEditor;
  SelectionAvailable:=NewSelectionAvailable;
end;

{ TDiffFiles }

function TDiffFiles.GetItems(Index: integer): TDiffFile;
begin
  Result:=TDiffFile(inherited Items[Index]);
end;

procedure TDiffFiles.SetItems(Index: integer; const AValue: TDiffFile);
begin
  inherited Items[Index]:=AValue;
end;

procedure TDiffFiles.Clear;
var
  i: Integer;
begin
  for i:=0 to Count-1 do
    Items[i].Free;
  inherited Clear;
end;

function TDiffFiles.Add(DiffFile: TDiffFile): integer;
begin
  Result:=inherited Add(DiffFile);
end;

function TDiffFiles.IndexOfName(const Name: string): integer;
begin
  Result:=Count-1;
  while (Result>=0) and (Items[Result].Name<>Name) do dec(Result);
end;

end.