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