source: editwin.c @ e56303f

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