1 | #include "owl.h" |
---|
2 | #include <stdlib.h> |
---|
3 | #include <unistd.h> |
---|
4 | #include <string.h> |
---|
5 | |
---|
6 | static const char fileIdent[] = "$Id$"; |
---|
7 | |
---|
8 | #define INCR 5000 |
---|
9 | |
---|
10 | void owl_editwin_init(owl_editwin *e, WINDOW *win, int winlines, int wincols, int style) { |
---|
11 | /* initialize the editwin e. |
---|
12 | * 'win' is an already initialzed curses window that will be used by editwin |
---|
13 | */ |
---|
14 | e->buff=owl_malloc(INCR); |
---|
15 | e->buff[0]='\0'; |
---|
16 | e->bufflen=0; |
---|
17 | e->allocated=INCR; |
---|
18 | e->buffx=0; |
---|
19 | e->buffy=0; |
---|
20 | e->topline=0; |
---|
21 | e->winlines=winlines; |
---|
22 | e->wincols=wincols; |
---|
23 | e->fillcol=owl_editwin_limit_maxcols(wincols-1, |
---|
24 | owl_global_get_edit_maxfillcols(&g)); |
---|
25 | e->wrapcol=owl_editwin_limit_maxcols(wincols-1, |
---|
26 | owl_global_get_edit_maxwrapcols(&g)); |
---|
27 | e->curswin=win; |
---|
28 | e->style=style; |
---|
29 | if ((style!=OWL_EDITWIN_STYLE_MULTILINE) && |
---|
30 | (style!=OWL_EDITWIN_STYLE_ONELINE)) { |
---|
31 | e->style=OWL_EDITWIN_STYLE_MULTILINE; |
---|
32 | } |
---|
33 | e->lock=0; |
---|
34 | e->dotsend=0; |
---|
35 | if (win) werase(win); |
---|
36 | } |
---|
37 | |
---|
38 | void owl_editwin_set_curswin(owl_editwin *e, WINDOW *w, int winlines, int wincols) { |
---|
39 | e->curswin=w; |
---|
40 | e->winlines=winlines; |
---|
41 | e->wincols=wincols; |
---|
42 | e->fillcol=owl_editwin_limit_maxcols(wincols-1, |
---|
43 | owl_global_get_edit_maxfillcols(&g)); |
---|
44 | e->wrapcol=owl_editwin_limit_maxcols(wincols-1, |
---|
45 | owl_global_get_edit_maxwrapcols(&g)); |
---|
46 | } |
---|
47 | |
---|
48 | WINDOW *owl_editwin_get_curswin(owl_editwin *e) { |
---|
49 | return e->curswin; |
---|
50 | } |
---|
51 | |
---|
52 | void owl_editwin_set_dotsend(owl_editwin *e) { |
---|
53 | e->dotsend=1; |
---|
54 | } |
---|
55 | |
---|
56 | int owl_editwin_limit_maxcols(int v, int maxv) { |
---|
57 | if (maxv > 5 && v > maxv) { |
---|
58 | return maxv; |
---|
59 | } else { |
---|
60 | return v; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | void owl_editwin_set_locktext(owl_editwin *e, char *text) { |
---|
65 | /* set text to be 'locked in' at the beginning of the buffer, any |
---|
66 | previous text (including locked text) will be overwritten */ |
---|
67 | |
---|
68 | int x, y; |
---|
69 | |
---|
70 | x=e->buffx; |
---|
71 | y=e->buffy; |
---|
72 | e->buffx=0; |
---|
73 | e->buffy=0; |
---|
74 | owl_editwin_overwrite_string(e, text); |
---|
75 | e->lock=strlen(text); |
---|
76 | /* if (text[e->lock-1]=='\n') e->lock--; */ |
---|
77 | e->buffx=x; |
---|
78 | e->buffy=y; |
---|
79 | owl_editwin_adjust_for_locktext(e); |
---|
80 | owl_editwin_redisplay(e, 0); |
---|
81 | } |
---|
82 | |
---|
83 | int owl_editwin_get_style(owl_editwin *e) { |
---|
84 | return(e->style); |
---|
85 | } |
---|
86 | |
---|
87 | void owl_editwin_new_style(owl_editwin *e, int newstyle) { |
---|
88 | char *ptr; |
---|
89 | |
---|
90 | if (e->style==newstyle) return; |
---|
91 | |
---|
92 | if (newstyle==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
93 | e->style=newstyle; |
---|
94 | } else if (newstyle==OWL_EDITWIN_STYLE_ONELINE) { |
---|
95 | e->style=newstyle; |
---|
96 | |
---|
97 | /* nuke everything after the first line */ |
---|
98 | if (e->bufflen > 0) { |
---|
99 | ptr=strchr(e->buff, '\n')-1; |
---|
100 | if (ptr) { |
---|
101 | e->bufflen=ptr - e->buff; |
---|
102 | e->buff[e->bufflen]='\0'; |
---|
103 | e->buffx=0; |
---|
104 | e->buffy=0; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | void owl_editwin_fullclear(owl_editwin *e) { |
---|
111 | /* completly reinitialize the buffer */ |
---|
112 | owl_free(e->buff); |
---|
113 | owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style); |
---|
114 | } |
---|
115 | |
---|
116 | void owl_editwin_clear(owl_editwin *e) { |
---|
117 | /* clear all text except for locktext and put the cursor at the beginning */ |
---|
118 | int lock; |
---|
119 | char *locktext=NULL; |
---|
120 | |
---|
121 | lock=0; |
---|
122 | if (e->lock > 0) { |
---|
123 | lock=1; |
---|
124 | |
---|
125 | locktext=owl_malloc(e->lock+20); |
---|
126 | strncpy(locktext, e->buff, e->lock); |
---|
127 | locktext[e->lock]='\0'; |
---|
128 | } |
---|
129 | |
---|
130 | owl_free(e->buff); |
---|
131 | owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style); |
---|
132 | |
---|
133 | if (lock > 0) { |
---|
134 | owl_editwin_set_locktext(e, locktext); |
---|
135 | } |
---|
136 | |
---|
137 | if (locktext) owl_free(locktext); |
---|
138 | owl_editwin_adjust_for_locktext(e); |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | void _owl_editwin_addspace(owl_editwin *e) { |
---|
143 | /* malloc more space for the buffer */ |
---|
144 | e->buff=owl_realloc(e->buff, e->allocated+INCR); |
---|
145 | if (!e->buff) { |
---|
146 | /* error */ |
---|
147 | return; |
---|
148 | } |
---|
149 | e->allocated+=INCR; |
---|
150 | } |
---|
151 | |
---|
152 | void owl_editwin_recenter(owl_editwin *e) { |
---|
153 | e->topline=e->buffy-(e->winlines/2); |
---|
154 | if (e->topline<0) e->topline=0; |
---|
155 | if (e->topline>owl_editwin_get_numlines(e)) e->topline=owl_editwin_get_numlines(e); |
---|
156 | } |
---|
157 | |
---|
158 | void owl_editwin_redisplay(owl_editwin *e, int update) { |
---|
159 | /* regenerate the text on the curses window */ |
---|
160 | /* if update == 1 then do a doupdate(), otherwise do not */ |
---|
161 | |
---|
162 | char *ptr1, *ptr2, *ptr3, *buff; |
---|
163 | int i; |
---|
164 | |
---|
165 | werase(e->curswin); |
---|
166 | wmove(e->curswin, 0, 0); |
---|
167 | |
---|
168 | /* start at topline */ |
---|
169 | ptr1=e->buff; |
---|
170 | for (i=0; i<e->topline; i++) { |
---|
171 | ptr2=strchr(ptr1, '\n'); |
---|
172 | if (!ptr2) { |
---|
173 | /* we're already on the last line */ |
---|
174 | break; |
---|
175 | } |
---|
176 | ptr1=ptr2+1; |
---|
177 | } |
---|
178 | /* ptr1 now stores the starting point */ |
---|
179 | |
---|
180 | /* find the ending point and store it in ptr3 */ |
---|
181 | ptr2=ptr1; |
---|
182 | ptr3=ptr1; |
---|
183 | for (i=0; i<e->winlines; i++) { |
---|
184 | ptr3=strchr(ptr2, '\n'); |
---|
185 | if (!ptr3) { |
---|
186 | /* we've hit the last line */ |
---|
187 | /* print everything to the end */ |
---|
188 | ptr3=e->buff+e->bufflen-1; |
---|
189 | ptr3--; |
---|
190 | break; |
---|
191 | } |
---|
192 | ptr2=ptr3+1; |
---|
193 | } |
---|
194 | ptr3+=2; |
---|
195 | |
---|
196 | buff=owl_malloc(ptr3-ptr1+50); |
---|
197 | strncpy(buff, ptr1, ptr3-ptr1); |
---|
198 | buff[ptr3-ptr1]='\0'; |
---|
199 | waddstr(e->curswin, buff); |
---|
200 | wmove(e->curswin, e->buffy-e->topline, e->buffx); |
---|
201 | wnoutrefresh(e->curswin); |
---|
202 | if (update==1) { |
---|
203 | doupdate(); |
---|
204 | } |
---|
205 | owl_free(buff); |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | int _owl_editwin_linewrap_word(owl_editwin *e) { |
---|
210 | /* linewrap the word just before the cursor. |
---|
211 | * returns 0 on success |
---|
212 | * returns -1 if we could not wrap. |
---|
213 | */ |
---|
214 | |
---|
215 | int i, z; |
---|
216 | |
---|
217 | z=_owl_editwin_get_index_from_xy(e); |
---|
218 | /* move back and line wrap the previous word */ |
---|
219 | for (i=z-1; ; i--) { |
---|
220 | /* move back until you find a space or hit the beginning of the line */ |
---|
221 | if (e->buff[i]==' ') { |
---|
222 | /* replace the space with a newline */ |
---|
223 | e->buff[i]='\n'; |
---|
224 | e->buffy++; |
---|
225 | e->buffx=z-i-1; |
---|
226 | /* were we on the last line */ |
---|
227 | return(0); |
---|
228 | } else if (e->buff[i]=='\n' || i<=e->lock) { |
---|
229 | /* we hit the begginning of the line or the buffer, we cannot |
---|
230 | * wrap. |
---|
231 | */ |
---|
232 | return(-1); |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | void owl_editwin_insert_char(owl_editwin *e, char c) { |
---|
238 | /* insert a character at the current point (shift later |
---|
239 | * characters over) */ |
---|
240 | |
---|
241 | int z, i, ret; |
---|
242 | |
---|
243 | /* \r is \n */ |
---|
244 | if (c=='\r') { |
---|
245 | c='\n'; |
---|
246 | } |
---|
247 | |
---|
248 | if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) { |
---|
249 | /* perhaps later this will change some state that allows the string |
---|
250 | to be read */ |
---|
251 | return; |
---|
252 | } |
---|
253 | |
---|
254 | /* make sure there is enough memory for the new text */ |
---|
255 | if ((e->bufflen+1) > (e->allocated-5)) { |
---|
256 | _owl_editwin_addspace(e); |
---|
257 | } |
---|
258 | |
---|
259 | /* get the insertion point */ |
---|
260 | z=_owl_editwin_get_index_from_xy(e); |
---|
261 | |
---|
262 | /* If we're going to insert at the last column do word wrapping, unless it's a \n */ |
---|
263 | if ((e->buffx+1==e->wrapcol) && (c!='\n')) { |
---|
264 | ret=_owl_editwin_linewrap_word(e); |
---|
265 | if (ret==-1) { |
---|
266 | /* we couldn't wrap, insert a hard newline instead */ |
---|
267 | owl_editwin_insert_char(e, '\n'); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | z=_owl_editwin_get_index_from_xy(e); |
---|
272 | /* shift all the other characters right */ |
---|
273 | for (i=e->bufflen; i>z; i--) { |
---|
274 | e->buff[i]=e->buff[i-1]; |
---|
275 | } |
---|
276 | |
---|
277 | /* insert the new one */ |
---|
278 | e->buff[z]=c; |
---|
279 | |
---|
280 | /* housekeeping */ |
---|
281 | e->bufflen++; |
---|
282 | e->buff[e->bufflen]='\0'; |
---|
283 | |
---|
284 | /* advance the cursor */ |
---|
285 | if (c=='\n') { |
---|
286 | e->buffx=0; |
---|
287 | e->buffy++; |
---|
288 | } else { |
---|
289 | e->buffx++; |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | void owl_editwin_overwrite_char(owl_editwin *e, char c) { |
---|
294 | /* overwrite the character at the current point with 'c' */ |
---|
295 | int z; |
---|
296 | |
---|
297 | /* \r is \n */ |
---|
298 | if (c=='\r') { |
---|
299 | c='\n'; |
---|
300 | } |
---|
301 | |
---|
302 | if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) { |
---|
303 | /* perhaps later this will change some state that allows the string |
---|
304 | to be read */ |
---|
305 | return; |
---|
306 | } |
---|
307 | |
---|
308 | z=_owl_editwin_get_index_from_xy(e); |
---|
309 | |
---|
310 | /* only if we are at the end of the buffer do we create new space */ |
---|
311 | if (z==e->bufflen) { |
---|
312 | if ((e->bufflen+1) > (e->allocated-5)) { |
---|
313 | _owl_editwin_addspace(e); |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | e->buff[z]=c; |
---|
318 | |
---|
319 | /* housekeeping if we are at the end of the buffer */ |
---|
320 | if (z==e->bufflen) { |
---|
321 | e->bufflen++; |
---|
322 | e->buff[e->bufflen]='\0'; |
---|
323 | } |
---|
324 | |
---|
325 | /* advance the cursor */ |
---|
326 | if (c=='\n') { |
---|
327 | e->buffx=0; |
---|
328 | e->buffy++; |
---|
329 | } else { |
---|
330 | e->buffx++; |
---|
331 | } |
---|
332 | |
---|
333 | } |
---|
334 | |
---|
335 | void owl_editwin_delete_char(owl_editwin *e) { |
---|
336 | /* delete the character at the current point, following chars |
---|
337 | * shift left. |
---|
338 | */ |
---|
339 | int z, i; |
---|
340 | |
---|
341 | if (e->bufflen==0) return; |
---|
342 | |
---|
343 | /* get the deletion point */ |
---|
344 | z=_owl_editwin_get_index_from_xy(e); |
---|
345 | |
---|
346 | if (z==e->bufflen) return; |
---|
347 | |
---|
348 | for (i=z; i<e->bufflen; i++) { |
---|
349 | e->buff[i]=e->buff[i+1]; |
---|
350 | } |
---|
351 | e->bufflen--; |
---|
352 | e->buff[e->bufflen]='\0'; |
---|
353 | } |
---|
354 | |
---|
355 | void owl_editwin_insert_string(owl_editwin *e, char *string) { |
---|
356 | /* insert 'string' at the current point, later text is shifted |
---|
357 | * right |
---|
358 | */ |
---|
359 | int i, j; |
---|
360 | |
---|
361 | j=strlen(string); |
---|
362 | for (i=0; i<j; i++) { |
---|
363 | owl_editwin_insert_char(e, string[i]); |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | void owl_editwin_overwrite_string(owl_editwin *e, char *string) { |
---|
368 | int i, j; |
---|
369 | /* write 'string' at the current point, overwriting text that is |
---|
370 | * already there |
---|
371 | */ |
---|
372 | |
---|
373 | j=strlen(string); |
---|
374 | for (i=0; i<j; i++) { |
---|
375 | owl_editwin_overwrite_char(e, string[i]); |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | int _owl_editwin_get_index_from_xy(owl_editwin *e) { |
---|
380 | /* get the index into e->buff for the current cursor |
---|
381 | * position. |
---|
382 | */ |
---|
383 | int i; |
---|
384 | char *ptr1, *ptr2; |
---|
385 | |
---|
386 | if (e->bufflen==0) return(0); |
---|
387 | |
---|
388 | /* first go to the yth line */ |
---|
389 | ptr1=e->buff; |
---|
390 | for (i=0; i<e->buffy; i++) { |
---|
391 | ptr2=strchr(ptr1, '\n'); |
---|
392 | if (!ptr2) { |
---|
393 | /* we're already on the last line */ |
---|
394 | break; |
---|
395 | } |
---|
396 | ptr1=ptr2+1; |
---|
397 | } |
---|
398 | |
---|
399 | /* now go to the xth character */ |
---|
400 | ptr2=strchr(ptr1, '\n'); |
---|
401 | if (!ptr2) { |
---|
402 | ptr2=e->buff+e->bufflen; |
---|
403 | } |
---|
404 | |
---|
405 | if ((ptr2-ptr1) < e->buffx) { |
---|
406 | ptr1=ptr2-1; |
---|
407 | } else { |
---|
408 | ptr1+=e->buffx; |
---|
409 | } |
---|
410 | |
---|
411 | /* printf("DEBUG: index is %i\r\n", ptr1-e->buff); */ |
---|
412 | return(ptr1-e->buff); |
---|
413 | } |
---|
414 | |
---|
415 | void _owl_editwin_set_xy_by_index(owl_editwin *e, int index) { |
---|
416 | int z, i; |
---|
417 | |
---|
418 | z=_owl_editwin_get_index_from_xy(e); |
---|
419 | if (index>z) { |
---|
420 | for (i=0; i<index-z; i++) { |
---|
421 | owl_editwin_key_right(e); |
---|
422 | } |
---|
423 | } else if (index<z) { |
---|
424 | for (i=0; i<z-index; i++) { |
---|
425 | owl_editwin_key_left(e); |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | void owl_editwin_adjust_for_locktext(owl_editwin *e) { |
---|
431 | /* if we happen to have the cursor over locked text |
---|
432 | * move it to be out of the locktext region */ |
---|
433 | if (_owl_editwin_get_index_from_xy(e)<e->lock) { |
---|
434 | _owl_editwin_set_xy_by_index(e, e->lock); |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | void owl_editwin_backspace(owl_editwin *e) { |
---|
439 | /* delete the char before the current one |
---|
440 | * and shift later chars left |
---|
441 | */ |
---|
442 | if (_owl_editwin_get_index_from_xy(e) > e->lock) { |
---|
443 | owl_editwin_key_left(e); |
---|
444 | owl_editwin_delete_char(e); |
---|
445 | } |
---|
446 | owl_editwin_adjust_for_locktext(e); |
---|
447 | } |
---|
448 | |
---|
449 | void owl_editwin_key_up(owl_editwin *e) { |
---|
450 | if (e->buffy > 0) e->buffy--; |
---|
451 | if (e->buffx >= owl_editwin_get_numchars_on_line(e, e->buffy)) { |
---|
452 | e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
453 | } |
---|
454 | |
---|
455 | /* do we need to scroll? */ |
---|
456 | if (e->buffy-e->topline < 0) { |
---|
457 | e->topline-=e->winlines/2; |
---|
458 | } |
---|
459 | |
---|
460 | owl_editwin_adjust_for_locktext(e); |
---|
461 | } |
---|
462 | |
---|
463 | void owl_editwin_key_down(owl_editwin *e) { |
---|
464 | /* move down if we can */ |
---|
465 | if (e->buffy+1 < owl_editwin_get_numlines(e)) e->buffy++; |
---|
466 | |
---|
467 | /* if we're past the last character move back */ |
---|
468 | if (e->buffx >= owl_editwin_get_numchars_on_line(e, e->buffy)) { |
---|
469 | e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
470 | } |
---|
471 | |
---|
472 | /* do we need to scroll? */ |
---|
473 | if (e->buffy-e->topline > e->winlines) { |
---|
474 | e->topline+=e->winlines/2; |
---|
475 | } |
---|
476 | |
---|
477 | /* adjust for locktext */ |
---|
478 | owl_editwin_adjust_for_locktext(e); |
---|
479 | } |
---|
480 | |
---|
481 | void owl_editwin_key_left(owl_editwin *e) { |
---|
482 | /* move left if we can, and maybe up a line */ |
---|
483 | if (e->buffx>0) { |
---|
484 | e->buffx--; |
---|
485 | } else if (e->buffy>0) { |
---|
486 | e->buffy--; |
---|
487 | e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
488 | } |
---|
489 | |
---|
490 | /* do we need to scroll up? */ |
---|
491 | if (e->buffy-e->topline < 0) { |
---|
492 | e->topline-=e->winlines/2; |
---|
493 | } |
---|
494 | |
---|
495 | /* make sure to avoid locktext */ |
---|
496 | owl_editwin_adjust_for_locktext(e); |
---|
497 | } |
---|
498 | |
---|
499 | void owl_editwin_key_right(owl_editwin *e) { |
---|
500 | int i; |
---|
501 | |
---|
502 | /* move right if we can, and skip down a line if needed */ |
---|
503 | i=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
504 | if (e->buffx < i) { |
---|
505 | e->buffx++; |
---|
506 | /* } else if (e->buffy+1 < owl_editwin_get_numlines(e)) { */ |
---|
507 | } else if (_owl_editwin_get_index_from_xy(e) < e->bufflen) { |
---|
508 | if (e->style==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
509 | e->buffx=0; |
---|
510 | e->buffy++; |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | /* do we need to scroll down? */ |
---|
515 | if (e->buffy-e->topline >= e->winlines) { |
---|
516 | e->topline+=e->winlines/2; |
---|
517 | } |
---|
518 | } |
---|
519 | |
---|
520 | void owl_editwin_move_to_nextword(owl_editwin *e) { |
---|
521 | int i, x; |
---|
522 | |
---|
523 | /* if we're starting on a space, find the first non-space */ |
---|
524 | i=_owl_editwin_get_index_from_xy(e); |
---|
525 | if (e->buff[i]==' ') { |
---|
526 | for (x=i; x<e->bufflen; x++) { |
---|
527 | if (e->buff[x]!=' ' && e->buff[x]!='\n') { |
---|
528 | _owl_editwin_set_xy_by_index(e, x); |
---|
529 | break; |
---|
530 | } |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | /* find the next space, newline or end of line and go there, if |
---|
535 | already at the end of the line, continue on to the next */ |
---|
536 | i=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
537 | if (e->buffx < i) { |
---|
538 | /* move right till end of line */ |
---|
539 | while (e->buffx < i) { |
---|
540 | e->buffx++; |
---|
541 | if (e->buff[_owl_editwin_get_index_from_xy(e)]==' ') return; |
---|
542 | if (e->buffx == i) return; |
---|
543 | } |
---|
544 | } else if (e->buffx == i) { |
---|
545 | /* try to move down */ |
---|
546 | if (e->style==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
547 | if (e->buffy+1 < owl_editwin_get_numlines(e)) { |
---|
548 | e->buffx=0; |
---|
549 | e->buffy++; |
---|
550 | owl_editwin_move_to_nextword(e); |
---|
551 | } |
---|
552 | } |
---|
553 | } |
---|
554 | } |
---|
555 | |
---|
556 | void owl_editwin_move_to_previousword(owl_editwin *e) { |
---|
557 | /* go backwards to the last non-space character */ |
---|
558 | int i, x; |
---|
559 | |
---|
560 | /* are we already at the beginning of the word? */ |
---|
561 | i=_owl_editwin_get_index_from_xy(e); |
---|
562 | if ( (e->buff[i]!=' ' && e->buff[i]!='\n' && e->buff[i]!='\0') && |
---|
563 | (e->buff[i-1]==' ' || e->buff[i-1]=='\n') ) { |
---|
564 | owl_editwin_key_left(e); |
---|
565 | } |
---|
566 | |
---|
567 | /* are we starting on a space character? */ |
---|
568 | i=_owl_editwin_get_index_from_xy(e); |
---|
569 | if (e->buff[i]==' ' || e->buff[i]=='\n' || e->buff[i]=='\0') { |
---|
570 | /* find the first non-space */ |
---|
571 | for (x=i; x>=e->lock; x--) { |
---|
572 | if (e->buff[x]!=' ' && e->buff[x]!='\n' && e->buff[x]!='\0') { |
---|
573 | _owl_editwin_set_xy_by_index(e, x); |
---|
574 | break; |
---|
575 | } |
---|
576 | } |
---|
577 | } |
---|
578 | |
---|
579 | /* find the last non-space */ |
---|
580 | i=_owl_editwin_get_index_from_xy(e); |
---|
581 | for (x=i; x>=e->lock; x--) { |
---|
582 | if (e->buff[x-1]==' ' || e->buff[x-1]=='\n') { |
---|
583 | _owl_editwin_set_xy_by_index(e, x); |
---|
584 | break; |
---|
585 | } |
---|
586 | } |
---|
587 | _owl_editwin_set_xy_by_index(e, x); |
---|
588 | } |
---|
589 | |
---|
590 | |
---|
591 | void owl_editwin_delete_nextword(owl_editwin *e) { |
---|
592 | int z; |
---|
593 | |
---|
594 | if (e->bufflen==0) return; |
---|
595 | |
---|
596 | /* if we start out on a space character then gobble all the spaces |
---|
597 | up first */ |
---|
598 | while (1) { |
---|
599 | z=_owl_editwin_get_index_from_xy(e); |
---|
600 | if (e->buff[z]==' ' || e->buff[z]=='\n') { |
---|
601 | owl_editwin_delete_char(e); |
---|
602 | } else { |
---|
603 | break; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | /* then nuke the next word */ |
---|
608 | while (1) { |
---|
609 | z=_owl_editwin_get_index_from_xy(e); |
---|
610 | if (e->buff[z+1]==' ' || e->buff[z+1]=='\n' || e->buff[z+1]=='\0') break; |
---|
611 | owl_editwin_delete_char(e); |
---|
612 | } |
---|
613 | owl_editwin_delete_char(e); |
---|
614 | } |
---|
615 | |
---|
616 | void owl_editwin_delete_previousword(owl_editwin *e) { |
---|
617 | /* go backwards to the last non-space character, then delete chars */ |
---|
618 | int i, startpos, endpos; |
---|
619 | |
---|
620 | startpos = _owl_editwin_get_index_from_xy(e); |
---|
621 | owl_editwin_move_to_previousword(e); |
---|
622 | endpos = _owl_editwin_get_index_from_xy(e); |
---|
623 | for (i=0; i<startpos-endpos; i++) { |
---|
624 | owl_editwin_delete_char(e); |
---|
625 | } |
---|
626 | } |
---|
627 | |
---|
628 | void owl_editwin_delete_to_endofline(owl_editwin *e) { |
---|
629 | int i; |
---|
630 | |
---|
631 | if (owl_editwin_get_numchars_on_line(e, e->buffy)>e->buffx) { |
---|
632 | /* normal line */ |
---|
633 | i=_owl_editwin_get_index_from_xy(e); |
---|
634 | while(i < e->bufflen) { |
---|
635 | if (e->buff[i]!='\n') { |
---|
636 | owl_editwin_delete_char(e); |
---|
637 | } else if ((e->buff[i]=='\n') && (i==e->bufflen-1)) { |
---|
638 | owl_editwin_delete_char(e); |
---|
639 | } else { |
---|
640 | return; |
---|
641 | } |
---|
642 | } |
---|
643 | } else if (e->buffy+1 < owl_editwin_get_numlines(e)) { |
---|
644 | /* line with cursor at the end but not on very last line */ |
---|
645 | owl_editwin_key_right(e); |
---|
646 | owl_editwin_backspace(e); |
---|
647 | } |
---|
648 | } |
---|
649 | |
---|
650 | void owl_editwin_move_to_line_end(owl_editwin *e) { |
---|
651 | e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
652 | } |
---|
653 | |
---|
654 | void owl_editwin_move_to_line_start(owl_editwin *e) { |
---|
655 | e->buffx=0; |
---|
656 | owl_editwin_adjust_for_locktext(e); |
---|
657 | } |
---|
658 | |
---|
659 | void owl_editwin_move_to_end(owl_editwin *e) { |
---|
660 | /* go to last char */ |
---|
661 | e->buffy=owl_editwin_get_numlines(e)-1; |
---|
662 | e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
663 | owl_editwin_key_right(e); |
---|
664 | |
---|
665 | /* do we need to scroll? */ |
---|
666 | if (e->buffy-e->topline > e->winlines) { |
---|
667 | e->topline+=e->winlines/2; |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | void owl_editwin_move_to_top(owl_editwin *e) { |
---|
672 | _owl_editwin_set_xy_by_index(e, 0); |
---|
673 | |
---|
674 | /* do we need to scroll? */ |
---|
675 | e->topline=0; |
---|
676 | |
---|
677 | owl_editwin_adjust_for_locktext(e); |
---|
678 | } |
---|
679 | |
---|
680 | void owl_editwin_fill_paragraph(owl_editwin *e) { |
---|
681 | int i, save; |
---|
682 | |
---|
683 | /* save our starting point */ |
---|
684 | save=_owl_editwin_get_index_from_xy(e); |
---|
685 | |
---|
686 | /* scan back to the beginning of this paragraph */ |
---|
687 | for (i=save; i>=e->lock; i--) { |
---|
688 | if ( (i<=e->lock) || |
---|
689 | ((e->buff[i]=='\n') && (e->buff[i-1]=='\n'))) { |
---|
690 | _owl_editwin_set_xy_by_index(e, i+1); |
---|
691 | break; |
---|
692 | } |
---|
693 | } |
---|
694 | |
---|
695 | /* main loop */ |
---|
696 | while (1) { |
---|
697 | i=_owl_editwin_get_index_from_xy(e); |
---|
698 | |
---|
699 | /* bail if we hit the end of the buffer */ |
---|
700 | if (i>=e->bufflen) break; |
---|
701 | |
---|
702 | /* bail if we hit the end of the paragraph */ |
---|
703 | if (e->buff[i]=='\n' && e->buff[i+1]=='\n') break; |
---|
704 | |
---|
705 | /* if we've travelled too far, linewrap */ |
---|
706 | if ((e->buffx) >= e->fillcol) { |
---|
707 | _owl_editwin_linewrap_word(e); |
---|
708 | } |
---|
709 | |
---|
710 | /* did we hit the end of a line too soon? */ |
---|
711 | i=_owl_editwin_get_index_from_xy(e); |
---|
712 | if (e->buff[i]=='\n' && e->buffx<e->fillcol-1) { |
---|
713 | /* ********* we need to make sure we don't pull in a word that's too long ***********/ |
---|
714 | e->buff[i]=' '; |
---|
715 | } |
---|
716 | |
---|
717 | /* fix spacing */ |
---|
718 | i=_owl_editwin_get_index_from_xy(e); |
---|
719 | if (e->buff[i]==' ' && e->buff[i+1]==' ') { |
---|
720 | if (e->buff[i-1]=='.' || e->buff[i-1]=='!' || e->buff[i-1]=='?') { |
---|
721 | owl_editwin_key_right(e); |
---|
722 | } else { |
---|
723 | owl_editwin_delete_char(e); |
---|
724 | /* if we did this ahead of the save point, adjust it */ |
---|
725 | if (i<save) save--; |
---|
726 | } |
---|
727 | } else { |
---|
728 | owl_editwin_key_right(e); |
---|
729 | } |
---|
730 | |
---|
731 | } |
---|
732 | |
---|
733 | /* put cursor back at starting point */ |
---|
734 | _owl_editwin_set_xy_by_index(e, save); |
---|
735 | |
---|
736 | /* do we need to scroll? */ |
---|
737 | if (e->buffy-e->topline < 0) { |
---|
738 | e->topline-=e->winlines/2; |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | /* returns if only whitespace remains */ |
---|
743 | int owl_editwin_is_at_end(owl_editwin *e) { |
---|
744 | int i, cur; |
---|
745 | cur=_owl_editwin_get_index_from_xy(e); |
---|
746 | if (cur >= e->bufflen) return(1); |
---|
747 | for (i=e->bufflen-1; i>cur; i--) { |
---|
748 | if (e->buff[i] != '\r' |
---|
749 | && e->buff[i] != '\n' |
---|
750 | && e->buff[i] != ' ') { |
---|
751 | return(0); |
---|
752 | } |
---|
753 | } |
---|
754 | if (cur==i) return(1); |
---|
755 | else return(0); |
---|
756 | } |
---|
757 | |
---|
758 | int owl_editwin_check_dotsend(owl_editwin *e) { |
---|
759 | int i; |
---|
760 | |
---|
761 | if (!e->dotsend) return(0); |
---|
762 | for (i=e->bufflen-1; i>0; i--) { |
---|
763 | if (e->buff[i] == '.' |
---|
764 | && (e->buff[i-1] == '\n' || e->buff[i-1] == '\r') |
---|
765 | && (e->buff[i+1] == '\n' || e->buff[i+1] == '\r')) { |
---|
766 | e->bufflen = i; |
---|
767 | e->buff[i] = '\0'; |
---|
768 | return(1); |
---|
769 | } |
---|
770 | if (e->buff[i] != '\r' |
---|
771 | && e->buff[i] != '\n' |
---|
772 | && e->buff[i] != ' ') { |
---|
773 | return(0); |
---|
774 | } |
---|
775 | } |
---|
776 | return(0); |
---|
777 | |
---|
778 | #if 0 /* old implementation */ |
---|
779 | if (e->bufflen>=2) { |
---|
780 | if ((e->buff[e->bufflen-1]=='\n') && |
---|
781 | (e->buff[e->bufflen-2]=='.') && |
---|
782 | ((e->bufflen==2) || (e->buff[e->bufflen-3]=='\n'))) { |
---|
783 | e->buff[e->bufflen-2]='\0'; |
---|
784 | e->bufflen-=2; |
---|
785 | owl_editwin_redisplay(e, 0); |
---|
786 | return(1); |
---|
787 | } |
---|
788 | } |
---|
789 | return(0); |
---|
790 | #endif |
---|
791 | } |
---|
792 | |
---|
793 | void owl_editwin_post_process_char(owl_editwin *e, int j) { |
---|
794 | /* check if we need to scroll down */ |
---|
795 | if (e->buffy-e->topline >= e->winlines) { |
---|
796 | e->topline+=e->winlines/2; |
---|
797 | } |
---|
798 | if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) { |
---|
799 | owl_command_editmulti_done(e); |
---|
800 | return; |
---|
801 | } |
---|
802 | owl_editwin_redisplay(e, 0); |
---|
803 | } |
---|
804 | |
---|
805 | void owl_editwin_process_char(owl_editwin *e, int j) { |
---|
806 | if (j == ERR) return; |
---|
807 | if (j>127 || ((j<32) && (j!=10) && (j!=13))) { |
---|
808 | return; |
---|
809 | } else { |
---|
810 | owl_editwin_insert_char(e, j); |
---|
811 | } |
---|
812 | } |
---|
813 | |
---|
814 | char *owl_editwin_get_text(owl_editwin *e) { |
---|
815 | return(e->buff+e->lock); |
---|
816 | } |
---|
817 | |
---|
818 | int owl_editwin_get_numchars_on_line(owl_editwin *e, int line) { |
---|
819 | int i; |
---|
820 | char *ptr1, *ptr2; |
---|
821 | |
---|
822 | if (e->bufflen==0) return(0); |
---|
823 | |
---|
824 | /* first go to the yth line */ |
---|
825 | ptr1=e->buff; |
---|
826 | for (i=0; i<line; i++) { |
---|
827 | ptr2=strchr(ptr1, '\n'); |
---|
828 | if (!ptr2) { |
---|
829 | /* we're already on the last line */ |
---|
830 | return(0); |
---|
831 | } |
---|
832 | ptr1=ptr2+1; |
---|
833 | } |
---|
834 | |
---|
835 | /* now go to the xth character */ |
---|
836 | ptr2=strchr(ptr1, '\n'); |
---|
837 | if (!ptr2) { |
---|
838 | return(e->buff + e->bufflen - ptr1); |
---|
839 | } |
---|
840 | return(ptr2-ptr1); /* don't count the newline for now */ |
---|
841 | } |
---|
842 | |
---|
843 | int owl_editwin_get_numlines(owl_editwin *e) { |
---|
844 | return(owl_text_num_lines(e->buff)); |
---|
845 | } |
---|
846 | |
---|