home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / eel / tex.e < prev    next >
Text File  |  1994-03-04  |  5KB  |  130 lines

  1.  
  2. /********** Documentation to be inserted in file "edoc" ***********
  3.  
  4. show-matching-dollar    Insert $ and show matching $ or $$ in TeX mode.
  5.         Calls normal-character to insert the key $, then shows the
  6.         matching $ that delimits math-mode or displayed material.
  7.  
  8. tex-mode    Show delimiters for TeX code and fill.
  9.         This command puts the current buffer in TeX mode and
  10.         is invoked automatically for a file with extension .tex .
  11.         ([{ are flashed using show-matching-delimiter().  Dollar signs
  12.         $ or $$ invoke show-matching-dollar().  fill-mode is set on, and
  13.         fill-column is set to 72.  The mode line says TeX Fill.
  14.  
  15. ***********             End of documentation            **********/
  16.  
  17. /* -----  Gary R. Smith  (smith#gary@b.mfenet@nmfecc.arpa) */
  18.  
  19. /****************** Beginning of file "tex.e" ******************/
  20.  
  21.  
  22. #include "eel.h"
  23.  
  24. /*
  25. Show delimiters for TeX code and fill.
  26. Written by Gary R. Smith in Oct. 1986.
  27.             
  28. Fill mode is enabled automatically and the fill column set to 72.
  29.  
  30. When one of )]} is typed, the matching ([{ is flashed, by binding the
  31. former keys to show-matching-delimiter().
  32.  
  33. Matching dollar signs, which indicate math-mode and displayed
  34. material, are searched for using show-matching-dollar().  That
  35. function performs searches that are limited to a small portion of
  36. text by these assumptions about the TeX code:  (a) new paragraphs do
  37. not begin in $...$ or $$...$$, and (b) $...$ is not nested within
  38. $$...$$, or vice versa.  The following searches are made:
  39.  
  40. (1) If the $ just typed is preceded by another $, search backwards,
  41.     counting occurrences of $$, until a solitary $ or the beginning of
  42.     the buffer or the beginning of a paragraph is found (\n\n, i.e., a
  43.     blank line, or TeX command \par).  If the $$ just typed is the
  44.     first, third, fifth, etc., occurrence, then flash the first of the
  45.     matching $$.
  46.  
  47. (2) A solitary $ causes a search backwards, counting occurrences of
  48.     solitary $, until $$ or the beginning of the buffer or the
  49.     beginning of a paragraph is found.  If the $ just typed is the
  50.     first, third, fifth, etc., occurrence, then flash the first of the
  51.     matching $.
  52. */
  53.  
  54. buffer int tex_mode_on = 0;     /* Are we in TeX mode? */
  55.  
  56. keytable tex_tab;               /* Key table for TeX mode */
  57.  
  58. command show_matching_dollar() on tex_tab['$']
  59. {
  60.         int orig;
  61.         
  62.         normal_character();
  63.         iter = 0;
  64.         say("");
  65.         orig = point;
  66.         if (dollar_match()) show_line();        /* Function from prog.e */
  67.         point = orig;
  68.         return;
  69. }
  70.  
  71. dollar_match()  /* Return 1 if backwards search finds matching $,
  72.                    return 0 otherwise */
  73. {
  74.         int double = 0;
  75.         int count = 0;
  76.         int loc;        /* Will hold location of match, if found */
  77.         
  78.         if (point < 3) return 0;
  79.         point -= 2;
  80.         if (curchar() == '$') double = 1;       /* $$ just typed */
  81.         else point++;
  82.         
  83.         while (re_search(-1, "$|\n\n|\\par")) { /* To beginning or break */
  84.                 if (curchar() == '$') { /* Found $ */
  85.                         if (double) {   /* Trying to match $$ */
  86.                                 if (point > 0 && character(point-1) == '$') {
  87.                                         /* Yes $$ */
  88.                                         point--;
  89.                                         if (!count++) loc = point;
  90.                                         /* Count, and save loc if first */
  91.                                 }
  92.                                 else break;     /* Found solitary $ */
  93.                         }
  94.                         else {          /* Trying to match solitary $ */
  95.                                 if (!point) {
  96.                                         if (!count++) loc = 0;
  97.                                 }
  98.                                 else if (character(point-1) != '$') {
  99.                                         /* Found $ */
  100.                                         if (!count++) loc = point;
  101.                                         /* Count, and save loc if first */
  102.                                 }
  103.                                 else break;     /* Found $$ */
  104.                         }
  105.                 }
  106.                 else break;     /* Found beginning of paragraph */
  107.         }
  108.         
  109.         point = loc;
  110.         return count % 2;
  111. }
  112.  
  113. command tex_mode()
  114. {
  115.         mode_keys = tex_tab;            /* Use these keys */
  116.         tex_tab[')'] = (short) show_matching_delimiter;
  117.         tex_tab[']'] = (short) show_matching_delimiter;
  118.         tex_tab['}'] = (short) show_matching_delimiter;
  119.         fill_mode = 1;
  120.         margin_right = 72;
  121.         tex_mode_on = 1;
  122.         major_mode = strsave("TeX");
  123.         make_mode();
  124. }
  125.  
  126. /* Make this the default mode for .tex files */
  127. suffix_tex()    { tex_mode(); }
  128.  
  129. /****************** End of file "tex.e" ******************/
  130.