source: editwin.c @ 16cfd12a

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 16cfd12a was 16cfd12a, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
wrap words on insert character ow, brain hurty.
  • Property mode set to 100644
File size: 26.2 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2#include <stdlib.h>
3#include <unistd.h>
4#include <string.h>
[10b866d]5#include <ctype.h>
[7d4fbcd]6
[1aee7d9]7static const char fileIdent[] = "$Id$";
8
[ebf0128]9#define VALID_EXCURSION (0x9a2b4729)
10
11typedef struct oe_excursion_struct { /*noproto*/
12  int valid;
13  int index;
[16cfd12a]14  int mark;
[ebf0128]15  int goal_column;
16  int lock;
17  struct oe_excursion_struct *next;
18} oe_excursion;
19
[a556caa]20struct _owl_editwin { /*noproto*/
21  char *buff;
22  owl_history *hist;
23  int bufflen;
24  int allocated;
25  int index;
[16cfd12a]26  int mark;
[a556caa]27  int goal_column;
28  int topindex;
[ebf0128]29  int cursorx;
[a556caa]30  int winlines, wincols, fillcol, wrapcol;
31  WINDOW *curswin;
32  int style;
33  int lock;
34  int dotsend;
35  int echochar;
[ebf0128]36  oe_excursion *excursions;
[a556caa]37
38  char *command;
39  void (*callback)(struct _owl_editwin*);
40  void *cbdata;
41};
42
43
[e20d8179]44static void oe_reframe(owl_editwin *e);
[bab52da]45static void oe_save_excursion(owl_editwin *e, oe_excursion *x);
[ebf0128]46static void oe_release_excursion(owl_editwin *e, oe_excursion *x);
[bab52da]47static void oe_restore_excursion(owl_editwin *e, oe_excursion *x);
[ebf0128]48static int oe_count_glyphs(char *s);
49static int oe_char_width(gunichar c, int column);
[16cfd12a]50static int oe_region_width(owl_editwin *e, int start, int end, int width);
[bab52da]51static int oe_find_display_line(owl_editwin *e, int *x, int index);
[ebf0128]52static void oe_insert_char(owl_editwin *e, gunichar c);
[bab52da]53static int owl_editwin_limit_maxcols(int v, int maxv);
54static int owl_editwin_check_dotsend(owl_editwin *e);
55static int owl_editwin_is_char_in(owl_editwin *e, char *set);
56static gunichar owl_editwin_get_char_at_point(owl_editwin *e);
[ebf0128]57static int owl_editwin_replace(owl_editwin *e, int count, char *s);
[bab52da]58
[5b5f3e6]59#define INCR 4096
[7d4fbcd]60
[a556caa]61#define WHITESPACE " \n\t"
62
[bab52da]63owl_editwin *owl_editwin_allocate(void)
64{
[8321cb7]65  owl_editwin *e;
66  e = owl_malloc(sizeof(owl_editwin));
67  memset(e, 0, sizeof(*e));
68  return e;
[a556caa]69}
70
[5b5f3e6]71static int oe_count_glyphs(char *s)
72{
73  int count = 0;
74  char *p;
75
76  for(p = s; *p != 0; p = g_utf8_find_next_char(p, NULL))
77    if (!g_unichar_ismark(g_utf8_get_char(p)))
78      count++;
79
80  return count;
81}
82
[bab52da]83static inline void oe_set_index(owl_editwin *e, int index)
84{
[ebf0128]85  if (index != e->index) {
86    e->goal_column = -1;
87    e->cursorx = -1;
88  }
[521bc84]89  e->index = index;
90}
91
[cf83b7a]92/* initialize the editwin e.
93 * 'win' is an already initialzed curses window that will be used by editwin
94 */
[c9334b1]95void owl_editwin_init(owl_editwin *e, WINDOW *win, int winlines, int wincols, int style, owl_history *hist)
96{
[e20d8179]97  e->buff=owl_malloc(INCR);
[7d4fbcd]98  e->buff[0]='\0';
99  e->bufflen=0;
[10b866d]100  e->hist=hist;
[7d4fbcd]101  e->allocated=INCR;
[521bc84]102  oe_set_index(e, 0);
[16cfd12a]103  e->mark = 0;
[a556caa]104  e->goal_column = -1;
[ebf0128]105  e->cursorx = -1;
106  e->topindex = 0;
107  e->excursions = NULL;
108  owl_editwin_set_curswin(e, win, winlines, wincols);
[7d4fbcd]109  e->style=style;
110  if ((style!=OWL_EDITWIN_STYLE_MULTILINE) &&
111      (style!=OWL_EDITWIN_STYLE_ONELINE)) {
112    e->style=OWL_EDITWIN_STYLE_MULTILINE;
113  }
114  e->lock=0;
115  e->dotsend=0;
[c9334b1]116  e->echochar='\0';
[e74c01c]117
[af1920fd]118  /* We get initialized multiple times, but we need to hold on to
119     the callbacks, so we can't NULL them here. */
[e74c01c]120  /*
121    e->command = NULL;
122    e->callback = NULL;
123    e->cbdata = NULL;
124  */
[7d4fbcd]125  if (win) werase(win);
126}
127
[c9334b1]128void owl_editwin_set_curswin(owl_editwin *e, WINDOW *w, int winlines, int wincols)
129{
[7d4fbcd]130  e->curswin=w;
131  e->winlines=winlines;
132  e->wincols=wincols;
[9a6cc40]133  e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g));
134  e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g));
[7d4fbcd]135}
136
[c9334b1]137/* echo the character 'ch' for each normal character keystroke,
138 * excepting locktext.  This is useful for entering passwords etc.  If
139 * ch=='\0' characters are echo'd normally
140 */
141void owl_editwin_set_echochar(owl_editwin *e, int ch)
142{
143  e->echochar=ch;
144}
145
146WINDOW *owl_editwin_get_curswin(owl_editwin *e)
147{
[060b3b4]148  return(e->curswin);
[7d4fbcd]149}
150
[c9334b1]151owl_history *owl_editwin_get_history(owl_editwin *e)
152{
[060b3b4]153  return(e->hist);
[10b866d]154}
155
[c9334b1]156void owl_editwin_set_dotsend(owl_editwin *e)
157{
[7d4fbcd]158  e->dotsend=1;
159}
160
[bab52da]161void owl_editwin_set_command(owl_editwin *e, char *command)
162{
[e74c01c]163  if(e->command) owl_free(e->command);
164  e->command = owl_strdup(command);
165}
166
[bab52da]167char *owl_editwin_get_command(owl_editwin *e)
168{
[e74c01c]169  if(e->command) return e->command;
170  return "";
171}
172
[bab52da]173void owl_editwin_set_callback(owl_editwin *e, void (*cb)(owl_editwin*))
174{
[e74c01c]175  e->callback = cb;
176}
177
[bab52da]178void (*owl_editwin_get_callback(owl_editwin *e))(owl_editwin*)
179{
[e74c01c]180  return e->callback;
181}
182
[bab52da]183void owl_editwin_set_cbdata(owl_editwin *e, void *data)
184{
[e74c01c]185  e->cbdata = data;
186}
187
[a556caa]188void *owl_editwin_get_cbdata(owl_editwin *e) {
[e74c01c]189  return e->cbdata;
190}
191
192void owl_editwin_do_callback(owl_editwin *e) {
193  void (*cb)(owl_editwin*);
194  cb=owl_editwin_get_callback(e);
195  if(!cb) {
196    owl_function_error("Internal error: No editwin callback!");
197  } else {
[af1920fd]198    /* owl_function_error("text: |%s|", owl_editwin_get_text(e)); */
[e74c01c]199    cb(e);
200  }
201}
202
[bab52da]203static int owl_editwin_limit_maxcols(int v, int maxv)
[c9334b1]204{
[ebf0128]205  /* maxv > 5 ? MAX(v, vax) : v */
[d36f2cb]206  if (maxv > 5 && v > maxv) {
[060b3b4]207    return(maxv);
[d36f2cb]208  } else {
[060b3b4]209    return(v);
[d36f2cb]210  }
211}
212
[c9334b1]213/* set text to be 'locked in' at the beginning of the buffer, any
214 * previous text (including locked text) will be overwritten
215 */
216void owl_editwin_set_locktext(owl_editwin *e, char *text)
217{
[521bc84]218  oe_set_index(e, 0);
[5b5f3e6]219  e->lock = 0;
220  owl_editwin_replace(e, e->bufflen, text);
221  e->buff[e->bufflen] = 0;
222  e->lock=e->bufflen;
[521bc84]223  oe_set_index(e, e->lock);
[7d4fbcd]224  owl_editwin_redisplay(e, 0);
225}
226
[c9334b1]227int owl_editwin_get_style(owl_editwin *e)
228{
[7d4fbcd]229  return(e->style);
230}
231
[c9334b1]232void owl_editwin_new_style(owl_editwin *e, int newstyle, owl_history *h)
233{
[98f1e69]234  e->hist = h;
235
[7d4fbcd]236  if (e->style==newstyle) return;
237
238  if (newstyle==OWL_EDITWIN_STYLE_MULTILINE) {
239    e->style=newstyle;
240  } else if (newstyle==OWL_EDITWIN_STYLE_ONELINE) {
241    e->style=newstyle;
242
243    /* nuke everything after the first line */
[5b5f3e6]244    owl_editwin_move_to_top(e);
245    owl_editwin_move_to_end_of_line(e);
246    owl_editwin_replace(e, oe_count_glyphs(e->buff + e->index),  "");
[7d4fbcd]247  }
248}
249
[c9334b1]250/* completly reinitialize the buffer */
251void owl_editwin_fullclear(owl_editwin *e)
252{
[7d4fbcd]253  owl_free(e->buff);
[10b866d]254  owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style, e->hist);
[7d4fbcd]255}
256
[c9334b1]257/* clear all text except for locktext and put the cursor at the
258 * beginning
259 */
260void owl_editwin_clear(owl_editwin *e)
261{
262
[6073462]263  int lock = e->lock;
[10b866d]264  int dotsend=e->dotsend;
[7d4fbcd]265  char *locktext=NULL;
[cc6f009]266  char echochar=e->echochar;
[10b866d]267
[6073462]268  if (lock > 0) {
[5b5f3e6]269    locktext = owl_malloc(lock+1);
270    strncpy(locktext, e->buff, lock);
271    locktext[lock] = 0;
[7d4fbcd]272  }
273
274  owl_free(e->buff);
[10b866d]275  owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style, e->hist);
[7d4fbcd]276
277  if (lock > 0) {
278    owl_editwin_set_locktext(e, locktext);
279  }
[10b866d]280  if (dotsend) {
281    owl_editwin_set_dotsend(e);
282  }
[cc6f009]283  if (echochar) {
284    owl_editwin_set_echochar(e, echochar);
285  }
[7d4fbcd]286
[5b5f3e6]287  if (locktext)
288    owl_free(locktext);
[6073462]289
[521bc84]290  oe_set_index(e, lock);
[7d4fbcd]291}
292
[c9334b1]293void owl_editwin_recenter(owl_editwin *e)
294{
[a556caa]295  e->topindex = -1;
296}
[e20d8179]297
[a0fbdee]298static void oe_save_excursion(owl_editwin *e, oe_excursion *x)
299{
300  x->index = e->index;
301  x->goal_column = e->goal_column;
302  x->lock = e->lock;
[ebf0128]303
304  x->valid = VALID_EXCURSION;
305  x->next = e->excursions;
306  e->excursions = x;
307}
308
309static void oe_release_excursion(owl_editwin *e, oe_excursion *x)
310{
311  oe_excursion *p;
312
313  x->valid = 0;
314  if (e->excursions == NULL)
315    /* XXX huh. */ ;
316  else if (e->excursions == x)
317    e->excursions = x->next;
318  else {
319    for (p = e->excursions; p->next != NULL; p = p->next)
320      if (p->next == x) {
321        p->next = p->next->next;
322        break;
323      }
324    /* and if we ran off the end? XXX */
325  }
[a0fbdee]326}
327
328static void oe_restore_excursion(owl_editwin *e, oe_excursion *x)
329{
[ebf0128]330  if (x->valid == VALID_EXCURSION) {
331    oe_set_index(e, x->index);
332    e->goal_column = x->goal_column;
333    e->lock = x->lock;
334
335    oe_release_excursion(e, x);
336  }
[a0fbdee]337}
338
[e20d8179]339static inline char *oe_next_point(owl_editwin *e, char *p)
340{
341  char *boundary = e->buff + e->bufflen + 1;
342  char *q;
343
344  q = g_utf8_find_next_char(p, boundary);
345  while (q && g_unichar_ismark(g_utf8_get_char(q)))
346    q = g_utf8_find_next_char(q, boundary);
347
348  if (q == p)
349    return NULL;
350  return q;
351}
352
353static inline char *oe_prev_point(owl_editwin *e, char *p)
354{
355  char *boundary = e->buff + e->lock;
356
357  p = g_utf8_find_prev_char(boundary, p);
358  while (p && g_unichar_ismark(g_utf8_get_char(p)))
359    p = g_utf8_find_prev_char(boundary, p);
360
361  return p;
362}
363
[ebf0128]364static int oe_char_width(gunichar c, int column)
365{
366  int cw;
367
368  if (c == 9) /* TAB */
369    return TABSIZE - column % TABSIZE;
370
371  cw = mk_wcwidth(c);
372
373  if (cw < 0) /* control characters */
374    cw = 0;
375
376  return cw;
377}
378
[19f765d]379static int oe_find_display_line(owl_editwin *e, int *x, int index)
380{
381  int width = 0, cw;
[ebf0128]382  gunichar c;
[19f765d]383  char *p;
384
[77d4402]385  while(1) {
386    /* note the position of the dot */
[2021bea]387    if (x != NULL && index == e->index && width < e->wincols)
[19f765d]388      *x = width;
[77d4402]389
[19f765d]390    /* get the current character */
391    c = g_utf8_get_char(e->buff + index);
392
393    /* figure out how wide it is */
[ebf0128]394    cw = oe_char_width(c, width);
[19f765d]395
[77d4402]396    if (width + cw > e->wincols) {
397      if (x != NULL && *x == width)
398        *x = -1;
[19f765d]399      break;
[77d4402]400    }
[19f765d]401    width += cw;
402
403    if (c == '\n') {
[77d4402]404      if (width < e->wincols)
405        ++index; /* skip the newline */
[19f765d]406      break;
407    }
408
409    /* find the next character */
410    p = oe_next_point(e, e->buff + index);
411    if (p == NULL) { /* we ran off the end */
412      if (x != NULL && e->index > index)
413        *x = width + 1;
414      break;
415    }
416    index = p - e->buff;
[cedc95c]417
[19f765d]418  }
419  return index;
420}
421
[e20d8179]422static void oe_reframe(owl_editwin *e) {
[cedc95c]423  oe_excursion x;
424  int goal = e->winlines / 2;
425  int index;
426  int count = 0;
427  int point;
428  int n, i;
429  int last;
430
431  oe_save_excursion(e, &x);
432  /* step back line-by-line through the buffer until we have >= goal lines of
433     display text */
434  e->lock = 0; /* we can (must) tread on the locktext */
435
436  point = e->index;
437  last = -1;
438  while (count < goal) {
439    index = e->index;
440    owl_editwin_move_to_beginning_of_line(e);
441    if (last == e->index)
442      break;
443    last = e->index;
444    for (n = 0, i = e->index; i < index; n++)
445      i = oe_find_display_line(e, NULL, i);
446    count += n == 0 ? 1 : n;
447    if (count < goal)
448      owl_editwin_point_move(e, -1);
449  }
450
451  e->topindex = e->index;
[521bc84]452  /* if we overshot, backtrack */
453  for (n = 0; n < (count - goal); n++)
454    e->topindex = oe_find_display_line(e, NULL, e->topindex);
[cedc95c]455
456  oe_restore_excursion(e, &x);
[7d4fbcd]457}
458
[c9334b1]459/* regenerate the text on the curses window */
460/* if update == 1 then do a doupdate(), otherwise do not */
461void owl_editwin_redisplay(owl_editwin *e, int update)
462{
[19f765d]463  int x = -1, y = -1, t;
[77d4402]464  int line, index, lineindex, times = 0;
[7d4fbcd]465
[bd1a1ae]466  do {
[cedc95c]467    werase(e->curswin);
468
[bd1a1ae]469    if (e->topindex == -1 || e->index < e->topindex)
[e20d8179]470      oe_reframe(e);
471
[bd1a1ae]472    line = 0;
473    index = e->topindex;
474    while(line < e->winlines) {
475      lineindex = index;
[19f765d]476      t = -1;
477      index = oe_find_display_line(e, &t, lineindex);
478      if (x == -1 && t != -1)
479        x = t, y = line;
[bd1a1ae]480      if (index - lineindex)
481        mvwaddnstr(e->curswin, line, 0,
482                   e->buff + lineindex,
483                   index - lineindex);
484      line++;
[a556caa]485    }
[19f765d]486    if (x == -1)
[bd1a1ae]487        e->topindex = -1; /* force a reframe */
[77d4402]488    times++;
489  } while(x == -1 && times < 3);
[e20d8179]490
[19f765d]491  wmove(e->curswin, y, x);
[ebf0128]492  e->cursorx = x;
493
[7d4fbcd]494  wnoutrefresh(e->curswin);
[a556caa]495  if (update == 1)
[7d4fbcd]496    doupdate();
497}
498
[ebf0128]499/* replace count characters at the point with s, returning the change in size */
500static int owl_editwin_replace(owl_editwin *e, int replace, char *s)
501{
502  int start, end, i, free, need, size, change;
[5b5f3e6]503  char *p;
[ebf0128]504  oe_excursion *x;
[e9bb404]505
[5b5f3e6]506  if (!g_utf8_validate(s, -1, NULL)) {
507    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
[ebf0128]508    return 0;
[47519e1b]509  }
510
[5b5f3e6]511  start = e->index;
512  for (i = 0, p = e->buff + start; i < replace && p != NULL; i++)
513    p = oe_next_point(e, p);
514  if (p != NULL)
515    end = p - e->buff;
516  else
517    end = e->bufflen;
518
519  free = e->allocated - e->bufflen + end - start;
520
521  need = strlen(s) - free;
522  if (need > 0) {
523    size = e->allocated + need + INCR - (need % INCR);
524    p = realloc(e->buff, size);
525    if (p == NULL) {
526      /* XXX signal impending doom somehow and don't do anything */
[ebf0128]527      return 0;
[e9bb404]528    }
[5b5f3e6]529    e->buff = p;
530    e->allocated = size;
[47519e1b]531  }
[e9bb404]532
[5b5f3e6]533  memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end);
534  memcpy(e->buff + start, s, strlen(s));
[ebf0128]535  change = start - end + strlen(s);
536  e->bufflen += change;
[5b5f3e6]537  e->index += strlen(s);
[ebf0128]538
539  /* fix up any saved points after the replaced area */
540  for (x = e->excursions; x != NULL; x = x->next)
541    if (x->index > start) {
542      if (x->index < end)
543        x->index = end;
544      else
545        x->index += change;
546    }
547
548  return change;
[47519e1b]549}
550
[5b5f3e6]551#if 0 /*XXX*/
[c9334b1]552/* linewrap the word just before the cursor.
553 * returns 0 on success
554 * returns -1 if we could not wrap.
555 */
[5b5f3e6]556static int _owl_editwin_linewrap_word(owl_editwin *e)
[c9334b1]557{
[84027015]558  int i;
559  char *ptr1, *start;
560  gunichar c;
[7d4fbcd]561
[84027015]562  start = e->buff + e->lock;
563
[a556caa]564  ptr1 = e->buff + e->index;
[84027015]565  ptr1 = g_utf8_find_prev_char(start, ptr1);
566
567  while (ptr1) {
568    c = g_utf8_get_char(ptr1);
569    if (owl_util_can_break_after(c)) {
570      if (c != ' ') {
571        i = ptr1 - e->buff;
[521bc84]572        oe_set_index(e, i);
[b2c1bd4]573        _owl_editwin_insert_bytes(e, 1);
574        /* _owl_editwin_insert_bytes may move e->buff. */
[84027015]575        ptr1 = e->buff + i;
576      }
577      *ptr1 = '\n';
578      return 0;
[7d4fbcd]579    }
[2d4ff14]580    else if (c == '\n') {
581      return 0;
582    }
[84027015]583    ptr1 = g_utf8_find_prev_char(start, ptr1);
[7d4fbcd]584  }
[84027015]585  return -1;
[7d4fbcd]586}
[a556caa]587#endif
[7d4fbcd]588
[c9334b1]589/* delete the character at the current point, following chars
590 * shift left.
[e20d8179]591 */
[c9334b1]592void owl_editwin_delete_char(owl_editwin *e)
593{
[5b5f3e6]594  owl_editwin_replace(e, 1, "");
[7d4fbcd]595}
596
[c9334b1]597/* Swap the character at point with the character at point-1 and
598 * advance the pointer.  If point is at beginning of buffer do
599 * nothing.  If point is after the last character swap point-1 with
[e20d8179]600 * point-2.  (Behaves as observed in tcsh and emacs).
[c9334b1]601 */
602void owl_editwin_transpose_chars(owl_editwin *e)
603{
[5b5f3e6]604  char *middle, *end, *start, *tmp;
[f2e36b5]605
[47519e1b]606  if (e->bufflen == 0) return;
[e20d8179]607
[5b5f3e6]608  if (e->index == e->bufflen)
609    owl_editwin_point_move(e, -1);     /* point is after last character */
[f2e36b5]610
[5b5f3e6]611  if (owl_editwin_at_beginning_of_buffer(e))
612    return;     /* point is at beginning of buffer, do nothing */
[f2e36b5]613
[47519e1b]614  /* Transpose two utf-8 unicode glyphs. */
[5b5f3e6]615  middle = e->buff + e->index;
[47519e1b]616
[5b5f3e6]617  end = oe_next_point(e, middle);
618  if (end == NULL)
[e20d8179]619    return;
[47519e1b]620
[5b5f3e6]621  start = oe_prev_point(e, middle);
622  if (start == NULL)
[e20d8179]623    return;
[47519e1b]624
[5b5f3e6]625  tmp = owl_malloc((end - start) + 1);
626  tmp[(end - start)] = 0;
627  memcpy(tmp, middle, end - middle);
628  memcpy(tmp + (end - middle), start, middle - start);
629
630  owl_editwin_point_move(e, -1);
631  owl_editwin_replace(e, 2, tmp);
[f2e36b5]632}
633
[c9334b1]634/* insert 'string' at the current point, later text is shifted
635 * right
636 */
[5b5f3e6]637void owl_editwin_insert_string(owl_editwin *e, char *s)
[c9334b1]638{
[5b5f3e6]639  owl_editwin_replace(e, 0, s);
[7d4fbcd]640}
641
[a556caa]642/* We assume index is not set to point to a mid-char */
[bab52da]643static gunichar owl_editwin_get_char_at_point(owl_editwin *e)
[c9334b1]644{
[a556caa]645  return g_utf8_get_char(e->buff + e->index);
646}
[7d4fbcd]647
[e20d8179]648int owl_editwin_point_move(owl_editwin *e, int delta)
[84027015]649{
[e20d8179]650  char *p;
[19f765d]651  int change, d = 0;
[a556caa]652
653  change = MAX(delta, - delta);
654  p = e->buff + e->index;
655
656  while (d < change && p != NULL) {
[e20d8179]657    if (delta > 0)
658      p = oe_next_point(e, p);
659    else
660      p = oe_prev_point(e, p);
661    if (p != NULL) {
[521bc84]662      oe_set_index(e, p - e->buff);
[e20d8179]663      d++;
[a556caa]664    }
665  }
666
667  return delta > 0 ? d : -d;
[84027015]668}
669
[a556caa]670int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
671  if (e->index == e->lock)
672    return 1;
[84027015]673
[a556caa]674  return 0;
675}
676
677int owl_at_end_of_buffer(owl_editwin *e) {
678  if (e->index == e->bufflen)
679    return 1;
680
681  return 0;
682}
683
684int owl_editwin_at_beginning_of_line(owl_editwin *e) /*noproto*/
[c9334b1]685{
[a0fbdee]686  oe_excursion x;
[a556caa]687  int ret;
[47519e1b]688
[a556caa]689  if (owl_editwin_at_beginning_of_buffer(e))
690    return 1;
[47519e1b]691
[a0fbdee]692  oe_save_excursion(e, &x);
[a556caa]693  owl_editwin_point_move(e, -1);
[bab52da]694  ret = (owl_editwin_get_char_at_point(e) == '\n');
[a0fbdee]695  oe_restore_excursion(e, &x);
[a556caa]696
697  return ret;
698}
699
[e20d8179]700static int owl_editwin_is_char_in(owl_editwin *e, char *set)
[a556caa]701{
702  char *p;
[5b5f3e6]703
704  for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL))
705    if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p))
[a556caa]706      return 1;
707  return 0;
708}
709
[e20d8179]710int owl_editwin_move_if_in(owl_editwin *e, int delta, char *set)
[a556caa]711{
712  int change, distance = 0;
[e20d8179]713  while (owl_editwin_is_char_in(e, set)) {
[a556caa]714    change = owl_editwin_point_move(e, delta);
715    distance += change;
716    if (change == 0)
717      break;
[47519e1b]718  }
[a556caa]719  return distance;
720}
721
[e20d8179]722int owl_editwin_move_if_not_in(owl_editwin *e, int delta, char *set)
[a556caa]723{
724  int change, distance = 0;
[e20d8179]725  while (!owl_editwin_is_char_in(e, set)) {
[a556caa]726    change = owl_editwin_point_move(e, delta);
727    distance += change;
728    if (change == 0)
729      break;
[7d4fbcd]730  }
[a556caa]731  return distance;
[7d4fbcd]732}
733
[e20d8179]734int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
[47519e1b]735{
[a556caa]736  int distance = 0;
[47519e1b]737
[a556caa]738  if (!owl_editwin_at_beginning_of_line(e)) {
739    /* move off the \n if were at the end of a line */
740    distance += owl_editwin_point_move(e, -1);
741    distance += owl_editwin_move_if_not_in(e, -1, "\n");
742    if (distance && !owl_editwin_at_beginning_of_buffer(e))
743      distance += owl_editwin_point_move(e, 1);
[47519e1b]744  }
[77d4402]745  e->goal_column = 0; /* subtleties */
[84027015]746
[a556caa]747  return distance;
748}
[e20d8179]749
750int owl_editwin_move_to_end_of_line(owl_editwin *e)
[a556caa]751{
752  return owl_editwin_move_if_not_in(e, 1, "\n");
[47519e1b]753}
754
[e20d8179]755int owl_editwin_line_move(owl_editwin *e, int delta)
[c9334b1]756{
[d7043b4]757  int goal_column, change, ll, distance;
758  int count = 0;
[a556caa]759
760  change = MAX(delta, -delta);
761
[d7043b4]762  goal_column = e->goal_column;
763  distance = owl_editwin_move_to_beginning_of_line(e);
764  goal_column = goal_column == -1 ? -distance : goal_column;
[a556caa]765
766  while(count < change) {
767    if (delta > 0) {
768      distance += owl_editwin_move_if_not_in(e, 1, "\n");
769      distance += owl_editwin_point_move(e, 1);
770    } else {
771      /* I really want to assert delta < 0 here */
772      distance += owl_editwin_point_move(e, -1); /* to the newline on
773                                                    the previous line */
774      distance += owl_editwin_move_to_beginning_of_line(e);
775    }
776    count++;
[7d4fbcd]777  }
[a556caa]778
779  distance += (ll = owl_editwin_move_to_end_of_line(e));
780  if (ll > goal_column)
781    distance += owl_editwin_point_move(e, goal_column - ll);
782
783  e->goal_column = goal_column;
784
785  return distance;
[7d4fbcd]786}
787
[c9334b1]788void owl_editwin_backspace(owl_editwin *e)
789{
[7d4fbcd]790  /* delete the char before the current one
791   * and shift later chars left
792   */
[a556caa]793  if(owl_editwin_point_move(e, -1))
[7d4fbcd]794    owl_editwin_delete_char(e);
795}
796
[c9334b1]797void owl_editwin_key_up(owl_editwin *e)
798{
[a556caa]799  owl_editwin_line_move(e, -1);
[7d4fbcd]800}
801
[c9334b1]802void owl_editwin_key_down(owl_editwin *e)
803{
[a556caa]804  owl_editwin_line_move(e, 1);
[7d4fbcd]805}
806
[c9334b1]807void owl_editwin_key_left(owl_editwin *e)
808{
[a556caa]809  owl_editwin_point_move(e, -1);
[7d4fbcd]810}
811
[c9334b1]812void owl_editwin_key_right(owl_editwin *e)
813{
[a556caa]814  owl_editwin_point_move(e, 1);
[7d4fbcd]815}
816
[5b5f3e6]817int owl_editwin_forward_word(owl_editwin *e)
[c9334b1]818{
[5b5f3e6]819  int distance;
[7d4fbcd]820  /* if we're starting on a space, find the first non-space */
[5b5f3e6]821  distance = owl_editwin_move_if_in(e, 1, WHITESPACE);
[7d4fbcd]822
[a556caa]823  /* now find the end of this word */
[5b5f3e6]824  distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE);
825
826  return distance;
827}
828
829void owl_editwin_move_to_nextword(owl_editwin *e)
830{
831  owl_editwin_forward_word(e);
[7d4fbcd]832}
833
[c9334b1]834/* go backwards to the last non-space character
835 */
[5b5f3e6]836int owl_editwin_backward_word(owl_editwin *e)
[c9334b1]837{
[a0fbdee]838  oe_excursion x;
[5b5f3e6]839  int distance = 0;
840  int further = 0;
[a556caa]841  int beginning;
842  /* if in middle of word, beginning of word */
843
844  /* if at beginning of a word, find beginning of previous word */
845
[e20d8179]846  if (owl_editwin_is_char_in(e, WHITESPACE)) {
[a556caa]847    /* if in whitespace past end of word, find a word , the find the beginning*/
[5b5f3e6]848    distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
849                                                              character of the word */
[a0fbdee]850    oe_save_excursion(e, &x);
[a556caa]851    /* are we at the beginning of a word? */
852    owl_editwin_point_move(e, -1);
[e20d8179]853    beginning = owl_editwin_is_char_in(e, WHITESPACE);
[a0fbdee]854    oe_restore_excursion(e, &x);
[a556caa]855    if (beginning)
[5b5f3e6]856      return distance;
[a556caa]857   } else {
858    /* in the middle of the word; */
[a0fbdee]859    oe_save_excursion(e, &x);
[5b5f3e6]860    further += owl_editwin_point_move(e, -1);
[e20d8179]861    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
[5b5f3e6]862      distance += owl_editwin_backward_word(e); /* previous case */
[8321cb7]863      oe_release_excursion(e, &x);
[5b5f3e6]864      return distance + further;
[a556caa]865    } else {
[a0fbdee]866      oe_restore_excursion(e, &x);
[7d4fbcd]867    }
868  }
[5b5f3e6]869  distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE);
[a556caa]870  /* will go past */
871  if (e->index > e->lock)
[5b5f3e6]872    distance += owl_editwin_point_move(e, 1);
873  return distance;
874}
875
876void owl_editwin_move_to_previousword(owl_editwin *e)
877{
878  owl_editwin_backward_word(e);
[7d4fbcd]879}
880
[c9334b1]881void owl_editwin_delete_nextword(owl_editwin *e)
882{
[a0fbdee]883  oe_excursion x;
[5b5f3e6]884  int distance;
[a556caa]885
[a0fbdee]886  oe_save_excursion(e, &x);
[5b5f3e6]887  distance = owl_editwin_forward_word(e);
[a0fbdee]888  oe_restore_excursion(e, &x);
[5b5f3e6]889  owl_editwin_replace(e, distance, "");
[7d4fbcd]890}
891
[c9334b1]892void owl_editwin_delete_previousword(owl_editwin *e)
893{
[b68f9cd]894  /* go backwards to the last non-space character, then delete chars */
[5b5f3e6]895  int distance;
[b68f9cd]896
[5b5f3e6]897  distance = owl_editwin_backward_word(e);
898  owl_editwin_replace(e, -distance, "");
[b68f9cd]899}
900
[a556caa]901void owl_editwin_move_to_line_end(owl_editwin *e)
[c9334b1]902{
[a556caa]903  owl_editwin_move_to_end_of_line(e);
[7d4fbcd]904}
905
[a556caa]906void owl_editwin_delete_to_endofline(owl_editwin *e)
[c9334b1]907{
[a0fbdee]908  oe_excursion x;
[5b5f3e6]909  int distance;
[a556caa]910
[a0fbdee]911  oe_save_excursion(e, &x);
[5b5f3e6]912  distance = owl_editwin_move_to_end_of_line(e);
[a0fbdee]913  oe_restore_excursion(e, &x);
[5b5f3e6]914  owl_editwin_replace(e, distance ? distance : 1, "");
[7d4fbcd]915}
916
[c9334b1]917void owl_editwin_move_to_line_start(owl_editwin *e)
918{
[a556caa]919  owl_editwin_move_to_beginning_of_line(e);
[7d4fbcd]920}
921
[c9334b1]922void owl_editwin_move_to_end(owl_editwin *e)
923{
[521bc84]924  oe_set_index(e, e->bufflen);
[7d4fbcd]925}
926
[c9334b1]927void owl_editwin_move_to_top(owl_editwin *e)
928{
[521bc84]929  oe_set_index(e, e->lock);
[7d4fbcd]930}
931
[c9334b1]932void owl_editwin_fill_paragraph(owl_editwin *e)
933{
[a556caa]934#if 0 /* XXX */
[a0fbdee]935  oe_excursion x;
[7d4fbcd]936  int i, save;
937
938  /* save our starting point */
[a0fbdee]939  oe_save_excursion(e, &x);
[a556caa]940
941  save = e->index;
[7d4fbcd]942
943  /* scan back to the beginning of this paragraph */
944  for (i=save; i>=e->lock; i--) {
945    if ( (i<=e->lock) ||
946         ((e->buff[i]=='\n') && (e->buff[i-1]=='\n'))) {
[521bc84]947      oe_set_index(i + 1);
[7d4fbcd]948      break;
949    }
950  }
951
952  /* main loop */
953  while (1) {
[47519e1b]954    i = _owl_editwin_get_index_from_xy(e);
[7d4fbcd]955
956    /* bail if we hit the end of the buffer */
[47519e1b]957    if (i >= e->bufflen || e->buff[i] == '\0') break;
[7d4fbcd]958
959    /* bail if we hit the end of the paragraph */
[47519e1b]960    if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break;
[7d4fbcd]961
[50e671c]962    /* bail if we hit a trailing dot on the buffer */
963    if (e->buff[i] == '\n' && e->buff[i+1] == '.'
964        && ((i+2) >= e->bufflen || e->buff[i+2] == '\0'))
965      break;
966
[7d4fbcd]967    /* if we've travelled too far, linewrap */
968    if ((e->buffx) >= e->fillcol) {
[b2c1bd4]969      int len = e->bufflen;
[7d4fbcd]970      _owl_editwin_linewrap_word(e);
[b2c1bd4]971      /* we may have added a character. */
972      if (i < save) save += e->bufflen - len;
[521bc84]973      oe_set_index(i);
[7d4fbcd]974    }
975
976    /* did we hit the end of a line too soon? */
[84027015]977    /* asedeno: Here we replace a newline with a space. We may want to
978       consider removing the space if the characters to either side
979       are CJK ideograms.*/
[47519e1b]980    i = _owl_editwin_get_index_from_xy(e);
981    if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) {
[7d4fbcd]982      /* ********* we need to make sure we don't pull in a word that's too long ***********/
983      e->buff[i]=' ';
984    }
[b2c1bd4]985
[7d4fbcd]986    /* fix spacing */
[47519e1b]987    i = _owl_editwin_get_index_from_xy(e);
988    if (e->buff[i] == ' ' && e->buff[i+1] == ' ') {
989      if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') {
[7d4fbcd]990        owl_editwin_key_right(e);
991      } else {
992        owl_editwin_delete_char(e);
[84027015]993        /* if we did this ahead of the save point, adjust it. Changing
994           by one is fine here because we're only removing an ASCII
995           space. */
[47519e1b]996        if (i < save) save--;
[7d4fbcd]997      }
998    } else {
999      owl_editwin_key_right(e);
1000    }
1001  }
1002
1003  /* put cursor back at starting point */
[a0fbdee]1004  oe_restore_excursion(e, &x);
[a556caa]1005#endif
[7d4fbcd]1006}
1007
[cf83b7a]1008/* returns true if only whitespace remains */
[c9334b1]1009int owl_editwin_is_at_end(owl_editwin *e)
1010{
[a556caa]1011  return (only_whitespace(e->buff + e->index));
[217a43e]1012}
1013
[bab52da]1014static int owl_editwin_check_dotsend(owl_editwin *e)
[c9334b1]1015{
[47519e1b]1016  char *p, *p_n, *p_p;
1017  gunichar c;
[7d4fbcd]1018
1019  if (!e->dotsend) return(0);
[47519e1b]1020
1021  p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen);
1022  p_n = g_utf8_find_next_char(p, NULL);
1023  p_p = g_utf8_find_prev_char(e->buff, p);
1024  c = g_utf8_get_char(p);
1025  while (p != NULL) {
1026    if (*p == '.'
1027        && p_p != NULL && (*p_p == '\n' || *p_p == '\r')
1028        && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) {
1029      e->bufflen = p - e->buff;
1030      e->buff[e->bufflen] = '\0';
[7d4fbcd]1031      return(1);
1032    }
[47519e1b]1033    if (c != '\0' && !g_unichar_isspace(c)) return(0);
1034    p_n = p;
1035    p = p_p;
1036    c = g_utf8_get_char(p);
1037    p_p = g_utf8_find_prev_char(e->buff, p);
[7d4fbcd]1038  }
1039  return(0);
1040}
1041
[fac5463]1042void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
[c9334b1]1043{
[a556caa]1044  /* XXX force a redisplay? */
[fac5463]1045  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
[7d4fbcd]1046    owl_command_editmulti_done(e);
1047    return;
1048  }
[e20d8179]1049  owl_editwin_redisplay(e, 0);
[7d4fbcd]1050}
1051
[16cfd12a]1052static int oe_region_width(owl_editwin *e, int start, int end, int width)
1053{
1054  char *p;
1055  for(p = e->buff + start;
1056      p < e->buff + end;
1057      p = g_utf8_find_next_char(p, NULL))
1058    width += oe_char_width(g_utf8_get_char(p), width);
1059
1060  return width;
1061}
1062
[ebf0128]1063static void oe_insert_char(owl_editwin *e, gunichar c)
[fac5463]1064{
[16cfd12a]1065  oe_excursion x;
[5b5f3e6]1066  char tmp[7];
[16cfd12a]1067  int replaced = -1;
[5b5f3e6]1068
[ebf0128]1069  if (c == '\r') /* translate CRs to NLs */
1070    c = '\n';
1071
[16cfd12a]1072  if (!g_unichar_iscntrl(c) || c == '\n' || c== '\t' ) {
[ebf0128]1073    memset(tmp, 0, 7);
1074
1075    if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
1076      return;
1077    }
1078
[16cfd12a]1079    if (e->cursorx != -1 && e->cursorx + oe_char_width(c, e->cursorx) > e->wrapcol) {
1080      /* XXX this is actually wrong:
1081       * + If the line has been been wrapped, we can be past the wrap column but
1082       *   e->cursorx be much smaller.
1083       * + If the user went back and inserted a bunch of stuff in the middle of
1084       *   the line, there may be more than one word past the wrap column.
1085       */
1086      oe_save_excursion(e, &x);
1087
1088      if (c == ' ' || c == '\t') {
1089        owl_editwin_point_move(e, -1);
1090        replaced = -owl_editwin_move_if_in(e, -1, " \t");
1091        if (!replaced) {
1092          c = '\n';
1093          replaced = -1;
1094        }
1095      } else {
1096        while(!owl_editwin_at_beginning_of_line(e)) {
1097          owl_editwin_point_move(e, -1);
1098          if (owl_util_can_break_after(owl_editwin_get_char_at_point(e))) {
1099            replaced = -owl_editwin_move_if_in(e, -1, " \t");
1100            break;
1101          }
1102        }
1103        if (owl_editwin_at_beginning_of_line(e))
1104          replaced = -1;
1105      }
1106      if (replaced && !owl_editwin_at_beginning_of_line(e))
1107        owl_editwin_point_move(e, 1);
1108      if (replaced >= 0) {
1109        owl_editwin_replace(e, replaced, "\n");
1110      }
1111     
1112      oe_restore_excursion(e, &x);
1113    }
1114
1115    if (replaced >= 0 && (c == ' ' || c == '\t'))
1116      return; /* our work here is done */
1117
[ebf0128]1118    g_unichar_to_utf8(c, tmp);
1119    owl_editwin_replace(e, 0, tmp);
1120  }
1121}
1122
1123void owl_editwin_process_char(owl_editwin *e, owl_input j)
1124{
[5b5f3e6]1125  if (j.ch == ERR)
1126    return;
[fac5463]1127  /* Ignore ncurses control characters. */
[e20d8179]1128  if (j.ch < 0x100) {
[ebf0128]1129    oe_insert_char(e, j.uch);
[fac5463]1130  }
1131}
1132
[c9334b1]1133char *owl_editwin_get_text(owl_editwin *e)
1134{
[7d4fbcd]1135  return(e->buff+e->lock);
1136}
1137
[a556caa]1138int owl_editwin_get_echochar(owl_editwin *e) {
1139  return e->echochar;
1140}
[2f21a41]1141
1142
1143/*
1144 * Local Variables:
1145 * mode:C
1146 * c-basic-offset:2
1147 * End:
1148 */
Note: See TracBrowser for help on using the repository browser.