home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / JED / JED097-1.TAR / jed / lib / tex.sl < prev    next >
Encoding:
Text File  |  1994-12-12  |  5.1 KB  |  261 lines

  1. %
  2. %  simple (La)TeX mode for JED
  3. %
  4. %  When tex mode is loaded, 'tex_mode_hook' is called.  This hook will allow
  5. %  users to customize the mode.  In particular, certain functions here have
  6. %  no keybindings, e.g., 'latex_do_environment'.  So, in your jed.rc file,
  7. %  add something like:
  8. %    define tex_mode_hook () {
  9. %       local_setkey ("latex_do_environment", "^C^E");
  10. %    }
  11. %  which binds the function to Ctrl-C Ctrl-E.
  12.  
  13.  
  14.  
  15.  
  16. %  This hook identifies lines containing TeX comments as paragraph separator
  17.  
  18. define tex_is_comment ()
  19. {
  20.    bol ();
  21.    while (ffind ("\\%")) go_right (2);
  22.    ffind ("%"); % return value on stack
  23. }
  24.  
  25. variable Tex_Ignore_Comment = 0;       % if true, line containing a comment
  26.                                        % does not delimit a paragraph
  27.  
  28. define tex_paragraph_separator ()
  29. {
  30.    bol();
  31.    if (looking_at("\\")
  32.        or (skip_white(), eolp())
  33.        or looking_at("$$")) return 1;
  34.    
  35.    %
  36.    %  look for comment
  37.    %
  38.    return not (Tex_Ignore_Comment) and tex_is_comment ();
  39.  
  40.  
  41. define tex_wrap_hook ()
  42. {
  43.    variable yep;
  44.    push_spot ();
  45.    yep = up(1) and tex_is_comment ();
  46.    pop_spot ();
  47.    if (yep) 
  48.      {
  49.     push_spot ();
  50.     bol (); 
  51.     skip_white ();
  52.     insert ("% ");
  53.     pop_spot ();
  54.      }
  55. }
  56.  
  57. define tex_isolate_paragraph ()
  58. {
  59.    variable ic = Tex_Ignore_Comment;
  60.    Tex_Ignore_Comment = 1;
  61.    push_spot (); push_mark ();
  62.    backward_paragraph ();
  63.    narrow ();
  64.    Tex_Ignore_Comment = ic;
  65. }
  66.  
  67. define tex_blink_dollar ()
  68. {
  69.    variable d = "$", p, unmatched, p1, n, n1;
  70.    variable pnow = POINT, pmax;
  71.    
  72.    insert(d);
  73.    if (blooking_at ("\\$")) return;
  74.    push_spot ();
  75.    
  76.    tex_isolate_paragraph ();           %  spot pushed
  77.    pop_spot (); 
  78.    n = whatline ();
  79.    bob ();
  80.    
  81.    unmatched = 0;
  82.    
  83.    while (fsearch (d))
  84.      {
  85.     p = POINT;
  86.     pmax = 0x7FFF;
  87.     if (tex_is_comment ())
  88.       {
  89.          pmax = POINT;
  90.       }
  91.     POINT = p;
  92.     
  93.     if ((n == whatline ()) and (pnow < pmax)) pmax = pnow;
  94.     
  95.     if (p >= pmax)
  96.       {
  97.          if (n == whatline ()) break;
  98.          eol ();
  99.          continue;
  100.       }
  101.     
  102.     if (blooking_at("\\")) 
  103.       {
  104.          POINT++;
  105.          continue;
  106.       }
  107.     
  108.     !if (unmatched) 
  109.       {
  110.          p1 = p;
  111.          n1 = whatline ();
  112.       }
  113.     
  114.     unmatched = not(unmatched);
  115.     POINT++;
  116.      }
  117.    
  118.    if (unmatched)
  119.      {
  120.     n = n - n1;
  121.     goto_line (n1);
  122.     POINT = p1;
  123.     widen ();
  124.     if (n >= window_info ('r'))
  125.       {
  126.          bol (); push_mark (); eol (); 
  127.          message (strcat ("Matches ", bufsubstr ()));
  128.       }
  129.     else
  130.       {
  131.          update(0);
  132.          input_pending(10); pop();
  133.       }
  134.      }
  135.    else widen ();
  136.    pop_spot ();
  137. }
  138.  
  139. define tex_insert_quote ()
  140. {
  141.    variable c, l, r;
  142.    l = char ('`');   r = char ('\'');
  143.    
  144.  
  145.    if ((LAST_CHAR != '\'') and (LAST_CHAR != '"'))
  146.      {
  147.     insert(char(LAST_CHAR));
  148.     return;
  149.      }
  150.    
  151.    c = '[';
  152.    !if (bolp())
  153.      {
  154.     go_left(1);
  155.     c = what_char();
  156.     go_right(1);
  157.      }
  158.    
  159.    if (c == '\\')
  160.      {
  161.     insert (char(LAST_CHAR));
  162.     return();
  163.      }
  164.    
  165.    if (is_substr("[({\t ", char(c)))
  166.      {
  167.     insert (l);
  168.     if (LAST_CHAR == '"') insert (l);
  169.      }
  170.    else
  171.      {
  172.     insert (r);
  173.     if (LAST_CHAR == '"') insert (r);
  174.      }
  175. }
  176.  
  177.  
  178. define latex_do_environment ()
  179. {
  180.    variable env = Null_String;
  181.    
  182.    push_spot ();
  183.    if (bsearch ("\\begin{"))
  184.      { 
  185.     go_right (7); push_mark ();
  186.     ffind ("}"); pop ();
  187.     env = bufsubstr ();
  188.      }
  189.    pop_spot ();
  190.    
  191.    eol (); newline ();
  192.    env = read_mini ("Enter Environment Name:", env, Null_String);
  193.    insert (Sprintf("\\\\begin{%s}\n\n\\\\end{%s}\n", env, env, 2));
  194.    go_up(2);
  195. }
  196.  
  197. define tex_ldots ()
  198. {
  199.    if (blooking_at (".."))
  200.      {
  201.     go_left (2);
  202.     deln (2);
  203.     insert ("\\ldots ");
  204.     return;
  205.      }
  206.    insert_char ('.');
  207. }
  208.  
  209.    
  210. !if (keymap_p("TeX-Mode"))
  211. {
  212.    make_keymap ("TeX-Mode");
  213.    definekey ("tex_insert_quote", "\"", "TeX-Mode");
  214.    definekey ("tex_insert_quote", "'", "TeX-Mode");
  215.    definekey ("tex_blink_dollar", "$", "TeX-Mode");
  216.    definekey ("tex_ldots", ".", "TeX-Mode");
  217. }
  218.  
  219. %!% Mode useful for editing TeX and LaTeX modes.  
  220. %!% Useful bindings:
  221. %!%  '"'  :  tex_insert_quote
  222. %!%  '\'' :  tex_insert_quote
  223. %!%  '$'  :  tex_blink_dollar
  224. %!%  '.'  :  tex_ldots.  Inserts a '.' except if preceeded by two dots.  In 
  225. %!%           this case, the dots are converted to \ldots.
  226. %!%
  227. %!%  When tex mode is loaded, 'tex_mode_hook' is called.  This hook will allow
  228. %!%  users to customize the mode.  In particular, certain functions here have
  229. %!%  no keybindings, e.g., 'latex_do_environment'.  So, in your jed.rc file,
  230. %!%  add something like:
  231. %!%    define tex_mode_hook () {
  232. %!%       local_setkey ("latex_do_environment", "^C^E");
  233. %!%    }
  234. %!%  which binds the function to Ctrl-C Ctrl-E.
  235. define tex_mode ()
  236. {
  237.    variable mode = "TeX";
  238.    use_keymap ("TeX-Mode");
  239.    setmode ("TeX", 0x1 | 0x20);
  240.    set_buffer_hook ("par_sep", "tex_paragraph_separator");
  241.    set_buffer_hook ("wrap_hook", "tex_wrap_hook");
  242.    runhooks ("tex_mode_hook");
  243.    % This is called after the hook ti give the hook a chance to load the
  244.    % abbrev table.
  245.    if (abbrev_table_p (mode)) use_abbrev_table (mode);
  246. }
  247.  
  248. %-----------------------------------------------------------%
  249.  
  250. define tex_info_find_node ()
  251. {
  252.    variable node;
  253.    
  254.    node = read_mini ("Node:", Null_String, Null_String);
  255.    !if (strlen (node)) return;
  256.    info_mode ();
  257.    info_find_node ("(latex)top");
  258.    info_find_node (strcat ("(latex)", node));
  259. }
  260.