home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / MABBREV.ZIP / CABBREV.C < prev    next >
C/C++ Source or Header  |  1990-01-26  |  7KB  |  297 lines

  1. /*
  2.  │   File: cabbrev.c
  3.  │   Proj: M Editor Extension
  4.  │ Design: Joseph Van Valen
  5.  │   Desc: Creates C templates based on contents of line
  6.  │History:
  7.  │ 20-Jan-1990 JVV: Created
  8.  │ 26-Jan-1990 JVV: Modifed to work with both version 1.0 and 1.02. Also
  9.  │                  fixed "do..while()" to work in overstrike mode.
  10.  */
  11.  
  12. #define ID      " C Abbreviations ver 1.01 "##__DATE__
  13.  
  14.  
  15. #include <string.h>
  16. #include "ext.h"
  17.  
  18. #define TRUE    -1
  19. #define FALSE    0
  20. #define NULL   ( (void *) 0)
  21.  
  22. #ifndef MODIFIES
  23. #define MODIFIES  0x1000
  24. #endif
  25.  
  26. /* useful variables and declarations */
  27.  
  28. char *cptr;
  29. char *lineBuf;
  30. char tmpBuf[20];
  31. char buf[80];
  32. COL  xTmp;
  33. LINE yTmp;
  34. COL  hold_col;
  35. LINE hold_row;
  36. int  len;
  37.  
  38.  
  39. /*
  40.    List 1 contains expressions requiring no additional cursor movements when
  41.    expanded
  42. */
  43.  
  44. char *abbrevList1="~return ~break;~do~default:~else ~continue;~#if ~#define ~#include ~#ifdef ~#ifndef ~#endif";
  45.  
  46.  
  47.  
  48. /*
  49.    List 1 contains expressions requiring the cursor to move left one space
  50.    when expanded
  51. */
  52.  
  53. char *abbrevList2="~ei~else if ()~if ()~while ()~for ()~switch ()~case :~";
  54.  
  55. char *   pascal ltrim(char *str);
  56. char *   pascal rtrim(char *str);
  57. char *   pascal cpyExp(char *szDst,char *szExp);
  58. void     pascal writeString(char *string,COL x,LINE y,PFILE pf,int insflg);
  59. flagType pascal EXTERNAL CAbbrev(unsigned int argData, ARG far *pArg, flagType fMeta);
  60. void     pascal id (char *pszFcn);
  61. int      WhenLoaded (void);
  62.  
  63.  
  64. /*────────────────────────────────────────────────────────────────────────*/
  65.  
  66.  
  67. char * pascal ltrim(char *str)
  68. {
  69.    /*
  70.     │   Desc: Removes whitespace from the start of a string
  71.     │    Pre: Input string
  72.     │   Post: Removes whitespace and returns pointer to string for convenience
  73.     │History:
  74.     │ 21-Jan-1990 JVV: Created
  75.     */
  76.  
  77.    char *pTmp;
  78.  
  79.    pTmp = str;
  80.    while (*pTmp == ' ' || *pTmp == '\t')
  81.       pTmp++;
  82.  
  83.    strcpy(str,pTmp);
  84.    return(str);
  85. }
  86.  
  87.  
  88. /*────────────────────────────────────────────────────────────────────────*/
  89.  
  90.  
  91.  
  92. char * pascal rtrim(char *str)
  93. {
  94.    /*
  95.     │   Desc: Removes whitespace from the end of a string
  96.     │    Pre: Input string
  97.     │   Post: Removes whitespace and returns pointer to string for convenience
  98.     │History:
  99.     │ 21-Jan-1990 JVV: Created
  100.     */
  101.  
  102.    char *pTmp;
  103.  
  104.    pTmp = str+strlen(str)-1;
  105.  
  106.    while (pTmp >= str && (*pTmp == ' ' || *pTmp == '\t'))
  107.       pTmp--;
  108.    pTmp++;
  109.    *pTmp = '\0';
  110.  
  111.    return(str);
  112. }
  113.  
  114. /*────────────────────────────────────────────────────────────────────────*/
  115.  
  116.  
  117. void pascal writeString(char *string,COL x,LINE y,PFILE pf,int insflg)
  118. {
  119.    /*
  120.     │   Desc: Writes a string into a file with or without insertion
  121.     │    Pre: Input string, row and column destination, a handle to the target
  122.     │         file, and a flag to determine whether to insert or overwrite
  123.     │   Post: Returns nothing. String placed into file
  124.     │History:
  125.     │ 21-Jan-1990 JVV: Created
  126.     */
  127.  
  128.     cptr = string;
  129.     while (*cptr)
  130.         Replace(*cptr++,x++,y,pf,insflg);
  131.     MoveCur(x,y);
  132. }
  133.  
  134. /*────────────────────────────────────────────────────────────────────────*/
  135.  
  136. char * pascal cpyExp(char *szDst,char *szExp)
  137. {
  138.    /*
  139.     │   Desc: Extracts an expression from a string of expressions delimited by
  140.     │         '~'
  141.     │    Pre: A pointer to a destination buffer for the results and a
  142.     │         pointer to the start of the desired expression.
  143.     │   Post: Destination buffer contains the desired expression. Returns
  144.     │         pointer to the destination buffer
  145.     │History:
  146.     │ 21-Jan-1990 JVV: Created
  147.    */
  148.  
  149.    char *pTmp;
  150.  
  151.    pTmp = szDst;
  152.  
  153.    do {
  154.       *pTmp++ = *szExp++;
  155.    } while(*szExp != '~');
  156.  
  157.    *pTmp = '\0';
  158.  
  159.    return(szDst);
  160. }
  161.  
  162.  
  163.  
  164. /*────────────────────────────────────────────────────────────────────────*/
  165.  
  166.  
  167.  
  168. flagType pascal EXTERNAL CAbbrev(unsigned int argData, ARG far *pArg, flagType fMeta)
  169. {
  170.    /*
  171.     │   Desc: Recognizes abbreviations of C expressions on the current line
  172.     │         and expands them to full C expressions.
  173.     │    Pre: valid for NOARG
  174.     │   Post: Expanded C expression in file if recognized as a abbreviation.
  175.     │         Returns TRUE if expansion occured, FALSE if not.
  176.     │History:
  177.     │ 21-Jan-1990 JVV: Created
  178.     */
  179.  
  180.    PFILE curFile;
  181.    char  *pTmp1,*pTmp2;
  182.  
  183.    curFile = FileNameToHandle("",NULL);
  184.    xTmp = pArg->arg.noarg.x;
  185.    yTmp = pArg->arg.noarg.y;
  186.  
  187.    len = GetLine(yTmp,buf,curFile);
  188.    *tmpBuf = '\0';
  189.    ltrim(buf);
  190.    rtrim(buf);
  191.    if (*buf) {
  192.       strcpy(tmpBuf,"~");
  193.       strcat(tmpBuf,buf);
  194.    }
  195.  
  196.    if ( (pTmp1 = strstr(abbrevList1,tmpBuf)) != NULL) {
  197.       pTmp1++;
  198.  
  199.       cpyExp(tmpBuf,pTmp1);
  200.  
  201.       MoveCur(xTmp,yTmp);
  202.       fExecute("BegLine");
  203.       GetCursor(&xTmp,&yTmp);
  204.  
  205.       if (!strcmp(tmpBuf,"do")) {
  206.          writeString("do {",xTmp,yTmp,curFile,FALSE);
  207.          fExecute("begline down linsert");
  208.          GetCursor(&xTmp,&yTmp);
  209.          writeString("} while ();",xTmp,yTmp,curFile,FALSE);
  210.          fExecute("begline linsert tab");
  211.       }
  212.       else
  213.          writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
  214.       return(TRUE);
  215.    }
  216.    else if( (pTmp1 = strstr(abbrevList2,tmpBuf)) != NULL) {
  217.       pTmp1++;
  218.  
  219.       cpyExp(tmpBuf,pTmp1);
  220.  
  221.       fExecute("BegLine");
  222.       GetCursor(&xTmp,&yTmp);
  223.  
  224.       if (!strcmp(tmpBuf,"ei"))
  225.          writeString("else if()",xTmp,yTmp,curFile,FALSE);
  226.       else
  227.          writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
  228.  
  229.       GetCursor(&xTmp,&yTmp);
  230.       MoveCur(xTmp-1,yTmp);
  231.       return(TRUE);
  232.    }
  233.    else
  234.       return(FALSE);
  235. }
  236.  
  237. /*────────────────────────────────────────────────────────────────────────*/
  238.  
  239. int WhenLoaded ()
  240. {
  241.    /*
  242.     │   Desc: Initialization function. Only displays id string and assigns key.
  243.     │    Pre: None
  244.     │   Post: Returns nothing. Assigns default key assignment
  245.     │History:
  246.     │ 21-Jan-1990 JVV: Created
  247.     */
  248.  
  249.    id("");
  250.    SetKey("cabbrev","Alt+Tab");
  251.    return(TRUE);
  252. }
  253.  
  254. /*────────────────────────────────────────────────────────────────────────*/
  255.  
  256. void pascal id (char *pszFcn)
  257. {
  258.    /*
  259.     │   Desc: Displays id string on message line
  260.     │    Pre: Prefix string for message and ID suffix string
  261.     │   Post: Returns nothing. id string displayed
  262.     │History:
  263.     │ ??-???-?? MSoft: Borrowed from Microsoft Extension example
  264.     */
  265.  
  266.     char    buf[80];                                /* message buffer       */
  267.  
  268.     strcpy (buf,pszFcn);                            /* start with message   */
  269.     strcat (buf,ID);                                /* append version       */
  270.     DoMessage (buf);
  271. }
  272.  
  273.  
  274. /*────────────────────────────────────────────────────────────────────────*/
  275.  
  276. /*
  277.  │ Switch communication table to the editor.
  278.  │ This extension defines no switches.
  279.  │
  280.  */
  281.  
  282. struct swiDesc  swiTable[] = {
  283.     {0, 0, 0}
  284. };
  285.  
  286. /*────────────────────────────────────────────────────────────────────────*/
  287.  
  288. /*
  289.  │ Command communication table to the editor.
  290.  │ Defines the name, location and acceptable argument types.
  291.  │
  292.  */
  293. struct cmdDesc    cmdTable[] = {
  294.     {"cabbrev",CAbbrev,0, NOARG|MODIFIES },
  295.     {0, 0, 0}
  296. };
  297.