1 New arrow key action routines improving the "word jump" action procedures
5 http://sourceforge.net/tracker/index.php?func=detail&aid=970496&group_id=11005&atid=311005
6 [ 970496 ] New arrow key action routines improving "word jump" actions
7 ForbackwardSame.diff 2004-06-10 08:41
9 Instead of always stopping at the fronts of words, you can set these action
10 routines to stop at line ends, the ends of runs of spaces, etc. by setting
11 appropriate translations. This patch also allows "jump to end of selection":
12 if you have an active selection, you can get your left/right keys to move to
13 the end of it before deselecting it, rather than just moving one character
16 - Added forward_same/backward_same action routines, to bind to
17 ctrl-right/left for word skipping
18 - Added forward/backward_character skipselect argument, to move to either
19 end of an active selection before clearing it
20 - Added forward_same/backward_same skipselect, skipblanks arguments,
21 allowing fine control over where the cursor ends up
25 NEdit*text.translations: #override \
26 Shift Ctrl<KeyPress>osfLeft: backward_same("extend")\n\
27 Ctrl<KeyPress>osfLeft: backward_same("skipselect","skipblanks")\n\
28 Shift Ctrl<KeyPress>osfRight: forward_same("extend","skipblanks")\n\
29 Ctrl<KeyPress>osfRight: forward_same("skipselect","skipblanks")\n\
30 ~Alt ~Shift ~Ctrl ~Meta<KeyPress>osfRight: forward_character(skipselect)\n\
31 ~Alt ~Shift ~Ctrl ~Meta<KeyPress>osfLeft: backward_character(skipselect)\n\
36 source/text.c | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
37 1 files changed, 207 insertions(+), 2 deletions(-)
39 diff --quilt old/source/text.c new/source/text.c
42 @@ -162,18 +162,24 @@ static void deleteNextWordAP(Widget w, X
44 static void deleteToStartOfLineAP(Widget w, XEvent *event, String *args,
46 static void deleteToEndOfLineAP(Widget w, XEvent *event, String *args,
48 +static Boolean forwardSkipSelect(Widget w, int insertPos);
49 +static Boolean backwardSkipSelect(Widget w, int insertPos);
50 static void forwardCharacterAP(Widget w, XEvent *event, String *args,
52 static void backwardCharacterAP(Widget w, XEvent *event, String *args,
54 static void forwardWordAP(Widget w, XEvent *event, String *args,
56 static void backwardWordAP(Widget w, XEvent *event, String *args,
58 +static void forwardSameAP(Widget w, XEvent *event, String *args,
60 +static void backwardSameAP(Widget w, XEvent *event, String *args,
62 static void forwardParagraphAP(Widget w, XEvent *event, String *args,
64 static void backwardParagraphAP(Widget w, XEvent *event, String *args,
66 static void keySelectAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
67 @@ -214,10 +220,12 @@ static void focusInAP(Widget w, XEvent *
69 static void focusOutAP(Widget w, XEvent *event, String *args,
71 static void checkMoveSelectionChange(Widget w, XEvent *event, int startPos,
72 String *args, Cardinal *nArgs);
73 +static int getWidgetSelectionPos(Widget w, int *start, int *end, int *isRect,
74 + int *rectStart, int *rectEnd);
75 static void keyMoveExtendSelection(Widget w, XEvent *event, int startPos,
77 static void checkAutoShowInsertPos(Widget w);
78 static int checkReadOnly(Widget w);
79 static void simpleInsertAtCursor(Widget w, char *chars, XEvent *event,
80 @@ -537,10 +545,14 @@ static XtActionsRec actionsList[] = {
81 {"process_home", beginningOfLineAP},
82 {"forward-word", forwardWordAP},
83 {"forward_word", forwardWordAP},
84 {"backward-word", backwardWordAP},
85 {"backward_word", backwardWordAP},
86 + {"forward-same", forwardSameAP},
87 + {"forward_same", forwardSameAP},
88 + {"backward-same", backwardSameAP},
89 + {"backward_same", backwardSameAP},
90 {"forward-paragraph", forwardParagraphAP},
91 {"forward_paragraph", forwardParagraphAP},
92 {"backward-paragraph", backwardParagraphAP},
93 {"backward_paragraph", backwardParagraphAP},
94 {"beginning-of-line", beginningOfLineAP},
95 @@ -2649,18 +2661,44 @@ static void deleteToStartOfLineAP(Widget
96 BufRemove(textD->buffer, startOfLine, insertPos);
97 checkAutoShowInsertPos(w);
98 callCursorMovementCBs(w, event);
101 +/* move insertPos to end of selection if in selection - return true if done */
102 +static Boolean forwardSkipSelect(Widget w, int insertPos)
105 + if (getWidgetSelectionPos(w, &start, &end, NULL, NULL, NULL) &&
106 + start <= insertPos && insertPos <= end) {
107 + TextDSetInsertPosition(((TextWidget)w)->text.textD, end);
113 +/* move insertPos to start of selection if in selection - return true if done */
114 +static Boolean backwardSkipSelect(Widget w, int insertPos)
117 + if (getWidgetSelectionPos(w, &start, &end, NULL, NULL, NULL) &&
118 + start <= insertPos && insertPos <= end) {
119 + TextDSetInsertPosition(((TextWidget)w)->text.textD, start);
125 static void forwardCharacterAP(Widget w, XEvent *event, String *args,
128 int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
129 int silent = hasKey("nobell", args, nArgs);
130 + int skipsel = hasKey("skipselect", args, nArgs);
133 - if (!TextDMoveRight(((TextWidget)w)->text.textD)) {
134 + if (!(skipsel && forwardSkipSelect(w, insertPos)) &&
135 + !TextDMoveRight(((TextWidget)w)->text.textD)) {
136 ringIfNecessary(silent, w);
138 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
139 checkAutoShowInsertPos(w);
140 callCursorMovementCBs(w, event);
141 @@ -2669,13 +2707,15 @@ static void forwardCharacterAP(Widget w,
142 static void backwardCharacterAP(Widget w, XEvent *event, String *args,
145 int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
146 int silent = hasKey("nobell", args, nArgs);
147 + int skipsel = hasKey("skipselect", args, nArgs);
150 - if (!TextDMoveLeft(((TextWidget)w)->text.textD)) {
151 + if (!(skipsel && backwardSkipSelect(w, insertPos)) &&
152 + !TextDMoveLeft(((TextWidget)w)->text.textD)) {
153 ringIfNecessary(silent, w);
155 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
156 checkAutoShowInsertPos(w);
157 callCursorMovementCBs(w, event);
158 @@ -2759,10 +2799,158 @@ static void backwardWordAP(Widget w, XEv
159 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
160 checkAutoShowInsertPos(w);
161 callCursorMovementCBs(w, event);
164 +static char *sameCharClassMap(char map[256], char *delimiters)
166 + static char spaces[] = " \t";
167 + static char classMap[256];
168 + static int done = 0;
169 + static int spaceClass = 0, nlClass = 0, delimClass = 0;
172 + /* default value */
173 + for (i = 0; i < sizeof classMap; i++)
174 + classMap[i] = done; /* zero */
176 + /* letters, digits, _ */
178 + for (i = 0; i < sizeof classMap; i++)
179 + if (isalnum(i) || i == '_')
180 + classMap[i] = done;
182 + /* other graphic characters */
184 + for (i = 0; i < sizeof classMap; i++)
185 + if (isgraph(i) && !classMap[i])
186 + classMap[i] = done;
188 + /* control characters */
190 + for (i = 0; i < sizeof classMap; i++)
191 + if (iscntrl(i) && !classMap[i])
192 + classMap[i] = done;
194 + /* other ascii characters */
196 + for (i = 0; i < sizeof classMap; i++)
197 + if (isascii(i) && !classMap[i])
198 + classMap[i] = done;
200 + /* NEdit's spaces */
201 + spaceClass = ++done;
202 + for (i = 0; spaces[i]; i++)
203 + classMap[spaces[i]] = done;
206 + classMap['\n'] = done;
207 + /* and a class for delimiters */
208 + delimClass = ++done;
211 + /* current delimiters */
212 + memcpy(map, classMap, 256);
213 + for (i = 0; delimiters[i]; i++) {
214 + unsigned char ch = delimiters[i];
215 + int cl = classMap[ch];
216 + /* do NOT overwrite the NEdit spaces/newline classes */
217 + if (cl != spaceClass && cl != nlClass)
218 + map[ch] = delimClass;
223 +static void forwardSameAP(Widget w, XEvent *event, String *args,
226 + textDisp *textD = ((TextWidget)w)->text.textD;
227 + textBuffer *buf = textD->buffer;
228 + int pos, insertPos = TextDGetInsertPosition(textD);
229 + char *delimiters = ((TextWidget)w)->text.delimiters;
230 + int silent = hasKey("nobell", args, nArgs);
231 + int incBlanks = hasKey("skipblanks", args, nArgs);
232 + int skipsel = hasKey("skipselect", args, nArgs);
234 + char classMap[256];
235 + int chClass, spClass;
238 + if (insertPos == buf->length) {
239 + ringIfNecessary(silent, w);
243 + if (!(skipsel && forwardSkipSelect(w, insertPos))) {
245 + /* pick up the class map and the current char class, move forward */
246 + sameCharClassMap(classMap, delimiters);
247 + chClass = classMap[(unsigned char)BufGetCharacter(buf, pos++)];
248 + spClass = incBlanks ? classMap[' '] : chClass;
250 + /* now skip over all characters from here of that class */
251 + for (; pos < buf->length; pos++) {
252 + if (classMap[(unsigned char)BufGetCharacter(buf, pos)] != chClass)
255 + /* and skip over all characters from there for trailing blanks */
256 + for (; spClass != chClass && pos < buf->length; pos++) {
257 + if (classMap[(unsigned char)BufGetCharacter(buf, pos)] != spClass)
260 + /* pos is position of first character not of the same class */
261 + TextDSetInsertPosition(textD, pos);
263 + checkMoveSelectionChange(w, event, insertPos, args, nArgs);
264 + checkAutoShowInsertPos(w);
265 + callCursorMovementCBs(w, event);
268 +static void backwardSameAP(Widget w, XEvent *event, String *args,
271 + textDisp *textD = ((TextWidget)w)->text.textD;
272 + textBuffer *buf = textD->buffer;
273 + int pos, insertPos = TextDGetInsertPosition(textD);
274 + char *delimiters = ((TextWidget)w)->text.delimiters;
275 + int silent = hasKey("nobell", args, nArgs);
276 + int incBlanks = hasKey("skipblanks", args, nArgs);
277 + int skipsel = hasKey("skipselect", args, nArgs);
279 + char classMap[256];
280 + int chClass, spClass;
282 + if (insertPos == 0) {
283 + ringIfNecessary(silent, w);
287 + if (!(skipsel && backwardSkipSelect(w, insertPos))) {
288 + pos = insertPos - 1; /* we know insertPos != 0 */
289 + sameCharClassMap(classMap, delimiters); /* get class map */
290 + /* pick up the class of previous character */
291 + chClass = classMap[(unsigned char)BufGetCharacter(buf, pos)];
292 + spClass = incBlanks ? classMap[' '] : chClass;
294 + /* now skip back over all characters from here of that class */
295 + for (; pos > 0; --pos) {
296 + if (classMap[(unsigned char)BufGetCharacter(buf, pos-1)] != chClass)
299 + /* and skip back over all characters from there for leading blanks */
300 + for (; spClass != chClass && pos > 0; --pos) {
301 + if (classMap[(unsigned char)BufGetCharacter(buf, pos-1)] != spClass)
304 + /* pos is position of first character not of the same class */
305 + TextDSetInsertPosition(textD, pos);
307 + checkMoveSelectionChange(w, event, insertPos, args, nArgs);
308 + checkAutoShowInsertPos(w);
309 + callCursorMovementCBs(w, event);
312 static Boolean lineIsBlank(textBuffer *buf, int pos, int *start)
314 static const char whiteChars[] = " \t";
317 @@ -3597,10 +3785,27 @@ static void checkMoveSelectionChange(Wid
319 BufUnselect((((TextWidget)w)->text.textD)->buffer);
323 +** Fish out current selection information, returning true if there is one.
325 +static int getWidgetSelectionPos(Widget w, int *start, int *end, int *isRect,
326 + int *rectStart, int *rectEnd)
332 + rectStart = &dummy;
335 + return BufGetSelectionPos((((TextWidget)w)->text.textD)->buffer,
336 + start, end, isRect, rectStart, rectEnd);
340 ** If a selection change was requested via a keyboard command for moving
341 ** the insertion cursor (usually with the "extend" keyword), adjust the
342 ** selection to include the new cursor position, or begin a new selection
343 ** between startPos and the new cursor position with anchor at startPos.