source: editwin.c @ e75a6df

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