re-fresh
[nedit-bw.git] / ForbackwardSame.diff
blobf68b62443f60cba56b5791afe4c39e70c4271d78
1 From: Tony Balinski <ajbj@free.fr>
2 Subject: New arrow key action routines improving the "word jump" action procedures
4 Available as a patch:
6 http://sourceforge.net/tracker/index.php?func=detail&aid=970496&group_id=11005&atid=311005
7 [ 970496 ] New arrow key action routines improving "word jump" actions
8 ForbackwardSame.diff 2004-06-10 08:41
10 Instead of always stopping at the fronts of words, you can set these action
11 routines to stop at line ends, the ends of runs of spaces, etc. by setting
12 appropriate translations. This patch also allows "jump to end of selection":
13 if you have an active selection, you can get your left/right keys to move to
14 the end of it before deselecting it, rather than just moving one character
15 position.
17 * Added forward_same/backward_same action routines, to bind to
18 ctrl-right/left for word skipping
19 * Added forward/backward_character skipselect argument, to move to
20 either end of an active selection before clearing it
21 * Added forward_same/backward_same skipselect, skipblanks arguments,
22 allowing fine control over where the cursor ends up
24 Example translations:
26 NEdit*text.translations: #override \
27 Shift Ctrl<KeyPress>osfLeft: backward_same("extend")\n\
28 Ctrl<KeyPress>osfLeft: backward_same("skipselect","skipblanks")\n\
29 Shift Ctrl<KeyPress>osfRight: forward_same("extend","skipblanks")\n\
30 Ctrl<KeyPress>osfRight: forward_same("skipselect","skipblanks")\n\
31 ~Alt ~Shift ~Ctrl ~Meta<KeyPress>osfRight: forward_character(skipselect)\n\
32 ~Alt ~Shift ~Ctrl ~Meta<KeyPress>osfLeft: backward_character(skipselect)\n\
33 ...
35 ---
37 source/text.c | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
38 1 file changed, 207 insertions(+), 2 deletions(-)
40 diff --quilt old/source/text.c new/source/text.c
41 --- old/source/text.c
42 +++ new/source/text.c
43 @@ -164,6 +164,8 @@ static void deleteToStartOfLineAP(Widget
44 Cardinal *nArgs);
45 static void deleteToEndOfLineAP(Widget w, XEvent *event, String *args,
46 Cardinal *nArgs);
47 +static Boolean forwardSkipSelect(Widget w, int insertPos);
48 +static Boolean backwardSkipSelect(Widget w, int insertPos);
49 static void forwardCharacterAP(Widget w, XEvent *event, String *args,
50 Cardinal *nArgs);
51 static void backwardCharacterAP(Widget w, XEvent *event, String *args,
52 @@ -172,6 +174,10 @@ static void forwardWordAP(Widget w, XEve
53 Cardinal *nArgs);
54 static void backwardWordAP(Widget w, XEvent *event, String *args,
55 Cardinal *nArgs);
56 +static void forwardSameAP(Widget w, XEvent *event, String *args,
57 + Cardinal *nArgs);
58 +static void backwardSameAP(Widget w, XEvent *event, String *args,
59 + Cardinal *nArgs);
60 static void forwardParagraphAP(Widget w, XEvent *event, String *args,
61 Cardinal *nArgs);
62 static void backwardParagraphAP(Widget w, XEvent *event, String *args,
63 @@ -216,6 +222,8 @@ static void focusOutAP(Widget w, XEvent
64 Cardinal *nArgs);
65 static void checkMoveSelectionChange(Widget w, XEvent *event, int startPos,
66 String *args, Cardinal *nArgs);
67 +static int getWidgetSelectionPos(Widget w, int *start, int *end, int *isRect,
68 + int *rectStart, int *rectEnd);
69 static void keyMoveExtendSelection(Widget w, XEvent *event, int startPos,
70 int rectangular);
71 static void checkAutoShowInsertPos(Widget w);
72 @@ -539,6 +547,10 @@ static XtActionsRec actionsList[] = {
73 {"forward_word", forwardWordAP},
74 {"backward-word", backwardWordAP},
75 {"backward_word", backwardWordAP},
76 + {"forward-same", forwardSameAP},
77 + {"forward_same", forwardSameAP},
78 + {"backward-same", backwardSameAP},
79 + {"backward_same", backwardSameAP},
80 {"forward-paragraph", forwardParagraphAP},
81 {"forward_paragraph", forwardParagraphAP},
82 {"backward-paragraph", backwardParagraphAP},
83 @@ -2641,14 +2653,40 @@ static void deleteToStartOfLineAP(Widget
84 callCursorMovementCBs(w, event);
87 +/* move insertPos to end of selection if in selection - return true if done */
88 +static Boolean forwardSkipSelect(Widget w, int insertPos)
90 + int start, end;
91 + if (getWidgetSelectionPos(w, &start, &end, NULL, NULL, NULL) &&
92 + start <= insertPos && insertPos <= end) {
93 + TextDSetInsertPosition(((TextWidget)w)->text.textD, end);
94 + return True;
95 + }
96 + return False;
99 +/* move insertPos to start of selection if in selection - return true if done */
100 +static Boolean backwardSkipSelect(Widget w, int insertPos)
102 + int start, end;
103 + if (getWidgetSelectionPos(w, &start, &end, NULL, NULL, NULL) &&
104 + start <= insertPos && insertPos <= end) {
105 + TextDSetInsertPosition(((TextWidget)w)->text.textD, start);
106 + return True;
108 + return False;
111 static void forwardCharacterAP(Widget w, XEvent *event, String *args,
112 Cardinal *nArgs)
114 int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
115 int silent = hasKey("nobell", args, nArgs);
116 + int skipsel = hasKey("skipselect", args, nArgs);
118 cancelDrag(w);
119 - if (!TextDMoveRight(((TextWidget)w)->text.textD)) {
120 + if (!(skipsel && forwardSkipSelect(w, insertPos)) &&
121 + !TextDMoveRight(((TextWidget)w)->text.textD)) {
122 ringIfNecessary(silent, w);
124 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
125 @@ -2661,9 +2699,11 @@ static void backwardCharacterAP(Widget w
127 int insertPos = TextDGetInsertPosition(((TextWidget)w)->text.textD);
128 int silent = hasKey("nobell", args, nArgs);
129 + int skipsel = hasKey("skipselect", args, nArgs);
131 cancelDrag(w);
132 - if (!TextDMoveLeft(((TextWidget)w)->text.textD)) {
133 + if (!(skipsel && backwardSkipSelect(w, insertPos)) &&
134 + !TextDMoveLeft(((TextWidget)w)->text.textD)) {
135 ringIfNecessary(silent, w);
137 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
138 @@ -2751,6 +2791,154 @@ static void backwardWordAP(Widget w, XEv
139 callCursorMovementCBs(w, event);
142 +static char *sameCharClassMap(char map[256], char *delimiters)
144 + static char spaces[] = " \t";
145 + static char classMap[256];
146 + static int done = 0;
147 + static int spaceClass = 0, nlClass = 0, delimClass = 0;
148 + int i;
149 + if (!done) {
150 + /* default value */
151 + for (i = 0; i < sizeof classMap; i++)
152 + classMap[i] = done; /* zero */
154 + /* letters, digits, _ */
155 + ++done;
156 + for (i = 0; i < sizeof classMap; i++)
157 + if (isalnum(i) || i == '_')
158 + classMap[i] = done;
160 + /* other graphic characters */
161 + ++done;
162 + for (i = 0; i < sizeof classMap; i++)
163 + if (isgraph(i) && !classMap[i])
164 + classMap[i] = done;
166 + /* control characters */
167 + ++done;
168 + for (i = 0; i < sizeof classMap; i++)
169 + if (iscntrl(i) && !classMap[i])
170 + classMap[i] = done;
172 + /* other ascii characters */
173 + ++done;
174 + for (i = 0; i < sizeof classMap; i++)
175 + if (isascii(i) && !classMap[i])
176 + classMap[i] = done;
178 + /* NEdit's spaces */
179 + spaceClass = ++done;
180 + for (i = 0; spaces[i]; i++)
181 + classMap[spaces[i]] = done;
182 + /* and newline */
183 + nlClass = ++done;
184 + classMap['\n'] = done;
185 + /* and a class for delimiters */
186 + delimClass = ++done;
189 + /* current delimiters */
190 + memcpy(map, classMap, 256);
191 + for (i = 0; delimiters[i]; i++) {
192 + unsigned char ch = delimiters[i];
193 + int cl = classMap[ch];
194 + /* do NOT overwrite the NEdit spaces/newline classes */
195 + if (cl != spaceClass && cl != nlClass)
196 + map[ch] = delimClass;
198 + return map;
201 +static void forwardSameAP(Widget w, XEvent *event, String *args,
202 + Cardinal *nArgs)
204 + textDisp *textD = ((TextWidget)w)->text.textD;
205 + textBuffer *buf = textD->buffer;
206 + int pos, insertPos = TextDGetInsertPosition(textD);
207 + char *delimiters = ((TextWidget)w)->text.delimiters;
208 + int silent = hasKey("nobell", args, nArgs);
209 + int incBlanks = hasKey("skipblanks", args, nArgs);
210 + int skipsel = hasKey("skipselect", args, nArgs);
212 + char classMap[256];
213 + int chClass, spClass;
215 + cancelDrag(w);
216 + if (insertPos == buf->length) {
217 + ringIfNecessary(silent, w);
218 + return;
221 + if (!(skipsel && forwardSkipSelect(w, insertPos))) {
222 + pos = insertPos;
223 + /* pick up the class map and the current char class, move forward */
224 + sameCharClassMap(classMap, delimiters);
225 + chClass = classMap[(unsigned char)BufGetCharacter(buf, pos++)];
226 + spClass = incBlanks ? classMap[' '] : chClass;
228 + /* now skip over all characters from here of that class */
229 + for (; pos < buf->length; pos++) {
230 + if (classMap[(unsigned char)BufGetCharacter(buf, pos)] != chClass)
231 + break;
233 + /* and skip over all characters from there for trailing blanks */
234 + for (; spClass != chClass && pos < buf->length; pos++) {
235 + if (classMap[(unsigned char)BufGetCharacter(buf, pos)] != spClass)
236 + break;
238 + /* pos is position of first character not of the same class */
239 + TextDSetInsertPosition(textD, pos);
241 + checkMoveSelectionChange(w, event, insertPos, args, nArgs);
242 + checkAutoShowInsertPos(w);
243 + callCursorMovementCBs(w, event);
246 +static void backwardSameAP(Widget w, XEvent *event, String *args,
247 + Cardinal *nArgs)
249 + textDisp *textD = ((TextWidget)w)->text.textD;
250 + textBuffer *buf = textD->buffer;
251 + int pos, insertPos = TextDGetInsertPosition(textD);
252 + char *delimiters = ((TextWidget)w)->text.delimiters;
253 + int silent = hasKey("nobell", args, nArgs);
254 + int incBlanks = hasKey("skipblanks", args, nArgs);
255 + int skipsel = hasKey("skipselect", args, nArgs);
257 + char classMap[256];
258 + int chClass, spClass;
259 + cancelDrag(w);
260 + if (insertPos == 0) {
261 + ringIfNecessary(silent, w);
262 + return;
265 + if (!(skipsel && backwardSkipSelect(w, insertPos))) {
266 + pos = insertPos - 1; /* we know insertPos != 0 */
267 + sameCharClassMap(classMap, delimiters); /* get class map */
268 + /* pick up the class of previous character */
269 + chClass = classMap[(unsigned char)BufGetCharacter(buf, pos)];
270 + spClass = incBlanks ? classMap[' '] : chClass;
272 + /* now skip back over all characters from here of that class */
273 + for (; pos > 0; --pos) {
274 + if (classMap[(unsigned char)BufGetCharacter(buf, pos-1)] != chClass)
275 + break;
277 + /* and skip back over all characters from there for leading blanks */
278 + for (; spClass != chClass && pos > 0; --pos) {
279 + if (classMap[(unsigned char)BufGetCharacter(buf, pos-1)] != spClass)
280 + break;
282 + /* pos is position of first character not of the same class */
283 + TextDSetInsertPosition(textD, pos);
285 + checkMoveSelectionChange(w, event, insertPos, args, nArgs);
286 + checkAutoShowInsertPos(w);
287 + callCursorMovementCBs(w, event);
290 static Boolean lineIsBlank(textBuffer *buf, int pos, int *start)
292 static const char whiteChars[] = " \t";
293 @@ -3589,6 +3777,23 @@ static void checkMoveSelectionChange(Wid
297 +** Fish out current selection information, returning true if there is one.
299 +static int getWidgetSelectionPos(Widget w, int *start, int *end, int *isRect,
300 + int *rectStart, int *rectEnd)
302 + int dummy;
303 + if (!isRect)
304 + isRect = &dummy;
305 + if (!rectStart)
306 + rectStart = &dummy;
307 + if (!rectEnd)
308 + rectEnd = &dummy;
309 + return BufGetSelectionPos((((TextWidget)w)->text.textD)->buffer,
310 + start, end, isRect, rectStart, rectEnd);
314 ** If a selection change was requested via a keyboard command for moving
315 ** the insertion cursor (usually with the "extend" keyword), adjust the
316 ** selection to include the new cursor position, or begin a new selection