#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "search.h"
#include "regularExp.h"
#include "textBuf.h"
#include "text.h"
#include "nedit.h"
#include "server.h"
#include "window.h"
#include "userCmds.h"
#include "preferences.h"
#include "file.h"
#include "highlight.h"
#include "selection.h"
#ifdef REPLACE_SCOPE
#include "textDisp.h"
#include "textP.h"
#endif
#include "../util/DialogF.h"
#include "../util/misc.h"
#include "../util/utils.h"
#include "../util/nedit_malloc.h"
#include "../util/textfield.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <wctype.h>
#include <wchar.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <Xm/Xm.h>
#include <X11/Shell.h>
#include <Xm/XmP.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#ifdef REPLACE_SCOPE
#if XmVersion >=
1002
#include <Xm/PrimitiveP.h>
#endif
#endif
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Text.h>
#include <Xm/ToggleB.h>
#include <Xm/List.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/X.h>
#ifdef HAVE_DEBUG_H
#include "../debug.h"
#endif
int NHist =
0;
time_t lastSearchdbModTime =
0;
typedef struct _SelectionInfo {
int done;
WindowInfo* window;
char* selection;
} SelectionInfo;
typedef struct {
int direction;
int searchType;
int searchWrap;
} SearchSelectedCallData;
static char *SearchHistory[
MAX_SEARCH_HISTORY];
static char *ReplaceHistory[
MAX_SEARCH_HISTORY];
static int SearchTypeHistory[
MAX_SEARCH_HISTORY];
static int HistStart =
0;
static int textFieldNonEmpty(Widget w);
static void setTextField(WindowInfo* window, Time time, Widget textField);
static void getSelectionCB(Widget w, XtPointer selectionInfo, Atom *selection,
Atom *type, XtPointer value,
unsigned long *length,
int *format);
static void fFocusCB(Widget w, WindowInfo *window,
caddr_t *callData);
static void rFocusCB(Widget w, WindowInfo *window,
caddr_t *callData);
static void rKeepCB(Widget w, WindowInfo *window,
caddr_t *callData);
static void fKeepCB(Widget w, WindowInfo *window,
caddr_t *callData);
static void replaceCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void replaceAllCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void rInSelCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void rCancelCB(Widget w, WindowInfo *window,
caddr_t callData);
static void fCancelCB(Widget w, WindowInfo *window,
caddr_t callData);
static void rFindCB(Widget w,WindowInfo *window,XmAnyCallbackStruct *callData);
static void rFindTextValueChangedCB(Widget w, WindowInfo *window, XKeyEvent *event);
static void rFindArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event);
static void rSetActionButtons(WindowInfo* window,
int replaceBtn,
int replaceFindBtn,
int replaceAndFindBtn,
#ifndef REPLACE_SCOPE
int replaceInWinBtn,
int replaceInSelBtn,
#endif
int replaceAllBtn);
#ifdef REPLACE_SCOPE
static void rScopeWinCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void rScopeSelCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void rScopeMultiCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void replaceAllScopeCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
#endif
static void replaceArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event);
static void fUpdateActionButtons(WindowInfo *window);
static void findTextValueChangedCB(Widget w, WindowInfo *window, XKeyEvent *event);
static void findArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event);
static void replaceFindCB(Widget w, WindowInfo *window, XmAnyCallbackStruct *callData);
static void findCB(Widget w, WindowInfo *window,XmAnyCallbackStruct *callData);
static void replaceMultiFileCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void rMultiFileReplaceCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct * callData);
static void rMultiFileCancelCB(Widget w, WindowInfo *window,
caddr_t callData);
static void rMultiFileSelectAllCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void rMultiFileDeselectAllCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct * callData);
static void rMultiFilePathCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void uploadFileListItems(WindowInfo* window, Bool replace);
static int countWindows(
void);
static int countWritableWindows(
void);
static void collectWritableWindows(WindowInfo* window);
static void freeWritableWindowsCB(Widget w, WindowInfo* window,
XmAnyCallbackStruct *callData);
static void checkMultiReplaceListForDoomedW(WindowInfo* window,
WindowInfo* doomedWindow);
static void removeDoomedWindowFromList(WindowInfo* window,
int index);
static void unmanageReplaceDialogs(
const WindowInfo *window);
static void flashTimeoutProc(XtPointer clientData, XtIntervalId *id);
static void eraseFlash(WindowInfo *window);
static int getReplaceDlogInfo(WindowInfo *window,
int *direction,
char *searchString,
char *replaceString,
int *searchType);
static int getFindDlogInfo(WindowInfo *window,
int *direction,
char *searchString,
int *searchType);
static void selectedSearchCB(Widget w, XtPointer callData, Atom *selection,
Atom *type, XtPointer value,
unsigned long *length,
int *format);
static void iSearchTextClearAndPasteAP(Widget w, XEvent *event, String *args,
Cardinal *nArg);
static void iSearchTextClearCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void iSearchTextActivateCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void iSearchTextValueChangedCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void iSearchTextKeyEH(Widget w, WindowInfo *window,
XKeyEvent *event, Boolean *continueDispatch);
static int searchLiteral(
const char *string,
const char *searchString,
int caseSense,
int direction,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW);
static int searchLiteralWord(
const char *string,
const char *searchString,
int caseSense,
int direction,
int wrap,
int beginPos,
int *startPos,
int *endPos,
const char * delimiters);
static int searchRegex(
const char *string,
const char *searchString,
int direction,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW,
const char *delimiters,
int defaultFlags);
static int forwardRegexSearch(
const char *string,
const char *searchString,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW,
const char *delimiters,
int defaultFlags);
static int backwardRegexSearch(
const char *string,
const char *searchString,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW,
const char *delimiters,
int defaultFlags);
static void resetFindTabGroup(WindowInfo *window);
static void resetReplaceTabGroup(WindowInfo *window);
static int searchMatchesSelection(WindowInfo *window,
const char *searchString,
int searchType,
int *left,
int *right,
int *searchExtentBW,
int *searchExtentFW);
static int findMatchingChar(WindowInfo *window,
char toMatch,
void *toMatchStyle,
int charPos,
int startLimit,
int endLimit,
int *matchPos);
static Boolean replaceUsingRE(
const char* searchStr,
const char* replaceStr,
const char* sourceStr,
int beginPos,
char* destStr,
int maxDestLen,
int prevChar,
const char* delimiters,
int defaultFlags);
static void enableFindAgainCmds(
void);
static void saveSearchHistory(
const char *searchString,
const char *replaceString,
int searchType,
int isIncremental);
static int historyIndex(
int nCycles);
static char *searchTypeArg(
int searchType);
static char *searchWrapArg(
int searchWrap);
static char *directionArg(
int direction);
static int isRegexType(
int searchType);
static int defaultRegexFlags(
int searchType);
static void findRegExpToggleCB(Widget w, XtPointer clientData,
XtPointer callData);
static void replaceRegExpToggleCB(Widget w, XtPointer clientData,
XtPointer callData);
static void iSearchRegExpToggleCB(Widget w, XtPointer clientData,
XtPointer callData);
static void findCaseToggleCB(Widget w, XtPointer clientData,
XtPointer callData);
static void replaceCaseToggleCB(Widget w, XtPointer clientData,
XtPointer callData);
static void iSearchCaseToggleCB(Widget w, XtPointer clientData,
XtPointer callData);
static void iSearchTryBeepOnWrap(WindowInfo *window,
int direction,
int beginPos,
int startPos);
static void iSearchRecordLastBeginPos(WindowInfo *window,
int direction,
int initPos);
static Boolean prefOrUserCancelsSubst(
const Widget parent,
const Display* display);
static int translateEscPos(EscSeqArray *array,
int pos);
static void translatePosAndRestoreBuf(
textBuffer *buf,
EscSeqArray *array,
int found,
int *begin,
int *end,
int *extentBW,
int *extentFW);
typedef struct _charMatchTable {
char c;
char match;
char direction;
} charMatchTable;
#define N_MATCH_CHARS 13
#define N_FLASH_CHARS 6
static charMatchTable MatchingChars[
N_MATCH_CHARS] = {
{
'{',
'}',
SEARCH_FORWARD},
{
'}',
'{',
SEARCH_BACKWARD},
{
'(',
')',
SEARCH_FORWARD},
{
')',
'(',
SEARCH_BACKWARD},
{
'[',
']',
SEARCH_FORWARD},
{
']',
'[',
SEARCH_BACKWARD},
{
'<',
'>',
SEARCH_FORWARD},
{
'>',
'<',
SEARCH_BACKWARD},
{
'/',
'/',
SEARCH_FORWARD},
{
'""',
'""',
SEARCH_FORWARD},
{
'\'',
'\'',
SEARCH_FORWARD},
{
'`',
'`',
SEARCH_FORWARD},
{
'\\',
'\\',
SEARCH_FORWARD},
};
static char *searchTypeStrings[] = {
"literal",
"case",
"regex",
"word",
"caseWord",
"regexNoCase",
NULL
};
static WindowInfo *windowNotToClose =
NULL;
Boolean WindowCanBeClosed(WindowInfo *window)
{
if (windowNotToClose &&
GetTopDocument(window->shell) ==
GetTopDocument(windowNotToClose->shell)) {
return False;
}
return True;
}
static void initToggleButtons(
int searchType, Widget regexToggle,
Widget caseToggle, Widget* wordToggle,
Bool* lastLiteralCase,
Bool* lastRegexCase)
{
switch (searchType) {
case SEARCH_LITERAL:
*lastLiteralCase = False;
*lastRegexCase = True;
XmToggleButtonSetState(regexToggle, False, False);
XmToggleButtonSetState(caseToggle, False, False);
if (wordToggle) {
XmToggleButtonSetState(*wordToggle, False, False);
XtSetSensitive(*wordToggle, True);
}
break;
case SEARCH_CASE_SENSE:
*lastLiteralCase = True;
*lastRegexCase = True;
XmToggleButtonSetState(regexToggle, False, False);
XmToggleButtonSetState(caseToggle, True, False);
if (wordToggle) {
XmToggleButtonSetState(*wordToggle, False, False);
XtSetSensitive(*wordToggle, True);
}
break;
case SEARCH_LITERAL_WORD:
*lastLiteralCase = False;
*lastRegexCase = True;
XmToggleButtonSetState(regexToggle, False, False);
XmToggleButtonSetState(caseToggle, False, False);
if (wordToggle) {
XmToggleButtonSetState(*wordToggle, True, False);
XtSetSensitive(*wordToggle, True);
}
break;
case SEARCH_CASE_SENSE_WORD:
*lastLiteralCase = True;
*lastRegexCase = True;
XmToggleButtonSetState(regexToggle, False, False);
XmToggleButtonSetState(caseToggle, True, False);
if (wordToggle) {
XmToggleButtonSetState(*wordToggle, True, False);
XtSetSensitive(*wordToggle, True);
}
break;
case SEARCH_REGEX:
*lastLiteralCase = False;
*lastRegexCase = True;
XmToggleButtonSetState(regexToggle, True, False);
XmToggleButtonSetState(caseToggle, True, False);
if (wordToggle) {
XmToggleButtonSetState(*wordToggle, False, False);
XtSetSensitive(*wordToggle, False);
}
break;
case SEARCH_REGEX_NOCASE:
*lastLiteralCase = False;
*lastRegexCase = False;
XmToggleButtonSetState(regexToggle, True, False);
XmToggleButtonSetState(caseToggle, False, False);
if (wordToggle) {
XmToggleButtonSetState(*wordToggle, False, False);
XtSetSensitive(*wordToggle, False);
}
break;
}
}
#ifdef REPLACE_SCOPE
static int selectionSpansMultipleLines(WindowInfo *window)
{
int selStart, selEnd, isRect, rectStart, rectEnd, lineStartStart,
lineStartEnd;
int lineWidth;
textDisp *textD;
if (!BufGetSelectionPos(window->buffer, &selStart, &selEnd, &isRect,
&rectStart, &rectEnd))
return FALSE;
lineStartStart = BufStartOfLine(window->buffer, selStart);
lineStartEnd = BufStartOfLine(window->buffer, selEnd);
if (lineStartStart != lineStartEnd )
return TRUE;
if (window->wrapMode !=
CONTINUOUS_WRAP)
return FALSE;
textD = ((TextWidget)window->textArea)->text.textD;
if (textD->font->fonts->font->max_advance_width >
0)
lineWidth = textD->width / textD->font->fonts->font->max_advance_width;
else
lineWidth =
1;
if (lineWidth <
1) lineWidth =
1;
if ((selStart-lineStartStart)/lineWidth !=
(selEnd-lineStartStart)/lineWidth )
return TRUE;
return FALSE;
}
#endif
void DoFindReplaceDlog(WindowInfo *window,
int direction,
int keepDialogs,
int searchType, Time time)
{
if (window->replaceDlog ==
NULL)
CreateReplaceDlog(window->shell, window);
setTextField(window, time, window->replaceText);
if (XtIsManaged(window->replaceDlog)) {
RaiseDialogWindow(XtParent(window->replaceDlog));
return;
}
XNETextSetString(window->replaceWithText,
"");
initToggleButtons(searchType, window->replaceRegexToggle,
window->replaceCaseToggle, &window->replaceWordToggle,
&window->replaceLastLiteralCase,
&window->replaceLastRegexCase);
XmToggleButtonSetState(window->replaceRevToggle,
direction ==
SEARCH_FORWARD ? False: True, True);
XmToggleButtonSetState(window->replaceKeepBtn, keepDialogs, True);
#ifdef REPLACE_SCOPE
if (window->wasSelected) {
switch(GetPrefReplaceDefScope()) {
case REPL_DEF_SCOPE_SELECTION:
RadioButtonChangeState(window->replaceScopeSelToggle,
True, True);
break;
case REPL_DEF_SCOPE_SMART:
if (selectionSpansMultipleLines(window)) {
RadioButtonChangeState(window->replaceScopeSelToggle,
True, True);
}
else {
RadioButtonChangeState(window->replaceScopeWinToggle,
True, True);
}
break;
default:
RadioButtonChangeState(window->replaceScopeWinToggle,
True, True);
window->replaceScope =
REPL_SCOPE_WIN;
break;
}
}
else {
RadioButtonChangeState(window->replaceScopeWinToggle, True, True);
window->replaceScope =
REPL_SCOPE_WIN;
}
#endif
UpdateReplaceActionButtons(window);
ReadSearchHistory();
window->rHistIndex =
0;
ManageDialogCenteredOnPointer(window->replaceDlog);
XmProcessTraversal(window->replaceText, XmTRAVERSE_CURRENT);
}
static void setTextField(WindowInfo *window, Time time, Widget textField)
{
XEvent nextEvent;
char *primary_selection =
0;
SelectionInfo *selectionInfo = NEditNew(SelectionInfo);
if (GetPrefFindReplaceUsesSelection()) {
selectionInfo->done =
0;
selectionInfo->window = window;
selectionInfo->selection =
0;
XtGetSelectionValue(window->textArea,
XA_PRIMARY,
XA_STRING,
getSelectionCB, selectionInfo, time);
while (selectionInfo->done ==
0) {
XtAppNextEvent(XtWidgetToApplicationContext(window->textArea), &nextEvent);
ServerDispatchEvent(&nextEvent);
}
primary_selection = selectionInfo->selection;
}
if (primary_selection ==
0) {
primary_selection = NEditStrdup(
"");
}
XNETextSetString(textField, primary_selection);
NEditFree(primary_selection);
NEditFree(selectionInfo);
}
static void getSelectionCB(Widget w, XtPointer si, Atom *selection,
Atom *type, XtPointer v,
unsigned long *length,
int *format)
{
SelectionInfo *selectionInfo = si;
char *value = v;
WindowInfo *window = selectionInfo->window;
if (*type ==
XT_CONVERT_FAIL || *type !=
XA_STRING || value ==
NULL || *length ==
0) {
NEditFree(value);
selectionInfo->selection =
0;
selectionInfo->done =
1;
return;
}
if (*format !=
8) {
DialogF(
DF_WARN, window->shell,
1,
"Invalid Format",
"XNEdit can''t handle non 8-bit text",
"OK");
NEditFree(value);
selectionInfo->selection =
0;
selectionInfo->done =
1;
return;
}
selectionInfo->selection = (
char*)NEditMalloc(*length+
1);
memcpy(selectionInfo->selection, value, *length);
selectionInfo->selection[*length] =
0;
NEditFree(value);
selectionInfo->done =
1;
}
void DoFindDlog(WindowInfo *window,
int direction,
int keepDialogs,
int searchType, Time time)
{
if (window->findDlog ==
NULL)
CreateFindDlog(window->shell, window);
setTextField(window, time, window->findText);
if (XtIsManaged(window->findDlog)) {
RaiseDialogWindow(XtParent(window->findDlog));
return;
}
initToggleButtons(searchType, window->findRegexToggle,
window->findCaseToggle, &window->findWordToggle,
&window->findLastLiteralCase,
&window->findLastRegexCase);
XmToggleButtonSetState(window->findRevToggle,
direction ==
SEARCH_FORWARD ? False : True, True);
XmToggleButtonSetState(window->findKeepBtn, keepDialogs, True);
fUpdateActionButtons(window);
ReadSearchHistory();
window->fHistIndex =
0;
ManageDialogCenteredOnPointer(window->findDlog);
XmProcessTraversal(window->findText, XmTRAVERSE_CURRENT);
}
void DoReplaceMultiFileDlog(WindowInfo *window)
{
char searchString[
SEARCHMAX], replaceString[
SEARCHMAX];
int direction, searchType;
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
if (*searchString ==
'\0') {
resetReplaceTabGroup(window);
if (!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
return;
}
if (window->replaceMultiFileDlog ==
NULL)
CreateReplaceMultiFileDlog(window);
collectWritableWindows(window);
uploadFileListItems(window, False);
ManageDialogCenteredOnPointer(window->replaceMultiFileDlog);
}
void RemoveFromMultiReplaceDialog(WindowInfo *doomedWindow)
{
WindowInfo *w;
for (w=WindowList; w!=
NULL; w=w->next)
if (w->writableWindows)
checkMultiReplaceListForDoomedW(w, doomedWindow);
}
void CreateReplaceDlog(Widget parent, WindowInfo *window)
{
Arg args[
50];
int argcnt, defaultBtnOffset;
XmString st1;
Widget form, btnForm;
#ifdef REPLACE_SCOPE
Widget scopeForm, replaceAllBtn;
#else
Widget label3, allForm;
#endif
Widget inWinBtn, inSelBtn, inMultiBtn;
Widget searchTypeBox;
Widget label2, label1, label, replaceText, findText;
Widget findBtn, cancelBtn, replaceBtn;
Widget replaceFindBtn;
Widget searchDirBox, reverseBtn, keepBtn;
char title[
MAXPATHLEN +
19];
Dimension shadowThickness;
argcnt =
0;
XtSetArg(args[argcnt], XmNautoUnmanage, False); argcnt++;
form = CreateFormDialog(parent,
"replaceDialog", args, argcnt);
XtVaSetValues(form, XmNshadowThickness,
0,
NULL);
if (GetPrefKeepSearchDlogs()) {
snprintf(title,
sizeof(title),
"Replace/Find (in %s)", window->filename);
XtVaSetValues(XtParent(form), XmNtitle, title,
NULL);
}
else
XtVaSetValues(XtParent(form), XmNtitle,
"Replace/Find",
NULL);
argcnt =
0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
4); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_BEGINNING); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"String to Find:"));
argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
't'); argcnt++;
label1 = XmCreateLabel(form,
"label1", args, argcnt);
XmStringFree(st1);
XtManageChild(label1);
argcnt =
0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_END); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"(use up arrow key to recall previous)")); argcnt++;
label2 = XmCreateLabel(form,
"label2", args, argcnt);
XmStringFree(st1);
XtManageChild(label2);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, label1); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNmaxLength,
SEARCHMAX); argcnt++;
findText = XNECreateText(form,
"replaceString", args, argcnt);
XtAddCallback(findText, XmNfocusCallback, (XtCallbackProc)rFocusCB, window);
XtAddCallback(findText, XmNvalueChangedCallback,
(XtCallbackProc)rFindTextValueChangedCB, window);
XtAddEventHandler(findText, KeyPressMask, False,
(XtEventHandler)rFindArrowKeyCB, window);
RemapDeleteKey(findText);
XtManageChild(findText);
XmAddTabGroup(findText);
XtVaSetValues(label1, XmNuserData, findText,
NULL);
argcnt =
0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, findText); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_BEGINNING); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Replace With:")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'W'); argcnt++;
label = XmCreateLabel(form,
"label", args, argcnt);
XmStringFree(st1);
XtManageChild(label);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, label); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNmaxLength,
SEARCHMAX); argcnt++;
replaceText = XNECreateText(form,
"replaceWithString", args, argcnt);
XtAddEventHandler(replaceText, KeyPressMask, False,
(XtEventHandler)replaceArrowKeyCB, window);
RemapDeleteKey(replaceText);
XtManageChild(replaceText);
XmAddTabGroup(replaceText);
XtVaSetValues(label, XmNuserData, replaceText,
NULL);
argcnt =
0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight,
0); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, replaceText); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
2); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
4); argcnt++;
searchTypeBox = XmCreateRowColumn(form,
"searchTypeBox", args, argcnt);
XtManageChild(searchTypeBox);
XmAddTabGroup(searchTypeBox);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Regular Expression")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'R'); argcnt++;
window->replaceRegexToggle = XmCreateToggleButton(searchTypeBox,
"regExp", args, argcnt);
XmStringFree(st1);
XtManageChild(window->replaceRegexToggle);
XtAddCallback(window->replaceRegexToggle, XmNvalueChangedCallback, (XtCallbackProc) replaceRegExpToggleCB, window);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Case Sensitive")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'C'); argcnt++;
window->replaceCaseToggle = XmCreateToggleButton(searchTypeBox,
"caseSensitive", args, argcnt);
XmStringFree(st1);
XtManageChild(window->replaceCaseToggle);
XtAddCallback(window->replaceCaseToggle, XmNvalueChangedCallback, (XtCallbackProc) replaceCaseToggleCB, window);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Whole Word")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'h'); argcnt++;
window->replaceWordToggle = XmCreateToggleButton(searchTypeBox,
"wholeWord", args, argcnt);
XmStringFree(st1);
XtManageChild(window->replaceWordToggle);
argcnt =
0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight,
0); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
0); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchTypeBox); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
2); argcnt++;
XtSetArg(args[argcnt], XmNradioBehavior, False); argcnt++;
searchDirBox = XmCreateRowColumn(form,
"searchDirBox", args, argcnt);
XtManageChild(searchDirBox);
XmAddTabGroup(searchDirBox);
argcnt =
0;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Search Backward")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'B'); argcnt++;
reverseBtn = XmCreateToggleButton(searchDirBox,
"reverse", args, argcnt);
XmStringFree(st1);
XtManageChild(reverseBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Keep Dialog")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'K'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
0); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchTypeBox); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
4); argcnt++;
keepBtn = XmCreateToggleButton(form,
"keep", args, argcnt);
XtAddCallback(keepBtn, XmNvalueChangedCallback,
(XtCallbackProc)rKeepCB, window);
XmStringFree(st1);
XtManageChild(keepBtn);
XmAddTabGroup(keepBtn);
#ifdef REPLACE_SCOPE
argcnt =
0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight,
0); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchDirBox); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
2); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNradioBehavior, True); argcnt++;
XtSetArg(args[argcnt], XmNradioAlwaysOne, True); argcnt++;
scopeForm = XmCreateRowColumn(form,
"scope", args, argcnt);
XtManageChild(scopeForm);
XmAddTabGroup(scopeForm);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"In Window"));
argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'i'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
inWinBtn = XmCreateToggleButton(scopeForm,
"inWindow", args, argcnt);
XtAddCallback(inWinBtn, XmNvalueChangedCallback,
(XtCallbackProc)rScopeWinCB, window);
XmStringFree(st1);
XtManageChild(inWinBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"In Selection")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'S'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftWidget, inWinBtn); argcnt++;
inSelBtn = XmCreateToggleButton(scopeForm,
"inSel", args, argcnt);
XtAddCallback(inSelBtn, XmNvalueChangedCallback,
(XtCallbackProc)rScopeSelCB, window);
XmStringFree(st1);
XtManageChild(inSelBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"In Multiple Documents")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'M'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftWidget, inSelBtn); argcnt++;
inMultiBtn = XmCreateToggleButton(scopeForm,
"multiFile", args, argcnt);
XtAddCallback(inMultiBtn, XmNvalueChangedCallback,
(XtCallbackProc)rScopeMultiCB, window);
XmStringFree(st1);
XtManageChild(inMultiBtn);
#else
argcnt =
0;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchDirBox); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
allForm = XmCreateForm(form,
"all", args, argcnt);
XtManageChild(allForm);
XmAddTabGroup(allForm);
argcnt =
0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
4); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_BEGINNING); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Replace all in:"));
argcnt++;
label3 = XmCreateLabel(allForm,
"label3", args, argcnt);
XmStringFree(st1);
XtManageChild(label3);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Window"));
argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'i'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftWidget, label3); argcnt++;
inWinBtn = XmCreatePushButton(allForm,
"inWindow", args, argcnt);
XtAddCallback(inWinBtn, XmNactivateCallback,
(XtCallbackProc)replaceAllCB, window);
XmStringFree(st1);
XtManageChild(inWinBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Selection")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'S'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftWidget, inWinBtn); argcnt++;
inSelBtn = XmCreatePushButton(allForm,
"inSel", args, argcnt);
XtAddCallback(inSelBtn, XmNactivateCallback,
(XtCallbackProc)rInSelCB, window);
XmStringFree(st1);
XtManageChild(inSelBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Multiple Documents...")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'M'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftWidget, inSelBtn); argcnt++;
inMultiBtn = XmCreatePushButton(allForm,
"multiFile", args, argcnt);
XtAddCallback(inMultiBtn, XmNactivateCallback,
(XtCallbackProc)replaceMultiFileCB, window);
XmStringFree(st1);
XtManageChild(inMultiBtn);
#endif
argcnt =
0;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
#ifdef REPLACE_SCOPE
XtSetArg(args[argcnt], XmNtopWidget, scopeForm); argcnt++;
#else
XtSetArg(args[argcnt], XmNtopWidget, allForm); argcnt++;
#endif
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
btnForm = XmCreateForm(form,
"buttons", args, argcnt);
XtManageChild(btnForm);
XmAddTabGroup(btnForm);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Replace")); argcnt++;
XtSetArg(args[argcnt], XmNshowAsDefault, (
short)
1); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_FORM); argcnt++;
#ifdef REPLACE_SCOPE
XtSetArg(args[argcnt], XmNleftPosition,
0); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
21); argcnt++;
#else
XtSetArg(args[argcnt], XmNleftPosition,
0); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
25); argcnt++;
#endif
replaceBtn = XmCreatePushButton(btnForm,
"replace", args, argcnt);
XtAddCallback(replaceBtn, XmNactivateCallback, (XtCallbackProc)replaceCB, window);
XmStringFree(st1);
XtManageChild(replaceBtn);
XtVaGetValues(replaceBtn, XmNshadowThickness, &shadowThickness,
NULL);
defaultBtnOffset = shadowThickness +
4;
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Find")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'F'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
#ifdef REPLACE_SCOPE
XtSetArg(args[argcnt], XmNleftPosition,
21); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
33); argcnt++;
#else
XtSetArg(args[argcnt], XmNleftPosition,
25); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
42); argcnt++;
#endif
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
XtSetArg(args[argcnt], XmNbottomOffset, defaultBtnOffset); argcnt++;
findBtn = XmCreatePushButton(btnForm,
"find", args, argcnt);
XtAddCallback(findBtn, XmNactivateCallback, (XtCallbackProc)rFindCB, window);
XmStringFree(st1);
XtManageChild(findBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Replace & Find")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'n'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
#ifdef REPLACE_SCOPE
XtSetArg(args[argcnt], XmNleftPosition,
33); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
62); argcnt++;
#else
XtSetArg(args[argcnt], XmNleftPosition,
42); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
79); argcnt++;
#endif
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
XtSetArg(args[argcnt], XmNbottomOffset, defaultBtnOffset); argcnt++;
replaceFindBtn = XmCreatePushButton(btnForm,
"replacefind", args, argcnt);
XtAddCallback(replaceFindBtn, XmNactivateCallback, (XtCallbackProc)replaceFindCB, window);
XmStringFree(st1);
XtManageChild(replaceFindBtn);
#ifdef REPLACE_SCOPE
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Replace All")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'A'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition,
62); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
85); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
replaceAllBtn = XmCreatePushButton(btnForm,
"all", args, argcnt);
XtAddCallback(replaceAllBtn, XmNactivateCallback,
(XtCallbackProc)replaceAllScopeCB, window);
XmStringFree(st1);
XtManageChild(replaceAllBtn);
#endif
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Cancel")); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
#ifdef REPLACE_SCOPE
XtSetArg(args[argcnt], XmNleftPosition,
85); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
100); argcnt++;
#else
XtSetArg(args[argcnt], XmNleftPosition,
79); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
100); argcnt++;
#endif
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
XtSetArg(args[argcnt], XmNbottomOffset, defaultBtnOffset); argcnt++;
cancelBtn = XmCreatePushButton(btnForm,
"cancel", args, argcnt);
XmStringFree(st1);
XtAddCallback(cancelBtn, XmNactivateCallback, (XtCallbackProc)rCancelCB,
window);
XtManageChild(cancelBtn);
XtVaSetValues(form, XmNcancelButton, cancelBtn,
NULL);
AddDialogMnemonicHandler(form,
FALSE);
window->replaceDlog = form;
window->replaceText = findText;
window->replaceWithText = replaceText;
window->replaceRevToggle = reverseBtn;
window->replaceKeepBtn = keepBtn;
window->replaceBtns = btnForm;
window->replaceBtn = replaceBtn;
window->replaceAndFindBtn = replaceFindBtn;
window->replaceFindBtn = findBtn;
window->replaceSearchTypeBox = searchTypeBox;
#ifdef REPLACE_SCOPE
window->replaceAllBtn = replaceAllBtn;
window->replaceScopeWinToggle = inWinBtn;
window->replaceScopeSelToggle = inSelBtn;
window->replaceScopeMultiToggle = inMultiBtn;
#else
window->replaceInWinBtn = inWinBtn;
window->replaceAllBtn = inMultiBtn;
window->replaceInSelBtn = inSelBtn;
#endif
}
void CreateFindDlog(Widget parent, WindowInfo *window)
{
Arg args[
50];
int argcnt, defaultBtnOffset;
XmString st1;
Widget form, btnForm, searchTypeBox;
Widget findText, label1, label2, cancelBtn, findBtn;
Widget searchDirBox, reverseBtn, keepBtn;
char title[
MAXPATHLEN +
11];
Dimension shadowThickness;
argcnt =
0;
XtSetArg(args[argcnt], XmNautoUnmanage, False); argcnt++;
form = CreateFormDialog(parent,
"findDialog", args, argcnt);
XtVaSetValues(form, XmNshadowThickness,
0,
NULL);
if (GetPrefKeepSearchDlogs()) {
snprintf(title,
sizeof(title),
"Find (in %s)", window->filename);
XtVaSetValues(XtParent(form), XmNtitle, title,
NULL);
}
else
XtVaSetValues(XtParent(form), XmNtitle,
"Find",
NULL);
argcnt =
0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_BEGINNING); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"String to Find:"));
argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'S'); argcnt++;
label1 = XmCreateLabel(form,
"label1", args, argcnt);
XmStringFree(st1);
XtManageChild(label1);
argcnt =
0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_END); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"(use up arrow key to recall previous)")); argcnt++;
label2 = XmCreateLabel(form,
"label2", args, argcnt);
XmStringFree(st1);
XtManageChild(label2);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, label1); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNmaxLength,
SEARCHMAX); argcnt++;
findText = XNECreateText(form,
"searchString", args, argcnt);
XtAddCallback(findText, XmNfocusCallback, (XtCallbackProc)fFocusCB, window);
XtAddCallback(findText, XmNvalueChangedCallback,
(XtCallbackProc)findTextValueChangedCB, window);
XtAddEventHandler(findText, KeyPressMask, False,
(XtEventHandler)findArrowKeyCB, window);
RemapDeleteKey(findText);
XtManageChild(findText);
XmAddTabGroup(findText);
XtVaSetValues(label1, XmNuserData, findText,
NULL);
argcnt =
0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight,
0); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, findText); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
2); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
4); argcnt++;
searchTypeBox = XmCreateRowColumn(form,
"searchTypeBox", args, argcnt);
XtManageChild(searchTypeBox);
XmAddTabGroup(searchTypeBox);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Regular Expression")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'R'); argcnt++;
window->findRegexToggle = XmCreateToggleButton(searchTypeBox,
"regExp", args, argcnt);
XmStringFree(st1);
XtManageChild(window->findRegexToggle);
XtAddCallback(window->findRegexToggle, XmNvalueChangedCallback, (XtCallbackProc) findRegExpToggleCB, window);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Case Sensitive")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'C'); argcnt++;
window->findCaseToggle = XmCreateToggleButton(searchTypeBox,
"caseSensitive", args, argcnt);
XmStringFree(st1);
XtManageChild(window->findCaseToggle);
XtAddCallback(window->findCaseToggle, XmNvalueChangedCallback, (XtCallbackProc) findCaseToggleCB, window);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Whole Word")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'h'); argcnt++;
window->findWordToggle = XmCreateToggleButton(searchTypeBox,
"wholeWord", args, argcnt);
XmStringFree(st1);
XtManageChild(window->findWordToggle);
argcnt =
0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight,
0); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
0); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchTypeBox); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
2); argcnt++;
XtSetArg(args[argcnt], XmNradioBehavior, False); argcnt++;
searchDirBox = XmCreateRowColumn(form,
"searchDirBox", args, argcnt);
XtManageChild(searchDirBox);
XmAddTabGroup(searchDirBox);
argcnt =
0;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Search Backward")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'B'); argcnt++;
reverseBtn = XmCreateToggleButton(searchDirBox,
"reverse", args, argcnt);
XmStringFree(st1);
XtManageChild(reverseBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Keep Dialog")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'K'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
0); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchTypeBox); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
4); argcnt++;
keepBtn = XmCreateToggleButton(form,
"keep", args, argcnt);
XtAddCallback(keepBtn, XmNvalueChangedCallback,
(XtCallbackProc)fKeepCB, window);
XmStringFree(st1);
XtManageChild(keepBtn);
XmAddTabGroup(keepBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchDirBox); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
2); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
4); argcnt++;
btnForm = XmCreateForm(form,
"buttons", args, argcnt);
XtManageChild(btnForm);
XmAddTabGroup(btnForm);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Find")); argcnt++;
XtSetArg(args[argcnt], XmNshowAsDefault, (
short)
1); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition,
20); argcnt++;
XtSetArg(args[argcnt], XmNbottomOffset,
6); argcnt++;
findBtn = XmCreatePushButton(btnForm,
"find", args, argcnt);
XtAddCallback(findBtn, XmNactivateCallback, (XtCallbackProc)findCB, window);
XmStringFree(st1);
XtManageChild(findBtn);
XtVaGetValues(findBtn, XmNshadowThickness, &shadowThickness,
NULL);
defaultBtnOffset = shadowThickness +
4;
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Cancel")); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
80); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
cancelBtn = XmCreatePushButton(btnForm,
"cancel", args, argcnt);
XtAddCallback(cancelBtn, XmNactivateCallback, (XtCallbackProc)fCancelCB,
window);
XmStringFree(st1);
XtManageChild(cancelBtn);
XtVaSetValues(form, XmNcancelButton, cancelBtn,
NULL);
AddDialogMnemonicHandler(form,
FALSE);
window->findDlog = form;
window->findText = findText;
window->findRevToggle = reverseBtn;
window->findKeepBtn = keepBtn;
window->findBtns = btnForm;
window->findBtn = findBtn;
window->findSearchTypeBox = searchTypeBox;
}
void CreateReplaceMultiFileDlog(WindowInfo *window)
{
Arg args[
50];
int argcnt, defaultBtnOffset;
XmString st1;
Widget list, label1, form, pathBtn;
Widget btnForm, replaceBtn, selectBtn, deselectBtn, cancelBtn;
Dimension shadowThickness;
argcnt =
0;
XtSetArg(args[argcnt], XmNautoUnmanage, False); argcnt++;
XtSetArg (args[argcnt], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL);
argcnt ++;
form = CreateFormDialog(window->shell,
"replaceMultiFileDialog",
args, argcnt);
XtVaSetValues(form, XmNshadowThickness,
0,
NULL);
XtVaSetValues(XtParent(form), XmNtitle,
"Replace All in Multiple Documents",
NULL);
argcnt =
0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
10); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_BEGINNING); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Files in which to Replace All:")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'F'); argcnt++;
label1 = XmCreateLabel(form,
"label1", args, argcnt);
XmStringFree(st1);
XtManageChild(label1);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNset, False); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_END); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=
MKSTRING(
"Show Path Names")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'P'); argcnt++;
pathBtn = XmCreateToggleButton(form,
"path", args, argcnt);
XmStringFree(st1);
XtAddCallback(pathBtn, XmNvalueChangedCallback,
(XtCallbackProc)rMultiFilePathCB, window);
XtManageChild(pathBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNresizable, (
short)
0); argcnt++;
btnForm = XmCreateForm(form,
"buttons", args, argcnt);
XtManageChild(btnForm);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Replace")); argcnt++;
XtSetArg(args[argcnt], XmNshowAsDefault, (
short)
1); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'R'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition,
0); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
25); argcnt++;
replaceBtn = XmCreatePushButton(btnForm,
"replace", args, argcnt);
XmStringFree(st1);
XtAddCallback(replaceBtn, XmNactivateCallback,
(XtCallbackProc)rMultiFileReplaceCB, window);
XtManageChild(replaceBtn);
XtVaGetValues(replaceBtn, XmNshadowThickness, &shadowThickness,
NULL);
defaultBtnOffset = shadowThickness +
4;
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Select All"));
argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'S'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition,
25); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
50); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
selectBtn = XmCreatePushButton(btnForm,
"select", args, argcnt);
XmStringFree(st1);
XtAddCallback(selectBtn, XmNactivateCallback,
(XtCallbackProc)rMultiFileSelectAllCB, window);
XtManageChild(selectBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Deselect All"));
argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'D'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition,
50); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
75); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
deselectBtn = XmCreatePushButton(btnForm,
"deselect", args, argcnt);
XmStringFree(st1);
XtAddCallback(deselectBtn, XmNactivateCallback,
(XtCallbackProc)rMultiFileDeselectAllCB, window);
XtManageChild(deselectBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness,
2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=
MKSTRING(
"Cancel")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic,
'C'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition,
75); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition,
100); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
cancelBtn = XmCreatePushButton(btnForm,
"cancel", args, argcnt);
XmStringFree(st1);
XtAddCallback(cancelBtn, XmNactivateCallback,
(XtCallbackProc)rMultiFileCancelCB, window);
XtManageChild(cancelBtn);
argcnt =
0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomWidget, btnForm); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, label1); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset,
10); argcnt++;
XtSetArg(args[argcnt], XmNvisibleItemCount,
10); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNbottomOffset,
6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset,
10); argcnt++;
XtSetArg(args[argcnt], XmNselectionPolicy, XmMULTIPLE_SELECT); argcnt++;
list = XmCreateScrolledList(form,
"list_of_files", args, argcnt);
AddMouseWheelSupport(list);
XtManageChild(list);
XmAddTabGroup(list);
XmAddTabGroup(btnForm);
XmAddTabGroup(pathBtn);
XtVaSetValues(label1, XmNuserData, list,
NULL);
XtVaSetValues(form, XmNcancelButton, cancelBtn,
NULL);
AddDialogMnemonicHandler(form,
FALSE);
window->replaceMultiFileDlog = form;
window->replaceMultiFileList = list;
window->replaceMultiFilePathBtn = pathBtn;
XtAddCallback(form, XmNunmapCallback,
(XtCallbackProc)freeWritableWindowsCB, window);
}
static void checkMultiReplaceListForDoomedW(WindowInfo* window,
WindowInfo* doomedWindow)
{
WindowInfo *w;
int i;
if (window == doomedWindow) {
XtUnmanageChild(window->replaceMultiFileDlog);
return;
}
for (i =
0; i < window->nWritableWindows; ++i) {
w = window->writableWindows[i];
if (w == doomedWindow) {
removeDoomedWindowFromList(window, i);
break;
}
}
}
static void removeDoomedWindowFromList(WindowInfo* window,
int index)
{
int entriesToMove;
if (window->nWritableWindows <=
1) {
XtUnmanageChild(window->replaceMultiFileDlog);
return;
}
entriesToMove = window->nWritableWindows - index -
1;
memmove(&(window->writableWindows[index]),
&(window->writableWindows[index+
1]),
(
size_t)(entriesToMove*
sizeof(WindowInfo*)));
window->nWritableWindows -=
1;
XmListDeletePos(window->replaceMultiFileList, index +
1);
}
static void fFocusCB(Widget w, WindowInfo *window,
caddr_t *callData)
{
window = WidgetToWindow(w);
SET_ONE_RSRC(window->findDlog, XmNdefaultButton, window->findBtn);
}
static void rFocusCB(Widget w, WindowInfo *window,
caddr_t *callData)
{
window = WidgetToWindow(w);
SET_ONE_RSRC(window->replaceDlog, XmNdefaultButton, window->replaceBtn);
}
static void rKeepCB(Widget w, WindowInfo *window,
caddr_t *callData)
{
char title[
MAXPATHLEN +
19];
window = WidgetToWindow(w);
if (XmToggleButtonGetState(w)) {
snprintf(title,
sizeof(title),
"Replace/Find (in %s)", window->filename);
XtVaSetValues(XtParent(window->replaceDlog), XmNtitle, title,
NULL);
}
else
XtVaSetValues(XtParent(window->replaceDlog), XmNtitle,
"Replace/Find",
NULL);
}
static void fKeepCB(Widget w, WindowInfo *window,
caddr_t *callData)
{
char title[
MAXPATHLEN +
11];
window = WidgetToWindow(w);
if (XmToggleButtonGetState(w)) {
snprintf(title,
sizeof(title),
"Find (in %s)", window->filename);
XtVaSetValues(XtParent(window->findDlog), XmNtitle, title,
NULL);
}
else
XtVaSetValues(XtParent(window->findDlog), XmNtitle,
"Find",
NULL);
}
static void replaceCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char searchString[
SEARCHMAX], replaceString[
SEARCHMAX];
int direction, searchType;
char *params[
5];
window = WidgetToWindow(w);
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
resetReplaceTabGroup(window);
params[
0] = searchString;
params[
1] = replaceString;
params[
2] = directionArg(direction);
params[
3] = searchTypeArg(searchType);
params[
4] = searchWrapArg(GetPrefSearchWraps());
windowNotToClose = window;
XtCallActionProc(window->lastFocus,
"replace", callData->event, params,
5);
windowNotToClose =
NULL;
if (!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
}
static void replaceAllCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char searchString[
SEARCHMAX], replaceString[
SEARCHMAX];
int direction, searchType;
char *params[
3];
window = WidgetToWindow(w);
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
resetReplaceTabGroup(window);
params[
0] = searchString;
params[
1] = replaceString;
params[
2] = searchTypeArg(searchType);
windowNotToClose = window;
XtCallActionProc(window->lastFocus,
"replace_all", callData->event,
params,
3);
windowNotToClose =
NULL;
if (!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
}
static void replaceMultiFileCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
DoReplaceMultiFileDlog(window);
}
static void freeWritableWindowsCB(Widget w, WindowInfo* window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
NEditFree(window->writableWindows);
window->writableWindows =
NULL;
window->nWritableWindows =
0;
}
static int compareWindowNames(
const void *windowA,
const void *windowB)
{
return strcmp((*((WindowInfo**)windowA))->filename,
(*((WindowInfo**)windowB))->filename);
}
static int countWindows(
void)
{
int nWindows;
const WindowInfo *w;
for (w=WindowList, nWindows=
0; w!=
NULL; w=w->next, ++nWindows);
return nWindows;
}
static int countWritableWindows(
void)
{
int nWritable, nBefore, nAfter;
WindowInfo *w;
nBefore = countWindows();
for (w=WindowList, nWritable=
0; w!=
NULL; w=w->next) {
CheckForChangesToFile(w);
nAfter = countWindows();
if (nAfter != nBefore) {
nBefore = nAfter;
w = WindowList;
nWritable =
0;
continue;
}
if (!
IS_ANY_LOCKED(w->lockReasons)) ++nWritable;
}
return nWritable;
}
static void collectWritableWindows(WindowInfo* window)
{
int nWritable = countWritableWindows();
int i;
WindowInfo *w;
WindowInfo **windows;
NEditFree(window->writableWindows);
windows = (WindowInfo **)NEditMalloc(
sizeof(WindowInfo *) * nWritable);
for (w=WindowList, i=
0; w!=
NULL; w=w->next)
if (!
IS_ANY_LOCKED(w->lockReasons)) windows[i++] = w;
qsort(windows, nWritable,
sizeof(WindowInfo *), compareWindowNames);
window->writableWindows = windows;
window->nWritableWindows = nWritable;
}
static void rMultiFileReplaceCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char searchString[
SEARCHMAX], replaceString[
SEARCHMAX];
int direction, searchType;
char *params[
4];
int nSelected, i;
WindowInfo *writableWin;
Bool replaceFailed, noWritableLeft;
window = WidgetToWindow(w);
nSelected =
0;
for (i=
0; i<window->nWritableWindows; ++i)
if (XmListPosSelected(window->replaceMultiFileList, i+
1))
++nSelected;
if (!nSelected)
{
DialogF(
DF_INF, XtParent(window->replaceMultiFileDlog),
1,
"No Files",
"No files selected!",
"OK");
return;
}
resetReplaceTabGroup(window);
if (DialogF(
DF_QUES, window->shell,
2,
"Multi-File Replacement",
"Multi-file replacements are difficult to undo.\n"
"Proceed with the replacement ?",
"Yes",
"Cancel") !=
1)
{
XtUnmanageChild(window->replaceMultiFileDlog);
return;
}
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
resetReplaceTabGroup(window);
params[
0] = searchString;
params[
1] = replaceString;
params[
2] = searchTypeArg(searchType);
replaceFailed = True;
noWritableLeft = True;
for (i=
0; i<window->nWritableWindows; ++i) {
writableWin = window->writableWindows[i];
if (XmListPosSelected(window->replaceMultiFileList, i+
1)) {
if (!
IS_ANY_LOCKED(writableWin->lockReasons)) {
noWritableLeft = False;
writableWin->multiFileReplSelected = True;
writableWin->multiFileBusy = True;
writableWin->replaceFailed = False;
XtCallActionProc(writableWin->lastFocus,
"replace_all",
callData->event, params,
3);
writableWin->multiFileBusy = False;
if (!writableWin->replaceFailed)
replaceFailed = False;
}
}
else {
writableWin->multiFileReplSelected = False;
}
}
if (!XmToggleButtonGetState(window->replaceKeepBtn)) {
unmanageReplaceDialogs(window);
}
else {
XtUnmanageChild(window->replaceMultiFileDlog);
}
if (replaceFailed) {
if (GetPrefSearchDlogs()) {
if (noWritableLeft) {
DialogF(
DF_INF, window->shell,
1,
"Read-only Files",
"All selected files have become read-only.",
"OK");
}
else {
DialogF(
DF_INF, window->shell,
1,
"String not found",
"String was not found",
"OK");
}
}
else {
XBell(TheDisplay,
0);
}
}
}
static void rMultiFileCancelCB(Widget w, WindowInfo *window,
caddr_t callData)
{
window = WidgetToWindow(w);
resetReplaceTabGroup(window);
XtUnmanageChild(window->replaceMultiFileDlog);
}
static void rMultiFileSelectAllCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
int i;
char policy;
Widget list;
window = WidgetToWindow(w);
list = window->replaceMultiFileList;
XtVaGetValues(list, XmNselectionPolicy, &policy,
NULL);
XtVaSetValues(list, XmNselectionPolicy, XmMULTIPLE_SELECT,
NULL);
XmListDeselectAllItems(window->replaceMultiFileList);
for (i=
0; i<window->nWritableWindows; ++i) {
XmListSelectPos(list, i+
1,
FALSE);
}
XtVaSetValues(list, XmNselectionPolicy, policy,
NULL);
}
static void rMultiFileDeselectAllCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
XmListDeselectAllItems(window->replaceMultiFileList);
}
static void rMultiFilePathCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
uploadFileListItems(window, True);
}
static void uploadFileListItems(WindowInfo* window, Bool replace)
{
XmStringTable names;
int nWritable, i, *selected, selectedCount;
char buf[
2*
MAXPATHLEN], policy;
Bool usePathNames;
WindowInfo *w;
Widget list;
nWritable = window->nWritableWindows;
list = window->replaceMultiFileList;
names = (XmStringTable) NEditMalloc(nWritable *
sizeof(XmString*));
usePathNames = XmToggleButtonGetState(window->replaceMultiFilePathBtn);
for (i =
0; i < nWritable; ++i) {
w = window->writableWindows[i];
if (usePathNames && window->filenameSet) {
snprintf(buf,
sizeof(buf),
"%s%s", w->path, w->filename);
}
else {
snprintf(buf,
sizeof(buf),
"%s", w->filename);
}
names[i] = XmStringCreateSimple(buf);
}
XtVaGetValues(list, XmNselectionPolicy, &policy,
NULL);
XtVaSetValues(list, XmNselectionPolicy, XmMULTIPLE_SELECT,
NULL);
if (replace) {
XmListGetSelectedPos(list, &selected, &selectedCount);
XmListReplaceItemsPos(list, names, nWritable,
1);
XmListDeselectAllItems(list);
for (i =
0; i < selectedCount; ++i) {
XmListSelectPos(list, selected[i], False);
}
NEditFree(selected);
}
else {
Arg args[
1];
int nVisible;
int firstSelected =
0;
XmListDeleteAllItems(list);
XmListAddItems(list, names, nWritable,
1);
selectedCount =
0;
for (i =
0; i < nWritable; ++i) {
if (window->writableWindows[i]->multiFileReplSelected) {
XmListSelectPos(list, i+
1, False);
++selectedCount;
if (firstSelected ==
0) firstSelected = i+
1;
}
}
if (selectedCount ==
0) {
for (i =
0; i < nWritable; ++i) {
XmListSelectPos(list, i+
1, False);
}
firstSelected =
1;
}
XtSetArg(args[
0], XmNvisibleItemCount, &nVisible);
XtGetValues(list, args,
1);
if (nWritable <= nVisible) {
firstSelected =
1;
}
else {
int maxFirst = nWritable - nVisible +
1;
if (firstSelected > maxFirst)
firstSelected = maxFirst;
}
XmListSetPos(list, firstSelected);
}
XtVaSetValues(list, XmNselectionPolicy, policy,
NULL);
for (i =
0; i < nWritable; ++i)
XmStringFree(names[i]);
NEditFree(names);
}
static void unmanageReplaceDialogs(
const WindowInfo *window)
{
if (window->replaceMultiFileDlog &&
XtIsManaged(window->replaceMultiFileDlog)) {
XtUnmanageChild(window->replaceMultiFileDlog);
}
if (window->replaceDlog &&
XtIsManaged(window->replaceDlog)) {
XtUnmanageChild(window->replaceDlog);
}
}
static void rInSelCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char searchString[
SEARCHMAX], replaceString[
SEARCHMAX];
int direction, searchType;
char *params[
3];
window = WidgetToWindow(w);
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
resetReplaceTabGroup(window);
params[
0] = searchString;
params[
1] = replaceString;
params[
2] = searchTypeArg(searchType);
windowNotToClose = window;
XtCallActionProc(window->lastFocus,
"replace_in_selection",
callData->event, params,
3);
windowNotToClose =
NULL;
if (!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
}
static void rCancelCB(Widget w, WindowInfo *window,
caddr_t callData)
{
window = WidgetToWindow(w);
resetReplaceTabGroup(window);
unmanageReplaceDialogs(window);
}
static void fCancelCB(Widget w, WindowInfo *window,
caddr_t callData)
{
window = WidgetToWindow(w);
resetFindTabGroup(window);
XtUnmanageChild(window->findDlog);
}
static void rFindCB(Widget w, WindowInfo *window,XmAnyCallbackStruct *callData)
{
char searchString[
SEARCHMAX], replaceString[
SEARCHMAX];
int direction, searchType;
char *params[
4];
window = WidgetToWindow(w);
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
resetReplaceTabGroup(window);
params[
0] = searchString;
params[
1] = directionArg(direction);
params[
2] = searchTypeArg(searchType);
params[
3] = searchWrapArg(GetPrefSearchWraps());
windowNotToClose = window;
XtCallActionProc(window->lastFocus,
"find", callData->event, params,
4);
windowNotToClose =
NULL;
if (historyIndex(
1) != -
1 &&
!strcmp(SearchHistory[historyIndex(
1)], searchString)) {
NEditFree(ReplaceHistory[historyIndex(
1)]);
ReplaceHistory[historyIndex(
1)] = NEditStrdup(replaceString);
}
if (!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
}
static void replaceFindCB(Widget w, WindowInfo *window, XmAnyCallbackStruct *callData)
{
char searchString[
SEARCHMAX+
1], replaceString[
SEARCHMAX+
1];
int direction, searchType;
char *params[
4];
window = WidgetToWindow(w);
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
resetReplaceTabGroup(window);
params[
0] = searchString;
params[
1] = replaceString;
params[
2] = directionArg(direction);
params[
3] = searchTypeArg(searchType);
windowNotToClose = window;
XtCallActionProc(window->lastFocus,
"replace_find", callData->event, params,
4);
windowNotToClose =
NULL;
if (!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
}
static void rSetActionButtons(WindowInfo* window,
int replaceBtn,
int replaceFindBtn,
int replaceAndFindBtn,
#ifndef REPLACE_SCOPE
int replaceInWinBtn,
int replaceInSelBtn,
#endif
int replaceAllBtn)
{
XtSetSensitive(window->replaceBtn, replaceBtn);
XtSetSensitive(window->replaceFindBtn, replaceFindBtn);
XtSetSensitive(window->replaceAndFindBtn, replaceAndFindBtn);
#ifndef REPLACE_SCOPE
XtSetSensitive(window->replaceInWinBtn, replaceInWinBtn);
XtSetSensitive(window->replaceInSelBtn, replaceInSelBtn);
#endif
XtSetSensitive(window->replaceAllBtn, replaceAllBtn);
}
void UpdateReplaceActionButtons(WindowInfo* window)
{
int searchText = textFieldNonEmpty(window->replaceText);
#ifdef REPLACE_SCOPE
switch (window->replaceScope)
{
case REPL_SCOPE_WIN:
rSetActionButtons(window, searchText, searchText, searchText, searchText);
break;
case REPL_SCOPE_SEL:
rSetActionButtons(window, False, False, False, searchText && window->wasSelected);
break;
case REPL_SCOPE_MULTI:
rSetActionButtons(window, False, False, False, searchText);
break;
}
#else
rSetActionButtons(window, searchText, searchText, searchText,
searchText, searchText && window->wasSelected,
searchText && (countWritableWindows() >
1));
#endif
}
#ifdef REPLACE_SCOPE
static void rScopeWinCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
if (XmToggleButtonGetState(window->replaceScopeWinToggle)) {
window->replaceScope =
REPL_SCOPE_WIN;
UpdateReplaceActionButtons(window);
}
}
static void rScopeSelCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
if (XmToggleButtonGetState(window->replaceScopeSelToggle)) {
window->replaceScope =
REPL_SCOPE_SEL;
UpdateReplaceActionButtons(window);
}
}
static void rScopeMultiCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
if (XmToggleButtonGetState(window->replaceScopeMultiToggle)) {
window->replaceScope =
REPL_SCOPE_MULTI;
UpdateReplaceActionButtons(window);
}
}
static void replaceAllScopeCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
switch(window->replaceScope) {
case REPL_SCOPE_WIN:
replaceAllCB(w, window, callData);
break;
case REPL_SCOPE_SEL:
rInSelCB(w, window, callData);
break;
case REPL_SCOPE_MULTI:
replaceMultiFileCB(w, window, callData);
break;
}
}
#endif
static int textFieldNonEmpty(Widget w)
{
char *str = XNETextGetString(w);
int nonEmpty = (str[
0] !=
'\0');
NEditFree(str);
return(nonEmpty);
}
static void rFindTextValueChangedCB(Widget w, WindowInfo *window, XKeyEvent *event)
{
window = WidgetToWindow(w);
UpdateReplaceActionButtons(window);
}
static void rFindArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event)
{
KeySym keysym = XLookupKeysym(event,
0);
int index;
char *searchStr, *replaceStr;
int searchType;
window = WidgetToWindow(w);
index = window->rHistIndex;
if (keysym != XK_Up && keysym != XK_Down)
return;
index += (keysym == XK_Up) ?
1 : -
1;
if (index !=
0 && historyIndex(index) == -
1) {
XBell(TheDisplay,
0);
return;
}
window = WidgetToWindow(w);
if (index ==
0) {
searchStr =
"";
replaceStr =
"";
searchType = GetPrefSearch();
}
else {
searchStr = SearchHistory[historyIndex(index)];
replaceStr = ReplaceHistory[historyIndex(index)];
searchType = SearchTypeHistory[historyIndex(index)];
}
initToggleButtons(searchType, window->replaceRegexToggle,
window->replaceCaseToggle, &window->replaceWordToggle,
&window->replaceLastLiteralCase,
&window->replaceLastRegexCase);
XNETextSetString(window->replaceText, searchStr);
XNETextSetString(window->replaceWithText, replaceStr);
UpdateReplaceActionButtons(window);
window->rHistIndex = index;
}
static void replaceArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event)
{
KeySym keysym = XLookupKeysym(event,
0);
int index;
window = WidgetToWindow(w);
index = window->rHistIndex;
if (keysym != XK_Up && keysym != XK_Down)
return;
index += (keysym == XK_Up) ?
1 : -
1;
if (index !=
0 && historyIndex(index) == -
1) {
XBell(TheDisplay,
0);
return;
}
window = WidgetToWindow(w);
if (index ==
0)
XNETextSetString(window->replaceWithText,
"");
else
XNETextSetString(window->replaceWithText,
ReplaceHistory[historyIndex(index)]);
window->rHistIndex = index;
}
static void fUpdateActionButtons(WindowInfo *window)
{
int buttonState = textFieldNonEmpty(window->findText);
XtSetSensitive(window->findBtn, buttonState);
}
static void findTextValueChangedCB(Widget w, WindowInfo *window, XKeyEvent *event)
{
window = WidgetToWindow(w);
fUpdateActionButtons(window);
}
static void findArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event)
{
KeySym keysym = XLookupKeysym(event,
0);
int index;
char *searchStr;
int searchType;
window = WidgetToWindow(w);
index = window->fHistIndex;
if (keysym != XK_Up && keysym != XK_Down)
return;
index += (keysym == XK_Up) ?
1 : -
1;
if (index !=
0 && historyIndex(index) == -
1) {
XBell(TheDisplay,
0);
return;
}
if (index ==
0) {
searchStr =
"";
searchType = GetPrefSearch();
}
else {
searchStr = SearchHistory[historyIndex(index)];
searchType = SearchTypeHistory[historyIndex(index)];
}
initToggleButtons(searchType, window->findRegexToggle,
window->findCaseToggle, &window->findWordToggle,
&window->findLastLiteralCase,
&window->findLastRegexCase);
XNETextSetString(window->findText, searchStr);
fUpdateActionButtons(window);
window->fHistIndex = index;
}
static void findCB(Widget w, WindowInfo *window,XmAnyCallbackStruct *callData)
{
char searchString[
SEARCHMAX];
int direction, searchType;
char *params[
4];
window = WidgetToWindow(w);
if (!getFindDlogInfo(window, &direction, searchString, &searchType))
return;
resetFindTabGroup(window);
params[
0] = searchString;
params[
1] = directionArg(direction);
params[
2] = searchTypeArg(searchType);
params[
3] = searchWrapArg(GetPrefSearchWraps());
windowNotToClose = window;
XtCallActionProc(window->lastFocus,
"find", callData->event, params,
4);
windowNotToClose =
NULL;
if (!XmToggleButtonGetState(window->findKeepBtn))
XtUnmanageChild(window->findDlog);
}
static int getReplaceDlogInfo(WindowInfo *window,
int *direction,
char *searchString,
char *replaceString,
int *searchType)
{
char *replaceText, *replaceWithText;
regexp *compiledRE =
NULL;
char *compileMsg;
replaceText = XNETextGetString(window->replaceText);
replaceWithText = XNETextGetString(window->replaceWithText);
if(XmToggleButtonGetState(window->replaceRegexToggle)) {
int regexDefault;
if(XmToggleButtonGetState(window->replaceCaseToggle)) {
*searchType =
SEARCH_REGEX;
regexDefault =
REDFLT_STANDARD;
}
else {
*searchType =
SEARCH_REGEX_NOCASE;
regexDefault =
REDFLT_CASE_INSENSITIVE;
}
compiledRE = CompileRE(replaceText, &compileMsg, regexDefault);
if (compiledRE ==
NULL) {
DialogF(
DF_WARN, XtParent(window->replaceDlog),
1,
"Search String",
"Please respecify the search string:\n%s",
"OK", compileMsg);
NEditFree(replaceText);
NEditFree(replaceWithText);
return FALSE;
}
NEditFree(compiledRE);
}
else {
if(XmToggleButtonGetState(window->replaceCaseToggle)) {
if(XmToggleButtonGetState(window->replaceWordToggle))
*searchType =
SEARCH_CASE_SENSE_WORD;
else
*searchType =
SEARCH_CASE_SENSE;
}
else {
if(XmToggleButtonGetState(window->replaceWordToggle))
*searchType =
SEARCH_LITERAL_WORD;
else
*searchType =
SEARCH_LITERAL;
}
}
*direction = XmToggleButtonGetState(window->replaceRevToggle) ?
SEARCH_BACKWARD :
SEARCH_FORWARD;
if (strlen(replaceText) >=
SEARCHMAX) {
DialogF(
DF_WARN, XtParent(window->replaceDlog),
1,
"String too long",
"Search string too long.",
"OK");
NEditFree(replaceText);
NEditFree(replaceWithText);
return FALSE;
}
if (strlen(replaceWithText) >=
SEARCHMAX) {
DialogF(
DF_WARN, XtParent(window->replaceDlog),
1,
"String too long",
"Replace string too long.",
"OK");
NEditFree(replaceText);
NEditFree(replaceWithText);
return FALSE;
}
strcpy(searchString, replaceText);
strcpy(replaceString, replaceWithText);
NEditFree(replaceText);
NEditFree(replaceWithText);
return TRUE;
}
static int getFindDlogInfo(WindowInfo *window,
int *direction,
char *searchString,
int *searchType)
{
char *findText;
regexp *compiledRE =
NULL;
char *compileMsg;
findText = XNETextGetString(window->findText);
if(XmToggleButtonGetState(window->findRegexToggle)) {
int regexDefault;
if(XmToggleButtonGetState(window->findCaseToggle)) {
*searchType =
SEARCH_REGEX;
regexDefault =
REDFLT_STANDARD;
}
else {
*searchType =
SEARCH_REGEX_NOCASE;
regexDefault =
REDFLT_CASE_INSENSITIVE;
}
compiledRE = CompileRE(findText, &compileMsg, regexDefault);
if (compiledRE ==
NULL) {
DialogF(
DF_WARN, XtParent(window->findDlog),
1,
"Regex Error",
"Please respecify the search string:\n%s",
"OK", compileMsg);
return FALSE;
}
NEditFree(compiledRE);
}
else {
if(XmToggleButtonGetState(window->findCaseToggle)) {
if(XmToggleButtonGetState(window->findWordToggle))
*searchType =
SEARCH_CASE_SENSE_WORD;
else
*searchType =
SEARCH_CASE_SENSE;
}
else {
if(XmToggleButtonGetState(window->findWordToggle))
*searchType =
SEARCH_LITERAL_WORD;
else
*searchType =
SEARCH_LITERAL;
}
}
*direction = XmToggleButtonGetState(window->findRevToggle) ?
SEARCH_BACKWARD :
SEARCH_FORWARD;
if (isRegexType(*searchType)) {
}
if (strlen(findText) >=
SEARCHMAX) {
DialogF(
DF_WARN, XtParent(window->findDlog),
1,
"String too long",
"Search string too long.",
"OK");
NEditFree(findText);
return FALSE;
}
strcpy(searchString, findText);
NEditFree(findText);
return TRUE;
}
int SearchAndSelectSame(WindowInfo *window,
int direction,
int searchWrap)
{
if (NHist <
1) {
XBell(TheDisplay,
0);
return FALSE;
}
return SearchAndSelect(window, direction, SearchHistory[historyIndex(
1)],
SearchTypeHistory[historyIndex(
1)], searchWrap);
}
int SearchAndSelect(WindowInfo *window,
int direction,
const char *searchString,
int searchType,
int searchWrap)
{
int startPos, endPos;
int beginPos, cursorPos, selStart, selEnd;
int movedFwd =
0;
saveSearchHistory(searchString,
NULL, searchType,
FALSE);
if (searchMatchesSelection(window, searchString, searchType,
&selStart, &selEnd,
NULL,
NULL)) {
if (direction ==
SEARCH_BACKWARD) {
beginPos = selStart -
1;
}
else {
beginPos = selStart +
1;
movedFwd =
1;
}
}
else {
selStart = -
1; selEnd = -
1;
cursorPos = TextGetCursorPos(window->lastFocus);
if (direction ==
SEARCH_BACKWARD) {
beginPos = cursorPos-
1;
}
else {
beginPos = cursorPos;
}
}
iSearchRecordLastBeginPos(window, direction, beginPos);
if (!SearchWindow(window, direction, searchString, searchType, searchWrap,
beginPos, &startPos, &endPos,
NULL,
NULL))
return FALSE;
if (direction==
SEARCH_FORWARD && beginPos==startPos && beginPos==endPos) {
if (!movedFwd &&
!SearchWindow(window, direction, searchString, searchType,
searchWrap, beginPos+
1, &startPos, &endPos,
NULL,
NULL))
return FALSE;
}
if (selStart==startPos && selEnd==endPos) {
XBell(TheDisplay,
0);
return FALSE;
}
BufSelect(window->buffer, startPos, endPos);
MakeSelectionVisible(window, window->lastFocus);
TextSetCursorPos(window->lastFocus, endPos);
return TRUE;
}
void SearchForSelected(WindowInfo *window,
int direction,
int searchType,
int searchWrap, Time time)
{
SearchSelectedCallData *callData = XtNew(SearchSelectedCallData);
callData->direction = direction;
callData->searchType = searchType;
callData->searchWrap = searchWrap;
XtGetSelectionValue(window->textArea,
XA_PRIMARY,
XA_STRING,
selectedSearchCB, callData, time);
}
static void selectedSearchCB(Widget w, XtPointer callData, Atom *selection,
Atom *type, XtPointer v,
unsigned long *length,
int *format)
{
WindowInfo *window = WidgetToWindow(w);
SearchSelectedCallData *callDataItems = (SearchSelectedCallData *)callData;
char *value = v;
int searchType;
char searchString[
SEARCHMAX+
1];
window = WidgetToWindow(w);
if (*type ==
XT_CONVERT_FAIL || value ==
NULL) {
if (GetPrefSearchDlogs())
DialogF(
DF_WARN, window->shell,
1,
"Wrong Selection",
"Selection not appropriate for searching",
"OK");
else
XBell(TheDisplay,
0);
NEditFree(callData);
return;
}
if (*length >
SEARCHMAX) {
if (GetPrefSearchDlogs())
DialogF(
DF_WARN, window->shell,
1,
"Selection too long",
"Selection too long",
"OK");
else
XBell(TheDisplay,
0);
NEditFree(value);
NEditFree(callData);
return;
}
if (*length ==
0) {
XBell(TheDisplay,
0);
NEditFree(value);
NEditFree(callData);
return;
}
if (*format !=
8) {
fprintf(stderr,
"XNEdit: can''t handle non 8-bit text\n");
XBell(TheDisplay,
0);
NEditFree(value);
NEditFree(callData);
return;
}
strncpy(searchString, value, *length);
searchString[*length] =
'\0';
NEditFree(value);
searchType = callDataItems->searchType;
if (searchType ==
SEARCH_REGEX )
searchType =
SEARCH_CASE_SENSE;
else if (searchType ==
SEARCH_REGEX_NOCASE)
searchType =
SEARCH_LITERAL;
SearchAndSelect(window, callDataItems->direction, searchString,
searchType, callDataItems->searchWrap);
NEditFree(callData);
}
void BeginISearch(WindowInfo *window,
int direction)
{
ReadSearchHistory();
window->iSearchStartPos = -
1;
XNETextSetString(window->iSearchText,
"");
XmToggleButtonSetState(window->iSearchRevToggle,
direction ==
SEARCH_BACKWARD,
FALSE);
TempShowISearch(window,
TRUE);
XmProcessTraversal(window->iSearchText, XmTRAVERSE_CURRENT);
}
void EndISearch(WindowInfo *window)
{
window->iSearchStartPos = -
1;
saveSearchHistory(
"",
NULL,
0,
FALSE);
TempShowISearch(window,
FALSE);
}
static void iSearchRecordLastBeginPos(WindowInfo *window,
int direction,
int initPos)
{
window->iSearchLastBeginPos = initPos;
if (direction ==
SEARCH_BACKWARD)
window->iSearchLastBeginPos--;
}
int SearchAndSelectIncremental(WindowInfo *window,
int direction,
const char *searchString,
int searchType,
int searchWrap,
int continued)
{
int beginPos, startPos, endPos;
if (!continued || window->iSearchStartPos == -
1) {
window->iSearchStartPos = TextGetCursorPos(window->lastFocus);
iSearchRecordLastBeginPos(window, direction, window->iSearchStartPos);
}
beginPos = window->iSearchStartPos;
if(searchString[
0] ==
0) {
int beepBeginPos = (direction ==
SEARCH_BACKWARD) ? beginPos-
1:beginPos;
iSearchTryBeepOnWrap(window, direction, beepBeginPos, beepBeginPos);
iSearchRecordLastBeginPos(window, direction, window->iSearchStartPos);
BufUnselect(window->buffer);
TextSetCursorPos(window->lastFocus, beginPos);
return TRUE;
}
if(!(window->iSearchHistIndex >
1 && !strcmp(searchString,
SearchHistory[historyIndex(window->iSearchHistIndex)]))) {
saveSearchHistory(searchString,
NULL, searchType,
TRUE);
window->iSearchHistIndex =
1;
}
if (direction ==
SEARCH_BACKWARD)
beginPos--;
if (!SearchWindow(window, direction, searchString, searchType, searchWrap,
beginPos, &startPos, &endPos,
NULL,
NULL))
return FALSE;
window->iSearchLastBeginPos = startPos;
if (direction==
SEARCH_FORWARD && beginPos==startPos && beginPos==endPos)
if (!SearchWindow(window, direction, searchString, searchType, searchWrap,
beginPos+
1, &startPos, &endPos,
NULL,
NULL))
return FALSE;
window->iSearchLastBeginPos = startPos;
BufSelect(window->buffer, startPos, endPos);
MakeSelectionVisible(window, window->lastFocus);
TextSetCursorPos(window->lastFocus, endPos);
return TRUE;
}
void SetISearchTextCallbacks(WindowInfo *window)
{
static XtTranslations tableText =
NULL;
static char *translationsText =
"Shift<KeyPress>Return: activate()\n";
static XtTranslations tableClear =
NULL;
static char *translationsClear =
"<Btn2Down>:Arm()\n<Btn2Up>: isearch_clear_and_paste() Disarm()\n";
static XtActionsRec actions[] = {
{
"isearch_clear_and_paste", iSearchTextClearAndPasteAP }
};
if (tableText ==
NULL)
tableText = XtParseTranslationTable(translationsText);
XtOverrideTranslations(window->iSearchText, tableText);
if (tableClear ==
NULL) {
XtAppAddActions(XtWidgetToApplicationContext(window->iSearchText),
actions, XtNumber(actions));
tableClear = XtParseTranslationTable(translationsClear);
}
XtOverrideTranslations(window->iSearchClearButton, tableClear);
XtAddCallback(window->iSearchText, XmNactivateCallback,
(XtCallbackProc)iSearchTextActivateCB, window);
XtAddCallback(window->iSearchText, XmNvalueChangedCallback,
(XtCallbackProc)iSearchTextValueChangedCB, window);
XtAddEventHandler(window->iSearchText, KeyPressMask, False,
(XtEventHandler)iSearchTextKeyEH, window);
XtAddCallback(window->iSearchCaseToggle, XmNvalueChangedCallback,
(XtCallbackProc)iSearchCaseToggleCB, window);
XtAddCallback(window->iSearchRegexToggle, XmNvalueChangedCallback,
(XtCallbackProc)iSearchRegExpToggleCB, window);
XtAddCallback(window->iSearchCaseToggle, XmNvalueChangedCallback,
(XtCallbackProc)iSearchTextValueChangedCB, window);
XtAddCallback(window->iSearchRegexToggle, XmNvalueChangedCallback,
(XtCallbackProc)iSearchTextValueChangedCB, window);
XtAddCallback(window->iSearchRevToggle, XmNvalueChangedCallback,
(XtCallbackProc)iSearchTextValueChangedCB, window);
XtAddCallback(window->iSearchFindButton, XmNactivateCallback,
(XtCallbackProc)iSearchTextActivateCB, window);
XtAddCallback(window->iSearchClearButton, XmNactivateCallback,
(XtCallbackProc)iSearchTextClearCB, window);
}
static void iSearchTextSetString(Widget w, WindowInfo *window,
char *str)
{
XtRemoveAllCallbacks(window->iSearchText, XmNvalueChangedCallback);
XtRemoveAllCallbacks(window->iSearchText, XmNactivateCallback);
XNETextSetString(window->iSearchText, str ? str :
"");
XtAddCallback(window->iSearchText, XmNactivateCallback,
(XtCallbackProc)iSearchTextActivateCB, window);
XtAddCallback(window->iSearchText, XmNvalueChangedCallback,
(XtCallbackProc)iSearchTextValueChangedCB, window);
}
static void iSearchTextClearAndPasteAP(Widget w, XEvent *event, String *args,
Cardinal *nArg)
{
WindowInfo *window;
char *selText;
XmAnyCallbackStruct cbdata;
memset(&cbdata,
0,
sizeof (cbdata));
cbdata.event = event;
window = WidgetToWindow(w);
selText = GetAnySelection(window);
iSearchTextSetString(w, window, selText);
if (selText) {
XNETextSetInsertionPosition(window->iSearchText, strlen(selText));
NEditFree(selText);
}
iSearchTextActivateCB(w, window, &cbdata);
}
static void iSearchTextClearCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
window = WidgetToWindow(w);
iSearchTextSetString(w, window,
NULL);
}
static void iSearchTextActivateCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char *params[
4];
char *searchString;
int searchType, direction;
window = WidgetToWindow(w);
searchString = XNETextGetString(window->iSearchText);
if(XmToggleButtonGetState(window->iSearchCaseToggle)) {
if(XmToggleButtonGetState(window->iSearchRegexToggle))
searchType =
SEARCH_REGEX;
else
searchType =
SEARCH_CASE_SENSE;
}
else {
if(XmToggleButtonGetState(window->iSearchRegexToggle))
searchType =
SEARCH_REGEX_NOCASE;
else
searchType =
SEARCH_LITERAL;
}
direction = XmToggleButtonGetState(window->iSearchRevToggle) ?
SEARCH_BACKWARD :
SEARCH_FORWARD;
if (callData->event->xbutton.state & (ShiftMask | ControlMask))
direction = direction ==
SEARCH_FORWARD ?
SEARCH_BACKWARD :
SEARCH_FORWARD;
params[
0] = searchString;
params[
1] = directionArg(direction);
params[
2] = searchTypeArg(searchType);
params[
3] = searchWrapArg(GetPrefSearchWraps());
XtCallActionProc(window->lastFocus,
"find", callData->event, params,
4);
NEditFree(searchString);
}
static void iSearchTextValueChangedCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char *params[
5];
char *searchString;
int searchType, direction, nParams;
window = WidgetToWindow(w);
searchString = XNETextGetString(window->iSearchText);
if(XmToggleButtonGetState(window->iSearchCaseToggle)) {
if(XmToggleButtonGetState(window->iSearchRegexToggle))
searchType =
SEARCH_REGEX;
else
searchType =
SEARCH_CASE_SENSE;
}
else {
if(XmToggleButtonGetState(window->iSearchRegexToggle))
searchType =
SEARCH_REGEX_NOCASE;
else
searchType =
SEARCH_LITERAL;
}
direction = XmToggleButtonGetState(window->iSearchRevToggle) ?
SEARCH_BACKWARD :
SEARCH_FORWARD;
if (isRegexType(searchType)) {
regexp *compiledRE =
NULL;
char *compileMsg;
compiledRE = CompileRE(searchString, &compileMsg,
defaultRegexFlags(searchType));
if (compiledRE ==
NULL) {
NEditFree(searchString);
return;
}
NEditFree(compiledRE);
}
nParams =
0;
params[nParams++] = searchString;
params[nParams++] = directionArg(direction);
params[nParams++] = searchTypeArg(searchType);
params[nParams++] = searchWrapArg(GetPrefSearchWraps());
if (window->iSearchStartPos != -
1)
params[nParams++] =
"continued";
XtCallActionProc(window->lastFocus,
"find_incremental",
callData->event, params, nParams);
NEditFree(searchString);
}
static void iSearchTextKeyEH(Widget w, WindowInfo *window,
XKeyEvent *event, Boolean *continueDispatch)
{
KeySym keysym = XLookupKeysym(event,
0);
int index;
char *searchStr;
int searchType;
if (keysym != XK_Up && keysym != XK_Down && keysym != XK_Escape) {
*continueDispatch =
TRUE;
return;
}
window = WidgetToWindow(w);
index = window->iSearchHistIndex;
*continueDispatch =
FALSE;
if (keysym == XK_Escape) {
XmProcessTraversal(window->lastFocus, XmTRAVERSE_CURRENT);
EndISearch(window);
return;
}
index += (keysym == XK_Up) ?
1 : -
1;
if (index !=
0 && historyIndex(index) == -
1) {
XBell(TheDisplay,
0);
return;
}
if (index ==
0) {
searchStr =
"";
searchType = GetPrefSearch();
}
else {
searchStr = SearchHistory[historyIndex(index)];
searchType = SearchTypeHistory[historyIndex(index)];
}
window->iSearchHistIndex = index;
initToggleButtons(searchType, window->iSearchRegexToggle,
window->iSearchCaseToggle,
NULL,
&window->iSearchLastLiteralCase,
&window->iSearchLastRegexCase);
XNETextSetString(window->iSearchText, searchStr);
XNETextSetInsertionPosition(window->iSearchText,
XNETextGetLastPosition(window->iSearchText));
}
void FlashMatching(WindowInfo *window, Widget textW)
{
char c;
void *style;
int pos, matchIndex;
int startPos, endPos, searchPos, matchPos;
int constrain;
if (window->flashTimeoutID !=
0) {
eraseFlash(window);
XtRemoveTimeOut(window->flashTimeoutID);
window->flashTimeoutID =
0;
}
if (window->showMatchingStyle ==
NO_FLASH) {
return;
}
if (window->buffer->primary.selected)
return;
pos = TextGetCursorPos(textW) -
1;
if (pos <
0)
return;
c = BufGetCharacter(window->buffer, pos);
style = GetHighlightInfo(window, pos);
for (matchIndex =
0; matchIndex<
N_FLASH_CHARS; matchIndex++) {
if (MatchingChars[matchIndex].c == c)
break;
}
if (matchIndex ==
N_FLASH_CHARS)
return;
constrain = ((window->nPanes ==
0) &&
(window->showMatchingStyle ==
FLASH_DELIMIT));
if (MatchingChars[matchIndex].direction ==
SEARCH_BACKWARD) {
startPos = constrain ? TextFirstVisiblePos(textW) :
0;
endPos = pos;
searchPos = endPos;
}
else {
startPos = pos;
endPos = constrain ? TextLastVisiblePos(textW) :
window->buffer->length;
searchPos = startPos;
}
if (!findMatchingChar(window, c, style, searchPos, startPos, endPos,
&matchPos))
return;
if (window->showMatchingStyle ==
FLASH_DELIMIT) {
BufHighlight(window->buffer, matchPos, matchPos+
1);
}
else {
if (MatchingChars[matchIndex].direction ==
SEARCH_BACKWARD) {
BufHighlight(window->buffer, matchPos, pos+
1);
}
else {
BufHighlight(window->buffer, matchPos+
1, pos);
}
}
window->flashTimeoutID = XtAppAddTimeOut(
XtWidgetToApplicationContext(window->shell),
1500,
flashTimeoutProc, window);
window->flashPos = matchPos;
}
void SelectToMatchingCharacter(WindowInfo *window)
{
int selStart, selEnd;
int startPos, endPos, matchPos;
textBuffer *buf = window->buffer;
if (!GetSimpleSelection(buf, &selStart, &selEnd)) {
selEnd = TextGetCursorPos(window->lastFocus);
if (window->overstrike)
selEnd +=
1;
selStart = selEnd -
1;
if (selStart <
0) {
XBell(TheDisplay,
0);
return;
}
}
if ((selEnd - selStart) !=
1) {
XBell(TheDisplay,
0);
return;
}
if (!findMatchingChar(window, BufGetCharacter(buf, selStart),
GetHighlightInfo(window, selStart), selStart,
0, buf->length, &matchPos)) {
XBell(TheDisplay,
0);
return;
}
startPos = (matchPos > selStart) ? selStart : matchPos;
endPos = (matchPos > selStart) ? matchPos : selStart;
XtVaSetValues(window->lastFocus, textNautoShowInsertPos, False,
NULL);
BufSelect(buf, startPos, endPos+
1);
MakeSelectionVisible(window, window->lastFocus);
XtVaSetValues(window->lastFocus, textNautoShowInsertPos, True,
NULL);
}
void GotoMatchingCharacter(WindowInfo *window)
{
int selStart, selEnd;
int matchPos;
textBuffer *buf = window->buffer;
if (!GetSimpleSelection(buf, &selStart, &selEnd)) {
selEnd = TextGetCursorPos(window->lastFocus);
if (window->overstrike)
selEnd +=
1;
selStart = selEnd -
1;
if (selStart <
0) {
XBell(TheDisplay,
0);
return;
}
}
if ((selEnd - selStart) !=
1) {
XBell(TheDisplay,
0);
return;
}
if (!findMatchingChar(window, BufGetCharacter(buf, selStart),
GetHighlightInfo(window, selStart), selStart,
0,
buf->length, &matchPos)) {
XBell(TheDisplay,
0);
return;
}
XtVaSetValues(window->lastFocus, textNautoShowInsertPos, False,
NULL);
TextSetCursorPos(window->lastFocus, matchPos+
1);
MakeSelectionVisible(window, window->lastFocus);
XtVaSetValues(window->lastFocus, textNautoShowInsertPos, True,
NULL);
}
static int findMatchingChar(WindowInfo *window,
char toMatch,
void* styleToMatch,
int charPos,
int startLimit,
int endLimit,
int *matchPos)
{
int nestDepth, matchIndex, direction, beginPos, pos;
char matchChar, c;
void *style =
NULL;
textBuffer *buf = window->buffer;
int matchSyntaxBased = window->matchSyntaxBased;
if (!matchSyntaxBased) style = styleToMatch;
for (matchIndex =
0; matchIndex<
N_MATCH_CHARS; matchIndex++) {
if (MatchingChars[matchIndex].c == toMatch)
break;
}
if (matchIndex ==
N_MATCH_CHARS)
return FALSE;
matchChar = MatchingChars[matchIndex].match;
direction = MatchingChars[matchIndex].direction;
beginPos = (direction==
SEARCH_FORWARD) ? charPos+
1 : charPos-
1;
nestDepth =
1;
if (direction ==
SEARCH_FORWARD) {
for (pos=beginPos; pos<endLimit; pos++) {
c=BufGetCharacter(buf, pos);
if (c == matchChar) {
if (matchSyntaxBased) style = GetHighlightInfo(window, pos);
if (style == styleToMatch) {
nestDepth--;
if (nestDepth ==
0) {
*matchPos = pos;
return TRUE;
}
}
}
else if (c == toMatch) {
if (matchSyntaxBased) style = GetHighlightInfo(window, pos);
if (style == styleToMatch)
nestDepth++;
}
}
}
else {
for (pos=beginPos; pos>=startLimit; pos--) {
c=BufGetCharacter(buf, pos);
if (c == matchChar) {
if (matchSyntaxBased) style = GetHighlightInfo(window, pos);
if (style == styleToMatch) {
nestDepth--;
if (nestDepth ==
0) {
*matchPos = pos;
return TRUE;
}
}
}
else if (c == toMatch) {
if (matchSyntaxBased) style = GetHighlightInfo(window, pos);
if (style == styleToMatch)
nestDepth++;
}
}
}
return FALSE;
}
static void flashTimeoutProc(XtPointer clientData, XtIntervalId *id)
{
eraseFlash((WindowInfo *)clientData);
((WindowInfo *)clientData)->flashTimeoutID =
0;
}
static void eraseFlash(WindowInfo *window)
{
BufUnhighlight(window->buffer);
}
int ReplaceSame(WindowInfo *window,
int direction,
int searchWrap)
{
if (NHist <
1) {
XBell(TheDisplay,
0);
return FALSE;
}
return SearchAndReplace(window, direction, SearchHistory[historyIndex(
1)],
ReplaceHistory[historyIndex(
1)],
SearchTypeHistory[historyIndex(
1)], searchWrap);
}
int ReplaceFindSame(WindowInfo *window,
int direction,
int searchWrap)
{
if (NHist <
1) {
XBell(TheDisplay,
0);
return FALSE;
}
return ReplaceAndSearch(window, direction, SearchHistory[historyIndex(
1)],
ReplaceHistory[historyIndex(
1)],
SearchTypeHistory[historyIndex(
1)], searchWrap);
}
int ReplaceAndSearch(WindowInfo *window,
int direction,
const char *searchString,
const char *replaceString,
int searchType,
int searchWrap)
{
int startPos =
0, endPos =
0, replaceLen =
0;
int searchExtentBW, searchExtentFW;
int replaced;
saveSearchHistory(searchString, replaceString, searchType,
FALSE);
replaced =
0;
if (searchMatchesSelection(window, searchString, searchType,
&startPos, &endPos, &searchExtentBW,
&searchExtentFW)) {
if (isRegexType(searchType)) {
char replaceResult[
SEARCHMAX+
1], *foundString;
foundString = BufGetRange(window->buffer, searchExtentBW,
searchExtentFW+
1);
replaceUsingRE(searchString, replaceString, foundString,
startPos-searchExtentBW,
replaceResult,
SEARCHMAX, startPos ==
0 ?
'\0' :
BufGetCharacter(window->buffer, startPos-
1),
GetWindowDelimiters(window), defaultRegexFlags(searchType));
NEditFree(foundString);
BufReplace(window->buffer, startPos, endPos, replaceResult);
replaceLen = strlen(replaceResult);
}
else {
BufReplace(window->buffer, startPos, endPos, replaceString);
replaceLen = strlen(replaceString);
}
TextSetCursorPos(window->lastFocus, startPos +
((direction ==
SEARCH_FORWARD) ? replaceLen :
0));
replaced =
1;
}
SearchAndSelect(window, direction, searchString, searchType, searchWrap);
return replaced;
}
int SearchAndReplace(WindowInfo *window,
int direction,
const char *searchString,
const char *replaceString,
int searchType,
int searchWrap)
{
int startPos, endPos, replaceLen, searchExtentBW, searchExtentFW;
int found;
int beginPos, cursorPos;
saveSearchHistory(searchString, replaceString, searchType,
FALSE);
if (!searchMatchesSelection(window, searchString, searchType,
&startPos, &endPos, &searchExtentBW, &searchExtentFW)) {
cursorPos = TextGetCursorPos(window->lastFocus);
if (direction ==
SEARCH_BACKWARD) {
beginPos = cursorPos-
1;
}
else {
beginPos = cursorPos;
}
found = SearchWindow(window, direction, searchString, searchType, searchWrap,
beginPos, &startPos, &endPos, &searchExtentBW, &searchExtentFW);
if (!found)
return FALSE;
}
if (isRegexType(searchType)) {
char replaceResult[
SEARCHMAX], *foundString;
foundString = BufGetRange(window->buffer, searchExtentBW, searchExtentFW+
1);
replaceUsingRE(searchString, replaceString, foundString,
startPos - searchExtentBW,
replaceResult,
SEARCHMAX, startPos ==
0 ?
'\0' :
BufGetCharacter(window->buffer, startPos-
1),
GetWindowDelimiters(window), defaultRegexFlags(searchType));
NEditFree(foundString);
BufReplace(window->buffer, startPos, endPos, replaceResult);
replaceLen = strlen(replaceResult);
}
else {
BufReplace(window->buffer, startPos, endPos, replaceString);
replaceLen = strlen(replaceString);
}
BufUnselect(window->buffer);
XtVaSetValues(window->lastFocus, textNautoShowInsertPos, False,
NULL);
TextSetCursorPos(window->lastFocus, startPos +
((direction ==
SEARCH_FORWARD) ? replaceLen :
0));
MakeSelectionVisible(window, window->lastFocus);
XtVaSetValues(window->lastFocus, textNautoShowInsertPos, True,
NULL);
return TRUE;
}
static Boolean prefOrUserCancelsSubst(
const Widget parent,
const Display* display)
{
Boolean cancel = True;
unsigned confirmResult =
0;
switch (GetPrefTruncSubstitution()) {
case TRUNCSUBST_SILENT:
cancel = True;
break;
case TRUNCSUBST_FAIL:
XBell((Display*) display,
0);
DialogF(
DF_INF, parent,
1,
"Substitution Failed",
"The result length of the substitution exceeded an internal limit.\n"
"The substitution is canceled.",
"OK");
cancel = True;
break;
case TRUNCSUBST_WARN:
XBell((Display*) display,
0);
confirmResult = DialogF(
DF_WARN, parent,
2,
"Substitution Failed",
"The result length of the substitution exceeded an internal limit.\n"
"Executing the substitution will result in loss of data.",
"Lose Data",
"Cancel");
cancel = (
1 != confirmResult);
break;
case TRUNCSUBST_IGNORE:
cancel = False;
break;
}
return cancel;
}
void ReplaceInSelection(
const WindowInfo* window,
const char* searchString,
const char* replaceString,
int searchType)
{
int selStart, selEnd, beginPos, startPos, endPos, realOffset, replaceLen;
int found, isRect, rectStart, rectEnd, lineStart, cursorPos;
int extentBW, extentFW;
char *fileString;
textBuffer *tempBuf;
Boolean substSuccess = False;
Boolean anyFound = False;
Boolean cancelSubst = True;
saveSearchHistory(searchString, replaceString, searchType,
FALSE);
if (!BufGetSelectionPos(window->buffer, &selStart, &selEnd, &isRect,
&rectStart, &rectEnd))
return;
if (isRect) {
selStart = BufStartOfLine(window->buffer, selStart);
selEnd = BufEndOfLine(window->buffer, selEnd);
fileString = BufGetRange(window->buffer, selStart, selEnd);
}
else
fileString = BufGetSelectionText(window->buffer);
tempBuf = BufCreate();
BufSetAll(tempBuf, fileString);
replaceLen = strlen(replaceString);
found =
TRUE;
beginPos =
0;
cursorPos =
0;
realOffset =
0;
while (found) {
found = SearchString(fileString, searchString,
SEARCH_FORWARD,
searchType,
FALSE, beginPos, &startPos, &endPos, &extentBW,
&extentFW, GetWindowDelimiters(window));
if (!found)
break;
anyFound = True;
if (isRect) {
lineStart = BufStartOfLine(window->buffer, selStart+startPos);
if (BufCountDispChars(window->buffer, lineStart, selStart+startPos) <
rectStart || BufCountDispChars(window->buffer, lineStart,
selStart+endPos) > rectEnd) {
if (fileString[endPos] ==
'\0')
break;
if (BufCountDispChars(window->buffer, lineStart,
selStart+startPos) < rectStart &&
BufCountDispChars(window->buffer, lineStart,
selStart+endPos) > rectStart)
beginPos +=
1;
else
beginPos = (startPos == endPos) ? endPos+
1 : endPos;
continue;
}
}
if (startPos == (selEnd - selStart)) {
found = False;
break;
}
if (isRegexType(searchType)) {
char replaceResult[
SEARCHMAX], *foundString;
foundString = BufGetRange(tempBuf, extentBW+realOffset,
extentFW+realOffset+
1);
substSuccess = replaceUsingRE(searchString, replaceString,
foundString, startPos - extentBW, replaceResult,
SEARCHMAX,
0 == (startPos + realOffset)
?
'\0'
: BufGetCharacter(tempBuf, startPos + realOffset -
1),
GetWindowDelimiters(window), defaultRegexFlags(searchType));
NEditFree(foundString);
if (!substSuccess) {
cancelSubst = prefOrUserCancelsSubst(window->shell, TheDisplay);
if (cancelSubst) {
break;
}
}
BufReplace(tempBuf, startPos+realOffset, endPos+realOffset,
replaceResult);
replaceLen = strlen(replaceResult);
}
else {
BufReplace(tempBuf, startPos+realOffset, endPos+realOffset,
replaceString);
substSuccess = True;
}
realOffset += replaceLen - (endPos - startPos);
beginPos = (startPos == endPos) ? endPos+
1 : endPos;
cursorPos = endPos;
if (fileString[endPos] ==
'\0')
break;
}
NEditFree(fileString);
if (anyFound) {
if (substSuccess || !cancelSubst) {
BufReplace(window->buffer, selStart, selEnd, BufAsString(tempBuf));
TextSetCursorPos(window->lastFocus, selStart + cursorPos + realOffset);
if (!isRect) {
BufSelect(window->buffer, selStart, selEnd + realOffset);
}
}
}
else {
if (GetPrefSearchDlogs()) {
if (window->findDlog && XtIsManaged(window->findDlog) &&
!XmToggleButtonGetState(window->findKeepBtn))
XtUnmanageChild(window->findDlog);
if (window->replaceDlog && XtIsManaged(window->replaceDlog) &&
!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
DialogF(
DF_INF, window->shell,
1,
"String not found",
"String was not found",
"OK");
}
else
XBell(TheDisplay,
0);
}
BufFree(tempBuf);
return;
}
int ReplaceAll(WindowInfo *window,
const char *searchString,
const char *replaceString,
int searchType)
{
const char *fileString;
char *newFileString;
int copyStart, copyEnd, replacementLen;
if (*searchString ==
'\0')
return FALSE;
saveSearchHistory(searchString, replaceString, searchType,
FALSE);
fileString = BufAsString(window->buffer);
newFileString = ReplaceAllInString(fileString, searchString, replaceString,
searchType, ©Start, ©End, &replacementLen,
GetWindowDelimiters(window));
if (newFileString ==
NULL) {
if (window->multiFileBusy) {
window->replaceFailed =
TRUE;
}
else if (GetPrefSearchDlogs()) {
if (window->findDlog && XtIsManaged(window->findDlog) &&
!XmToggleButtonGetState(window->findKeepBtn))
XtUnmanageChild(window->findDlog);
if (window->replaceDlog && XtIsManaged(window->replaceDlog) &&
!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
DialogF(
DF_INF, window->shell,
1,
"String not found",
"String was not found",
"OK");
}
else
XBell(TheDisplay,
0);
return FALSE;
}
BufReplace(window->buffer, copyStart, copyEnd, newFileString);
TextSetCursorPos(window->lastFocus, copyStart + replacementLen);
NEditFree(newFileString);
return TRUE;
}
char *ReplaceAllInString(
const char *inString,
const char *searchString,
const char *replaceString,
int searchType,
int *copyStart,
int *copyEnd,
int *replacementLength,
const char *delimiters)
{
int beginPos, startPos, endPos, lastEndPos;
int found, nFound, removeLen, replaceLen, copyLen, addLen;
char *outString, *fillPtr;
int searchExtentBW, searchExtentFW;
if (*searchString ==
'\0')
return NULL;
replaceLen = strlen(replaceString);
found =
TRUE;
nFound =
0;
removeLen =
0;
addLen =
0;
beginPos =
0;
*copyStart = -
1;
while (found) {
found = SearchString(inString, searchString,
SEARCH_FORWARD, searchType,
FALSE, beginPos, &startPos, &endPos, &searchExtentBW,
&searchExtentFW, delimiters);
if (found) {
if (*copyStart <
0)
*copyStart = startPos;
*copyEnd = endPos;
beginPos = (startPos == endPos) ? endPos+
1 : endPos;
nFound++;
removeLen += endPos - startPos;
if (isRegexType(searchType)) {
char replaceResult[
SEARCHMAX];
replaceUsingRE(searchString, replaceString, &inString[searchExtentBW],
startPos-searchExtentBW,
replaceResult,
SEARCHMAX, startPos ==
0 ?
'\0' :
inString[startPos-
1], delimiters,
defaultRegexFlags(searchType));
addLen += strlen(replaceResult);
}
else
addLen += replaceLen;
if (inString[endPos] ==
'\0')
break;
}
}
if (nFound ==
0)
return NULL;
copyLen = *copyEnd - *copyStart;
outString = (
char*)NEditMalloc(copyLen - removeLen + addLen +
1);
found =
TRUE;
beginPos =
0;
lastEndPos =
0;
fillPtr = outString;
while (found) {
found = SearchString(inString, searchString,
SEARCH_FORWARD, searchType,
FALSE, beginPos, &startPos, &endPos, &searchExtentBW,
&searchExtentFW, delimiters);
if (found) {
if (beginPos !=
0) {
memcpy(fillPtr, &inString[lastEndPos], startPos - lastEndPos);
fillPtr += startPos - lastEndPos;
}
if (isRegexType(searchType)) {
char replaceResult[
SEARCHMAX];
replaceUsingRE(searchString, replaceString, &inString[searchExtentBW],
startPos-searchExtentBW,
replaceResult,
SEARCHMAX, startPos ==
0 ?
'\0' :
inString[startPos-
1], delimiters,
defaultRegexFlags(searchType));
replaceLen = strlen(replaceResult);
memcpy(fillPtr, replaceResult, replaceLen);
}
else {
memcpy(fillPtr, replaceString, replaceLen);
}
fillPtr += replaceLen;
lastEndPos = endPos;
beginPos = (startPos == endPos) ? endPos+
1 : endPos;
if (inString[endPos] ==
'\0')
break;
}
}
*fillPtr =
'\0';
*replacementLength = fillPtr - outString;
return outString;
}
static void iSearchTryBeepOnWrap(WindowInfo *window,
int direction,
int beginPos,
int startPos)
{
if (GetPrefBeepOnSearchWrap()) {
if (direction ==
SEARCH_FORWARD) {
if ((startPos >= beginPos
&& window->iSearchLastBeginPos < beginPos)
||(startPos < beginPos
&& window->iSearchLastBeginPos >= beginPos)) {
XBell(TheDisplay,
0);
}
}
else {
if ((startPos <= beginPos
&& window->iSearchLastBeginPos > beginPos)
||(startPos > beginPos
&& window->iSearchLastBeginPos <= beginPos)) {
XBell(TheDisplay,
0);
}
}
}
}
int SearchWindow(WindowInfo *window,
int direction,
const char *searchString,
int searchType,
int searchWrap,
int beginPos,
int *startPos,
int *endPos,
int *extentBW,
int *extentFW)
{
const char *fileString;
int found, resp, fileEnd = window->buffer->length -
1, outsideBounds;
if (*searchString ==
'\0')
return FALSE;
EscSeqArray *esc;
fileString = BufAsStringCleaned(window->buffer, &esc);
if ((direction ==
SEARCH_FORWARD && beginPos > fileEnd +
1)
|| (direction ==
SEARCH_BACKWARD && beginPos <
0))
{
outsideBounds =
TRUE;
}
else
{
outsideBounds =
FALSE;
}
if (window->iSearchStartPos == -
1) {
found = !outsideBounds &&
SearchString(fileString, searchString, direction, searchType,
FALSE, beginPos, startPos, endPos, extentBW, extentFW,
GetWindowDelimiters(window));
if (window->findDlog && XtIsManaged(window->findDlog) &&
!XmToggleButtonGetState(window->findKeepBtn))
XtUnmanageChild(window->findDlog);
if (window->replaceDlog && XtIsManaged(window->replaceDlog) &&
!XmToggleButtonGetState(window->replaceKeepBtn))
unmanageReplaceDialogs(window);
if (!found) {
if (searchWrap) {
if (direction ==
SEARCH_FORWARD && beginPos !=
0) {
if(GetPrefBeepOnSearchWrap()) {
XBell(TheDisplay,
0);
}
else if (GetPrefSearchDlogs()) {
resp = DialogF(
DF_QUES, window->shell,
2,
"Wrap Search",
"Continue search from\nbeginning of file?",
"Continue",
"Cancel");
if (resp ==
2) {
translatePosAndRestoreBuf(window->buffer, esc, found, startPos, endPos, extentBW, extentFW);
return False;
}
}
found = SearchString(fileString, searchString, direction,
searchType,
FALSE,
0, startPos, endPos, extentBW,
extentFW, GetWindowDelimiters(window));
}
else if (direction ==
SEARCH_BACKWARD && beginPos != fileEnd) {
if(GetPrefBeepOnSearchWrap()) {
XBell(TheDisplay,
0);
}
else if (GetPrefSearchDlogs()) {
resp = DialogF(
DF_QUES, window->shell,
2,
"Wrap Search",
"Continue search\nfrom end of file?",
"Continue",
"Cancel");
if (resp ==
2) {
translatePosAndRestoreBuf(window->buffer, esc, found, startPos, endPos, extentBW, extentFW);
return False;
}
}
found = SearchString(fileString, searchString, direction,
searchType,
FALSE, fileEnd +
1, startPos, endPos, extentBW,
extentFW, GetWindowDelimiters(window));
}
}
translatePosAndRestoreBuf(window->buffer, esc, found, startPos, endPos, extentBW, extentFW);
esc =
NULL;
if (!found) {
if (GetPrefSearchDlogs()) {
DialogF(
DF_INF, window->shell,
1,
"String not found",
"String was not found",
"OK");
}
else {
XBell(TheDisplay,
0);
}
}
}
}
else {
if (outsideBounds && searchWrap) {
if (direction ==
SEARCH_FORWARD) beginPos =
0;
else beginPos = fileEnd+
1;
outsideBounds =
FALSE;
}
found = !outsideBounds &&
SearchString(fileString, searchString, direction,
searchType, searchWrap, beginPos, startPos, endPos,
extentBW, extentFW, GetWindowDelimiters(window));
if (found) {
iSearchTryBeepOnWrap(window, direction, beginPos, *startPos);
}
else
XBell(TheDisplay,
0);
}
translatePosAndRestoreBuf(window->buffer, esc, found, startPos, endPos, extentBW, extentFW);
return found;
}
int SearchString(
const char *string,
const char *searchString,
int direction,
int searchType,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW,
const char *delimiters)
{
switch (searchType) {
case SEARCH_CASE_SENSE_WORD:
return searchLiteralWord(string, searchString,
TRUE, direction, wrap,
beginPos, startPos, endPos, delimiters);
case SEARCH_LITERAL_WORD:
return searchLiteralWord(string, searchString,
FALSE, direction, wrap,
beginPos, startPos, endPos, delimiters);
case SEARCH_CASE_SENSE:
return searchLiteral(string, searchString,
TRUE, direction, wrap,
beginPos, startPos, endPos, searchExtentBW,
searchExtentFW);
case SEARCH_LITERAL:
return searchLiteral(string, searchString,
FALSE, direction, wrap,
beginPos, startPos, endPos, searchExtentBW, searchExtentFW);
case SEARCH_REGEX:
return searchRegex(string, searchString, direction, wrap,
beginPos, startPos, endPos, searchExtentBW, searchExtentFW,
delimiters,
REDFLT_STANDARD);
case SEARCH_REGEX_NOCASE:
return searchRegex(string, searchString, direction, wrap,
beginPos, startPos, endPos, searchExtentBW, searchExtentFW,
delimiters,
REDFLT_CASE_INSENSITIVE);
}
return FALSE;
}
int StringToSearchType(
const char * string,
int *searchType)
{
int i;
for (i =
0; searchTypeStrings[i]; i++) {
if (!strcmp(string, searchTypeStrings[i])) {
break;
}
}
if (!searchTypeStrings[i]) {
return FALSE;
}
*searchType = i;
return TRUE;
}
static int searchLiteralWord(
const char *string,
const char *searchString,
int caseSense,
int direction,
int wrap,
int beginPos,
int *startPos,
int *endPos,
const char * delimiters)
{
#define DOSEARCHWORD() \
if (*filePtr == *ucString || *filePtr == *lcString) { \
\
ucPtr = ucString; \
lcPtr = lcString; \
tempPtr = filePtr; \
while (*tempPtr == *ucPtr || *tempPtr == *lcPtr) { \
tempPtr++; ucPtr++; lcPtr++; \
if ( *ucPtr ==
0 \
&& (cignore_R ||\
isspace((
unsigned char)*tempPtr) ||\
strchr(delimiters, *tempPtr) ) \
\
&& (cignore_L ||\
filePtr==string || \
isspace((
unsigned char)filePtr[-
1]) ||\
strchr(delimiters,filePtr[-
1]) ))\
{ \
*startPos = filePtr - string; \
*endPos = tempPtr - string; \
return TRUE; \
} \
} \
}
register const char *filePtr, *tempPtr, *ucPtr, *lcPtr;
char lcString[
SEARCHMAX], ucString[
SEARCHMAX];
int cignore_L=
0, cignore_R=
0;
if (strlen(searchString) >=
SEARCHMAX)
return FALSE;
if (delimiters==
NULL) delimiters = GetPrefDelimiters();
if ( isspace((
unsigned char)*searchString)
|| strchr(delimiters, *searchString))
cignore_L=
1;
if ( isspace((
unsigned char)searchString[strlen(searchString)-
1])
|| strchr(delimiters, searchString[strlen(searchString)-
1]) )
cignore_R=
1;
if (caseSense) {
strcpy(ucString, searchString);
strcpy(lcString, searchString);
}
else {
UpCaseString(ucString, searchString);
DownCaseString(lcString, searchString);
}
if (direction ==
SEARCH_FORWARD) {
for (filePtr=string+beginPos; *filePtr!=
0; filePtr++) {
DOSEARCHWORD()
}
if (!wrap)
return FALSE;
for (filePtr=string; filePtr<=string+beginPos; filePtr++) {
DOSEARCHWORD()
}
return FALSE;
}
else {
if (beginPos >=
0) {
for (filePtr=string+beginPos; filePtr>=string; filePtr--) {
DOSEARCHWORD()
}
}
if (!wrap)
return FALSE;
for (filePtr=string+strlen(string); filePtr>=string+beginPos; filePtr--) {
DOSEARCHWORD()
}
return FALSE;
}
}
static int searchLiteral(
const char *string,
const char *searchString,
int caseSense,
int direction,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW)
{
#define DOSEARCH() \
if (*filePtr == *ucString || *filePtr == *lcString) { \
\
ucPtr = ucString; \
lcPtr = lcString; \
tempPtr = filePtr; \
while (*tempPtr == *ucPtr || *tempPtr == *lcPtr) { \
tempPtr++; ucPtr++; lcPtr++; \
if (*ucPtr ==
0) { \
\
*startPos = filePtr - string; \
*endPos = tempPtr - string; \
if (searchExtentBW !=
NULL) \
*searchExtentBW = *startPos; \
if (searchExtentFW !=
NULL) \
*searchExtentFW = *endPos; \
return TRUE; \
} \
} \
} \
register const char *filePtr, *tempPtr, *ucPtr, *lcPtr;
char lcString[
SEARCHMAX], ucString[
SEARCHMAX];
if (strlen(searchString) >=
SEARCHMAX)
return FALSE;
if (caseSense) {
strcpy(ucString, searchString);
strcpy(lcString, searchString);
}
else {
UpCaseString(ucString, searchString);
DownCaseString(lcString, searchString);
}
if (direction ==
SEARCH_FORWARD) {
for (filePtr=string+beginPos; *filePtr!=
0; filePtr++) {
DOSEARCH()
}
if (!wrap)
return FALSE;
for (filePtr=string; filePtr<=string+beginPos; filePtr++) {
DOSEARCH()
}
return FALSE;
}
else {
if (beginPos >=
0) {
for (filePtr=string+beginPos; filePtr>=string; filePtr--) {
DOSEARCH()
}
}
if (!wrap)
return FALSE;
for (filePtr=string+strlen(string);
filePtr>=string+beginPos; filePtr--) {
DOSEARCH()
}
return FALSE;
}
}
static int searchRegex(
const char *string,
const char *searchString,
int direction,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW,
const char *delimiters,
int defaultFlags)
{
if (direction ==
SEARCH_FORWARD)
return forwardRegexSearch(string, searchString, wrap,
beginPos, startPos, endPos, searchExtentBW, searchExtentFW,
delimiters, defaultFlags);
else
return backwardRegexSearch(string, searchString, wrap,
beginPos, startPos, endPos, searchExtentBW, searchExtentFW,
delimiters, defaultFlags);
}
static int forwardRegexSearch(
const char *string,
const char *searchString,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW,
const char *delimiters,
int defaultFlags)
{
regexp *compiledRE =
NULL;
char *compileMsg;
compiledRE = CompileRE(searchString, &compileMsg, defaultFlags);
if (compiledRE ==
NULL)
return FALSE;
if (ExecRE(compiledRE, string + beginPos,
NULL,
FALSE,
(beginPos ==
0) ?
'\0' : string[beginPos-
1],
'\0', delimiters,
string,
NULL)) {
*startPos = compiledRE->startp[
0] - string;
*endPos = compiledRE->endp[
0] - string;
if (searchExtentFW !=
NULL)
*searchExtentFW = compiledRE->extentpFW - string;
if (searchExtentBW !=
NULL)
*searchExtentBW = compiledRE->extentpBW - string;
NEditFree(compiledRE);
return TRUE;
}
if (!wrap) {
NEditFree(compiledRE);
return FALSE;
}
if (ExecRE(compiledRE, string, string + beginPos,
FALSE,
'\0',
string[beginPos], delimiters, string,
NULL)) {
*startPos = compiledRE->startp[
0] - string;
*endPos = compiledRE->endp[
0] - string;
if (searchExtentFW !=
NULL)
*searchExtentFW = compiledRE->extentpFW - string;
if (searchExtentBW !=
NULL)
*searchExtentBW = compiledRE->extentpBW - string;
NEditFree(compiledRE);
return TRUE;
}
NEditFree(compiledRE);
return FALSE;
}
static int backwardRegexSearch(
const char *string,
const char *searchString,
int wrap,
int beginPos,
int *startPos,
int *endPos,
int *searchExtentBW,
int *searchExtentFW,
const char *delimiters,
int defaultFlags)
{
regexp *compiledRE =
NULL;
char *compileMsg;
int length;
compiledRE = CompileRE(searchString, &compileMsg, defaultFlags);
if (compiledRE ==
NULL)
return FALSE;
if (beginPos >=
0) {
if (ExecRE(compiledRE, string, string + beginPos,
TRUE,
'\0',
'\0',
delimiters, string,
NULL)) {
*startPos = compiledRE->startp[
0] - string;
*endPos = compiledRE->endp[
0] - string;
if (searchExtentFW !=
NULL)
*searchExtentFW = compiledRE->extentpFW - string;
if (searchExtentBW !=
NULL)
*searchExtentBW = compiledRE->extentpBW - string;
NEditFree(compiledRE);
return TRUE;
}
}
if (!wrap) {
NEditFree(compiledRE);
return FALSE;
}
if (beginPos <
0)
beginPos =
0;
length = strlen(string);
if (ExecRE(compiledRE, string + beginPos, string + length,
TRUE,
(beginPos ==
0) ?
'\0' : string[beginPos-
1],
'\0', delimiters,
string,
NULL)) {
*startPos = compiledRE->startp[
0] - string;
*endPos = compiledRE->endp[
0] - string;
if (searchExtentFW !=
NULL)
*searchExtentFW = compiledRE->extentpFW - string;
if (searchExtentBW !=
NULL)
*searchExtentBW = compiledRE->extentpBW - string;
NEditFree(compiledRE);
return TRUE;
}
NEditFree(compiledRE);
return FALSE;
}
static int check_len(
const char *in,
int len) {
for(
int i=
0;i<len;i++) {
if(in[i] ==
0)
return i;
}
return len;
}
void ChangeCase(
const char *in,
char *out,
int makeUpper,
int *in_len,
int *out_len) {
mbstate_t state;
memset(&state,
0,
sizeof(
mbstate_t));
wchar_t w =
0;
int len = Utf8CharLen((
const unsigned char*)in);
len = check_len(in, len);
*in_len = len;
mbrtowc(&w, in, len, &state);
wchar_t wc = makeUpper ? towupper(w) : towlower(w);
if(wc ==
0) wc = w;
char bufChar[
8];
const char *src_buf = bufChar;
int clen = wctomb(bufChar, wc);
if(clen > len) {
clen = len;
src_buf = in;
}
*out_len = clen;
memcpy(out, src_buf, clen);
}
void UpCaseString(
char *outString,
const char *inString)
{
char *outPtr;
const char *inPtr;
for (outPtr=outString, inPtr=inString; *inPtr!=
0; inPtr++, outPtr++) {
if(*inPtr >=
0) {
*outPtr = toupper((
unsigned char)*inPtr);
}
else {
int in_len, out_len;
ChangeCase(inPtr, outPtr, True, &in_len, &out_len);
inPtr += in_len -
1;
outPtr += out_len -
1;
}
}
*outPtr =
0;
}
void DownCaseString(
char *outString,
const char *inString)
{
char *outPtr;
const char *inPtr;
for (outPtr=outString, inPtr=inString; *inPtr!=
0; inPtr++, outPtr++) {
if(*inPtr >=
0) {
*outPtr = tolower((
unsigned char)*inPtr);
}
else {
int in_len, out_len;
ChangeCase(inPtr, outPtr, False, &in_len, &out_len);
inPtr += in_len -
1;
outPtr += out_len -
1;
}
}
*outPtr =
0;
}
static void resetFindTabGroup(WindowInfo *window)
{
XmProcessTraversal(window->findText, XmTRAVERSE_CURRENT);
}
static void resetReplaceTabGroup(WindowInfo *window)
{
XmProcessTraversal(window->replaceText, XmTRAVERSE_CURRENT);
}
static int searchMatchesSelection(WindowInfo *window,
const char *searchString,
int searchType,
int *left,
int *right,
int *searchExtentBW,
int *searchExtentFW)
{
int selLen, selStart, selEnd, startPos, endPos, extentBW, extentFW, beginPos;
int regexLookContext = isRegexType(searchType) ?
1000 :
0;
char *string;
int found, isRect, rectStart, rectEnd, lineStart =
0;
if (!BufGetEmptySelectionPos(window->buffer, &selStart, &selEnd, &isRect,
&rectStart, &rectEnd))
return FALSE;
if (selEnd - selStart >
SEARCHMAX)
return FALSE;
if (isRect) {
lineStart = BufStartOfLine(window->buffer, selStart);
if (lineStart != BufStartOfLine(window->buffer, selEnd))
return FALSE;
}
if (isRect) {
int stringStart = lineStart + rectStart - regexLookContext;
if (stringStart <
0) stringStart =
0;
string = BufGetRange(window->buffer, stringStart,
lineStart + rectEnd + regexLookContext);
selLen = rectEnd - rectStart;
beginPos = lineStart + rectStart - stringStart;
}
else {
int stringStart = selStart - regexLookContext;
if (stringStart <
0) stringStart =
0;
string = BufGetRange(window->buffer, stringStart,
selEnd + regexLookContext);
selLen = selEnd - selStart;
beginPos = selStart - stringStart;
}
if (*string ==
'\0') {
NEditFree(string);
return FALSE;
}
found = SearchString(string, searchString,
SEARCH_FORWARD, searchType,
FALSE, beginPos, &startPos, &endPos, &extentBW, &extentFW,
GetWindowDelimiters(window));
NEditFree(string);
if (!found)
return FALSE;
if (startPos != beginPos || endPos - beginPos != selLen )
return FALSE;
if (isRect)
GetSimpleSelection(window->buffer, left, right);
else {
*left = selStart;
*right = selEnd;
}
if (searchExtentBW !=
NULL)
*searchExtentBW = *left - (startPos - extentBW);
if (searchExtentFW !=
NULL)
*searchExtentFW = *right + extentFW - endPos;
return TRUE;
}
static Boolean replaceUsingRE(
const char* searchStr,
const char* replaceStr,
const char* sourceStr,
int beginPos,
char* destStr,
int maxDestLen,
int prevChar,
const char* delimiters,
int defaultFlags)
{
regexp *compiledRE;
char *compileMsg;
Boolean substResult = False;
compiledRE = CompileRE(searchStr, &compileMsg, defaultFlags);
ExecRE(compiledRE, sourceStr+beginPos,
NULL, False, prevChar,
'\0',
delimiters, sourceStr,
NULL);
substResult = SubstituteRE(compiledRE, replaceStr, destStr, maxDestLen);
NEditFree(compiledRE);
return substResult;
}
static void enableFindAgainCmds(
void)
{
WindowInfo *w;
for (w=WindowList; w!=
NULL; w=w->next) {
if (!IsTopDocument(w))
continue;
XtSetSensitive(w->findAgainItem, True);
XtSetSensitive(w->replaceFindAgainItem, True);
XtSetSensitive(w->replaceAgainItem, True);
}
}
void WriteSearchHistory(
void)
{
const char *fullName = GetRCFileName(
SEARCH_HISTORY);
const char *searchStr, *replaceStr;
struct stat attribute;
FILE *fp;
int i;
if (!GetPrefSaveSearchHistory())
return;
if ((fp = fopen(fullName,
"w")) ==
NULL) {
#ifdef VMS
if ((fp = fopen(fullName,
"r+")) ==
NULL)
return;
if (ftruncate(fileno(fp),
0) !=
0) {
fclose(fp);
return;
}
#else
return;
#endif
}
for (i = NHist; i >=
1; i--) {
searchStr = SearchHistory[historyIndex(i)];
replaceStr = ReplaceHistory[historyIndex(i)];
fprintf(fp,
"%d:%u:%u\n%s\n%s\n",
SearchTypeHistory[historyIndex(i)],
(
unsigned)strlen(searchStr), (
unsigned)strlen(replaceStr),
searchStr, replaceStr);
}
fclose(fp);
if (
0 == stat(fullName, &attribute)) {
lastSearchdbModTime = attribute.st_mtime;
}
}
void ReadSearchHistory(
void)
{
const char *fullName = GetRCFileName(
SEARCH_HISTORY);
struct stat attribute;
char line[
16];
size_t lineLen;
int type;
unsigned srchLen, replLen;
FILE *fp;
if (!GetPrefSaveSearchHistory())
return;
if (
0 == stat(fullName, &attribute)) {
if (lastSearchdbModTime >= attribute.st_mtime) {
return;
}
else {
lastSearchdbModTime = attribute.st_mtime;
}
}
else {
if (
ENOENT != errno)
fprintf(stderr,
"NEdit: Error reading file %s (%s)\n",
fullName, strerror(errno));
return;
}
if ((fp = fopen(fullName,
"r")) ==
NULL)
return;
while (
0 != NHist) {
XtFree(SearchHistory[historyIndex(NHist)]);
XtFree(ReplaceHistory[historyIndex(NHist)]);
NHist--;
}
HistStart =
0;
while (fgets(line,
sizeof(line), fp) !=
NULL) {
lineLen = strlen(line);
if (lineLen ==
0 || line[
0] ==
'\n' || line[
0] ==
'#') {
continue;
}
if (line[lineLen -
1] !=
'\n') {
fprintf(stderr,
"NEdit: Line too long in file %s\n", fullName);
break;
}
line[--lineLen] =
'\0';
if (sscanf(line,
"%d:%u:%u", &type, &srchLen, &replLen) !=
3) {
fprintf(stderr,
"NEdit: Invalid line in file %s\n", fullName);
break;
}
SearchTypeHistory[HistStart]=type;
if (type <
0 || type >=
N_SEARCH_TYPES
|| srchLen >
SEARCHMAX || replLen >
SEARCHMAX) {
fprintf(stderr,
"NEdit: Invalid values in file %s\n", fullName);
break;
}
if (NHist ==
MAX_SEARCH_HISTORY) {
XtFree(SearchHistory[HistStart]);
XtFree(ReplaceHistory[HistStart]);
}
else
NHist++;
SearchHistory[HistStart] = XtMalloc(srchLen+
1);
if (fread(SearchHistory[HistStart],
1, srchLen +
1, fp) != srchLen +
1
|| SearchHistory[HistStart][srchLen] !=
'\n') {
fprintf(stderr,
"NEdit: Error reading file %s (%s)\n",
fullName, strerror(errno));
XtFree(SearchHistory[HistStart]);
NHist--;
break;
}
SearchHistory[HistStart][srchLen]=
'\0';
ReplaceHistory[HistStart] = XtMalloc(replLen+
1);
if (fread(ReplaceHistory[HistStart],
1, replLen +
1, fp) != replLen +
1
|| ReplaceHistory[HistStart][replLen] !=
'\n') {
fprintf(stderr,
"NEdit: Error reading file %s (%s)\n",
fullName, strerror(errno));
XtFree(SearchHistory[HistStart]);
XtFree(ReplaceHistory[HistStart]);
NHist--;
break;
}
ReplaceHistory[HistStart][replLen]=
'\0';
if (++HistStart >=
MAX_SEARCH_HISTORY)
HistStart =
0;
}
fclose(fp);
if (NHist)
enableFindAgainCmds();
}
static void saveSearchHistory(
const char *searchString,
const char *replaceString,
int searchType,
int isIncremental)
{
char *sStr, *rStr;
static int currentItemIsIncremental =
FALSE;
if (!isIncremental)
currentItemIsIncremental =
FALSE;
if (searchString[
0] ==
'\0')
return;
if (replaceString ==
NULL)
replaceString = NHist >=
1 ? ReplaceHistory[historyIndex(
1)] :
"";
if (NHist >=
1 && searchType == SearchTypeHistory[historyIndex(
1)] &&
!strcmp(SearchHistory[historyIndex(
1)], searchString) &&
!strcmp(ReplaceHistory[historyIndex(
1)], replaceString)) {
return;
}
if (currentItemIsIncremental && isIncremental) {
NEditFree(SearchHistory[historyIndex(
1)]);
SearchHistory[historyIndex(
1)] = NEditStrdup(searchString);
SearchTypeHistory[historyIndex(
1)] = searchType;
WriteSearchHistory();
return;
}
currentItemIsIncremental = isIncremental;
if (NHist ==
0)
enableFindAgainCmds();
ReadSearchHistory();
if (NHist ==
MAX_SEARCH_HISTORY) {
NEditFree(SearchHistory[HistStart]);
NEditFree(ReplaceHistory[HistStart]);
}
else
NHist++;
sStr = NEditStrdup(searchString);
rStr = NEditStrdup(replaceString);
SearchHistory[HistStart] = sStr;
ReplaceHistory[HistStart] = rStr;
SearchTypeHistory[HistStart] = searchType;
HistStart++;
if (HistStart >=
MAX_SEARCH_HISTORY)
HistStart =
0;
WriteSearchHistory();
}
static int historyIndex(
int nCycles)
{
int index;
if (nCycles > NHist || nCycles <=
0)
return -
1;
index = HistStart - nCycles;
if (index <
0)
index =
MAX_SEARCH_HISTORY + index;
return index;
}
static char *searchTypeArg(
int searchType)
{
if (
0 <= searchType && searchType <
N_SEARCH_TYPES) {
return searchTypeStrings[searchType];
}
return searchTypeStrings[
SEARCH_LITERAL];
}
static char *searchWrapArg(
int searchWrap)
{
if (searchWrap) {
return "wrap";
}
return "nowrap";
}
static char *directionArg(
int direction)
{
if (direction ==
SEARCH_BACKWARD)
return "backward";
return "forward";
}
static int isRegexType(
int searchType)
{
return searchType ==
SEARCH_REGEX || searchType ==
SEARCH_REGEX_NOCASE;
}
static int defaultRegexFlags(
int searchType)
{
switch (searchType) {
case SEARCH_REGEX:
return REDFLT_STANDARD;
case SEARCH_REGEX_NOCASE:
return REDFLT_CASE_INSENSITIVE;
default:
return REDFLT_STANDARD;
}
}
static void findRegExpToggleCB(Widget w, XtPointer clientData, XtPointer callData)
{
WindowInfo * window = WidgetToWindow(w);
int searchRegex = XmToggleButtonGetState(w);
int searchCaseSense = XmToggleButtonGetState(window->findCaseToggle);
if(GetPrefStickyCaseSenseBtn()) {
if(searchRegex) {
window->findLastLiteralCase = searchCaseSense;
XmToggleButtonSetState(window->findCaseToggle,
window->findLastRegexCase, False);
}
else {
window->findLastRegexCase = searchCaseSense;
XmToggleButtonSetState(window->findCaseToggle,
window->findLastLiteralCase, False);
}
}
XtSetSensitive(window->findWordToggle, !searchRegex);
}
static void replaceRegExpToggleCB(Widget w, XtPointer clientData, XtPointer callData)
{
WindowInfo * window = WidgetToWindow(w);
int searchRegex = XmToggleButtonGetState(w);
int searchCaseSense = XmToggleButtonGetState(window->replaceCaseToggle);
if(GetPrefStickyCaseSenseBtn()) {
if(searchRegex) {
window->replaceLastLiteralCase = searchCaseSense;
XmToggleButtonSetState(window->replaceCaseToggle,
window->replaceLastRegexCase, False);
}
else {
window->replaceLastRegexCase = searchCaseSense;
XmToggleButtonSetState(window->replaceCaseToggle,
window->replaceLastLiteralCase, False);
}
}
XtSetSensitive(window->replaceWordToggle, !searchRegex);
}
static void iSearchRegExpToggleCB(Widget w, XtPointer clientData, XtPointer callData)
{
WindowInfo * window = WidgetToWindow(w);
int searchRegex = XmToggleButtonGetState(w);
int searchCaseSense = XmToggleButtonGetState(window->iSearchCaseToggle);
if(GetPrefStickyCaseSenseBtn()) {
if(searchRegex) {
window->iSearchLastLiteralCase = searchCaseSense;
XmToggleButtonSetState(window->iSearchCaseToggle,
window->iSearchLastRegexCase, False);
}
else {
window->iSearchLastRegexCase = searchCaseSense;
XmToggleButtonSetState(window->iSearchCaseToggle,
window->iSearchLastLiteralCase, False);
}
}
}
static void findCaseToggleCB(Widget w, XtPointer clientData, XtPointer callData)
{
WindowInfo * window = WidgetToWindow(w);
int searchCaseSense = XmToggleButtonGetState(w);
if(XmToggleButtonGetState(window->findRegexToggle))
window->findLastRegexCase = searchCaseSense;
else
window->findLastLiteralCase = searchCaseSense;
}
static void replaceCaseToggleCB(Widget w, XtPointer clientData, XtPointer callData)
{
WindowInfo * window = WidgetToWindow(w);
int searchCaseSense = XmToggleButtonGetState(w);
if(XmToggleButtonGetState(window->replaceRegexToggle))
window->replaceLastRegexCase = searchCaseSense;
else
window->replaceLastLiteralCase = searchCaseSense;
}
static void iSearchCaseToggleCB(Widget w, XtPointer clientData, XtPointer callData)
{
WindowInfo * window = WidgetToWindow(w);
int searchCaseSense = XmToggleButtonGetState(w);
if(XmToggleButtonGetState(window->iSearchRegexToggle))
window->iSearchLastRegexCase = searchCaseSense;
else
window->iSearchLastLiteralCase = searchCaseSense;
}
static int translateEscPos(EscSeqArray *array,
int pos)
{
int p = pos;
for(
size_t i=
0;i<array->num_esc;i++) {
EscSeqStr e = array->esc[i];
if(e.off_trans < pos)
p += e.len;
else
break;
}
return p;
}
static void translatePosAndRestoreBuf(
textBuffer *buf,
EscSeqArray *array,
int found,
int *begin,
int *end,
int *extentBW,
int *extentFW)
{
if(found && array) {
if(begin) *begin = translateEscPos(array, *begin);
if(end) *end = translateEscPos(array, *end);
if(extentBW) *extentBW = translateEscPos(array, *extentBW);
if(extentFW) *extentFW = translateEscPos(array, *extentFW);
}
BufReintegrateEscSeq(buf, array);
}