source: editwin.c @ fc5eef4

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