source: editwin.c @ b644688

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