source: editwin.c @ 8dfb59c

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