/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
/*                                                                     */
/*        P�l Hedne 1990                                               */
/*                                                                     */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
/* #include "norsk.cb"*/
extern begin_command;
extern end_command;
extern put_commands;
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
_init (...)
{
	int tabular_key,tabbing_key,thebib_key;
	global tabular_key,tabbing_key,thebib_key;

	tabular_key=0;
   tabbing_key=0;
   thebib_key=0;
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 									  DOCUMENT										 ; 	 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_document (...)
{
	int line, col, ins;

	inq_position (line, col);
	ins = insert_mode (1);
	move_abs (1, 1);
	insert ("\\documentstyle [12pt,a4,block] {article}" + "\n");
	insert ("\\begin{document}" + "\n");
	insert ("\\newcommand {\\grad} {$^{\\circ}$\\ }" + "\n");
	move_abs (line + 2, col);
	insert_mode (ins);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_document (...)
{
	int line, col;

	inq_position (line, col);
	end_of_buffer ();
	insert ("\n\\end{document}");
	move_abs (line, col);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
document (...)
{
	begin_document ();
	end_document ();
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
kill (...)
{
	insert ("\\kill");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
grad (...)
{
	insert ("\\grad ");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
hline (...)
{
	insert ("\\hline");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
tab_and (...)
{
	insert (" & ");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
tab_gt (...)
{
	insert (" \\> ");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
tab (...)
{
	insert ("\t");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_enter (...)
{
	insert (" \\\\");
	push_back (key_to_int ("<Enter>"));
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 							  TABULAR												 ; 	 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
def_tabular (...)
{
	keyboard_push ();
	assign_to_key ("<Tab>", "tab_and");
	assign_to_key ("<Ctrl-Enter>", "end_enter");
	assign_to_key ("<Ctrl-m>", "multicolumn");
	assign_to_key ("<Ctrl-n>", "hline");
	tabular_key = inq_keyboard ();
	keyboard_pop (1);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_tabular (...)
{
	begin_command ("tabular");
   if (tabular_key == 0) def_tabular ();
	use_local_keyboard (tabular_key);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_tabular (...)
{
	end_command ("tabular");
	use_local_keyboard (0);
   check_tab("tabular");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
tabular (...)
{
	put_commands ("tabular");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
table (...)
{
   int line,col,match;

	put_commands ("table");
	inq_position (line, col);
	match=search_back("\\begin{table}",0,0,0,1);
   if (match>0)
      {
      end_of_line(); 
      insert("[htbp]");
      move_abs(line,col);
      }
   insert ("\\centering\n");
   move_rel(0,col);
   insert("\\caption{}\n");
   move_rel(0,col+1);
	put_commands ("center");
	put_commands ("minipage");
	inq_position (line, col);
	match=search_back("\\begin{minipage}",0,0,0,1);
   if (match>0)
      {
      end_of_line();
      insert("{1.0\\textwidth}");
      move_abs(line,col);
      }
	put_commands ("tabular");
}

/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 							  TABBING												 ; 	 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
def_tabbing (...)
{
	keyboard_push ();
	assign_to_key ("<Tab>", "tab_gt");
	assign_to_key ("<Ctrl-Enter>", "end_enter");
	tabbing_key = inq_keyboard ();
	keyboard_pop (1);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_tabbing (...)
{
   if (tabbing_key == 0) def_tabbing ();
	use_local_keyboard (tabbing_key);
	begin_command ("tabbing");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_tabbing (...)
{
	int line, col, match, i;

	int line1, col1, ntab, length;

	int ins, j;

	string text, mess;

	end_command ("tabbing");
	use_local_keyboard (0);

/*	pause_on_error (1);*/
	inq_position (line, col);
	match = search_back ("\\begin{tabbing}", 0, 0, 0);
	if (match > 0)
		{
		inq_position (line1, col1);
		move_abs (line1 + 1, 1);	  /*	Line after \begin{...					 */
		text = read_logic_line ();
		text = translate_string (text, "\\>", "\\=");
		text = translate_string (text, "\\\\", "");
		text = translate_string (text, "\n", "");
      text=text+" \\kill\n";
		i = strlen (text);
		move_abs (line1 + 1, 1);
/*		delete_char (i);*/
		insert (text);
      ++line;
		ntab = 0;

		while ((match = search_string ("\\=", text, length, 0, 0)) > 0)
			{
			++ntab;
			text = substr (text, match + 2);
			}
		if (ntab > 0)

			{
			inq_position (line1, col1);	/*  Logical line after \begin{...	 */
			j = 1;
			while (line1 < line - 1)
				{
				text = read_logic_line ();
				i = 0;
				++j;
				while ((match = search_string ("\\>", text, length, 0, 0)) > 0)
					{
					++i;
					text = substr (text, match + 1);
					}
				if (i != ntab)
					{
					if (i < ntab)
						error ("Only %d tabs in line %d (tabbing line %d)!", i, line1, j);
					else
						error ("%d tabs in line %d (tabbing line %d) are too much!", i, line1, j);
					}
				inq_position (line1, col1);
				++line1;
				move_abs (line1, col1);
				}
			}
		move_abs (line, col);
		}
/*	pause_on_error (0);*/
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
tabbing (...)
{
	put_commands ("tabbing");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 								  MINIPAGE											 ; 	 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_minipage (...)
{
	begin_command ("minipage");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_minipage (...)
{
	end_command ("minipage");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
minipage (...)
{
	put_commands ("minipage");
}

/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 								  ITEMIZE											 ; 	 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_itemize (...)
{
	begin_command ("itemize");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_itemize (...)
{
	end_command ("itemize");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
itemize (...)
{
	put_commands ("itemize");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 								  enumerate 											;	 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_enumerate (...)
{
	begin_command ("enumerate");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_enumerate (...)
{
	end_command ("enumerate");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
enumerate (...)
{
	put_commands ("enumerate");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 								  description 											;	 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_description (...)
{
	begin_command ("description");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_description (...)
{
	end_command ("description");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
description (...)
{
	put_commands ("description");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 								  center 											;		 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_center (...)
{
	begin_command ("center");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_center (...)
{
	end_command ("center");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
center (...)
{
	put_commands ("center");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 								  thebibliography 											; */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
def_thebib (...)
{
	keyboard_push ();
	assign_to_key ("<Ctrl-n>", "bibitem");
	assign_to_key ("<Ctrl-Enter>", "end_enter");
	thebib_key = inq_keyboard ();
	keyboard_pop (1);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_thebibliography (...)
{
	begin_command ("thebibliography");
   if (thebib_key == 0) def_thebib ();
	use_local_keyboard (thebib_key);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_thebibliography (...)
{
	end_command ("thebibliography");
	use_local_keyboard (0);
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
thebibliography (...)
{
	put_commands ("thebibliography");
}

/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
/* 																						 ; 	 */
/* 								  figure 											;		 */
/* 																						 ; 	 */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
begin_figure (...)
{
	begin_command ("figure");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
end_figure (...)
{
	end_command ("figure");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
figure (...)
{
	put_commands ("figure");
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */

read_logic_line (...)
{
	int line, col;

	string text;

	/* 	  (pause_on_error 1) 															 */
	inq_position (line, col);
	text = read ();
	while (index (text, "\\\\") == 0 && index (text, "\\end{") == 0)
		{
		move_abs (++line, 1);
		text = text + read ();
		/* 			(error "text = %s" text)											 */
		}
	return text;
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */

translate_string (...)
{
	string text, dum, old, new;

	int ind, length, i;

	/* 	  (pause_on_error 1) 															 */
	get_parm (0, text);
	dum = text;
	get_parm (1, old);
	get_parm (2, new);
	/* 	  (error "text = %s old = %s new = %s" text old new)					 */
	i = 0;
	while ((ind = search_string (old, text, length, 0, 0)) > 0)
		{
		dum = substr (text, ind + length);
		i = i + ind;
		text = (substr (text, 1, ind - 1) + new) + dum;
		/* 			  (error "text = %s dum = %s" text dum )						 */
		}
	return text;
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */
check_tab (...)
{
	int line, col, match, i, j
	   ,line1,col1,ntab,length;
	string text,mess,command;

   get_parm (0,command);
	inq_position (line, col);
   sprintf(text,"\\begin{%s}",command);
	match = search_back (text,0,0,0);
	if (match > 0)
		{
		inq_position (line1, col1);
		move_abs (line1 + 1, 1);
		text = read_logic_line ();
		ntab = 0;
		while ((match = search_string ("&", text, length, 0, 0)) > 0)
			{
			++ntab;
			text = substr (text, match + 1);
			}
		if (ntab > 0)
			{
			move_abs (line1, col1);
			end_of_line ();
   		if (command=="tabular") 
	   		insert ("{||c");
         else 
	   		insert ("{c");
			i = 0;
			while (ntab > i)
				{
				if (command=="tabular") 
               insert ("|c");
            else 
               insert ("c");
				++i;
				}
			if (command=="tabular") 
   			insert ("||}");
         else 
   			insert ("}");
			line1 = line1 + 1;
			move_abs (line1, col1);
			j = 0;
			while (line1 < line - 1)
				{
				text = read_logic_line ();
				++j;
				i = 0;
				length = 0;
				while ((match = search_string ("&", text, length, 0, 0)) > 0)
					{
					++i;
					text = substr (text, match + 1);
					}
				if (i != ntab)
					{
					if (i < ntab)
						sprintf (mess, "Only %d tabs in line %d (%s line %d)!", i,line1,command, j);
					else
						sprintf (mess, "%d tabs in line %d (%s line %d) are too much!",i, line1,command, j);
					error (mess);
					beep ();
					}
				inq_position (line1, col1);
				++line1;
				move_abs (line1, col1);
				}
			}
		move_abs (line, col);
		}
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 	 */

sfreport (...)
{
   insert("\\documentstyle[block,sflogo,sfreph]{article}\n");
   insert("\\begin{document}\n");
   insert("  \\begin{sfreport}\n");
   insert("  \\sfrepmal\n");
   insert("  \\sfrepno{STF11 }\n");
   insert("  \\sfclass{}\n");
   insert("    \\sftitle{}\n");
   insert("    \\sfdate{\\today}\n");
   insert("    \\sfpages{ / }\n");
   insert("    \\sfauthor{}\n");
   insert("    \\sfprojman{}\n");
   insert("    \\sfdivision{Sintef 11}\n");
   insert("    \\sfprojnum{11}\n");
   insert("    \\sfisbnnum{}\n");
   insert("    \\sfpricegr{}\n");
   insert("    \\sfclient{}\n");
   insert("    \\sfcliref{}\n");
   insert("    \\sfabstract{}\n");
   insert("    \\sfgroupi{}\n");
   insert("    \\sfgroupii{}\n");
   insert("    \\sfgroupin{}\n");
   insert("    \\sfgroupiin{}\n");
   insert("    \\sfkeywi{}\n");
   insert("    \\sfkeywin{}\n");
   insert("    \\sfkeywii{}\n");
   insert("    \\sfkeywiin{}\n");
   insert("    \\sfkeywiii{}\n");
   insert("    \\sfkeywiiin{}\n");
   insert("  \\end{sfreport}\n");
   insert("\\end{document}\n");
   move_rel(-26,16);
}