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