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

  1. /*
  2.  │   File: pasabbrv.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: Modified to work with both v1.00 and v1.02. Also fixed 
  9.  │                  overwrite mode problems with some of the templates.
  10.  */
  11.  
  12. #define ID      " Pascal 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 or requiring special handling
  42. */
  43.  
  44. char *abbrevList1="~IMPLEMENTATION ~INTERFACE ~VAR ~TYPE ~BEGIN ~END~REPEAT~FUNCTION~";
  45.  
  46.  
  47.  
  48. /*
  49.    List 2 contains expressions requiring the cursor to move left one space
  50.    when expanded
  51. */
  52.  
  53. char *abbrevList2="~PROCEDURE ;~UNIT ;~PROGRAM ;~";
  54.  
  55. /*
  56.    List 3 contains expressions requiring the cursor to move left one word
  57.    when expanded
  58. */
  59.  
  60. char *abbrevList3="~ELSE~EI~ELSE IF  THEN~IF  THEN~FOR  DO~WHILE  DO~CASE  OF~WITH  DO~";
  61.  
  62. char *   pascal ltrim(char *str);
  63. char *   pascal rtrim(char *str);
  64. char *   pascal cpyExp(char *szDst,char *szExp);
  65. void     pascal writeString(char *string,COL x,LINE y,PFILE pf,int insflg);
  66. flagType pascal EXTERNAL PasAbbrev(unsigned int argData, ARG far *pArg, flagType fMeta);
  67. void     pascal id (char *pszFcn);
  68. int             WhenLoaded (void);
  69.  
  70.  
  71. /*────────────────────────────────────────────────────────────────────────*/
  72.  
  73.  
  74. char * pascal ltrim(char *str)
  75. {
  76.    /*
  77.     │   Desc: Removes whitespace from the start of a string
  78.     │    Pre: Input string
  79.     │   Post: Removes whitespace and returns pointer to string for convenience
  80.     │History:
  81.     │ 21-Jan-1990 JVV: Created
  82.     */
  83.  
  84.    char *pTmp;
  85.  
  86.    pTmp = str;
  87.    while (*pTmp == ' ' || *pTmp == '\t')
  88.       pTmp++;
  89.  
  90.    strcpy(str,pTmp);
  91.    return(str);
  92. }
  93.  
  94.  
  95. /*────────────────────────────────────────────────────────────────────────*/
  96.  
  97.  
  98.  
  99. char * pascal rtrim(char *str)
  100. {
  101.    /*
  102.     │   Desc: Removes whitespace from the end of a string
  103.     │    Pre: Input string
  104.     │   Post: Removes whitespace and returns pointer to string for convenience
  105.     │History:
  106.     │ 21-Jan-1990 JVV: Created
  107.     */
  108.  
  109.    char *pTmp;
  110.  
  111.    pTmp = str+strlen(str)-1;
  112.  
  113.    while (pTmp >= str && (*pTmp == ' ' || *pTmp == '\t'))
  114.       pTmp--;
  115.    pTmp++;
  116.    *pTmp = '\0';
  117.  
  118.    return(str);
  119. }
  120.  
  121. /*────────────────────────────────────────────────────────────────────────*/
  122.  
  123.  
  124. void pascal writeString(char *string,COL x,LINE y,PFILE pf,int insflg)
  125. {
  126.    /*
  127.     │   Desc: Writes a string into a file with or without insertion
  128.     │    Pre: Input string, row and column destination, a handle to the target
  129.     │         file, and a flag to determine whether to insert or overwrite
  130.     │   Post: Returns nothing. String placed into file
  131.     │History:
  132.     │ 21-Jan-1990 JVV: Created
  133.     */
  134.  
  135.     cptr = string;
  136.     while (*cptr)
  137.         Replace(*cptr++,x++,y,pf,insflg);
  138.     MoveCur(x,y);
  139. }
  140.  
  141. /*────────────────────────────────────────────────────────────────────────*/
  142.  
  143. char * pascal cpyExp(char *szDst,char *szExp)
  144. {
  145.    /*
  146.     │   Desc: Extracts an expression from a string of expressions delimited by
  147.     │         '~'
  148.     │    Pre: A pointer to a destination buffer for the results and a
  149.     │         pointer to the start of the desired expression.
  150.     │   Post: Destination buffer contains the desired expression. Returns
  151.     │         pointer to the destination buffer
  152.     │History:
  153.     │ 21-Jan-1990 JVV: Created
  154.    */
  155.  
  156.    char *pTmp;
  157.  
  158.    pTmp = szDst;
  159.  
  160.    do {
  161.       *pTmp++ = *szExp++;
  162.    } while(*szExp != '~');
  163.  
  164.    *pTmp = '\0';
  165.  
  166.    return(szDst);
  167. }
  168.  
  169.  
  170.  
  171. /*────────────────────────────────────────────────────────────────────────*/
  172.  
  173.  
  174.  
  175. flagType pascal EXTERNAL PasAbbrev(unsigned int argData, ARG far *pArg, flagType fMeta)
  176. {
  177.    /*
  178.     │   Desc: Recognizes abbreviations of C expressions on the current line
  179.     │         and expands them to full C expressions.
  180.     │    Pre: valid for NOARG
  181.     │   Post: Expanded C expression in file if recognized as a abbreviation.
  182.     │         Returns TRUE if expansion occured, FALSE if not.
  183.     │History:
  184.     │ 21-Jan-1990 JVV: Created
  185.     */
  186.  
  187.    PFILE curFile;
  188.    char  *pTmp1,*pTmp2;
  189.  
  190.    curFile = FileNameToHandle("",NULL);
  191.    xTmp = pArg->arg.noarg.x;
  192.    yTmp = pArg->arg.noarg.y;
  193.  
  194.    len = GetLine(yTmp,buf,curFile);
  195.    *tmpBuf = '\0';
  196.    ltrim(buf);
  197.    rtrim(buf);
  198.    strupr(buf);
  199.    if (*buf) {
  200.       strcpy(tmpBuf,"~");
  201.       strcat(tmpBuf,buf);
  202.    }
  203.  
  204.    if((pTmp1 = strstr(abbrevList3,tmpBuf)) != NULL) {
  205.       pTmp1++;
  206.  
  207.       cpyExp(tmpBuf,pTmp1);
  208.  
  209.       fExecute("BegLine");
  210.       GetCursor(&xTmp,&yTmp);
  211.  
  212.       if (!strcmp(tmpBuf,"ELSE")) {
  213.          writeString("ELSE",xTmp,yTmp,curFile,FALSE);
  214.          fExecute("begline down linsert tab");
  215.          return(TRUE);
  216.       }
  217.  
  218.       if (!strcmp(tmpBuf,"EI"))
  219.          writeString("ELSE IF  THEN",xTmp,yTmp,curFile,FALSE);
  220.       else
  221.          writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
  222.  
  223.       fExecute("mword left");
  224.  
  225.       return(TRUE);
  226.    }
  227.  
  228.    if ( (pTmp1 = strstr(abbrevList1,tmpBuf)) != NULL) {
  229.       pTmp1++;
  230.  
  231.       cpyExp(tmpBuf,pTmp1);
  232.  
  233.       MoveCur(xTmp,yTmp);
  234.       fExecute("BegLine");
  235.       GetCursor(&xTmp,&yTmp);
  236.  
  237.       if (!strcmp(tmpBuf,"BEGIN ")) {
  238.          writeString("BEGIN",xTmp,yTmp,curFile,FALSE);
  239.          fExecute("begline down linsert");
  240.          GetCursor(&xTmp,&yTmp);
  241.          writeString("END;",xTmp,yTmp,curFile,FALSE);
  242.          fExecute("begline linsert tab");
  243.       }
  244.       else if (!strcmp(tmpBuf,"REPEAT")) {
  245.          writeString("REPEAT",xTmp,yTmp,curFile,FALSE);
  246.          fExecute("begline down linsert");
  247.          GetCursor(&xTmp,&yTmp);
  248.          writeString("UNTIL ;",xTmp,yTmp,curFile,FALSE);
  249.          fExecute("begline linsert tab");
  250.       }
  251.       else if (!strcmp(tmpBuf,"FUNCTION")) {
  252.          writeString("FUNCTION :;",xTmp,yTmp,curFile,FALSE);
  253.          GetCursor(&xTmp,&yTmp);
  254.          MoveCur(xTmp-2,yTmp);
  255.       }
  256.       else {
  257.          writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
  258.          fExecute("begline down linsert tab");
  259.       }
  260.  
  261.       return(TRUE);
  262.    }
  263.  
  264.    if( (pTmp1 = strstr(abbrevList2,tmpBuf)) != NULL) {
  265.       pTmp1++;
  266.  
  267.       cpyExp(tmpBuf,pTmp1);
  268.  
  269.       fExecute("BegLine");
  270.       GetCursor(&xTmp,&yTmp);
  271.       writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
  272.  
  273.       GetCursor(&xTmp,&yTmp);
  274.       MoveCur(xTmp-1,yTmp);
  275.       return (TRUE);
  276.    }
  277.  
  278.    return(FALSE);
  279. }
  280.  
  281. /*────────────────────────────────────────────────────────────────────────*/
  282.  
  283. int WhenLoaded ()
  284. {
  285.    /*
  286.     │   Desc: Initialization function. Only displays id string and assigns key.
  287.     │    Pre: None
  288.     │   Post: Returns nothing. Assigns default key assignment
  289.     │History:
  290.     │ 21-Jan-1990 JVV: Created
  291.     */
  292.  
  293.    id("");
  294.    SetKey("pasabbrev","Alt+Tab");
  295.    return (TRUE);
  296. }
  297.  
  298. /*────────────────────────────────────────────────────────────────────────*/
  299.  
  300. void pascal id (char *pszFcn)
  301. {
  302.    /*
  303.     │   Desc: Displays id string on message line
  304.     │    Pre: Prefix string for message and ID suffix string
  305.     │   Post: Returns nothing. id string displayed
  306.     │History:
  307.     │ ??-???-?? MSoft: Borrowed from Microsoft Extension example
  308.     */
  309.  
  310.     char    buf[80];                                /* message buffer       */
  311.  
  312.     strcpy (buf,pszFcn);                            /* start with message   */
  313.     strcat (buf,ID);                                /* append version       */
  314.     DoMessage (buf);
  315. }
  316.  
  317.  
  318. /*────────────────────────────────────────────────────────────────────────*/
  319.  
  320. /*
  321.  │ Switch communication table to the editor.
  322.  │ This extension defines no switches.
  323.  │
  324.  */
  325.  
  326. struct swiDesc  swiTable[] = {
  327.     {0, 0, 0}
  328. };
  329.  
  330. /*────────────────────────────────────────────────────────────────────────*/
  331.  
  332. /*
  333.  │ Command communication table to the editor.
  334.  │ Defines the name, location and acceptable argument types.
  335.  │
  336.  */
  337. struct cmdDesc    cmdTable[] = {
  338.     {"pasabbrev",PasAbbrev,0, NOARG|MODIFIES },
  339.     {0, 0, 0}
  340. };
  341.