Skip to content

Commit 34ae163

Browse files
committed
Keep configuration files in a subdirectory
Entity mappings are now under the 'HTML 5' section; older configs have them under 'HTML 4', so no entities will be recognized in current and later plugin versions Since b7ea8a5, the user's entity file is preferred over the installed one. Changing the file path ensures the new file gets copied to the config directory and loaded after a plugin upgrade Old configs will be ignored, but that's a gentler breaking change than losing all entity decoding functionality
1 parent 85442c3 commit 34ae163

3 files changed

Lines changed: 33 additions & 22 deletions

File tree

src/Forms/AboutForm.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ constructor TFrmAbout.Create(AOwner: TComponent);
150150
WrapFilePath(txtHomeDir);
151151

152152
lblConfigDir := MakeText('Config location');
153-
txtConfigDir := MakeText(UTF8ToAnsi(UTF8Encode(Npp.ConfigDir)), 24);
153+
txtConfigDir := MakeText(UTF8ToAnsi(UTF8Encode(Npp.PluginConfigDir)), 24);
154154
WrapFilePath(txtConfigDir);
155155

156156
lblEntities := MakeText('HTML entities file');
@@ -306,7 +306,7 @@ procedure TFrmAbout.FindEntities;
306306
FEntities := Npp.Entities;
307307

308308
if not FileExists(FEntities) then
309-
FEntities := ChangeFilePath(Npp.Entities, TSpecialFolders.DLL);
309+
FEntities := Npp.DefaultEntitiesPath;
310310

311311
txtEntities.Text := UTF8Encode(FEntities);
312312

