unit RANotepad;

uses Global;

var
  ModulesReg: TJvRegAuto;
  Ss: TStringList;
  FileOpenedFuncList: TStringList;
  FileClosedFuncList: TStringList;

{ Function Main called by Editor at startup }
procedure Main;
var
  FN: string;
  i: integer;
begin
  ODS('main.InitGlobal');
  InitGlobal;
  Editor.GutterWidth := 16;
  ModulesReg := TJvRegAuto.Create(MainWindow);
  ModulesReg.IniFile := WorkingDir + 'modules.ini';
  ModulesReg.UseReg := False;
  ModulesReg.UseIni := True;
  Ss := TStringList.Create;

 { load plug-in units }
  ModulesReg.ReadWholeSection('Modules', Ss);
  ODS(Ss.Text);
  DeleteEmptyLines(Ss);
  for i := 0 to Ss.Count - 1 do
    UseUnit(Ss[i]);

 { init plug-in units }
  ModulesReg.ReadWholeSection('Init', Ss);
  DeleteEmptyLines(Ss);
  for i := 0 to Ss.Count - 1 do
    Call(Ss[i]);

  FileOpenedFuncList := TStringList.Create;
  ModulesReg.ReadWholeSection('FileOpened', FileOpenedFuncList);
  DeleteEmptyLines(FileOpenedFuncList);
  FileClosedFuncList := TStringList.Create;
  ModulesReg.ReadWholeSection('FileClosed', FileClosedFuncList);
  DeleteEmptyLines(FileClosedFuncList);

  InitTest;
end;

{ Function Done called by Editor at shutdown }
procedure Done;
var
  i: integer;
begin
 { init plug-in units }
  ODS('Done');
  ModulesReg.ReadSection('Done', Ss);
  DeleteEmptyLines(Ss);
  for i := 0 to Ss.Count - 1 do
    Call(Ss.Strings[i]);

  Ss.Free;
  FileOpenedFuncList.Free;
  FileClosedFuncList.Free;
end;

{ Function FileOpened called by Editor }
procedure FileOpened(FileName: string);
var
  i: integer;
begin
  ODS(Format('File %s is opened !', [FileName]));
  for i := 0 to FileOpenedFuncList.Count - 1 do
    Call(FileOpenedFuncList.Strings[i], FileName);
end;

{ Function FileClosed called by Editor }
procedure FileClosed(FileName: string);
var
  i: integer;
begin
  ODS(Format('File %s is closed !', [FileName]));
  for i := 0 to FileClosedFuncList.Count - 1 do
    Call(FileClosedFuncList.Strings[i], FileName);
end;


{************* Keyboard hooks *************}
{ Function KeyDown called by Editor on each KeyDown event }
procedure KeyDown(var Key: Integer; Shift: TShiftState);
var
  i: Integer;
begin
  { Alt+Z - zoom edit window }
  if (Key = ord('Z')) and (Shift = [ssAlt]) then
  begin
    ODS('Alt + Z');
    if MainWindow.WindowState = wsNormal then
      MainWindow.WindowState := wsMaximized
    else
      MainWindow.WindowState := wsNormal;
    Key := 0;
  end;
end;

{ Function KeyPress called by Editor on each KeyPress event }
procedure KeyPress(var Key: string);
begin
{  if Key = '~' then
  begin
    ShowMessage('Console Called !');
    Key := '';
  end; }
end;


{********** Test section for debug new functions ***********}
procedure InitTest;
var
  Test: TMenuItem;
begin
{  Test := NewItem('Test', 0, False, True, 0, nil, 'miTest');
  MainWindow.Menu.Items.Insert(MainWindow.Menu.Items.Count - 1, Test);
  Test.OnClick := TestClick; }
end;

procedure TestClick(Sender: TObject);
begin
  ShowMessage('Going to end of document');
  Editor.Command(ecEndDoc);
end;

end.
