source: editwin.c @ 03c5bdd

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