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 @@ -222,14 +222,14 @@ static void simpleInsertAtCursor(Widget
15 int allowPendingDelete);
16 static int pendingSelection(Widget w);
17 static int deletePendingSelection(Widget w, XEvent *event);
18 static int deleteEmulatedTab(Widget w, XEvent *event);
19 static void selectWord(Widget w, int pointerX);
20 -static int spanForward(textBuffer *buf, int startPos, char *searchChars,
21 - int ignoreSpace, int *foundPos);
22 -static int spanBackward(textBuffer *buf, int startPos, char *searchChars, int
23 - ignoreSpace, int *foundPos);
24 +static int spanForward(textBuffer *buf, int startPos, const char *searchChars,
25 + int ignoreSpace, int *foundPos);
26 +static int spanBackward(textBuffer *buf, int startPos, const char *searchChars,
27 + int ignoreSpace, int *foundPos);
28 static void selectLine(Widget w);
29 static int startOfWord(TextWidget w, int pos);
30 static int endOfWord(TextWidget w, int pos);
31 static void checkAutoScroll(TextWidget w, int x, int y);
32 static void endDrag(Widget w);
33 @@ -2713,69 +2713,133 @@ static void backwardWordAP(Widget w, XEv
34 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
35 checkAutoShowInsertPos(w);
36 callCursorMovementCBs(w, event);
39 +static Boolean lineIsBlank(textBuffer *buf, int pos, int *start)
41 + static const char whiteChars[] = " \t";
44 + pos = BufStartOfLine(buf, pos);
47 + while (pos < buf->length) {
48 + c = BufGetCharacter(buf, pos);
50 + /* we reached the end of a blank line */
55 + /* check for any blank character */
56 + if (NULL != strchr(whiteChars, c)) {
59 + /* this line is a not a blank line */
64 + /* no end of line and no non blank character */
68 +static Boolean nextLineIsBlank(textBuffer *buf, int pos, int *next)
70 + /* get start of next line */
71 + pos = min(BufEndOfLine(buf, pos) + 1, buf->length);
73 + if (pos == buf->length) {
74 + /* buffer without last end-of-line */
78 + return lineIsBlank(buf, pos, next);
82 +static Boolean prevLineIsBlank(textBuffer *buf, int pos, int *prev)
84 + /* get end of prev line */
85 + pos = max(BufStartOfLine(buf, pos) - 1, 0);
87 + return lineIsBlank(buf, pos, prev);
90 static void forwardParagraphAP(Widget w, XEvent *event, String *args,
94 textDisp *textD = ((TextWidget)w)->text.textD;
95 int pos, insertPos = TextDGetInsertPosition(textD);
96 textBuffer *buf = textD->buffer;
98 - static char whiteChars[] = " \t";
99 int silent = hasKey("nobell", args, nArgs);
103 + /* check if we are at the end of the file, than return */
104 if (insertPos == buf->length) {
105 ringIfNecessary(silent, w);
109 - pos = min(BufEndOfLine(buf, insertPos)+1, buf->length);
111 + /* need to skip current paragraph? */
112 + if (!lineIsBlank(buf, insertPos, &pos)) {
113 + while (pos < buf->length) {
114 + if (nextLineIsBlank(buf, pos, &pos)) {
120 + /* skip all blank lines */
121 while (pos < buf->length) {
122 - c = BufGetCharacter(buf, pos);
125 - if (strchr(whiteChars, c) != NULL)
128 - pos = min(BufEndOfLine(buf, pos)+1, buf->length);
129 + if (!nextLineIsBlank(buf, pos, &pos)) {
133 - TextDSetInsertPosition(textD, min(pos+1, buf->length));
135 + TextDSetInsertPosition(textD, min(pos, buf->length));
136 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
137 checkAutoShowInsertPos(w);
138 callCursorMovementCBs(w, event);
141 static void backwardParagraphAP(Widget w, XEvent *event, String *args,
145 textDisp *textD = ((TextWidget)w)->text.textD;
146 int parStart, pos, insertPos = TextDGetInsertPosition(textD);
147 textBuffer *buf = textD->buffer;
149 - static char whiteChars[] = " \t";
150 int silent = hasKey("nobell", args, nArgs);
154 - if (insertPos == 0) {
156 + /* check if we are at the beginning of the file, than return */
157 + if (0 == insertPos) {
158 ringIfNecessary(silent, w);
162 - parStart = BufStartOfLine(buf, max(insertPos-1, 0));
163 - pos = max(parStart - 2, 0);
165 - c = BufGetCharacter(buf, pos);
168 - if (strchr(whiteChars, c) != NULL)
171 - parStart = BufStartOfLine(buf, pos);
172 - pos = max(parStart - 2, 0);
175 + if (lineIsBlank(buf, insertPos, &parStart)
176 + || (insertPos == parStart
177 + && prevLineIsBlank(buf, parStart, &parStart))) {
178 + while (parStart > 0) {
179 + if (!prevLineIsBlank(buf, parStart, &pos)) {
186 + while (parStart > 0) {
187 + if (prevLineIsBlank(buf, parStart, &pos)) {
193 TextDSetInsertPosition(textD, parStart);
194 checkMoveSelectionChange(w, event, insertPos, args, nArgs);
195 checkAutoShowInsertPos(w);
196 callCursorMovementCBs(w, event);
198 @@ -3695,15 +3759,15 @@ static int endOfWord(TextWidget w, int p
199 ** Search forwards in buffer "buf" for the first character NOT in
200 ** "searchChars", starting with the character "startPos", and returning the
201 ** result in "foundPos" returns True if found, False if not. If ignoreSpace
202 ** is set, then Space, Tab, and Newlines are ignored in searchChars.
204 -static int spanForward(textBuffer *buf, int startPos, char *searchChars,
205 - int ignoreSpace, int *foundPos)
206 +static int spanForward(textBuffer *buf, int startPos, const char *searchChars,
207 + int ignoreSpace, int *foundPos)
214 while (pos < buf->length) {
215 for (c=searchChars; *c!='\0'; c++)
216 if(!(ignoreSpace && (*c==' ' || *c=='\t' || *c=='\n')))
217 @@ -3723,15 +3787,15 @@ static int spanForward(textBuffer *buf,
218 ** Search backwards in buffer "buf" for the first character NOT in
219 ** "searchChars", starting with the character BEFORE "startPos", returning the
220 ** result in "foundPos" returns True if found, False if not. If ignoreSpace is
221 ** set, then Space, Tab, and Newlines are ignored in searchChars.
223 -static int spanBackward(textBuffer *buf, int startPos, char *searchChars, int
224 - ignoreSpace, int *foundPos)
225 +static int spanBackward(textBuffer *buf, int startPos, const char *searchChars,
226 + int ignoreSpace, int *foundPos)