source: editwin.c @ fc2677b

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