source: editwin.c @ a1d98e4

release-1.10release-1.6release-1.7release-1.8release-1.9
Last change on this file since a1d98e4 was 6e58ff2, checked in by Nelson Elhage <nelhage@mit.edu>, 14 years ago
Check for an unset mark in owl_editwin_replace_region. Fixes #134.
  • Property mode set to 100644
File size: 30.0 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);
50static void oe_insert_char(owl_editwin *e, gunichar c);
51static int owl_editwin_limit_maxcols(int v, int maxv);
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 v, int maxv)
221{
222  /* maxv > 5 ? MAX(v, vax) : v */
223  if (maxv > 5 && v > maxv) {
224    return(maxv);
225  } else {
226    return(v);
227  }
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)
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) {
408      if (x != NULL && *x == width)
409        *x = -1;
410      break;
411    }
412    width += cw;
413
414    if (c == '\n') {
415      if (width < e->wincols)
416        ++index; /* skip the newline */
417      break;
418    }
419
420    /* find the next character */
421    p = oe_next_point(e, e->buff + index);
422    if (p == NULL) { /* we ran off the end */
423      if (x != NULL && e->index > index)
424        *x = width + 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);
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);
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;
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);
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, line, index, 0));
519        }
520      }
521      line++;
522    }
523    if (x == -1)
524        e->topindex = -1; /* force a reframe */
525    times++;
526  } while(x == -1 && times < 3);
527
528  wmove(e->curswin, y, x);
529  e->cursorx = x;
530}
531
532static inline void oe_fixup(int *target, int start, int end, int change) {
533  if (*target > start) {
534    if (*target <= end)
535      *target = end + change;
536    else
537      *target += change;
538  }
539}
540
541int owl_editwin_replace_region(owl_editwin *e, const char *s)
542{
543  oe_excursion x;
544  int ret;
545
546  if (e->mark == -1) {
547    owl_function_error("The mark is unset, there is no region to replace.");
548    return 0;
549  }
550
551  oe_save_excursion(e, &x);
552
553  if(e->index > e->mark) {
554    owl_editwin_exchange_point_and_mark(e);
555  }
556
557  ret = owl_editwin_replace_internal(e, e->mark - e->index, s);
558
559  oe_restore_excursion(e, &x);
560
561  return ret;
562}
563
564/* replace 'replace' characters at the point with s, returning the change in size */
565int owl_editwin_replace(owl_editwin *e, int replace, const char *s)
566{
567  int start, end, i;
568  const char *p;
569
570  if (!g_utf8_validate(s, -1, NULL)) {
571    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
572    return 0;
573  }
574
575  start = e->index;
576  for (i = 0, p = e->buff + start; i < replace && p != NULL; i++)
577    p = oe_next_point(e, p);
578  if (p != NULL)
579    end = p - e->buff;
580  else
581    end = e->bufflen;
582
583  return owl_editwin_replace_internal(e, end - start, s);
584}
585
586static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s)
587{
588  int start, end, free, need, size, change;
589  oe_excursion *x;
590
591  start = e->index;
592  end   = start + replace;
593
594  free = e->allocated - e->bufflen + end - start;
595
596  need = strlen(s) - free;
597  if (need > 0) {
598    size = e->allocated + need + INCR - (need % INCR);
599    e->buff = owl_realloc(e->buff, size);
600    e->allocated = size;
601  }
602
603  memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end);
604  memcpy(e->buff + start, s, strlen(s));
605  change = start - end + strlen(s);
606  e->bufflen += change;
607  e->index += strlen(s);
608
609  /* fix up the mark */
610  oe_fixup(&e->mark, start, end, change);
611  oe_fixup(&e->topindex, start, end, change);
612  /* fix up any saved points after the replaced area */
613  for (x = e->excursions; x != NULL; x = x->next) {
614    oe_fixup(&x->index, start, end, change);
615    oe_fixup(&x->mark, start, end, change);
616  }
617
618  /* recenter if needed */
619  if (start <= e->topindex)
620    owl_editwin_recenter(e);
621
622  return change;
623}
624
625/* linewrap the word just before the cursor.
626 * returns 0 on success
627 * returns -1 if we could not wrap.
628 */
629static void _owl_editwin_linewrap_word(owl_editwin *e)
630{
631  oe_excursion x;
632  gunichar c;
633
634  oe_save_excursion(e, &x);
635
636  while (owl_editwin_point_move(e, -1)) {
637    c = owl_editwin_get_char_at_point(e);
638    if (owl_util_can_break_after(c) || c == '\n') {
639      if (c != '\n')
640        owl_editwin_replace(e, c != ' ' ? 0 : 1, "\n");
641      break;
642    }
643  }
644
645  oe_restore_excursion(e, &x);
646}
647
648/* delete the character at the current point, following chars
649 * shift left.
650 */
651void owl_editwin_delete_char(owl_editwin *e)
652{
653  owl_editwin_replace(e, 1, "");
654}
655
656/* Swap the character at point with the character at point-1 and
657 * advance the pointer.  If point is at beginning of buffer do
658 * nothing.  If point is after the last character swap point-1 with
659 * point-2.  (Behaves as observed in tcsh and emacs).
660 */
661void owl_editwin_transpose_chars(owl_editwin *e)
662{
663  const char *middle, *end, *start;
664  char *tmp;
665
666  if (e->bufflen == 0) return;
667
668  if (e->index == e->bufflen)
669    owl_editwin_point_move(e, -1);     /* point is after last character */
670
671  if (owl_editwin_at_beginning_of_buffer(e))
672    return;     /* point is at beginning of buffer, do nothing */
673
674  /* Transpose two utf-8 unicode glyphs. */
675  middle = e->buff + e->index;
676
677  end = oe_next_point(e, middle);
678  if (end == NULL)
679    return;
680
681  start = oe_prev_point(e, middle);
682  if (start == NULL)
683    return;
684
685  tmp = owl_malloc((end - start) + 1);
686  tmp[(end - start)] = 0;
687  memcpy(tmp, middle, end - middle);
688  memcpy(tmp + (end - middle), start, middle - start);
689
690  owl_editwin_point_move(e, -1);
691  owl_editwin_replace(e, 2, tmp);
692}
693
694/* insert 'string' at the current point, later text is shifted
695 * right
696 */
697void owl_editwin_insert_string(owl_editwin *e, const char *s)
698{
699  owl_editwin_replace(e, 0, s);
700}
701
702/* We assume index is not set to point to a mid-char */
703static gunichar owl_editwin_get_char_at_point(owl_editwin *e)
704{
705  return g_utf8_get_char(e->buff + e->index);
706}
707
708void owl_editwin_exchange_point_and_mark(owl_editwin *e) {
709  int tmp;
710
711  if (e->mark != -1) {
712    tmp = e->mark;
713    owl_editwin_set_mark(e);
714    oe_set_index(e, tmp);
715  }
716}
717
718int owl_editwin_point_move(owl_editwin *e, int delta)
719{
720  const char *p;
721  int change, d = 0;
722
723  change = MAX(delta, - delta);
724  p = e->buff + e->index;
725
726  while (d < change && p != NULL) {
727    if (delta > 0)
728      p = oe_next_point(e, p);
729    else
730      p = oe_prev_point(e, p);
731    if (p != NULL) {
732      oe_set_index(e, p - e->buff);
733      d++;
734    }
735  }
736
737  return delta > 0 ? d : -d;
738}
739
740int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
741  if (e->index == e->lock)
742    return 1;
743
744  return 0;
745}
746
747int owl_at_end_of_buffer(owl_editwin *e) {
748  if (e->index == e->bufflen)
749    return 1;
750
751  return 0;
752}
753
754static int owl_editwin_at_beginning_of_line(owl_editwin *e)
755{
756  oe_excursion x;
757  int ret;
758
759  if (owl_editwin_at_beginning_of_buffer(e))
760    return 1;
761
762  oe_save_excursion(e, &x);
763  owl_editwin_point_move(e, -1);
764  ret = (owl_editwin_get_char_at_point(e) == '\n');
765  oe_restore_excursion(e, &x);
766
767  return ret;
768}
769
770static int owl_editwin_is_char_in(owl_editwin *e, const char *set)
771{
772  const char *p;
773
774  for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL))
775    if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p))
776      return 1;
777  return 0;
778}
779
780int owl_editwin_move_if_in(owl_editwin *e, int delta, const char *set)
781{
782  int change, distance = 0;
783  while (owl_editwin_is_char_in(e, set)) {
784    change = owl_editwin_point_move(e, delta);
785    distance += change;
786    if (change == 0)
787      break;
788  }
789  return distance;
790}
791
792int owl_editwin_move_if_not_in(owl_editwin *e, int delta, const char *set)
793{
794  int change, distance = 0;
795  while (!owl_editwin_is_char_in(e, set)) {
796    change = owl_editwin_point_move(e, delta);
797    distance += change;
798    if (change == 0)
799      break;
800  }
801  return distance;
802}
803
804int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
805{
806  int distance = 0;
807
808  if (!owl_editwin_at_beginning_of_line(e)) {
809    /* move off the \n if were at the end of a line */
810    distance += owl_editwin_point_move(e, -1);
811    distance += owl_editwin_move_if_not_in(e, -1, "\n");
812    if (distance && !owl_editwin_at_beginning_of_buffer(e))
813      distance += owl_editwin_point_move(e, 1);
814  }
815  e->goal_column = 0; /* subtleties */
816
817  return distance;
818}
819
820int owl_editwin_move_to_end_of_line(owl_editwin *e)
821{
822  return owl_editwin_move_if_not_in(e, 1, "\n");
823}
824
825int owl_editwin_line_move(owl_editwin *e, int delta)
826{
827  int goal_column, change, ll, distance;
828  int count = 0;
829
830  change = MAX(delta, -delta);
831
832  goal_column = e->goal_column;
833  distance = owl_editwin_move_to_beginning_of_line(e);
834  goal_column = goal_column == -1 ? -distance : goal_column;
835
836  while(count < change) {
837    if (delta > 0) {
838      distance += owl_editwin_move_if_not_in(e, 1, "\n");
839      distance += owl_editwin_point_move(e, 1);
840    } else {
841      /* I really want to assert delta < 0 here */
842      distance += owl_editwin_point_move(e, -1); /* to the newline on
843                                                    the previous line */
844      distance += owl_editwin_move_to_beginning_of_line(e);
845    }
846    count++;
847  }
848
849  distance += (ll = owl_editwin_move_to_end_of_line(e));
850  if (ll > goal_column)
851    distance += owl_editwin_point_move(e, goal_column - ll);
852
853  e->goal_column = goal_column;
854
855  return distance;
856}
857
858void owl_editwin_backspace(owl_editwin *e)
859{
860  /* delete the char before the current one
861   * and shift later chars left
862   */
863  if(owl_editwin_point_move(e, -1))
864    owl_editwin_delete_char(e);
865}
866
867void owl_editwin_key_up(owl_editwin *e)
868{
869  owl_editwin_line_move(e, -1);
870}
871
872void owl_editwin_key_down(owl_editwin *e)
873{
874  owl_editwin_line_move(e, 1);
875}
876
877void owl_editwin_key_left(owl_editwin *e)
878{
879  owl_editwin_point_move(e, -1);
880}
881
882void owl_editwin_key_right(owl_editwin *e)
883{
884  owl_editwin_point_move(e, 1);
885}
886
887int owl_editwin_forward_word(owl_editwin *e)
888{
889  int distance;
890  /* if we're starting on a space, find the first non-space */
891  distance = owl_editwin_move_if_in(e, 1, WHITESPACE);
892
893  /* now find the end of this word */
894  distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE);
895
896  return distance;
897}
898
899void owl_editwin_move_to_nextword(owl_editwin *e)
900{
901  owl_editwin_forward_word(e);
902}
903
904/* go backwards to the last non-space character
905 */
906int owl_editwin_backward_word(owl_editwin *e)
907{
908  oe_excursion x;
909  int distance = 0;
910  int further = 0;
911  int beginning;
912  /* if in middle of word, beginning of word */
913
914  /* if at beginning of a word, find beginning of previous word */
915
916  if (owl_editwin_is_char_in(e, WHITESPACE)) {
917    /* if in whitespace past end of word, find a word , the find the beginning*/
918    distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
919                                                              character of the word */
920    oe_save_excursion(e, &x);
921    /* are we at the beginning of a word? */
922    owl_editwin_point_move(e, -1);
923    beginning = owl_editwin_is_char_in(e, WHITESPACE);
924    oe_restore_excursion(e, &x);
925    if (beginning)
926      return distance;
927   } else {
928    /* in the middle of the word; */
929    oe_save_excursion(e, &x);
930    further += owl_editwin_point_move(e, -1);
931    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
932      distance += owl_editwin_backward_word(e); /* previous case */
933      oe_release_excursion(e, &x);
934      return distance + further;
935    } else {
936      oe_restore_excursion(e, &x);
937    }
938  }
939  distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE);
940  /* will go past */
941  if (e->index > e->lock)
942    distance += owl_editwin_point_move(e, 1);
943  return distance;
944}
945
946void owl_editwin_move_to_previousword(owl_editwin *e)
947{
948  owl_editwin_backward_word(e);
949}
950
951void owl_editwin_delete_nextword(owl_editwin *e)
952{
953  oe_excursion x;
954
955  oe_save_excursion(e, &x);
956  oe_set_mark(e, e->index);
957  owl_editwin_forward_word(e);
958  owl_editwin_kill_region(e);
959  oe_restore_mark_only(e, &x);
960}
961
962void owl_editwin_delete_previousword(owl_editwin *e)
963{
964  oe_excursion x;
965
966  oe_save_excursion(e, &x);
967  oe_set_mark(e, e->index);
968  owl_editwin_backward_word(e);
969  owl_editwin_kill_region(e);
970  oe_restore_mark_only(e, &x);
971}
972
973void owl_editwin_move_to_line_end(owl_editwin *e)
974{
975  owl_editwin_move_to_end_of_line(e);
976}
977
978void owl_editwin_delete_to_endofline(owl_editwin *e)
979{
980  oe_excursion x;
981  int distance;
982
983  oe_save_excursion(e, &x);
984  owl_editwin_set_mark(e);
985  distance = owl_editwin_move_to_end_of_line(e);
986  if (distance)
987    owl_editwin_kill_region(e);
988  else
989    owl_editwin_replace(e, 1, "");
990  oe_restore_excursion(e, &x);
991}
992
993void owl_editwin_yank(owl_editwin *e)
994{
995  if (e->killbuf != NULL)
996    owl_editwin_replace(e, 0, e->killbuf);
997}
998
999static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len)
1000{
1001  char *p;
1002
1003  p = owl_malloc(len + 1);
1004
1005  if (p != NULL) {
1006    owl_free(e->killbuf);
1007    e->killbuf = p;
1008    memcpy(e->killbuf, buf, len);
1009    e->killbuf[len] = 0;
1010  }
1011
1012  return p;
1013}
1014
1015static int oe_copy_region(owl_editwin *e)
1016{
1017  const char *p;
1018  int start, end;
1019
1020  if (e->mark == -1)
1021    return 0;
1022
1023  start = MIN(e->index, e->mark);
1024  end = MAX(e->index, e->mark);
1025
1026  p = oe_copy_buf(e, e->buff + start, end - start);
1027  if (p != NULL)
1028    return end - start;
1029  return 0;
1030}
1031
1032void owl_editwin_copy_region_as_kill(owl_editwin *e)
1033{
1034  oe_copy_region(e);
1035}
1036
1037void owl_editwin_kill_region(owl_editwin *e)
1038{
1039  if (e->index > e->mark)
1040    owl_editwin_exchange_point_and_mark(e);
1041
1042  owl_editwin_replace_internal(e, oe_copy_region(e), "");
1043}
1044
1045void owl_editwin_move_to_line_start(owl_editwin *e)
1046{
1047  owl_editwin_move_to_beginning_of_line(e);
1048}
1049
1050void owl_editwin_move_to_end(owl_editwin *e)
1051{
1052  oe_set_index(e, e->bufflen);
1053}
1054
1055void owl_editwin_move_to_top(owl_editwin *e)
1056{
1057  oe_set_index(e, e->lock);
1058}
1059
1060void owl_editwin_backward_paragraph(owl_editwin *e)
1061{
1062  owl_editwin_point_move(e, -1);
1063  for (; e->index >= e->lock; owl_editwin_point_move(e, -1)) {
1064    if (e->index <= e->lock ||
1065        ((e->buff[e->index] == '\n') && (e->buff[e->index - 1]=='\n')))
1066      break;
1067  }
1068}
1069
1070void owl_editwin_forward_paragraph(owl_editwin *e)
1071{
1072  owl_editwin_point_move(e, 1);
1073  /* scan forward to the start of the next paragraph */
1074  for(; e->index < e->bufflen; owl_editwin_point_move(e, 1)) {
1075    if (e->buff[e->index -1] == '\n' && e->buff[e->index] == '\n')
1076      break;
1077  }
1078}
1079
1080int owl_editwin_current_column(owl_editwin *e)
1081{
1082  oe_excursion x;
1083  int lineindex;
1084
1085  oe_save_excursion(e, &x);
1086  owl_editwin_move_to_beginning_of_line(e);
1087  lineindex = e->index;
1088  oe_restore_excursion(e, &x);
1089  return oe_region_width(e, lineindex, e->index, 0);
1090}
1091
1092void owl_editwin_fill_paragraph(owl_editwin *e)
1093{
1094  oe_excursion x;
1095  gunichar ch;
1096  int sentence;
1097
1098  oe_save_excursion(e, &x);
1099
1100  /* Mark the end of the paragraph */
1101  owl_editwin_forward_paragraph(e);
1102  /* Skip the trailing newline */
1103  owl_editwin_point_move(e, -1);
1104  owl_editwin_set_mark(e);
1105
1106  owl_editwin_backward_paragraph(e);
1107
1108  /* Don't mess with the leading newline */
1109  if (owl_editwin_get_char_at_point(e) == '\n')
1110    owl_editwin_point_move(e, 1);
1111
1112  /*
1113   * First pass: Scan forward replacing all series of spaces with ' '
1114   * (or nothing after CJK ideograms)
1115   */
1116  sentence = 0;
1117  for(;e->index < e->mark; owl_editwin_point_move(e, 1)) {
1118    /* bail if we hit a trailing dot on the buffer */
1119    if (strcmp(e->buff + e->index, "\n.") == 0) {
1120      owl_editwin_set_mark(e);
1121      break;
1122    }
1123
1124    ch = owl_editwin_get_char_at_point(e);
1125
1126    if (owl_util_can_break_after(ch) || ch == '\n') {
1127      if (g_unichar_isspace(ch)) {
1128        owl_editwin_replace(e, 1, " ");
1129      }
1130
1131      if (sentence && g_unichar_isspace(owl_editwin_get_char_at_point(e))
1132          && e->index < e->mark)
1133        owl_editwin_point_move(e, 1);
1134
1135      while(g_unichar_isspace(owl_editwin_get_char_at_point(e))
1136            && e->index < e->mark) {
1137        owl_editwin_delete_char(e);
1138      }
1139    }
1140
1141    if(ch == '.' || ch == '!' || ch == '?')
1142      sentence = 1;
1143    else
1144      sentence = 0;
1145  }
1146
1147  owl_editwin_backward_paragraph(e);
1148
1149  /* Now go through inserting newlines as needed */
1150  while(e->index < e->mark) {
1151    /* if we've travelled too far, linewrap */
1152    if (owl_editwin_current_column(e) >= e->fillcol)
1153      _owl_editwin_linewrap_word(e);
1154    owl_editwin_point_move(e, 1);
1155  }
1156
1157  oe_restore_excursion(e, &x);
1158}
1159
1160/* returns true if only whitespace remains */
1161int owl_editwin_is_at_end(owl_editwin *e)
1162{
1163  return (only_whitespace(e->buff + e->index));
1164}
1165
1166static int owl_editwin_check_dotsend(owl_editwin *e)
1167{
1168  int zdot = 0;
1169  oe_excursion x;
1170
1171  if (!e->dotsend) return(0);
1172  if (!owl_editwin_is_at_end(e)) return (0);
1173
1174  oe_save_excursion(e, &x);
1175
1176  owl_editwin_point_move(e, -3);
1177
1178  if(strncmp(e->buff + e->index, "\n.\n", 3) == 0) {
1179    owl_editwin_point_move(e, 1);
1180    zdot = 1;
1181  } else if(e->index == e->lock &&
1182            strncmp(e->buff + e->index, ".\n", 2) == 0) {
1183    zdot = 1;
1184  }
1185
1186  if(zdot) {
1187    owl_editwin_set_mark(e);
1188    owl_editwin_move_to_end(e);
1189    owl_editwin_replace_region(e, "");
1190  }
1191
1192  oe_restore_excursion(e, &x);
1193
1194  return zdot;
1195}
1196
1197void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
1198{
1199  /* XXX force a redisplay? */
1200  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
1201    owl_command_edit_done(e);
1202    return;
1203  }
1204  owl_editwin_redisplay(e);
1205}
1206
1207static int oe_region_width(owl_editwin *e, int start, int end, int offset)
1208{
1209  const char *p;
1210  int width = offset;
1211 
1212  for(p = e->buff + start;
1213      p < e->buff + end;
1214      p = g_utf8_find_next_char(p, NULL))
1215    width += oe_char_width(g_utf8_get_char(p), width);
1216
1217  return width - offset;
1218}
1219
1220static void oe_insert_char(owl_editwin *e, gunichar c)
1221{
1222  oe_excursion x;
1223  char tmp[7];
1224  int replaced = -1;
1225
1226  if (c == '\r') /* translate CRs to NLs */
1227    c = '\n';
1228
1229  if (!g_unichar_iscntrl(c) || c == '\n' || c== '\t' ) {
1230    if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
1231      return;
1232    }
1233
1234    if (e->cursorx != -1 && e->cursorx + oe_char_width(c, e->cursorx) > e->wrapcol) {
1235      /* XXX this is actually wrong:
1236       * + If the line has been been wrapped, we can be past the wrap column but
1237       *   e->cursorx be much smaller.
1238       * + If the user went back and inserted a bunch of stuff in the middle of
1239       *   the line, there may be more than one word past the wrap column.
1240       */
1241      oe_save_excursion(e, &x);
1242
1243      if (c == ' ' || c == '\t') {
1244        owl_editwin_point_move(e, -1);
1245        replaced = -owl_editwin_move_if_in(e, -1, " \t");
1246        if (!replaced) {
1247          c = '\n';
1248          replaced = -1;
1249        }
1250      } else {
1251        while(!owl_editwin_at_beginning_of_line(e)) {
1252          owl_editwin_point_move(e, -1);
1253          if (owl_util_can_break_after(owl_editwin_get_char_at_point(e))) {
1254            replaced = -owl_editwin_move_if_in(e, -1, " \t");
1255            break;
1256          }
1257        }
1258        if (owl_editwin_at_beginning_of_line(e))
1259          replaced = -1;
1260      }
1261      if (replaced && !owl_editwin_at_beginning_of_line(e))
1262        owl_editwin_point_move(e, 1);
1263      if (replaced >= 0) {
1264        owl_editwin_replace(e, replaced, "\n");
1265      }
1266      oe_restore_excursion(e, &x);
1267    }
1268
1269    if (replaced >= 0 && (c == ' ' || c == '\t'))
1270      return; /* our work here is done */
1271
1272    tmp[g_unichar_to_utf8(c, tmp)] = '\0';
1273    owl_editwin_replace(e, 0, tmp);
1274  }
1275}
1276
1277void owl_editwin_process_char(owl_editwin *e, owl_input j)
1278{
1279  if (j.ch == ERR)
1280    return;
1281  /* Ignore ncurses control characters. */
1282  if (j.ch < 0x100) {
1283    oe_insert_char(e, j.uch);
1284  }
1285}
1286
1287const char *owl_editwin_get_text(owl_editwin *e)
1288{
1289  return(e->buff+e->lock);
1290}
1291
1292char *owl_editwin_get_region(owl_editwin *e)
1293{
1294  int start, end;
1295  start = e->index;
1296  end   = e->mark;
1297  if(start > end) {
1298    int tmp = end;
1299    end = start;
1300    start = tmp;
1301  }
1302
1303  return oe_chunk(e, start, end);
1304}
1305
1306int owl_editwin_get_echochar(owl_editwin *e)
1307{
1308  return e->echochar;
1309}
1310
1311static char *oe_chunk(owl_editwin *e, int start, int end)
1312{
1313  char *p;
1314 
1315  p = owl_malloc(end - start + 1);
1316  memcpy(p, e->buff + start, end - start);
1317  p[end - start] = 0;
1318
1319  return p;
1320}
1321
1322/*
1323 * The only guarantee made about these values is that comparisons
1324 * between them, as well as comparison between multiple calls to these
1325 * functions without modifying the editwin in-between, are meaningful.
1326 */
1327
1328int owl_editwin_get_point(owl_editwin *e)
1329{
1330  return e->index;
1331}
1332
1333int owl_editwin_get_mark(owl_editwin *e)
1334{
1335  return e->mark;
1336}
1337
1338
1339/*
1340 * Local Variables:
1341 * mode:C
1342 * c-basic-offset:2
1343 * End:
1344 */
Note: See TracBrowser for help on using the repository browser.