%  -*- mode: SLang -*-
%
% This file is full with examples of things you can add to your private
% jed.rc (e. g. ~/.jedrc or ~/.jed/jed.rc) to improve and customize the
% LaTeX mode.
%

% Setting these variables produce a more verbose backtrace upon error.
_traceback = 1;
_slangtrace = 1;

% These lines should be anywhere in your jed.rc to make the files of the
% LaTeX mode will be found if you place them in your home directory.
#ifdef MSWINDOWS
Jed_Home_Directory = path_concat(getenv("APPDATA"), "jed");
#else
Jed_Home_Directory = path_concat(getenv("HOME"), ".jed");
#endif
set_jed_library_path(Jed_Home_Directory + "," + get_jed_library_path());
Jed_Highlight_Cache_Dir = path_concat(Jed_Home_Directory, "dfa");

% If your TeX binaries aren't in the PATH you can use one of these
% commands to make jed find them:
% putenv("PATH=/media/cdrom/texlive/bin/i386-linux:$PATH"$);  % UNIX
% putenv("PATH=D:\texlive\bin\win32:$PATH"R$);                % WINDOWS

% Add these lines to load the LaTeX mode for files with the extension .tex
% and .latex.
add_mode_for_extension("latex", "tex");
add_mode_for_extension("latex", "ltx");
add_mode_for_extension("latex", "sty");
add_mode_for_extension("latex", "cls");

#ifdef HAS_DFA_SYNTAX
% Enable dfa syntax highlighting for the LaTeX mode.
enable_dfa_syntax_for_mode("LaTeX");
#endif

% % This function is run after the file latex_external.sl was loaded. You can
% % place here commands they should run exaclty one time, but need functions
% % defined in latex_external.sl
% define after_latex_external_load_hook()
% {
%     % Define your own configuration for building and viewing the output file.
%     call_function("latex_external->cust_set",
%                   "rubber_srcspcl", "Rubber with source specials",
%                   "", "",
%                   "rubber --src-specials --inplace %m", "",
%                   "", "rubber --clean %m",
%                   "",
%                   "xdvi -nofork -editor %e -sourceposition %l:%c%f %M.dvi",
%                   0);

%     % Redefine the print command and in the second call the makeindex and the
%     % print command of the default configurations.
%     call_function("latex_external->cust_set",
%                   "rubber_dvi", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
%                   "sh -c dvilj4l% -q% -e-% %M.dvi% |% lp% -t% \"%M\"% -",
%                   NULL, NULL);
%     call_function("latex_external->cust_set",
%                   "latex", "LaTeX with Xindy", NULL,
%                   "texindy --language german-din %M.idx", NULL, NULL,
%                   NULL, NULL,
%                   "sh -c dvilj4l% -e-% \"%M.dvi\"|lp% -t% \"%M\"",
%                   NULL, NULL);
% }

% Select your own configurations
variable LaTeX_Config = "rubber_dvi";

% Add your own template directory to the search list.
variable LaTeX_Template_Dir = path_concat(Jed_Home_Directory, "latex-templ");
#ifdef UNIX
 LaTeX_Template_Dir += ",/usr/local/share/jjm/latex-templ-public";
#endif

% Set your prefered packages to be used for templates.
variable LaTeX_Default_Packages = "babel:inputenc:fontenc:fixltx2e:microtype";

% Set your prefered class options to be used for templates.
variable LaTeX_Default_Class_Options = "";

% The file where informations about newly known cmds and envs should go
variable LaTeX_File_New = path_concat(Jed_Home_Directory, "latex_cfg.sl");

% Define an abbreviation table for your LaTeX files
#if (_slang_utf8_ok)
create_abbrev_table("TeX", "0-9A-Za-z�-��-��-");
#else
create_abbrev_table ("TeX", "0-9A-Za-z�-��-��-");
#endif
define_abbrev ("TeX",   "ie",   "i.e.\\ ");  % that is
define_abbrev ("TeX",   "eg",   "e.g.\\ ");  % for example
define_abbrev ("TeX",   "latex","\\LaTeX{}");
define_abbrev ("TeX",   "tex",  "\\TeX{}");

% german abbreviations
define_abbrev ("TeX",   "dh",   "d.\\,h.");  % das hei�t
define_abbrev ("TeX",   "zB",   "z.\\,B.");  % zum Beispiel
define_abbrev ("TeX",   "idR",  "i.\\,d.\\,R."); % in der Regel
define_abbrev ("TeX",   "obda", "o.\\,B.\\,d.\\,A.");

