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

  1. %
  2. % dabbrev.sl - Author: Adrian Savage (A.F.Savage@bradford.ac.uk)
  3. %              Date:   May 1994
  4. %
  5. % This file may be freely distributed and modified
  6. %
  7. % find the previous match for the current word and add it to the current word
  8.  
  9. !if (is_defined("Dabbrev_Num_Matches"))
  10. {
  11.    variable Dabbrev_Num_Matches = 0;
  12.    variable Dabbrev_Last_Matches = create_array ('s', 10, 1);
  13. }
  14.  
  15.  
  16. define dabbrev_is_already_matched (str, old)
  17. {
  18.    variable i;
  19.    !if (strcmp(str, old)) return 1;
  20.  
  21.    _for (0, Dabbrev_Num_Matches - 1, 1)
  22.      {
  23.     =i;
  24.     !if (strcmp (str, Dabbrev_Last_Matches[i])) return 1;
  25.      }
  26.    if (Dabbrev_Num_Matches == 10) return 0;
  27.    
  28.    Dabbrev_Last_Matches [Dabbrev_Num_Matches] = str;
  29.    Dabbrev_Num_Matches++;
  30.    return 0;
  31. }
  32.  
  33.  
  34. define dabbrev_skip_word_chars (dir)
  35. {
  36.    if (dir > 0)
  37.      {
  38.     do 
  39.       {
  40.          skip_chars ("_");
  41.          skip_word_chars ();
  42.       }
  43.     while (looking_at_char ('_'));
  44.     return;
  45.      }
  46.   
  47.    forever
  48.      {
  49.     bskip_word_chars ();
  50.     if (bolp()) break;
  51.     go_left(1);
  52.     !if (looking_at_char ('_'))
  53.       {
  54.          go_right(1);
  55.          break;
  56.       }
  57.      }
  58. }
  59.  
  60. define dabbrev_expand_dir (dir)
  61. {
  62.    variable hilite_wrd, num_spots = 0, num_marks = 0, ndel, 
  63.      complete, fun, use_call, success = 0, ch, search_fun;
  64.    
  65.    ERROR_BLOCK 
  66.      {
  67.     loop (num_spots) pop_spot ();
  68.     loop (num_marks) pop_mark (1);
  69.     !if (success)
  70.       {
  71.          push_mark ();
  72.          dabbrev_skip_word_chars(-1);
  73.          del_region ();
  74.          insert (hilite_wrd);
  75.       }
  76.      }
  77.    
  78.    
  79.    push_mark ();  num_marks++;           % remember initial position
  80.    
  81.    push_mark();                   % mark end of this word  
  82.    dabbrev_skip_word_chars(-1);           % jump to beginning of word    
  83.    hilite_wrd = bufsubstr();           % get the word  
  84.    push_mark ();  num_marks++;           % remember beginning position
  85.  
  86.    ndel = strlen (hilite_wrd);
  87.    
  88.    search_fun = &bsearch;
  89.    if (dir) 
  90.      {
  91.     search_fun = &fsearch;
  92.     go_right (ndel);
  93.      }
  94.    
  95.    
  96.    forever 
  97.      {
  98.     !if (search_fun (hilite_wrd))
  99.       {
  100.          error (strcat ("No more completions for ", hilite_wrd));
  101.       }
  102.     
  103.     
  104.     dabbrev_skip_word_chars (-1);
  105.     !if (looking_at(hilite_wrd)) 
  106.       {
  107.          if (dir) dabbrev_skip_word_chars (1);
  108.          continue;
  109.       }
  110.     
  111.     push_mark ();               % set a mark there  
  112.     dabbrev_skip_word_chars(1);    % skip to end of    word      
  113.     complete = bufsubstr();           % get the completed word
  114.     
  115.     !if (dir) dabbrev_skip_word_chars (-1);
  116.     if (dabbrev_is_already_matched(complete, hilite_wrd)) continue;
  117.     
  118.     push_spot (); num_spots++;     %  remember how far we got
  119.     
  120.     pop_mark(1);               %  back to beginning
  121.     push_mark;
  122.     
  123.     insert (complete);
  124.     deln (ndel);               %  delete word
  125.     ndel = strlen (complete);
  126.     
  127.     update (1);               %  force update
  128.     
  129.     ch = getkey (); ungetkey (ch);
  130.     
  131.     fun = get_key_function ();
  132.     if (strlen (fun))
  133.       {
  134.          use_call = ();
  135.       }
  136.  
  137.     if (strcmp (fun, "dabbrev")) break;   %  use hit another key, done
  138.     variable zzzz = create_user_mark ();
  139.     pop_spot (); num_spots--;      %  returns and continue looking
  140.      }
  141.    
  142.    % we get here only when we are finished
  143.    success = 1;
  144.    
  145.    EXECUTE_ERROR_BLOCK;               %  pop spots/marks
  146.    
  147.    update (0);
  148.    !if (strlen (fun)) return;
  149.    if (use_call) call(fun); else eval(fun);
  150. }
  151.  
  152. define dabbrev ()
  153. {
  154.    Dabbrev_Num_Matches = 0;
  155.    ERROR_BLOCK
  156.      {
  157.     _clear_error ();
  158.     dabbrev_expand_dir (1);
  159.      }
  160.    dabbrev_expand_dir (0);
  161. }
  162.