source: editwin.c @ 0190c4d

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