% This is needed for the symbol Key_Shift_Tab in the latex_mode_hook below
require("keydefs");

% This function is run after the file latex.sl was loaded. You can place
% here commands they should run exaclty one time, but need functions
% defined in latex.sl
define after_latex_load_hook(mode)
{
   % Define a new completion pair for the environment faelle. Nothing is
   % inserted before the newline and \item after the newline.
   call_function("latex->set_nl_completion", "faelle", "", "\\item ");

   % Define additional commands for the ^cs completion list.
   call_function("latex->cmd_register", "bigm",  0, 1, "same as \big but add more horizontal space before and after delimiter"R, "amsmath", NULL);
   call_function("latex->cmd_register", "biggm", 0, 1, "same as \bigg but add more horizontal space before and after delimiter"R, "amsmath", NULL);
   call_function("latex->cmd_register", "Bigm",  0, 1, "same as \Big but add more horizontal space before and after delimiter"R, "amsmath", NULL);
   call_function("latex->cmd_register", "Biggm", 0, 1, "same as \Bigg but add more horizontal space before and after delimiter"R, "amsmath", NULL);
   call_function("latex->cmd_register", "complement", 0, 1, "Symbol for complement of a set", "amsmath", NULL);
   call_function("latex->cmd_register", "null",  0, 0, "empty \mbox{}"R, "", NULL);

   % Maybe you use these commands in your documents.
   call_function("latex->cmd_register", "todo",  1, 0, "If you have to add or rework something, make a note", "", NULL);
   call_function("latex->cmd_register", "help",  1, 0, "If something is sketchy, write down you question", "", NULL);
   call_function("latex->cmd_register", "abs",   1, 1, "Absolute value: |x|", "", NULL);
   call_function("latex->cmd_register", "norm",  1, 1, "Mathematical norm: ||x||", "", NULL);

   % If you want different default options for amsmath ...
   % call_function("latex->pkg_register", "amsmath", "intlimits,leqno", "The main package for mathematical stuff", "amsldoc", NULL);

   % Redefine local key mapping of german umlauts to use babel commands.
   definekey(" \"o", "�", mode);
   definekey(" \"O", "�", mode);
   definekey(" \"a", "�", mode);
   definekey(" \"A", "�", mode);
   definekey(" \"u", "�", mode);
   definekey(" \"U", "�", mode);
   definekey(" \"s", "�", mode);
   definekey(" \"<", "�", mode);
   definekey(" \">", "�", mode);
   definekey(" \\S", "�", mode);

   % Define a new key combination ^cnd (^cn are additional math
   % commands) to insert the command \vdash.
   definekey_reserved("latex->cmd_insert(\"vdash\")", "nd", mode);
   definekey_reserved("latex->cmd_insert(\"abs\")", "na", mode);
   definekey_reserved("latex->cmd_insert(\"norm\")", "nn", mode);
   definekey("latex->textormath(\"self_insert_cmd\", \"local_tex_math_pipe\")",
                "|", mode);

   % Bind array_next_cell to TAB and indent_line to ^X TAB
   definekey("latex->array_next_cell", "\t", mode);
   definekey("latex->array_prev_cell", Key_Shift_Tab, mode);
   definekey("indent_line", "^X\t", mode);

   % Load configuration entered while previous LaTeX sessions
   if (file_status(LaTeX_File_New) == 1)
     () = evalfile(LaTeX_File_New);
}

% This function is loaded by latex_mode() after the LaTeX mode is setup
% for a file
define latex_mode_hook ()
{
   % In LaTeX documents you can use the whole width of the screen.
   WRAP=80;

   % Do not ask if this file should be saved before running LaTeX.
  _set_buffer_flag(0x80);

   % fold the preample
   if ( bol_fsearch("\\documentclass") )
   {
       () = down(1);
       call_function("latex->fold", 0);
       bob();
   }

   % fold the first sublevel in document
   if ( bol_fsearch("\\begin{document}") )
   {
       () = down(1);
       call_function("latex->fold", 1);
       bob();
   }
}

define local_tex_math_pipe()
{
   if ( blooking_at("\\") )
   {
       call("self_insert_cmd");
       return;
   }

   if ( andelse {left(5)} {looking_at("\\abs{}")} )
   {
       () = right(1);
       () = replace_chars(3, "norm");
       () = right(1);
   }
   else
   {
       () = right(5);
       call_function("latex->cmd_insert", "abs");
   }
}