source: editwin.c @ a0fbdee

release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since a0fbdee was a0fbdee, checked in by Nelson Elhage <nelhage@mit.edu>, 12 years ago
s/_?owl_editwin_(save_|restore_|)excursion/oe_\1excursion/ (and move the functions up in the file)
  • Property mode set to 100644
File size: 25.4 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;
346  }
347  return index;
348}
349
[e20d8179]350static void oe_reframe(owl_editwin *e) {
[bd1a1ae]351  e->topindex = 0; /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
[7d4fbcd]352}
353
[c9334b1]354/* regenerate the text on the curses window */
355/* if update == 1 then do a doupdate(), otherwise do not */
356void owl_editwin_redisplay(owl_editwin *e, int update)
357{
[19f765d]358  int x = -1, y = -1, t;
359  int line, index, lineindex;
[7d4fbcd]360
361  werase(e->curswin);
[e0ffe77]362
[bd1a1ae]363  do {
364    if (e->topindex == -1 || e->index < e->topindex)
[e20d8179]365      oe_reframe(e);
366
[bd1a1ae]367    line = 0;
368    index = e->topindex;
369    while(line < e->winlines) {
370      lineindex = index;
[19f765d]371      t = -1;
372      index = oe_find_display_line(e, &t, lineindex);
373      if (x == -1 && t != -1)
374        x = t, y = line;
[bd1a1ae]375      if (index - lineindex)
376        mvwaddnstr(e->curswin, line, 0,
377                   e->buff + lineindex,
378                   index - lineindex);
379      line++;
[a556caa]380    }
[19f765d]381    if (x == -1)
[bd1a1ae]382        e->topindex = -1; /* force a reframe */
383  } while(x == -1);
[e20d8179]384
[19f765d]385  wmove(e->curswin, y, x);
[7d4fbcd]386  wnoutrefresh(e->curswin);
[a556caa]387  if (update == 1)
[7d4fbcd]388    doupdate();
389}
390
[47519e1b]391/* Remove n bytes at cursor. */
392void _owl_editwin_remove_bytes(owl_editwin *e, int n) /*noproto*/
393{
[a556caa]394  int i = e->index + n;
[47519e1b]395  for (; i < e->bufflen; i++) {
396    e->buff[i-n] = e->buff[i];
397  }
[e20d8179]398
[47519e1b]399  e->bufflen -= n;
400  e->buff[e->bufflen] = '\0';
401}
[830c36e]402
[47519e1b]403/* Insert n bytes at cursor.*/
404void _owl_editwin_insert_bytes(owl_editwin *e, int n) /*noproto*/
405{
[a556caa]406  int i;
[e9bb404]407
[47519e1b]408  if ((e->bufflen + n) > (e->allocated - 5)) {
409    _owl_editwin_addspace(e);
410  }
411
[a556caa]412  if(e->index != e->bufflen) {
413    for (i = e->bufflen + n - 1; i > e->index; i--) {
[e9bb404]414      e->buff[i] = e->buff[i - n];
415    }
[47519e1b]416  }
[e9bb404]417
418  e->bufflen += n;
419  e->buff[e->bufflen] = '\0';
420
[47519e1b]421}
422
[7d4fbcd]423
[c9334b1]424/* linewrap the word just before the cursor.
425 * returns 0 on success
426 * returns -1 if we could not wrap.
427 */
428int _owl_editwin_linewrap_word(owl_editwin *e)
429{
[84027015]430  int i;
431  char *ptr1, *start;
432  gunichar c;
[7d4fbcd]433
[84027015]434  start = e->buff + e->lock;
435
[a556caa]436  ptr1 = e->buff + e->index;
[84027015]437  ptr1 = g_utf8_find_prev_char(start, ptr1);
438
439  while (ptr1) {
440    c = g_utf8_get_char(ptr1);
441    if (owl_util_can_break_after(c)) {
442      if (c != ' ') {
443        i = ptr1 - e->buff;
[a556caa]444        e->index = i;
[b2c1bd4]445        _owl_editwin_insert_bytes(e, 1);
446        /* _owl_editwin_insert_bytes may move e->buff. */
[84027015]447        ptr1 = e->buff + i;
448      }
449      *ptr1 = '\n';
450      return 0;
[7d4fbcd]451    }
[2d4ff14]452    else if (c == '\n') {
453      return 0;
454    }
[84027015]455    ptr1 = g_utf8_find_prev_char(start, ptr1);
[7d4fbcd]456  }
[84027015]457  return -1;
[7d4fbcd]458}
459
[c9334b1]460/* insert a character at the current point (shift later
461 * characters over)
462 */
[47519e1b]463void owl_editwin_insert_char(owl_editwin *e, gunichar c)
[c9334b1]464{
[a556caa]465  int i, ret, len;
[47519e1b]466  char tmp[6];
467  memset(tmp, '\0', 6);
[830c36e]468
[7d4fbcd]469  /* \r is \n */
[47519e1b]470  if (c == '\r') {
471    c = '\n';
[7d4fbcd]472  }
473
[47519e1b]474  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
[7d4fbcd]475    /* perhaps later this will change some state that allows the string
476       to be read */
477    return;
478  }
479
[47519e1b]480  g_unichar_to_utf8(c, tmp);
481  len = strlen(tmp);
[830c36e]482
[7d4fbcd]483  /* make sure there is enough memory for the new text */
[47519e1b]484  if ((e->bufflen + len) > (e->allocated - 5)) {
[7d4fbcd]485    _owl_editwin_addspace(e);
486  }
487
488  /* If we're going to insert at the last column do word wrapping, unless it's a \n */
[a556caa]489#if 0 /* XXX */
[47519e1b]490  if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) {
491    ret = _owl_editwin_linewrap_word(e);
492    if (ret == -1) {
[7d4fbcd]493      /* we couldn't wrap, insert a hard newline instead */
494      owl_editwin_insert_char(e, '\n');
495    }
496  }
[a556caa]497#endif
[7d4fbcd]498
499  /* shift all the other characters right */
[e9bb404]500  _owl_editwin_insert_bytes(e, len);
[7d4fbcd]501
[47519e1b]502  /* insert the new character */
503  for(i = 0; i < len; i++) {
[a556caa]504    e->buff[e->index + i] = tmp[i];
[47519e1b]505  }
[7d4fbcd]506
507  /* advance the cursor */
[a556caa]508  e->index += len;
[7d4fbcd]509}
510
[c9334b1]511/* overwrite the character at the current point with 'c' */
[47519e1b]512void owl_editwin_overwrite_char(owl_editwin *e, gunichar c)
[c9334b1]513{
[a556caa]514  int oldlen, newlen, i;
515  char tmp[6], *t;
[47519e1b]516  memset(tmp, '\0', 6);
[830c36e]517
[7d4fbcd]518  /* \r is \n */
[47519e1b]519  if (c == '\r') {
520    c = '\n';
[7d4fbcd]521  }
[e20d8179]522
[47519e1b]523  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
[7d4fbcd]524    /* perhaps later this will change some state that allows the string
525       to be read */
526    return;
527  }
528
[47519e1b]529  g_unichar_to_utf8(c, tmp);
530  newlen = strlen(tmp);
[7d4fbcd]531
[a556caa]532  t = g_utf8_find_next_char(e->buff + e->index, NULL);
533  oldlen = (t ? (t - (e->buff + e->index)) : 0);
[7d4fbcd]534
[830c36e]535  /* only if we are at the end of the buffer do we create new space here */
[a556caa]536  if (e->index == e->bufflen) {
[830c36e]537    if ((e->bufflen+newlen) > (e->allocated-5)) {
538      _owl_editwin_addspace(e);
539    }
[7d4fbcd]540  }
[e20d8179]541  /* if not at the end of the buffer, adjust based in char size difference. */
[830c36e]542  else if (oldlen > newlen) {
[47519e1b]543    _owl_editwin_remove_bytes(e, oldlen-newlen);
544  }
545  else /* oldlen < newlen */ {
546    _owl_editwin_insert_bytes(e, newlen-oldlen);
[7d4fbcd]547  }
[47519e1b]548  /* Overwrite the old char*/
549  for (i = 0; i < newlen; i++) {
[a556caa]550    e->buff[e->index + i] = tmp[i];
[47519e1b]551  }
[e20d8179]552
[47519e1b]553  /* housekeeping */
[a556caa]554  if (e->index == e->bufflen) {
[830c36e]555    e->bufflen += newlen;
556    e->buff[e->bufflen] = '\0';
557  }
[e20d8179]558
[47519e1b]559  /* advance the cursor */
[a556caa]560  e->index += newlen;
[7d4fbcd]561}
562
[c9334b1]563/* delete the character at the current point, following chars
564 * shift left.
[e20d8179]565 */
[c9334b1]566void owl_editwin_delete_char(owl_editwin *e)
567{
[47519e1b]568  char *p1, *p2;
569  gunichar c;
[7d4fbcd]570
[47519e1b]571  if (e->bufflen == 0) return;
[e20d8179]572
[a556caa]573  if (e->index == e->bufflen) return;
[7d4fbcd]574
[a556caa]575  p1 = e->buff + e->index;
[47519e1b]576  p2 = g_utf8_next_char(p1);
577  c = g_utf8_get_char(p2);
578  while (g_unichar_ismark(c)) {
579    p2 = g_utf8_next_char(p2);
580    c = g_utf8_get_char(p2);
[7d4fbcd]581  }
[47519e1b]582  _owl_editwin_remove_bytes(e, p2-p1);
[7d4fbcd]583}
584
[c9334b1]585/* Swap the character at point with the character at point-1 and
586 * advance the pointer.  If point is at beginning of buffer do
587 * nothing.  If point is after the last character swap point-1 with
[e20d8179]588 * point-2.  (Behaves as observed in tcsh and emacs).
[c9334b1]589 */
590void owl_editwin_transpose_chars(owl_editwin *e)
591{
[47519e1b]592  char *p1, *p2, *p3, *tmp;
[f2e36b5]593
[47519e1b]594  if (e->bufflen == 0) return;
[e20d8179]595
[a556caa]596  if (e->index == e->bufflen) {
[f2e36b5]597    /* point is after last character */
[a556caa]598    e->index--;
[e20d8179]599  }
[f2e36b5]600
[a556caa]601  if (e->index - 1 < e->lock) {
[f2e36b5]602    /* point is at beginning of buffer, do nothing */
603    return;
604  }
605
[47519e1b]606  /* Transpose two utf-8 unicode glyphs. */
[a556caa]607  p1 = e->buff + e->index;
[47519e1b]608
[e20d8179]609  p2 = oe_next_point(e, p1); /* XXX make sure we can't transpose past the end
610                                of the buffer */
611  if (p2 == NULL)
612    return;
[47519e1b]613
[e20d8179]614  p3 = oe_prev_point(e, p1);
615  if (p3 == NULL)
616    return;
[47519e1b]617
618  tmp = owl_malloc(p2 - p3 + 5);
619  *tmp = '\0';
620  strncat(tmp, p1, p2 - p1);
621  strncat(tmp, p3, p1 - p3);
622  strncpy(p3, tmp, p2 - p3);
623  owl_free(tmp);
[a556caa]624  e->index = p3 - e->buff;
[f2e36b5]625}
626
[c9334b1]627/* insert 'string' at the current point, later text is shifted
628 * right
629 */
630void owl_editwin_insert_string(owl_editwin *e, char *string)
631{
[47519e1b]632  char *p;
633  gunichar c;
634  if (!g_utf8_validate(string, -1, NULL)) {
635    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
636    return;
637  }
638  p = string;
639  c = g_utf8_get_char(p);
640  while (c) {
[fac5463]641    _owl_editwin_process_char(e, c);
[47519e1b]642    p = g_utf8_next_char(p);
643    c = g_utf8_get_char(p);
[7d4fbcd]644  }
645}
646
[c9334b1]647/* write 'string' at the current point, overwriting text that is
648 * already there
649 */
650
651void owl_editwin_overwrite_string(owl_editwin *e, char *string)
652{
[47519e1b]653  char *p;
654  gunichar c;
[7d4fbcd]655
[47519e1b]656  if (!g_utf8_validate(string, -1, NULL)) {
657    owl_function_debugmsg("owl_editwin_overwrite_string: received non-utf-8 string.");
658    return;
659  }
660  p = string;
661  c = g_utf8_get_char(p);
662  while (c) {
663    owl_editwin_overwrite_char(e, c);
664    p = g_utf8_next_char(p);
665    c = g_utf8_get_char(p);
[7d4fbcd]666  }
667}
668
[a556caa]669/* We assume index is not set to point to a mid-char */
670gunichar _owl_editwin_get_char_at_point(owl_editwin *e)
[c9334b1]671{
[a556caa]672  return g_utf8_get_char(e->buff + e->index);
673}
[7d4fbcd]674
[a556caa]675void owl_editwin_adjust_for_locktext(owl_editwin *e)
676{
677  /* if we happen to have the cursor over locked text
678   * move it to be out of the locktext region */
679  if (e->index < e->lock) {
680    e->index = e->lock;
[7d4fbcd]681  }
682}
683
[e20d8179]684int owl_editwin_point_move(owl_editwin *e, int delta)
[84027015]685{
[e20d8179]686  char *p;
[19f765d]687  int change, d = 0;
[a556caa]688
689  change = MAX(delta, - delta);
690  p = e->buff + e->index;
691
692  while (d < change && p != NULL) {
[e20d8179]693    if (delta > 0)
694      p = oe_next_point(e, p);
695    else
696      p = oe_prev_point(e, p);
697    if (p != NULL) {
698      e->index = p - e->buff;
699      d++;
[a556caa]700    }
701  }
702
703  if (delta)
704    e->goal_column = -1;
705
706  return delta > 0 ? d : -d;
[84027015]707}
708
[a556caa]709int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
710  if (e->index == e->lock)
711    return 1;
[84027015]712
[a556caa]713  return 0;
714}
715
716int owl_at_end_of_buffer(owl_editwin *e) {
717  if (e->index == e->bufflen)
718    return 1;
719
720  return 0;
721}
722
723int owl_editwin_at_beginning_of_line(owl_editwin *e) /*noproto*/
[c9334b1]724{
[a0fbdee]725  oe_excursion x;
[a556caa]726  int ret;
[47519e1b]727
[a556caa]728  if (owl_editwin_at_beginning_of_buffer(e))
729    return 1;
[47519e1b]730
[a0fbdee]731  oe_save_excursion(e, &x);
[a556caa]732  owl_editwin_point_move(e, -1);
733  ret = (_owl_editwin_get_char_at_point(e) == '\n');
[a0fbdee]734  oe_restore_excursion(e, &x);
[a556caa]735
736  return ret;
737}
738
[e20d8179]739static int owl_editwin_is_char_in(owl_editwin *e, char *set)
[a556caa]740{
741  char *p;
742  /* It would be awfully nice if we could do UTF-8 comparisons */
743  for (p = set; *p != 0; p++)
744    if (_owl_editwin_get_char_at_point(e) == *p)
745      return 1;
746  return 0;
747}
748
[e20d8179]749int owl_editwin_move_if_in(owl_editwin *e, int delta, char *set)
[a556caa]750{
751  int change, distance = 0;
[e20d8179]752  while (owl_editwin_is_char_in(e, set)) {
[a556caa]753    change = owl_editwin_point_move(e, delta);
754    distance += change;
755    if (change == 0)
756      break;
[47519e1b]757  }
[a556caa]758  return distance;
759}
760
[e20d8179]761int owl_editwin_move_if_not_in(owl_editwin *e, int delta, char *set)
[a556caa]762{
763  int change, distance = 0;
[e20d8179]764  while (!owl_editwin_is_char_in(e, set)) {
[a556caa]765    change = owl_editwin_point_move(e, delta);
766    distance += change;
767    if (change == 0)
768      break;
[7d4fbcd]769  }
[a556caa]770  return distance;
[7d4fbcd]771}
772
[e20d8179]773int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
[47519e1b]774{
[a556caa]775  int distance = 0;
[47519e1b]776
[a556caa]777  if (!owl_editwin_at_beginning_of_line(e)) {
778    /* move off the \n if were at the end of a line */
779    distance += owl_editwin_point_move(e, -1);
780    distance += owl_editwin_move_if_not_in(e, -1, "\n");
781    if (distance && !owl_editwin_at_beginning_of_buffer(e))
782      distance += owl_editwin_point_move(e, 1);
[47519e1b]783  }
[84027015]784
[a556caa]785  return distance;
786}
[e20d8179]787
788int owl_editwin_move_to_end_of_line(owl_editwin *e)
[a556caa]789{
790  return owl_editwin_move_if_not_in(e, 1, "\n");
[47519e1b]791}
792
[e20d8179]793int owl_editwin_line_move(owl_editwin *e, int delta)
[c9334b1]794{
[d7043b4]795  int goal_column, change, ll, distance;
796  int count = 0;
[a556caa]797
798  change = MAX(delta, -delta);
799
[d7043b4]800  goal_column = e->goal_column;
801  distance = owl_editwin_move_to_beginning_of_line(e);
802  goal_column = goal_column == -1 ? -distance : goal_column;
[a556caa]803
804  while(count < change) {
805    if (delta > 0) {
806      distance += owl_editwin_move_if_not_in(e, 1, "\n");
807      distance += owl_editwin_point_move(e, 1);
808    } else {
809      /* I really want to assert delta < 0 here */
810      distance += owl_editwin_point_move(e, -1); /* to the newline on
811                                                    the previous line */
812      distance += owl_editwin_move_to_beginning_of_line(e);
813    }
814    count++;
[7d4fbcd]815  }
[a556caa]816
817  distance += (ll = owl_editwin_move_to_end_of_line(e));
818  if (ll > goal_column)
819    distance += owl_editwin_point_move(e, goal_column - ll);
820
821  e->goal_column = goal_column;
822
823  return distance;
[7d4fbcd]824}
825
[c9334b1]826void owl_editwin_backspace(owl_editwin *e)
827{
[7d4fbcd]828  /* delete the char before the current one
829   * and shift later chars left
830   */
[a556caa]831  if(owl_editwin_point_move(e, -1))
[7d4fbcd]832    owl_editwin_delete_char(e);
833}
834
[c9334b1]835void owl_editwin_key_up(owl_editwin *e)
836{
[a556caa]837  owl_editwin_line_move(e, -1);
[7d4fbcd]838}
839
[c9334b1]840void owl_editwin_key_down(owl_editwin *e)
841{
[a556caa]842  owl_editwin_line_move(e, 1);
[7d4fbcd]843}
844
[c9334b1]845void owl_editwin_key_left(owl_editwin *e)
846{
[a556caa]847  owl_editwin_point_move(e, -1);
[7d4fbcd]848}
849
[c9334b1]850void owl_editwin_key_right(owl_editwin *e)
851{
[a556caa]852  owl_editwin_point_move(e, 1);
[7d4fbcd]853}
854
[c9334b1]855void owl_editwin_move_to_nextword(owl_editwin *e)
856{
[7d4fbcd]857  /* if we're starting on a space, find the first non-space */
[a556caa]858  owl_editwin_move_if_in(e, 1, WHITESPACE);
[7d4fbcd]859
[a556caa]860  /* now find the end of this word */
861  owl_editwin_move_if_not_in(e, 1, WHITESPACE);
[7d4fbcd]862}
863
[c9334b1]864/* go backwards to the last non-space character
865 */
866void owl_editwin_move_to_previousword(owl_editwin *e)
867{
[a0fbdee]868  oe_excursion x;
[a556caa]869  int beginning;
870  /* if in middle of word, beginning of word */
871
872  /* if at beginning of a word, find beginning of previous word */
873
[e20d8179]874  if (owl_editwin_is_char_in(e, WHITESPACE)) {
[a556caa]875    /* if in whitespace past end of word, find a word , the find the beginning*/
876    owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
877                                                    character of the word */
[a0fbdee]878    oe_save_excursion(e, &x);
[a556caa]879    /* are we at the beginning of a word? */
880    owl_editwin_point_move(e, -1);
[e20d8179]881    beginning = owl_editwin_is_char_in(e, WHITESPACE);
[a0fbdee]882    oe_restore_excursion(e, &x);
[a556caa]883    if (beginning)
884      return;
885   } else {
886    /* in the middle of the word; */
[a0fbdee]887    oe_save_excursion(e, &x);
[a556caa]888    owl_editwin_point_move(e, -1);
[e20d8179]889    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
[a556caa]890      owl_editwin_move_to_previousword(e); /* previous case */
891      return;
892    } else {
[a0fbdee]893      oe_restore_excursion(e, &x);
[7d4fbcd]894    }
895  }
[a556caa]896  owl_editwin_move_if_not_in(e, -1, WHITESPACE);
897  /* will go past */
898  if (e->index > e->lock)
899    owl_editwin_point_move(e, 1);
[7d4fbcd]900}
901
[c9334b1]902void owl_editwin_delete_nextword(owl_editwin *e)
903{
[a0fbdee]904  oe_excursion x;
[a556caa]905  int end;
906
[a0fbdee]907  oe_save_excursion(e, &x);
[a556caa]908  owl_editwin_move_to_nextword(e);
909  end = e->index;
[a0fbdee]910  oe_restore_excursion(e, &x);
[a556caa]911  _owl_editwin_remove_bytes(e, end - e->index);
[7d4fbcd]912}
913
[c9334b1]914void owl_editwin_delete_previousword(owl_editwin *e)
915{
[b68f9cd]916  /* go backwards to the last non-space character, then delete chars */
[84027015]917  int startpos, endpos;
[b68f9cd]918
[a556caa]919  startpos = e->index;
[b68f9cd]920  owl_editwin_move_to_previousword(e);
[a556caa]921  endpos = e->index;
[84027015]922  _owl_editwin_remove_bytes(e, startpos-endpos);
[b68f9cd]923}
924
[a556caa]925void owl_editwin_move_to_line_end(owl_editwin *e)
[c9334b1]926{
[a556caa]927  owl_editwin_move_to_end_of_line(e);
[7d4fbcd]928}
929
[a556caa]930void owl_editwin_delete_to_endofline(owl_editwin *e)
[c9334b1]931{
[a0fbdee]932  oe_excursion x;
[a556caa]933  int end;
934
[a0fbdee]935  oe_save_excursion(e, &x);
[a556caa]936  owl_editwin_move_to_line_end(e);
937  end = e->index;
[a0fbdee]938  oe_restore_excursion(e, &x);
[a556caa]939  _owl_editwin_remove_bytes(e, end - e->index);
[7d4fbcd]940}
941
[c9334b1]942void owl_editwin_move_to_line_start(owl_editwin *e)
943{
[a556caa]944  owl_editwin_move_to_beginning_of_line(e);
[7d4fbcd]945}
946
[c9334b1]947void owl_editwin_move_to_end(owl_editwin *e)
948{
[a556caa]949  e->index = e->bufflen;
[7d4fbcd]950}
951
[c9334b1]952void owl_editwin_move_to_top(owl_editwin *e)
953{
[a556caa]954  e->index = e->lock;
[7d4fbcd]955}
956
[c9334b1]957void owl_editwin_fill_paragraph(owl_editwin *e)
958{
[a556caa]959#if 0 /* XXX */
[a0fbdee]960  oe_excursion x;
[7d4fbcd]961  int i, save;
962
963  /* save our starting point */
[a0fbdee]964  oe_save_excursion(e, &x);
[a556caa]965
966  save = e->index;
[7d4fbcd]967
968  /* scan back to the beginning of this paragraph */
969  for (i=save; i>=e->lock; i--) {
970    if ( (i<=e->lock) ||
971         ((e->buff[i]=='\n') && (e->buff[i-1]=='\n'))) {
[a556caa]972      e->index = i + 1;
[7d4fbcd]973      break;
974    }
975  }
976
977  /* main loop */
978  while (1) {
[47519e1b]979    i = _owl_editwin_get_index_from_xy(e);
[7d4fbcd]980
981    /* bail if we hit the end of the buffer */
[47519e1b]982    if (i >= e->bufflen || e->buff[i] == '\0') break;
[7d4fbcd]983
984    /* bail if we hit the end of the paragraph */
[47519e1b]985    if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break;
[7d4fbcd]986
[50e671c]987    /* bail if we hit a trailing dot on the buffer */
988    if (e->buff[i] == '\n' && e->buff[i+1] == '.'
989        && ((i+2) >= e->bufflen || e->buff[i+2] == '\0'))
990      break;
991
[7d4fbcd]992    /* if we've travelled too far, linewrap */
993    if ((e->buffx) >= e->fillcol) {
[b2c1bd4]994      int len = e->bufflen;
[7d4fbcd]995      _owl_editwin_linewrap_word(e);
[b2c1bd4]996      /* we may have added a character. */
997      if (i < save) save += e->bufflen - len;
[a556caa]998      e->index = i;
[7d4fbcd]999    }
1000
1001    /* did we hit the end of a line too soon? */
[84027015]1002    /* asedeno: Here we replace a newline with a space. We may want to
1003       consider removing the space if the characters to either side
1004       are CJK ideograms.*/
[47519e1b]1005    i = _owl_editwin_get_index_from_xy(e);
1006    if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) {
[7d4fbcd]1007      /* ********* we need to make sure we don't pull in a word that's too long ***********/
1008      e->buff[i]=' ';
1009    }
[b2c1bd4]1010
[7d4fbcd]1011    /* fix spacing */
[47519e1b]1012    i = _owl_editwin_get_index_from_xy(e);
1013    if (e->buff[i] == ' ' && e->buff[i+1] == ' ') {
1014      if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') {
[7d4fbcd]1015        owl_editwin_key_right(e);
1016      } else {
1017        owl_editwin_delete_char(e);
[84027015]1018        /* if we did this ahead of the save point, adjust it. Changing
1019           by one is fine here because we're only removing an ASCII
1020           space. */
[47519e1b]1021        if (i < save) save--;
[7d4fbcd]1022      }
1023    } else {
1024      owl_editwin_key_right(e);
1025    }
1026  }
1027
1028  /* put cursor back at starting point */
[a0fbdee]1029  oe_restore_excursion(e, &x);
[a556caa]1030#endif
[7d4fbcd]1031}
1032
[cf83b7a]1033/* returns true if only whitespace remains */
[c9334b1]1034int owl_editwin_is_at_end(owl_editwin *e)
1035{
[a556caa]1036  return (only_whitespace(e->buff + e->index));
[217a43e]1037}
1038
[c9334b1]1039int owl_editwin_check_dotsend(owl_editwin *e)
1040{
[47519e1b]1041  char *p, *p_n, *p_p;
1042  gunichar c;
[7d4fbcd]1043
1044  if (!e->dotsend) return(0);
[47519e1b]1045
1046  p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen);
1047  p_n = g_utf8_find_next_char(p, NULL);
1048  p_p = g_utf8_find_prev_char(e->buff, p);
1049  c = g_utf8_get_char(p);
1050  while (p != NULL) {
1051    if (*p == '.'
1052        && p_p != NULL && (*p_p == '\n' || *p_p == '\r')
1053        && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) {
1054      e->bufflen = p - e->buff;
1055      e->buff[e->bufflen] = '\0';
[7d4fbcd]1056      return(1);
1057    }
[47519e1b]1058    if (c != '\0' && !g_unichar_isspace(c)) return(0);
1059    p_n = p;
1060    p = p_p;
1061    c = g_utf8_get_char(p);
1062    p_p = g_utf8_find_prev_char(e->buff, p);
[7d4fbcd]1063  }
1064  return(0);
1065}
1066
[fac5463]1067void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
[c9334b1]1068{
[a556caa]1069  /* XXX force a redisplay? */
[fac5463]1070  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
[7d4fbcd]1071    owl_command_editmulti_done(e);
1072    return;
1073  }
[e20d8179]1074  owl_editwin_redisplay(e, 0);
[7d4fbcd]1075}
1076
[fac5463]1077void _owl_editwin_process_char(owl_editwin *e, gunichar j)
[c9334b1]1078{
[bd1a1ae]1079  if (!(g_unichar_iscntrl(j) && (j != 10) && (j != 13)) || j==9 ) {
[7d4fbcd]1080    owl_editwin_insert_char(e, j);
1081  }
1082}
1083
[fac5463]1084
1085void owl_editwin_process_char(owl_editwin *e, owl_input j)
1086{
1087  if (j.ch == ERR) return;
1088  /* Ignore ncurses control characters. */
[e20d8179]1089  if (j.ch < 0x100) {
[fac5463]1090    _owl_editwin_process_char(e, j.uch);
1091  }
1092}
1093
[c9334b1]1094char *owl_editwin_get_text(owl_editwin *e)
1095{
[7d4fbcd]1096  return(e->buff+e->lock);
1097}
1098
[c9334b1]1099int owl_editwin_get_numchars_on_line(owl_editwin *e, int line)
1100{
[7d4fbcd]1101  int i;
1102  char *ptr1, *ptr2;
1103
1104  if (e->bufflen==0) return(0);
[e20d8179]1105
[7d4fbcd]1106  /* first go to the yth line */
1107  ptr1=e->buff;
1108  for (i=0; i<line; i++) {
1109    ptr2=strchr(ptr1, '\n');
1110    if (!ptr2) {
1111      /* we're already on the last line */
1112      return(0);
1113    }
1114    ptr1=ptr2+1;
1115  }
1116
[47519e1b]1117  /* now count characters */
1118  i = 0;
1119  ptr2 = ptr1;
1120  while (ptr2 - e->buff < e->bufflen
1121         && *ptr2 != '\n') {
1122    ++i;
1123    ptr2 = g_utf8_next_char(ptr2);
1124  }
1125  return i;
1126}
1127
[c9334b1]1128int owl_editwin_get_numlines(owl_editwin *e)
1129{
[7d4fbcd]1130  return(owl_text_num_lines(e->buff));
1131}
1132
[a556caa]1133int owl_editwin_get_echochar(owl_editwin *e) {
1134  return e->echochar;
1135}
[e20d8179]1136
1137void owl_editwin_set_goal_column(owl_editwin *e, int column)
1138{
1139  e->goal_column = column;
1140}
1141
1142int owl_editwin_get_goal_column(owl_editwin *e)
1143{
1144  return e->goal_column;
1145}
Note: See TracBrowser for help on using the repository browser.