// Compiles TeX Document based on the preferences in the Execution Modes

  Requires(20170103); // 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:: ======================================================

// ==================================================================
// Execute a custom Batch file (%N.bat)?
// ==================================================================

  IfStr('%$(!"Compile-UseBatchFile");',!'1',!'<>',>
    !'JMP(!"not_Batch");');
  IfFileExists('%P\%N.bat',!'Relax;',>
    !'JMP(!"not_Batch");');
  // Change !'1' to !'?' if you always want Windows Console!
  IfStr("%$(!'PDFTeXify-WinEdt_Console');",!'1',!'<>',>
    !'JMP(!"Windows_Console");');
  RunConsole('%P\%N.bat "%N%T" "%N" "%T" "%P"','%P','%N.bat ...');
  Exit;

:Windows_Console:: ==================================================
  Run('%P\%N.bat "%N%T" "%N" "%T" "%P"','%P','%N.bat ...');
  Exit;

:not_Batch:: ========================================================

  PushTagsandRegisters;
  Assign('Compile-Flag','');
  SetOK(1);  // Just in case ...
  SetErrorFlag(0);

  Assign('OLD-PDFLATEX','%@("PDFLATEX");');
  Assign('OLD-LATEX','%@("LATEX");');
  Assign('Old-BibTeX-Executable','%$("BibTeX-Executable");');
  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("External-Call");
  Assign("Pre-Process");
  Assign("Post-Process");
  IfStr("%$('Force-Focus');","1","<>",!|FocusConsole(-1);|);
  SetEnvVar('PDFLATEX','%$("OLD-PDFLATEX");');
  Release('OLD-PDFLATEX');
  SetEnvVar('LATEX','%$("OLD-LATEX");');
  Release('OLD-LATEX');
  Assign('BibTeX-Executable','%$("Old-BibTeX-Executable");');
  Release('Old-BibTeX-Executable');
  PopTagsandRegisters;
  IfStr("%$('CS-Output');",!"2",!"=",>
    !'Exe("%b\Exec\%$(|Exec-Dir|);\ExeMode.edt");');
  SetExitHandler;
  StopWorking;
  UpdateStatus;
Return;

:Start:: ============================================================

