1 Subject: rewrite paragraph jumping code
3 This uses a state machine like code for the paragraph jumping code.
4 This makes it simplier to extend it with more versatility.
8 source/text.c | 146 +++++++++++++++++++++++++++++++++++++++++-----------------
9 1 file changed, 105 insertions(+), 41 deletions(-)
11 diff --quilt old/source/text.c new/source/text.c
14 @@ -224,10 +224,10 @@ static int pendingSelection(Widget w);
15 static int deletePendingSelection(Widget w, XEvent *event);
16 static int deleteEmulatedTab(Widget w, XEvent *event);
17 static void selectWord(Widget w, int pointerX);
18 -static int spanForward(textBuffer *buf, int startPos, char *searchChars,
19 - int ignoreSpace, int *foundPos);
20 -static int spanBackward(textBuffer *buf, int startPos, char *searchChars, int
21 - ignoreSpace, int *foundPos);
22 +static int spanForward(textBuffer *buf, int startPos, const char *searchChars,
23 + int ignoreSpace, int *foundPos);
24 +static int spanBackward(textBuffer *buf, int startPos, const char *searchChars,
25 + int ignoreSpace, int *foundPos);
26 static void selectLine(Widget w);
27 static int startOfWord(TextWidget w, int pos);
28 static int endOfWord(TextWidget w, int pos);
29 @@ -2715,65 +2715,129 @@ static void backwardWordAP(Widget w, XEv
30 callCursorMovementCBs(w, event);
33 +static Boolean lineIsBlank(textBuffer *buf, int pos, int *start)
35 + static const char whiteChars[] = " \t";
38 + pos = BufStartOfLine(buf, pos);
41 + while (pos < buf->length) {
42 + c = BufGetCharacter(buf, pos);
44 + /* we reached the end of a blank line */
49 + /* check for any blank character */
50 + if (NULL != strchr(whiteChars, c)) {
53 + /* this line is a not a blank line */
58 + /* no end of line and no non blank character */
62 +static Boolean nextLineIsBlank(textBuffer *buf, int pos, int *next)
64 + /* get start of next line */
65 + pos = min(BufEndOfLine(buf, pos) + 1, buf->length);
67 + if (pos == buf->length) {
68 + /* buffer without last end-of-line */
72 + return lineIsBlank(buf, pos, next);
76 +static Boolean prevLineIsBlank(textBuffer *buf, int pos, int *prev)
78 + /* get end of prev line */
79 + pos = max(BufStartOfLine(buf, pos) - 1, 0);
81 + return lineIsBlank(buf, pos, prev);
84 static void forwardParagraphAP(Widget w, XEvent *event, String *args,
88 textDisp *textD = ((TextWidget)w)->text.textD;
89 int pos, insertPos = TextDGetInsertPosition(textD);
90 textBuffer *buf = textD->buffer;
92 - static char whiteChars[] = " \t";
93 int silent = hasKey("nobell", args, nArgs);
97 + /* check if we are at the end of the file, than return */
98 if (insertPos == buf->length) {
99 ringIfNecessary(silent, w);
103 - pos = min(BufEndOfLine(buf, insertPos)+1, buf->length);
105 + /* need to skip current paragraph? */
106 + if (!lineIsBlank(buf, insertPos, &pos)) {
107 + while (pos < buf->length) {
108 + if (nextLineIsBlank(buf, pos, &pos)) {
114 + /* skip all blank lines */
115 while (pos < buf->length) {
116 - c = BufGetCharacter(buf, pos);
119 - if (strchr(whiteChars, c) != NULL)
122 - pos = min(BufEndOfLine(buf, pos)+1, buf->length);
123 + if (!nextLineIsBlank(buf, pos, &pos)) {
127 - TextDSetInsertPosition(textD, min(pos+1, buf->length));
129 + TextDSetInsertPosition(textD, min(pos, buf->length));
130 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
131 checkAutoShowInsertPos(w);
132 callCursorMovementCBs(w, event);
135 static void backwardParagraphAP(Widget w, XEvent *event, String *args,
139 textDisp *textD = ((TextWidget)w)->text.textD;
140 int parStart, pos, insertPos = TextDGetInsertPosition(textD);
141 textBuffer *buf = textD->buffer;
143 - static char whiteChars[] = " \t";
144 int silent = hasKey("nobell", args, nArgs);
148 - if (insertPos == 0) {
150 + /* check if we are at the beginning of the file, than return */
151 + if (0 == insertPos) {
152 ringIfNecessary(silent, w);
156 - parStart = BufStartOfLine(buf, max(insertPos-1, 0));
157 - pos = max(parStart - 2, 0);
159 - c = BufGetCharacter(buf, pos);
162 - if (strchr(whiteChars, c) != NULL)
165 - parStart = BufStartOfLine(buf, pos);
166 - pos = max(parStart - 2, 0);
169 + if (lineIsBlank(buf, insertPos, &parStart)
170 + || (insertPos == parStart
171 + && prevLineIsBlank(buf, parStart, &parStart))) {
172 + while (parStart > 0) {
173 + if (!prevLineIsBlank(buf, parStart, &pos)) {
180 + while (parStart > 0) {
181 + if (prevLineIsBlank(buf, parStart, &pos)) {
187 TextDSetInsertPosition(textD, parStart);
188 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
189 checkAutoShowInsertPos(w);
190 @@ -3697,11 +3761,11 @@ static int endOfWord(TextWidget w, int p
191 ** result in "foundPos" returns True if found, False if not. If ignoreSpace
192 ** is set, then Space, Tab, and Newlines are ignored in searchChars.
194 -static int spanForward(textBuffer *buf, int startPos, char *searchChars,
195 - int ignoreSpace, int *foundPos)
196 +static int spanForward(textBuffer *buf, int startPos, const char *searchChars,
197 + int ignoreSpace, int *foundPos)
204 while (pos < buf->length) {
205 @@ -3725,11 +3789,11 @@ static int spanForward(textBuffer *buf,
206 ** result in "foundPos" returns True if found, False if not. If ignoreSpace is
207 ** set, then Space, Tab, and Newlines are ignored in searchChars.
209 -static int spanBackward(textBuffer *buf, int startPos, char *searchChars, int
210 - ignoreSpace, int *foundPos)
211 +static int spanBackward(textBuffer *buf, int startPos, const char *searchChars,
212 + int ignoreSpace, int *foundPos)