home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ME22-OS2.ZIP / MACROS.ZIP / ABBREV2.M < prev    next >
Text File  |  1987-08-15  |  8KB  |  218 lines

  1. /*-----------------------------------------------------------------
  2. |                           ABBREV                                |
  3. | This macro sorta simulates the abbrev facility that was made    |
  4. | popular in EMACS, and was later commercialized even further in  |
  5. | packages like Productivity Plus (TM). It also can be considered |
  6. | to be a way to further enhance the editor's keyboard macro      |
  7. | facility.                                                       |
  8. |                                                                 |
  9. | The main idea is that we have a buffer where each line contains |
  10. | an abbreviation, a space, and the abbrev's expansion. When we   |
  11. | are editing text, we can type an abbrev, hit a function key, and|
  12. | have the abbrev expanded automatically. (Note - instead of a    |
  13. | function key, you might optionally program this macro to be     |
  14. | invoked when you hit the space bar.)                            |
  15. |                                                                 |
  16. | For instance, if you wanted the word "ii" to be an abbrev for   |
  17. | the phrase "Institute of Intermediate Ignoramuses", you could   |
  18. | enter this abbrev and expansion in the abbrev buffer. When      |
  19. | you typed "ii" and then the EXPAND ABBREV function key, "ii"    |
  20. | would be replaced by "Institute of ...".                        |
  21. |                                                                 |
  22. | This package allows you to create/load and write an abbrev      |
  23. | buffer. You may add new abbrevs and delete existing abbrevs.    |
  24. | Of course, you may want to add other functions which can operate|
  25. | of abbrevs. For this, you need the macro compiler.              |
  26. | The first extension which comes to mind is support for multiple |
  27. | abbrev buffers.                                                 |
  28. |                                                                 |
  29. | ENJOY!!!!                                                       |
  30. |                                                                 |
  31. | <<<< Marc Adler  7/87 >>>>                                      |
  32. |                                                                 |
  33. | ABBREV2                                                         |
  34. |   This version uses the ARRAY capabilities which are found      |
  35. | starting with version 1.3 of ME/NYEDIT.                         |
  36. |                                                                 |
  37. -----------------------------------------------------------------*/
  38.  
  39. #define ALT_A  158
  40. #define ALT_E  146
  41. #define ALT_I  151
  42.  
  43. #define MAXABBREVS  20          /* Change this for more abbrevs */
  44.  
  45. int    NumAbbrevs;              /* Current # of abbrevs defined */
  46. string Abbrevs[MAXABBREVS];     /* array of abbrevs - "abbrev expansion" */
  47. string AbbrevFilename;          /* Name of the abbrev array */
  48.  
  49. init()
  50. {
  51.   assign_key("load_abbrev",   ALT_A);   /* ALT A creates/loads an abbrev */
  52.   assign_key("expand_abbrev", ALT_E);   /* ALT E expands an abbrev    */
  53.   assign_key("insert_abbrev", ALT_I);   /* ALT I inserts a new abbrev */
  54. }
  55.  
  56.  
  57. /* expand_abbrev - expands an abbrev when you press ALT E. We assume that */
  58. /*                 the cursor is just to the right of the abbrev to expand*/
  59. expand_abbrev()
  60. {
  61.   string abbrev;
  62.  
  63.   /* back up and extract the abbrev from the text */
  64.   origcol = currcol();
  65.   prevword();
  66.   startcol = currcol();
  67.   
  68.   abbrev = substr(currline(), startcol, origcol - startcol);
  69.   i      = search_abbrev(abbrev);
  70.   
  71.   if (i)                /* it was found */
  72.   {
  73.     delword();          /* get rid of the abbrev */
  74.     insert(substr(Abbrevs[i], index(Abbrevs[i], " ")+1, 100));
  75.   }
  76.   else                  /* abbrev not found */
  77.   {
  78.     setcol(origcol);    /* restore the cursor */
  79.     bell();
  80.   }
  81. }
  82.  
  83. /* search_abbrev - searches the current abbrev buffer for the requested */
  84. /*                 abbrev. Return the NULL string if not found.         */
  85. search_abbrev(candidate)
  86.   string candidate;
  87. {
  88.   for (i = 1;  i <= NumAbbrevs;  i++)
  89.     if (substr(Abbrevs[i], 1, index(Abbrevs[i]," ") - 1) == candidate)
  90.       return i;
  91.   return 0;
  92. }
  93.  
  94. /* insert_abbrev - inserts an abbrev and the expansion in the current */
  95. /*                 abbrev buffer.                                     */
  96. insert_abbrev()
  97. {
  98.   string abbrev, defn;
  99.   
  100.   if ((abbrev = get_tty_str("Abbrev to insert : ")) == "")
  101.     return;
  102.  
  103.   /* Search the abbrev buffer for a duplicate entry */
  104.   if ((i = search_abbrev(abbrev)) > 0)
  105.   {
  106.      c = get_tty_str("Abbrev already exists - replace it? (Y/N)");
  107.      if (c != "y" && c != "Y")
  108.        return;
  109.   }
  110.  
  111.   /* Prompt the user for the expansion */
  112.   if ((defn = get_tty_str("Expansion : ")) == "")
  113.     return;
  114.   
  115.   if (i == 0)
  116.   {
  117.     NumAbbrevs++;
  118.     i = NumAbbrevs;
  119.   }
  120.   Abbrevs[i] = sprintf("%s %s", abbrev, defn);
  121. }
  122.  
  123. /* delete_abbrev - deletes an abbrev from the abbrev buffer */
  124. delete_abbrev()
  125. {
  126.   string abbrev;
  127.   
  128.   if ((abbrev = get_tty_str("Abbrev to delete : ")) == "")
  129.     return;
  130.   if ((i = search_abbrev(abbrev)) == 0)   /* Abbrev not found */
  131.   {
  132.     bell();
  133.     get_tty_str("The abbrev wasn't found");
  134.   }
  135.   else                          /* The abbrev was found */
  136.   {
  137.     while (i < NumAbbrevs)
  138.       Abbrevs[i] = Abbrevs[i+1];
  139.     NumAbbrevs--;
  140.     get_tty_str("The abbrev was deleted");
  141.   }
  142. }
  143.  
  144. /************************* I/O OPERATIONS ***********************/
  145.  
  146. /* load_abbrev - creates a new abbrev buffer, or loads in an existing one.*/
  147. /* Abbrev files will have the default extension of ABV. */
  148. load_abbrev()
  149. {
  150.   string fname;
  151.   
  152.   if ((fname = get_tty_str("Abbrev file : ")) == "")    /* prompt the user */
  153.     return;
  154.   if (!index(fname, "."))            /* User didn't supply an extension */
  155.     fname = strcat(fname, ".ABV");   /* Add the ABV extension           */
  156.   AbbrevFilename = fname;
  157.   NumAbbrevs = 0;
  158.  
  159.   oldbuf = currbuf();                /* save the current buffer id      */
  160.  
  161.   setcurrbuf(abuf = create_buffer(fname));      /* create/load the buffer */
  162.   if (currline() == "")                         /* a new buffer? */
  163.     get_tty_str("New abbrev buffer");
  164.   else
  165.   {
  166.     for (NumAbbrevs = 1;  currlinenum() < lastlinenum();  NumAbbrevs++)
  167.     {
  168.       Abbrevs[NumAbbrevs] = currline();
  169.       down();
  170.     }
  171.     Abbrevs[NumAbbrevs] = currline();
  172.     get_tty_str("Abbrev file loaded");
  173.   }
  174.  
  175.   delete_buffer(abuf);
  176.   setcurrbuf(oldbuf);                 /* Restore the original editing buffer */
  177. }
  178.  
  179.  
  180. /* write_abbrev - writes the current abbrev buffer to a file */
  181. write_abbrev()
  182. {
  183.   string fname;
  184.  
  185.   if (NumAbbrevs <= 0)
  186.   {
  187.     get_tty_str("There are no abbrevs defined");
  188.     return;
  189.   }
  190.  
  191.   oldbuf = currbuf();
  192.  
  193.   /* Get the current abbrev buffer's name, maybe concat */
  194.   /* the ABV extension and dump it to a file.           */
  195.   fname = AbbrevFilename;
  196.   if (!index(fname, "."))
  197.     fname = strcat(fname, ".ABV");
  198.  
  199.   setcurrbuf(abuf = create_buffer(fname));      /* create/load the buffer */
  200.   if (currline() != "")                         /* a new buffer? */
  201.   {
  202.     message("Overwrite the existing abbrev file? (y/N)");
  203.     if ((c = get_tty_char()) != 'y' && c != 'Y')
  204.       return;
  205.   }
  206.  
  207.   for (i = 1;  i <= NumAbbrevs;  i++)
  208.   {
  209.     insert(Abbrevs[i]);
  210.     insert("\n");
  211.   }
  212.  
  213.   writefile(fname);
  214.  
  215.   delete_buffer(abuf);          /* remove the temp buffer */
  216.   setcurrbuf(oldbuf);           /* return to our current buffer */
  217. }
  218.