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