New arrow key action routines improving the "word jump" action procedures Instead of always stopping at the fronts of words, you can set these action routines to stop at at line ends, the ends of runs of spaces, etc. by setting appropriate translations. This patch also allows "jump to end of selection": if you have an active selection, you can get your left/right keys to move to the end of it before deselecting it, rather than just moving one character position. - Added forward_same/backward_same action routines, to bind to ctrl-right/left for word skipping - Added forward/backward_character skipselect argument, to move to either end of an active selection before clearing it - Added forward_same/backward_same skipselect, skipblanks arguments, allowing fine control over where the cursor ends up Example translations NEdit*text.translations: #override \ Shift CtrlosfLeft: backward_same("extend")\n\ CtrlosfLeft: backward_same("skipselect","skipblanks")\n\ Shift CtrlosfRight: forward_same("extend","skipblanks")\n\ CtrlosfRight: forward_same("skipselect","skipblanks")\n\ ~Alt ~Shift ~Ctrl ~MetaosfRight: forward_character(skipselect)\n\ ~Alt ~Shift ~Ctrl ~MetaosfLeft: backward_character(skipselect)\n\ ... diff -ur -b nedit_official nedit_mod diff -ur -b nedit_official/source/text.c nedit_mod/source/text.c --- nedit_official/source/text.c 2004-04-28 22:34:01.000000000 -0400 +++ nedit_mod/source/text.c 2004-05-18 00:29:15.305550000 -0400 @@ -161,6 +161,8 @@ Cardinal *nArgs); static void deleteToEndOfLineAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); +static Boolean forwardSkipSelect(Widget w, int insertPos); +static Boolean backwardSkipSelect(Widget w, int insertPos); static void forwardCharacterAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); static void backwardCharacterAP(Widget w, XEvent *event, String *args, @@ -169,6 +171,10 @@ Cardinal *nArgs); static void backwardWordAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); +static void forwardSameAP(Widget w, XEvent *event, String *args, + Cardinal *nArgs); +static void backwardSameAP(Widget w, XEvent *event, String *args, + Cardinal *nArgs); static void forwardParagraphAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); static void backwardParagraphAP(Widget w, XEvent *event, String *args, @@ -211,6 +217,8 @@ Cardinal *nArgs); static void checkMoveSelectionChange(Widget w, XEvent *event, int startPos, String *args, Cardinal *nArgs); +static int getWidgetSelectionPos(Widget w, int *start, int *end, int *isRect, + int *rectStart, int *rectEnd); static void keyMoveExtendSelection(Widget w, XEvent *event, int startPos, int rectangular); static void checkAutoShowInsertPos(Widget w); @@ -528,6 +536,10 @@ {"forward_word", forwardWordAP}, {"backward-word", backwardWordAP}, {"backward_word", backwardWordAP}, + {"forward-same", forwardSameAP}, + {"forward_same", forwardSameAP}, + {"backward-same", backwardSameAP}, + {"backward_same", backwardSameAP}, {"forward-paragraph", forwardParagraphAP}, {"forward_paragraph", forwardParagraphAP}, {"backward-paragraph", backwardParagraphAP}, @@ -2577,14 +2589,40 @@ callCursorMovementCBs(w, event); } +/* move insertPos to end of selection if in selection - return true if done */ +static Boolean forwardSkipSelect(Widget w, int insertPos) +{ + int start, end; + if (getWidgetSelectionPos(w, &start, &end, NULL, NULL, NULL) && + start <= insertPos && insertPos <= end) { + TextDSetInsertPosition(((TextWidget)w)->text.textD, end); + return True; + } + return False; +} + +/* move insertPos to start of selection if in selection - return true if done */ +static Boolean backwardSkipSelect(Widget w, int insertPos) +{ + int start, end; + if (getWidgetSelectionPos(w, &start, &end, NULL, NULL, NULL) && + start <= insertPos && insertPos <= end) { + TextDSetInsertPosition(((TextWidget)w)->text.textD, start); + return True; + } + return False; +} + static void forwardCharacterAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) { int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD); int silent = hasKey("nobell", args, nArgs); + int skipsel = hasKey("skipselect", args, nArgs); cancelDrag(w); - if (!TextDMoveRight(((TextWidget)w)->text.textD)) + if (!(skipsel && forwardSkipSelect(w, insertPos)) && + !TextDMoveRight(((TextWidget)w)->text.textD)) ringIfNecessary(silent, w); checkMoveSelectionChange(w, event, insertPos, args, nArgs); checkAutoShowInsertPos(w); @@ -2596,9 +2634,11 @@ { int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD); int silent = hasKey("nobell", args, nArgs); + int skipsel = hasKey("skipselect", args, nArgs); cancelDrag(w); - if (!TextDMoveLeft(((TextWidget)w)->text.textD)) + if (!(skipsel && backwardSkipSelect(w, insertPos)) && + !TextDMoveLeft(((TextWidget)w)->text.textD)) ringIfNecessary(silent, w); checkMoveSelectionChange(w, event, insertPos, args, nArgs); checkAutoShowInsertPos(w); @@ -2667,6 +2707,154 @@ callCursorMovementCBs(w, event); } +static char *sameCharClassMap(char map[256], char *delimiters) +{ + static char spaces[] = " \t"; + static char classMap[256]; + static int done = 0; + static int spaceClass = 0, nlClass = 0, delimClass = 0; + int i; + if (!done) { + /* default value */ + for (i = 0; i < sizeof classMap; i++) + classMap[i] = done; /* zero */ + + /* letters, digits, _ */ + ++done; + for (i = 0; i < sizeof classMap; i++) + if (isalnum(i) || i == '_') + classMap[i] = done; + + /* other graphic characters */ + ++done; + for (i = 0; i < sizeof classMap; i++) + if (isgraph(i) && !classMap[i]) + classMap[i] = done; + + /* control characters */ + ++done; + for (i = 0; i < sizeof classMap; i++) + if (iscntrl(i) && !classMap[i]) + classMap[i] = done; + + /* other ascii characters */ + ++done; + for (i = 0; i < sizeof classMap; i++) + if (isascii(i) && !classMap[i]) + classMap[i] = done; + + /* NEdit's spaces */ + spaceClass = ++done; + for (i = 0; spaces[i]; i++) + classMap[spaces[i]] = done; + /* and newline */ + nlClass = ++done; + classMap['\n'] = done; + /* and a class for delimiters */ + delimClass = ++done; + } + + /* current delimiters */ + memcpy(map, classMap, 256); + for (i = 0; delimiters[i]; i++) { + unsigned char ch = delimiters[i]; + int cl = classMap[ch]; + /* do NOT overwrite the NEdit spaces/newline classes */ + if (cl != spaceClass && cl != nlClass) + map[ch] = delimClass; + } + return map; +} + +static void forwardSameAP(Widget w, XEvent *event, String *args, + Cardinal *nArgs) +{ + textDisp *textD = ((TextWidget)w)->text.textD; + textBuffer *buf = textD->buffer; + int pos, insertPos = TextDGetInsertPosition(textD); + char *delimiters = ((TextWidget)w)->text.delimiters; + int silent = hasKey("nobell", args, nArgs); + int incBlanks = hasKey("skipblanks", args, nArgs); + int skipsel = hasKey("skipselect", args, nArgs); + + char classMap[256]; + int chClass, spClass; + + cancelDrag(w); + if (insertPos == buf->length) { + ringIfNecessary(silent, w); + return; + } + + if (!(skipsel && forwardSkipSelect(w, insertPos))) { + pos = insertPos; + /* pick up the class map and the current char class, move forward */ + sameCharClassMap(classMap, delimiters); + chClass = classMap[(unsigned char)BufGetCharacter(buf, pos++)]; + spClass = incBlanks ? classMap[' '] : chClass; + + /* now skip over all characters from here of that class */ + for (; pos < buf->length; pos++) { + if (classMap[(unsigned char)BufGetCharacter(buf, pos)] != chClass) + break; + } + /* and skip over all characters from there for trailing blanks */ + for (; spClass != chClass && pos < buf->length; pos++) { + if (classMap[(unsigned char)BufGetCharacter(buf, pos)] != spClass) + break; + } + /* pos is position of first character not of the same class */ + TextDSetInsertPosition(textD, pos); + } + checkMoveSelectionChange(w, event, insertPos, args, nArgs); + checkAutoShowInsertPos(w); + callCursorMovementCBs(w, event); +} + +static void backwardSameAP(Widget w, XEvent *event, String *args, + Cardinal *nArgs) +{ + textDisp *textD = ((TextWidget)w)->text.textD; + textBuffer *buf = textD->buffer; + int pos, insertPos = TextDGetInsertPosition(textD); + char *delimiters = ((TextWidget)w)->text.delimiters; + int silent = hasKey("nobell", args, nArgs); + int incBlanks = hasKey("skipblanks", args, nArgs); + int skipsel = hasKey("skipselect", args, nArgs); + + char classMap[256]; + int chClass, spClass; + cancelDrag(w); + if (insertPos == 0) { + ringIfNecessary(silent, w); + return; + } + + if (!(skipsel && backwardSkipSelect(w, insertPos))) { + pos = insertPos - 1; /* we know insertPos != 0 */ + sameCharClassMap(classMap, delimiters); /* get class map */ + /* pick up the class of previous character */ + chClass = classMap[(unsigned char)BufGetCharacter(buf, pos)]; + spClass = incBlanks ? classMap[' '] : chClass; + + /* now skip back over all characters from here of that class */ + for (; pos > 0; --pos) { + if (classMap[(unsigned char)BufGetCharacter(buf, pos-1)] != chClass) + break; + } + /* and skip back over all characters from there for leading blanks */ + for (; spClass != chClass && pos > 0; --pos) { + if (classMap[(unsigned char)BufGetCharacter(buf, pos-1)] != spClass) + break; + } + /* pos is position of first character not of the same class */ + TextDSetInsertPosition(textD, pos); + } + checkMoveSelectionChange(w, event, insertPos, args, nArgs); + checkAutoShowInsertPos(w); + callCursorMovementCBs(w, event); +} + static void forwardParagraphAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) { @@ -3344,6 +3532,23 @@ } /* +** Fish out current selection information, returning true if there is one. +*/ +static int getWidgetSelectionPos(Widget w, int *start, int *end, int *isRect, + int *rectStart, int *rectEnd) +{ + int dummy; + if (!isRect) + isRect = &dummy; + if (!rectStart) + rectStart = &dummy; + if (!rectEnd) + rectEnd = &dummy; + return BufGetSelectionPos((((TextWidget)w)->text.textD)->buffer, + start, end, isRect, rectStart, rectEnd); +} + +/* ** If a selection change was requested via a keyboard command for moving ** the insertion cursor (usually with the "extend" keyword), adjust the ** selection to include the new cursor position, or begin a new selection