source: editwin.c @ 9518a85

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