source: editwin.c @ 9c3f334

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