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