// -*- ASCII:EDT -*-
// TeX: TeXify ...

  Requires(20120413); // Requires this build of WinEdt to work properly

  IfConsoleRunning("Relax;","JMP('not_Running');");
  ShowConsole(1);
  Prompt("Another Application is already running in WinEdt's Console.>
 Terminate the application or wait for the execution to finish!");
  FocusConsole(1);
  Exit;

:not_Running:: ======================================================
  PushTagsandRegisters;
  SetExitHandler('Exit_Handler');
  JMP('Start');

:Exit_Handler:: =====================================================
// Restore the Execution Modes even if execution
// is canceled or goes badly wrong for some reason ...
  Assign('Compile-Flag'); // Not inside a (multiple conversion) compile...
  Assign("Pre-Process");
  IfStr("%$('Force-Focus');","1","<>",>
    !|IfStr("%$('External-Call');","1","<>",!"FocusConsole(-1);");|);
  IfStr("%$('External-Call');","1","<>",!"Exe('%b\Exec\%$(|Exec-Dir|);\ExeMode.edt');");
  PopTagsandRegisters;
  SetExitHandler;
Return;

:Start:: ============================================================
  Assign('ExitCode-OK','0'); // OK Exit Code
  LetReg(9,'TeXify');   // Execution Modes Compiler
  LetReg(8,'log');      // Log File (or empty)
  LetReg(7,'dvi');      // Output Format
  LetReg(6,'%O');       // Startup Folder

  // MiKTeX's texify.exe?
  IfIStr("%$('%!9-Executable');",!"texify.exe",!"=",!"Assign(!'TeX-Opt',!'--tex-option=');",!"Assign(!'TeX-Opt');");

  // Display a Dialog for Extra Switches:
  IfStr("%$('%!9-GUI');","1","=",>
    !|GetString("Switches: ","%!9 Switches","%$('%!9--Switches');");>
      IfOK(!`Assign('%!9--Switches','%!?');`,!`PopTagsandRegisters;Exit;`);|,>
    !|Release('%!9--Switches');|);
  LetReg(5,@@"%$('%!9-Switches');");
  IfIStr(@@"%$('%!9-Executable');","dvilualatex.exe","=",>
    !"SubstituteInString('%!5','--src','',10,5);Assign('%!9-Switches','%!5');");
  LetReg(3, "%$('%!9-Executable'); %$('%!9-Switches');");
  IfIStr("%$('%!9-Executable');",!"texify.exe",!"<>",!"JMP('no_Batch');");
  IfStr("%$('%!9-No_Interaction');","1",!"=",!|LetReg(3,'%!3 --batch');|);
  SetEnvVar("BIBTEX","%$('BibTeX-Executable');");
  SetEnvVar("MAKEINDEX","%$('MakeIndex-Executable');");
:no_Batch:: =========================================================
  // Non-Stop Mode?
  IfStr("%$('%!9-No_Interaction');","1",!"=",>
    !|LetReg(4,'%$("TeX-Opt");--interaction=nonstopmode');|,>
    !|LetReg(4,'%$("TeX-Opt");--interaction=errorstopmode');|);
  // C Style Errors?
  IfStr("%$('%!9-C-style-errors');","1",!"=",!|LetReg(4,'%!4 %$("TeX-Opt");--file-line-error');|);
  // TeX Sync?
  Assign('Sync');
  IfStr('%0',!'--src',!'<>',!'JMP("no_Sync");');
  IfStr('%$("PDF-SyncTeX");',!'1',!'<>',!'JMP("no_Sync");');
  IfStr('%$("PDF-Sync");',!'',!'=',!'JMP("no_Sync");');
  Assign('Sync','%$("TeX-Opt");%$("PDF-Sync");');
:no_Sync:: ==========================================================
  // Build the Command Line ...
  LetReg(5, "%!3 %!4 %$('Sync'); %$('%!9--Switches'); %$('%!9-Parameters');");

:Execute:: ==========================================================
  // Clean the files
  IfFileExists("%O\%N.blg",!"DeleteFile('%O\%N.blg');");
  IfFileExists("%O\%N.ilg",!"DeleteFile('%O\%N.ilg');");
  IfFileExists("%O\%N.glg",!"DeleteFile('%O\%N.glg');");
  IfStr("%$('%!9-Clean_Build');","1","=",>
    !|IfFileExists("%O\%N.bbl",!"DeleteFile('%O\%N.bbl');");>
      IfFileExists("%O\%N.ind",!"DeleteFile('%O\%N.ind');");>
      IfFileExists("%O\%N.idx",!"DeleteFile('%O\%N.idx');");>
      IfFileExists("%O\%N.gls",!"DeleteFile('%O\%N.gls');");>
      IfFileExists("%O\%N.glo",!"DeleteFile('%O\%N.glo');");>
      IfFileExists("%O\%N.toc",!"DeleteFile('%O\%N.toc');");>
      IfFileExists("%O\%N.lof",!"DeleteFile('%O\%N.lof');");>
      IfFileExists("%O\%N.lot",!"DeleteFile('%O\%N.lot');");>
      IfFileExists("%O\%N.aux",!"DeleteFile('%O\%N.aux');");>
      IfFileExists("%O\%N.out",!"DeleteFile('%O\%N.out');");>
     |);
  UpdateStatus;

  IfStr("%$('%!9-WinEdt_Console');",'1','<>',!'JMP(!"Windows_Console");');

:WinEdt_Console:: ===================================================
  // Running from TeXify / Compile Macro?
  IfStr("%$('Compile-Flag');",'','=',>
   !|>
      IfStr("%$('%!9-Hidden_Window');",'1','=',!"ResetConsole;",!"ResetConsole;ShowConsole(1);");>
      IfStr("%$('%!9-Background_Window');",'1','=',!"FocusConsole(-1);",!"FocusConsole(1);");>
    |);
  JMP('Console_OK');

:Windows_Console:: ==================================================
  IfStr("%$('Compile-Flag');",'','=',>
    !|IfStr("%$('%!9-STD_IO');","1","=",!"ResetConsole;");|);

:Console_OK:: =======================================================
  Assign('Report-Shown','');
  Assign('Force-Focus','');
  Assign("TeXify-Iterations","%$('Max-TeXify-Iterations');");

:TeXify_Iterate:: ===================================================
  LetReg(3,"%$('TeXify-Iterations');");
  IfStr("%$('Pre-Process');","","=",!"Assign('Pre-Process','TeXify');");
  IfNum("%$(!'Max-TeXify-Iterations');",!"0",!">",>
    !|Exe('%b\Exec\TeX\BibTeXify.edt');|);
  Assign("Pre-Process");
  // Use Execution Modes to determine the flow of execution...
  Assign(!"Compile-Flag",!"3");
  Exe('%b\Exec\ExeCompiler.edt');
  IfStr("%$('Compile Status');",!"OK",!"<>",>
    !"IfStr(`%$('Compile Status');`,`Output`,`<>`,!`JMP('TeXify_Done');`);");
  LetReg(3,"%$('TeXify-Iterations');");
  LetRegNum(3,"%!3-1");
  Assign("TeXify-Iterations","%!3");
  IfNum("%!3",!"0",!">",!"JMP('TeXify_Iterate');");

:TeXify_Done:: ======================================================
  IfStr("%$('Report-Shown');",!"1",!"<>",>
    !"IfStr(`%$('External-Call');`,`1`,`<>`,>
        !`IfStr(|%$('Compile Status');|,|Run Error|,|<>|,>
            !|IfStr(*%$('%!9-Report');*,*1*,*=*,!*Exe('%b\Exec\ShowReport.edt');*);|);`);");
  IfStr("%$('Force-Focus');","1","<>",>
    !|IfStr("%$('External-Call');","1","<>",!"FocusConsole(-1);");|);
  SetExitHandler;
  PopTagsandRegisters;
  // Restore the execution modes ...
  IfStr("%$('External-Call');","1","<>",!"Exe('%b\Exec\%$(|Exec-Dir|);\ExeMode.edt');");
  Assign('Compile-Flag','');

End;
