[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 | |
---|
[cf83b7a] | 11 | /* initialize the editwin e. |
---|
| 12 | * 'win' is an already initialzed curses window that will be used by editwin |
---|
| 13 | */ |
---|
[c9334b1] | 14 | void owl_editwin_init(owl_editwin *e, WINDOW *win, int winlines, int wincols, int style, owl_history *hist) |
---|
| 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; |
---|
[9a6cc40] | 26 | e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g)); |
---|
| 27 | e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g)); |
---|
[7d4fbcd] | 28 | e->curswin=win; |
---|
| 29 | e->style=style; |
---|
| 30 | if ((style!=OWL_EDITWIN_STYLE_MULTILINE) && |
---|
| 31 | (style!=OWL_EDITWIN_STYLE_ONELINE)) { |
---|
| 32 | e->style=OWL_EDITWIN_STYLE_MULTILINE; |
---|
| 33 | } |
---|
| 34 | e->lock=0; |
---|
| 35 | e->dotsend=0; |
---|
[c9334b1] | 36 | e->echochar='\0'; |
---|
[e74c01c] | 37 | |
---|
| 38 | /* |
---|
| 39 | // We get initialized multiple times, but we need to hold on to |
---|
| 40 | // the callbacks, so we can't NULL them here. |
---|
| 41 | e->command = NULL; |
---|
| 42 | e->callback = NULL; |
---|
| 43 | e->cbdata = NULL; |
---|
| 44 | */ |
---|
[7d4fbcd] | 45 | if (win) werase(win); |
---|
| 46 | } |
---|
| 47 | |
---|
[c9334b1] | 48 | void owl_editwin_set_curswin(owl_editwin *e, WINDOW *w, int winlines, int wincols) |
---|
| 49 | { |
---|
[7d4fbcd] | 50 | e->curswin=w; |
---|
| 51 | e->winlines=winlines; |
---|
| 52 | e->wincols=wincols; |
---|
[9a6cc40] | 53 | e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g)); |
---|
| 54 | e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g)); |
---|
[7d4fbcd] | 55 | } |
---|
| 56 | |
---|
[c9334b1] | 57 | /* echo the character 'ch' for each normal character keystroke, |
---|
| 58 | * excepting locktext. This is useful for entering passwords etc. If |
---|
| 59 | * ch=='\0' characters are echo'd normally |
---|
| 60 | */ |
---|
| 61 | void owl_editwin_set_echochar(owl_editwin *e, int ch) |
---|
| 62 | { |
---|
| 63 | e->echochar=ch; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | WINDOW *owl_editwin_get_curswin(owl_editwin *e) |
---|
| 67 | { |
---|
[060b3b4] | 68 | return(e->curswin); |
---|
[7d4fbcd] | 69 | } |
---|
| 70 | |
---|
[c9334b1] | 71 | void owl_editwin_set_history(owl_editwin *e, owl_history *h) |
---|
| 72 | { |
---|
[060b3b4] | 73 | e->hist=h; |
---|
[10b866d] | 74 | } |
---|
| 75 | |
---|
[c9334b1] | 76 | owl_history *owl_editwin_get_history(owl_editwin *e) |
---|
| 77 | { |
---|
[060b3b4] | 78 | return(e->hist); |
---|
[10b866d] | 79 | } |
---|
| 80 | |
---|
[c9334b1] | 81 | void owl_editwin_set_dotsend(owl_editwin *e) |
---|
| 82 | { |
---|
[7d4fbcd] | 83 | e->dotsend=1; |
---|
| 84 | } |
---|
| 85 | |
---|
[e74c01c] | 86 | void owl_editwin_set_command(owl_editwin *e, char *command) { |
---|
| 87 | if(e->command) owl_free(e->command); |
---|
| 88 | e->command = owl_strdup(command); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | char *owl_editwin_get_command(owl_editwin *e) { |
---|
| 92 | if(e->command) return e->command; |
---|
| 93 | return ""; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | void owl_editwin_set_callback(owl_editwin *e, void (*cb)(owl_editwin*)) { |
---|
| 97 | e->callback = cb; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void (*owl_editwin_get_callback(owl_editwin *e))(owl_editwin*) { |
---|
| 101 | return e->callback; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void owl_editwin_set_cbdata(owl_editwin *e, void *data) { |
---|
| 105 | e->cbdata = data; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void* owl_editwin_get_cbdata(owl_editwin *e) { |
---|
| 109 | return e->cbdata; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void owl_editwin_do_callback(owl_editwin *e) { |
---|
| 113 | void (*cb)(owl_editwin*); |
---|
| 114 | cb=owl_editwin_get_callback(e); |
---|
| 115 | if(!cb) { |
---|
| 116 | owl_function_error("Internal error: No editwin callback!"); |
---|
| 117 | } else { |
---|
| 118 | // owl_function_error("text: |%s|", owl_editwin_get_text(e)); |
---|
| 119 | cb(e); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
[c9334b1] | 123 | int owl_editwin_limit_maxcols(int v, int maxv) |
---|
| 124 | { |
---|
[d36f2cb] | 125 | if (maxv > 5 && v > maxv) { |
---|
[060b3b4] | 126 | return(maxv); |
---|
[d36f2cb] | 127 | } else { |
---|
[060b3b4] | 128 | return(v); |
---|
[d36f2cb] | 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
[c9334b1] | 132 | /* set text to be 'locked in' at the beginning of the buffer, any |
---|
| 133 | * previous text (including locked text) will be overwritten |
---|
| 134 | */ |
---|
| 135 | void owl_editwin_set_locktext(owl_editwin *e, char *text) |
---|
| 136 | { |
---|
[7d4fbcd] | 137 | |
---|
| 138 | int x, y; |
---|
| 139 | |
---|
| 140 | x=e->buffx; |
---|
| 141 | y=e->buffy; |
---|
| 142 | e->buffx=0; |
---|
| 143 | e->buffy=0; |
---|
| 144 | owl_editwin_overwrite_string(e, text); |
---|
[47519e1b] | 145 | owl_editwin_overwrite_char(e, '\0'); |
---|
[7d4fbcd] | 146 | e->lock=strlen(text); |
---|
| 147 | /* if (text[e->lock-1]=='\n') e->lock--; */ |
---|
[47519e1b] | 148 | /* e->buffx=x; */ |
---|
| 149 | /* e->buffy=y; */ |
---|
[7d4fbcd] | 150 | owl_editwin_adjust_for_locktext(e); |
---|
| 151 | owl_editwin_redisplay(e, 0); |
---|
| 152 | } |
---|
| 153 | |
---|
[c9334b1] | 154 | int owl_editwin_get_style(owl_editwin *e) |
---|
| 155 | { |
---|
[7d4fbcd] | 156 | return(e->style); |
---|
| 157 | } |
---|
| 158 | |
---|
[c9334b1] | 159 | void owl_editwin_new_style(owl_editwin *e, int newstyle, owl_history *h) |
---|
| 160 | { |
---|
[7d4fbcd] | 161 | char *ptr; |
---|
[10b866d] | 162 | |
---|
| 163 | owl_editwin_set_history(e, h); |
---|
[7d4fbcd] | 164 | if (e->style==newstyle) return; |
---|
| 165 | |
---|
| 166 | if (newstyle==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
| 167 | e->style=newstyle; |
---|
| 168 | } else if (newstyle==OWL_EDITWIN_STYLE_ONELINE) { |
---|
| 169 | e->style=newstyle; |
---|
| 170 | |
---|
| 171 | /* nuke everything after the first line */ |
---|
| 172 | if (e->bufflen > 0) { |
---|
| 173 | ptr=strchr(e->buff, '\n')-1; |
---|
| 174 | if (ptr) { |
---|
| 175 | e->bufflen=ptr - e->buff; |
---|
| 176 | e->buff[e->bufflen]='\0'; |
---|
| 177 | e->buffx=0; |
---|
| 178 | e->buffy=0; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
[c9334b1] | 184 | /* completly reinitialize the buffer */ |
---|
| 185 | void owl_editwin_fullclear(owl_editwin *e) |
---|
| 186 | { |
---|
[7d4fbcd] | 187 | owl_free(e->buff); |
---|
[10b866d] | 188 | owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style, e->hist); |
---|
[7d4fbcd] | 189 | } |
---|
| 190 | |
---|
[c9334b1] | 191 | /* clear all text except for locktext and put the cursor at the |
---|
| 192 | * beginning |
---|
| 193 | */ |
---|
| 194 | void owl_editwin_clear(owl_editwin *e) |
---|
| 195 | { |
---|
| 196 | |
---|
[7d4fbcd] | 197 | int lock; |
---|
[10b866d] | 198 | int dotsend=e->dotsend; |
---|
[7d4fbcd] | 199 | char *locktext=NULL; |
---|
[10b866d] | 200 | |
---|
[7d4fbcd] | 201 | lock=0; |
---|
| 202 | if (e->lock > 0) { |
---|
| 203 | lock=1; |
---|
| 204 | |
---|
| 205 | locktext=owl_malloc(e->lock+20); |
---|
| 206 | strncpy(locktext, e->buff, e->lock); |
---|
| 207 | locktext[e->lock]='\0'; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | owl_free(e->buff); |
---|
[10b866d] | 211 | owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style, e->hist); |
---|
[7d4fbcd] | 212 | |
---|
| 213 | if (lock > 0) { |
---|
| 214 | owl_editwin_set_locktext(e, locktext); |
---|
| 215 | } |
---|
[10b866d] | 216 | if (dotsend) { |
---|
| 217 | owl_editwin_set_dotsend(e); |
---|
| 218 | } |
---|
[7d4fbcd] | 219 | |
---|
| 220 | if (locktext) owl_free(locktext); |
---|
| 221 | owl_editwin_adjust_for_locktext(e); |
---|
| 222 | } |
---|
| 223 | |
---|
[c9334b1] | 224 | /* malloc more space for the buffer */ |
---|
| 225 | void _owl_editwin_addspace(owl_editwin *e) |
---|
| 226 | { |
---|
[7d4fbcd] | 227 | e->buff=owl_realloc(e->buff, e->allocated+INCR); |
---|
| 228 | if (!e->buff) { |
---|
| 229 | /* error */ |
---|
| 230 | return; |
---|
| 231 | } |
---|
| 232 | e->allocated+=INCR; |
---|
| 233 | } |
---|
| 234 | |
---|
[c9334b1] | 235 | void owl_editwin_recenter(owl_editwin *e) |
---|
| 236 | { |
---|
[7d4fbcd] | 237 | e->topline=e->buffy-(e->winlines/2); |
---|
| 238 | if (e->topline<0) e->topline=0; |
---|
| 239 | if (e->topline>owl_editwin_get_numlines(e)) e->topline=owl_editwin_get_numlines(e); |
---|
| 240 | } |
---|
| 241 | |
---|
[c9334b1] | 242 | /* regenerate the text on the curses window */ |
---|
| 243 | /* if update == 1 then do a doupdate(), otherwise do not */ |
---|
| 244 | void owl_editwin_redisplay(owl_editwin *e, int update) |
---|
| 245 | { |
---|
[7d4fbcd] | 246 | |
---|
| 247 | char *ptr1, *ptr2, *ptr3, *buff; |
---|
[cf83b7a] | 248 | int i; |
---|
[7d4fbcd] | 249 | |
---|
| 250 | werase(e->curswin); |
---|
| 251 | wmove(e->curswin, 0, 0); |
---|
| 252 | |
---|
| 253 | /* start at topline */ |
---|
[47519e1b] | 254 | ptr1 = e->buff; |
---|
| 255 | for (i = 0; i < e->topline; i++) { |
---|
| 256 | ptr2 = strchr(ptr1, '\n'); |
---|
[7d4fbcd] | 257 | if (!ptr2) { |
---|
| 258 | /* we're already on the last line */ |
---|
| 259 | break; |
---|
| 260 | } |
---|
[47519e1b] | 261 | ptr1 = ptr2 + 1; |
---|
[7d4fbcd] | 262 | } |
---|
| 263 | /* ptr1 now stores the starting point */ |
---|
| 264 | |
---|
| 265 | /* find the ending point and store it in ptr3 */ |
---|
[47519e1b] | 266 | ptr2 = ptr1; |
---|
| 267 | ptr3 = ptr1; |
---|
| 268 | for (i = 0; i < e->winlines; i++) { |
---|
| 269 | ptr3 = strchr(ptr2, '\n'); |
---|
[7d4fbcd] | 270 | if (!ptr3) { |
---|
| 271 | /* we've hit the last line */ |
---|
| 272 | /* print everything to the end */ |
---|
[47519e1b] | 273 | ptr3 = e->buff + e->bufflen - 1; |
---|
[7d4fbcd] | 274 | ptr3--; |
---|
| 275 | break; |
---|
| 276 | } |
---|
[47519e1b] | 277 | ptr2 = ptr3 + 1; |
---|
[7d4fbcd] | 278 | } |
---|
[47519e1b] | 279 | ptr3 += 2; |
---|
[7d4fbcd] | 280 | |
---|
[47519e1b] | 281 | buff = owl_malloc(ptr3 - ptr1 + 50); |
---|
| 282 | strncpy(buff, ptr1, ptr3 - ptr1); |
---|
| 283 | buff[ptr3 - ptr1] = '\0'; |
---|
| 284 | if (e->echochar == '\0') { |
---|
[c9334b1] | 285 | waddstr(e->curswin, buff); |
---|
| 286 | } else { |
---|
| 287 | /* translate to echochar, *except* for the locktext */ |
---|
| 288 | int len; |
---|
[47519e1b] | 289 | int dolocklen = e->lock - (ptr1 - e->buff); |
---|
[c9334b1] | 290 | |
---|
[47519e1b] | 291 | for (i = 0; i < dolocklen; i++) { |
---|
[c9334b1] | 292 | waddch(e->curswin, buff[i]); |
---|
| 293 | } |
---|
[47519e1b] | 294 | len = strlen(buff); |
---|
| 295 | for (i = 0; i < len-dolocklen; i++) { |
---|
[c9334b1] | 296 | waddch(e->curswin, e->echochar); |
---|
| 297 | } |
---|
| 298 | } |
---|
[47519e1b] | 299 | wmove(e->curswin, e->buffy-e->topline, e->buffx + _owl_editwin_cursor_adjustment(e)); |
---|
[7d4fbcd] | 300 | wnoutrefresh(e->curswin); |
---|
[47519e1b] | 301 | if (update == 1) { |
---|
[7d4fbcd] | 302 | doupdate(); |
---|
| 303 | } |
---|
| 304 | owl_free(buff); |
---|
| 305 | } |
---|
| 306 | |
---|
[47519e1b] | 307 | /* Remove n bytes at cursor. */ |
---|
| 308 | void _owl_editwin_remove_bytes(owl_editwin *e, int n) /*noproto*/ |
---|
| 309 | { |
---|
| 310 | int i = _owl_editwin_get_index_from_xy(e) + n; |
---|
| 311 | for (; i < e->bufflen; i++) { |
---|
| 312 | e->buff[i-n] = e->buff[i]; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | e->bufflen -= n; |
---|
| 316 | e->buff[e->bufflen] = '\0'; |
---|
| 317 | } |
---|
| 318 | /* Insert n bytes at cursor.*/ |
---|
| 319 | void _owl_editwin_insert_bytes(owl_editwin *e, int n) /*noproto*/ |
---|
| 320 | { |
---|
| 321 | int i, z; |
---|
| 322 | |
---|
| 323 | if ((e->bufflen + n) > (e->allocated - 5)) { |
---|
| 324 | _owl_editwin_addspace(e); |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | e->bufflen += n; |
---|
| 328 | e->buff[e->bufflen] = '\0'; |
---|
| 329 | |
---|
| 330 | z = _owl_editwin_get_index_from_xy(e); |
---|
| 331 | for (i = e->bufflen - 1; i > z; i--) { |
---|
| 332 | e->buff[i] = e->buff[i - n]; |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
[7d4fbcd] | 336 | |
---|
[c9334b1] | 337 | /* linewrap the word just before the cursor. |
---|
| 338 | * returns 0 on success |
---|
| 339 | * returns -1 if we could not wrap. |
---|
| 340 | */ |
---|
| 341 | int _owl_editwin_linewrap_word(owl_editwin *e) |
---|
| 342 | { |
---|
[7d4fbcd] | 343 | int i, z; |
---|
| 344 | |
---|
[47519e1b] | 345 | z = _owl_editwin_get_index_from_xy(e); |
---|
[7d4fbcd] | 346 | /* move back and line wrap the previous word */ |
---|
[47519e1b] | 347 | for (i = z - 1; ; i--) { |
---|
[7d4fbcd] | 348 | /* move back until you find a space or hit the beginning of the line */ |
---|
[47519e1b] | 349 | if (e->buff[i] == ' ') { |
---|
[7d4fbcd] | 350 | /* replace the space with a newline */ |
---|
[47519e1b] | 351 | e->buff[i] = '\n'; |
---|
[7d4fbcd] | 352 | e->buffy++; |
---|
[47519e1b] | 353 | e->buffx = z - i - 1; |
---|
[7d4fbcd] | 354 | /* were we on the last line */ |
---|
| 355 | return(0); |
---|
[47519e1b] | 356 | } else if (e->buff[i] == '\n' || i <= e->lock) { |
---|
| 357 | /* we hit the beginning of the line or the buffer, we cannot |
---|
[7d4fbcd] | 358 | * wrap. |
---|
| 359 | */ |
---|
| 360 | return(-1); |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | |
---|
[c9334b1] | 365 | /* insert a character at the current point (shift later |
---|
| 366 | * characters over) |
---|
| 367 | */ |
---|
[47519e1b] | 368 | void owl_editwin_insert_char(owl_editwin *e, gunichar c) |
---|
[c9334b1] | 369 | { |
---|
[47519e1b] | 370 | int z, i, ret, len; |
---|
| 371 | char tmp[6]; |
---|
| 372 | memset(tmp, '\0', 6); |
---|
[7d4fbcd] | 373 | |
---|
| 374 | /* \r is \n */ |
---|
[47519e1b] | 375 | if (c == '\r') { |
---|
| 376 | c = '\n'; |
---|
[7d4fbcd] | 377 | } |
---|
| 378 | |
---|
[47519e1b] | 379 | if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) { |
---|
[7d4fbcd] | 380 | /* perhaps later this will change some state that allows the string |
---|
| 381 | to be read */ |
---|
| 382 | return; |
---|
| 383 | } |
---|
| 384 | |
---|
[47519e1b] | 385 | g_unichar_to_utf8(c, tmp); |
---|
| 386 | len = strlen(tmp); |
---|
| 387 | |
---|
[7d4fbcd] | 388 | /* make sure there is enough memory for the new text */ |
---|
[47519e1b] | 389 | if ((e->bufflen + len) > (e->allocated - 5)) { |
---|
[7d4fbcd] | 390 | _owl_editwin_addspace(e); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | /* get the insertion point */ |
---|
[47519e1b] | 394 | z = _owl_editwin_get_index_from_xy(e); |
---|
[7d4fbcd] | 395 | |
---|
| 396 | /* If we're going to insert at the last column do word wrapping, unless it's a \n */ |
---|
[47519e1b] | 397 | if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) { |
---|
| 398 | ret = _owl_editwin_linewrap_word(e); |
---|
| 399 | if (ret == -1) { |
---|
[7d4fbcd] | 400 | /* we couldn't wrap, insert a hard newline instead */ |
---|
| 401 | owl_editwin_insert_char(e, '\n'); |
---|
| 402 | } |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | /* shift all the other characters right */ |
---|
[47519e1b] | 406 | _owl_editwin_insert_bytes(e, len); |
---|
[7d4fbcd] | 407 | |
---|
[47519e1b] | 408 | /* insert the new character */ |
---|
| 409 | for(i = 0; i < len; i++) { |
---|
| 410 | e->buff[z + i] = tmp[i]; |
---|
| 411 | } |
---|
[7d4fbcd] | 412 | |
---|
| 413 | /* housekeeping */ |
---|
[47519e1b] | 414 | e->bufflen += len; |
---|
| 415 | e->buff[e->bufflen] = '\0'; |
---|
[7d4fbcd] | 416 | |
---|
| 417 | /* advance the cursor */ |
---|
[47519e1b] | 418 | z += len; |
---|
| 419 | _owl_editwin_set_xy_by_index(e, z); |
---|
[7d4fbcd] | 420 | } |
---|
| 421 | |
---|
[c9334b1] | 422 | /* overwrite the character at the current point with 'c' */ |
---|
[47519e1b] | 423 | void owl_editwin_overwrite_char(owl_editwin *e, gunichar c) |
---|
[c9334b1] | 424 | { |
---|
[47519e1b] | 425 | int z, oldlen, newlen, i; |
---|
| 426 | char tmp[6]; |
---|
| 427 | memset(tmp, '\0', 6); |
---|
[7d4fbcd] | 428 | |
---|
| 429 | /* \r is \n */ |
---|
[47519e1b] | 430 | if (c == '\r') { |
---|
| 431 | c = '\n'; |
---|
[7d4fbcd] | 432 | } |
---|
| 433 | |
---|
[47519e1b] | 434 | if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) { |
---|
[7d4fbcd] | 435 | /* perhaps later this will change some state that allows the string |
---|
| 436 | to be read */ |
---|
| 437 | return; |
---|
| 438 | } |
---|
| 439 | |
---|
[47519e1b] | 440 | g_unichar_to_utf8(c, tmp); |
---|
| 441 | newlen = strlen(tmp); |
---|
[7d4fbcd] | 442 | |
---|
[47519e1b] | 443 | z = _owl_editwin_get_index_from_xy(e); |
---|
| 444 | { |
---|
| 445 | char *t = g_utf8_find_next_char(e->buff + z, NULL); |
---|
| 446 | oldlen = (t ? (t - (e->buff + z)) : 1); |
---|
[7d4fbcd] | 447 | } |
---|
| 448 | |
---|
[47519e1b] | 449 | if ((e->bufflen + newlen - oldlen + 1) > (e->allocated - 5)) { |
---|
| 450 | _owl_editwin_addspace(e); |
---|
[7d4fbcd] | 451 | } |
---|
| 452 | |
---|
[47519e1b] | 453 | if (oldlen > newlen) { |
---|
| 454 | _owl_editwin_remove_bytes(e, oldlen-newlen); |
---|
| 455 | } |
---|
| 456 | else /* oldlen < newlen */ { |
---|
| 457 | _owl_editwin_insert_bytes(e, newlen-oldlen); |
---|
[7d4fbcd] | 458 | } |
---|
[47519e1b] | 459 | /* Overwrite the old char*/ |
---|
| 460 | for (i = 0; i < newlen; i++) { |
---|
| 461 | e->buff[z+i] = tmp[i]; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | /* housekeeping */ |
---|
| 465 | e->buff[e->bufflen] = '\0'; |
---|
[7d4fbcd] | 466 | |
---|
[47519e1b] | 467 | /* advance the cursor */ |
---|
| 468 | z += newlen; |
---|
| 469 | _owl_editwin_set_xy_by_index(e, z); |
---|
[7d4fbcd] | 470 | } |
---|
| 471 | |
---|
[c9334b1] | 472 | /* delete the character at the current point, following chars |
---|
| 473 | * shift left. |
---|
| 474 | */ |
---|
| 475 | void owl_editwin_delete_char(owl_editwin *e) |
---|
| 476 | { |
---|
[47519e1b] | 477 | int z; |
---|
| 478 | char *p1, *p2; |
---|
| 479 | gunichar c; |
---|
[7d4fbcd] | 480 | |
---|
[47519e1b] | 481 | if (e->bufflen == 0) return; |
---|
[7d4fbcd] | 482 | |
---|
| 483 | /* get the deletion point */ |
---|
[47519e1b] | 484 | z = _owl_editwin_get_index_from_xy(e); |
---|
[7d4fbcd] | 485 | |
---|
[47519e1b] | 486 | if (z == e->bufflen) return; |
---|
[7d4fbcd] | 487 | |
---|
[47519e1b] | 488 | p1 = e->buff + z; |
---|
| 489 | p2 = g_utf8_next_char(p1); |
---|
| 490 | c = g_utf8_get_char(p2); |
---|
| 491 | while (g_unichar_ismark(c)) { |
---|
| 492 | p2 = g_utf8_next_char(p2); |
---|
| 493 | c = g_utf8_get_char(p2); |
---|
[7d4fbcd] | 494 | } |
---|
[47519e1b] | 495 | _owl_editwin_remove_bytes(e, p2-p1); |
---|
[7d4fbcd] | 496 | } |
---|
| 497 | |
---|
[c9334b1] | 498 | /* Swap the character at point with the character at point-1 and |
---|
| 499 | * advance the pointer. If point is at beginning of buffer do |
---|
| 500 | * nothing. If point is after the last character swap point-1 with |
---|
| 501 | * point-2. (Behaves as observed in tcsh and emacs). |
---|
| 502 | */ |
---|
| 503 | void owl_editwin_transpose_chars(owl_editwin *e) |
---|
| 504 | { |
---|
[f2e36b5] | 505 | int z; |
---|
[47519e1b] | 506 | char *p1, *p2, *p3, *tmp; |
---|
[f2e36b5] | 507 | |
---|
[47519e1b] | 508 | if (e->bufflen == 0) return; |
---|
[f2e36b5] | 509 | |
---|
| 510 | /* get the cursor point */ |
---|
[47519e1b] | 511 | z = _owl_editwin_get_index_from_xy(e); |
---|
[f2e36b5] | 512 | |
---|
[47519e1b] | 513 | if (z == e->bufflen) { |
---|
[f2e36b5] | 514 | /* point is after last character */ |
---|
| 515 | z--; |
---|
| 516 | } |
---|
| 517 | |
---|
[47519e1b] | 518 | if (z - 1 < e->lock) { |
---|
[f2e36b5] | 519 | /* point is at beginning of buffer, do nothing */ |
---|
| 520 | return; |
---|
| 521 | } |
---|
| 522 | |
---|
[47519e1b] | 523 | /* Transpose two utf-8 unicode glyphs. */ |
---|
| 524 | p1 = e->buff + z; |
---|
| 525 | |
---|
| 526 | p2 = g_utf8_find_next_char(p1, NULL); |
---|
| 527 | while (p2 != NULL && g_unichar_ismark(g_utf8_get_char(p2))) { |
---|
| 528 | p2 = g_utf8_find_next_char(p2, NULL); |
---|
| 529 | } |
---|
| 530 | if (p2 == NULL) return; |
---|
| 531 | |
---|
| 532 | p3 = g_utf8_find_prev_char(e->buff, p1); |
---|
| 533 | while (p3 != NULL && g_unichar_ismark(g_utf8_get_char(p3))) { |
---|
| 534 | p3 = g_utf8_find_prev_char(p3, NULL); |
---|
| 535 | } |
---|
| 536 | if (p3 == NULL) return; |
---|
| 537 | |
---|
| 538 | tmp = owl_malloc(p2 - p3 + 5); |
---|
| 539 | *tmp = '\0'; |
---|
| 540 | strncat(tmp, p1, p2 - p1); |
---|
| 541 | strncat(tmp, p3, p1 - p3); |
---|
| 542 | strncpy(p3, tmp, p2 - p3); |
---|
| 543 | owl_free(tmp); |
---|
| 544 | _owl_editwin_set_xy_by_index(e, p3 - e->buff); |
---|
[f2e36b5] | 545 | } |
---|
| 546 | |
---|
[c9334b1] | 547 | /* insert 'string' at the current point, later text is shifted |
---|
| 548 | * right |
---|
| 549 | */ |
---|
| 550 | void owl_editwin_insert_string(owl_editwin *e, char *string) |
---|
| 551 | { |
---|
[47519e1b] | 552 | char *p; |
---|
| 553 | gunichar c; |
---|
| 554 | if (!g_utf8_validate(string, -1, NULL)) { |
---|
| 555 | owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string."); |
---|
| 556 | return; |
---|
| 557 | } |
---|
| 558 | p = string; |
---|
| 559 | c = g_utf8_get_char(p); |
---|
| 560 | while (c) { |
---|
| 561 | owl_editwin_process_char(e, c); |
---|
| 562 | p = g_utf8_next_char(p); |
---|
| 563 | c = g_utf8_get_char(p); |
---|
[7d4fbcd] | 564 | } |
---|
| 565 | } |
---|
| 566 | |
---|
[c9334b1] | 567 | /* write 'string' at the current point, overwriting text that is |
---|
| 568 | * already there |
---|
| 569 | */ |
---|
| 570 | |
---|
| 571 | void owl_editwin_overwrite_string(owl_editwin *e, char *string) |
---|
| 572 | { |
---|
[47519e1b] | 573 | char *p; |
---|
| 574 | gunichar c; |
---|
[7d4fbcd] | 575 | |
---|
[47519e1b] | 576 | if (!g_utf8_validate(string, -1, NULL)) { |
---|
| 577 | owl_function_debugmsg("owl_editwin_overwrite_string: received non-utf-8 string."); |
---|
| 578 | return; |
---|
| 579 | } |
---|
| 580 | p = string; |
---|
| 581 | c = g_utf8_get_char(p); |
---|
| 582 | while (c) { |
---|
| 583 | owl_editwin_overwrite_char(e, c); |
---|
| 584 | p = g_utf8_next_char(p); |
---|
| 585 | c = g_utf8_get_char(p); |
---|
[7d4fbcd] | 586 | } |
---|
| 587 | } |
---|
| 588 | |
---|
[c9334b1] | 589 | /* get the index into e->buff for the current cursor |
---|
| 590 | * position. |
---|
| 591 | */ |
---|
| 592 | int _owl_editwin_get_index_from_xy(owl_editwin *e) |
---|
| 593 | { |
---|
[7d4fbcd] | 594 | int i; |
---|
| 595 | char *ptr1, *ptr2; |
---|
[47519e1b] | 596 | gunichar c; |
---|
[7d4fbcd] | 597 | |
---|
[47519e1b] | 598 | if (e->bufflen == 0) return(0); |
---|
[7d4fbcd] | 599 | |
---|
| 600 | /* first go to the yth line */ |
---|
[47519e1b] | 601 | ptr1 = e->buff; |
---|
| 602 | for (i = 0; i < e->buffy; i++) { |
---|
| 603 | ptr2= strchr(ptr1, '\n'); |
---|
[7d4fbcd] | 604 | if (!ptr2) { |
---|
| 605 | /* we're already on the last line */ |
---|
| 606 | break; |
---|
| 607 | } |
---|
[47519e1b] | 608 | ptr1 = ptr2 + 1; |
---|
[7d4fbcd] | 609 | } |
---|
| 610 | |
---|
[47519e1b] | 611 | /* now go to the xth cell */ |
---|
| 612 | ptr2 = ptr1; |
---|
| 613 | i = 0; |
---|
| 614 | while (ptr2 != NULL && i < e->buffx && (ptr2 - e->buff) < e->bufflen) { |
---|
| 615 | c = g_utf8_get_char(ptr2); |
---|
| 616 | i += (c == '\n' ? 1 : mk_wcwidth(c)); |
---|
| 617 | ptr2 = g_utf8_next_char(ptr2); |
---|
[7d4fbcd] | 618 | } |
---|
[47519e1b] | 619 | while(ptr2 != NULL && g_unichar_ismark(g_utf8_get_char(ptr2))) { |
---|
| 620 | ptr2 = g_utf8_next_char(ptr2); |
---|
[7d4fbcd] | 621 | } |
---|
[47519e1b] | 622 | if (ptr2 == NULL) return e->bufflen; |
---|
| 623 | return(ptr2 - e->buff); |
---|
[7d4fbcd] | 624 | } |
---|
| 625 | |
---|
[c9334b1] | 626 | void _owl_editwin_set_xy_by_index(owl_editwin *e, int index) |
---|
| 627 | { |
---|
[47519e1b] | 628 | char *ptr1, *ptr2, *target; |
---|
| 629 | gunichar c; |
---|
| 630 | |
---|
| 631 | e->buffx = 0; |
---|
| 632 | e->buffy = 0; |
---|
| 633 | |
---|
| 634 | ptr1 = e->buff; |
---|
| 635 | target = ptr1 + index; |
---|
| 636 | /* target sanitizing */ |
---|
| 637 | if ((target[0] & 0x80) && (~target[0] & 0x40)) { |
---|
| 638 | /* middle of a utf-8 character, back up to previous character. */ |
---|
| 639 | target = g_utf8_find_prev_char(e->buff, target); |
---|
| 640 | } |
---|
| 641 | c = g_utf8_get_char(target); |
---|
| 642 | while (g_unichar_ismark(c) && target > e->buff) { |
---|
| 643 | /* Adjust the target off of combining characters and the like. */ |
---|
| 644 | target = g_utf8_find_prev_char(e->buff, target); |
---|
| 645 | c = g_utf8_get_char(target); |
---|
| 646 | } |
---|
| 647 | /* If we start with a mark, something is wrong.*/ |
---|
| 648 | if (g_unichar_ismark(c)) return; |
---|
[7d4fbcd] | 649 | |
---|
[47519e1b] | 650 | /* Now our target should be acceptable. */ |
---|
| 651 | ptr2 = strchr(ptr1, '\n'); |
---|
| 652 | while (ptr2 != NULL && ptr2 < target) { |
---|
| 653 | e->buffy++; |
---|
| 654 | ptr1 = ptr2 + 1; |
---|
| 655 | ptr2 = strchr(ptr1, '\n'); |
---|
| 656 | } |
---|
| 657 | ptr2 = ptr1; |
---|
| 658 | while (ptr2 != NULL && ptr2 < target) { |
---|
| 659 | c = g_utf8_get_char(ptr2); |
---|
| 660 | e->buffx += mk_wcwidth(c); |
---|
| 661 | ptr2 = g_utf8_next_char(ptr2); |
---|
[7d4fbcd] | 662 | } |
---|
| 663 | } |
---|
| 664 | |
---|
[47519e1b] | 665 | int _owl_editwin_cursor_adjustment(owl_editwin *e) |
---|
| 666 | { |
---|
| 667 | char *ptr1, *ptr2; |
---|
| 668 | gunichar c; |
---|
| 669 | int x, i; |
---|
| 670 | |
---|
| 671 | ptr1 = e->buff; |
---|
| 672 | ptr2 = strchr(ptr1, '\n'); |
---|
| 673 | for (i = 0; ptr2 != NULL && i < e->buffy; i++) { |
---|
| 674 | ptr1 = ptr2 + 1; |
---|
| 675 | ptr2 = strchr(ptr1, '\n'); |
---|
| 676 | } |
---|
| 677 | ptr2 = ptr1; |
---|
| 678 | x = 0; |
---|
| 679 | while (ptr2 != NULL && x < e->buffx) { |
---|
| 680 | if (*ptr2 == '\n') return 0; |
---|
| 681 | c = g_utf8_get_char(ptr2); |
---|
| 682 | x += mk_wcwidth(c); |
---|
| 683 | ptr2 = g_utf8_next_char(ptr2); |
---|
| 684 | } |
---|
| 685 | return x - e->buffx; |
---|
| 686 | } |
---|
| 687 | |
---|
[c9334b1] | 688 | void owl_editwin_adjust_for_locktext(owl_editwin *e) |
---|
| 689 | { |
---|
[7d4fbcd] | 690 | /* if we happen to have the cursor over locked text |
---|
| 691 | * move it to be out of the locktext region */ |
---|
[47519e1b] | 692 | if (_owl_editwin_get_index_from_xy(e) < e->lock) { |
---|
[7d4fbcd] | 693 | _owl_editwin_set_xy_by_index(e, e->lock); |
---|
| 694 | } |
---|
| 695 | } |
---|
| 696 | |
---|
[c9334b1] | 697 | void owl_editwin_backspace(owl_editwin *e) |
---|
| 698 | { |
---|
[7d4fbcd] | 699 | /* delete the char before the current one |
---|
| 700 | * and shift later chars left |
---|
| 701 | */ |
---|
| 702 | if (_owl_editwin_get_index_from_xy(e) > e->lock) { |
---|
| 703 | owl_editwin_key_left(e); |
---|
| 704 | owl_editwin_delete_char(e); |
---|
| 705 | } |
---|
| 706 | owl_editwin_adjust_for_locktext(e); |
---|
| 707 | } |
---|
| 708 | |
---|
[c9334b1] | 709 | void owl_editwin_key_up(owl_editwin *e) |
---|
| 710 | { |
---|
[7d4fbcd] | 711 | if (e->buffy > 0) e->buffy--; |
---|
[47519e1b] | 712 | if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) { |
---|
| 713 | e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy); |
---|
[7d4fbcd] | 714 | } |
---|
| 715 | |
---|
| 716 | /* do we need to scroll? */ |
---|
| 717 | if (e->buffy-e->topline < 0) { |
---|
| 718 | e->topline-=e->winlines/2; |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | owl_editwin_adjust_for_locktext(e); |
---|
| 722 | } |
---|
| 723 | |
---|
[c9334b1] | 724 | void owl_editwin_key_down(owl_editwin *e) |
---|
| 725 | { |
---|
[7d4fbcd] | 726 | /* move down if we can */ |
---|
| 727 | if (e->buffy+1 < owl_editwin_get_numlines(e)) e->buffy++; |
---|
| 728 | |
---|
| 729 | /* if we're past the last character move back */ |
---|
[47519e1b] | 730 | if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) { |
---|
| 731 | e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy); |
---|
[7d4fbcd] | 732 | } |
---|
| 733 | |
---|
| 734 | /* do we need to scroll? */ |
---|
| 735 | if (e->buffy-e->topline > e->winlines) { |
---|
| 736 | e->topline+=e->winlines/2; |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | /* adjust for locktext */ |
---|
| 740 | owl_editwin_adjust_for_locktext(e); |
---|
| 741 | } |
---|
| 742 | |
---|
[c9334b1] | 743 | void owl_editwin_key_left(owl_editwin *e) |
---|
| 744 | { |
---|
[47519e1b] | 745 | int i; |
---|
| 746 | char * p; |
---|
| 747 | i = _owl_editwin_get_index_from_xy(e); |
---|
| 748 | p = e->buff + i; |
---|
| 749 | p = g_utf8_find_prev_char(e->buff, p); |
---|
| 750 | while (p && g_unichar_ismark(g_utf8_get_char(p))) { |
---|
| 751 | p = g_utf8_find_prev_char(e->buff, p); |
---|
[7d4fbcd] | 752 | } |
---|
[47519e1b] | 753 | if (p == NULL) p = e->buff; |
---|
| 754 | _owl_editwin_set_xy_by_index(e, p - e->buff); |
---|
[7d4fbcd] | 755 | |
---|
[47519e1b] | 756 | if (e->buffy - e->topline < 0) { |
---|
| 757 | e->topline -= e->winlines / 2; |
---|
[7d4fbcd] | 758 | } |
---|
| 759 | |
---|
| 760 | /* make sure to avoid locktext */ |
---|
| 761 | owl_editwin_adjust_for_locktext(e); |
---|
| 762 | } |
---|
| 763 | |
---|
[c9334b1] | 764 | void owl_editwin_key_right(owl_editwin *e) |
---|
| 765 | { |
---|
[7d4fbcd] | 766 | int i; |
---|
[47519e1b] | 767 | char * p; |
---|
| 768 | i = _owl_editwin_get_index_from_xy(e); |
---|
| 769 | p = e->buff + i; |
---|
| 770 | p = g_utf8_find_next_char(p, NULL); |
---|
| 771 | while (p && g_unichar_ismark(g_utf8_get_char(p))) { |
---|
| 772 | p = g_utf8_find_next_char(p, NULL); |
---|
| 773 | } |
---|
| 774 | if (p == NULL) { |
---|
| 775 | _owl_editwin_set_xy_by_index(e, e->bufflen); |
---|
| 776 | } |
---|
| 777 | else { |
---|
| 778 | _owl_editwin_set_xy_by_index(e, p - e->buff); |
---|
[7d4fbcd] | 779 | } |
---|
| 780 | |
---|
| 781 | /* do we need to scroll down? */ |
---|
[47519e1b] | 782 | if (e->buffy - e->topline >= e->winlines) { |
---|
| 783 | e->topline += e->winlines / 2; |
---|
[7d4fbcd] | 784 | } |
---|
| 785 | } |
---|
| 786 | |
---|
[c9334b1] | 787 | void owl_editwin_move_to_nextword(owl_editwin *e) |
---|
| 788 | { |
---|
[47519e1b] | 789 | /* asedeno: needs fixing for utf-8*/ |
---|
[7d4fbcd] | 790 | int i, x; |
---|
| 791 | |
---|
| 792 | /* if we're starting on a space, find the first non-space */ |
---|
| 793 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 794 | if (e->buff[i]==' ') { |
---|
| 795 | for (x=i; x<e->bufflen; x++) { |
---|
| 796 | if (e->buff[x]!=' ' && e->buff[x]!='\n') { |
---|
| 797 | _owl_editwin_set_xy_by_index(e, x); |
---|
| 798 | break; |
---|
| 799 | } |
---|
| 800 | } |
---|
| 801 | } |
---|
| 802 | |
---|
| 803 | /* find the next space, newline or end of line and go there, if |
---|
| 804 | already at the end of the line, continue on to the next */ |
---|
| 805 | i=owl_editwin_get_numchars_on_line(e, e->buffy); |
---|
| 806 | if (e->buffx < i) { |
---|
| 807 | /* move right till end of line */ |
---|
| 808 | while (e->buffx < i) { |
---|
| 809 | e->buffx++; |
---|
| 810 | if (e->buff[_owl_editwin_get_index_from_xy(e)]==' ') return; |
---|
| 811 | if (e->buffx == i) return; |
---|
| 812 | } |
---|
| 813 | } else if (e->buffx == i) { |
---|
| 814 | /* try to move down */ |
---|
| 815 | if (e->style==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
| 816 | if (e->buffy+1 < owl_editwin_get_numlines(e)) { |
---|
| 817 | e->buffx=0; |
---|
| 818 | e->buffy++; |
---|
| 819 | owl_editwin_move_to_nextword(e); |
---|
| 820 | } |
---|
| 821 | } |
---|
| 822 | } |
---|
| 823 | } |
---|
| 824 | |
---|
[c9334b1] | 825 | /* go backwards to the last non-space character |
---|
| 826 | */ |
---|
| 827 | void owl_editwin_move_to_previousword(owl_editwin *e) |
---|
| 828 | { |
---|
[47519e1b] | 829 | /* asedeno: needs fixing for utf-8*/ |
---|
[7d4fbcd] | 830 | int i, x; |
---|
| 831 | |
---|
| 832 | /* are we already at the beginning of the word? */ |
---|
| 833 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 834 | if ( (e->buff[i]!=' ' && e->buff[i]!='\n' && e->buff[i]!='\0') && |
---|
| 835 | (e->buff[i-1]==' ' || e->buff[i-1]=='\n') ) { |
---|
| 836 | owl_editwin_key_left(e); |
---|
| 837 | } |
---|
| 838 | |
---|
| 839 | /* are we starting on a space character? */ |
---|
| 840 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 841 | if (e->buff[i]==' ' || e->buff[i]=='\n' || e->buff[i]=='\0') { |
---|
| 842 | /* find the first non-space */ |
---|
| 843 | for (x=i; x>=e->lock; x--) { |
---|
| 844 | if (e->buff[x]!=' ' && e->buff[x]!='\n' && e->buff[x]!='\0') { |
---|
| 845 | _owl_editwin_set_xy_by_index(e, x); |
---|
| 846 | break; |
---|
| 847 | } |
---|
| 848 | } |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | /* find the last non-space */ |
---|
| 852 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 853 | for (x=i; x>=e->lock; x--) { |
---|
| 854 | if (e->buff[x-1]==' ' || e->buff[x-1]=='\n') { |
---|
| 855 | _owl_editwin_set_xy_by_index(e, x); |
---|
| 856 | break; |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | _owl_editwin_set_xy_by_index(e, x); |
---|
| 860 | } |
---|
| 861 | |
---|
| 862 | |
---|
[c9334b1] | 863 | void owl_editwin_delete_nextword(owl_editwin *e) |
---|
| 864 | { |
---|
[47519e1b] | 865 | /* asedeno: needs fixing for utf-8*/ |
---|
[7d4fbcd] | 866 | int z; |
---|
| 867 | |
---|
| 868 | if (e->bufflen==0) return; |
---|
| 869 | |
---|
| 870 | /* if we start out on a space character then gobble all the spaces |
---|
| 871 | up first */ |
---|
| 872 | while (1) { |
---|
| 873 | z=_owl_editwin_get_index_from_xy(e); |
---|
| 874 | if (e->buff[z]==' ' || e->buff[z]=='\n') { |
---|
| 875 | owl_editwin_delete_char(e); |
---|
| 876 | } else { |
---|
| 877 | break; |
---|
| 878 | } |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | /* then nuke the next word */ |
---|
| 882 | while (1) { |
---|
| 883 | z=_owl_editwin_get_index_from_xy(e); |
---|
[4c46dfd] | 884 | /* z == e->bufflen check added to prevent a hang I (nelhage) have |
---|
| 885 | seen repeatedly while using owl. I'm not sure precisely what |
---|
| 886 | conditions lead to it. */ |
---|
| 887 | if (z == e->bufflen |
---|
| 888 | || e->buff[z+1]==' ' || e->buff[z+1]=='\n' || e->buff[z+1]=='\0') break; |
---|
[7d4fbcd] | 889 | owl_editwin_delete_char(e); |
---|
| 890 | } |
---|
| 891 | owl_editwin_delete_char(e); |
---|
| 892 | } |
---|
| 893 | |
---|
[c9334b1] | 894 | void owl_editwin_delete_previousword(owl_editwin *e) |
---|
| 895 | { |
---|
[47519e1b] | 896 | /* asedeno: needs fixing for utf-8*/ |
---|
[b68f9cd] | 897 | /* go backwards to the last non-space character, then delete chars */ |
---|
| 898 | int i, startpos, endpos; |
---|
| 899 | |
---|
| 900 | startpos = _owl_editwin_get_index_from_xy(e); |
---|
| 901 | owl_editwin_move_to_previousword(e); |
---|
| 902 | endpos = _owl_editwin_get_index_from_xy(e); |
---|
| 903 | for (i=0; i<startpos-endpos; i++) { |
---|
| 904 | owl_editwin_delete_char(e); |
---|
| 905 | } |
---|
| 906 | } |
---|
| 907 | |
---|
[c9334b1] | 908 | void owl_editwin_delete_to_endofline(owl_editwin *e) |
---|
| 909 | { |
---|
[47519e1b] | 910 | /* asedeno: needs fixing for utf-8*/ |
---|
[7d4fbcd] | 911 | int i; |
---|
| 912 | |
---|
[47519e1b] | 913 | if (owl_editwin_get_numchars_on_line(e, e->buffy) > e->buffx) { |
---|
[7d4fbcd] | 914 | /* normal line */ |
---|
| 915 | i=_owl_editwin_get_index_from_xy(e); |
---|
| 916 | while(i < e->bufflen) { |
---|
| 917 | if (e->buff[i]!='\n') { |
---|
| 918 | owl_editwin_delete_char(e); |
---|
| 919 | } else if ((e->buff[i]=='\n') && (i==e->bufflen-1)) { |
---|
| 920 | owl_editwin_delete_char(e); |
---|
| 921 | } else { |
---|
| 922 | return; |
---|
| 923 | } |
---|
| 924 | } |
---|
| 925 | } else if (e->buffy+1 < owl_editwin_get_numlines(e)) { |
---|
| 926 | /* line with cursor at the end but not on very last line */ |
---|
| 927 | owl_editwin_key_right(e); |
---|
| 928 | owl_editwin_backspace(e); |
---|
| 929 | } |
---|
| 930 | } |
---|
| 931 | |
---|
[c9334b1] | 932 | void owl_editwin_move_to_line_end(owl_editwin *e) |
---|
| 933 | { |
---|
[47519e1b] | 934 | e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy); |
---|
[7d4fbcd] | 935 | } |
---|
| 936 | |
---|
[c9334b1] | 937 | void owl_editwin_move_to_line_start(owl_editwin *e) |
---|
| 938 | { |
---|
[7d4fbcd] | 939 | e->buffx=0; |
---|
| 940 | owl_editwin_adjust_for_locktext(e); |
---|
| 941 | } |
---|
| 942 | |
---|
[c9334b1] | 943 | void owl_editwin_move_to_end(owl_editwin *e) |
---|
| 944 | { |
---|
[7d4fbcd] | 945 | /* go to last char */ |
---|
| 946 | e->buffy=owl_editwin_get_numlines(e)-1; |
---|
[47519e1b] | 947 | e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy); |
---|
[7d4fbcd] | 948 | owl_editwin_key_right(e); |
---|
| 949 | |
---|
| 950 | /* do we need to scroll? */ |
---|
[12c35df] | 951 | /* |
---|
[7d4fbcd] | 952 | if (e->buffy-e->topline > e->winlines) { |
---|
| 953 | e->topline+=e->winlines/2; |
---|
| 954 | } |
---|
[12c35df] | 955 | */ |
---|
| 956 | owl_editwin_recenter(e); |
---|
[7d4fbcd] | 957 | } |
---|
| 958 | |
---|
[c9334b1] | 959 | void owl_editwin_move_to_top(owl_editwin *e) |
---|
| 960 | { |
---|
[7d4fbcd] | 961 | _owl_editwin_set_xy_by_index(e, 0); |
---|
| 962 | |
---|
| 963 | /* do we need to scroll? */ |
---|
| 964 | e->topline=0; |
---|
| 965 | |
---|
| 966 | owl_editwin_adjust_for_locktext(e); |
---|
| 967 | } |
---|
| 968 | |
---|
[c9334b1] | 969 | void owl_editwin_fill_paragraph(owl_editwin *e) |
---|
| 970 | { |
---|
[47519e1b] | 971 | /* asedeno: needs fixing for utf-8*/ |
---|
[7d4fbcd] | 972 | int i, save; |
---|
| 973 | |
---|
| 974 | /* save our starting point */ |
---|
| 975 | save=_owl_editwin_get_index_from_xy(e); |
---|
| 976 | |
---|
| 977 | /* scan back to the beginning of this paragraph */ |
---|
| 978 | for (i=save; i>=e->lock; i--) { |
---|
| 979 | if ( (i<=e->lock) || |
---|
| 980 | ((e->buff[i]=='\n') && (e->buff[i-1]=='\n'))) { |
---|
| 981 | _owl_editwin_set_xy_by_index(e, i+1); |
---|
| 982 | break; |
---|
| 983 | } |
---|
| 984 | } |
---|
| 985 | |
---|
| 986 | /* main loop */ |
---|
| 987 | while (1) { |
---|
[47519e1b] | 988 | i = _owl_editwin_get_index_from_xy(e); |
---|
[7d4fbcd] | 989 | |
---|
| 990 | /* bail if we hit the end of the buffer */ |
---|
[47519e1b] | 991 | if (i >= e->bufflen || e->buff[i] == '\0') break; |
---|
[7d4fbcd] | 992 | |
---|
| 993 | /* bail if we hit the end of the paragraph */ |
---|
[47519e1b] | 994 | if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break; |
---|
[7d4fbcd] | 995 | |
---|
| 996 | /* if we've travelled too far, linewrap */ |
---|
| 997 | if ((e->buffx) >= e->fillcol) { |
---|
| 998 | _owl_editwin_linewrap_word(e); |
---|
| 999 | } |
---|
| 1000 | |
---|
| 1001 | /* did we hit the end of a line too soon? */ |
---|
[47519e1b] | 1002 | i = _owl_editwin_get_index_from_xy(e); |
---|
| 1003 | if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) { |
---|
[7d4fbcd] | 1004 | /* ********* we need to make sure we don't pull in a word that's too long ***********/ |
---|
| 1005 | e->buff[i]=' '; |
---|
| 1006 | } |
---|
| 1007 | |
---|
| 1008 | /* fix spacing */ |
---|
[47519e1b] | 1009 | i = _owl_editwin_get_index_from_xy(e); |
---|
| 1010 | if (e->buff[i] == ' ' && e->buff[i+1] == ' ') { |
---|
| 1011 | if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') { |
---|
[7d4fbcd] | 1012 | owl_editwin_key_right(e); |
---|
| 1013 | } else { |
---|
| 1014 | owl_editwin_delete_char(e); |
---|
| 1015 | /* if we did this ahead of the save point, adjust it */ |
---|
[47519e1b] | 1016 | if (i < save) save--; |
---|
[7d4fbcd] | 1017 | } |
---|
| 1018 | } else { |
---|
| 1019 | owl_editwin_key_right(e); |
---|
| 1020 | } |
---|
| 1021 | |
---|
| 1022 | } |
---|
| 1023 | |
---|
| 1024 | /* put cursor back at starting point */ |
---|
| 1025 | _owl_editwin_set_xy_by_index(e, save); |
---|
| 1026 | |
---|
| 1027 | /* do we need to scroll? */ |
---|
| 1028 | if (e->buffy-e->topline < 0) { |
---|
| 1029 | e->topline-=e->winlines/2; |
---|
| 1030 | } |
---|
| 1031 | } |
---|
| 1032 | |
---|
[cf83b7a] | 1033 | /* returns true if only whitespace remains */ |
---|
[c9334b1] | 1034 | int owl_editwin_is_at_end(owl_editwin *e) |
---|
| 1035 | { |
---|
[10b866d] | 1036 | int cur=_owl_editwin_get_index_from_xy(e); |
---|
[c9334b1] | 1037 | return (only_whitespace(e->buff+cur)); |
---|
[217a43e] | 1038 | } |
---|
| 1039 | |
---|
[c9334b1] | 1040 | int owl_editwin_check_dotsend(owl_editwin *e) |
---|
| 1041 | { |
---|
[47519e1b] | 1042 | char *p, *p_n, *p_p; |
---|
| 1043 | gunichar c; |
---|
[7d4fbcd] | 1044 | |
---|
| 1045 | if (!e->dotsend) return(0); |
---|
[47519e1b] | 1046 | |
---|
| 1047 | p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen); |
---|
| 1048 | p_n = g_utf8_find_next_char(p, NULL); |
---|
| 1049 | p_p = g_utf8_find_prev_char(e->buff, p); |
---|
| 1050 | c = g_utf8_get_char(p); |
---|
| 1051 | while (p != NULL) { |
---|
| 1052 | if (*p == '.' |
---|
| 1053 | && p_p != NULL && (*p_p == '\n' || *p_p == '\r') |
---|
| 1054 | && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) { |
---|
| 1055 | e->bufflen = p - e->buff; |
---|
| 1056 | e->buff[e->bufflen] = '\0'; |
---|
[7d4fbcd] | 1057 | return(1); |
---|
| 1058 | } |
---|
[47519e1b] | 1059 | if (c != '\0' && !g_unichar_isspace(c)) return(0); |
---|
| 1060 | p_n = p; |
---|
| 1061 | p = p_p; |
---|
| 1062 | c = g_utf8_get_char(p); |
---|
| 1063 | p_p = g_utf8_find_prev_char(e->buff, p); |
---|
[7d4fbcd] | 1064 | } |
---|
| 1065 | return(0); |
---|
| 1066 | } |
---|
| 1067 | |
---|
[47519e1b] | 1068 | void owl_editwin_post_process_char(owl_editwin *e, gunichar j) |
---|
[c9334b1] | 1069 | { |
---|
[7d4fbcd] | 1070 | /* check if we need to scroll down */ |
---|
| 1071 | if (e->buffy-e->topline >= e->winlines) { |
---|
| 1072 | e->topline+=e->winlines/2; |
---|
| 1073 | } |
---|
| 1074 | if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) { |
---|
| 1075 | owl_command_editmulti_done(e); |
---|
| 1076 | return; |
---|
| 1077 | } |
---|
| 1078 | owl_editwin_redisplay(e, 0); |
---|
| 1079 | } |
---|
| 1080 | |
---|
[47519e1b] | 1081 | void owl_editwin_process_char(owl_editwin *e, gunichar j) |
---|
[c9334b1] | 1082 | { |
---|
[7d4fbcd] | 1083 | if (j == ERR) return; |
---|
[47519e1b] | 1084 | if (g_unichar_iscntrl(j) && (j != 10) && (j != 13)) { |
---|
[7d4fbcd] | 1085 | return; |
---|
[47519e1b] | 1086 | } |
---|
| 1087 | else { |
---|
[7d4fbcd] | 1088 | owl_editwin_insert_char(e, j); |
---|
| 1089 | } |
---|
| 1090 | } |
---|
| 1091 | |
---|
[c9334b1] | 1092 | char *owl_editwin_get_text(owl_editwin *e) |
---|
| 1093 | { |
---|
[7d4fbcd] | 1094 | return(e->buff+e->lock); |
---|
| 1095 | } |
---|
| 1096 | |
---|
[c9334b1] | 1097 | int owl_editwin_get_numchars_on_line(owl_editwin *e, int line) |
---|
| 1098 | { |
---|
[7d4fbcd] | 1099 | int i; |
---|
| 1100 | char *ptr1, *ptr2; |
---|
| 1101 | |
---|
| 1102 | if (e->bufflen==0) return(0); |
---|
| 1103 | |
---|
| 1104 | /* first go to the yth line */ |
---|
| 1105 | ptr1=e->buff; |
---|
| 1106 | for (i=0; i<line; i++) { |
---|
| 1107 | ptr2=strchr(ptr1, '\n'); |
---|
| 1108 | if (!ptr2) { |
---|
| 1109 | /* we're already on the last line */ |
---|
| 1110 | return(0); |
---|
| 1111 | } |
---|
| 1112 | ptr1=ptr2+1; |
---|
| 1113 | } |
---|
| 1114 | |
---|
[47519e1b] | 1115 | /* now count characters */ |
---|
| 1116 | i = 0; |
---|
| 1117 | ptr2 = ptr1; |
---|
| 1118 | while (ptr2 - e->buff < e->bufflen |
---|
| 1119 | && *ptr2 != '\n') { |
---|
| 1120 | ++i; |
---|
| 1121 | ptr2 = g_utf8_next_char(ptr2); |
---|
| 1122 | } |
---|
| 1123 | return i; |
---|
| 1124 | } |
---|
| 1125 | |
---|
| 1126 | int owl_editwin_get_numcells_on_line(owl_editwin *e, int line) |
---|
| 1127 | { |
---|
| 1128 | int i; |
---|
| 1129 | char *ptr1, *ptr2; |
---|
| 1130 | gunichar c; |
---|
| 1131 | |
---|
| 1132 | if (e->bufflen==0) return(0); |
---|
| 1133 | |
---|
| 1134 | /* first go to the yth line */ |
---|
| 1135 | ptr1=e->buff; |
---|
| 1136 | for (i=0; i<line; i++) { |
---|
| 1137 | ptr2=strchr(ptr1, '\n'); |
---|
| 1138 | if (!ptr2) { |
---|
| 1139 | /* we're already on the last line */ |
---|
| 1140 | return(0); |
---|
| 1141 | } |
---|
| 1142 | ptr1=ptr2+1; |
---|
| 1143 | } |
---|
| 1144 | |
---|
| 1145 | /* now count cells */ |
---|
| 1146 | i = 0; |
---|
| 1147 | ptr2 = ptr1; |
---|
| 1148 | while (ptr2 - e->buff < e->bufflen |
---|
| 1149 | && *ptr2 != '\n') { |
---|
| 1150 | c = g_utf8_get_char(ptr2); |
---|
| 1151 | i += mk_wcwidth(c); |
---|
| 1152 | ptr2 = g_utf8_next_char(ptr2); |
---|
[7d4fbcd] | 1153 | } |
---|
[47519e1b] | 1154 | return i; |
---|
[7d4fbcd] | 1155 | } |
---|
| 1156 | |
---|
[c9334b1] | 1157 | int owl_editwin_get_numlines(owl_editwin *e) |
---|
| 1158 | { |
---|
[7d4fbcd] | 1159 | return(owl_text_num_lines(e->buff)); |
---|
| 1160 | } |
---|
| 1161 | |
---|