[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <unistd.h> |
---|
| 4 | #include <string.h> |
---|
| 5 | |
---|
[1aee7d9] | 6 | static const char fileIdent[] = "$Id$"; |
---|
| 7 | |
---|
[7d4fbcd] | 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; |
---|
[d36f2cb] | 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)); |
---|
[7d4fbcd] | 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; |
---|
[d36f2cb] | 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)); |
---|
[7d4fbcd] | 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 | |
---|
[d36f2cb] | 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 | |
---|
[7d4fbcd] | 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_to_endofline(owl_editwin *e) { |
---|
| 617 | int i; |
---|
| 618 | |
---|
| 619 | if (owl_editwin_get_numchars_on_line(e, e->buffy)>e->buffx) { |
---|
| 620 | /* normal line */ |
---|
| 621 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 622 | while(i < e->bufflen) { |
---|
| 623 | if (e->buff[i]!='\n') { |
---|
| 624 | owl_editwin_delete_char(e); |
---|
| 625 | } else if ((e->buff[i]=='\n') && (i==e->bufflen-1)) { |
---|
| 626 | owl_editwin_delete_char(e); |
---|
| 627 | } else { |
---|
| 628 | return; |
---|
| 629 | } |
---|
| 630 | } |
---|
| 631 | } else if (e->buffy+1 < owl_editwin_get_numlines(e)) { |
---|
| 632 | /* line with cursor at the end but not on very last line */ |
---|
| 633 | owl_editwin_key_right(e); |
---|
| 634 | owl_editwin_backspace(e); |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | void owl_editwin_move_to_line_end(owl_editwin *e) { |
---|
| 639 | e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | void owl_editwin_move_to_line_start(owl_editwin *e) { |
---|
| 643 | e->buffx=0; |
---|
| 644 | owl_editwin_adjust_for_locktext(e); |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | void owl_editwin_move_to_end(owl_editwin *e) { |
---|
| 648 | /* go to last char */ |
---|
| 649 | e->buffy=owl_editwin_get_numlines(e)-1; |
---|
| 650 | e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
| 651 | owl_editwin_key_right(e); |
---|
| 652 | |
---|
| 653 | /* do we need to scroll? */ |
---|
| 654 | if (e->buffy-e->topline > e->winlines) { |
---|
| 655 | e->topline+=e->winlines/2; |
---|
| 656 | } |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | void owl_editwin_move_to_top(owl_editwin *e) { |
---|
| 660 | _owl_editwin_set_xy_by_index(e, 0); |
---|
| 661 | |
---|
| 662 | /* do we need to scroll? */ |
---|
| 663 | e->topline=0; |
---|
| 664 | |
---|
| 665 | owl_editwin_adjust_for_locktext(e); |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | void owl_editwin_fill_paragraph(owl_editwin *e) { |
---|
| 669 | int i, save; |
---|
| 670 | |
---|
| 671 | /* save our starting point */ |
---|
| 672 | save=_owl_editwin_get_index_from_xy(e); |
---|
| 673 | |
---|
| 674 | /* scan back to the beginning of this paragraph */ |
---|
| 675 | for (i=save; i>=e->lock; i--) { |
---|
| 676 | if ( (i<=e->lock) || |
---|
| 677 | ((e->buff[i]=='\n') && (e->buff[i-1]=='\n'))) { |
---|
| 678 | _owl_editwin_set_xy_by_index(e, i+1); |
---|
| 679 | break; |
---|
| 680 | } |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | /* main loop */ |
---|
| 684 | while (1) { |
---|
| 685 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 686 | |
---|
| 687 | /* bail if we hit the end of the buffer */ |
---|
| 688 | if (i>=e->bufflen) break; |
---|
| 689 | |
---|
| 690 | /* bail if we hit the end of the paragraph */ |
---|
| 691 | if (e->buff[i]=='\n' && e->buff[i+1]=='\n') break; |
---|
| 692 | |
---|
| 693 | /* if we've travelled too far, linewrap */ |
---|
| 694 | if ((e->buffx) >= e->fillcol) { |
---|
| 695 | _owl_editwin_linewrap_word(e); |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | /* did we hit the end of a line too soon? */ |
---|
| 699 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 700 | if (e->buff[i]=='\n' && e->buffx<e->fillcol-1) { |
---|
| 701 | /* ********* we need to make sure we don't pull in a word that's too long ***********/ |
---|
| 702 | e->buff[i]=' '; |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | /* fix spacing */ |
---|
| 706 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 707 | if (e->buff[i]==' ' && e->buff[i+1]==' ') { |
---|
| 708 | if (e->buff[i-1]=='.' || e->buff[i-1]=='!' || e->buff[i-1]=='?') { |
---|
| 709 | owl_editwin_key_right(e); |
---|
| 710 | } else { |
---|
| 711 | owl_editwin_delete_char(e); |
---|
| 712 | /* if we did this ahead of the save point, adjust it */ |
---|
| 713 | if (i<save) save--; |
---|
| 714 | } |
---|
| 715 | } else { |
---|
| 716 | owl_editwin_key_right(e); |
---|
| 717 | } |
---|
| 718 | |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | /* put cursor back at starting point */ |
---|
| 722 | _owl_editwin_set_xy_by_index(e, save); |
---|
| 723 | |
---|
| 724 | /* do we need to scroll? */ |
---|
| 725 | if (e->buffy-e->topline < 0) { |
---|
| 726 | e->topline-=e->winlines/2; |
---|
| 727 | } |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | int owl_editwin_check_dotsend(owl_editwin *e) { |
---|
| 731 | int i; |
---|
| 732 | |
---|
| 733 | if (!e->dotsend) return(0); |
---|
| 734 | for (i=e->bufflen-1; i>0; i--) { |
---|
| 735 | if (e->buff[i] == '.' |
---|
| 736 | && (e->buff[i-1] == '\n' || e->buff[i-1] == '\r') |
---|
| 737 | && (e->buff[i+1] == '\n' || e->buff[i+1] == '\r')) { |
---|
| 738 | e->bufflen = i; |
---|
| 739 | e->buff[i] = '\0'; |
---|
| 740 | return(1); |
---|
| 741 | } |
---|
| 742 | if (e->buff[i] != '\r' |
---|
| 743 | && e->buff[i] != '\n' |
---|
| 744 | && e->buff[i] != ' ') { |
---|
| 745 | return(0); |
---|
| 746 | } |
---|
| 747 | } |
---|
| 748 | return(0); |
---|
| 749 | |
---|
| 750 | #if 0 /* old implementation */ |
---|
| 751 | if (e->bufflen>=2) { |
---|
| 752 | if ((e->buff[e->bufflen-1]=='\n') && |
---|
| 753 | (e->buff[e->bufflen-2]=='.') && |
---|
| 754 | ((e->bufflen==2) || (e->buff[e->bufflen-3]=='\n'))) { |
---|
| 755 | e->buff[e->bufflen-2]='\0'; |
---|
| 756 | e->bufflen-=2; |
---|
| 757 | owl_editwin_redisplay(e, 0); |
---|
| 758 | return(1); |
---|
| 759 | } |
---|
| 760 | } |
---|
| 761 | return(0); |
---|
| 762 | #endif |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | void owl_editwin_post_process_char(owl_editwin *e, int j) { |
---|
| 766 | /* check if we need to scroll down */ |
---|
| 767 | if (e->buffy-e->topline >= e->winlines) { |
---|
| 768 | e->topline+=e->winlines/2; |
---|
| 769 | } |
---|
| 770 | if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) { |
---|
| 771 | owl_command_editmulti_done(e); |
---|
| 772 | return; |
---|
| 773 | } |
---|
| 774 | owl_editwin_redisplay(e, 0); |
---|
| 775 | } |
---|
| 776 | |
---|
| 777 | void owl_editwin_process_char(owl_editwin *e, int j) { |
---|
| 778 | if (j == ERR) return; |
---|
| 779 | if (j>127 || ((j<32) && (j!=10) && (j!=13))) { |
---|
| 780 | return; |
---|
| 781 | } else { |
---|
| 782 | owl_editwin_insert_char(e, j); |
---|
| 783 | } |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | char *owl_editwin_get_text(owl_editwin *e) { |
---|
| 787 | return(e->buff+e->lock); |
---|
| 788 | } |
---|
| 789 | |
---|
| 790 | int owl_editwin_get_numchars_on_line(owl_editwin *e, int line) { |
---|
| 791 | int i; |
---|
| 792 | char *ptr1, *ptr2; |
---|
| 793 | |
---|
| 794 | if (e->bufflen==0) return(0); |
---|
| 795 | |
---|
| 796 | /* first go to the yth line */ |
---|
| 797 | ptr1=e->buff; |
---|
| 798 | for (i=0; i<line; i++) { |
---|
| 799 | ptr2=strchr(ptr1, '\n'); |
---|
| 800 | if (!ptr2) { |
---|
| 801 | /* we're already on the last line */ |
---|
| 802 | return(0); |
---|
| 803 | } |
---|
| 804 | ptr1=ptr2+1; |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | /* now go to the xth character */ |
---|
| 808 | ptr2=strchr(ptr1, '\n'); |
---|
| 809 | if (!ptr2) { |
---|
| 810 | return(e->buff + e->bufflen - ptr1); |
---|
| 811 | } |
---|
| 812 | return(ptr2-ptr1); /* don't count the newline for now */ |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | int owl_editwin_get_numlines(owl_editwin *e) { |
---|
| 816 | return(owl_text_num_lines(e->buff)); |
---|
| 817 | } |
---|
| 818 | |
---|