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