source: editwin.c @ b5ef65c

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