Beispiel #1
0
/*
** Filter the current selection through shell command "command".  The selection
** is removed, and replaced by the output from the command execution.  Failed
** command status and output to stderr are presented in dialog form.
*/
void FilterSelection(WindowInfo *window, const char *command, int fromMacro)
{
    int left, right, textLen;
    char *text;

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

    /* Get the selection and the range in character positions that it
       occupies.  Beep and return if no selection */
    text = BufGetSelectionText(window->buffer);
    if (*text == '\0') {
	XtFree(text);
	XBell(TheDisplay, 0);
	return;
    }
    textLen = strlen(text);
    BufUnsubstituteNullChars(text, window->buffer);
    left = window->buffer->primary.start;
    right = window->buffer->primary.end;
    
    /* Issue the command and collect its output */
    issueCommand(window, command, text, textLen, ACCUMULATE | ERROR_DIALOGS |
	    REPLACE_SELECTION, window->lastFocus, left, right, fromMacro);
}
Beispiel #2
0
/*
** Getting the current selection by making the request, and then blocking
** (processing events) while waiting for a reply.  On failure (timeout or
** bad format) returns NULL, otherwise returns the contents of the selection.
*/
char *GetAnySelection(WindowInfo *window)
{
    static char waitingMarker[1] = "";
    char *selText = waitingMarker;
    XEvent nextEvent;	 
    
    /* If the selection is in the window's own buffer get it from there,
       but substitute null characters as if it were an external selection */
    if (window->buffer->primary.selected) {
	selText = BufGetSelectionText(window->buffer);
	BufUnsubstituteNullChars(selText, window->buffer);
	return selText;
    }
    
    /* Request the selection value to be delivered to getAnySelectionCB */
    XtGetSelectionValue(window->textArea, XA_PRIMARY, XA_STRING,
	    (XtSelectionCallbackProc)getAnySelectionCB, &selText, 
	    XtLastTimestampProcessed(XtDisplay(window->textArea)));

    /* Wait for the value to appear */
    while (selText == waitingMarker) {
	XtAppNextEvent(XtWidgetToApplicationContext(window->textArea), 
		&nextEvent);
	ServerDispatchEvent(&nextEvent);
    }
    return selText;
}
Beispiel #3
0
/*
** Copy the primary selection to the clipboard
*/
void CopyToClipboard(Ne_Text_Editor* textD, double time)
{
   long itemID = 0;

   /* Get the selected text, if there's no selection, do nothing */
   char* text = BufGetSelectionText(textD->buffer);
   if (*text == '\0')
   {
      free__(text);
      return;
   }

   /* If the string contained ascii-nul characters, something else was substituted in the buffer.  Put the nulls back */
   int length = strlen(text);
   BufUnsubstituteNullChars(text, textD->buffer);

   // Copy to the clipboard
   Fl::copy(text, length, 1);
}
Beispiel #4
0
/*
** Do a shell command, with the options allowed to users (input source,
** output destination, save first and load after) in the shell commands
** menu.
*/
void DoShellMenuCmd(WindowInfo *window, const char *command,
        int input, int output,
	int outputReplacesInput, int saveFirst, int loadAfter, int fromMacro) 
{
    int flags = 0;
    char *text;
    char *subsCommand, fullName[MAXPATHLEN];
    int left, right, textLen;
    int pos, line, column;
    char lineNumber[11];
    WindowInfo *inWindow = window;
    Widget outWidget;

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

    /* Substitute the current file name for % and the current line number
       for # in the shell command */
    strcpy(fullName, window->path);
    strcat(fullName, window->filename);
    pos = TextGetCursorPos(window->lastFocus);
    TextPosToLineAndCol(window->lastFocus, pos, &line, &column);
    sprintf(lineNumber, "%d", line);
    
    subsCommand = shellCommandSubstitutes(command, 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;
    }

    /* Get the command input as a text string.  If there is input, errors
      shouldn't be mixed in with output, so set flags to ERROR_DIALOGS */
    if (input == FROM_SELECTION) {
	text = BufGetSelectionText(window->buffer);
	if (*text == '\0') {
    	    XtFree(text);
            free(subsCommand);
    	    XBell(TheDisplay, 0);
    	    return;
    	}
    	flags |= ACCUMULATE | ERROR_DIALOGS;
    } else if (input == FROM_WINDOW) {
	text = BufGetAll(window->buffer);
    	flags |= ACCUMULATE | ERROR_DIALOGS;
    } else if (input == FROM_EITHER) {
	text = BufGetSelectionText(window->buffer);
	if (*text == '\0') {
	    XtFree(text);
	    text = BufGetAll(window->buffer);
    	}
    	flags |= ACCUMULATE | ERROR_DIALOGS;
    } else /* FROM_NONE */
    	text = NULL;
    
    /* If the buffer was substituting another character for ascii-nuls,
       put the nuls back in before exporting the text */
    if (text != NULL) {
	textLen = strlen(text);
	BufUnsubstituteNullChars(text, window->buffer);
    } else
	textLen = 0;
    
    /* Assign the output destination.  If output is to a new window,
       create it, and run the command from it instead of the current
       one, to free the current one from waiting for lengthy execution */
    if (output == TO_DIALOG) {
    	outWidget = NULL;
	flags |= OUTPUT_TO_DIALOG;
    	left = right = 0;
    } else if (output == TO_NEW_WINDOW) {
    	EditNewFile(GetPrefOpenInTab()?inWindow:NULL, NULL, False, NULL, window->path);
    	outWidget = WindowList->textArea;
	inWindow = WindowList;
    	left = right = 0;
	CheckCloseDim();
    } else { /* TO_SAME_WINDOW */
    	outWidget = window->lastFocus;
    	if (outputReplacesInput && input != FROM_NONE) {
    	    if (input == FROM_WINDOW) {
    		left = 0;
    		right = window->buffer->length;
    	    } else if (input == FROM_SELECTION) {
    	    	GetSimpleSelection(window->buffer, &left, &right);
	        flags |= ACCUMULATE | REPLACE_SELECTION;
    	    } else if (input == FROM_EITHER) {
    	    	if (GetSimpleSelection(window->buffer, &left, &right))
	            flags |= ACCUMULATE | REPLACE_SELECTION;
	        else {
	            left = 0;
	            right = window->buffer->length;
	        }
	    }
    	} else {
	    if (GetSimpleSelection(window->buffer, &left, &right))
	        flags |= ACCUMULATE | REPLACE_SELECTION;
	    else
    		left = right = TextGetCursorPos(window->lastFocus);
    	}
    }
    
    /* If the command requires the file be saved first, save it */
    if (saveFirst) {
    	if (!SaveWindow(window)) {
    	    if (input != FROM_NONE)
    		XtFree(text);
            free(subsCommand);
    	    return;
	}
    }
    
    /* If the command requires the file to be reloaded after execution, set
       a flag for issueCommand to deal with it when execution is complete */
    if (loadAfter)
    	flags |= RELOAD_FILE_AFTER;
    	
    /* issue the command */
    issueCommand(inWindow, subsCommand, text, textLen, flags, outWidget, left,
	    right, fromMacro);
    free(subsCommand);
}
/*
** 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;
}