source: editwin.c @ 26ad412

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