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