| 1 | #include "owl.h" |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <unistd.h> |
|---|
| 4 | #include <string.h> |
|---|
| 5 | #include <ctype.h> |
|---|
| 6 | |
|---|
| 7 | #define VALID_EXCURSION (0x9a2b4729) |
|---|
| 8 | |
|---|
| 9 | typedef struct _owl_editwin_excursion { /*noproto*/ |
|---|
| 10 | int valid; |
|---|
| 11 | int index; |
|---|
| 12 | int mark; |
|---|
| 13 | int goal_column; |
|---|
| 14 | int lock; |
|---|
| 15 | struct _owl_editwin_excursion *next; |
|---|
| 16 | } oe_excursion; |
|---|
| 17 | |
|---|
| 18 | struct _owl_editwin { /*noproto*/ |
|---|
| 19 | int refcount; |
|---|
| 20 | char *buff; |
|---|
| 21 | owl_history *hist; |
|---|
| 22 | int bufflen; |
|---|
| 23 | int allocated; |
|---|
| 24 | int index; |
|---|
| 25 | int mark; |
|---|
| 26 | char *killbuf; |
|---|
| 27 | int goal_column; |
|---|
| 28 | int topindex; |
|---|
| 29 | int column; |
|---|
| 30 | int winlines, wincols, fillcol, wrapcol; |
|---|
| 31 | owl_window *win; |
|---|
| 32 | gulong repaint_id; |
|---|
| 33 | gulong resized_id; |
|---|
| 34 | int style; |
|---|
| 35 | int lock; |
|---|
| 36 | int dotsend; |
|---|
| 37 | int echochar; |
|---|
| 38 | oe_excursion *excursions; |
|---|
| 39 | |
|---|
| 40 | void (*callback)(struct _owl_editwin*); |
|---|
| 41 | void (*destroy_cbdata)(void *); |
|---|
| 42 | void *cbdata; |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | static void oe_set_window(owl_editwin *e, owl_window *w, int winlines, int wincols); |
|---|
| 46 | static void oe_redraw(owl_window *win, WINDOW *curswin, void *user_data); |
|---|
| 47 | static void oe_reframe(owl_editwin *e); |
|---|
| 48 | static void oe_save_excursion(owl_editwin *e, oe_excursion *x); |
|---|
| 49 | static void oe_release_excursion(owl_editwin *e, oe_excursion *x); |
|---|
| 50 | static void oe_restore_excursion(owl_editwin *e, oe_excursion *x); |
|---|
| 51 | static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x); |
|---|
| 52 | static int oe_char_width(gunichar c, int column); |
|---|
| 53 | static int oe_region_column(owl_editwin *e, int start, int end, int offset); |
|---|
| 54 | static int oe_region_width(owl_editwin *e, int start, int end, int offset); |
|---|
| 55 | static int oe_find_display_line(owl_editwin *e, int *x, int index, int *hard); |
|---|
| 56 | static void oe_insert_char(owl_editwin *e, gunichar c); |
|---|
| 57 | static int owl_editwin_limit_maxcols(int width, int cols); |
|---|
| 58 | static int owl_editwin_check_dotsend(owl_editwin *e); |
|---|
| 59 | static int owl_editwin_is_char_in(owl_editwin *e, const char *set); |
|---|
| 60 | static gunichar owl_editwin_get_char_at_point(owl_editwin *e); |
|---|
| 61 | static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s); |
|---|
| 62 | static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len); |
|---|
| 63 | static int oe_copy_region(owl_editwin *e); |
|---|
| 64 | static char *oe_chunk(owl_editwin *e, int start, int end); |
|---|
| 65 | static void oe_destroy_cbdata(owl_editwin *e); |
|---|
| 66 | static void oe_dirty(owl_editwin *e); |
|---|
| 67 | static void oe_window_resized(owl_window *w, owl_editwin *e); |
|---|
| 68 | |
|---|
| 69 | #define INCR 4096 |
|---|
| 70 | |
|---|
| 71 | #define WHITESPACE " \n\t" |
|---|
| 72 | |
|---|
| 73 | static owl_editwin *owl_editwin_allocate(void) |
|---|
| 74 | { |
|---|
| 75 | owl_editwin *e; |
|---|
| 76 | e = owl_malloc(sizeof(owl_editwin)); |
|---|
| 77 | memset(e, 0, sizeof(*e)); |
|---|
| 78 | e->refcount = 1; |
|---|
| 79 | return e; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | static void _owl_editwin_delete(owl_editwin *e) |
|---|
| 83 | { |
|---|
| 84 | if (e->win) { |
|---|
| 85 | g_signal_handler_disconnect(e->win, e->repaint_id); |
|---|
| 86 | g_signal_handler_disconnect(e->win, e->resized_id); |
|---|
| 87 | g_object_unref(e->win); |
|---|
| 88 | } |
|---|
| 89 | owl_free(e->buff); |
|---|
| 90 | owl_free(e->killbuf); |
|---|
| 91 | /* just in case someone forgot to clean up */ |
|---|
| 92 | while (e->excursions) { |
|---|
| 93 | oe_release_excursion(e, e->excursions); |
|---|
| 94 | } |
|---|
| 95 | oe_destroy_cbdata(e); |
|---|
| 96 | |
|---|
| 97 | owl_free(e); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | static inline void oe_set_index(owl_editwin *e, int index) |
|---|
| 101 | { |
|---|
| 102 | if (index != e->index) { |
|---|
| 103 | e->goal_column = -1; |
|---|
| 104 | e->column = -1; |
|---|
| 105 | } |
|---|
| 106 | e->index = index; |
|---|
| 107 | oe_dirty(e); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | static inline void oe_set_mark(owl_editwin *e, int mark) |
|---|
| 111 | { |
|---|
| 112 | e->mark = mark; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void owl_editwin_set_mark(owl_editwin *e) |
|---|
| 116 | { |
|---|
| 117 | oe_set_mark(e, e->index); |
|---|
| 118 | /* owl_function_makemsg("Mark set."); */ |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | static void _owl_editwin_init(owl_editwin *e, |
|---|
| 122 | int winlines, |
|---|
| 123 | int wincols, |
|---|
| 124 | int style, |
|---|
| 125 | owl_history *hist) |
|---|
| 126 | { |
|---|
| 127 | e->buff=owl_malloc(INCR); |
|---|
| 128 | e->buff[0]='\0'; |
|---|
| 129 | e->bufflen=0; |
|---|
| 130 | e->hist=hist; |
|---|
| 131 | e->allocated=INCR; |
|---|
| 132 | oe_set_index(e, 0); |
|---|
| 133 | oe_set_mark(e, -1); |
|---|
| 134 | if (e->killbuf != NULL) |
|---|
| 135 | owl_free(e->killbuf); |
|---|
| 136 | e->killbuf = NULL; |
|---|
| 137 | e->goal_column = -1; |
|---|
| 138 | e->column = -1; |
|---|
| 139 | e->topindex = 0; |
|---|
| 140 | e->excursions = NULL; |
|---|
| 141 | e->style=style; |
|---|
| 142 | if ((style!=OWL_EDITWIN_STYLE_MULTILINE) && |
|---|
| 143 | (style!=OWL_EDITWIN_STYLE_ONELINE)) { |
|---|
| 144 | e->style=OWL_EDITWIN_STYLE_MULTILINE; |
|---|
| 145 | } |
|---|
| 146 | e->lock=0; |
|---|
| 147 | e->dotsend=0; |
|---|
| 148 | e->echochar='\0'; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | owl_editwin *owl_editwin_new(owl_window *win, int winlines, int wincols, int style, owl_history *hist) |
|---|
| 152 | { |
|---|
| 153 | owl_editwin *e = owl_editwin_allocate(); |
|---|
| 154 | |
|---|
| 155 | _owl_editwin_init(e, winlines, wincols, style, hist); |
|---|
| 156 | oe_set_window(e, win, winlines, wincols); |
|---|
| 157 | return e; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | owl_editwin *owl_editwin_ref(owl_editwin *e) |
|---|
| 161 | { |
|---|
| 162 | e->refcount++; |
|---|
| 163 | return e; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | void owl_editwin_unref(owl_editwin *e) |
|---|
| 167 | { |
|---|
| 168 | e->refcount--; |
|---|
| 169 | if (e->refcount <= 0) |
|---|
| 170 | _owl_editwin_delete(e); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /* TODO: collapse this window into oe_window_resized. Need to stop |
|---|
| 174 | * passing winlines and wincols to oe_set_window and get them out of |
|---|
| 175 | * the owl_window. The tester code will first need to pass in an |
|---|
| 176 | * owl_window headless or so. */ |
|---|
| 177 | static void oe_set_window_size(owl_editwin *e, int winlines, int wincols) |
|---|
| 178 | { |
|---|
| 179 | e->winlines=winlines; |
|---|
| 180 | e->wincols=wincols; |
|---|
| 181 | /* fillcol and wrapcol may have changed. */ |
|---|
| 182 | e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g)); |
|---|
| 183 | if (e->style == OWL_EDITWIN_STYLE_MULTILINE) |
|---|
| 184 | e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g)); |
|---|
| 185 | else |
|---|
| 186 | e->wrapcol = 0; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | static void oe_window_resized(owl_window *w, owl_editwin *e) |
|---|
| 190 | { |
|---|
| 191 | int winlines, wincols; |
|---|
| 192 | owl_window_get_position(w, &winlines, &wincols, NULL, NULL); |
|---|
| 193 | oe_set_window_size(e, winlines, wincols); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | static void oe_set_window(owl_editwin *e, owl_window *w, int winlines, int wincols) |
|---|
| 197 | { |
|---|
| 198 | e->win=w; |
|---|
| 199 | oe_set_window_size(e, winlines, wincols); |
|---|
| 200 | if (e->win) { |
|---|
| 201 | g_object_ref(e->win); |
|---|
| 202 | e->repaint_id = g_signal_connect(w, "redraw", G_CALLBACK(oe_redraw), e); |
|---|
| 203 | e->resized_id = g_signal_connect(w, "resized", G_CALLBACK(oe_window_resized), e); |
|---|
| 204 | owl_window_dirty(e->win); |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | owl_window *owl_editwin_get_window(owl_editwin *e) |
|---|
| 209 | { |
|---|
| 210 | return e->win; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | /* echo the character 'ch' for each normal character keystroke, |
|---|
| 214 | * excepting locktext. This is useful for entering passwords etc. If |
|---|
| 215 | * ch=='\0' characters are echo'd normally |
|---|
| 216 | */ |
|---|
| 217 | void owl_editwin_set_echochar(owl_editwin *e, int ch) |
|---|
| 218 | { |
|---|
| 219 | e->echochar=ch; |
|---|
| 220 | oe_dirty(e); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | owl_history *owl_editwin_get_history(owl_editwin *e) |
|---|
| 224 | { |
|---|
| 225 | return(e->hist); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | void owl_editwin_set_dotsend(owl_editwin *e) |
|---|
| 229 | { |
|---|
| 230 | e->dotsend=1; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | void owl_editwin_set_callback(owl_editwin *e, void (*cb)(owl_editwin*)) |
|---|
| 234 | { |
|---|
| 235 | e->callback = cb; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | void (*owl_editwin_get_callback(owl_editwin *e))(owl_editwin*) |
|---|
| 239 | { |
|---|
| 240 | return e->callback; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | static void oe_destroy_cbdata(owl_editwin *e) { |
|---|
| 244 | if (e->destroy_cbdata) |
|---|
| 245 | e->destroy_cbdata(e->cbdata); |
|---|
| 246 | e->cbdata = NULL; |
|---|
| 247 | e->destroy_cbdata = NULL; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | void owl_editwin_set_cbdata(owl_editwin *e, void *data, void (*destroy)(void *)) |
|---|
| 251 | { |
|---|
| 252 | oe_destroy_cbdata(e); |
|---|
| 253 | e->cbdata = data; |
|---|
| 254 | e->destroy_cbdata = destroy; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | void *owl_editwin_get_cbdata(owl_editwin *e) { |
|---|
| 258 | return e->cbdata; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | void owl_editwin_do_callback(owl_editwin *e) { |
|---|
| 262 | void (*cb)(owl_editwin*); |
|---|
| 263 | cb=owl_editwin_get_callback(e); |
|---|
| 264 | if(!cb) { |
|---|
| 265 | owl_function_error("Internal error: No editwin callback!"); |
|---|
| 266 | } else { |
|---|
| 267 | /* owl_function_error("text: |%s|", owl_editwin_get_text(e)); */ |
|---|
| 268 | cb(e); |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | static int owl_editwin_limit_maxcols(int width, int cols) |
|---|
| 273 | { |
|---|
| 274 | if (cols == 0) |
|---|
| 275 | return width; |
|---|
| 276 | return cols; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | /* set text to be 'locked in' at the beginning of the buffer, any |
|---|
| 280 | * previous text (including locked text) will be overwritten |
|---|
| 281 | */ |
|---|
| 282 | void owl_editwin_set_locktext(owl_editwin *e, const char *text) |
|---|
| 283 | { |
|---|
| 284 | oe_set_index(e, 0); |
|---|
| 285 | e->lock = 0; |
|---|
| 286 | owl_editwin_replace(e, e->bufflen, text); |
|---|
| 287 | e->buff[e->bufflen] = 0; |
|---|
| 288 | e->lock=e->bufflen; |
|---|
| 289 | oe_set_index(e, e->lock); |
|---|
| 290 | oe_dirty(e); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | int owl_editwin_get_style(owl_editwin *e) |
|---|
| 294 | { |
|---|
| 295 | return(e->style); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /* clear all text except for locktext and put the cursor at the |
|---|
| 299 | * beginning |
|---|
| 300 | */ |
|---|
| 301 | void owl_editwin_clear(owl_editwin *e) |
|---|
| 302 | { |
|---|
| 303 | |
|---|
| 304 | int lock = e->lock; |
|---|
| 305 | int dotsend=e->dotsend; |
|---|
| 306 | char *locktext=NULL; |
|---|
| 307 | char echochar=e->echochar; |
|---|
| 308 | |
|---|
| 309 | if (lock > 0) { |
|---|
| 310 | locktext = owl_malloc(lock+1); |
|---|
| 311 | strncpy(locktext, e->buff, lock); |
|---|
| 312 | locktext[lock] = 0; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | owl_free(e->buff); |
|---|
| 316 | _owl_editwin_init(e, e->winlines, e->wincols, e->style, e->hist); |
|---|
| 317 | |
|---|
| 318 | if (lock > 0) { |
|---|
| 319 | owl_editwin_set_locktext(e, locktext); |
|---|
| 320 | } |
|---|
| 321 | if (dotsend) { |
|---|
| 322 | owl_editwin_set_dotsend(e); |
|---|
| 323 | } |
|---|
| 324 | if (echochar) { |
|---|
| 325 | owl_editwin_set_echochar(e, echochar); |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | if (locktext) |
|---|
| 329 | owl_free(locktext); |
|---|
| 330 | |
|---|
| 331 | oe_set_index(e, lock); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | void owl_editwin_recenter(owl_editwin *e) |
|---|
| 335 | { |
|---|
| 336 | e->topindex = -1; |
|---|
| 337 | oe_dirty(e); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | static void oe_save_excursion(owl_editwin *e, oe_excursion *x) |
|---|
| 341 | { |
|---|
| 342 | x->index = e->index; |
|---|
| 343 | x->mark = e->mark; |
|---|
| 344 | x->goal_column = e->goal_column; |
|---|
| 345 | x->lock = e->lock; |
|---|
| 346 | |
|---|
| 347 | x->valid = VALID_EXCURSION; |
|---|
| 348 | x->next = e->excursions; |
|---|
| 349 | e->excursions = x; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | static void oe_release_excursion(owl_editwin *e, oe_excursion *x) |
|---|
| 353 | { |
|---|
| 354 | oe_excursion **px; |
|---|
| 355 | |
|---|
| 356 | x->valid = 0; |
|---|
| 357 | for (px = &e->excursions; *px != NULL; px = &(*px)->next) |
|---|
| 358 | if (*px == x) { |
|---|
| 359 | *px = x->next; |
|---|
| 360 | return; |
|---|
| 361 | } |
|---|
| 362 | abort(); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | static void oe_restore_excursion(owl_editwin *e, oe_excursion *x) |
|---|
| 366 | { |
|---|
| 367 | if (x->valid == VALID_EXCURSION) { |
|---|
| 368 | oe_set_index(e, x->index); |
|---|
| 369 | e->goal_column = x->goal_column; |
|---|
| 370 | e->mark = x->mark; |
|---|
| 371 | e->lock = x->lock; |
|---|
| 372 | |
|---|
| 373 | oe_release_excursion(e, x); |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x) |
|---|
| 378 | { |
|---|
| 379 | if (x->valid == VALID_EXCURSION) { |
|---|
| 380 | e->mark = x->mark; |
|---|
| 381 | |
|---|
| 382 | oe_release_excursion(e, x); |
|---|
| 383 | } |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | /* External interface to oe_save_excursion */ |
|---|
| 387 | owl_editwin_excursion *owl_editwin_begin_excursion(owl_editwin *e) |
|---|
| 388 | { |
|---|
| 389 | owl_editwin_excursion *x = owl_malloc(sizeof *x); |
|---|
| 390 | oe_save_excursion(e, x); |
|---|
| 391 | return x; |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | void owl_editwin_end_excursion(owl_editwin *e, owl_editwin_excursion *x) |
|---|
| 395 | { |
|---|
| 396 | oe_restore_excursion(e, x); |
|---|
| 397 | owl_free(x); |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | static inline const char *oe_next_point(owl_editwin *e, const char *p) |
|---|
| 401 | { |
|---|
| 402 | const char *boundary = e->buff + e->bufflen + 1; |
|---|
| 403 | const char *q; |
|---|
| 404 | |
|---|
| 405 | q = g_utf8_find_next_char(p, boundary); |
|---|
| 406 | while (q && g_unichar_ismark(g_utf8_get_char(q))) |
|---|
| 407 | q = g_utf8_find_next_char(q, boundary); |
|---|
| 408 | |
|---|
| 409 | if (q == p) |
|---|
| 410 | return NULL; |
|---|
| 411 | return q; |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | static inline const char *oe_prev_point(owl_editwin *e, const char *p) |
|---|
| 415 | { |
|---|
| 416 | const char *boundary = e->buff + e->lock; |
|---|
| 417 | |
|---|
| 418 | p = g_utf8_find_prev_char(boundary, p); |
|---|
| 419 | while (p && g_unichar_ismark(g_utf8_get_char(p))) |
|---|
| 420 | p = g_utf8_find_prev_char(boundary, p); |
|---|
| 421 | |
|---|
| 422 | return p; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | static int oe_char_width(gunichar c, int column) |
|---|
| 426 | { |
|---|
| 427 | int cw; |
|---|
| 428 | |
|---|
| 429 | if (c == 9) /* TAB */ |
|---|
| 430 | return TABSIZE - column % TABSIZE; |
|---|
| 431 | |
|---|
| 432 | cw = mk_wcwidth(c); |
|---|
| 433 | |
|---|
| 434 | if (cw < 0) /* control characters */ |
|---|
| 435 | cw = 0; |
|---|
| 436 | |
|---|
| 437 | return cw; |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | /* Finds the display line of 'e' starting at the character |
|---|
| 441 | * 'index'. The index just after the line is returned. Whether the |
|---|
| 442 | * line ends at a hard break (newline) or soft break (wrapping) is |
|---|
| 443 | * returned in 'hard'. |
|---|
| 444 | * |
|---|
| 445 | * If the point (e->index) is contained in the line, its position is |
|---|
| 446 | * returned in 'x'. |
|---|
| 447 | */ |
|---|
| 448 | static int oe_find_display_line(owl_editwin *e, int *x, int index, int *hard) |
|---|
| 449 | { |
|---|
| 450 | int width = 0, cw; |
|---|
| 451 | gunichar c; |
|---|
| 452 | const char *p; |
|---|
| 453 | |
|---|
| 454 | while(1) { |
|---|
| 455 | /* note the position of the dot */ |
|---|
| 456 | if (x != NULL && index == e->index && width < e->wincols) |
|---|
| 457 | *x = width; |
|---|
| 458 | |
|---|
| 459 | /* get the current character */ |
|---|
| 460 | c = g_utf8_get_char(e->buff + index); |
|---|
| 461 | |
|---|
| 462 | /* figure out how wide it is */ |
|---|
| 463 | cw = oe_char_width(c, width); |
|---|
| 464 | |
|---|
| 465 | if (width + cw > e->wincols - 1) { |
|---|
| 466 | if (x != NULL && *x == width) |
|---|
| 467 | *x = -1; |
|---|
| 468 | if (hard != NULL) *hard = 0; |
|---|
| 469 | break; |
|---|
| 470 | } |
|---|
| 471 | width += cw; |
|---|
| 472 | |
|---|
| 473 | if (c == '\n') { |
|---|
| 474 | if (width < e->wincols) |
|---|
| 475 | ++index; /* skip the newline */ |
|---|
| 476 | if (hard != NULL) *hard = 1; |
|---|
| 477 | break; |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | /* find the next character */ |
|---|
| 481 | p = oe_next_point(e, e->buff + index); |
|---|
| 482 | if (p == NULL) { /* we ran off the end */ |
|---|
| 483 | if (x != NULL && e->index > index) |
|---|
| 484 | *x = width + 1; |
|---|
| 485 | if (hard != NULL) *hard = 1; |
|---|
| 486 | break; |
|---|
| 487 | } |
|---|
| 488 | index = p - e->buff; |
|---|
| 489 | |
|---|
| 490 | } |
|---|
| 491 | return index; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | static void oe_reframe(owl_editwin *e) { |
|---|
| 495 | oe_excursion x; |
|---|
| 496 | int goal = 1 + e->winlines / 2; |
|---|
| 497 | int index; |
|---|
| 498 | int count = 0; |
|---|
| 499 | int n, i; |
|---|
| 500 | int last; |
|---|
| 501 | |
|---|
| 502 | oe_save_excursion(e, &x); |
|---|
| 503 | /* step back line-by-line through the buffer until we have >= goal lines of |
|---|
| 504 | display text */ |
|---|
| 505 | e->lock = 0; /* we can (must) tread on the locktext */ |
|---|
| 506 | |
|---|
| 507 | last = -1; |
|---|
| 508 | while (count < goal) { |
|---|
| 509 | index = e->index; |
|---|
| 510 | owl_editwin_move_to_beginning_of_line(e); |
|---|
| 511 | if (last == e->index) |
|---|
| 512 | break; |
|---|
| 513 | last = e->index; |
|---|
| 514 | for (n = 0, i = e->index; i < index; n++) |
|---|
| 515 | i = oe_find_display_line(e, NULL, i, NULL); |
|---|
| 516 | count += n == 0 ? 1 : n; |
|---|
| 517 | if (count < goal) |
|---|
| 518 | owl_editwin_point_move(e, -1); |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | e->topindex = e->index; |
|---|
| 522 | /* if we overshot, backtrack */ |
|---|
| 523 | for (n = 0; n < (count - goal); n++) |
|---|
| 524 | e->topindex = oe_find_display_line(e, NULL, e->topindex, NULL); |
|---|
| 525 | |
|---|
| 526 | oe_restore_excursion(e, &x); |
|---|
| 527 | oe_dirty(e); |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | static void oe_addnec(owl_editwin *e, WINDOW *curswin, int count) |
|---|
| 531 | { |
|---|
| 532 | int i; |
|---|
| 533 | |
|---|
| 534 | for (i = 0; i < count; i++) |
|---|
| 535 | waddch(curswin, e->echochar); |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | static void oe_mvaddnec(owl_editwin *e, WINDOW *curswin, int y, int x, int count) |
|---|
| 539 | { |
|---|
| 540 | wmove(curswin, y, x); |
|---|
| 541 | oe_addnec(e, curswin, count); |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | /* regenerate the text on the curses window */ |
|---|
| 545 | static void oe_redraw(owl_window *win, WINDOW *curswin, void *user_data) |
|---|
| 546 | { |
|---|
| 547 | int x = -1, y = -1, t, hard; |
|---|
| 548 | int line, index, lineindex, times = 0; |
|---|
| 549 | owl_editwin *e = user_data; |
|---|
| 550 | |
|---|
| 551 | do { |
|---|
| 552 | werase(curswin); |
|---|
| 553 | |
|---|
| 554 | if (e->topindex == -1 || e->index < e->topindex) |
|---|
| 555 | oe_reframe(e); |
|---|
| 556 | |
|---|
| 557 | line = 0; |
|---|
| 558 | index = e->topindex; |
|---|
| 559 | while(line < e->winlines) { |
|---|
| 560 | lineindex = index; |
|---|
| 561 | t = -1; |
|---|
| 562 | index = oe_find_display_line(e, &t, lineindex, &hard); |
|---|
| 563 | if (x == -1 && t != -1) |
|---|
| 564 | x = t, y = line; |
|---|
| 565 | if (index - lineindex) { |
|---|
| 566 | if (!e->echochar) |
|---|
| 567 | mvwaddnstr(curswin, line, 0, |
|---|
| 568 | e->buff + lineindex, |
|---|
| 569 | index - lineindex); |
|---|
| 570 | else { |
|---|
| 571 | if(lineindex < e->lock) { |
|---|
| 572 | mvwaddnstr(curswin, line, 0, |
|---|
| 573 | e->buff + lineindex, |
|---|
| 574 | MIN(index - lineindex, |
|---|
| 575 | e->lock - lineindex)); |
|---|
| 576 | if (e->lock < index) |
|---|
| 577 | oe_addnec(e, curswin, |
|---|
| 578 | oe_region_width(e, e->lock, index, |
|---|
| 579 | oe_region_width(e, lineindex, e->lock, 0))); |
|---|
| 580 | } else |
|---|
| 581 | oe_mvaddnec(e, curswin, line, 0, oe_region_width(e, lineindex, index, 0)); |
|---|
| 582 | } |
|---|
| 583 | if (!hard) |
|---|
| 584 | waddch(curswin, '\\'); |
|---|
| 585 | } |
|---|
| 586 | line++; |
|---|
| 587 | } |
|---|
| 588 | if (x == -1) |
|---|
| 589 | e->topindex = -1; /* force a reframe */ |
|---|
| 590 | times++; |
|---|
| 591 | } while(x == -1 && times < 3); |
|---|
| 592 | |
|---|
| 593 | wmove(curswin, y, x); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | static inline void oe_fixup(int *target, int start, int end, int change) { |
|---|
| 597 | if (*target > start) { |
|---|
| 598 | if (*target <= end) |
|---|
| 599 | *target = end + change; |
|---|
| 600 | else |
|---|
| 601 | *target += change; |
|---|
| 602 | } |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | int owl_editwin_replace_region(owl_editwin *e, const char *s) |
|---|
| 606 | { |
|---|
| 607 | oe_excursion x; |
|---|
| 608 | int ret; |
|---|
| 609 | |
|---|
| 610 | if (e->mark == -1) { |
|---|
| 611 | owl_function_error("The mark is unset, there is no region to replace."); |
|---|
| 612 | return 0; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | oe_save_excursion(e, &x); |
|---|
| 616 | |
|---|
| 617 | if(e->index > e->mark) { |
|---|
| 618 | owl_editwin_exchange_point_and_mark(e); |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | ret = owl_editwin_replace_internal(e, e->mark - e->index, s); |
|---|
| 622 | |
|---|
| 623 | oe_restore_excursion(e, &x); |
|---|
| 624 | |
|---|
| 625 | return ret; |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | /* replace 'replace' characters at the point with s, returning the change in size */ |
|---|
| 629 | int owl_editwin_replace(owl_editwin *e, int replace, const char *s) |
|---|
| 630 | { |
|---|
| 631 | int start, end, i; |
|---|
| 632 | const char *p; |
|---|
| 633 | |
|---|
| 634 | if (!g_utf8_validate(s, -1, NULL)) { |
|---|
| 635 | owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string."); |
|---|
| 636 | return 0; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | start = e->index; |
|---|
| 640 | for (i = 0, p = e->buff + start; i < replace && p != NULL; i++) |
|---|
| 641 | p = oe_next_point(e, p); |
|---|
| 642 | if (p != NULL) |
|---|
| 643 | end = p - e->buff; |
|---|
| 644 | else |
|---|
| 645 | end = e->bufflen; |
|---|
| 646 | |
|---|
| 647 | return owl_editwin_replace_internal(e, end - start, s); |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s) |
|---|
| 651 | { |
|---|
| 652 | int start, end, free, need, size, change, oldindex; |
|---|
| 653 | oe_excursion *x; |
|---|
| 654 | |
|---|
| 655 | start = e->index; |
|---|
| 656 | end = start + replace; |
|---|
| 657 | |
|---|
| 658 | free = e->allocated - e->bufflen + end - start; |
|---|
| 659 | |
|---|
| 660 | need = strlen(s) - free; |
|---|
| 661 | if (need > 0) { |
|---|
| 662 | size = e->allocated + need + INCR - (need % INCR); |
|---|
| 663 | e->buff = owl_realloc(e->buff, size); |
|---|
| 664 | e->allocated = size; |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end); |
|---|
| 668 | memcpy(e->buff + start, s, strlen(s)); |
|---|
| 669 | change = start - end + strlen(s); |
|---|
| 670 | e->bufflen += change; |
|---|
| 671 | oldindex = e->index; |
|---|
| 672 | e->index += strlen(s); |
|---|
| 673 | if (e->column != -1) |
|---|
| 674 | e->column = oe_region_column(e, oldindex, e->index, e->column); |
|---|
| 675 | |
|---|
| 676 | /* fix up the mark */ |
|---|
| 677 | oe_fixup(&e->mark, start, end, change); |
|---|
| 678 | oe_fixup(&e->topindex, start, end, change); |
|---|
| 679 | /* fix up any saved points after the replaced area */ |
|---|
| 680 | for (x = e->excursions; x != NULL; x = x->next) { |
|---|
| 681 | oe_fixup(&x->index, start, end, change); |
|---|
| 682 | oe_fixup(&x->mark, start, end, change); |
|---|
| 683 | } |
|---|
| 684 | |
|---|
| 685 | /* recenter if needed */ |
|---|
| 686 | if (start <= e->topindex) |
|---|
| 687 | owl_editwin_recenter(e); |
|---|
| 688 | |
|---|
| 689 | oe_dirty(e); |
|---|
| 690 | |
|---|
| 691 | return change; |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | /* linewrap the word just before the cursor. |
|---|
| 695 | * returns 0 on success |
|---|
| 696 | * returns -1 if we could not wrap. |
|---|
| 697 | */ |
|---|
| 698 | static void _owl_editwin_linewrap_word(owl_editwin *e) |
|---|
| 699 | { |
|---|
| 700 | oe_excursion x; |
|---|
| 701 | gunichar c; |
|---|
| 702 | |
|---|
| 703 | oe_save_excursion(e, &x); |
|---|
| 704 | |
|---|
| 705 | while (owl_editwin_point_move(e, -1)) { |
|---|
| 706 | c = owl_editwin_get_char_at_point(e); |
|---|
| 707 | if (owl_util_can_break_after(c) || c == '\n') { |
|---|
| 708 | if (c != '\n') |
|---|
| 709 | owl_editwin_replace(e, c != ' ' ? 0 : 1, "\n"); |
|---|
| 710 | break; |
|---|
| 711 | } |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | oe_restore_excursion(e, &x); |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | /* delete the character at the current point, following chars |
|---|
| 718 | * shift left. |
|---|
| 719 | */ |
|---|
| 720 | void owl_editwin_delete_char(owl_editwin *e) |
|---|
| 721 | { |
|---|
| 722 | owl_editwin_replace(e, 1, ""); |
|---|
| 723 | } |
|---|
| 724 | |
|---|
| 725 | /* Swap the character at point with the character at point-1 and |
|---|
| 726 | * advance the pointer. If point is at beginning of buffer do |
|---|
| 727 | * nothing. If point is after the last character swap point-1 with |
|---|
| 728 | * point-2. (Behaves as observed in tcsh and emacs). |
|---|
| 729 | */ |
|---|
| 730 | void owl_editwin_transpose_chars(owl_editwin *e) |
|---|
| 731 | { |
|---|
| 732 | const char *middle, *end, *start; |
|---|
| 733 | char *tmp; |
|---|
| 734 | |
|---|
| 735 | if (e->bufflen == 0) return; |
|---|
| 736 | |
|---|
| 737 | if (e->index == e->bufflen) |
|---|
| 738 | owl_editwin_point_move(e, -1); /* point is after last character */ |
|---|
| 739 | |
|---|
| 740 | if (owl_editwin_at_beginning_of_buffer(e)) |
|---|
| 741 | return; /* point is at beginning of buffer, do nothing */ |
|---|
| 742 | |
|---|
| 743 | /* Transpose two utf-8 unicode glyphs. */ |
|---|
| 744 | middle = e->buff + e->index; |
|---|
| 745 | |
|---|
| 746 | end = oe_next_point(e, middle); |
|---|
| 747 | if (end == NULL) |
|---|
| 748 | return; |
|---|
| 749 | |
|---|
| 750 | start = oe_prev_point(e, middle); |
|---|
| 751 | if (start == NULL) |
|---|
| 752 | return; |
|---|
| 753 | |
|---|
| 754 | tmp = owl_malloc((end - start) + 1); |
|---|
| 755 | tmp[(end - start)] = 0; |
|---|
| 756 | memcpy(tmp, middle, end - middle); |
|---|
| 757 | memcpy(tmp + (end - middle), start, middle - start); |
|---|
| 758 | |
|---|
| 759 | owl_editwin_point_move(e, -1); |
|---|
| 760 | owl_editwin_replace(e, 2, tmp); |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | /* insert 'string' at the current point, later text is shifted |
|---|
| 764 | * right |
|---|
| 765 | */ |
|---|
| 766 | void owl_editwin_insert_string(owl_editwin *e, const char *s) |
|---|
| 767 | { |
|---|
| 768 | owl_editwin_replace(e, 0, s); |
|---|
| 769 | } |
|---|
| 770 | |
|---|
| 771 | /* We assume index is not set to point to a mid-char */ |
|---|
| 772 | static gunichar owl_editwin_get_char_at_point(owl_editwin *e) |
|---|
| 773 | { |
|---|
| 774 | return g_utf8_get_char(e->buff + e->index); |
|---|
| 775 | } |
|---|
| 776 | |
|---|
| 777 | void owl_editwin_exchange_point_and_mark(owl_editwin *e) { |
|---|
| 778 | int tmp; |
|---|
| 779 | |
|---|
| 780 | if (e->mark != -1) { |
|---|
| 781 | tmp = e->mark; |
|---|
| 782 | owl_editwin_set_mark(e); |
|---|
| 783 | oe_set_index(e, tmp); |
|---|
| 784 | } |
|---|
| 785 | } |
|---|
| 786 | |
|---|
| 787 | int owl_editwin_point_move(owl_editwin *e, int delta) |
|---|
| 788 | { |
|---|
| 789 | const char *p; |
|---|
| 790 | int change, d = 0; |
|---|
| 791 | |
|---|
| 792 | change = MAX(delta, - delta); |
|---|
| 793 | p = e->buff + e->index; |
|---|
| 794 | |
|---|
| 795 | while (d < change && p != NULL) { |
|---|
| 796 | if (delta > 0) |
|---|
| 797 | p = oe_next_point(e, p); |
|---|
| 798 | else |
|---|
| 799 | p = oe_prev_point(e, p); |
|---|
| 800 | if (p != NULL) { |
|---|
| 801 | oe_set_index(e, p - e->buff); |
|---|
| 802 | d++; |
|---|
| 803 | } |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | return delta > 0 ? d : -d; |
|---|
| 807 | } |
|---|
| 808 | |
|---|
| 809 | int owl_editwin_at_beginning_of_buffer(owl_editwin *e) { |
|---|
| 810 | if (e->index == e->lock) |
|---|
| 811 | return 1; |
|---|
| 812 | |
|---|
| 813 | return 0; |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | int owl_at_end_of_buffer(owl_editwin *e) { |
|---|
| 817 | if (e->index == e->bufflen) |
|---|
| 818 | return 1; |
|---|
| 819 | |
|---|
| 820 | return 0; |
|---|
| 821 | } |
|---|
| 822 | |
|---|
| 823 | static int owl_editwin_at_beginning_of_line(owl_editwin *e) |
|---|
| 824 | { |
|---|
| 825 | oe_excursion x; |
|---|
| 826 | int ret; |
|---|
| 827 | |
|---|
| 828 | if (owl_editwin_at_beginning_of_buffer(e)) |
|---|
| 829 | return 1; |
|---|
| 830 | |
|---|
| 831 | oe_save_excursion(e, &x); |
|---|
| 832 | owl_editwin_point_move(e, -1); |
|---|
| 833 | ret = (owl_editwin_get_char_at_point(e) == '\n'); |
|---|
| 834 | oe_restore_excursion(e, &x); |
|---|
| 835 | |
|---|
| 836 | return ret; |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| 839 | static int owl_editwin_is_char_in(owl_editwin *e, const char *set) |
|---|
| 840 | { |
|---|
| 841 | const char *p; |
|---|
| 842 | |
|---|
| 843 | for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL)) |
|---|
| 844 | if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p)) |
|---|
| 845 | return 1; |
|---|
| 846 | return 0; |
|---|
| 847 | } |
|---|
| 848 | |
|---|
| 849 | int owl_editwin_move_if_in(owl_editwin *e, int delta, const char *set) |
|---|
| 850 | { |
|---|
| 851 | int change, distance = 0; |
|---|
| 852 | while (owl_editwin_is_char_in(e, set)) { |
|---|
| 853 | change = owl_editwin_point_move(e, delta); |
|---|
| 854 | distance += change; |
|---|
| 855 | if (change == 0) |
|---|
| 856 | break; |
|---|
| 857 | } |
|---|
| 858 | return distance; |
|---|
| 859 | } |
|---|
| 860 | |
|---|
| 861 | int owl_editwin_move_if_not_in(owl_editwin *e, int delta, const char *set) |
|---|
| 862 | { |
|---|
| 863 | int change, distance = 0; |
|---|
| 864 | while (!owl_editwin_is_char_in(e, set)) { |
|---|
| 865 | change = owl_editwin_point_move(e, delta); |
|---|
| 866 | distance += change; |
|---|
| 867 | if (change == 0) |
|---|
| 868 | break; |
|---|
| 869 | } |
|---|
| 870 | return distance; |
|---|
| 871 | } |
|---|
| 872 | |
|---|
| 873 | int owl_editwin_move_to_beginning_of_line(owl_editwin *e) |
|---|
| 874 | { |
|---|
| 875 | int distance = 0; |
|---|
| 876 | |
|---|
| 877 | if (!owl_editwin_at_beginning_of_line(e)) { |
|---|
| 878 | /* move off the \n if were at the end of a line */ |
|---|
| 879 | distance += owl_editwin_point_move(e, -1); |
|---|
| 880 | distance += owl_editwin_move_if_not_in(e, -1, "\n"); |
|---|
| 881 | /* If we stopped because we reached a '\n', rather than because we |
|---|
| 882 | * hit the top of the buffer, move forward from the end of the |
|---|
| 883 | * previous line to the start of the current. */ |
|---|
| 884 | if (owl_editwin_get_char_at_point(e) == '\n') |
|---|
| 885 | distance += owl_editwin_point_move(e, 1); |
|---|
| 886 | } |
|---|
| 887 | e->goal_column = 0; /* subtleties */ |
|---|
| 888 | |
|---|
| 889 | return distance; |
|---|
| 890 | } |
|---|
| 891 | |
|---|
| 892 | int owl_editwin_move_to_end_of_line(owl_editwin *e) |
|---|
| 893 | { |
|---|
| 894 | return owl_editwin_move_if_not_in(e, 1, "\n"); |
|---|
| 895 | } |
|---|
| 896 | |
|---|
| 897 | int owl_editwin_line_move(owl_editwin *e, int delta) |
|---|
| 898 | { |
|---|
| 899 | int goal_column, change, ll, distance; |
|---|
| 900 | int count = 0; |
|---|
| 901 | |
|---|
| 902 | change = MAX(delta, -delta); |
|---|
| 903 | |
|---|
| 904 | goal_column = e->goal_column; |
|---|
| 905 | distance = owl_editwin_move_to_beginning_of_line(e); |
|---|
| 906 | goal_column = goal_column == -1 ? -distance : goal_column; |
|---|
| 907 | |
|---|
| 908 | while(count < change) { |
|---|
| 909 | if (delta > 0) { |
|---|
| 910 | distance += owl_editwin_move_if_not_in(e, 1, "\n"); |
|---|
| 911 | distance += owl_editwin_point_move(e, 1); |
|---|
| 912 | } else { |
|---|
| 913 | /* I really want to assert delta < 0 here */ |
|---|
| 914 | distance += owl_editwin_point_move(e, -1); /* to the newline on |
|---|
| 915 | the previous line */ |
|---|
| 916 | distance += owl_editwin_move_to_beginning_of_line(e); |
|---|
| 917 | } |
|---|
| 918 | count++; |
|---|
| 919 | } |
|---|
| 920 | |
|---|
| 921 | distance += (ll = owl_editwin_move_to_end_of_line(e)); |
|---|
| 922 | if (ll > goal_column) |
|---|
| 923 | distance += owl_editwin_point_move(e, goal_column - ll); |
|---|
| 924 | |
|---|
| 925 | e->goal_column = goal_column; |
|---|
| 926 | oe_dirty(e); |
|---|
| 927 | |
|---|
| 928 | return distance; |
|---|
| 929 | } |
|---|
| 930 | |
|---|
| 931 | void owl_editwin_backspace(owl_editwin *e) |
|---|
| 932 | { |
|---|
| 933 | /* delete the char before the current one |
|---|
| 934 | * and shift later chars left |
|---|
| 935 | */ |
|---|
| 936 | if(owl_editwin_point_move(e, -1)) |
|---|
| 937 | owl_editwin_delete_char(e); |
|---|
| 938 | } |
|---|
| 939 | |
|---|
| 940 | void owl_editwin_key_up(owl_editwin *e) |
|---|
| 941 | { |
|---|
| 942 | owl_editwin_line_move(e, -1); |
|---|
| 943 | } |
|---|
| 944 | |
|---|
| 945 | void owl_editwin_key_down(owl_editwin *e) |
|---|
| 946 | { |
|---|
| 947 | owl_editwin_line_move(e, 1); |
|---|
| 948 | } |
|---|
| 949 | |
|---|
| 950 | void owl_editwin_key_left(owl_editwin *e) |
|---|
| 951 | { |
|---|
| 952 | owl_editwin_point_move(e, -1); |
|---|
| 953 | } |
|---|
| 954 | |
|---|
| 955 | void owl_editwin_key_right(owl_editwin *e) |
|---|
| 956 | { |
|---|
| 957 | owl_editwin_point_move(e, 1); |
|---|
| 958 | } |
|---|
| 959 | |
|---|
| 960 | int owl_editwin_forward_word(owl_editwin *e) |
|---|
| 961 | { |
|---|
| 962 | int distance; |
|---|
| 963 | /* if we're starting on a space, find the first non-space */ |
|---|
| 964 | distance = owl_editwin_move_if_in(e, 1, WHITESPACE); |
|---|
| 965 | |
|---|
| 966 | /* now find the end of this word */ |
|---|
| 967 | distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE); |
|---|
| 968 | |
|---|
| 969 | return distance; |
|---|
| 970 | } |
|---|
| 971 | |
|---|
| 972 | void owl_editwin_move_to_nextword(owl_editwin *e) |
|---|
| 973 | { |
|---|
| 974 | owl_editwin_forward_word(e); |
|---|
| 975 | } |
|---|
| 976 | |
|---|
| 977 | /* go backwards to the last non-space character |
|---|
| 978 | */ |
|---|
| 979 | int owl_editwin_backward_word(owl_editwin *e) |
|---|
| 980 | { |
|---|
| 981 | oe_excursion x; |
|---|
| 982 | int distance = 0; |
|---|
| 983 | int further = 0; |
|---|
| 984 | int beginning; |
|---|
| 985 | /* if in middle of word, beginning of word */ |
|---|
| 986 | |
|---|
| 987 | /* if at beginning of a word, find beginning of previous word */ |
|---|
| 988 | |
|---|
| 989 | if (owl_editwin_is_char_in(e, WHITESPACE)) { |
|---|
| 990 | /* if in whitespace past end of word, find a word , the find the beginning*/ |
|---|
| 991 | distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last |
|---|
| 992 | character of the word */ |
|---|
| 993 | oe_save_excursion(e, &x); |
|---|
| 994 | /* are we at the beginning of a word? */ |
|---|
| 995 | owl_editwin_point_move(e, -1); |
|---|
| 996 | beginning = owl_editwin_is_char_in(e, WHITESPACE); |
|---|
| 997 | oe_restore_excursion(e, &x); |
|---|
| 998 | if (beginning) |
|---|
| 999 | return distance; |
|---|
| 1000 | } else { |
|---|
| 1001 | /* in the middle of the word; */ |
|---|
| 1002 | oe_save_excursion(e, &x); |
|---|
| 1003 | further += owl_editwin_point_move(e, -1); |
|---|
| 1004 | if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */ |
|---|
| 1005 | distance += owl_editwin_backward_word(e); /* previous case */ |
|---|
| 1006 | oe_release_excursion(e, &x); |
|---|
| 1007 | return distance + further; |
|---|
| 1008 | } else { |
|---|
| 1009 | oe_restore_excursion(e, &x); |
|---|
| 1010 | } |
|---|
| 1011 | } |
|---|
| 1012 | distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE); |
|---|
| 1013 | /* will go past */ |
|---|
| 1014 | if (e->index > e->lock) |
|---|
| 1015 | distance += owl_editwin_point_move(e, 1); |
|---|
| 1016 | return distance; |
|---|
| 1017 | } |
|---|
| 1018 | |
|---|
| 1019 | void owl_editwin_move_to_previousword(owl_editwin *e) |
|---|
| 1020 | { |
|---|
| 1021 | owl_editwin_backward_word(e); |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | void owl_editwin_delete_nextword(owl_editwin *e) |
|---|
| 1025 | { |
|---|
| 1026 | oe_excursion x; |
|---|
| 1027 | |
|---|
| 1028 | oe_save_excursion(e, &x); |
|---|
| 1029 | oe_set_mark(e, e->index); |
|---|
| 1030 | owl_editwin_forward_word(e); |
|---|
| 1031 | owl_editwin_kill_region(e); |
|---|
| 1032 | oe_restore_mark_only(e, &x); |
|---|
| 1033 | } |
|---|
| 1034 | |
|---|
| 1035 | void owl_editwin_delete_previousword(owl_editwin *e) |
|---|
| 1036 | { |
|---|
| 1037 | oe_excursion x; |
|---|
| 1038 | |
|---|
| 1039 | oe_save_excursion(e, &x); |
|---|
| 1040 | oe_set_mark(e, e->index); |
|---|
| 1041 | owl_editwin_backward_word(e); |
|---|
| 1042 | owl_editwin_kill_region(e); |
|---|
| 1043 | oe_restore_mark_only(e, &x); |
|---|
| 1044 | } |
|---|
| 1045 | |
|---|
| 1046 | void owl_editwin_move_to_line_end(owl_editwin *e) |
|---|
| 1047 | { |
|---|
| 1048 | owl_editwin_move_to_end_of_line(e); |
|---|
| 1049 | } |
|---|
| 1050 | |
|---|
| 1051 | void owl_editwin_delete_to_endofline(owl_editwin *e) |
|---|
| 1052 | { |
|---|
| 1053 | oe_excursion x; |
|---|
| 1054 | int distance; |
|---|
| 1055 | |
|---|
| 1056 | oe_save_excursion(e, &x); |
|---|
| 1057 | owl_editwin_set_mark(e); |
|---|
| 1058 | distance = owl_editwin_move_to_end_of_line(e); |
|---|
| 1059 | if (distance) |
|---|
| 1060 | owl_editwin_kill_region(e); |
|---|
| 1061 | else |
|---|
| 1062 | owl_editwin_replace(e, 1, ""); |
|---|
| 1063 | oe_restore_excursion(e, &x); |
|---|
| 1064 | } |
|---|
| 1065 | |
|---|
| 1066 | void owl_editwin_yank(owl_editwin *e) |
|---|
| 1067 | { |
|---|
| 1068 | if (e->killbuf != NULL) |
|---|
| 1069 | owl_editwin_replace(e, 0, e->killbuf); |
|---|
| 1070 | } |
|---|
| 1071 | |
|---|
| 1072 | static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len) |
|---|
| 1073 | { |
|---|
| 1074 | char *p; |
|---|
| 1075 | |
|---|
| 1076 | p = owl_malloc(len + 1); |
|---|
| 1077 | |
|---|
| 1078 | if (p != NULL) { |
|---|
| 1079 | owl_free(e->killbuf); |
|---|
| 1080 | e->killbuf = p; |
|---|
| 1081 | memcpy(e->killbuf, buf, len); |
|---|
| 1082 | e->killbuf[len] = 0; |
|---|
| 1083 | } |
|---|
| 1084 | |
|---|
| 1085 | return p; |
|---|
| 1086 | } |
|---|
| 1087 | |
|---|
| 1088 | static int oe_copy_region(owl_editwin *e) |
|---|
| 1089 | { |
|---|
| 1090 | const char *p; |
|---|
| 1091 | int start, end; |
|---|
| 1092 | |
|---|
| 1093 | if (e->mark == -1) |
|---|
| 1094 | return 0; |
|---|
| 1095 | |
|---|
| 1096 | start = MIN(e->index, e->mark); |
|---|
| 1097 | end = MAX(e->index, e->mark); |
|---|
| 1098 | |
|---|
| 1099 | p = oe_copy_buf(e, e->buff + start, end - start); |
|---|
| 1100 | if (p != NULL) |
|---|
| 1101 | return end - start; |
|---|
| 1102 | return 0; |
|---|
| 1103 | } |
|---|
| 1104 | |
|---|
| 1105 | void owl_editwin_copy_region_as_kill(owl_editwin *e) |
|---|
| 1106 | { |
|---|
| 1107 | oe_copy_region(e); |
|---|
| 1108 | } |
|---|
| 1109 | |
|---|
| 1110 | void owl_editwin_kill_region(owl_editwin *e) |
|---|
| 1111 | { |
|---|
| 1112 | if (e->index > e->mark) |
|---|
| 1113 | owl_editwin_exchange_point_and_mark(e); |
|---|
| 1114 | |
|---|
| 1115 | owl_editwin_replace_internal(e, oe_copy_region(e), ""); |
|---|
| 1116 | } |
|---|
| 1117 | |
|---|
| 1118 | void owl_editwin_move_to_line_start(owl_editwin *e) |
|---|
| 1119 | { |
|---|
| 1120 | owl_editwin_move_to_beginning_of_line(e); |
|---|
| 1121 | } |
|---|
| 1122 | |
|---|
| 1123 | void owl_editwin_move_to_end(owl_editwin *e) |
|---|
| 1124 | { |
|---|
| 1125 | oe_set_index(e, e->bufflen); |
|---|
| 1126 | } |
|---|
| 1127 | |
|---|
| 1128 | void owl_editwin_move_to_top(owl_editwin *e) |
|---|
| 1129 | { |
|---|
| 1130 | oe_set_index(e, e->lock); |
|---|
| 1131 | } |
|---|
| 1132 | |
|---|
| 1133 | void owl_editwin_backward_paragraph(owl_editwin *e) |
|---|
| 1134 | { |
|---|
| 1135 | owl_editwin_point_move(e, -1); |
|---|
| 1136 | for (; e->index >= e->lock; owl_editwin_point_move(e, -1)) { |
|---|
| 1137 | if (e->index <= e->lock || |
|---|
| 1138 | ((e->buff[e->index] == '\n') && (e->buff[e->index - 1]=='\n'))) |
|---|
| 1139 | break; |
|---|
| 1140 | } |
|---|
| 1141 | } |
|---|
| 1142 | |
|---|
| 1143 | void owl_editwin_forward_paragraph(owl_editwin *e) |
|---|
| 1144 | { |
|---|
| 1145 | owl_editwin_point_move(e, 1); |
|---|
| 1146 | /* scan forward to the start of the next paragraph */ |
|---|
| 1147 | for(; e->index < e->bufflen; owl_editwin_point_move(e, 1)) { |
|---|
| 1148 | if (e->buff[e->index -1] == '\n' && e->buff[e->index] == '\n') |
|---|
| 1149 | break; |
|---|
| 1150 | } |
|---|
| 1151 | } |
|---|
| 1152 | |
|---|
| 1153 | int owl_editwin_current_column(owl_editwin *e) |
|---|
| 1154 | { |
|---|
| 1155 | if (e->column == -1) { |
|---|
| 1156 | oe_excursion x; |
|---|
| 1157 | int lineindex; |
|---|
| 1158 | |
|---|
| 1159 | oe_save_excursion(e, &x); |
|---|
| 1160 | owl_editwin_move_to_beginning_of_line(e); |
|---|
| 1161 | lineindex = e->index; |
|---|
| 1162 | oe_restore_excursion(e, &x); |
|---|
| 1163 | e->column = oe_region_width(e, lineindex, e->index, 0); |
|---|
| 1164 | } |
|---|
| 1165 | return e->column; |
|---|
| 1166 | } |
|---|
| 1167 | |
|---|
| 1168 | void owl_editwin_fill_paragraph(owl_editwin *e) |
|---|
| 1169 | { |
|---|
| 1170 | oe_excursion x; |
|---|
| 1171 | gunichar ch; |
|---|
| 1172 | int sentence; |
|---|
| 1173 | |
|---|
| 1174 | if (e->fillcol < 0) |
|---|
| 1175 | /* auto-fill disabled */ |
|---|
| 1176 | return; |
|---|
| 1177 | |
|---|
| 1178 | oe_save_excursion(e, &x); |
|---|
| 1179 | |
|---|
| 1180 | /* Mark the end of the paragraph */ |
|---|
| 1181 | owl_editwin_forward_paragraph(e); |
|---|
| 1182 | /* Skip the trailing newline */ |
|---|
| 1183 | owl_editwin_point_move(e, -1); |
|---|
| 1184 | owl_editwin_set_mark(e); |
|---|
| 1185 | |
|---|
| 1186 | owl_editwin_backward_paragraph(e); |
|---|
| 1187 | |
|---|
| 1188 | /* Don't mess with the leading newline */ |
|---|
| 1189 | if (owl_editwin_get_char_at_point(e) == '\n') |
|---|
| 1190 | owl_editwin_point_move(e, 1); |
|---|
| 1191 | |
|---|
| 1192 | /* |
|---|
| 1193 | * First pass: Scan forward replacing all series of spaces with ' ' |
|---|
| 1194 | * (or nothing after CJK ideograms) |
|---|
| 1195 | */ |
|---|
| 1196 | sentence = 0; |
|---|
| 1197 | for(;e->index < e->mark; owl_editwin_point_move(e, 1)) { |
|---|
| 1198 | /* bail if we hit a trailing dot on the buffer */ |
|---|
| 1199 | if (strcmp(e->buff + e->index, "\n.") == 0) { |
|---|
| 1200 | owl_editwin_set_mark(e); |
|---|
| 1201 | break; |
|---|
| 1202 | } |
|---|
| 1203 | |
|---|
| 1204 | ch = owl_editwin_get_char_at_point(e); |
|---|
| 1205 | |
|---|
| 1206 | if (owl_util_can_break_after(ch) || ch == '\n') { |
|---|
| 1207 | if (g_unichar_isspace(ch)) { |
|---|
| 1208 | owl_editwin_replace(e, 1, " "); |
|---|
| 1209 | } |
|---|
| 1210 | |
|---|
| 1211 | if (sentence && g_unichar_isspace(owl_editwin_get_char_at_point(e)) |
|---|
| 1212 | && e->index < e->mark) |
|---|
| 1213 | owl_editwin_point_move(e, 1); |
|---|
| 1214 | |
|---|
| 1215 | while(g_unichar_isspace(owl_editwin_get_char_at_point(e)) |
|---|
| 1216 | && e->index < e->mark) { |
|---|
| 1217 | owl_editwin_delete_char(e); |
|---|
| 1218 | } |
|---|
| 1219 | } |
|---|
| 1220 | |
|---|
| 1221 | if(ch == '.' || ch == '!' || ch == '?') |
|---|
| 1222 | sentence = 1; |
|---|
| 1223 | else |
|---|
| 1224 | sentence = 0; |
|---|
| 1225 | } |
|---|
| 1226 | |
|---|
| 1227 | owl_editwin_backward_paragraph(e); |
|---|
| 1228 | |
|---|
| 1229 | /* Now go through inserting newlines as needed */ |
|---|
| 1230 | while(e->index < e->mark) { |
|---|
| 1231 | /* if we've travelled too far, linewrap */ |
|---|
| 1232 | if (owl_editwin_current_column(e) >= e->fillcol) |
|---|
| 1233 | _owl_editwin_linewrap_word(e); |
|---|
| 1234 | owl_editwin_point_move(e, 1); |
|---|
| 1235 | } |
|---|
| 1236 | |
|---|
| 1237 | oe_restore_excursion(e, &x); |
|---|
| 1238 | } |
|---|
| 1239 | |
|---|
| 1240 | /* returns true if only whitespace remains */ |
|---|
| 1241 | int owl_editwin_is_at_end(owl_editwin *e) |
|---|
| 1242 | { |
|---|
| 1243 | return (only_whitespace(e->buff + e->index)); |
|---|
| 1244 | } |
|---|
| 1245 | |
|---|
| 1246 | static int owl_editwin_check_dotsend(owl_editwin *e) |
|---|
| 1247 | { |
|---|
| 1248 | int zdot = 0; |
|---|
| 1249 | oe_excursion x; |
|---|
| 1250 | |
|---|
| 1251 | if (!e->dotsend) return(0); |
|---|
| 1252 | if (!owl_editwin_is_at_end(e)) return (0); |
|---|
| 1253 | |
|---|
| 1254 | oe_save_excursion(e, &x); |
|---|
| 1255 | |
|---|
| 1256 | owl_editwin_point_move(e, -3); |
|---|
| 1257 | |
|---|
| 1258 | if(strncmp(e->buff + e->index, "\n.\n", 3) == 0) { |
|---|
| 1259 | owl_editwin_point_move(e, 1); |
|---|
| 1260 | zdot = 1; |
|---|
| 1261 | } else if(e->index == e->lock && |
|---|
| 1262 | strncmp(e->buff + e->index, ".\n", 2) == 0) { |
|---|
| 1263 | zdot = 1; |
|---|
| 1264 | } |
|---|
| 1265 | |
|---|
| 1266 | if(zdot) { |
|---|
| 1267 | owl_editwin_set_mark(e); |
|---|
| 1268 | owl_editwin_move_to_end(e); |
|---|
| 1269 | owl_editwin_replace_region(e, ""); |
|---|
| 1270 | } |
|---|
| 1271 | |
|---|
| 1272 | oe_restore_excursion(e, &x); |
|---|
| 1273 | |
|---|
| 1274 | return zdot; |
|---|
| 1275 | } |
|---|
| 1276 | |
|---|
| 1277 | void owl_editwin_post_process_char(owl_editwin *e, owl_input j) |
|---|
| 1278 | { |
|---|
| 1279 | /* XXX force a redisplay? */ |
|---|
| 1280 | if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) { |
|---|
| 1281 | owl_command_edit_done(e); |
|---|
| 1282 | return; |
|---|
| 1283 | } |
|---|
| 1284 | } |
|---|
| 1285 | |
|---|
| 1286 | static int oe_region_column(owl_editwin *e, int start, int end, int offset) |
|---|
| 1287 | { |
|---|
| 1288 | const char *p; |
|---|
| 1289 | int column = offset; |
|---|
| 1290 | |
|---|
| 1291 | for(p = e->buff + start; |
|---|
| 1292 | p < e->buff + end; |
|---|
| 1293 | p = g_utf8_find_next_char(p, NULL)) { |
|---|
| 1294 | gunichar c = g_utf8_get_char(p); |
|---|
| 1295 | if (c == '\n') |
|---|
| 1296 | column = 0; |
|---|
| 1297 | else |
|---|
| 1298 | column += oe_char_width(c, column); |
|---|
| 1299 | } |
|---|
| 1300 | return column; |
|---|
| 1301 | } |
|---|
| 1302 | |
|---|
| 1303 | static int oe_region_width(owl_editwin *e, int start, int end, int offset) |
|---|
| 1304 | { |
|---|
| 1305 | const char *p; |
|---|
| 1306 | int width = offset; |
|---|
| 1307 | |
|---|
| 1308 | for(p = e->buff + start; |
|---|
| 1309 | p < e->buff + end; |
|---|
| 1310 | p = g_utf8_find_next_char(p, NULL)) |
|---|
| 1311 | width += oe_char_width(g_utf8_get_char(p), width); |
|---|
| 1312 | |
|---|
| 1313 | return width - offset; |
|---|
| 1314 | } |
|---|
| 1315 | |
|---|
| 1316 | static void oe_insert_char(owl_editwin *e, gunichar c) |
|---|
| 1317 | { |
|---|
| 1318 | oe_excursion x; |
|---|
| 1319 | char tmp[7]; |
|---|
| 1320 | int replaced = -1; |
|---|
| 1321 | int column; |
|---|
| 1322 | |
|---|
| 1323 | if (c == '\r') /* translate CRs to NLs */ |
|---|
| 1324 | c = '\n'; |
|---|
| 1325 | |
|---|
| 1326 | if (!g_unichar_iscntrl(c) || c == '\n' || c== '\t' ) { |
|---|
| 1327 | if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) { |
|---|
| 1328 | return; |
|---|
| 1329 | } |
|---|
| 1330 | |
|---|
| 1331 | column = owl_editwin_current_column(e); |
|---|
| 1332 | if (e->wrapcol > 0 && column != -1 && |
|---|
| 1333 | column + oe_char_width(c, column) > e->wrapcol) { |
|---|
| 1334 | /* XXX this is actually wrong: |
|---|
| 1335 | * + If the user went back and inserted a bunch of stuff in the middle of |
|---|
| 1336 | * the line, there may be more than one word past the wrap column. |
|---|
| 1337 | */ |
|---|
| 1338 | oe_save_excursion(e, &x); |
|---|
| 1339 | |
|---|
| 1340 | if (c == ' ' || c == '\t') { |
|---|
| 1341 | owl_editwin_point_move(e, -1); |
|---|
| 1342 | replaced = -owl_editwin_move_if_in(e, -1, " \t"); |
|---|
| 1343 | if (!replaced) { |
|---|
| 1344 | c = '\n'; |
|---|
| 1345 | replaced = -1; |
|---|
| 1346 | } |
|---|
| 1347 | } else { |
|---|
| 1348 | while(!owl_editwin_at_beginning_of_line(e)) { |
|---|
| 1349 | owl_editwin_point_move(e, -1); |
|---|
| 1350 | if (owl_util_can_break_after(owl_editwin_get_char_at_point(e))) { |
|---|
| 1351 | replaced = -owl_editwin_move_if_in(e, -1, " \t"); |
|---|
| 1352 | break; |
|---|
| 1353 | } |
|---|
| 1354 | } |
|---|
| 1355 | if (owl_editwin_at_beginning_of_line(e)) |
|---|
| 1356 | replaced = -1; |
|---|
| 1357 | } |
|---|
| 1358 | if (replaced && !owl_editwin_at_beginning_of_line(e)) |
|---|
| 1359 | owl_editwin_point_move(e, 1); |
|---|
| 1360 | if (replaced >= 0) { |
|---|
| 1361 | owl_editwin_replace(e, replaced, "\n"); |
|---|
| 1362 | } |
|---|
| 1363 | oe_restore_excursion(e, &x); |
|---|
| 1364 | } |
|---|
| 1365 | |
|---|
| 1366 | if (replaced >= 0 && (c == ' ' || c == '\t')) |
|---|
| 1367 | return; /* our work here is done */ |
|---|
| 1368 | |
|---|
| 1369 | tmp[g_unichar_to_utf8(c, tmp)] = '\0'; |
|---|
| 1370 | owl_editwin_replace(e, 0, tmp); |
|---|
| 1371 | } |
|---|
| 1372 | } |
|---|
| 1373 | |
|---|
| 1374 | void owl_editwin_process_char(owl_editwin *e, owl_input j) |
|---|
| 1375 | { |
|---|
| 1376 | if (j.ch == ERR) |
|---|
| 1377 | return; |
|---|
| 1378 | /* Ignore ncurses control characters. */ |
|---|
| 1379 | if (j.ch < 0x100) { |
|---|
| 1380 | oe_insert_char(e, j.uch); |
|---|
| 1381 | } |
|---|
| 1382 | } |
|---|
| 1383 | |
|---|
| 1384 | const char *owl_editwin_get_text(owl_editwin *e) |
|---|
| 1385 | { |
|---|
| 1386 | return(e->buff+e->lock); |
|---|
| 1387 | } |
|---|
| 1388 | |
|---|
| 1389 | char *owl_editwin_get_region(owl_editwin *e) |
|---|
| 1390 | { |
|---|
| 1391 | int start, end; |
|---|
| 1392 | start = e->index; |
|---|
| 1393 | end = e->mark; |
|---|
| 1394 | if(start > end) { |
|---|
| 1395 | int tmp = end; |
|---|
| 1396 | end = start; |
|---|
| 1397 | start = tmp; |
|---|
| 1398 | } |
|---|
| 1399 | |
|---|
| 1400 | return oe_chunk(e, start, end); |
|---|
| 1401 | } |
|---|
| 1402 | |
|---|
| 1403 | int owl_editwin_get_echochar(owl_editwin *e) |
|---|
| 1404 | { |
|---|
| 1405 | return e->echochar; |
|---|
| 1406 | } |
|---|
| 1407 | |
|---|
| 1408 | static char *oe_chunk(owl_editwin *e, int start, int end) |
|---|
| 1409 | { |
|---|
| 1410 | char *p; |
|---|
| 1411 | |
|---|
| 1412 | p = owl_malloc(end - start + 1); |
|---|
| 1413 | memcpy(p, e->buff + start, end - start); |
|---|
| 1414 | p[end - start] = 0; |
|---|
| 1415 | |
|---|
| 1416 | return p; |
|---|
| 1417 | } |
|---|
| 1418 | |
|---|
| 1419 | /* |
|---|
| 1420 | * The only guarantee made about these values is that comparisons |
|---|
| 1421 | * between them, as well as comparison between multiple calls to these |
|---|
| 1422 | * functions without modifying the editwin in-between, are meaningful. |
|---|
| 1423 | */ |
|---|
| 1424 | |
|---|
| 1425 | int owl_editwin_get_point(owl_editwin *e) |
|---|
| 1426 | { |
|---|
| 1427 | return e->index; |
|---|
| 1428 | } |
|---|
| 1429 | |
|---|
| 1430 | int owl_editwin_get_mark(owl_editwin *e) |
|---|
| 1431 | { |
|---|
| 1432 | return e->mark; |
|---|
| 1433 | } |
|---|
| 1434 | |
|---|
| 1435 | static void oe_dirty(owl_editwin *e) |
|---|
| 1436 | { |
|---|
| 1437 | if (e->win) owl_window_dirty(e->win); |
|---|
| 1438 | } |
|---|
| 1439 | |
|---|
| 1440 | |
|---|
| 1441 | /* |
|---|
| 1442 | * Local Variables: |
|---|
| 1443 | * mode:C |
|---|
| 1444 | * c-basic-offset:2 |
|---|
| 1445 | * End: |
|---|
| 1446 | */ |
|---|