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