home *** CD-ROM | disk | FTP | other *** search
- #include "mac.h"
- #define MPW TRUE
- #include "mac_ctype.h"
-
- static char findstring[256], replacestring[256];
- static Boolean backwards = FALSE, wraparound = FALSE,
- casesens = FALSE, fullword = FALSE;
-
-
- Boolean wrappedround = FALSE;
- static Boolean inited = FALSE;
-
- int finddialog();
- Boolean findLine();
- ListHandle SetUpDefnList();
-
- extern char *safemalloc(), *nextDefn(), ftolower();
-
- extern ListHandle SetUpModuleList(), createlist();
- extern short KeyModifiers;
-
- extern pascal Boolean ListDlgFilter();
- extern short dlgOtherButton;
- extern CursHandle watchcurs; /* Ibeam and Watch cursors */
-
-
- Boolean equalstrings(patt,match,len,casesens)
- char *patt, *match;
- int len;
- Boolean casesens;
- {
- int i;
-
- if(casesens)
- return(strncmp(patt,match,len) == 0);
-
- else for(i=0; i<len; ++i,++patt,++match)
- {
- if(ftolower(*match)!=*patt)
- return(FALSE);
- }
-
- return(TRUE);
-
- /* Handle diacriticals -- removed for speed */
- #if 0
- char *matchcopy = safemalloc(len+1);
- Boolean res;
-
- strncpy(matchcopy,match,len);
- matchcopy[len] = '\0';
-
- res = equalstring(patt,matchcopy,casesens,TRUE);
- free(matchcopy);
- return(res);
- #endif
- }
-
-
- /*
- Wraparound does not work for repeated search.
- Will search twice.
- Solution -- remember original posn and set in wrapround code,
- use this if wrapped around AND the direction hasn't changed.
- */
-
- Boolean searchForText(text,windex,backwards)
- char *text;
- int windex;
- Boolean backwards;
- {
- Boolean found = FALSE;
- Boolean saveLock;
-
- if(inited && isLegalWindow(windex) && *text != '\0')
- {
- CharsHandle chbuff = TEGetText(TEHANDLE(windex));
- char *chs;
- int tlen = strlen(text);
- short last = backwards? 0:
- (*TEHANDLE(windex))->teLength - tlen;
- short first = backwards? (*TEHANDLE(windex))->selStart-1:
- (*TEHANDLE(windex))->selEnd;
- short pos, inc = backwards? -1: 1;
- short count = wraparound?1:0;
-
- char *temptext;
-
- /* Convert the pattern to lower case, if necessary */
- if(!casesens)
- {
- int i;
- temptext = safemalloc(tlen+1);
- for(i=0; i < tlen; ++i)
- *(temptext+i) = isupper(*(text+i))? tolower(*(text+i)):
- *(text+i);
- text = temptext;
- }
-
- SafeHLock((Handle) chbuff, &saveLock);
- chs = (char *) **chbuff;
-
- SpinCursor(0);
-
- /* Repeat once or twice */
- do {
-
- for (pos = first; inc > 0? pos <= last: pos >= last; pos+=inc)
- {
- if((pos & 0x3f) == 0)
- SpinCursor(inc);
-
- if(equalstrings(text,chs+pos,tlen,casesens))
- {
- TESetSelect(pos,pos+tlen,TEHANDLE(windex));
- found = TRUE;
- break;
- }
- }
-
- if(!found && wraparound && !wrappedround)
- {
- last = backwards?
- (*TEHANDLE(windex))->selEnd:
- (*TEHANDLE(windex))->selStart-1;
- first = backwards?
- (*TEHANDLE(windex))->teLength - tlen:
- 0;
- wrappedround = TRUE;
- }
- } while (!found && count-- > 0);
-
-
- if(!found)
- SysBeep(1);
- else
- ScrollToSelection(windex);
-
- SafeHUnlock((Handle) chbuff, saveLock);
- InitCursor();
- }
- return(found);
- }
-
-
-
-
- Replace(replace,windex)
- char *replace;
- int windex;
- {
- if(inited)
- {
- short start = (*TEHANDLE(windex))->selStart;
-
- /* Is there enough memory to undo the replace? */
- CheckMemory("Replace",strlen(replace)+strlen(findstring)+512);
-
- /* Save the undo information */
- pasteundo(windex);
-
- TEDelete(TEHANDLE(windex));
- if(*replace != '\0')
- TEInsert((Ptr)replace, strlen(replace), TEHANDLE(windex));
-
- /* Highlight the new string */
- TESetSelect(start,(*TEHANDLE(windex))->selEnd,TEHANDLE(windex));
- changed(windex);
-
- /* Scroll to the new position */
- AdjustScrollBars(windex);
- ScrollToSelection(windex);
- }
- }
-
-
-
- Boolean SelIsSearch(windex,text)
- int windex;
- char *text;
- {
- CharsHandle chbuff;
- char *chs;
- Boolean saveLock;
-
- short start = (*TEHANDLE(windex))->selStart,
- end = (*TEHANDLE(windex))->selEnd;
-
- int tlen = strlen(text);
- Boolean equal;
-
- char *temptext;
-
- /* Convert the pattern to lower case, if necessary */
- if(!casesens)
- {
- int i;
- temptext = safemalloc(tlen+1);
- for(i=0; i < tlen; ++i)
- *(temptext+i) = isupper(*(text+i))? tolower(*(text+i)):*(text+i);
- text = temptext;
- }
-
- chbuff = (CharsHandle) (*TEHANDLE(windex))->hText;
- SafeHLock((Handle) chbuff, &saveLock);
- chs = (char *) *chbuff;
-
- equal = tlen > 0 && end-start == tlen && equalstrings(text,chs+start,tlen,casesens);
-
- SafeHUnlock((Handle) chbuff, saveLock);
- if(!casesens)
- free(text);
-
- return(equal);
- }
-
-
-
- doreplaceagain(windex)
- int windex;
- {
- if(isLegalWindow(windex))
- if(SelIsSearch(windex,findstring) ||
- searchForText(findstring,windex,backwards))
- Replace(replacestring,windex);
- }
-
-
- dofind(windex)
- int windex;
- {
- int dofind = finddialog();
-
- if(isLegalWindow(windex) && dofind != CANCEL)
- {
- updatewindows();
-
- /* Reset the "backwards" search direction */
- setitem(Menu_Find,MItem_Find_Backwards,backwards?"Find Forwards":"Find Backwards");
-
- if(dofind == REPLACE_ALL)
- {
- TESetSelect(0,0,TEHANDLE(windex));
- while(searchForText(findstring,windex,FALSE))
- Replace(replacestring,windex);
- }
- else if((SelIsSearch(windex,findstring) || searchForText(findstring,windex,backwards))
- && *replacestring != '\0')
- Replace(replacestring,windex);
- }
- }
-
- dofindagain(windex)
- int windex;
- {
- if(isLegalWindow(windex))
- searchForText(findstring,windex,backwards);
- }
-
-
- dofindbackwards(windex)
- int windex;
- {
- if(isLegalWindow(windex))
- searchForText(findstring,windex,!backwards);
- }
-
-
- /*
- Separated to deal with that *!#@ Gofer GC bug
- */
-
- #define NO_MORE_MODULES (-1)
- int dofindtypedefndialog();
-
- dofindtypedefn()
- {
- DialogPtr typedlg;
- ListHandle defnlist;
- int module;
- Boolean showingmodules = TRUE;
-
- SetCursor(&(qd.arrow));
- typedlg = GetNewDialog(Res_Dlg_Type,nil,(WindowPtr) -1);
-
- dlgOtherButton = 0;
-
- DisableButton(typedlg,MODULES);
- DisableButton(typedlg,FIND);
-
- /* Now show dialog window */
- ShowWindow(typedlg);
- highlightDefault(typedlg);
-
- defnlist = SetUpModuleList(typedlg,Res_DItem_Type,TRUE,TRUE,0);
-
- do
- {
- module = dofindtypedefndialog(typedlg,defnlist,showingmodules);
- if(module == NO_MORE_MODULES)
- break;
- defnlist = SetUpDefnList(typedlg,module,Res_DItem_Type,FALSE);
- (*defnlist)->selFlags = lNoExtend;
- showingmodules = FALSE;
- }
- while (TRUE);
- }
-
- int dofindtypedefndialog(typedlg,defnlist,showingmodules)
- DialogPtr typedlg;
- ListHandle defnlist;
- Boolean showingmodules;
- {
-
- for(;;)
- {
- short itemhit;
-
- ModalDialog((ModalFilterProcPtr) ListDlgFilter,&itemhit);
- switch(itemhit)
- {
- case Res_DItem_Type:
- {
- Point mousePt;
- GrafPtr savePort;
- Cell cell;
- Boolean doubleclick;
-
- GetPort(&savePort);
- SetPort((*defnlist)->port);
- GetMouse(&mousePt);
- SetPort(savePort);
-
-
- doubleclick = LClick(mousePt,KeyModifiers,defnlist);
- SetPt(&cell,0,0);
- if(LGetSelect(TRUE,&cell,defnlist))
- EnableButton(typedlg,FIND);
-
- if(!doubleclick)
- break;
- itemhit = FIND;
- }
-
- case FIND:
- {
- Cell cell;
- SetPt(&cell,0,0);
-
- if(!LGetSelect(TRUE,&cell,defnlist))
- {
- SysBeep(1);
- break;
- }
-
- if(showingmodules)
- {
- LDelColumn(0,0,defnlist);
- LDispose(defnlist);
- DisableButton(typedlg,FIND);
-
- showingmodules = FALSE;
- EnableButton(typedlg,MODULES);
- setitext(gethandle(Res_DItem_TypeHeader,typedlg),"Definitions:");
- SetButtonTitle(typedlg,FIND,"Type");
-
- /* HACK */
- return((int) cell.v);
- }
- showtypes(defnlist);
- }
-
- case CANCEL:
- LDispose(defnlist);
- DisposDialog(typedlg);
- return(NO_MORE_MODULES);
-
- case MODULES:
- LDelColumn(0,0,defnlist);
- LDispose(defnlist);
- DisableButton(typedlg,FIND);
- DisableButton(typedlg,MODULES);
- SetButtonTitle(typedlg,FIND,"List");
- setitext(gethandle(Res_DItem_TypeHeader,typedlg),"Modules:");
-
- defnlist = SetUpModuleList(typedlg,Res_DItem_Type,FALSE,TRUE,0);
- showingmodules = TRUE;
- break;
- }
- }
- }
-
-
-
- showtypes(defnlist)
- ListHandle defnlist;
- {
- Cell cell;
- short len;
- char buff[256];
-
- windowtofront(worksheet);
-
- SetPt(&cell,0,0);
- while(LGetSelect(TRUE,&cell,defnlist))
- {
- len = 255;
- LGetCell(buff,&len,cell,defnlist);
- buff[len] = '\0';
- showType(buff);
- ++(cell.v);
- }
- printPrompt();
- }
-
-
- dofindtype()
- {
- char defn[256];
-
- getdefnfromselection(defn);
-
- if(defn[0] == '\0')
- SysBeep(1);
- else
- {
- windowtofront(worksheet);
- writeNewLine();
- finddefn(defn,TYPE);
- }
- }
-
-
- getdefnfromselection(defn)
- char *defn;
- {
- TEHandle teh;
- thefrontwindow = findMyWindow(FrontWindow());
-
- defn[0] = '\0';
-
- if(thefrontwindow != ILLEGAL_WINDOW && (teh=TEHANDLE(thefrontwindow)) != NIL)
- {
- char *text = *(char **)((*teh)->hText);
- if((*teh)->selStart != (*teh)->selEnd)
- {
- strncpy(defn,text+(*teh)->selStart,
- (*teh)->selEnd - (*teh)->selStart);
- defn[(*teh)->selEnd - (*teh)->selStart] = '\0';
- if(strpbrk(defn," \t\n\r\f") != NULL)
- defn[0] = '\0';
- }
- else
- {
- short testart, teend, length = (*teh)->teLength;
- int id_found = FALSE;
- char ch;
-
- for ( testart = (*teh)->selStart-1;
- testart >= 0 && ((ch=text[testart]) == '_' || ch == '\'' || isalnum(ch)); --testart)
- id_found |= !isdigit(ch);
- ++testart;
-
- for ( teend = (*teh)->selStart;
- teend <= length && ((ch=text[teend]) == '_' || ch == '\'' || isalnum(ch)); ++teend)
- id_found |= !isdigit(ch);
- --teend;
-
- if(!id_found)
- {
- for ( testart = (*teh)->selStart-1; testart >= 0 && (issymb(text[testart]) || text[teend] == '~'); --testart)
- /* SKIP */;
- ++testart;
-
- for ( teend = (*teh)->selStart; teend < length && (issymb(text[teend]) || text[teend] == '~'); ++teend)
- /* SKIP */;
- --teend;
-
- id_found = testart <= teend;
- }
-
- if(id_found)
- {
- int length = teend-testart+1;
- if(length > 255)
- length = 255;
- else if (length <= 0)
- return;
-
- strncpy(defn,text+testart,length);
- defn[length] = '\0';
- }
- }
- }
- }
-
-
- /*
- Split into dofinddefn/finddefn because of Gofer GC.
- */
-
-
- dofinddefn()
- {
- char defn[256];
-
- getdefnfromselection(defn);
-
- if(defn[0] == '\0')
- SysBeep(1);
- else
- {
- writeNewLine();
- finddefn(defn,FIND);
- }
- }
-
-
- dofinddefns()
- {
- char defn[256];
- int action = FIND;
-
- action = defndialog(defn);
- updatewindows();
-
- finddefn(defn,action);
- }
-
-
- finddefn(defn,action)
- char *defn;
- int action;
- {
- if(action == FIND && *defn != '\0')
- {
- char file[256];
- int windex, line;
- short volnum, dummy;
-
- long dirID;
-
- /* Get zero-indexed line for defn in file */
- if(getfilefor(defn,&line,file,&volnum,&dirID,&dummy) == FALSE)
- {
- windowtofront(worksheet);
- return;
- }
-
- windex = doopenfile(file,volnum,dirID);
-
- if(!findLine(windex,line))
- ActivateTheWindow(FrontWindow(),TRUE);
- }
-
- else if (action == TYPE)
- {
- windowtofront(worksheet);
- showType(defn);
- printPrompt();
- }
- }
-
-
- extern int errorLine, errorMod;
-
- findError()
- {
- char file[256];
- short volnum;
- long dirID;
-
- if(getprojfile(errorMod,file,&volnum,&dirID) == FALSE)
- return;
-
- findFile(file,volnum,dirID,errorLine);
- }
-
-
- findFile(file,volnum,dirID,line)
- char *file;
- short volnum;
- long dirID;
- int line;
- {
- int windex;
-
- resolvealias(&file,&volnum,&dirID,FALSE);
-
- windex = findMyWindowName(file,volnum,dirID);
-
- if(isLegalWindow(windex))
- openthewindow(windex);
- else
- windex = doopenfile(file,volnum,dirID);
-
- if(!findLine(windex,line))
- ActivateTheWindow(FrontWindow(),TRUE);
- }
-
-
-
- Boolean findLine(windex,line)
- int windex, line;
- {
- if(isLegalWindow(windex) && OPEN(windex))
- {
- short linestart, lineend;
- TEHandle teh = TEHANDLE(windex);
-
- if(iconic(windex))
- DeIconiseWindow(windex);
-
- if(line > (*teh)->nLines)
- line = (*teh)->nLines;
- if(line <= 0)
- line = 1;
-
- linestart = (*teh)->lineStarts[line-1];
- lineend = line == (*teh)->nLines? (*teh)->teLength:
- (*teh)->lineStarts[line];
- TESetSelect(linestart,lineend,teh);
- ActivateTheWindow(WINDOW(windex),TRUE);
- updatewindows();
- ScrollToSelection(windex);
- return(TRUE);
- }
- else
- return(FALSE);
- }
-
-
- ListHandle SetUpDefnList(dlg,modind,item,drawframe)
- DialogPtr dlg;
- short modind, item;
- Boolean drawframe;
- {
- int i;
- ListHandle hlist;
- Cell cell;
- /* NB: Do not declare module "short" -- if you do, Gofer's GC may bite */
- int module = (int) GetModFromIndex(modind);
- int numdefns = initmoddefns(module+1);
-
- SetCursor(&(qd.arrow));
- hlist = createlist(dlg,item,numdefns,drawframe);
-
- /* Set up the definition list. */
- for( i=0; i < numdefns; ++i )
- {
- char *nextdefn = nextDefn();
-
- /* Ignore system Identifiers and bad values */
- if(nextdefn != NULL)
- {
- SetPt(&cell,0,i);
- LSetCell(nextdefn,strlen(nextdefn),cell,hlist);
- }
- }
-
- enableSingletons(dlg,hlist,numdefns);
-
- /* Tidy up and draw the list */
- donemoddefns();
- LDoDraw(TRUE,hlist);
- LUpdate((*hlist)->port->visRgn,hlist);
-
- resetDlgSearch(hlist,TRUE);
-
- return(hlist);
- }
-
-
- /*
- Separated to deal with that *!#@ Gofer GC bug
- */
-
- int dodefndialog();
-
- defndialog(defn)
- char *defn;
- {
- DialogPtr defndlg;
- ListHandle defnlist;
- Boolean showingmodules = TRUE;
-
- SetCursor(&(qd.arrow));
- defndlg = GetNewDialog(Res_Dlg_Definitions,nil,(WindowPtr) -1);
-
- dlgOtherButton = TYPE;
-
- DisableButton(defndlg,MODULES);
- DisableButton(defndlg,FIND);
- DisableButton(defndlg,TYPE);
-
- /* Now show dialog window */
- ShowWindow(defndlg);
- highlightDefault(defndlg);
-
- defnlist = SetUpModuleList(defndlg,Res_DItem_Definition,TRUE,TRUE,0);
-
- do
- {
- int module = dodefndialog(defn,defndlg,defnlist,showingmodules);
- if(module >= 0)
- return(module);
- defnlist = SetUpDefnList(defndlg,-(module+1),Res_DItem_Definition,FALSE);
- (*defnlist)->selFlags = lOnlyOne;
- showingmodules = FALSE;
- }
- while (TRUE);
- }
-
-
- dodefndialog(defn,defndlg,defnlist,showingmodules)
- char *defn;
- DialogPtr defndlg;
- ListHandle defnlist;
- Boolean showingmodules;
- {
- short itemhit;
-
- for(;;)
- {
- ModalDialog((ModalFilterProcPtr) ListDlgFilter,&itemhit);
- switch(itemhit)
- {
- case Res_DItem_Definition:
- {
- Point mousePt;
- GrafPtr savePort;
- Cell cell;
- Boolean doubleclick;
-
- GetPort(&savePort);
- SetPort((*defnlist)->port);
- GetMouse(&mousePt);
- SetPort(savePort);
-
- doubleclick = LClick(mousePt,0,defnlist);
- SetPt(&cell,0,0);
- if(LGetSelect(TRUE,&cell,defnlist))
- {
- EnableButton(defndlg,FIND);
- if(showingmodules)
- DisableButton(defndlg,TYPE);
- else
- EnableButton(defndlg,TYPE);
- }
-
- if(!doubleclick)
- break;
-
- itemhit = FIND;
- }
-
- case FIND:
- case TYPE:
- {
- Cell cell;
- short len;
- SetPt(&cell,0,0);
-
- if(!LGetSelect(TRUE,&cell,defnlist))
- {
- SysBeep(1);
- break;
- }
-
- if(showingmodules)
- {
- LDelColumn(0,0,defnlist);
- LDispose(defnlist);
- DisableButton(defndlg,TYPE);
- DisableButton(defndlg,FIND);
-
- EnableButton(defndlg,MODULES);
- setitext(gethandle(Res_DItem_DefnHeader,defndlg),"Definitions:");
- SetButtonTitle(defndlg,FIND,"Find");
-
- return((int)-(cell.v+1));
- }
-
- /* Get the defn name -- no more than 255 characters */
- len = 255;
- LGetCell((DataPtr)defn,&len,cell,defnlist);
- defn[len] = '\0';
- }
-
- case CANCEL:
- LDispose(defnlist);
- DisposDialog(defndlg);
- return(itemhit);
-
- case MODULES:
- LDelColumn(0,0,defnlist);
- LDispose(defnlist);
- DisableButton(defndlg,FIND);
- DisableButton(defndlg,TYPE);
- DisableButton(defndlg,MODULES);
- SetButtonTitle(defndlg,FIND,"List");
-
- defnlist = SetUpModuleList(defndlg,Res_DItem_Definition,FALSE,TRUE,0);
- showingmodules = TRUE;
- setitext(gethandle(Res_DItem_DefnHeader,defndlg),"Modules:");
- break;
- }
- }
- }
-
-
- int finddialog()
- {
- DialogPtr fnddlg;
- short itemhit;
- Boolean okisreplace = FALSE;
-
- if(!inited)
- {
- strcpy(findstring,"");
- strcpy(replacestring,"");
- backwards = FALSE;
- wraparound = FALSE;
- inited = TRUE;
- }
-
- wrappedround = FALSE;
-
- /* Initialise dialog */
- SetCursor(&(qd.arrow));
- fnddlg = GetNewDialog(Res_Dlg_Find,nil,(WindowPtr) -1);
- ShowWindow(fnddlg);
- highlightDefault(fnddlg);
-
- /* Initialise strings */
- SetDlgItemText(fnddlg, Res_DItem_Replaceitem, replacestring);
- SetDlgItemText(fnddlg, Res_DItem_Finditem, findstring);
-
- /* Set Check Boxes */
- SetCtlValue(getctlhandle(Res_DItem_Backwards, fnddlg), backwards);
- SetCtlValue(getctlhandle(Res_DItem_Wraparound, fnddlg), wraparound);
- SetCtlValue(getctlhandle(Res_DItem_Casesens, fnddlg), casesens);
-
- /* Set the Find/Replace Button */
- if(*replacestring != '\0')
- {
- okisreplace = TRUE;
- SetButtonTitle(fnddlg,OK,"Replace");
- }
-
- /* Now show dialog window */
- ShowWindow(fnddlg);
- highlightDefault(fnddlg);
-
- for(;;)
- {
- ModalDialog((ModalFilterProcPtr) NIL,&itemhit);
- switch(itemhit)
- {
- case REPLACE_ALL:
- case FIND:
- getitext(gethandle(Res_DItem_Finditem, fnddlg), findstring);
- getitext(gethandle(Res_DItem_Replaceitem, fnddlg), replacestring);
- if(itemhit == REPLACE_ALL && *replacestring=='\0')
- {
- SysBeep(1);
- break;
- }
-
- backwards = GetCtlValue(getctlhandle(Res_DItem_Backwards, fnddlg));
- wraparound = GetCtlValue(getctlhandle(Res_DItem_Wraparound, fnddlg));
- casesens = GetCtlValue(getctlhandle(Res_DItem_Casesens, fnddlg));
-
- case CANCEL:
- DisposDialog(fnddlg);
- return(itemhit);
-
- case Res_DItem_Backwards:
- case Res_DItem_Wraparound:
- case Res_DItem_Casesens:
- InvertDlgItemValue(fnddlg,itemhit);
- break;
-
- case Res_DItem_Replaceitem:
- getitext(gethandle(Res_DItem_Replaceitem, fnddlg), replacestring);
- if(okisreplace && *replacestring == '\0')
- {
- okisreplace = FALSE;
- SetButtonTitle(fnddlg,OK,"Find");
- }
- else if(!okisreplace && *replacestring != '\0')
- {
- okisreplace = TRUE;
- SetButtonTitle(fnddlg,OK,"Replace");
- }
- break;
- }
- }
- }
-