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