src/U_Entities.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ function LoadEntities(ANpp: TNppPluginHTMLTag; ASet: string = 'HTML 5'): TString
5656
Result := TStringList(EntityLists.Objects[i]);
5757
end else begin
5858
IniFile := ANpp.Entities;
59-
ErrMsg := WideFormat('%s must be saved to'#13#10'%s', [EntitiesConf, ExtractFileDir(IniFile)]);
59+
ErrMsg := WideFormat('%s must be saved in'#13#10'%s', [ExtractFileName(IniFile), ExtractFileDir(IniFile)]);
6060
if not FileExists(IniFile) then
61-
IniFile := ChangeFilePath(IniFile, TSpecialFolders.DLL);
62-
ErrMsg := Concat(ErrMsg, WideFormat(#13#10'or to'#13#10'%s', [ExtractFileDir(IniFile)]));
61+
IniFile := Npp.DefaultEntitiesPath;
62+
ErrMsg := Concat(ErrMsg, WideFormat(#13#10'or %s in'#13#10'%s', [ExtractFileName(IniFile), TSpecialFolders.DLL]));
6363
if not FileExists(IniFile) then begin
6464
MessageBoxW(ANpp.App.WindowHandle, PWideChar(ErrMsg), PWideChar('Missing Entities File'), MB_ICONERROR);
6565
FreeAndNil(EntityLists);

src/U_Npp_HTMLTag.pas

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ interface
1717
AboutForm,
1818
NppSimpleObjects, L_VersionInfoW;
1919

20-
const
21-
EntitiesConf: nppString = 'HTMLTag-entities.ini';
22-
2320
type
2421
TDecodeCmd = (dcAuto = -1, dcEntity, dcUnicode);
2522
TCmdMenuPosition = (cmpUnicode = 3, cmpEntities);
@@ -37,6 +34,9 @@ TNppPluginHTMLTag = class(TNppPlugin)
3734
FOptions: TPluginOptions;
3835
function GetOptionsFilePath: nppString;
3936
function GetEntitiesFilePath: nppString;
37+
function GetDefaultEntitiesPath: nppString;
38+
function PluginNameFromModule: nppString;
39+
function GetConfigDir: nppString;
4040
procedure LoadOptions;
4141
procedure SaveOptions;
4242
procedure FindAndDecode(const KeyCode: Integer; Cmd: TDecodeCmd = dcAuto);
@@ -63,6 +63,8 @@ TNppPluginHTMLTag = class(TNppPlugin)
6363
property Version: nppString read FVersionStr;
6464
property OptionsConfig: nppString read GetOptionsFilePath;
6565
property Entities: nppString read GetEntitiesFilePath;
66+
property DefaultEntitiesPath: nppString read GetDefaultEntitiesPath;
67+
property PluginConfigDir: nppString read GetConfigDir;
6668
end;
6769

6870
procedure _commandFindMatchingTag(); cdecl;
@@ -85,7 +87,6 @@ procedure _commandAbout(); cdecl;
8587
implementation
8688

8789
uses
88-
Strutils,
8990
ShellAPI,
9091
L_SpecialFolders,
9192
Utf8IniFiles,
@@ -207,11 +208,7 @@ constructor TNppPluginHTMLTag.Create;
207208

208209
try
209210
FVersionInfo := TFileVersionInfo.Create(TSpecialFolders.DLLFullName);
210-
{$IFDEF FPC}
211-
FVersionStr := UTF8ToString(ChangeFileExt(ExtractFileName(UTF8Encode(TSpecialFolders.DLLFullName)), ''));
212-
{$ELSE}
213-
FVersionStr := ChangeFileExt(ExtractFileName(TSpecialFolders.DLLFullName), '');
214-
{$ENDIF}
211+
FVersionStr := PluginNameFromModule();
215212
FVersionStr :=
216213
Concat(FVersionStr,
217214
WideFormat(' %d.%d.%d (%s bit)',
@@ -220,10 +217,6 @@ constructor TNppPluginHTMLTag.Create;
220217
except
221218
FreeAndNil(FVersionInfo);
222219
end;
223-
224-
{$IFNDEF CPUX64}
225-
FVersionStr := UTF8ToString(ReplaceStr(UTF8Encode(FVersionStr), '_unicode', ''));
226-
{$ENDIF}
227220
end;
228221

229222
{ ------------------------------------------------------------------------------------------------ }
@@ -242,7 +235,7 @@ procedure TNppPluginHTMLTag.SetInfo(NppData: TNppData);
242235
begin
243236
inherited SetInfo(NppData);
244237
if not FileExists(Entities) then
245-
CopyFileW(PWChar(ChangeFilePath(Entities, TSpecialFolders.DLL)), PWChar(Entities), True);
238+
CopyFileW(PWChar(DefaultEntitiesPath), PWChar(Entities), True);
246239

247240
LoadOptions;
248241
end;
@@ -433,17 +426,35 @@ procedure TNppPluginHTMLTag.commandAbout;
433426
{ ------------------------------------------------------------------------------------------------ }
434427
function TNppPluginHTMLTag.GetEntitiesFilePath: nppString;
435428
begin
436-
Result := IncludeTrailingPathDelimiter(Self.ConfigDir) + EntitiesConf;
429+
Result := IncludeTrailingPathDelimiter(Self.PluginConfigDir) + 'entities.ini';
437430
end {TNppPluginHTMLTag.GetEntitiesFilePath};
438431

439432
{ ------------------------------------------------------------------------------------------------ }
440433
function TNppPluginHTMLTag.GetOptionsFilePath: nppString;
434+
begin
435+
Result := IncludeTrailingPathDelimiter(Self.PluginConfigDir) + 'options.ini';
436+
end;
437+
438+
{ ------------------------------------------------------------------------------------------------ }
439+
function TNppPluginHTMLTag.GetDefaultEntitiesPath: nppString;
440+
begin
441+
Result := IncludeTrailingPathDelimiter(TSpecialFolders.DLL) + PluginNameFromModule() + '-entities.ini';
442+
end;
443+
444+
{ ------------------------------------------------------------------------------------------------ }
445+
function TNppPluginHTMLTag.PluginNameFromModule: nppString;
441446
var
442447
PluginName: WideString;
443448
begin
444449
PluginName := ChangeFileExt(ExtractFileName(TSpecialFolders.DLLFullName), EmptyWideStr);
445-
PluginName := WideStringReplace(PluginName, '_unicode', EmptyWideStr, []);
446-
Result := IncludeTrailingPathDelimiter(Self.ConfigDir) + PluginName + '.ini';
450+
Result := WideStringReplace(PluginName, '_unicode', EmptyWideStr, []);
451+
end;
452+
453+
{ ------------------------------------------------------------------------------------------------ }
454+
function TNppPluginHTMLTag.GetConfigDir: nppString;
455+
begin
456+
Result := IncludeTrailingPathDelimiter(Self.ConfigDir) + PluginNameFromModule();
457+
if (not DirectoryExists(Result)) then CreateDir(Result);
447458
end;
448459

449460
{ ------------------------------------------------------------------------------------------------ }

0 commit comments

Comments
 (0)