source: editwin.c @ 52172cc

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