
/********** Documentation to be inserted in file "edoc" ***********

show-matching-dollar	Insert $ and show matching $ or $$ in TeX mode.
        Calls normal-character to insert the key $, then shows the
        matching $ that delimits math-mode or displayed material.

tex-mode	Show delimiters for TeX code and fill.
        This command puts the current buffer in TeX mode and
        is invoked automatically for a file with extension .tex .
        ([{ are flashed using show-matching-delimiter().  Dollar signs
        $ or $$ invoke show-matching-dollar().  fill-mode is set on, and
        fill-column is set to 72.  The mode line says TeX Fill.

***********             End of documentation            **********/

/* -----  Gary R. Smith  (smith#gary@b.mfenet@nmfecc.arpa) */

/****************** Beginning of file "tex.e" ******************/


#include "eel.h"

/*
Show delimiters for TeX code and fill.
Written by Gary R. Smith in Oct. 1986.
            
Fill mode is enabled automatically and the fill column set to 72.

When one of )]} is typed, the matching ([{ is flashed, by binding the
former keys to show-matching-delimiter().

Matching dollar signs, which indicate math-mode and displayed
material, are searched for using show-matching-dollar().  That
function performs searches that are limited to a small portion of
text by these assumptions about the TeX code:  (a) new paragraphs do
not begin in $...$ or $$...$$, and (b) $...$ is not nested within
$$...$$, or vice versa.  The following searches are made:

(1) If the $ just typed is preceded by another $, search backwards,
    counting occurrences of $$, until a solitary $ or the beginning of
    the buffer or the beginning of a paragraph is found (\n\n, i.e., a
    blank line, or TeX command \par).  If the $$ just typed is the
    first, third, fifth, etc., occurrence, then flash the first of the
    matching $$.

(2) A solitary $ causes a search backwards, counting occurrences of
    solitary $, until $$ or the beginning of the buffer or the
    beginning of a paragraph is found.  If the $ just typed is the
    first, third, fifth, etc., occurrence, then flash the first of the
    matching $.
*/

buffer int tex_mode_on = 0;     /* Are we in TeX mode? */

keytable tex_tab;               /* Key table for TeX mode */

command show_matching_dollar() on tex_tab['$']
{
        int orig;
        
        normal_character();
        iter = 0;
        say("");
        orig = point;
        if (dollar_match()) show_line();        /* Function from prog.e */
        point = orig;
        return;
}

dollar_match()  /* Return 1 if backwards search finds matching $,
                   return 0 otherwise */
{
        int double = 0;
        int count = 0;
        int loc;        /* Will hold location of match, if found */
        
        if (point < 3) return 0;
        point -= 2;
        if (curchar() == '$') double = 1;       /* $$ just typed */
        else point++;
        
        while (re_search(-1, "$|\n\n|\\par")) { /* To beginning or break */
                if (curchar() == '$') { /* Found $ */
                        if (double) {   /* Trying to match $$ */
                                if (point > 0 && character(point-1) == '$') {
                                        /* Yes $$ */
                                        point--;
                                        if (!count++) loc = point;
                                        /* Count, and save loc if first */
                                }
                                else break;     /* Found solitary $ */
                        }
                        else {          /* Trying to match solitary $ */
                                if (!point) {
                                        if (!count++) loc = 0;
                                }
                                else if (character(point-1) != '$') {
                                        /* Found $ */
                                        if (!count++) loc = point;
                                        /* Count, and save loc if first */
                                }
                                else break;     /* Found $$ */
                        }
                }
                else break;     /* Found beginning of paragraph */
        }
        
        point = loc;
        return count % 2;
}

command tex_mode()
{
        mode_keys = tex_tab;            /* Use these keys */
        tex_tab[')'] = (short) show_matching_delimiter;
        tex_tab[']'] = (short) show_matching_delimiter;
        tex_tab['}'] = (short) show_matching_delimiter;
        fill_mode = 1;
        margin_right = 72;
        tex_mode_on = 1;
        major_mode = strsave("TeX");
        make_mode();
}

/* Make this the default mode for .tex files */
suffix_tex()    { tex_mode(); }

/****************** End of file "tex.e" ******************/
