home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ME22-OS2.ZIP / MACROS.ZIP / ABBREV.M < prev    next >
Text File  |  1987-09-14  |  7KB  |  219 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.  
  34. #define ALT_A  158
  35. #define ALT_E  146
  36. #define ALT_I  151
  37.  
  38. int  CurrAbbrevBuf;
  39. int  CurrAbbrevLine;
  40.  
  41. init()
  42. {
  43.   assign_key("load_abbrev",   ALT_A);   /* ALT A creates/loads an abbrev */
  44.   assign_key("expand_abbrev", ' ');     /* space expands an abbrev    */
  45.   assign_key("insert_abbrev", ALT_I);   /* ALT I inserts a new abbrev */
  46.   load_abbrev();
  47. }
  48.  
  49.  
  50. /* expand_abbrev - expands an abbrev when you press ALT E. We assume that */
  51. /*                 the cursor is just to the right of the abbrev to expand*/
  52. expand_abbrev()
  53. {
  54.   string abbrev,
  55.          defn;
  56.  
  57.   insert(" ");    
  58.   /* back up and extract the abbrev from the text */
  59.   origcol = currcol();
  60.   origrow = currlinenum();
  61.   prevword();         
  62.   if (origrow != currlinenum())  {
  63.         goline(origrow);
  64.         setcol(origcol);
  65.         return;
  66.   }
  67.   startcol = currcol();
  68.   
  69.   abbrev = substr(currline(), startcol, origcol - startcol - 1);
  70.   defn   = search_abbrev(abbrev);
  71.   
  72.   if (defn != "")       /* it was found */
  73.   {
  74.     delword();          /* get rid of the abbrev */
  75.     insert(defn);       /* and replace it with the expansion */
  76.   }
  77.   else                  /* abbrev not found */
  78.   {
  79.     goline(origrow);
  80.     setcol(origcol);    /* restore the cursor */
  81. /*    bell();   */
  82.   }
  83. }
  84.  
  85. /* search_abbrev - searches the current abbrev buffer for the requested */
  86. /*                 abbrev. Return the NULL string if not found.         */
  87. search_abbrev(candidate)
  88.   string candidate;
  89. {
  90.   /* Go to the start of the abbrev buffer */
  91.   oldbuf = currbuf();
  92.   setcurrbuf(CurrAbbrevBuf);
  93.   gobof();
  94.  
  95.   if (fsearch(pattern = sprintf("^%s ", candidate)) > 0)    /* found */
  96.   {
  97.     CurrAbbrevLine = currlinenum();
  98.     defn = substr(currline(), strlen(pattern), 128);
  99.     setcurrbuf(oldbuf);
  100.     return defn;
  101.   }
  102.   else          /* not found */
  103.   {
  104.     CurrAbbrevLine = 0;
  105.     setcurrbuf(oldbuf);
  106.     return "";
  107.   }
  108. }
  109.  
  110.  
  111. /* insert_abbrev - inserts an abbrev and the expansion in the current */
  112. /*                 abbrev buffer.                                     */
  113. insert_abbrev()
  114. {
  115.   string abbrev, defn;
  116.   
  117.   if ((abbrev = get_tty_str("Abbrev to insert : ")) == "")
  118.     return;
  119.  
  120.   /* Search the abbrev buffer for a duplicate entry */
  121.   search_abbrev(abbrev);
  122.   if (CurrAbbrevLine != 0)
  123.   {
  124.      c = get_tty_str("Abbrev already exists - replace it? (Y/N)");
  125.      if (c != "y" && c != "Y")
  126.        return;
  127.   }
  128.  
  129.   /* Prompt the user for the expansion */
  130.   if ((defn = get_tty_str("Expansion : ")) == "")
  131.     return;
  132.   
  133.   oldbuf = currbuf();
  134.   setcurrbuf(CurrAbbrevBuf);
  135.  
  136.   if (CurrAbbrevLine == 0)      /* inserting a new abbrev at the eof */
  137.   {
  138.     goeof();
  139.     if (currline() != "")
  140.       insert("\n");
  141.     insert(sprintf("%s %s", abbrev, defn));
  142.   }
  143.   else                          /* replacing an existing entry */
  144.   {
  145.     goline(CurrAbbrevLine);
  146.     deleol();
  147.     insert(sprintf("%s %s", abbrev, defn));
  148.   }
  149.  
  150.   setcurrbuf(oldbuf);
  151. }
  152.  
  153. /* delete_abbrev - deletes an abbrev from the abbrev buffer */
  154. delete_abbrev()
  155. {
  156.   string abbrev;
  157.   
  158.   if ((abbrev = get_tty_str("Abbrev to delete : ")) == "")
  159.     return;
  160.   search_abbrev(abbrev);
  161.   if (CurrAbbrevLine == 0)      /* Abbrev not found */
  162.   {
  163.     bell();
  164.     get_tty_str("The abbrev wasn't found");
  165.   }
  166.   else                          /* The abbrev was found */
  167.   {
  168.     oldbuf = currbuf();         /* save the curr buffer id */
  169.     setcurrbuf(CurrAbbrevBuf);  /* go to the abbrev buffer */
  170.     goline(CurrAbbrevLine);     /* and the delete the line */
  171.     delline();
  172.     setcurrbuf(oldbuf);
  173.   }
  174. }
  175.  
  176. /************************* I/O OPERATIONS ***********************/
  177.  
  178. /* load_abbrev - creates a new abbrev buffer, or loads in an existing one.*/
  179. /* Abbrev files will have the default extension of ABV. */
  180. load_abbrev()
  181. {
  182.   string fname;
  183.   
  184.   if ((fname = get_tty_str("Abbrev file : ")) == "")    /* prompt the user */
  185.     return;
  186.   if (!index(fname, "."))            /* User didn't supply an extension */
  187.     fname = strcat(fname, ".ABV");   /* Add the ABV extension           */
  188.  
  189.   oldbuf = currbuf();                /* save the current buffer id      */
  190.  
  191.   setcurrbuf(abuf = create_buffer(fname));      /* create/load the buffer */
  192.   if (currline() == "")                         /* a new buffer? */
  193.     get_tty_str("New abbrev buffer");
  194.   else
  195.     get_tty_str("Abbrev file loaded");
  196.   CurrAbbrevBuf = abuf;
  197.  
  198.   setcurrbuf(oldbuf);                 /* Restore the original editing buffer */
  199. }
  200.  
  201.  
  202. /* write_abbrev - writes the current abbrev buffer to a file */
  203. write_abbrev()
  204. {
  205.   string fname;
  206.  
  207.   oldbuf = currbuf();
  208.  
  209.   /* Get the current abbrev buffer's name, maybe concat */
  210.   /* the ABV extension and dump it to a file.           */
  211.   setcurrbuf(CurrAbbrevBuf);
  212.   fname = filename();
  213.   if (!index(fname, "."))
  214.     fname = strcat(fname, ".ABV");
  215.   writefile(fname);
  216.  
  217.   setcurrbuf(oldbuf);
  218. }
  219.