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