// ==================================================================
// Determine the compiler from comments in the preamble?
// ==================================================================

  IfStr('%$(!"Compile-UsePragmaComments");',!'1',!'<>',>
    !'JMP(!"Default_Compiler");');
  // If the preamble contains comments like:
  // % !TEX program = lualatex
  // % !BIB program = biber
  // then WinEdt will execute the corresponding programs...
  GetPreamble("\begin{document}",0);
  IfOK(!'Relax;',!'JMP(!"Default_Compiler");');
  FindInString("%!0",!'%@{ }!TEX @{ }program@{ }=@{ }\(*\)@{ }>',-1,-1,1);
  IfOK(!'LetReg(8,"%#0");',!'JMP(!"Default_Compiler");');
  // Admissible values for !TEX program are pdflatex, xelatex, lualatex, latex
  FindInString("%!8",!'\#~{?}{pdflatex|xelatex|lualatex|latex}~{?}',-1,-1,1);
  IfOK(!'Relax;',!'JMP(!"Default_Compiler");');
  FindInString("%!0",!'%@{ }!BIB @{ }program@{ }=@{ }\(*\)@{ }>',-1,-1,1);
  IfOK(!'LetReg(9,"%#0");',!'LetReg(9);');
  // Admissible values for !BIB program are none, default, bibtex, bibtex8, biber
  IfIStr('%!9',!'default',!'=',>
    !'LetReg(9,"%$(!`BibTeX-Executable`);");GetFileName("%!9",9);');
  FindInString("%!9",!'\#~{?}{bibtex8|bibtex|biber}~{?}',-1,-1,1);
  IfOK(!'FormatString("%!9",9,!"lowercase");',!'LetReg(9);');

  // Now WinEdt will determine the right compiler...
  IfIStr('%!8',!'latex',!'=',!'IfStr("%!9",!"",!"=",>
    !|Exe("%b\Exec\TeX\LaTeX.edt");JMP(!"Cleanup");|);');
  IfIStr('%!8',!'latex',!'=',>
    !'SetEnvVar(!"LATEX",!"latex.exe");>
      Assign(!"BibTeX-Executable","%!9.exe");JMP(!"DVI");');
  IfIStr('%!8',!'pdflatex',!'=',!'IfStr("%!9",!"",!"=",>
    !|Exe("%b\Exec\TeX\PDFLaTeX.edt");JMP(!"Cleanup");|);');
  IfIStr('%!8',!'pdflatex',!'=',>
    !'SetEnvVar(!"PDFLATEX",!"pdflatex.exe");>
      Assign(!"BibTeX-Executable","%!9.exe");JMP(!"PDF");');
  IfIStr('%!8',!'xelatex',!'=',!'IfStr("%!9",!"",!"=",>
    !|Exe("%b\Exec\TeX\XeLaTeX.edt");JMP(!"Cleanup");|);');
  IfIStr('%!8',!'xelatex',!'=',>
    !'SetEnvVar(!"PDFLATEX",!"xelatex.exe");>
      Assign(!"BibTeX-Executable","%!9.exe");JMP(!"PDF");');
  IfIStr('%!8',!'lualatex',!'=',!'IfStr("%!9",!"",!"=",>
    !|Exe("%b\Exec\TeX\LuaLaTeX.edt");JMP(!"Cleanup");|);');
  IfIStr('%!8',!'lualatex',!'=',>
    !'SetEnvVar(!"PDFLATEX",!"lualatex.exe");>
      Assign(!"BibTeX-Executable","%!9.exe");JMP(!"PDF");');

:Default_Compiler:: =================================================

// ==================================================================
// Determine the compiler from the drop-down TeX Compile button?
// ==================================================================

  IfStr('%$(!"Compile-UseSelectedTeXEngine");',!'1',!'=',>
    !'Relax;',!'JMP(!"TeX_Engine_OK");');

  // Execute [PDF]TeXify for the selected [PDF]LaTeX engine?
  // Not in the defaults (comment out the jump if that is what you want):
  // Compile Selected: do not use [PDF]TeXify!?
  IfStr("%$(!|Main_File|);",!"",!"<>",!"Relax;",!"JMP(!'not_TeXify');");
  // Compile: do not use [PDF]TeXify!?
  IfStr("%$(!|Main_File|);",!"",!"=",!"Relax;",!"JMP(!'not_TeXify');");
  // ... or else use [PDF]TeXify for the selected [PDF]LaTeX engine!
  BtnGetMenuItem(!"TeX Compile",-1);
  IfStr("%-1",!"PDFLaTeX",!"=",>
    !"SetEnvVar(!'PDFLATEX','%$(!|PDFLaTeX-Executable|);');JMP(!'PDF');");
  IfStr("%-1",!"LuaLaTeX",!"=",>
    !"SetEnvVar(!'PDFLATEX','%$(!|LuaLaTeX-Executable|);');JMP(!'PDF');");
  IfStr("%-1",!"XeLaTeX",!"=",>
    !"SetEnvVar(!'PDFLATEX','%$(!|XeLaTeX-Executable|);');JMP(!'PDF');");
  IfStr("%-1",!"LaTeX",!"=",>
    !"SetEnvVar(!'LATEX','%$(!|LaTeX-Executable|);');JMP(!'DVI');");

:not_TeXify:: =======================================================
  // Otherwise just simulate a click on the selected button:
  BtnClick(!"TeX Compile");
  JMP(!"Cleanup");

:TeX_Engine_OK:: ====================================================

// ==================================================================
// Default TeX compiler and method as defined in the Execution Modes
// ==================================================================

  Assign('Compile-Flag','');
  SetOK(1);  // Just in case ...
  SetErrorFlag(0);

  IfStr("%$('CS-Output');","1","=",!|JMP(!'DVI');|);
  IfStr("%$('CS-Output');","2","=",!|JMP(!'PS');|);

:PDF:: ==============================================================
  Exe('%b\Exec\TeX\PDFTeXify.edt');
  JMP(!'Cleanup');

:DVI:: ==============================================================
  Exe('%b\Exec\TeX\TeXify.edt');
  JMP(!'Cleanup');

:PS:: ===============================================================
  // TeXify
  IfFileExists("%P\%N.ps",!"DeleteFile('%P\%N.ps')");
  IfFileExists("%O\%N.ps",!"DeleteFile('%O\%N.ps')");
  Assign("_TeXify-Wait","%$('TeXify-Wait');");
  Assign("TeXify-Wait","1");
  Assign("_TeXify-Viewer","%$('TeXify-Viewer');");
  Assign("TeXify-Viewer");
  Assign("_TeXify-Pause","%$('TeXify-Pause');");
  IfStr("%$('TeXify-WinEdt_Console');","1","=",!|Assign("TeXify-Pause","1");|);
  Assign("External-Call","1");
  Assign("Pre-Process","TeXify");
  Exe('%b\Exec\TeX\TeXify.edt');
  Assign("Pre-Process");
  Assign("External-Call");
  Assign("TeXify-Wait","%$('_TeXify-Wait');");
  Assign("_TeXify-Wait");
  Assign("TeXify-Viewer","%$('_TeXify-Viewer');");
  Assign("_TeXify-Viewer");
  Assign("TeXify-Pause","%$('_TeXify-Pause');");
  Assign("_TeXify-Pause");
  Assign("PSTeXify-WinEdt_Console","%$('TeXify-WinEdt_Console');");
  IfStr("%$('Compile Status');",!"OK",!"<>",>
    !"IfStr(`%$('Compile Status');`,`Run Error`,`<>`,>
        !`IfStr(|%$('TeXify-Report');|,|1|,|=|,>
            !|LetReg(9,'PSTeXify');Exe('%b\Exec\ShowReport.edt');|);`);>
      Exit;");
  IfFileExists("%P\%N.dvi",!"Relax;",!"Exit;");

  // dvi2ps
  Assign('Compile-Flag','1');
  Assign("dvi2ps-GUI");
  Assign("dvi2ps-High_Priority","%$('TeXify-High_Priority');");
  Assign("dvi2ps-Wait","%$('TeXify-Wait');");
  Assign("dvi2ps-ExitCode","%$('TeXify-ExitCode');");
  Assign("dvi2ps-Viewer","%$('TeXify-Viewer');");
  Assign("dvi2ps-View_Search","%$('TeXify-View_Search');");
  Assign("dvi2ps-STD_IO","%$('TeXify-STD_IO');");
  Assign("dvi2ps-Clean_Build","1");
  Assign("dvi2ps-WinEdt_Console","%$('TeXify-WinEdt_Console');");
  Assign("dvi2ps-Hidden_Window","%$('TeXify-Hidden_Window');");
  Assign("dvi2ps-Background_Window","%$('TeXify-Background_Window');");
  Assign("dvi2ps-Pause","%$('TeXify-Pause');");
  Assign("dvi2ps-Report","%$('TeXify-Report');");
  Assign("PSTeXify-WinEdt_Console","%$('TeXify-WinEdt_Console');");
  Assign("Post-Process","PSTeXify");
  Exe('%b\Exec\TeX\dvi2ps.edt');
  Assign("Post-Process");
Exit;

:Cleanup:: ==========================================================
  IfStr("%$('Force-Focus');","1","<>",!|FocusConsole(-1);|);
  SetExitHandler;
  PopTagsandRegisters;
  Assign('Compile-Flag','');
  SetEnvVar('PDFLATEX','%$("OLD-PDFLATEX");');
  Release('OLD-PDFLATEX');
  SetEnvVar('LATEX','%$("OLD-LATEX");');
  Release('OLD-LATEX');
  Assign('BibTeX-Executable','%$("Old-BibTeX-Executable");');
  Release('Old-BibTeX-Executable');
  StopWorking;
  UpdateStatus;

End;
