This repository was archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathchgencodingdlg.pas
401 lines (364 loc) · 12.6 KB
/
chgencodingdlg.pas
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
{
***************************************************************************
* *
* 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
Dialog and functions to change encodings (e.g. UTF-8) of projects and packages.
}
unit ChgEncodingDlg;
{$mode objfpc}{$H+}
interface
uses
// RTL + FCL
Classes, SysUtils, RegExpr, Laz_AVL_Tree,
// LCL
LCLProc, Forms, Controls, ExtCtrls, StdCtrls, ComCtrls, Buttons,
// CodeTools
CodeCache, CodeToolManager, FileProcs,
// LazUtils
LConvEncoding, LazFileUtils, LazFileCache, AvgLvlTree,
// IDEIntf
IDEWindowIntf, SrcEditorIntf, IDEHelpIntf,
// IDE
IDEProcs, PackageDefs, PackageSystem, Project, LazarusIDEStrConsts;
type
{ TChgEncodingDialog }
TChgEncodingDialog = class(TForm)
ApplyButton: TBitBtn;
HelpButton: TBitBtn;
BtnPanel: TPanel;
CloseButton: TBitBtn;
LabelNoPreview: TLabel;
RegExprErrorLabel: TLabel;
NewEncodingComboBox: TComboBox;
FileFilterCombobox: TComboBox;
NewEncodingLabel: TLabel;
PreviewListView: TListView;
PreviewGroupBox: TGroupBox;
OwnerComboBox: TComboBox;
ScopeGroupBox: TGroupBox;
RegExprCheckBox: TCheckBox;
FileFilterLabel: TLabel;
NonUTF8FilesCheckBox: TCheckBox;
UTF8FilesCheckBox: TCheckBox;
FilesGroupBox: TGroupBox;
procedure ApplyButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure HelpButtonClick(Sender: TObject);
procedure PreviewClick(Sender: TObject);
private
FFiles: TFilenameToStringTree;
function GetFiles: Boolean;
procedure UpdatePreview;
public
end;
function ShowConvertEncodingDlg: TModalResult;
implementation
{$R *.lfm}
function ShowConvertEncodingDlg: TModalResult;
var
ChgEncodingDialog: TChgEncodingDialog;
begin
ChgEncodingDialog:=TChgEncodingDialog.Create(nil);
Result:=ChgEncodingDialog.ShowModal;
ChgEncodingDialog.Free;
end;
{ TChgEncodingDialog }
procedure TChgEncodingDialog.FormCreate(Sender: TObject);
var
List: TStringList;
Encoding: string;
i: Integer;
begin
IDEDialogLayoutList.ApplyLayout(Self);
Caption:=lisConvertEncoding;
ScopeGroupBox.Caption:=lisConvertProjectOrPackage;
NewEncodingLabel.Caption:=lisNewEncoding;
FilesGroupBox.Caption:=lisFileFilter;
UTF8FilesCheckBox.Caption:=lisFilesInASCIIOrUTF8Encoding;
NonUTF8FilesCheckBox.Caption:=lisFilesNotInASCIINorUTF8Encoding;
FileFilterLabel.Caption:=lisFilter;
RegExprCheckBox.Caption:=lisRegularExpression;
CloseButton.Caption:=lisClose;
ApplyButton.Caption:=lisConvert;
HelpButton.Caption:=lisHelp;
CloseButton.LoadGlyphFromResourceName(hInstance, 'btn_close');
ApplyButton.LoadGlyphFromResourceName(hInstance, 'btn_ok');
HelpButton.LoadGlyphFromResourceName(hInstance, 'btn_help');
PreviewGroupBox.Caption:=dlgWRDPreview;
PreviewListView.Column[0].Caption:=dlgEnvFiles;
PreviewListView.Column[0].Width:=350;
PreviewListView.Column[1].Caption:=uemEncoding;
// get possible encodings
List:=TStringList.Create;
GetSupportedEncodings(List);
for i:=List.Count-1 downto 0 do begin
Encoding:=List[i];
if (Encoding='') or (SysUtils.CompareText(Encoding,EncodingAnsi)=0) then
List.Delete(i);
end;
List.Sort;
NewEncodingComboBox.Items.Assign(List);
List.Free;
NewEncodingComboBox.Text:='UTF-8';
// get possible filters
List:=TStringList.Create;
List.Add('*.pas;*.pp;*.p;*.inc;*.lpr;*.lfm;*.lrs;*.txt');
List.Sort;
FileFilterCombobox.Items.Assign(List);
List.Free;
FileFilterCombobox.Text:=FileFilterCombobox.Items[0];
// get possible projects and packages
List:=TStringList.Create;
for i:=0 to PackageGraph.Count-1 do
if (List.IndexOf(PackageGraph[i].Name)<0)
and (not PackageGraph[i].ReadOnly)
and (not PackageGraph[i].IsVirtual)
then
List.Add(PackageGraph[i].Name);
List.Sort;
if not Project1.IsVirtual then
List.Insert(0,lisEdtDefCurrentProject);
OwnerComboBox.Items.Assign(List);
List.Free;
if OwnerComboBox.Items.Count>0 then
OwnerComboBox.Text:=OwnerComboBox.Items[0]
else
OwnerComboBox.Text:='';
FFiles:=TFilenameToStringTree.Create(FilenamesCaseSensitive);
UpdatePreview;
end;
procedure TChgEncodingDialog.FormDestroy(Sender: TObject);
begin
IDEDialogLayoutList.SaveLayout(Self);
FFiles.Free;
end;
procedure TChgEncodingDialog.ApplyButtonClick(Sender: TObject);
var
Buf: TCodeBuffer;
SrcEdit: TSourceEditorInterface;
Encoding, OldEncoding, NewEncoding: String;
Node: TAVLTreeNode;
Item: PStringToStringItem;
Filename: String;
HasChanged: boolean;
li, Cur: TListItem;
OldCount, i: Integer;
begin
HasChanged:=False;
NewEncoding:=NormalizeEncoding(NewEncodingComboBox.Text);
PreviewListView.BeginUpdate;
OldCount := PreviewListView.Items.Count;
Node:=FFiles.Tree.FindLowest;
while Node<>nil do begin
Item:=PStringToStringItem(Node.Data);
Filename:=Item^.Name;
Encoding:=Item^.Value;
Cur := PreviewListView.Items.FindCaption(0, Filename, False, True, False);
if Assigned(Cur) and Cur.Checked then
begin
DebugLn(['TChgEncodingDialog.ApplyButtonClick Filename=',Filename,' Encoding=',Encoding]);
Buf:=CodeToolBoss.LoadFile(Filename,true,false);
if (Buf<>nil) and (not Buf.ReadOnly) then begin
OldEncoding:=Buf.DiskEncoding;
SrcEdit:=SourceEditorManagerIntf.SourceEditorIntfWithFilename(Filename);
HasChanged:=true;
if SrcEdit<>nil then begin
DebugLn(['TChgEncodingDialog.ApplyButtonClick changing in source editor: ',Filename]);
Buf.DiskEncoding:=NewEncoding;
SrcEdit.Modified:=true;
end else begin
DebugLn(['TChgEncodingDialog.ApplyButtonClick changing on disk: ',Filename]);
// Buf:=CodeToolBoss.LoadFile(Filename,true,false);
Buf.DiskEncoding:=NewEncoding;
HasChanged:=Buf.Save;
if not HasChanged then
Buf.DiskEncoding:=OldEncoding;
end;
end;
if not HasChanged then begin
li:=PreviewListView.Items.Add;
li.Caption:=Filename;
li.SubItems.Add(Encoding);
li.Checked := True;
end;
end;
Node:=FFiles.Tree.FindSuccessor(Node);
end;
// Now delete all old nodes in PreviewListView
for i := OldCount - 1 downto 0 do PreviewListView.Items.Delete(i);
PreviewListView.EndUpdate;
PreviewGroupBox.Caption:=Format(lisEncodingNumberOfFilesFailed, [PreviewListView.Items.Count]);
end;
procedure TChgEncodingDialog.HelpButtonClick(Sender: TObject);
begin
LazarusHelp.ShowHelpForIDEControl(Self);
end;
procedure TChgEncodingDialog.PreviewClick(Sender: TObject);
begin
UpdatePreview;
end;
function TChgEncodingDialog.GetFiles: Boolean;
// Returns true if some files were found, even if they were not added to list.
var
AProject: TProject;
SearchPath: String;
APackage: TLazPackage;
Dir: String;
FileInfo: TSearchRec;
CurFilename: String;
Buf: TCodeBuffer;
CurEncoding, NewEncoding: String;
IncludeFilterRegExpr: TRegExpr;
CurOwner: TObject;
Expr: String;
ok: Boolean;
p: Integer;
begin
FFiles.Clear;
Result:=False;
// check owner
if OwnerComboBox.Text=lisEdtDefCurrentProject then
CurOwner:=Project1
else
CurOwner:=PackageGraph.FindPackageWithName(OwnerComboBox.Text,nil);
if CurOwner=nil then begin
DebugLn(['TChgEncodingDialog.UpdatePreview package not found: ',OwnerComboBox.Text]);
exit;
end;
// find search paths
if CurOwner is TProject then begin
AProject:=TProject(CurOwner);
SearchPath:=AProject.SourceDirectories.CreateSearchPathFromAllFiles;
SearchPath:=MergeSearchPaths(SearchPath,AProject.CompilerOptions.GetIncludePath(false));
end else begin
APackage:=TLazPackage(CurOwner);
SearchPath:=APackage.SourceDirectories.CreateSearchPathFromAllFiles;
SearchPath:=MergeSearchPaths(SearchPath,APackage.CompilerOptions.GetIncludePath(false));
end;
// find files
IncludeFilterRegExpr:=TRegExpr.Create;
try
Expr:=FileFilterCombobox.Text;
if not RegExprCheckBox.Checked then
Expr:=SimpleSyntaxToRegExpr(Expr);
ok:=false;
try
IncludeFilterRegExpr.Expression:=Expr;
IncludeFilterRegExpr.Compile;
ok:=true;
except
on E: Exception do begin
DebugLn('Invalid Include File Expression ',Expr,' ',E.Message);
RegExprErrorLabel.Caption:=E.Message;
end;
end;
RegExprErrorLabel.Visible := not ok;
if not ok then exit;
NewEncoding:=NormalizeEncoding(NewEncodingComboBox.Text);
p:=1;
while (p<=length(SearchPath)) do begin
Dir:=GetNextDirectoryInSearchPath(SearchPath,p);
if Dir='' then continue;
Dir:=AppendPathDelim(Dir);
DebugLn(['TChgEncodingDialog.GetFiles Dir=',Dir]);
if FindFirstUTF8(Dir+FileMask,faAnyFile,FileInfo)=0 then
try
repeat
// check if special file
if (FileInfo.Name='.') or (FileInfo.Name='..') or (FileInfo.Name='') then
continue;
CurFilename:=Dir+FileInfo.Name;
if FFiles.Contains(CurFilename) then
continue;
if not IncludeFilterRegExpr.Exec(CurFilename) then
continue;
if not FileIsTextCached(CurFilename) then
continue;
if (FileInfo.Attr and faDirectory)>0 then begin
// skip directory
end else begin
Buf:=CodeToolBoss.LoadFile(CurFilename,true,false);
if Buf<>nil then begin
//DebugLn(['TChgEncodingDialog.GetFiles Filename=',CurFilename,' Encoding=',NormalizeEncoding(Buf.DiskEncoding)]);
CurEncoding:=NormalizeEncoding(Buf.DiskEncoding);
Result:=True;
if CurEncoding=NewEncoding then
continue;
if (CurEncoding=EncodingUTF8) and (not UTF8FilesCheckBox.Checked) then
continue;
if (CurEncoding<>EncodingUTF8) and (not NonUTF8FilesCheckBox.Checked) then
continue;
FFiles[CurFilename]:=Buf.DiskEncoding;
end else begin
DebugLn(['TChgEncodingDialog.UpdatePreview read error: ',CurFilename]);
end;
end;
until FindNextUTF8(FileInfo)<>0;
finally
FindCloseUTF8(FileInfo);
end;
end;
finally
IncludeFilterRegExpr.Free;
end;
end;
procedure TChgEncodingDialog.UpdatePreview;
var
Node: TAVLTreeNode;
Item: PStringToStringItem;
Filename: String;
Encoding: String;
li: TListItem;
HasFiles: Boolean;
IsDone: Boolean;
begin
Screen.Cursor:=crHourGlass;
try
HasFiles:=GetFiles;
PreviewListView.Items.Clear;
IsDone:=HasFiles and (FFiles.Tree.Count=0);
PreviewGroupBox.Visible:=not IsDone;
LabelNoPreview.Visible:=IsDone;
LabelNoPreview.Caption:=lisFilesHaveRightEncoding;
ApplyButton.Enabled:=not IsDone;
if IsDone then exit;
PreviewListView.BeginUpdate;
Node:=FFiles.Tree.FindLowest;
while Node<>nil do begin
Item:=PStringToStringItem(Node.Data);
Filename:=Item^.Name;
Encoding:=Item^.Value;
DebugLn(['TChgEncodingDialog.UpdatePreview Filename=',Filename,' Encoding=',Encoding]);
li:=PreviewListView.Items.Add;
li.Caption:=Filename;
li.SubItems.Add(Encoding);
li.Checked := True;
Node:=FFiles.Tree.FindSuccessor(Node);
end;
PreviewListView.EndUpdate;
PreviewGroupBox.Caption:=
Format(lisNumberOfFilesToConvert, [IntToStr(PreviewListView.Items.Count)]);
ApplyButton.Enabled:=True;
finally
Screen.Cursor:=crDefault;
end;
end;
end.