/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
/*                                                                     */
/*        P�l Hedne 1990                                               */
/*                                                                     */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
extern latex_help;
extern MathKeys, NonMathKeys, DefMathKeys;
extern TexKeys, NormKeys;
extern math_key,tabbing_key,tabular_key,thebib_key;
extern keyboard;
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
_init (...)
{
       assign_to_key ("<Ctrl-F1>", "latex");
  message ("Ctrl-F1 aktiverer Latex macro Ver.PH/1.2");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
latex (...)
{
       int ikey, on_off, NorModus, MathModus;

       string command;

       global NorModus, on_off, MathModus;

       if (!on_off)
               {
               on_off = 1;

/*              if (!inq_macro("latex"))*/
     load_macro ("latex");
/*              error ("Latex loaded.");*/
/*      if (!inq_macro("embrace"))*/
     load_macro ("embrace");
/*              error ("Embrace loaded.");*/
/*      if (!inq_macro("latexmat"))*/
     load_macro ("latexmat");
/*              error ("Latexmat loaded.");*/
/*      if (!inq_macro("gresk"))*/
     load_macro ("gresk");
/*              error ("Gresk loaded.");*/

     autoload ("label","find_label");
/*              error ("Flabel loaded.");*/
     autoload ("gmatch","find_match");
/*              error ("Gmatch loaded.");*/
     autoload ("latexh","latex_help","gen_help");
/*              error ("Latexh loaded.");*/
               autoload ("norsk","nor_to_tex","tex_to_nor","NormKeys","TexKeys");
/*              error ("Norsk loaded.");*/
     autoload ("remmer","split_tex","join_tex");
/*              error ("Remmer loaded.");*/

               assign_to_key ("<Ctrl-F2>", "ToggleNor");
               assign_to_key ("<Ctrl-F3>", "nor_to_tex");
               assign_to_key ("<Shift-F3>", "tex_to_nor");
               assign_to_key ("<Ctrl-F4>", "find_match");
               assign_to_key ("<Shift-F4>", "disp_match");
               assign_to_key ("<Ctrl-F7>", "ToggleMath");
               assign_to_key ("<Ctrl-F8>", "find_label");
               assign_to_key ("<Shift-F8>", "disp_label");
               assign_to_key ("<Ctrl-F10>", "split_tex");
               assign_to_key ("<Shift-F10>", "join_tex");
               assign_to_key ("$", "dollar");

               assign_to_key ("<Ctrl-a>", "array");

               assign_to_key ("<Ctrl-b><a>", "begin_array");
               assign_to_key ("<Ctrl-b><b>", "begin_thebibliography");
               assign_to_key ("<Ctrl-b><d>", "begin_displaymath");
               assign_to_key ("<Ctrl-b><f>", "begin_figure");
               assign_to_key ("<Ctrl-b><i><i>", "begin_itemize");
               assign_to_key ("<Ctrl-b><i><e>", "begin_enumerate");
               assign_to_key ("<Ctrl-b><i><d>", "begin_description");
               assign_to_key ("<Ctrl-b><m>", "begin_math");
               assign_to_key ("<Ctrl-b><n>", "begin_tabbing");
               assign_to_key ("<Ctrl-b><p>", "begin_minipage");
               assign_to_key ("<Ctrl-b><q>", "begin_equation");
               assign_to_key ("<Ctrl-b><t>", "begin_tabular");
               assign_to_key ("<Ctrl-b><w>", "begin_document");
               assign_to_key ("<Ctrl-b><y>", "begin_eqnarray");

               assign_to_key ("<Ctrl-c>", "cite");
               assign_to_key ("<Ctrl-d>", "displaymath");

               assign_to_key ("<Ctrl-e><a>", "end_array");
               assign_to_key ("<Ctrl-e><b>", "end_thebibliography");
               assign_to_key ("<Ctrl-e><d>", "end_displaymath");
               assign_to_key ("<Ctrl-e><f>", "end_figure");
               assign_to_key ("<Ctrl-e><i><i>", "end_itemize");
               assign_to_key ("<Ctrl-e><i><e>", "end_enumerate");
               assign_to_key ("<Ctrl-e><i><d>", "end_description");
               assign_to_key ("<Ctrl-e><n>", "end_tabbing");
               assign_to_key ("<Ctrl-e><m>", "end_math");
               assign_to_key ("<Ctrl-e><p>", "end_minipage");
               assign_to_key ("<Ctrl-e><q>", "end_equation");
               assign_to_key ("<Ctrl-e><t>", "end_tabular");
               assign_to_key ("<Ctrl-e><w>", "end_document");
               assign_to_key ("<Ctrl-e><y>", "end_eqnarray");

               assign_to_key ("<Ctrl-f>", "figure");
               assign_to_key ("<Ctrl-g>", "grad");
               assign_to_key ("<Ctrl-h>", "latex_help");
               assign_to_key ("<Ctrl-i><i>", "itemize");
               assign_to_key ("<Ctrl-i><e>", "enumerate");
               assign_to_key ("<Ctrl-i><d>", "description");
               assign_to_key ("<Ctrl-i><d>", "description");
               assign_to_key ("<Ctrl-j>", "footnote");
               assign_to_key ("<Ctrl-k>", "caption");
               assign_to_key ("<Ctrl-l>", "label");
               assign_to_key ("<Ctrl-m>", "math");
               assign_to_key ("<Ctrl-n>", "tabbing");
               assign_to_key ("<Ctrl-o>", "table");
/*    ctrl-p = pop_up_error !! */
               assign_to_key ("<Ctrl-q>", "equation");
               assign_to_key ("<Ctrl-r>", "ref");
               assign_to_key ("<Ctrl-s><c>", "chapter");
               assign_to_key ("<Ctrl-s><s>", "section");
               assign_to_key ("<Ctrl-s><u>", "subsection");
               assign_to_key ("<Ctrl-t>", "tabular");
               assign_to_key ("<Ctrl-u><b>", "bold");
               assign_to_key ("<Ctrl-u><c>", "caps");
               assign_to_key ("<Ctrl-u><e>", "emphasize");
               assign_to_key ("<Ctrl-u><i>", "italic");
               assign_to_key ("<Ctrl-u><o>", "overline");
               assign_to_key ("<Ctrl-u><r>", "roman");
               assign_to_key ("<Ctrl-u><s>", "sansserif");
               assign_to_key ("<Ctrl-u><t>", "typewriter");
               assign_to_key ("<Ctrl-u><u>", "underline");
               assign_to_key ("<Ctrl-v>", "minipage");
               assign_to_key ("<Ctrl-w>", "document");
               assign_to_key ("<Ctrl-x>", "mbox");
               assign_to_key ("<Ctrl-y>", "eqnarray");

               NorModus = 0;
               MathModus = 0;
               assign_to_key ("<Enter>", "r_indent");
               message ("Latex template installed.Press Ctrl-h for help.");
               }
       else
               {
               on_off = 0;
               if (inq_macro("latex"))
        {delete_macro ("latex");
/*          error ("Latex removed.");*/
        }
               if (inq_macro("embrace"))
        {delete_macro ("embrace");
/*          error ("Embrace removed.");*/
        }
       if (inq_macro("latexmat"))
        {delete_macro ("latexmat");
/*          error ("Latexmat removed.");*/
        }
       if (inq_macro("gresk"))
        {delete_macro ("gresk");
/*          error ("Gresk removed.");*/
        }

     if (inq_macro("label"))
        {delete_macro ("label");
/*          error ("Label removed.");*/
        }
     if (inq_macro("gmatch"))
        {delete_macro ("gmatch");
/*          error ("Gmatch removed.");*/
        }
     if (inq_macro("latexh"))
        {delete_macro ("latexh");
/*          error ("Latexh removed.");*/
        }
     if (inq_macro("norsk"))
        {delete_macro ("norsk");
/*          error ("Norsk removed.");*/
        }
     if (inq_macro("remmer"))
        {delete_macro ("remmer");
/*          error ("Remmer removed.");*/
        }
               message ("Latex template removed.");
     keyboard();
               }
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                               EMBRACE                                                                                                         ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
embrace (...)
{
       int line, col, mtype, sw;

       int mline1, mcol1, mlinex, mcolx;

       string command;

       get_parm (0, command);
       get_parm (1, sw);
       inq_position (line, col);
       command = lower (command);
       if (sw == -1)
       command = "{" + ("\\" + (command + " "));
       else if (sw == 1)
               command = command + "{";
       else
               command = "\\" + (command + "{");
       if (command == "\\cite{")
               command = "~" + command;
       if (command == "\\ref{")
               command = "~" + command;
       mtype = inq_marked (mline1, mcol1, mlinex, mcolx);
       if (mtype > 0)
               {
               move_abs (mline1, mcol1);
               insert (command);
               move_abs (mline1, mcol1);
               swap_anchor ();
               move_abs (mlinex, mcolx + 1);
               if (mline1 == line)
                       move_abs (mlinex, (mcolx + strlen (command)) + 1);
               insert ("}");
               inq_position (line, col);
               move_abs (line, col - 1);
               }
       else
               {
               command = command + "}";
               insert (command);
               move_rel (0, -1);
               }
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
/*                                                                                                                                                                                                                                               */
/*                                                        PUT COMMAND                                                                                                                                            */
/*                                                                                                                                                                                                                                               */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
begin_command (...)
{
       int line, col, ins;

       string command;

       if (get_parm (0, command))
               {
               ins = insert_mode (1);
               inq_position (line, col);
               move_rel (0, 1 - col);
               insert ("\\begin{" + (command + "}\n"));
               move_abs (line + 1, col + 4);
               insert_mode (ins);
               }
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
/*                                                                                                                                                                               ;       */
/*                                                        PUT COMMAND                                                                                    ;       */
/*                                                                                                                                                                               ;       */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
end_command (...)
{
       int line, col, ins;

       string command;

       if (get_parm (0, command))
               {
               ins = insert_mode (1);
               inq_position (line, col);
               move_rel (0, 1 - col);
               insert ("\\end{" + (command + "}\n"));
               move_abs (line + 1, col);
               insert_mode (ins);
               }
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/*                                                                                                                                                                               ;       */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
put_commands (...)
{
       int i, line, col, mtype;

       int j, ins;

       int mline1, mcol1, mlinex, mcolx;

       string text, CR, command, ins_text;

       get_parm (0, command);
       command = lower (command);
       ins_text = "    ";
       if (command == "itemize")
               ins_text = "  \\item ";
       if (command == "enumerate")
               ins_text = "  \\item ";
       sprintf (CR, "%c", 13);
       inq_position (line, col);
       mtype = inq_marked (mline1, mcol1, mlinex, mcolx);
       ins = insert_mode (1);
       if (mtype > 0)
               {
               move_abs (mline1, mcol1);
               insert ("\\begin{" + (command + "}\n"));
               move_abs (mline1, mcol1);
               swap_anchor ();
               i = mline1;
               while (i < mlinex)
                       {
                       ++i;
                       move_abs (i, mcol1);
                       if (i == mline1 + 1 && mcol1 > 1)
                               {
                               j = 1;
                               move_abs (i, 1);
                               while (j < mcol1)
                                       {
                                       insert (" ");
                                       ++j;
                                       }
                               }
                       insert (ins_text);
                       }
               move_abs (mlinex + 1, mcol1);
               text = read ();
               if (text != CR)
                       {
                       move_abs (mlinex + 1, mcol1);
                       insert (ins_text);
                       move_abs (mlinex + 2, 1);
                       if (mcol1 > 1)
                               {
                               j = 1;
                               while (j <= mcol1)
                                       {
                                       insert (" ");
                                       ++j;
                                       }
                               }
                       }
               else
                       move_abs (mlinex + 1, mcol1);
               insert ("\\end{" + (command + "}\n"));
               move_abs (line + 2, 1);
               end_of_line ();
               }
       else
               {
               insert ("\\begin{" + (command + "}\n\n"));
               move_rel (0, col-1);
               insert ("\\end{" + (command + "}"));
               move_abs (line + 1, col + 4);
               }
       insert_mode (ins);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                                     */
ToggleNor (...)
{
       if (NorModus == 1)
               {
               message ("Norsk tegnsett p�");
               NormKeys ();
               NorModus = 0;
               }
       else
               {
               message ("Norsk tegnsett av");
               TexKeys ();
               NorModus = 1;
               }
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
ToggleMath (...)
{
       int ipar;

       if (get_parm (0, ipar))
          MathModus=ipar;
       else
        {
        if (MathModus == 0)
           MathModus=1;
        else
           MathModus=0;
        }
       if (MathModus == 0)
        {
        NonMathKeys ();
        message ("Math modus off");
        }
       else
        {
        MathKeys ();
        message ("Math modus on");
        }
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;          */
dollar (...)
{
       insert("$");
       ToggleMath();
}