source: editwin.c @ d625cfd

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