source: editwin.c @ 08263a8

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