Ejemplo n.º 1
0
/*
** Execute the line of text where the the insertion cursor is positioned
** as a shell command.
*/
void ExecCursorLine(WindowInfo *window, int fromMacro)
{
    char *cmdText;
    int left, right, insertPos;
    char *subsCommand, fullName[MAXPATHLEN];
    int pos, line, column;
    char lineNumber[11];

    /* Can't do two shell commands at once in the same window */
    if (window->shellCmdData != NULL) {
    	XBell(TheDisplay, 0);
    	return;
    }

    /* get all of the text on the line with the insert position */
    pos = TextGetCursorPos(window->lastFocus);
    if (!GetSimpleSelection(window->buffer, &left, &right)) {
	left = right = pos;
	left = BufStartOfLine(window->buffer, left);
	right = BufEndOfLine(window->buffer, right);
	insertPos = right;
    } else
    	insertPos = BufEndOfLine(window->buffer, right);
    cmdText = BufGetRange(window->buffer, left, right);
    BufUnsubstituteNullChars(cmdText, window->buffer);
    
    /* insert a newline after the entire line */
    BufInsert(window->buffer, insertPos, "\n");

    /* Substitute the current file name for % and the current line number
       for # in the shell command */
    strcpy(fullName, window->path);
    strcat(fullName, window->filename);
    TextPosToLineAndCol(window->lastFocus, pos, &line, &column);
    sprintf(lineNumber, "%d", line);
    
    subsCommand = shellCommandSubstitutes(cmdText, fullName, lineNumber);
    if (subsCommand == NULL)
    {
        DialogF(DF_ERR, window->shell, 1, "Shell Command",
                "Shell command is too long due to\n"
                "filename substitutions with '%%' or\n"
                "line number substitutions with '#'", "OK");
        return;
    }

    /* issue the command */
    issueCommand(window, subsCommand, NULL, 0, 0, window->lastFocus, insertPos+1,
	    insertPos+1, fromMacro);
    free(subsCommand);
    XtFree(cmdText);
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
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;
}