source: editwin.c @ 6337cb5

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