home *** CD-ROM | disk | FTP | other *** search
- /*
- │ File: cabbrev.c
- │ Proj: M Editor Extension
- │ Design: Joseph Van Valen
- │ Desc: Creates C templates based on contents of line
- │History:
- │ 20-Jan-1990 JVV: Created
- │ 26-Jan-1990 JVV: Modifed to work with both version 1.0 and 1.02. Also
- │ fixed "do..while()" to work in overstrike mode.
- */
-
- #define ID " C Abbreviations ver 1.01 "##__DATE__
-
-
- #include <string.h>
- #include "ext.h"
-
- #define TRUE -1
- #define FALSE 0
- #define NULL ( (void *) 0)
-
- #ifndef MODIFIES
- #define MODIFIES 0x1000
- #endif
-
- /* useful variables and declarations */
-
- char *cptr;
- char *lineBuf;
- char tmpBuf[20];
- char buf[80];
- COL xTmp;
- LINE yTmp;
- COL hold_col;
- LINE hold_row;
- int len;
-
-
- /*
- List 1 contains expressions requiring no additional cursor movements when
- expanded
- */
-
- char *abbrevList1="~return ~break;~do~default:~else ~continue;~#if ~#define ~#include ~#ifdef ~#ifndef ~#endif";
-
-
-
- /*
- List 1 contains expressions requiring the cursor to move left one space
- when expanded
- */
-
- char *abbrevList2="~ei~else if ()~if ()~while ()~for ()~switch ()~case :~";
-
- char * pascal ltrim(char *str);
- char * pascal rtrim(char *str);
- char * pascal cpyExp(char *szDst,char *szExp);
- void pascal writeString(char *string,COL x,LINE y,PFILE pf,int insflg);
- flagType pascal EXTERNAL CAbbrev(unsigned int argData, ARG far *pArg, flagType fMeta);
- void pascal id (char *pszFcn);
- int WhenLoaded (void);
-
-
- /*────────────────────────────────────────────────────────────────────────*/
-
-
- char * pascal ltrim(char *str)
- {
- /*
- │ Desc: Removes whitespace from the start of a string
- │ Pre: Input string
- │ Post: Removes whitespace and returns pointer to string for convenience
- │History:
- │ 21-Jan-1990 JVV: Created
- */
-
- char *pTmp;
-
- pTmp = str;
- while (*pTmp == ' ' || *pTmp == '\t')
- pTmp++;
-
- strcpy(str,pTmp);
- return(str);
- }
-
-
- /*────────────────────────────────────────────────────────────────────────*/
-
-
-
- char * pascal rtrim(char *str)
- {
- /*
- │ Desc: Removes whitespace from the end of a string
- │ Pre: Input string
- │ Post: Removes whitespace and returns pointer to string for convenience
- │History:
- │ 21-Jan-1990 JVV: Created
- */
-
- char *pTmp;
-
- pTmp = str+strlen(str)-1;
-
- while (pTmp >= str && (*pTmp == ' ' || *pTmp == '\t'))
- pTmp--;
- pTmp++;
- *pTmp = '\0';
-
- return(str);
- }
-
- /*────────────────────────────────────────────────────────────────────────*/
-
-
- void pascal writeString(char *string,COL x,LINE y,PFILE pf,int insflg)
- {
- /*
- │ Desc: Writes a string into a file with or without insertion
- │ Pre: Input string, row and column destination, a handle to the target
- │ file, and a flag to determine whether to insert or overwrite
- │ Post: Returns nothing. String placed into file
- │History:
- │ 21-Jan-1990 JVV: Created
- */
-
- cptr = string;
- while (*cptr)
- Replace(*cptr++,x++,y,pf,insflg);
- MoveCur(x,y);
- }
-
- /*────────────────────────────────────────────────────────────────────────*/
-
- char * pascal cpyExp(char *szDst,char *szExp)
- {
- /*
- │ Desc: Extracts an expression from a string of expressions delimited by
- │ '~'
- │ Pre: A pointer to a destination buffer for the results and a
- │ pointer to the start of the desired expression.
- │ Post: Destination buffer contains the desired expression. Returns
- │ pointer to the destination buffer
- │History:
- │ 21-Jan-1990 JVV: Created
- */
-
- char *pTmp;
-
- pTmp = szDst;
-
- do {
- *pTmp++ = *szExp++;
- } while(*szExp != '~');
-
- *pTmp = '\0';
-
- return(szDst);
- }
-
-
-
- /*────────────────────────────────────────────────────────────────────────*/
-
-
-
- flagType pascal EXTERNAL CAbbrev(unsigned int argData, ARG far *pArg, flagType fMeta)
- {
- /*
- │ Desc: Recognizes abbreviations of C expressions on the current line
- │ and expands them to full C expressions.
- │ Pre: valid for NOARG
- │ Post: Expanded C expression in file if recognized as a abbreviation.
- │ Returns TRUE if expansion occured, FALSE if not.
- │History:
- │ 21-Jan-1990 JVV: Created
- */
-
- PFILE curFile;
- char *pTmp1,*pTmp2;
-
- curFile = FileNameToHandle("",NULL);
- xTmp = pArg->arg.noarg.x;
- yTmp = pArg->arg.noarg.y;
-
- len = GetLine(yTmp,buf,curFile);
- *tmpBuf = '\0';
- ltrim(buf);
- rtrim(buf);
- if (*buf) {
- strcpy(tmpBuf,"~");
- strcat(tmpBuf,buf);
- }
-
- if ( (pTmp1 = strstr(abbrevList1,tmpBuf)) != NULL) {
- pTmp1++;
-
- cpyExp(tmpBuf,pTmp1);
-
- MoveCur(xTmp,yTmp);
- fExecute("BegLine");
- GetCursor(&xTmp,&yTmp);
-
- if (!strcmp(tmpBuf,"do")) {
- writeString("do {",xTmp,yTmp,curFile,FALSE);
- fExecute("begline down linsert");
- GetCursor(&xTmp,&yTmp);
- writeString("} while ();",xTmp,yTmp,curFile,FALSE);
- fExecute("begline linsert tab");
- }
- else
- writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
- return(TRUE);
- }
- else if( (pTmp1 = strstr(abbrevList2,tmpBuf)) != NULL) {
- pTmp1++;
-
- cpyExp(tmpBuf,pTmp1);
-
- fExecute("BegLine");
- GetCursor(&xTmp,&yTmp);
-
- if (!strcmp(tmpBuf,"ei"))
- writeString("else if()",xTmp,yTmp,curFile,FALSE);
- else
- writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
-
- GetCursor(&xTmp,&yTmp);
- MoveCur(xTmp-1,yTmp);
- return(TRUE);
- }
- else
- return(FALSE);
- }
-
- /*────────────────────────────────────────────────────────────────────────*/
-
- int WhenLoaded ()
- {
- /*
- │ Desc: Initialization function. Only displays id string and assigns key.
- │ Pre: None
- │ Post: Returns nothing. Assigns default key assignment
- │History:
- │ 21-Jan-1990 JVV: Created
- */
-
- id("");
- SetKey("cabbrev","Alt+Tab");
- return(TRUE);
- }
-
- /*────────────────────────────────────────────────────────────────────────*/
-
- void pascal id (char *pszFcn)
- {
- /*
- │ Desc: Displays id string on message line
- │ Pre: Prefix string for message and ID suffix string
- │ Post: Returns nothing. id string displayed
- │History:
- │ ??-???-?? MSoft: Borrowed from Microsoft Extension example
- */
-
- char buf[80]; /* message buffer */
-
- strcpy (buf,pszFcn); /* start with message */
- strcat (buf,ID); /* append version */
- DoMessage (buf);
- }
-
-
- /*────────────────────────────────────────────────────────────────────────*/
-
- /*
- │ Switch communication table to the editor.
- │ This extension defines no switches.
- │
- */
-
- struct swiDesc swiTable[] = {
- {0, 0, 0}
- };
-
- /*────────────────────────────────────────────────────────────────────────*/
-
- /*
- │ Command communication table to the editor.
- │ Defines the name, location and acceptable argument types.
- │
- */
- struct cmdDesc cmdTable[] = {
- {"cabbrev",CAbbrev,0, NOARG|MODIFIES },
- {0, 0, 0}
- };
-