⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selection.c

📁 nedit 是一款linux下的开发源码的功能强大的编辑器
💻 C
📖 第 1 页 / 共 2 页
字号:
static const char CVSID[] = "$Id: selection.c,v 1.25 2003/05/09 17:43:48 edg Exp $";/********************************************************************************									       ** Copyright (C) 1999 Mark Edel						       **									       ** This is free software; you can redistribute it and/or modify it under the    ** terms of the GNU General Public License as published by the Free Software    ** Foundation; either version 2 of the License, or (at your option) any later   ** version.							               ** 									       ** This software is distributed in the hope that it will be useful, but WITHOUT ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        ** FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License        ** for more details.							       ** 									       ** You should have received a copy of the GNU General Public License along with ** software; if not, write to the Free Software Foundation, Inc., 59 Temple     ** Place, Suite 330, Boston, MA  02111-1307 USA		                       **									       ** Nirvana Text Editor	    						       ** May 10, 1991								       **									       ** Written by Mark Edel							       **									       ********************************************************************************/#ifdef HAVE_CONFIG_H#include "../config.h"#endif#include "selection.h"#include "textBuf.h"#include "text.h"#include "nedit.h"#include "file.h"#include "window.h"#include "menu.h"#include "server.h"#include "../util/DialogF.h"#include "../util/fileUtils.h"#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include <string.h>#include <limits.h>#ifdef VMS#include "../util/VMSparam.h"#else#ifndef __MVS__#include <sys/param.h>#endif#endif /*VMS*/#if !defined(DONT_HAVE_GLOB) && !defined(USE_MOTIF_GLOB) && !defined(VMS)#include <glob.h>#endif#include <Xm/Xm.h>#include <X11/Xatom.h>#ifdef HAVE_DEBUG_H#include "../debug.h"#endifstatic void gotoCB(Widget widget, WindowInfo *window, Atom *sel,	Atom *type, char *value, int *length, int *format);static void fileCB(Widget widget, WindowInfo *window, Atom *sel,	Atom *type, char *value, int *length, int *format);static void getAnySelectionCB(Widget widget, char **result, Atom *sel,	Atom *type, char *value, int *length, int *format);static void processMarkEvent(Widget w, XtPointer clientData, XEvent *event,    	Boolean *continueDispatch, char *action, int extend);static void markTimeoutProc(XtPointer clientData, XtIntervalId *id);static void markKeyCB(Widget w, XtPointer clientData, XEvent *event,    	Boolean *continueDispatch);static void gotoMarkKeyCB(Widget w, XtPointer clientData, XEvent *event,    	Boolean *continueDispatch);static void gotoMarkExtendKeyCB(Widget w, XtPointer clientData, XEvent *event,    	Boolean *continueDispatch);static void maintainSelection(selection *sel, int pos, int nInserted,    	int nDeleted);static void maintainPosition(int *position, int modPos, int nInserted,    	int nDeleted);/*** Extract the line and column number from the text string.** Set the line and/or column number to -1 if not specified, and return -1 if** both line and column numbers are not specified.*/int StringToLineAndCol(const char *text, int *lineNum, int *column) {    char *endptr;    long  tempNum;    int   textLen;    /* Get line number */    tempNum = strtol( text, &endptr, 10 );    /* If user didn't specify a line number, set lineNum to -1 */    if      ( endptr  == text    ) { *lineNum = -1;      }    else if ( tempNum >= INT_MAX ) { *lineNum = INT_MAX; }    else if ( tempNum <  0       ) { *lineNum = 0;       }    else                           { *lineNum = tempNum; }    /* Find the next digit */    for ( textLen = strlen( endptr ); textLen > 0; endptr++, textLen-- ) {        if (isdigit((unsigned char) *endptr ) || *endptr == '-' || *endptr == '+') {            break;        }    }    /* Get column */    if ( *endptr != '\0' ) {        tempNum = strtol( endptr, NULL, 10 );        if      ( tempNum >= INT_MAX ) { *column = INT_MAX; }        else if ( tempNum <  0       ) { *column = 0;       }        else                           { *column = tempNum; }    }    else { *column = -1; }    return *lineNum == -1 && *column == -1 ? -1 : 0;}void GotoLineNumber(WindowInfo *window){    char lineNumText[DF_MAX_PROMPT_LENGTH], *params[1];    int lineNum, column, response;        response = DialogF(DF_PROMPT, window->shell, 2, "Goto Line Number",            "Goto Line (and/or Column)  Number:", lineNumText, "OK", "Cancel");    if (response == 2)    	return;    if (StringToLineAndCol(lineNumText, &lineNum, &column) == -1) {    	XBell(TheDisplay, 0);	return;    }    params[0] = lineNumText;    XtCallActionProc(window->lastFocus, "goto_line_number", NULL, params, 1);}    void GotoSelectedLineNumber(WindowInfo *window, Time time){    XtGetSelectionValue(window->textArea, XA_PRIMARY, XA_STRING,    	    (XtSelectionCallbackProc)gotoCB, window, time);}void OpenSelectedFile(WindowInfo *window, Time time){    XtGetSelectionValue(window->textArea, XA_PRIMARY, XA_STRING,    	    (XtSelectionCallbackProc)fileCB, window, time);}/*** 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;}static void gotoCB(Widget widget, WindowInfo *window, Atom *sel,    	Atom *type, char *value, int *length, int *format){     /* two integers and some space in between */    char lineText[(TYPE_INT_STR_SIZE(int) * 2) + 5];    int rc, lineNum, column, position, curCol;        /* skip if we can't get the selection data, or it's obviously not a number */    if (*type == XT_CONVERT_FAIL || value == NULL) {    	XBell(TheDisplay, 0);	return;    }    if (((size_t) *length) > sizeof(lineText) - 1) {    	XBell(TheDisplay, 0);	XtFree(value);	return;    }    /* should be of type text??? */    if (*format != 8) {    	fprintf(stderr, "NEdit: Can't handle non 8-bit text\n");    	XBell(TheDisplay, 0);	XtFree(value);	return;    }    strncpy(lineText, value, sizeof(lineText));    lineText[sizeof(lineText) - 1] = '\0';        rc = StringToLineAndCol(lineText, &lineNum, &column);    XtFree(value);    if (rc == -1) {    	XBell(TheDisplay, 0);	return;    }    /* User specified column, but not line number */    if ( lineNum == -1 ) {        position = TextGetCursorPos(widget);        if (TextPosToLineAndCol(widget, position, &lineNum, &curCol) == False) {            XBell(TheDisplay, 0);            return;        }    }    /* User didn't specify a column */    else if ( column == -1 ) {        SelectNumberedLine(window, lineNum);        return;    }    position = TextLineAndColToPos(widget, lineNum, column );    if ( position == -1 ) {        XBell(TheDisplay, 0);        return;    }    TextSetCursorPos(widget, position);}static void fileCB(Widget widget, WindowInfo *window, Atom *sel,    	Atom *type, char *value, int *length, int *format){    char nameText[MAXPATHLEN], includeName[MAXPATHLEN];    char filename[MAXPATHLEN], pathname[MAXPATHLEN];    char *inPtr, *outPtr;#ifdef VMS#ifndef __DECC    static char includeDir[] = "sys$library:";#else    static char includeDir[] = "decc$library_include:";#endif#else    static char includeDir[] = "/usr/include/";#endif /* VMS */        /* get the string, or skip if we can't get the selection data, or it's       obviously not a file name */    if (*type == XT_CONVERT_FAIL || value == NULL) {    	XBell(TheDisplay, 0);	return;    }    if (*length > MAXPATHLEN || *length == 0) {    	XBell(TheDisplay, 0);	XtFree(value);	return;    }    /* should be of type text??? */    if (*format != 8) {    	fprintf(stderr, "NEdit: Can't handle non 8-bit text\n");    	XBell(TheDisplay, 0);	XtFree(value);	return;    }    strncpy(nameText, value, *length);    XtFree(value);    nameText[*length] = '\0';        /* extract name from #include syntax */    if (sscanf(nameText, "#include \"%[^\"]\"", includeName) == 1)    	strcpy(nameText, includeName);    else if (sscanf(nameText, "#include <%[^<>]>", includeName) == 1)    	sprintf(nameText, "%s%s", includeDir, includeName);        /* strip whitespace from name */    for (inPtr=nameText, outPtr=nameText; *inPtr!='\0'; inPtr++)    	if (*inPtr != ' ' && *inPtr != '\t' && *inPtr != '\n')    	    *outPtr++ = *inPtr;    *outPtr = '\0';#ifdef VMS    /* If path name is relative, make it refer to current window's directory */    if ((strchr(nameText, ':') == NULL) && (strlen(nameText) > 1) &&      	    !((nameText[0] == '[') && (nameText[1] != '-') &&	      (nameText[1] != '.'))) {	strcpy(filename, window->path);	strcat(filename, nameText);	strcpy(nameText, filename);    }#else    /* Process ~ characters in name */    ExpandTilde(nameText);            /* If path name is relative, make it refer to current window's directory */    if (nameText[0] != '/') {	strcpy(filename, window->path);	strcat(filename, nameText);	strcpy(nameText, filename);    }#endif        /* Expand wildcards in file name.       Some older systems don't have the glob subroutine for expanding file       names, in these cases, either don't expand names, or try to use the       Motif internal parsing routine _XmOSGetDirEntries, which is not       guranteed to be available, but in practice is there and does work. */#if defined(DONT_HAVE_GLOB) || defined(VMS)    /* Open the file */    if (ParseFilename(nameText, filename, pathname) != 0) {        XBell(TheDisplay, 0);	return;    }	    EditExistingFile(WindowList, filename, pathname, 0, NULL, False, NULL);#elif defined(USE_MOTIF_GLOB)    { char **nameList = NULL;      int i, nFiles = 0, maxFiles = 30;      if (ParseFilename(nameText, filename, pathname) != 0) {           XBell(TheDisplay, 0);	   return;      }      _XmOSGetDirEntries(pathname, filename, XmFILE_ANY_TYPE, False, True,	      &nameList, &nFiles, &maxFiles);      for (i=0; i<nFiles; i++) {	  if (ParseFilename(nameList[i], filename, pathname) != 0) {	      XBell(TheDisplay, 0);	  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -