home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
MABBREV.ZIP
/
PASABBRV.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-26
|
8KB
|
341 lines
/*
│ File: pasabbrv.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: Modified to work with both v1.00 and v1.02. Also fixed
│ overwrite mode problems with some of the templates.
*/
#define ID " Pascal 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 or requiring special handling
*/
char *abbrevList1="~IMPLEMENTATION ~INTERFACE ~VAR ~TYPE ~BEGIN ~END~REPEAT~FUNCTION~";
/*
List 2 contains expressions requiring the cursor to move left one space
when expanded
*/
char *abbrevList2="~PROCEDURE ;~UNIT ;~PROGRAM ;~";
/*
List 3 contains expressions requiring the cursor to move left one word
when expanded
*/
char *abbrevList3="~ELSE~EI~ELSE IF THEN~IF THEN~FOR DO~WHILE DO~CASE OF~WITH DO~";
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 PasAbbrev(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 PasAbbrev(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);
strupr(buf);
if (*buf) {
strcpy(tmpBuf,"~");
strcat(tmpBuf,buf);
}
if((pTmp1 = strstr(abbrevList3,tmpBuf)) != NULL) {
pTmp1++;
cpyExp(tmpBuf,pTmp1);
fExecute("BegLine");
GetCursor(&xTmp,&yTmp);
if (!strcmp(tmpBuf,"ELSE")) {
writeString("ELSE",xTmp,yTmp,curFile,FALSE);
fExecute("begline down linsert tab");
return(TRUE);
}
if (!strcmp(tmpBuf,"EI"))
writeString("ELSE IF THEN",xTmp,yTmp,curFile,FALSE);
else
writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
fExecute("mword left");
return(TRUE);
}
if ( (pTmp1 = strstr(abbrevList1,tmpBuf)) != NULL) {
pTmp1++;
cpyExp(tmpBuf,pTmp1);
MoveCur(xTmp,yTmp);
fExecute("BegLine");
GetCursor(&xTmp,&yTmp);
if (!strcmp(tmpBuf,"BEGIN ")) {
writeString("BEGIN",xTmp,yTmp,curFile,FALSE);
fExecute("begline down linsert");
GetCursor(&xTmp,&yTmp);
writeString("END;",xTmp,yTmp,curFile,FALSE);
fExecute("begline linsert tab");
}
else if (!strcmp(tmpBuf,"REPEAT")) {
writeString("REPEAT",xTmp,yTmp,curFile,FALSE);
fExecute("begline down linsert");
GetCursor(&xTmp,&yTmp);
writeString("UNTIL ;",xTmp,yTmp,curFile,FALSE);
fExecute("begline linsert tab");
}
else if (!strcmp(tmpBuf,"FUNCTION")) {
writeString("FUNCTION :;",xTmp,yTmp,curFile,FALSE);
GetCursor(&xTmp,&yTmp);
MoveCur(xTmp-2,yTmp);
}
else {
writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
fExecute("begline down linsert tab");
}
return(TRUE);
}
if( (pTmp1 = strstr(abbrevList2,tmpBuf)) != NULL) {
pTmp1++;
cpyExp(tmpBuf,pTmp1);
fExecute("BegLine");
GetCursor(&xTmp,&yTmp);
writeString(tmpBuf,xTmp,yTmp,curFile,FALSE);
GetCursor(&xTmp,&yTmp);
MoveCur(xTmp-1,yTmp);
return (TRUE);
}
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("pasabbrev","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[] = {
{"pasabbrev",PasAbbrev,0, NOARG|MODIFIES },
{0, 0, 0}
};