source: editwin.c @ cb81570

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