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