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