source: editwin.c @ 0cfa6ee

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