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