re-fresh
[nedit-bw.git] / extended-paragraph.patch
blob38321e8c6226668c9d8a228bf01cad0c7e5e5a54
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.
6 ---
8 source/text.c | 146 +++++++++++++++++++++++++++++++++++++++++-----------------
9 1 file changed, 105 insertions(+), 41 deletions(-)
11 diff --quilt old/source/text.c new/source/text.c
12 --- old/source/text.c
13 +++ 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";
36 + char c;
38 + pos = BufStartOfLine(buf, pos);
39 + *start = pos;
41 + while (pos < buf->length) {
42 + c = BufGetCharacter(buf, pos);
44 + /* we reached the end of a blank line */
45 + if ('\n' == c) {
46 + return True;
47 + }
49 + /* check for any blank character */
50 + if (NULL != strchr(whiteChars, c)) {
51 + pos++;
52 + } else {
53 + /* this line is a not a blank line */
54 + return False;
55 + }
56 + }
58 + /* no end of line and no non blank character */
59 + return True;
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 */
69 + *next = pos;
70 + return False;
71 + } else {
72 + return lineIsBlank(buf, pos, next);
73 + }
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,
85 - Cardinal *nArgs)
86 + Cardinal *nArgs)
88 textDisp *textD = ((TextWidget)w)->text.textD;
89 int pos, insertPos = TextDGetInsertPosition(textD);
90 textBuffer *buf = textD->buffer;
91 - char c;
92 - static char whiteChars[] = " \t";
93 int silent = hasKey("nobell", args, nArgs);
95 cancelDrag(w);
97 + /* check if we are at the end of the file, than return */
98 if (insertPos == buf->length) {
99 ringIfNecessary(silent, w);
100 - return;
101 + return;
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)) {
109 + break;
114 + /* skip all blank lines */
115 while (pos < buf->length) {
116 - c = BufGetCharacter(buf, pos);
117 - if (c == '\n')
118 - break;
119 - if (strchr(whiteChars, c) != NULL)
120 - pos++;
121 - else
122 - pos = min(BufEndOfLine(buf, pos)+1, buf->length);
123 + if (!nextLineIsBlank(buf, pos, &pos)) {
124 + break;
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,
136 - Cardinal *nArgs)
137 + Cardinal *nArgs)
139 textDisp *textD = ((TextWidget)w)->text.textD;
140 int parStart, pos, insertPos = TextDGetInsertPosition(textD);
141 textBuffer *buf = textD->buffer;
142 - char c;
143 - static char whiteChars[] = " \t";
144 int silent = hasKey("nobell", args, nArgs);
147 cancelDrag(w);
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);
153 - return;
154 + return;
156 - parStart = BufStartOfLine(buf, max(insertPos-1, 0));
157 - pos = max(parStart - 2, 0);
158 - while (pos > 0) {
159 - c = BufGetCharacter(buf, pos);
160 - if (c == '\n')
161 - break;
162 - if (strchr(whiteChars, c) != NULL)
163 - pos--;
164 - else {
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)) {
174 + break;
176 + parStart = pos;
180 + while (parStart > 0) {
181 + if (prevLineIsBlank(buf, parStart, &pos)) {
182 + break;
184 + 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)
199 int pos;
200 - char *c;
201 + const char *c;
203 pos = startPos;
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)
214 int pos;
215 - char *c;
216 + const char *c;
218 if (startPos == 0) {
219 *foundPos = 0;