source: editwin.c @ 050d25e

release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 050d25e was d41f773, checked in by Nelson Elhage <nelhage@mit.edu>, 11 years ago
Revert "Force the locktext to be visible initially." This reverts commit 43fe02f57c9627f7d117292377d5f9738dabe3e4. This commit was pushed accidentally.
  • Property mode set to 100644
File size: 30.8 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_free(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, 0);
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 */
520/* if update == 1 then do a doupdate(), otherwise do not */
521void owl_editwin_redisplay(owl_editwin *e, int update)
522{
523  int x = -1, y = -1, t;
524  int line, index, lineindex, times = 0;
525
526  do {
527    werase(e->curswin);
528
529    if (e->topindex == -1 || e->index < e->topindex)
530      oe_reframe(e);
531
532    line = 0;
533    index = e->topindex;
534    while(line < e->winlines) {
535      lineindex = index;
536      t = -1;
537      index = oe_find_display_line(e, &t, lineindex);
538      if (x == -1 && t != -1)
539        x = t, y = line;
540      if (index - lineindex) {
541        if (!e->echochar)
542          mvwaddnstr(e->curswin, line, 0,
543                     e->buff + lineindex,
544                     index - lineindex);
545        else {
546          if(lineindex < e->lock) {
547            mvwaddnstr(e->curswin, line, 0,
548                       e->buff + lineindex,
549                       MIN(index - lineindex,
550                           e->lock - lineindex));
551            if (e->lock < index)
552              oe_addnec(e,
553                        oe_region_width(e, e->lock, index,
554                                        oe_region_width(e, lineindex, e->lock, 0)));
555          } else
556            oe_mvaddnec(e, line, 0, oe_region_width(e, line, index, 0));
557        }
558      }
559      line++;
560    }
561    if (x == -1)
562        e->topindex = -1; /* force a reframe */
563    times++;
564  } while(x == -1 && times < 3);
565
566  wmove(e->curswin, y, x);
567  e->cursorx = x;
568
569  wnoutrefresh(e->curswin);
570  if (update == 1)
571    doupdate();
572}
573
574static inline void oe_fixup(int *target, int start, int end, int change) {
575  if (*target > start) {
576    if (*target <= end)
577      *target = end + change;
578    else
579      *target += change;
580  }
581}
582
583int owl_editwin_replace_region(owl_editwin *e, const char *s)
584{
585  oe_excursion x;
586  int ret;
587  oe_save_excursion(e, &x);
588
589  if(e->index > e->mark) {
590    owl_editwin_exchange_point_and_mark(e);
591  }
592
593  ret = owl_editwin_replace_internal(e, e->mark - e->index, s);
594
595  oe_restore_excursion(e, &x);
596
597  return ret;
598}
599
600/* replace 'replace' characters at the point with s, returning the change in size */
601int owl_editwin_replace(owl_editwin *e, int replace, const char *s)
602{
603  int start, end, i;
604  const char *p;
605
606  if (!g_utf8_validate(s, -1, NULL)) {
607    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
608    return 0;
609  }
610
611  start = e->index;
612  for (i = 0, p = e->buff + start; i < replace && p != NULL; i++)
613    p = oe_next_point(e, p);
614  if (p != NULL)
615    end = p - e->buff;
616  else
617    end = e->bufflen;
618
619  return owl_editwin_replace_internal(e, end - start, s);
620}
621
622static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s)
623{
624  int start, end, free, need, size, change;
625  oe_excursion *x;
626
627  start = e->index;
628  end   = start + replace;
629
630  free = e->allocated - e->bufflen + end - start;
631
632  need = strlen(s) - free;
633  if (need > 0) {
634    size = e->allocated + need + INCR - (need % INCR);
635    e->buff = owl_realloc(e->buff, size);
636    e->allocated = size;
637  }
638
639  memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end);
640  memcpy(e->buff + start, s, strlen(s));
641  change = start - end + strlen(s);
642  e->bufflen += change;
643  e->index += strlen(s);
644
645  /* fix up the mark */
646  oe_fixup(&e->mark, start, end, change);
647  oe_fixup(&e->topindex, start, end, change);
648  /* fix up any saved points after the replaced area */
649  for (x = e->excursions; x != NULL; x = x->next) {
650    oe_fixup(&x->index, start, end, change);
651    oe_fixup(&x->mark, start, end, change);
652  }
653
654  /* recenter if needed */
655  if (start <= e->topindex)
656    owl_editwin_recenter(e);
657
658  return change;
659}
660
661/* linewrap the word just before the cursor.
662 * returns 0 on success
663 * returns -1 if we could not wrap.
664 */
665static void _owl_editwin_linewrap_word(owl_editwin *e)
666{
667  oe_excursion x;
668  gunichar c;
669
670  oe_save_excursion(e, &x);
671
672  while (owl_editwin_point_move(e, -1)) {
673    c = owl_editwin_get_char_at_point(e);
674    if (owl_util_can_break_after(c) || c == '\n') {
675      if (c != '\n')
676        owl_editwin_replace(e, c != ' ' ? 0 : 1, "\n");
677      break;
678    }
679  }
680
681  oe_restore_excursion(e, &x);
682}
683
684/* delete the character at the current point, following chars
685 * shift left.
686 */
687void owl_editwin_delete_char(owl_editwin *e)
688{
689  owl_editwin_replace(e, 1, "");
690}
691
692/* Swap the character at point with the character at point-1 and
693 * advance the pointer.  If point is at beginning of buffer do
694 * nothing.  If point is after the last character swap point-1 with
695 * point-2.  (Behaves as observed in tcsh and emacs).
696 */
697void owl_editwin_transpose_chars(owl_editwin *e)
698{
699  const char *middle, *end, *start;
700  char *tmp;
701
702  if (e->bufflen == 0) return;
703
704  if (e->index == e->bufflen)
705    owl_editwin_point_move(e, -1);     /* point is after last character */
706
707  if (owl_editwin_at_beginning_of_buffer(e))
708    return;     /* point is at beginning of buffer, do nothing */
709
710  /* Transpose two utf-8 unicode glyphs. */
711  middle = e->buff + e->index;
712
713  end = oe_next_point(e, middle);
714  if (end == NULL)
715    return;
716
717  start = oe_prev_point(e, middle);
718  if (start == NULL)
719    return;
720
721  tmp = owl_malloc((end - start) + 1);
722  tmp[(end - start)] = 0;
723  memcpy(tmp, middle, end - middle);
724  memcpy(tmp + (end - middle), start, middle - start);
725
726  owl_editwin_point_move(e, -1);
727  owl_editwin_replace(e, 2, tmp);
728}
729
730/* insert 'string' at the current point, later text is shifted
731 * right
732 */
733void owl_editwin_insert_string(owl_editwin *e, const char *s)
734{
735  owl_editwin_replace(e, 0, s);
736}
737
738/* We assume index is not set to point to a mid-char */
739static gunichar owl_editwin_get_char_at_point(owl_editwin *e)
740{
741  return g_utf8_get_char(e->buff + e->index);
742}
743
744void owl_editwin_exchange_point_and_mark(owl_editwin *e) {
745  int tmp;
746
747  if (e->mark != -1) {
748    tmp = e->mark;
749    owl_editwin_set_mark(e);
750    oe_set_index(e, tmp);
751  }
752}
753
754int owl_editwin_point_move(owl_editwin *e, int delta)
755{
756  const char *p;
757  int change, d = 0;
758
759  change = MAX(delta, - delta);
760  p = e->buff + e->index;
761
762  while (d < change && p != NULL) {
763    if (delta > 0)
764      p = oe_next_point(e, p);
765    else
766      p = oe_prev_point(e, p);
767    if (p != NULL) {
768      oe_set_index(e, p - e->buff);
769      d++;
770    }
771  }
772
773  return delta > 0 ? d : -d;
774}
775
776int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
777  if (e->index == e->lock)
778    return 1;
779
780  return 0;
781}
782
783int owl_at_end_of_buffer(owl_editwin *e) {
784  if (e->index == e->bufflen)
785    return 1;
786
787  return 0;
788}
789
790static int owl_editwin_at_beginning_of_line(owl_editwin *e)
791{
792  oe_excursion x;
793  int ret;
794
795  if (owl_editwin_at_beginning_of_buffer(e))
796    return 1;
797
798  oe_save_excursion(e, &x);
799  owl_editwin_point_move(e, -1);
800  ret = (owl_editwin_get_char_at_point(e) == '\n');
801  oe_restore_excursion(e, &x);
802
803  return ret;
804}
805
806static int owl_editwin_is_char_in(owl_editwin *e, const char *set)
807{
808  const char *p;
809
810  for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL))
811    if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p))
812      return 1;
813  return 0;
814}
815
816int owl_editwin_move_if_in(owl_editwin *e, int delta, const char *set)
817{
818  int change, distance = 0;
819  while (owl_editwin_is_char_in(e, set)) {
820    change = owl_editwin_point_move(e, delta);
821    distance += change;
822    if (change == 0)
823      break;
824  }
825  return distance;
826}
827
828int owl_editwin_move_if_not_in(owl_editwin *e, int delta, const char *set)
829{
830  int change, distance = 0;
831  while (!owl_editwin_is_char_in(e, set)) {
832    change = owl_editwin_point_move(e, delta);
833    distance += change;
834    if (change == 0)
835      break;
836  }
837  return distance;
838}
839
840int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
841{
842  int distance = 0;
843
844  if (!owl_editwin_at_beginning_of_line(e)) {
845    /* move off the \n if were at the end of a line */
846    distance += owl_editwin_point_move(e, -1);
847    distance += owl_editwin_move_if_not_in(e, -1, "\n");
848    if (distance && !owl_editwin_at_beginning_of_buffer(e))
849      distance += owl_editwin_point_move(e, 1);
850  }
851  e->goal_column = 0; /* subtleties */
852
853  return distance;
854}
855
856int owl_editwin_move_to_end_of_line(owl_editwin *e)
857{
858  return owl_editwin_move_if_not_in(e, 1, "\n");
859}
860
861int owl_editwin_line_move(owl_editwin *e, int delta)
862{
863  int goal_column, change, ll, distance;
864  int count = 0;
865
866  change = MAX(delta, -delta);
867
868  goal_column = e->goal_column;
869  distance = owl_editwin_move_to_beginning_of_line(e);
870  goal_column = goal_column == -1 ? -distance : goal_column;
871
872  while(count < change) {
873    if (delta > 0) {
874      distance += owl_editwin_move_if_not_in(e, 1, "\n");
875      distance += owl_editwin_point_move(e, 1);
876    } else {
877      /* I really want to assert delta < 0 here */
878      distance += owl_editwin_point_move(e, -1); /* to the newline on
879                                                    the previous line */
880      distance += owl_editwin_move_to_beginning_of_line(e);
881    }
882    count++;
883  }
884
885  distance += (ll = owl_editwin_move_to_end_of_line(e));
886  if (ll > goal_column)
887    distance += owl_editwin_point_move(e, goal_column - ll);
888
889  e->goal_column = goal_column;
890
891  return distance;
892}
893
894void owl_editwin_backspace(owl_editwin *e)
895{
896  /* delete the char before the current one
897   * and shift later chars left
898   */
899  if(owl_editwin_point_move(e, -1))
900    owl_editwin_delete_char(e);
901}
902
903void owl_editwin_key_up(owl_editwin *e)
904{
905  owl_editwin_line_move(e, -1);
906}
907
908void owl_editwin_key_down(owl_editwin *e)
909{
910  owl_editwin_line_move(e, 1);
911}
912
913void owl_editwin_key_left(owl_editwin *e)
914{
915  owl_editwin_point_move(e, -1);
916}
917
918void owl_editwin_key_right(owl_editwin *e)
919{
920  owl_editwin_point_move(e, 1);
921}
922
923int owl_editwin_forward_word(owl_editwin *e)
924{
925  int distance;
926  /* if we're starting on a space, find the first non-space */
927  distance = owl_editwin_move_if_in(e, 1, WHITESPACE);
928
929  /* now find the end of this word */
930  distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE);
931
932  return distance;
933}
934
935void owl_editwin_move_to_nextword(owl_editwin *e)
936{
937  owl_editwin_forward_word(e);
938}
939
940/* go backwards to the last non-space character
941 */
942int owl_editwin_backward_word(owl_editwin *e)
943{
944  oe_excursion x;
945  int distance = 0;
946  int further = 0;
947  int beginning;
948  /* if in middle of word, beginning of word */
949
950  /* if at beginning of a word, find beginning of previous word */
951
952  if (owl_editwin_is_char_in(e, WHITESPACE)) {
953    /* if in whitespace past end of word, find a word , the find the beginning*/
954    distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
955                                                              character of the word */
956    oe_save_excursion(e, &x);
957    /* are we at the beginning of a word? */
958    owl_editwin_point_move(e, -1);
959    beginning = owl_editwin_is_char_in(e, WHITESPACE);
960    oe_restore_excursion(e, &x);
961    if (beginning)
962      return distance;
963   } else {
964    /* in the middle of the word; */
965    oe_save_excursion(e, &x);
966    further += owl_editwin_point_move(e, -1);
967    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
968      distance += owl_editwin_backward_word(e); /* previous case */
969      oe_release_excursion(e, &x);
970      return distance + further;
971    } else {
972      oe_restore_excursion(e, &x);
973    }
974  }
975  distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE);
976  /* will go past */
977  if (e->index > e->lock)
978    distance += owl_editwin_point_move(e, 1);
979  return distance;
980}
981
982void owl_editwin_move_to_previousword(owl_editwin *e)
983{
984  owl_editwin_backward_word(e);
985}
986
987void owl_editwin_delete_nextword(owl_editwin *e)
988{
989  oe_excursion x;
990
991  oe_save_excursion(e, &x);
992  oe_set_mark(e, e->index);
993  owl_editwin_forward_word(e);
994  owl_editwin_kill_region(e);
995  oe_restore_mark_only(e, &x);
996}
997
998void owl_editwin_delete_previousword(owl_editwin *e)
999{
1000  oe_excursion x;
1001
1002  oe_save_excursion(e, &x);
1003  oe_set_mark(e, e->index);
1004  owl_editwin_backward_word(e);
1005  owl_editwin_kill_region(e);
1006  oe_restore_mark_only(e, &x);
1007}
1008
1009void owl_editwin_move_to_line_end(owl_editwin *e)
1010{
1011  owl_editwin_move_to_end_of_line(e);
1012}
1013
1014void owl_editwin_delete_to_endofline(owl_editwin *e)
1015{
1016  oe_excursion x;
1017  int distance;
1018
1019  oe_save_excursion(e, &x);
1020  owl_editwin_set_mark(e);
1021  distance = owl_editwin_move_to_end_of_line(e);
1022  if (distance)
1023    owl_editwin_kill_region(e);
1024  else
1025    owl_editwin_replace(e, 1, "");
1026  oe_restore_excursion(e, &x);
1027}
1028
1029void owl_editwin_yank(owl_editwin *e)
1030{
1031  if (e->killbuf != NULL)
1032    owl_editwin_replace(e, 0, e->killbuf);
1033}
1034
1035static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len)
1036{
1037  char *p;
1038
1039  p = owl_malloc(len + 1);
1040
1041  if (p != NULL) {
1042    owl_free(e->killbuf);
1043    e->killbuf = p;
1044    memcpy(e->killbuf, buf, len);
1045    e->killbuf[len] = 0;
1046  }
1047
1048  return p;
1049}
1050
1051static int oe_copy_region(owl_editwin *e)
1052{
1053  const char *p;
1054  int start, end;
1055
1056  if (e->mark == -1)
1057    return 0;
1058
1059  start = MIN(e->index, e->mark);
1060  end = MAX(e->index, e->mark);
1061
1062  p = oe_copy_buf(e, e->buff + start, end - start);
1063  if (p != NULL)
1064    return end - start;
1065  return 0;
1066}
1067
1068void owl_editwin_copy_region_as_kill(owl_editwin *e)
1069{
1070  oe_copy_region(e);
1071}
1072
1073void owl_editwin_kill_region(owl_editwin *e)
1074{
1075  if (e->index > e->mark)
1076    owl_editwin_exchange_point_and_mark(e);
1077
1078  owl_editwin_replace_internal(e, oe_copy_region(e), "");
1079}
1080
1081void owl_editwin_move_to_line_start(owl_editwin *e)
1082{
1083  owl_editwin_move_to_beginning_of_line(e);
1084}
1085
1086void owl_editwin_move_to_end(owl_editwin *e)
1087{
1088  oe_set_index(e, e->bufflen);
1089}
1090
1091void owl_editwin_move_to_top(owl_editwin *e)
1092{
1093  oe_set_index(e, e->lock);
1094}
1095
1096void owl_editwin_backward_paragraph(owl_editwin *e)
1097{
1098  owl_editwin_point_move(e, -1);
1099  for (; e->index >= e->lock; owl_editwin_point_move(e, -1)) {
1100    if (e->index <= e->lock ||
1101        ((e->buff[e->index] == '\n') && (e->buff[e->index - 1]=='\n')))
1102      break;
1103  }
1104}
1105
1106void owl_editwin_forward_paragraph(owl_editwin *e)
1107{
1108  owl_editwin_point_move(e, 1);
1109  /* scan forward to the start of the next paragraph */
1110  for(; e->index < e->bufflen; owl_editwin_point_move(e, 1)) {
1111    if (e->buff[e->index -1] == '\n' && e->buff[e->index] == '\n')
1112      break;
1113  }
1114}
1115
1116int owl_editwin_current_column(owl_editwin *e)
1117{
1118  oe_excursion x;
1119  int lineindex;
1120
1121  oe_save_excursion(e, &x);
1122  owl_editwin_move_to_beginning_of_line(e);
1123  lineindex = e->index;
1124  oe_restore_excursion(e, &x);
1125  return oe_region_width(e, lineindex, e->index, 0);
1126}
1127
1128void owl_editwin_fill_paragraph(owl_editwin *e)
1129{
1130  oe_excursion x;
1131  gunichar ch;
1132  int sentence;
1133
1134  oe_save_excursion(e, &x);
1135
1136  /* Mark the end of the paragraph */
1137  owl_editwin_forward_paragraph(e);
1138  /* Skip the trailing newline */
1139  owl_editwin_point_move(e, -1);
1140  owl_editwin_set_mark(e);
1141
1142  owl_editwin_backward_paragraph(e);
1143
1144  /* Don't mess with the leading newline */
1145  if (owl_editwin_get_char_at_point(e) == '\n')
1146    owl_editwin_point_move(e, 1);
1147
1148  /*
1149   * First pass: Scan forward replacing all series of spaces with ' '
1150   * (or nothing after CJK ideograms)
1151   */
1152  sentence = 0;
1153  for(;e->index < e->mark; owl_editwin_point_move(e, 1)) {
1154    /* bail if we hit a trailing dot on the buffer */
1155    if (strcmp(e->buff + e->index, "\n.") == 0) {
1156      owl_editwin_set_mark(e);
1157      break;
1158    }
1159
1160    ch = owl_editwin_get_char_at_point(e);
1161
1162    if (owl_util_can_break_after(ch) || ch == '\n') {
1163      if (g_unichar_isspace(ch)) {
1164        owl_editwin_replace(e, 1, " ");
1165      }
1166
1167      if (sentence && g_unichar_isspace(owl_editwin_get_char_at_point(e))
1168          && e->index < e->mark)
1169        owl_editwin_point_move(e, 1);
1170
1171      while(g_unichar_isspace(owl_editwin_get_char_at_point(e))
1172            && e->index < e->mark) {
1173        owl_editwin_delete_char(e);
1174      }
1175    }
1176
1177    if(ch == '.' || ch == '!' || ch == '?')
1178      sentence = 1;
1179    else
1180      sentence = 0;
1181  }
1182
1183  owl_editwin_backward_paragraph(e);
1184
1185  /* Now go through inserting newlines as needed */
1186  while(e->index < e->mark) {
1187    /* if we've travelled too far, linewrap */
1188    if (owl_editwin_current_column(e) >= e->fillcol)
1189      _owl_editwin_linewrap_word(e);
1190    owl_editwin_point_move(e, 1);
1191  }
1192
1193  oe_restore_excursion(e, &x);
1194}
1195
1196/* returns true if only whitespace remains */
1197int owl_editwin_is_at_end(owl_editwin *e)
1198{
1199  return (only_whitespace(e->buff + e->index));
1200}
1201
1202static int owl_editwin_check_dotsend(owl_editwin *e)
1203{
1204  int zdot = 0;
1205  oe_excursion x;
1206
1207  if (!e->dotsend) return(0);
1208  if (!owl_editwin_is_at_end(e)) return (0);
1209
1210  oe_save_excursion(e, &x);
1211
1212  owl_editwin_point_move(e, -3);
1213
1214  if(strncmp(e->buff + e->index, "\n.\n", 3) == 0) {
1215    owl_editwin_point_move(e, 1);
1216    zdot = 1;
1217  } else if(e->index == e->lock &&
1218            strncmp(e->buff + e->index, ".\n", 2) == 0) {
1219    zdot = 1;
1220  }
1221
1222  if(zdot) {
1223    owl_editwin_set_mark(e);
1224    owl_editwin_move_to_end(e);
1225    owl_editwin_replace_region(e, "");
1226  }
1227
1228  oe_restore_excursion(e, &x);
1229
1230  return zdot;
1231}
1232
1233void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
1234{
1235  /* XXX force a redisplay? */
1236  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
1237    owl_command_edit_done(e);
1238    return;
1239  }
1240  owl_editwin_redisplay(e, 0);
1241}
1242
1243static int oe_region_width(owl_editwin *e, int start, int end, int offset)
1244{
1245  const char *p;
1246  int width = offset;
1247 
1248  for(p = e->buff + start;
1249      p < e->buff + end;
1250      p = g_utf8_find_next_char(p, NULL))
1251    width += oe_char_width(g_utf8_get_char(p), width);
1252
1253  return width - offset;
1254}
1255
1256static void oe_insert_char(owl_editwin *e, gunichar c)
1257{
1258  oe_excursion x;
1259  char tmp[7];
1260  int replaced = -1;
1261
1262  if (c == '\r') /* translate CRs to NLs */
1263    c = '\n';
1264
1265  if (!g_unichar_iscntrl(c) || c == '\n' || c== '\t' ) {
1266    if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
1267      return;
1268    }
1269
1270    if (e->cursorx != -1 && e->cursorx + oe_char_width(c, e->cursorx) > e->wrapcol) {
1271      /* XXX this is actually wrong:
1272       * + If the line has been been wrapped, we can be past the wrap column but
1273       *   e->cursorx be much smaller.
1274       * + If the user went back and inserted a bunch of stuff in the middle of
1275       *   the line, there may be more than one word past the wrap column.
1276       */
1277      oe_save_excursion(e, &x);
1278
1279      if (c == ' ' || c == '\t') {
1280        owl_editwin_point_move(e, -1);
1281        replaced = -owl_editwin_move_if_in(e, -1, " \t");
1282        if (!replaced) {
1283          c = '\n';
1284          replaced = -1;
1285        }
1286      } else {
1287        while(!owl_editwin_at_beginning_of_line(e)) {
1288          owl_editwin_point_move(e, -1);
1289          if (owl_util_can_break_after(owl_editwin_get_char_at_point(e))) {
1290            replaced = -owl_editwin_move_if_in(e, -1, " \t");
1291            break;
1292          }
1293        }
1294        if (owl_editwin_at_beginning_of_line(e))
1295          replaced = -1;
1296      }
1297      if (replaced && !owl_editwin_at_beginning_of_line(e))
1298        owl_editwin_point_move(e, 1);
1299      if (replaced >= 0) {
1300        owl_editwin_replace(e, replaced, "\n");
1301      }
1302      oe_restore_excursion(e, &x);
1303    }
1304
1305    if (replaced >= 0 && (c == ' ' || c == '\t'))
1306      return; /* our work here is done */
1307
1308    tmp[g_unichar_to_utf8(c, tmp)] = '\0';
1309    owl_editwin_replace(e, 0, tmp);
1310  }
1311}
1312
1313void owl_editwin_process_char(owl_editwin *e, owl_input j)
1314{
1315  if (j.ch == ERR)
1316    return;
1317  /* Ignore ncurses control characters. */
1318  if (j.ch < 0x100) {
1319    oe_insert_char(e, j.uch);
1320  }
1321}
1322
1323const char *owl_editwin_get_text(owl_editwin *e)
1324{
1325  return(e->buff+e->lock);
1326}
1327
1328char *owl_editwin_get_region(owl_editwin *e)
1329{
1330  int start, end;
1331  start = e->index;
1332  end   = e->mark;
1333  if(start > end) {
1334    int tmp = end;
1335    end = start;
1336    start = tmp;
1337  }
1338
1339  return oe_chunk(e, start, end);
1340}
1341
1342int owl_editwin_get_echochar(owl_editwin *e)
1343{
1344  return e->echochar;
1345}
1346
1347static char *oe_chunk(owl_editwin *e, int start, int end)
1348{
1349  char *p;
1350 
1351  p = owl_malloc(end - start + 1);
1352  memcpy(p, e->buff + start, end - start);
1353  p[end - start] = 0;
1354
1355  return p;
1356}
1357
1358/*
1359 * The only guarantee made about these values is that comparisons
1360 * between them, as well as comparison between multiple calls to these
1361 * functions without modifying the editwin in-between, are meaningful.
1362 */
1363
1364int owl_editwin_get_point(owl_editwin *e)
1365{
1366  return e->index;
1367}
1368
1369int owl_editwin_get_mark(owl_editwin *e)
1370{
1371  return e->mark;
1372}
1373
1374
1375/*
1376 * Local Variables:
1377 * mode:C
1378 * c-basic-offset:2
1379 * End:
1380 */
Note: See TracBrowser for help on using the repository browser.