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

  1. %
  2. %  Fortran mode for jed
  3. %
  4. %   Loading this file, then executing 'fortran_mode' will start fortran mode
  5. %   on the current buffer.  The only keys that get remapped are:
  6. %     ^M (return)   which runs the fortan newline command
  7. %     ^I (tab) which indents the current line
  8. %     ^[; (escape semicolon) runs the fortran comment command
  9. %     ^[: (escape colon) runs fortran uncomment command
  10. %     ^[^M : start a continuation line
  11.  
  12. !if (is_defined ("Fortran_Continue_Char"))
  13. {
  14.    variable Fortran_Continue_Char = "&";      % default continuation char
  15. }
  16.  
  17. !if (is_defined ("Fortran_Comment_String"))
  18. {
  19.    variable Fortran_Comment_String = "C ";
  20. }
  21.  
  22. !if (is_defined ("Fortran_Indent_Amount"))
  23. {
  24.    variable Fortran_Indent_Amount = 2;
  25. }
  26.  
  27. % goto beginning of line and skip past continuation char
  28. define fortran_skip_label ()
  29. {
  30.    bol ();
  31.    skip_chars ("0-9 \t");
  32.    if (looking_at(Fortran_Continue_Char)) go_right(1);
  33.    skip_white ();
  34. }
  35.  
  36. % computes fortran indent
  37. define fortran_get_indent ()
  38. {
  39.    variable col = 7;    %/*  at top of buffer it should be 7 n'est pas? */
  40.    variable cse = CASE_SEARCH;
  41.    CASE_SEARCH = 0;
  42.  
  43.    push_spot ();
  44.    
  45.    while (up(1))
  46.      {
  47.     bol ();
  48.     skip_white();
  49.     if (eolp() or looking_at(Fortran_Continue_Char)) continue;
  50.     fortran_skip_label ();
  51.     col = what_column ();
  52.      
  53.     if (col == 1) continue;
  54.     
  55.     if (looking_at("do ") 
  56.         or looking_at("else"))
  57.       col += Fortran_Indent_Amount;
  58.     else if (looking_at("if ") or looking_at("if("))
  59.       {
  60.          if (ffind ("then")) col += Fortran_Indent_Amount;
  61.       }
  62.     break;
  63.      }
  64.    
  65.   % now check current line
  66.    pop_spot ();
  67.    push_spot ();
  68.    fortran_skip_label ();
  69.   
  70.    if (looking_at("end") or
  71.        looking_at("continue") or
  72.        looking_at("else")) col -= Fortran_Indent_Amount;
  73.   
  74.    if (col < 7) col = 7;
  75.    pop_spot ();
  76.    CASE_SEARCH = cse;
  77.    col;
  78. }
  79.  
  80. % used below-- skips range 
  81. % assumes we are after the label or continuation char 
  82. % and indents the rest to goal
  83. define fortran_tough_indent (goal)
  84. {
  85.    skip_chars ("0-9");
  86.    trim ();
  87.    if (looking_at (Fortran_Continue_Char))
  88.      {
  89.     insert_spaces (6 - what_column());
  90.     go_right(1); trim();
  91.     goal += Fortran_Indent_Amount;
  92.      }
  93.    
  94.    insert_spaces (goal - what_column());
  95. }
  96.  
  97.  
  98.  
  99. % fortran indent routine
  100. define fortran_indent ()
  101. {
  102.    variable col, ch, n, goal;
  103.    push_spot ();
  104.    goal = fortran_get_indent ();
  105.    bol (); skip_white ();
  106.    col = what_column ();
  107.    
  108.    switch (char(what_char()))
  109.      {
  110.     isdigit (()) :     %  label
  111.  
  112.     if (col >= 6)
  113.       {
  114.          bol (); trim ();
  115.          insert_single_space ();
  116.       }
  117.     
  118.     fortran_tough_indent (goal);
  119.      }
  120.      {
  121.     case Fortran_Continue_Char :  % continuation character
  122.     bol (); trim (); insert ("     ");
  123.     fortran_tough_indent (goal);
  124.      }
  125.      {
  126.     pop (); not (bolp()) or eolp ():                  % general case
  127.     bol (); trim ();
  128.     goal--; insert_spaces (goal);
  129.      }
  130.    pop_spot ();   
  131.    skip_white ();
  132. }
  133.  
  134. define fortran_is_comment ()
  135. {
  136.    bol ();
  137.    skip_chars (" \t0-9");
  138.    bolp () and not (eolp());
  139. }
  140.  
  141.  
  142. define fortran_newline ()
  143. {
  144.    variable p, cont;
  145.    
  146.    if (bolp ())
  147.      {
  148.     newline ();
  149.     return;
  150.      }
  151.  
  152.    fortran_indent ();
  153.    push_spot ();
  154.    bskip_chars (" \t"); trim();
  155.    
  156.    if (what_column () > 72)
  157.      {
  158.     push_spot ();
  159.     bol(); skip_white();
  160.     !if (bolp()) message ("Line exceeds 72 columns.");
  161.     pop_spot ();
  162.      }
  163.    
  164.    p = POINT;
  165.    bskip_chars("-+*=/,(");
  166.    
  167.    cont = (p != POINT);
  168.    
  169.    if (fortran_is_comment ()) cont = 0;
  170.    
  171.    bol ();
  172.    skip_white ();
  173.    if (looking_at("data ")) cont = 0;
  174.    
  175.    pop_spot ();
  176.    
  177.    newline ();
  178.    insert_single_space ();
  179.    if (cont) insert(Fortran_Continue_Char);
  180.    fortran_indent ();
  181. }
  182.  
  183.  
  184. define fortran_continue_newline ()
  185. {
  186.    fortran_newline ();
  187.    
  188.    push_spot ();
  189.    bol ();
  190.    skip_white ();
  191.    if (looking_at(Fortran_Continue_Char)) pop_spot ();
  192.    else
  193.      {
  194.     insert (Fortran_Continue_Char);
  195.     pop_spot ();
  196.     fortran_indent ();
  197.     go_right(1);
  198.     skip_white ();
  199.      }
  200. }
  201.  
  202. %
  203. %   electric labels
  204. %
  205. define fortran_electric_label ()
  206. {
  207.    variable ch, col;
  208.    insert_char (LAST_CHAR);
  209.    push_spot ();
  210.    bol (); skip_white (); % test for comment
  211.    if (bolp ()) pop_spot ();
  212.    else
  213.      {
  214.     skip_chars ("0-9"); trim ();
  215.     pop_spot ();
  216.     fortran_indent ();
  217.      }
  218. }
  219.  
  220.  
  221. % fortran comment/uncomment functions
  222.  
  223. define fortran_uncomment ()
  224. {
  225.    push_spot ();
  226.    if (fortran_is_comment ())
  227.      {
  228.     bol ();
  229.     if (looking_at (Fortran_Comment_String)) 
  230.       deln (strlen (Fortran_Comment_String));
  231.     else del ();
  232.      }
  233.    
  234.    fortran_indent ();
  235.    pop_spot ();
  236.    go_down (1);
  237. }
  238.  
  239. define fortran_comment ()
  240. {
  241.    !if (fortran_is_comment ())
  242.      {
  243.     push_spot ();
  244.     bol ();
  245.     insert (Fortran_Comment_String);
  246.      }
  247.    pop_spot ();
  248.    go_down(1);
  249. }
  250.  
  251.  
  252. %
  253. % Look for beginning of current subroutine/function
  254. %
  255. define fortran_beg_of_subprogram ()
  256. {
  257.    variable cas = CASE_SEARCH;
  258.  
  259.    CASE_SEARCH = 0;
  260.    do
  261.      {
  262.     bol (); skip_white ();
  263.     if (POINT)
  264.       {
  265.          if (looking_at ("program")
  266.          or looking_at ("function")
  267.          or looking_at ("subroutine")) break;
  268.       }
  269.      }
  270.    while (up(1));
  271.    CASE_SEARCH = cas;
  272. }
  273.  
  274. %
  275. % Look for end of current subroutine/function
  276. %
  277. define fortran_end_of_subprogram ()
  278. {
  279.    variable cas = CASE_SEARCH;
  280.    CASE_SEARCH = 0;
  281.    
  282.    do 
  283.      {
  284.     bol (); skip_white ();
  285.     if (looking_at ("end"))
  286.       {
  287.          go_right (3);
  288.          skip_white (); 
  289.          if (eolp ()) break;
  290.       }
  291.      }
  292.    while (down(1));
  293.    CASE_SEARCH = cas;                       %  restore search mode
  294. }
  295.  
  296. %
  297. % shows a ruler for FORTRAN source. Press any key to get rid of
  298. %
  299. define fortran_ruler ()
  300. {
  301.    variable c = what_column ();
  302.    variable r = window_line ();
  303.    
  304.    bol ();
  305.    push_mark ();
  306.    insert ("    5 7 10   15   20   25   30   35   40   45   50   55   60   65   70\n");
  307.    insert ("{    }|{ |    |    |    |    |    |    |    |    |    |    |    |    | }\n");
  308.    
  309.    goto_column (c);
  310.    if (r <= 2) r = 3;
  311.    recenter (r);
  312.    message ("Press SPACE to get rid of the ruler.");
  313.    update (1);
  314.    getkey (); pop ();
  315.    bol ();
  316.    del_region ();
  317.    goto_column (c);
  318.    flush_input ();
  319.    recenter (r);
  320. }
  321.  
  322. define fortran_prev_next_statement (dirfun)
  323. {
  324.    while (dirfun(1))
  325.      {
  326.     bol ();
  327.     skip_chars ("^0-9 \t");
  328.     !if (POINT) break;
  329.      }
  330.     goto_column_best_try (7); pop ();
  331. }
  332. %
  333. % moves cursor to the next statement, skipping comment lines
  334. %
  335. define fortran_next_statement ()
  336. {
  337.    fortran_prev_next_statement (&down);
  338. }
  339.  
  340.  
  341. %
  342. % moves cursor to the previous fortran statement, skipping comments
  343. %
  344. define fortran_previous_statement ()
  345. {
  346.    fortran_prev_next_statement (&up);
  347. }
  348.  
  349.  
  350. %
  351. % main entry point into the fortran mode
  352. %
  353.  
  354. !if (is_defined("Fortran_Mode"))
  355. {
  356.    variable Fortran_Mode = "Fortran";
  357.    make_keymap (Fortran_Mode);
  358.    definekey ("fortran_newline", "^M",  Fortran_Mode);
  359.    definekey ("fortran_indent",    "^I",    Fortran_Mode);
  360.    definekey ("fortran_comment",    "^[;",    Fortran_Mode);
  361.    definekey ("fortran_uncomment",    "^[:",    Fortran_Mode);
  362.    definekey ("fortran_continue_newline",    "^[^M",    Fortran_Mode);
  363.    definekey ("self_insert_cmd",    "'",    Fortran_Mode);
  364.    definekey ("self_insert_cmd",    "\"",    Fortran_Mode);
  365.    definekey ("fortran_beg_of_subprogram", "^[^A", Fortran_Mode);
  366.    definekey ("fortran_end_of_subprogram", "^[^E", Fortran_Mode);
  367.    definekey ("fortran_ruler", "^C^R", Fortran_Mode);
  368.    definekey ("fortran_next_statement", "^C^N", Fortran_Mode);
  369.    definekey ("fortran_previous_statement", "^C^P", Fortran_Mode);
  370.    _for (0, 9, 1)
  371.      {
  372.     =$1;
  373.     definekey ("fortran_electric_label", string($1), Fortran_Mode);
  374.      }
  375. }
  376.  
  377. %!% Mode designed for the purpose of editing FORTRAN files.
  378. %!% After the mode is loaded, the hook 'fortran_hook' is called.
  379. %!% Useful functions include
  380. %!% 
  381. %!%  Function:                    Default Binding:
  382. %!%   fortran_indent                    TAB
  383. %!%   fortran_newline                   RETURN  
  384. %!%     indents current line, inserts newline and indents it.
  385. %!%   fortran_continue_newline          ESC RETURN
  386. %!%     indents current line, and creates a continuation line on next line.
  387. %!%   fortran_comment                   ESC ;
  388. %!%     comments out current line
  389. %!%   fortran_uncomment                 ESC :
  390. %!%     uncomments current line
  391. %!%   fortran_electric_label            0-9
  392. %!%     Generates a label for current line or simply inserts a digit.
  393. %!%   fortran_next_statement            ^C^N
  394. %!%     moves to next fortran statementm skips comment lines
  395. %!%   fortran_previous_statement        ^C^P
  396. %!%     moves to previous fortran statement, skips comment lines
  397. %!%   fortran_ruler                     ^C^R
  398. %!%     inserts a ruler above the current line. Press any key to continue
  399. %!%   fortran_beg_of_subprogram         ESC ^A
  400. %!%     moves cursor to beginning of current subroutine/function
  401. %!%   fortran_end_of_subprogram         ESC ^E
  402. %!%     moves cursor to end of current subroutine/function
  403. %!%  
  404. %!% Variables include:
  405. %!%   Fortran_Continue_Char   --- character used as a continuation character.  
  406. %!%     By default, its value is "&"
  407. %!%   Fortran_Comment_String  --- string used by 'fortran_comment' to 
  408. %!%     comment out a line.  The default string is "C ";
  409. %!%   Fortran_Indent_Amount   --- number of spaces to indent statements in 
  410. %!%                               a block.  The default is 2.
  411. define fortran_mode ()
  412. {
  413.    use_keymap (Fortran_Mode);
  414.    setmode (Fortran_Mode, 0x4 | 0x10);
  415.    runhooks ("fortran_hook");
  416. }
  417.