Esempio n. 1
0
void SelectNumberedLine(WindowInfo *window, int lineNum)
{
    int i, lineStart = 0, lineEnd;

    /* count lines to find the start and end positions for the selection */
    if (lineNum < 1)
    	lineNum = 1;
    lineEnd = -1;
    for (i=1; i<=lineNum && lineEnd<window->buffer->length; i++) {
    	lineStart = lineEnd + 1;
    	lineEnd = BufEndOfLine(window->buffer, lineStart);
    }
    
    /* highlight the line */
    if (i>lineNum) {
	/* Line was found */
	if (lineEnd < window->buffer->length) {
	    BufSelect(window->buffer, lineStart, lineEnd+1);
	} else { 
	    /* Don't select past the end of the buffer ! */
	    BufSelect(window->buffer, lineStart, window->buffer->length);
	}
    } else {
	/* Line was not found -> position the selection & cursor at the end 
	   without making a real selection and beep */
	lineStart = window->buffer->length;
	BufSelect(window->buffer, lineStart, lineStart);
	XBell(TheDisplay, 0);
    }     
    MakeSelectionVisible(window, window->lastFocus);
    TextSetCursorPos(window->lastFocus, lineStart);
}
Esempio n. 2
0
void GotoMark(WindowInfo *window, Widget w, char label, int extendSel)
{
    int index, oldStart, newStart, oldEnd, newEnd, cursorPos;
    selection *sel, *oldSel;
    
    /* look up the mark in the mark table */
    label = toupper(label);
    for (index=0; index<window->nMarks; index++) {
    	if (window->markTable[index].label == label)
   	    break;
    }
    if (index == window->nMarks) {
    	XBell(TheDisplay, 0);
    	return;
    }
    
    /* reselect marked the selection, and move the cursor to the marked pos */
    sel = &window->markTable[index].sel;
    oldSel = &window->buffer->primary;
    cursorPos = window->markTable[index].cursorPos;
    if (extendSel) {
	oldStart = oldSel->selected ? oldSel->start : TextGetCursorPos(w);
	oldEnd = oldSel->selected ? oldSel->end : TextGetCursorPos(w);
	newStart = sel->selected ? sel->start : cursorPos;
	newEnd = sel->selected ? sel->end : cursorPos;
	BufSelect(window->buffer, oldStart < newStart ? oldStart : newStart,
		oldEnd > newEnd ? oldEnd : newEnd);
    } else {
	if (sel->selected) {
    	    if (sel->rectangular)
    		BufRectSelect(window->buffer, sel->start, sel->end,
			sel->rectStart, sel->rectEnd);
    	    else
    		BufSelect(window->buffer, sel->start, sel->end);
	} else
    	    BufUnselect(window->buffer);
    }
    
    /* Move the window into a pleasing position relative to the selection
       or cursor.   MakeSelectionVisible is not great with multi-line
       selections, and here we will sometimes give it one.  And to set the
       cursor position without first using the less pleasing capability
       of the widget itself for bringing the cursor in to view, you have to
       first turn it off, set the position, then turn it back on. */
    XtVaSetValues(w, textNautoShowInsertPos, False, NULL);
    TextSetCursorPos(w, cursorPos);
    MakeSelectionVisible(window, window->lastFocus);
    XtVaSetValues(w, textNautoShowInsertPos, True, NULL);
}
Esempio n. 3
0
/*
** Search the help text.  If allSections is true, searches all of the help
** text, otherwise searches only in parentTopic.
*/
static void searchHelpText(Widget parent, int parentTopic,
        const char *searchFor, int allSections, int startPos, int startTopic)
{    
    int topic, beginMatch, endMatch;
    int found = False;
    char * helpText  = NULL;
    
    /* Search for the string */
    for (topic=startTopic; topic<NUM_TOPICS; topic++)
    {
        if (!allSections && topic != parentTopic)
            continue;
        helpText = stitch(parent, HelpText[topic], NULL);

        if (SearchString(helpText, searchFor, SEARCH_FORWARD, SEARCH_LITERAL,
                False, topic == startTopic ? startPos : 0, &beginMatch,
                &endMatch, NULL, NULL, GetPrefDelimiters()))
        {
            found = True;
            XtFree(helpText);
            break;
        }
        XtFree(helpText);
    }

    if (!found)
    {
        if (startPos != 0 || (allSections && startTopic != 0))
        {
            /* Wrap search */
            searchHelpText(parent, parentTopic, searchFor, allSections, 0, 0);
            return;
        }
        DialogF(DF_INF, parent, 1, "String Not Found", "String Not Found", "OK");
        return;
    }
    
    /* update navigation history */  
    if (parentTopic != topic)
    {
        navHistForw[parentTopic]= topic;
        navHistBack[topic]= parentTopic;
    }
    
    /* If the appropriate window is already up, bring it to the top, if not,
       make the parent window become this topic */
    changeTopicOrRaise(parentTopic, topic);
    BufSelect(TextGetBuffer(HelpTextPanes[topic]), beginMatch, endMatch);
    TextSetCursorPos(HelpTextPanes[topic], endMatch);
    
    /* Save the search information for search-again */
    strcpy(LastSearchString, searchFor);
    LastSearchTopic = topic;
    LastSearchPos = endMatch;
    LastSearchWasAllTopics = allSections;
}
Esempio n. 4
0
/*
** Cancel a block drag operation
*/
void CancelBlockDrag(TextWidget tw)
{
    textBuffer *buf = tw->text.textD->buffer;
    textBuffer *origBuf = tw->text.dragOrigBuf;
    selection *origSel = &origBuf->primary;
    int modRangeStart = -1, origModRangeEnd, bufModRangeEnd;
    char *repText;
    dragEndCBStruct endStruct;

    /* If the operation was a move, make the modify range reflect the
       removal of the text from the starting position */
    if (tw->text.dragSourceDeleted != 0)
    	trackModifyRange(&modRangeStart, &bufModRangeEnd, &origModRangeEnd,
    	    	tw->text.dragSourceDeletePos, tw->text.dragSourceInserted,
    	    	tw->text.dragSourceDeleted);
    
    /* Include the insert being undone from the last step in the modified
       range. */
    trackModifyRange(&modRangeStart, &bufModRangeEnd, &origModRangeEnd,
    	   tw->text.dragInsertPos, tw->text.dragInserted, tw->text.dragDeleted);
 
    /* Make the changes in the buffer */
    repText = BufGetRange(origBuf, modRangeStart, origModRangeEnd);
    BufReplace(buf, modRangeStart, bufModRangeEnd, repText);
    XtFree(repText);
    
    /* Reset the selection and cursor position */
    if (origSel->rectangular)
    	BufRectSelect(buf, origSel->start, origSel->end, origSel->rectStart,
    	    	origSel->rectEnd);
    else
    	BufSelect(buf, origSel->start, origSel->end);
    TextDSetInsertPosition(tw->text.textD, buf->cursorPosHint);
    XtCallCallbacks((Widget)tw, textNcursorMovementCallback, NULL);
    tw->text.emTabsBeforeCursor = 0;
    
    /* Free the backup buffer */
    BufFree(origBuf);
    
    /* Indicate end of drag */
    tw->text.dragState = DRAG_CANCELED;
    
    /* Call finish-drag calback */
    endStruct.startPos = 0;
    endStruct.nCharsDeleted = 0;
    endStruct.nCharsInserted = 0;
    endStruct.deletedText = NULL;
    XtCallCallbacks((Widget)tw, textNdragEndCallback, (XtPointer)&endStruct);
}
Esempio n. 5
0
/*
** Clean up after the execution of a shell command sub-process and present
** the output/errors to the user as requested in the initial issueCommand
** call.  If "terminatedOnError" is true, don't bother trying to read the
** output, just close the i/o descriptors, free the memory, and restore the
** user interface state.
*/
static void finishCmdExecution(WindowInfo *window, int terminatedOnError)
{
    shellCmdInfo *cmdData = window->shellCmdData;
    textBuffer *buf;
    int status, failure, errorReport, reselectStart, outTextLen, errTextLen;
    int resp, cancel = False, fromMacro = cmdData->fromMacro;
    char *outText, *errText = NULL;

    /* Cancel any pending i/o on the file descriptors */
    if (cmdData->stdoutInputID != 0)
    	XtRemoveInput(cmdData->stdoutInputID);
    if (cmdData->stdinInputID != 0)
    	XtRemoveInput(cmdData->stdinInputID);
    if (cmdData->stderrInputID != 0)
    	XtRemoveInput(cmdData->stderrInputID);

    /* Close any file descriptors remaining open */
    close(cmdData->stdoutFD);
    if (cmdData->flags & ERROR_DIALOGS)
    	close(cmdData->stderrFD);
    if (cmdData->inPtr != NULL)
    	close(cmdData->stdinFD);

    /* Free the provided input text */
    if (cmdData->input != NULL)
	XtFree(cmdData->input);
    
    /* Cancel pending timeouts */
    if (cmdData->flushTimeoutID != 0)
    	XtRemoveTimeOut(cmdData->flushTimeoutID);
    if (cmdData->bannerTimeoutID != 0)
    	XtRemoveTimeOut(cmdData->bannerTimeoutID);
    
    /* Clean up waiting-for-shell-command-to-complete mode */
    if (!cmdData->fromMacro) {
	EndWait(window->shell);
	SetSensitive(window, window->cancelShellItem, False);
	if (cmdData->bannerIsUp)
    	    ClearModeMessage(window);
    }
    
    /* If the process was killed or became inaccessable, give up */
    if (terminatedOnError) {
	freeBufList(&cmdData->outBufs);
	freeBufList(&cmdData->errBufs);
    	waitpid(cmdData->childPid, &status, 0);
	goto cmdDone;
    }

    /* Assemble the output from the process' stderr and stdout streams into
       null terminated strings, and free the buffer lists used to collect it */
    outText = coalesceOutput(&cmdData->outBufs, &outTextLen);
    if (cmdData->flags & ERROR_DIALOGS)
    	errText = coalesceOutput(&cmdData->errBufs, &errTextLen);

    /* Wait for the child process to complete and get its return status */
    waitpid(cmdData->childPid, &status, 0);
    
    /* Present error and stderr-information dialogs.  If a command returned
       error output, or if the process' exit status indicated failure,
       present the information to the user. */
    if (cmdData->flags & ERROR_DIALOGS)
    {
        failure = WIFEXITED(status) && WEXITSTATUS(status) != 0;
        errorReport = *errText != '\0';

        if (failure && errorReport)
        {
            removeTrailingNewlines(errText);
            truncateString(errText, DF_MAX_MSG_LENGTH);
            resp = DialogF(DF_WARN, window->shell, 2, "Warning", "%s", "Cancel",
                    "Proceed", errText);
            cancel = resp == 1;
        } else if (failure)
        {
            truncateString(outText, DF_MAX_MSG_LENGTH-70);
            resp = DialogF(DF_WARN, window->shell, 2, "Command Failure",
                    "Command reported failed exit status.\n"
                    "Output from command:\n%s", "Cancel", "Proceed", outText);
            cancel = resp == 1;
        } else if (errorReport)
        {
            removeTrailingNewlines(errText);
            truncateString(errText, DF_MAX_MSG_LENGTH);
            resp = DialogF(DF_INF, window->shell, 2, "Information", "%s",
                    "Proceed", "Cancel", errText);
            cancel = resp == 2;
        }

        XtFree(errText);
        if (cancel)
        {
            XtFree(outText);
            goto cmdDone;
        }
    }
    
    /* If output is to a dialog, present the dialog.  Otherwise insert the
       (remaining) output in the text widget as requested, and move the
       insert point to the end */
    if (cmdData->flags & OUTPUT_TO_DIALOG) {
    	removeTrailingNewlines(outText);
	if (*outText != '\0')
    	    createOutputDialog(window->shell, outText);
    } else if (cmdData->flags & OUTPUT_TO_STRING) {
    	ReturnShellCommandOutput(window,outText, WEXITSTATUS(status));
    } else {
	buf = TextGetBuffer(cmdData->textW);
	if (!BufSubstituteNullChars(outText, outTextLen, buf)) {
	    fprintf(stderr,"NEdit: Too much binary data in shell cmd output\n");
	    outText[0] = '\0';
	}
	if (cmdData->flags & REPLACE_SELECTION) {
	    reselectStart = buf->primary.rectangular ? -1 : buf->primary.start;
	    BufReplaceSelected(buf, outText);
	    TextSetCursorPos(cmdData->textW, buf->cursorPosHint);
	    if (reselectStart != -1)
	    	BufSelect(buf, reselectStart, reselectStart + strlen(outText));
	} else {
	    safeBufReplace(buf, &cmdData->leftPos, &cmdData->rightPos, outText);
	    TextSetCursorPos(cmdData->textW, cmdData->leftPos+strlen(outText));
	}
    }

    /* If the command requires the file to be reloaded afterward, reload it */
    if (cmdData->flags & RELOAD_FILE_AFTER)
    	RevertToSaved(window);

    /* Command is complete, free data structure and continue macro execution */
    XtFree(outText);
cmdDone:
    XtFree((char *)cmdData);
    window->shellCmdData = NULL;
    if (fromMacro)
    	ResumeMacroExecution(window);
}
Esempio n. 6
0
/*
** Start the process of dragging the current primary-selected text across
** the window (move by dragging, as opposed to dragging to create the
** selection)
*/
void BeginBlockDrag(TextWidget tw)
{
    textDisp *textD = tw->text.textD;
    textBuffer *buf = textD->buffer;
    int fontHeight = textD->fontStruct->ascent + textD->fontStruct->descent;
    int fontWidth = textD->fontStruct->max_bounds.width;
    selection *sel = &buf->primary;
    int nLines, mousePos, lineStart;
    int x, y, lineEnd;
    
    char *text;
    
    /* Save a copy of the whole text buffer as a backup, and for
       deriving changes */
    tw->text.dragOrigBuf = BufCreate();
    BufSetTabDistance(tw->text.dragOrigBuf, buf->tabDist);
    tw->text.dragOrigBuf->useTabs = buf->useTabs;
    text = BufGetAll(buf);
    BufSetAll(tw->text.dragOrigBuf, text);
    XtFree(text);
    if (sel->rectangular)
    	BufRectSelect(tw->text.dragOrigBuf, sel->start, sel->end, sel->rectStart,
    	    	sel->rectEnd);
    else
    	BufSelect(tw->text.dragOrigBuf, sel->start, sel->end);
 
    /* Record the mouse pointer offsets from the top left corner of the
       selection (the position where text will actually be inserted In dragging
       non-rectangular selections)  */
    if (sel->rectangular) {
    	tw->text.dragXOffset = tw->text.btnDownX + textD->horizOffset -
    	    	textD->left - sel->rectStart * fontWidth;
    } else {
        if (!TextDPositionToXY(textD, sel->start, &x, &y))
            x = BufCountDispChars(buf, TextDStartOfLine(textD, sel->start),
            	    sel->start) * fontWidth + textD->left -
            	    textD->horizOffset;
        tw->text.dragXOffset = tw->text.btnDownX - x;
    }
    mousePos = TextDXYToPosition(textD, tw->text.btnDownX, tw->text.btnDownY);
    nLines = BufCountLines(buf, sel->start, mousePos);
    tw->text.dragYOffset = nLines * fontHeight + (((tw->text.btnDownY -
    	    tw->text.marginHeight) % fontHeight) - fontHeight/2);
    tw->text.dragNLines = BufCountLines(buf, sel->start, sel->end);
    
    /* Record the current drag insert position and the information for
       undoing the fictional insert of the selection in its new position */
    tw->text.dragInsertPos = sel->start;
    tw->text.dragInserted = sel->end - sel->start;
    if (sel->rectangular) {
    	textBuffer *testBuf = BufCreate();
    	char *testText = BufGetRange(buf, sel->start, sel->end);
        BufSetTabDistance(testBuf, buf->tabDist);
        testBuf->useTabs = buf->useTabs;
    	BufSetAll(testBuf, testText);
    	XtFree(testText);
    	BufRemoveRect(testBuf, 0, sel->end - sel->start, sel->rectStart,
    	    	sel->rectEnd);
    	tw->text.dragDeleted = testBuf->length;
    	BufFree(testBuf);
    	tw->text.dragRectStart = sel->rectStart;
    } else {
    	tw->text.dragDeleted = 0;
    	tw->text.dragRectStart = 0;
    }
    tw->text.dragType = DRAG_MOVE;
    tw->text.dragSourceDeletePos = sel->start;
    tw->text.dragSourceInserted = tw->text.dragDeleted;
    tw->text.dragSourceDeleted = tw->text.dragInserted;
    
    /* For non-rectangular selections, fill in the rectangular information in
       the selection for overlay mode drags which are done rectangularly */
    if (!sel->rectangular) {
    	lineStart = BufStartOfLine(buf, sel->start);
    	if (tw->text.dragNLines == 0) {
    	    tw->text.dragOrigBuf->primary.rectStart =
    	    	    BufCountDispChars(buf, lineStart, sel->start);
    	    tw->text.dragOrigBuf->primary.rectEnd =
    	    	    BufCountDispChars(buf, lineStart, sel->end);
    	} else {
    	    lineEnd = BufGetCharacter(buf, sel->end - 1) == '\n' ? 
    	    	    sel->end - 1 : sel->end;
    	    findTextMargins(buf, lineStart, lineEnd,
    	    	    &tw->text.dragOrigBuf->primary.rectStart,
    	    	    &tw->text.dragOrigBuf->primary.rectEnd);
    	}
    }
    
    /* Set the drag state to announce an ongoing block-drag */
    tw->text.dragState = PRIMARY_BLOCK_DRAG;
    
    /* Call the callback announcing the start of a block drag */
    XtCallCallbacks((Widget)tw, textNdragStartCallback, (XtPointer)NULL);
}
Esempio n. 7
0
/*
** Reposition the primary-selected text that is being dragged as a block
** for a new mouse position of (x, y)
*/
void BlockDragSelection(TextWidget tw, int x, int y, int dragType)
{
    textDisp *textD = tw->text.textD;
    textBuffer *buf = textD->buffer;
    int fontHeight = textD->fontStruct->ascent + textD->fontStruct->descent;
    int fontWidth = textD->fontStruct->max_bounds.width;
    textBuffer *origBuf = tw->text.dragOrigBuf;
    int dragXOffset = tw->text.dragXOffset;
    textBuffer *tempBuf;
    selection *origSel = &origBuf->primary;
    int rectangular = origSel->rectangular;
    int overlay, oldDragType = tw->text.dragType;
    int nLines = tw->text.dragNLines;
    int insLineNum, insLineStart, insRectStart, insRectEnd, insStart;
    char *repText, *text, *insText;
    int modRangeStart = -1, tempModRangeEnd = -1, bufModRangeEnd = -1;
    int referenceLine, referencePos, tempStart, tempEnd, origSelLen;
    int insertInserted, insertDeleted, row, column;
    int origSelLineStart, origSelLineEnd;
    int sourceInserted, sourceDeleted, sourceDeletePos;
    
    if (tw->text.dragState != PRIMARY_BLOCK_DRAG)
    	return;

    /* The operation of block dragging is simple in theory, but not so simple
       in practice.  There is a backup buffer (tw->text.dragOrigBuf) which
       holds a copy of the buffer as it existed before the drag.  When the
       user drags the mouse to a new location, this routine is called, and
       a temporary buffer is created and loaded with the local part of the
       buffer (from the backup) which might be changed by the drag.  The
       changes are all made to this temporary buffer, and the parts of this
       buffer which then differ from the real (displayed) buffer are used to
       replace those parts, thus one replace operation serves as both undo
       and modify.  This double-buffering of the operation prevents excessive
       redrawing (though there is still plenty of needless redrawing due to
       re-selection and rectangular operations).
       
       The hard part is keeping track of the changes such that a single replace
       operation will do everyting.  This is done using a routine called
       trackModifyRange which tracks expanding ranges of changes in the two
       buffers in modRangeStart, tempModRangeEnd, and bufModRangeEnd. */

    /* Create a temporary buffer for accumulating changes which will
       eventually be replaced in the real buffer.  Load the buffer with the
       range of characters which might be modified in this drag step
       (this could be tighter, but hopefully it's not too slow) */
    tempBuf = BufCreate();
    tempBuf->tabDist = buf->tabDist;
    tempBuf->useTabs = buf->useTabs;
    tempStart = min3(tw->text.dragInsertPos, origSel->start,
    	 BufCountBackwardNLines(buf, textD->firstChar, nLines+2));
    tempEnd = BufCountForwardNLines(buf, max3(tw->text.dragInsertPos,
    	 origSel->start, textD->lastChar), nLines+2) +
    	 origSel->end - origSel->start;
    text = BufGetRange(origBuf, tempStart, tempEnd);
    BufSetAll(tempBuf, text);
    XtFree(text);

    /* If the drag type is USE_LAST, use the last dragType applied */
    if (dragType == USE_LAST)
    	dragType = tw->text.dragType;
    overlay = dragType == DRAG_OVERLAY_MOVE || dragType == DRAG_OVERLAY_COPY;

    /* Overlay mode uses rectangular selections whether or not the original
       was rectangular.  To use a plain selection as if it were rectangular,
       the start and end positions need to be moved to the line boundaries
       and trailing newlines must be excluded */
    origSelLineStart = BufStartOfLine(origBuf, origSel->start);
    if (!rectangular && BufGetCharacter(origBuf, origSel->end - 1) == '\n')
    	origSelLineEnd = origSel->end - 1;
    else
    	origSelLineEnd = BufEndOfLine(origBuf, origSel->end);
    if (!rectangular && overlay && nLines != 0)
    	dragXOffset -= fontWidth * (origSel->rectStart -
    	    	(origSel->start - origSelLineStart));
    
    /* If the drag operation is of a different type than the last one, and the
       operation is a move, expand the modified-range to include undoing the
       text-removal at the site from which the text was dragged. */
    if (dragType != oldDragType && tw->text.dragSourceDeleted != 0)
    	trackModifyRange(&modRangeStart, &bufModRangeEnd, &tempModRangeEnd,
    	    	tw->text.dragSourceDeletePos, tw->text.dragSourceInserted,
    	    	tw->text.dragSourceDeleted);
    
    /* Do, or re-do the original text removal at the site where a move began.
       If this part has not changed from the last call, do it silently to
       bring the temporary buffer in sync with the real (displayed) 
       buffer.  If it's being re-done, track the changes to complete the
       redo operation begun above */
    if (dragType == DRAG_MOVE || dragType == DRAG_OVERLAY_MOVE) {
	if (rectangular || overlay) {
    	    int prevLen = tempBuf->length;
    	    origSelLen = origSelLineEnd - origSelLineStart;
    	    if (overlay)
    		BufClearRect(tempBuf, origSelLineStart-tempStart,
    		    	origSelLineEnd-tempStart, origSel->rectStart,
    		    	origSel->rectEnd);
    	    else
    		BufRemoveRect(tempBuf, origSelLineStart-tempStart,
    		    	origSelLineEnd-tempStart, origSel->rectStart,
    		    	origSel->rectEnd);
    	    sourceDeletePos = origSelLineStart;
    	    sourceInserted = origSelLen - prevLen + tempBuf->length;
    	    sourceDeleted = origSelLen;
	} else {
    	    BufRemove(tempBuf, origSel->start - tempStart,
    	    	    origSel->end - tempStart);
    	    sourceDeletePos = origSel->start;
    	    sourceInserted = 0;
    	    sourceDeleted = origSel->end - origSel->start;
	}
	if (dragType != oldDragType)
    	    trackModifyRange(&modRangeStart, &tempModRangeEnd, &bufModRangeEnd,
    	    	    sourceDeletePos, sourceInserted, sourceDeleted);
    } else {
    	sourceDeletePos = 0;
    	sourceInserted = 0;
    	sourceDeleted = 0;
    }
    
    /* Expand the modified-range to include undoing the insert from the last
       call. */
    trackModifyRange(&modRangeStart, &bufModRangeEnd, &tempModRangeEnd,
    	   tw->text.dragInsertPos, tw->text.dragInserted, tw->text.dragDeleted);
    
    /* Find the line number and column of the insert position.  Note that in
       continuous wrap mode, these must be calculated as if the text were
       not wrapped */
    TextDXYToUnconstrainedPosition(textD, max(0, x - dragXOffset),
    	    max(0, y - (tw->text.dragYOffset % fontHeight)), &row, &column);
    column = TextDOffsetWrappedColumn(textD, row, column);
    row = TextDOffsetWrappedRow(textD, row);
    insLineNum = row + textD->topLineNum - tw->text.dragYOffset / fontHeight;
    
    /* find a common point of reference between the two buffers, from which
       the insert position line number can be translated to a position */
    if (textD->firstChar > modRangeStart) {
    	referenceLine = textD->topLineNum -
    	    	BufCountLines(buf, modRangeStart, textD->firstChar);
    	referencePos = modRangeStart;
    } else {
    	referencePos = textD->firstChar;
    	referenceLine = textD->topLineNum;
    }

    /* find the position associated with the start of the new line in the
       temporary buffer */
    insLineStart = findRelativeLineStart(tempBuf, referencePos - tempStart,
    	    referenceLine, insLineNum) + tempStart;
    if (insLineStart - tempStart == tempBuf->length)
    	insLineStart = BufStartOfLine(tempBuf, insLineStart - tempStart) +
    	    	tempStart;
    
    /* Find the actual insert position */
    if (rectangular || overlay) {
    	insStart = insLineStart;
    	insRectStart = column;
    } else { /* note, this will fail with proportional fonts */
    	insStart = BufCountForwardDispChars(tempBuf, insLineStart - tempStart,
    	    	column) + tempStart;
    	insRectStart = 0;
    }
    
    /* If the position is the same as last time, don't bother drawing (it
       would be nice if this decision could be made earlier) */
    if (insStart == tw->text.dragInsertPos &&
    	    insRectStart == tw->text.dragRectStart && dragType == oldDragType) {
    	BufFree(tempBuf);
    	return;
    }

    /* Do the insert in the temporary buffer */
    if (rectangular || overlay) {
    	insText = BufGetTextInRect(origBuf, origSelLineStart, origSelLineEnd,
    	    	origSel->rectStart, origSel->rectEnd);
    	if (overlay)
    	    BufOverlayRect(tempBuf, insStart - tempStart, insRectStart,
    	    	    insRectStart + origSel->rectEnd - origSel->rectStart,
    	    	    insText, &insertInserted, &insertDeleted);
    	else
    	    BufInsertCol(tempBuf, insRectStart, insStart - tempStart, insText,
    	    	    &insertInserted, &insertDeleted);
    	trackModifyRange(&modRangeStart, &tempModRangeEnd, &bufModRangeEnd,
    	    	insStart, insertInserted, insertDeleted);
    	XtFree(insText);
    } else {
    	insText = BufGetSelectionText(origBuf);
    	BufInsert(tempBuf, insStart - tempStart, insText);
    	trackModifyRange(&modRangeStart, &tempModRangeEnd, &bufModRangeEnd,
    	    	    insStart, origSel->end - origSel->start, 0);
    	insertInserted = origSel->end - origSel->start;
    	insertDeleted = 0;
    	XtFree(insText);
    }
 
    /* Make the changes in the real buffer */
    repText = BufGetRange(tempBuf, modRangeStart - tempStart,
    	    tempModRangeEnd - tempStart);
    BufFree(tempBuf);
    TextDBlankCursor(textD);
    BufReplace(buf, modRangeStart, bufModRangeEnd, repText);
    XtFree(repText);
    
    /* Store the necessary information for undoing this step */
    tw->text.dragInsertPos = insStart;
    tw->text.dragRectStart = insRectStart;
    tw->text.dragInserted = insertInserted;
    tw->text.dragDeleted = insertDeleted;
    tw->text.dragSourceDeletePos = sourceDeletePos;
    tw->text.dragSourceInserted = sourceInserted;
    tw->text.dragSourceDeleted = sourceDeleted;
    tw->text.dragType = dragType;
 
    /* Reset the selection and cursor position */
    if (rectangular || overlay) {
    	insRectEnd = insRectStart + origSel->rectEnd - origSel->rectStart;
    	BufRectSelect(buf, insStart, insStart + insertInserted, insRectStart,
    	    	insRectEnd);
    	TextDSetInsertPosition(textD, BufCountForwardDispChars(buf,
    	    	BufCountForwardNLines(buf, insStart, tw->text.dragNLines),
    	    	insRectEnd));
    } else {
    	BufSelect(buf, insStart, insStart + origSel->end - origSel->start);
    	TextDSetInsertPosition(textD, insStart + origSel->end - origSel->start);
    }
    TextDUnblankCursor(textD);
    XtCallCallbacks((Widget)tw, textNcursorMovementCallback, (XtPointer)NULL);
    tw->text.emTabsBeforeCursor = 0;
}