source: editwin.c @ 13ee8f2

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