Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • editwin.c

    raf1920fd r2d4ff14  
    143143  e->buffy=0;
    144144  owl_editwin_overwrite_string(e, text);
     145  owl_editwin_overwrite_char(e, '\0');
    145146  e->lock=strlen(text);
    146147  /* if (text[e->lock-1]=='\n') e->lock--; */
    147   e->buffx=x;
    148   e->buffy=y;
    149   owl_editwin_adjust_for_locktext(e);
     148  /*  e->buffx=x; */
     149  /*  e->buffy=y; */
     150  _owl_editwin_set_xy_by_index(e, e->lock);
    150151  owl_editwin_redisplay(e, 0);
    151152}
     
    255256
    256257  /* start at topline */
    257   ptr1=e->buff;
    258   for (i=0; i<e->topline; i++) {
    259     ptr2=strchr(ptr1, '\n');
     258  ptr1 = e->buff;
     259  for (i = 0; i < e->topline; i++) {
     260    ptr2 = strchr(ptr1, '\n');
    260261    if (!ptr2) {
    261262      /* we're already on the last line */
    262263      break;
    263264    }
    264     ptr1=ptr2+1;
     265    ptr1 = ptr2 + 1;
    265266  }
    266267  /* ptr1 now stores the starting point */
    267268
    268269  /* find the ending point and store it in ptr3 */
    269   ptr2=ptr1;
    270   ptr3=ptr1;
    271   for (i=0; i<e->winlines; i++) {
    272     ptr3=strchr(ptr2, '\n');
     270  ptr2 = ptr1;
     271  ptr3 = ptr1;
     272  for (i = 0; i < e->winlines; i++) {
     273    ptr3 = strchr(ptr2, '\n');
    273274    if (!ptr3) {
    274275      /* we've hit the last line */
    275276      /* print everything to the end */
    276       ptr3=e->buff+e->bufflen-1;
     277      ptr3 = e->buff + e->bufflen - 1;
    277278      ptr3--;
    278279      break;
    279280    }
    280     ptr2=ptr3+1;
    281   }
    282   ptr3+=2;
    283 
    284   buff=owl_malloc(ptr3-ptr1+50);
    285   strncpy(buff, ptr1, ptr3-ptr1);
    286   buff[ptr3-ptr1]='\0';
    287   if (e->echochar=='\0') {
     281    ptr2 = ptr3 + 1;
     282  }
     283  ptr3 += 2;
     284
     285  buff = owl_malloc(ptr3 - ptr1 + 50);
     286  strncpy(buff, ptr1, ptr3 - ptr1);
     287  buff[ptr3 - ptr1] = '\0';
     288  if (e->echochar == '\0') {
    288289    waddstr(e->curswin, buff);
    289290  } else {
    290291    /* translate to echochar, *except* for the locktext */
    291292    int len;
    292     int dolocklen=e->lock-(ptr1-e->buff);
    293 
    294     for (i=0; i<dolocklen; i++) {
    295       waddch(e->curswin, buff[i]);
    296     }
    297     len=strlen(buff);
    298     for (i=0; i<len-dolocklen; i++) {
     293    int dolocklen = e->lock - (ptr1 - e->buff);
     294    char *locktext;
     295    char tmp = e->buff[dolocklen];
     296
     297    e->buff[dolocklen] = '\0';
     298    locktext = owl_strdup(e->buff);
     299    e->buff[dolocklen] = tmp;
     300
     301    waddstr(e->curswin, locktext);
     302   
     303    len = strlen(buff);
     304    for (i = 0; i < len-dolocklen; i++) {
    299305      waddch(e->curswin, e->echochar);
    300306    }
    301307  }
    302   wmove(e->curswin, e->buffy-e->topline, e->buffx);
     308  wmove(e->curswin, e->buffy-e->topline, e->buffx + _owl_editwin_cursor_adjustment(e));
    303309  wnoutrefresh(e->curswin);
    304   if (update==1) {
     310  if (update == 1) {
    305311    doupdate();
    306312  }
    307313  owl_free(buff);
     314}
     315
     316/* Remove n bytes at cursor. */
     317void _owl_editwin_remove_bytes(owl_editwin *e, int n) /*noproto*/
     318{
     319  int i = _owl_editwin_get_index_from_xy(e) + n;
     320  for (; i < e->bufflen; i++) {
     321    e->buff[i-n] = e->buff[i];
     322  }
     323 
     324  e->bufflen -= n;
     325  e->buff[e->bufflen] = '\0';
     326}
     327
     328/* Insert n bytes at cursor.*/
     329void _owl_editwin_insert_bytes(owl_editwin *e, int n) /*noproto*/
     330{
     331  int i, z;
     332 
     333  if ((e->bufflen + n) > (e->allocated - 5)) {
     334    _owl_editwin_addspace(e);
     335  }
     336
     337  e->bufflen += n;
     338  e->buff[e->bufflen] = '\0';
     339 
     340  z = _owl_editwin_get_index_from_xy(e);
     341  for (i = e->bufflen - 1; i > z; i--) {
     342    e->buff[i] = e->buff[i - n];
     343  }
    308344}
    309345
     
    315351int _owl_editwin_linewrap_word(owl_editwin *e)
    316352{
    317   int i, z;
    318 
    319   z=_owl_editwin_get_index_from_xy(e);
    320   /* move back and line wrap the previous word */
    321   for (i=z-1; ; i--) {
    322     /* move back until you find a space or hit the beginning of the line */
    323     if (e->buff[i]==' ') {
    324       /* replace the space with a newline */
    325       e->buff[i]='\n';
    326       e->buffy++;
    327       e->buffx=z-i-1;
    328       /* were we on the last line */
    329       return(0);
    330     } else if (e->buff[i]=='\n' || i<=e->lock) {
    331       /* we hit the begginning of the line or the buffer, we cannot
    332        * wrap.
    333        */
    334       return(-1);
    335     }
    336   }
     353  int x, y;
     354  int i;
     355  char *ptr1, *start;
     356  gunichar c;
     357
     358  /* saving values */
     359  x = e->buffx;
     360  y = e->buffy;
     361  start = e->buff + e->lock;
     362
     363  ptr1 = e->buff + _owl_editwin_get_index_from_xy(e);
     364  ptr1 = g_utf8_find_prev_char(start, ptr1);
     365
     366  while (ptr1) {
     367    c = g_utf8_get_char(ptr1);
     368    if (owl_util_can_break_after(c)) {
     369      if (c != ' ') {
     370        i = ptr1 - e->buff;
     371        _owl_editwin_set_xy_by_index(e, i);
     372        _owl_editwin_insert_bytes(e, 1);
     373        /* _owl_editwin_insert_bytes may move e->buff. */
     374        ptr1 = e->buff + i;
     375      }
     376      *ptr1 = '\n';
     377      return 0;
     378    }
     379    else if (c == '\n') {
     380      return 0;
     381    }
     382    ptr1 = g_utf8_find_prev_char(start, ptr1);
     383  }
     384  return -1;
    337385}
    338386
     
    340388 * characters over)
    341389 */
    342 void owl_editwin_insert_char(owl_editwin *e, char c)
    343 {
    344  
    345   int z, i, ret;
     390void owl_editwin_insert_char(owl_editwin *e, gunichar c)
     391{
     392  int z, i, ret, len;
     393  char tmp[6];
     394  memset(tmp, '\0', 6);
    346395
    347396  /* \r is \n */
    348   if (c=='\r') {
    349     c='\n';
    350   }
    351 
    352   if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {
     397  if (c == '\r') {
     398    c = '\n';
     399  }
     400
     401  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
    353402    /* perhaps later this will change some state that allows the string
    354403       to be read */
     
    356405  }
    357406
     407  g_unichar_to_utf8(c, tmp);
     408  len = strlen(tmp);
     409
    358410  /* make sure there is enough memory for the new text */
    359   if ((e->bufflen+1) > (e->allocated-5)) {
     411  if ((e->bufflen + len) > (e->allocated - 5)) {
    360412    _owl_editwin_addspace(e);
    361413  }
    362414
    363415  /* get the insertion point */
    364   z=_owl_editwin_get_index_from_xy(e);
     416  z = _owl_editwin_get_index_from_xy(e);
    365417
    366418  /* If we're going to insert at the last column do word wrapping, unless it's a \n */
    367   if ((e->buffx+1==e->wrapcol) && (c!='\n')) {
    368     ret=_owl_editwin_linewrap_word(e);
    369     if (ret==-1) {
     419  if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) {
     420    ret = _owl_editwin_linewrap_word(e);
     421    if (ret == -1) {
    370422      /* we couldn't wrap, insert a hard newline instead */
    371423      owl_editwin_insert_char(e, '\n');
     
    373425  }
    374426
    375   z=_owl_editwin_get_index_from_xy(e);
    376427  /* shift all the other characters right */
    377   for (i=e->bufflen; i>z; i--) {
    378     e->buff[i]=e->buff[i-1];
    379   }
    380 
    381   /* insert the new one */
    382   e->buff[z]=c;
     428  if (z != e->bufflen) {
     429    _owl_editwin_insert_bytes(e, len);
     430  }
     431
     432  /* insert the new character */
     433  for(i = 0; i < len; i++) {
     434    e->buff[z + i] = tmp[i];
     435  }
    383436
    384437  /* housekeeping */
    385   e->bufflen++;
    386   e->buff[e->bufflen]='\0';
    387 
     438  e->bufflen += len;
     439  e->buff[e->bufflen] = '\0';
     440 
    388441  /* advance the cursor */
    389   if (c=='\n') {
    390     e->buffx=0;
    391     e->buffy++;
    392   } else {
    393     e->buffx++;
    394   }
     442  z += len;
     443  _owl_editwin_set_xy_by_index(e, z);
    395444}
    396445
    397446/* overwrite the character at the current point with 'c' */
    398 void owl_editwin_overwrite_char(owl_editwin *e, char c)
    399 {
    400   int z;
    401  
     447void owl_editwin_overwrite_char(owl_editwin *e, gunichar c)
     448{
     449  int z, oldlen, newlen, i;
     450  char tmp[6];
     451  memset(tmp, '\0', 6);
     452
    402453  /* \r is \n */
    403   if (c=='\r') {
    404     c='\n';
    405   }
    406 
    407   if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {
     454  if (c == '\r') {
     455    c = '\n';
     456  }
     457 
     458  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
    408459    /* perhaps later this will change some state that allows the string
    409460       to be read */
     
    411462  }
    412463
    413   z=_owl_editwin_get_index_from_xy(e);
    414 
    415   /* only if we are at the end of the buffer do we create new space */
    416   if (z==e->bufflen) {
    417     if ((e->bufflen+1) > (e->allocated-5)) {
     464  g_unichar_to_utf8(c, tmp);
     465  newlen = strlen(tmp);
     466
     467  z = _owl_editwin_get_index_from_xy(e);
     468  {
     469    char *t = g_utf8_find_next_char(e->buff + z, NULL);
     470    oldlen = (t ? (t - (e->buff + z)) : 0);
     471  }
     472
     473  /* only if we are at the end of the buffer do we create new space here */
     474  if (z == e->bufflen) {
     475    if ((e->bufflen+newlen) > (e->allocated-5)) {
    418476      _owl_editwin_addspace(e);
    419477    }
    420478  }
    421  
    422   e->buff[z]=c;
    423 
    424   /* housekeeping if we are at the end of the buffer */
    425   if (z==e->bufflen) {
    426     e->bufflen++;
    427     e->buff[e->bufflen]='\0';
    428   }
    429 
     479  /* if not at the end of the buffer, adjust based in char size difference. */
     480  else if (oldlen > newlen) {
     481    _owl_editwin_remove_bytes(e, oldlen-newlen);
     482  }
     483  else /* oldlen < newlen */ {
     484    _owl_editwin_insert_bytes(e, newlen-oldlen);
     485  }
     486  /* Overwrite the old char*/
     487  for (i = 0; i < newlen; i++) {
     488    e->buff[z+i] = tmp[i];
     489  }
     490       
     491  /* housekeeping */
     492  if (z == e->bufflen) {
     493    e->bufflen += newlen;
     494    e->buff[e->bufflen] = '\0';
     495  }
     496 
    430497  /* advance the cursor */
    431   if (c=='\n') {
    432     e->buffx=0;
    433     e->buffy++;
    434   } else {
    435     e->buffx++;
    436   }
    437 
     498  z += newlen;
     499  _owl_editwin_set_xy_by_index(e, z);
    438500}
    439501
     
    443505void owl_editwin_delete_char(owl_editwin *e)
    444506{
    445   int z, i;
    446 
    447   if (e->bufflen==0) return;
     507  int z;
     508  char *p1, *p2;
     509  gunichar c;
     510
     511  if (e->bufflen == 0) return;
    448512 
    449513  /* get the deletion point */
    450   z=_owl_editwin_get_index_from_xy(e);
    451 
    452   if (z==e->bufflen) return;
    453 
    454   for (i=z; i<e->bufflen; i++) {
    455     e->buff[i]=e->buff[i+1];
    456   }
    457   e->bufflen--;
    458   e->buff[e->bufflen]='\0';
     514  z = _owl_editwin_get_index_from_xy(e);
     515
     516  if (z == e->bufflen) return;
     517
     518  p1 = e->buff + z;
     519  p2 = g_utf8_next_char(p1);
     520  c = g_utf8_get_char(p2);
     521  while (g_unichar_ismark(c)) {
     522    p2 = g_utf8_next_char(p2);
     523    c = g_utf8_get_char(p2);
     524  }
     525  _owl_editwin_remove_bytes(e, p2-p1);
    459526}
    460527
     
    467534{
    468535  int z;
    469   char tmp;
    470 
    471   if (e->bufflen==0) return;
     536  char *p1, *p2, *p3, *tmp;
     537
     538  if (e->bufflen == 0) return;
    472539 
    473540  /* get the cursor point */
    474   z=_owl_editwin_get_index_from_xy(e);
    475 
    476   if (z==e->bufflen) {
     541  z = _owl_editwin_get_index_from_xy(e);
     542
     543  if (z == e->bufflen) {
    477544    /* point is after last character */
    478545    z--;
    479546  } 
    480547
    481   if (z-1 < e->lock) {
     548  if (z - 1 < e->lock) {
    482549    /* point is at beginning of buffer, do nothing */
    483550    return;
    484551  }
    485552
    486   tmp=e->buff[z];
    487   e->buff[z]=e->buff[z-1];
    488   e->buff[z-1]=tmp;
    489   owl_editwin_key_right(e);
     553  /* Transpose two utf-8 unicode glyphs. */
     554  p1 = e->buff + z;
     555
     556  p2 = g_utf8_find_next_char(p1, NULL);
     557  while (p2 != NULL && g_unichar_ismark(g_utf8_get_char(p2))) {
     558    p2 = g_utf8_find_next_char(p2, NULL);
     559  }
     560  if (p2 == NULL) return;
     561
     562  p3 = g_utf8_find_prev_char(e->buff, p1);
     563  while (p3 != NULL && g_unichar_ismark(g_utf8_get_char(p3))) {
     564    p3 = g_utf8_find_prev_char(p3, NULL);
     565  }
     566  if (p3 == NULL) return;
     567
     568  tmp = owl_malloc(p2 - p3 + 5);
     569  *tmp = '\0';
     570  strncat(tmp, p1, p2 - p1);
     571  strncat(tmp, p3, p1 - p3);
     572  strncpy(p3, tmp, p2 - p3);
     573  owl_free(tmp);
     574  _owl_editwin_set_xy_by_index(e, p3 - e->buff);
    490575}
    491576
     
    495580void owl_editwin_insert_string(owl_editwin *e, char *string)
    496581{
    497   int i, j;
    498 
    499   j=strlen(string);
    500   for (i=0; i<j; i++) {
    501     owl_editwin_insert_char(e, string[i]);
     582  char *p;
     583  gunichar c;
     584  if (!g_utf8_validate(string, -1, NULL)) {
     585    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
     586    return;
     587  }
     588  p = string;
     589  c = g_utf8_get_char(p);
     590  while (c) {
     591    _owl_editwin_process_char(e, c);
     592    p = g_utf8_next_char(p);
     593    c = g_utf8_get_char(p);
    502594  }
    503595}
     
    509601void owl_editwin_overwrite_string(owl_editwin *e, char *string)
    510602{
    511   int i, j;
    512 
    513   j=strlen(string);
    514   for (i=0; i<j; i++) {
    515     owl_editwin_overwrite_char(e, string[i]);
     603  char *p;
     604  gunichar c;
     605
     606  if (!g_utf8_validate(string, -1, NULL)) {
     607    owl_function_debugmsg("owl_editwin_overwrite_string: received non-utf-8 string.");
     608    return;
     609  }
     610  p = string;
     611  c = g_utf8_get_char(p);
     612  while (c) {
     613    owl_editwin_overwrite_char(e, c);
     614    p = g_utf8_next_char(p);
     615    c = g_utf8_get_char(p);
    516616  }
    517617}
     
    524624  int i;
    525625  char *ptr1, *ptr2;
    526 
    527   if (e->bufflen==0) return(0);
     626  gunichar c;
     627
     628  if (e->bufflen == 0) return(0);
    528629 
    529630  /* first go to the yth line */
    530   ptr1=e->buff;
    531   for (i=0; i<e->buffy; i++) {
    532     ptr2=strchr(ptr1, '\n');
     631  ptr1 = e->buff;
     632  for (i = 0; i < e->buffy; i++) {
     633    ptr2= strchr(ptr1, '\n');
    533634    if (!ptr2) {
    534635      /* we're already on the last line */
    535636      break;
    536637    }
    537     ptr1=ptr2+1;
    538   }
    539 
    540   /* now go to the xth character */
    541   ptr2=strchr(ptr1, '\n');
    542   if (!ptr2) {
    543     ptr2=e->buff+e->bufflen;
    544   }
    545 
    546   if ((ptr2-ptr1) < e->buffx) {
    547     ptr1=ptr2-1;
    548   } else {
    549     ptr1+=e->buffx;
    550   }
    551 
    552   /* printf("DEBUG: index is %i\r\n", ptr1-e->buff); */
    553   return(ptr1-e->buff);
    554 }
     638    ptr1 = ptr2 + 1;
     639  }
     640
     641  /* now go to the xth cell */
     642  ptr2 = ptr1;
     643  i = 0;
     644  while (ptr2 != NULL && i < e->buffx && (ptr2 - e->buff) < e->bufflen) {
     645    c = g_utf8_get_char(ptr2);
     646    i += (c == '\n' ? 1 : mk_wcwidth(c));
     647    ptr2 = g_utf8_next_char(ptr2);
     648  }
     649  while(ptr2 != NULL && g_unichar_ismark(g_utf8_get_char(ptr2))) {
     650    ptr2 = g_utf8_next_char(ptr2);
     651  }
     652  if (ptr2 == NULL) return e->bufflen;
     653  return(ptr2 - e->buff);
     654}
     655
     656/* We assume x,y are not set to point to a mid-char */
     657gunichar _owl_editwin_get_char_at_xy(owl_editwin *e)
     658{
     659  return g_utf8_get_char(e->buff + _owl_editwin_get_index_from_xy(e));
     660}
     661
    555662
    556663void _owl_editwin_set_xy_by_index(owl_editwin *e, int index)
    557664{
    558   int z, i;
    559 
    560   z=_owl_editwin_get_index_from_xy(e);
    561   if (index>z) {
    562     for (i=0; i<index-z; i++) {
    563       owl_editwin_key_right(e);
    564     }
    565   } else if (index<z) {
    566     for (i=0; i<z-index; i++) {
    567       owl_editwin_key_left(e);
    568     }
    569   }
     665  char *ptr1, *ptr2, *target;
     666  gunichar c;
     667
     668  e->buffx = 0;
     669  e->buffy = 0;
     670
     671  ptr1 = e->buff;
     672  target = ptr1 + index;
     673  /* target sanitizing */
     674  if ((target[0] & 0x80) && (~target[0] & 0x40)) {
     675    /* middle of a utf-8 character, back up to previous character. */
     676    target = g_utf8_find_prev_char(e->buff, target);
     677  }
     678  c = g_utf8_get_char(target);
     679  while (g_unichar_ismark(c) && target > e->buff) {
     680    /* Adjust the target off of combining characters and the like. */
     681    target = g_utf8_find_prev_char(e->buff, target);
     682    c = g_utf8_get_char(target);
     683  }
     684  /* If we start with a mark, something is wrong.*/
     685  if (g_unichar_ismark(c)) return;
     686
     687  /* Now our target should be acceptable. */
     688  ptr2 = strchr(ptr1, '\n');
     689  while (ptr2 != NULL && ptr2 < target) {
     690    e->buffy++;
     691    ptr1 = ptr2 + 1;
     692    ptr2 = strchr(ptr1, '\n');
     693  }
     694  ptr2 = ptr1;
     695  while (ptr2 != NULL && ptr2 < target) {
     696    c = g_utf8_get_char(ptr2);
     697    e->buffx += mk_wcwidth(c);
     698    ptr2 = g_utf8_next_char(ptr2);
     699  }
     700}
     701
     702int _owl_editwin_cursor_adjustment(owl_editwin *e)
     703{
     704  char *ptr1, *ptr2;
     705  gunichar c;
     706  int x, i;
     707
     708  /* Find line */
     709  ptr1 = e->buff;
     710  ptr2 = strchr(ptr1, '\n');
     711  for (i = 0; ptr2 != NULL && i < e->buffy; i++) {
     712    ptr1 = ptr2 + 1;
     713    ptr2 = strchr(ptr1, '\n');
     714  }
     715  ptr2 = ptr1;
     716
     717  /* Find char */
     718  x = 0;
     719  while (ptr2 != NULL && x < e->buffx) {
     720    if (*ptr2 == '\n') return 0;
     721    c = g_utf8_get_char(ptr2);
     722    x += mk_wcwidth(c);
     723    ptr2 = g_utf8_next_char(ptr2);
     724  }
     725 
     726  /* calculate x offset */
     727  return x - e->buffx;
    570728}
    571729
     
    574732  /* if we happen to have the cursor over locked text
    575733   * move it to be out of the locktext region */
    576   if (_owl_editwin_get_index_from_xy(e)<e->lock) {
     734  if (_owl_editwin_get_index_from_xy(e) < e->lock) {
    577735    _owl_editwin_set_xy_by_index(e, e->lock);
    578736  }
     
    594752{
    595753  if (e->buffy > 0) e->buffy--;
    596   if (e->buffx >= owl_editwin_get_numchars_on_line(e, e->buffy)) {
    597     e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     754  if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) {
     755    e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    598756  }
    599757
     
    612770
    613771  /* if we're past the last character move back */
    614   if (e->buffx >= owl_editwin_get_numchars_on_line(e, e->buffy)) {
    615     e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     772  if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) {
     773    e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    616774  }
    617775
     
    627785void owl_editwin_key_left(owl_editwin *e)
    628786{
    629   /* move left if we can, and maybe up a line */
    630   if (e->buffx>0) {
    631     e->buffx--;
    632   } else if (e->buffy>0) {
    633     e->buffy--;
    634     e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
    635   }
    636 
    637   /* do we need to scroll up? */
    638   if (e->buffy-e->topline < 0) {
    639     e->topline-=e->winlines/2;
     787  int i;
     788  char * p;
     789  i = _owl_editwin_get_index_from_xy(e);
     790  p = e->buff + i;
     791  p = g_utf8_find_prev_char(e->buff, p);
     792  while (p && g_unichar_ismark(g_utf8_get_char(p))) {
     793    p = g_utf8_find_prev_char(e->buff, p);
     794  }
     795  if (p == NULL) p = e->buff;
     796  _owl_editwin_set_xy_by_index(e, p - e->buff);
     797
     798  if (e->buffy - e->topline < 0) {
     799    e->topline -= e->winlines / 2;
    640800  }
    641801
     
    647807{
    648808  int i;
    649 
    650   /* move right if we can, and skip down a line if needed */
    651   i=owl_editwin_get_numchars_on_line(e, e->buffy);
    652   if (e->buffx < i) {
    653     e->buffx++;
    654     /*  } else if (e->buffy+1 < owl_editwin_get_numlines(e)) { */
    655   } else if (_owl_editwin_get_index_from_xy(e) < e->bufflen) {
    656     if (e->style==OWL_EDITWIN_STYLE_MULTILINE) {
    657       e->buffx=0;
    658       e->buffy++;
    659     }
     809  char * p;
     810  i = _owl_editwin_get_index_from_xy(e);
     811  p = e->buff + i;
     812  p = g_utf8_find_next_char(p, NULL);
     813  while (p && g_unichar_ismark(g_utf8_get_char(p))) {
     814    p = g_utf8_find_next_char(p, NULL);
     815  }
     816  if (p == NULL) {
     817    _owl_editwin_set_xy_by_index(e, e->bufflen);
     818  }
     819  else {
     820    _owl_editwin_set_xy_by_index(e, p - e->buff);
    660821  }
    661822
    662823  /* do we need to scroll down? */
    663   if (e->buffy-e->topline >= e->winlines) {
    664     e->topline+=e->winlines/2;
     824  if (e->buffy - e->topline >= e->winlines) {
     825    e->topline += e->winlines / 2;
    665826  }
    666827}
     
    669830{
    670831  int i, x;
     832  gunichar c = '\0';
    671833
    672834  /* if we're starting on a space, find the first non-space */
     
    681843  }
    682844
    683   /* find the next space, newline or end of line and go there, if
    684      already at the end of the line, continue on to the next */
    685   i=owl_editwin_get_numchars_on_line(e, e->buffy);
     845  /* find the next space, newline or end of line and go
     846     there, if already at the end of the line, continue on to the next */
     847  i=owl_editwin_get_numcells_on_line(e, e->buffy);
     848  c = _owl_editwin_get_char_at_xy(e);
    686849  if (e->buffx < i) {
    687850    /* move right till end of line */
    688851    while (e->buffx < i) {
    689       e->buffx++;
    690       if (e->buff[_owl_editwin_get_index_from_xy(e)]==' ') return;
     852      owl_editwin_key_right(e);
     853      c = _owl_editwin_get_char_at_xy(e);
     854      if (c == ' ') return;
    691855      if (e->buffx == i) return;
    692856    }
     
    694858    /* try to move down */
    695859    if (e->style==OWL_EDITWIN_STYLE_MULTILINE) {
    696       if (e->buffy+1 <  owl_editwin_get_numlines(e)) {
     860      if (e->buffy+1 < owl_editwin_get_numlines(e)) {
    697861        e->buffx=0;
    698862        e->buffy++;
     
    707871void owl_editwin_move_to_previousword(owl_editwin *e)
    708872{
    709   int i, x;
     873  int i;
     874  gunichar c;
     875  char *ptr1, *ptr2;
    710876
    711877  /* are we already at the beginning of the word? */
    712   i=_owl_editwin_get_index_from_xy(e);
    713   if ( (e->buff[i]!=' ' && e->buff[i]!='\n' && e->buff[i]!='\0') &&
    714        (e->buff[i-1]==' ' || e->buff[i-1]=='\n') ) {
     878  c = _owl_editwin_get_char_at_xy(e);
     879  i = _owl_editwin_get_index_from_xy(e);
     880  ptr1 = e->buff + i;
     881  if (*ptr1 != ' ' && *ptr1 != '\n' && *ptr1 != '\0' ) {
     882    ptr1 = g_utf8_find_prev_char(e->buff, ptr1);
     883    c = g_utf8_get_char(ptr1);
     884    if (c == ' ' || c == '\n') {
     885      owl_editwin_key_left(e);     
     886    }
     887  }
     888
     889  /* are we starting on a space character? */
     890  i = _owl_editwin_get_index_from_xy(e);
     891  while (i > e->lock && (e->buff[i] == ' ' || e->buff[i] == '\n' || e->buff[i] == '\0')) {
     892    /* find the first non-space */
     893    owl_editwin_key_left(e);     
     894    i = _owl_editwin_get_index_from_xy(e);
     895  }
     896
     897  /* find the last non-space */
     898  ptr1 = e->buff + _owl_editwin_get_index_from_xy(e);
     899  while (ptr1 >= e->buff + e->lock) {
     900    ptr2 = g_utf8_find_prev_char(e->buff, ptr1);
     901    if (!ptr2) break;
     902   
     903    c = g_utf8_get_char(ptr2);
     904    if (c == ' ' || c == '\n'){
     905      break;
     906    }
    715907    owl_editwin_key_left(e);
    716   }
    717    
    718   /* are we starting on a space character? */
    719   i=_owl_editwin_get_index_from_xy(e);
    720   if (e->buff[i]==' ' || e->buff[i]=='\n' || e->buff[i]=='\0') {
    721     /* find the first non-space */
    722     for (x=i; x>=e->lock; x--) {
    723       if (e->buff[x]!=' ' && e->buff[x]!='\n' && e->buff[x]!='\0') {
    724         _owl_editwin_set_xy_by_index(e, x);
    725         break;
    726       }
    727     }
    728   }
    729 
    730   /* find the last non-space */
    731   i=_owl_editwin_get_index_from_xy(e);
    732   for (x=i; x>=e->lock; x--) {
    733     if (e->buff[x-1]==' ' || e->buff[x-1]=='\n') {
    734       _owl_editwin_set_xy_by_index(e, x);
    735       break;
    736     }
    737   }
    738   _owl_editwin_set_xy_by_index(e, x);
     908    ptr1 = e->buff + _owl_editwin_get_index_from_xy(e);
     909  }
    739910}
    740911
     
    742913void owl_editwin_delete_nextword(owl_editwin *e)
    743914{
    744   int z;
     915  char *ptr1, *start;
     916  gunichar c;
    745917
    746918  if (e->bufflen==0) return;
    747919
    748   /* if we start out on a space character then gobble all the spaces
    749      up first */
    750   while (1) {
    751     z=_owl_editwin_get_index_from_xy(e);
    752     if (e->buff[z]==' ' || e->buff[z]=='\n') {
    753       owl_editwin_delete_char(e);
    754     } else {
    755       break;
    756     }
    757   }
    758 
    759   /* then nuke the next word */
    760   while (1) {
    761     z=_owl_editwin_get_index_from_xy(e);
    762     /* z == e->bufflen check added to prevent a hang I (nelhage) have
    763        seen repeatedly while using owl. I'm not sure precisely what
    764        conditions lead to it. */
    765     if (z == e->bufflen
    766         || e->buff[z+1]==' ' || e->buff[z+1]=='\n' || e->buff[z+1]=='\0') break;
    767     owl_editwin_delete_char(e);
    768   }
    769   owl_editwin_delete_char(e);
     920  start = ptr1 = e->buff + _owl_editwin_get_index_from_xy(e);
     921  /* if we start out on a space character then jump past all the
     922     spaces up first */
     923  while (*ptr1 == ' ' || *ptr1 == '\n') {
     924    ++ptr1;
     925  }
     926
     927  /* then jump past the next word */
     928 
     929  while (ptr1 && ptr1 - e->buff < e->bufflen) {
     930    c = g_utf8_get_char(ptr1);
     931    if (c == ' ' || c == '\n' || c == '\0') break;
     932    ptr1 = g_utf8_find_next_char(ptr1, NULL);
     933  }
     934
     935  if (ptr1) { /* We broke on a space, */
     936    ptr1 = g_utf8_find_next_char(ptr1, NULL);
     937    if (ptr1) { /* and there's a character after it, */
     938      /* nuke everything back to our starting point. */
     939      _owl_editwin_remove_bytes(e, ptr1 - start);
     940      return;
     941    }
     942  }
     943 
     944  /* If we get here, we ran out of string, drop what's left. */
     945  *start = '\0';
     946  e->bufflen = start - e->buff;
    770947}
    771948
     
    773950{
    774951  /* go backwards to the last non-space character, then delete chars */
    775   int i, startpos, endpos;
     952  int startpos, endpos;
    776953
    777954  startpos = _owl_editwin_get_index_from_xy(e);
    778955  owl_editwin_move_to_previousword(e);
    779956  endpos = _owl_editwin_get_index_from_xy(e);
    780   for (i=0; i<startpos-endpos; i++) {
    781     owl_editwin_delete_char(e);
    782   }
     957  _owl_editwin_remove_bytes(e, startpos-endpos);
    783958}
    784959
     
    787962  int i;
    788963
    789   if (owl_editwin_get_numchars_on_line(e, e->buffy)>e->buffx) {
     964  if (owl_editwin_get_numchars_on_line(e, e->buffy) > e->buffx) {
    790965    /* normal line */
    791966    i=_owl_editwin_get_index_from_xy(e);
     
    808983void owl_editwin_move_to_line_end(owl_editwin *e)
    809984{
    810   e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     985  e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    811986}
    812987
     
    821996  /* go to last char */
    822997  e->buffy=owl_editwin_get_numlines(e)-1;
    823   e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     998  e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    824999  owl_editwin_key_right(e);
    8251000
     
    8611036  /* main loop */
    8621037  while (1) {
    863     i=_owl_editwin_get_index_from_xy(e);
     1038    i = _owl_editwin_get_index_from_xy(e);
    8641039
    8651040    /* bail if we hit the end of the buffer */
    866     if (i>=e->bufflen) break;
     1041    if (i >= e->bufflen || e->buff[i] == '\0') break;
    8671042
    8681043    /* bail if we hit the end of the paragraph */
    869     if (e->buff[i]=='\n' && e->buff[i+1]=='\n') break;
     1044    if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break;
    8701045
    8711046    /* if we've travelled too far, linewrap */
    8721047    if ((e->buffx) >= e->fillcol) {
     1048      int len = e->bufflen;
    8731049      _owl_editwin_linewrap_word(e);
     1050      /* we may have added a character. */
     1051      if (i < save) save += e->bufflen - len;
     1052      _owl_editwin_set_xy_by_index(e, i);
    8741053    }
    8751054
    8761055    /* did we hit the end of a line too soon? */
    877     i=_owl_editwin_get_index_from_xy(e);
    878     if (e->buff[i]=='\n' && e->buffx<e->fillcol-1) {
     1056    /* asedeno: Here we replace a newline with a space. We may want to
     1057       consider removing the space if the characters to either side
     1058       are CJK ideograms.*/
     1059    i = _owl_editwin_get_index_from_xy(e);
     1060    if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) {
    8791061      /* ********* we need to make sure we don't pull in a word that's too long ***********/
    8801062      e->buff[i]=' ';
    8811063    }
    882    
     1064
    8831065    /* fix spacing */
    884     i=_owl_editwin_get_index_from_xy(e);
    885     if (e->buff[i]==' ' && e->buff[i+1]==' ') {
    886       if (e->buff[i-1]=='.' || e->buff[i-1]=='!' || e->buff[i-1]=='?') {
     1066    i = _owl_editwin_get_index_from_xy(e);
     1067    if (e->buff[i] == ' ' && e->buff[i+1] == ' ') {
     1068      if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') {
    8871069        owl_editwin_key_right(e);
    8881070      } else {
    8891071        owl_editwin_delete_char(e);
    890         /* if we did this ahead of the save point, adjust it */
    891         if (i<save) save--;
     1072        /* if we did this ahead of the save point, adjust it. Changing
     1073           by one is fine here because we're only removing an ASCII
     1074           space. */
     1075        if (i < save) save--;
    8921076      }
    8931077    } else {
    8941078      owl_editwin_key_right(e);
    8951079    }
    896 
    8971080  }
    8981081
     
    9151098int owl_editwin_check_dotsend(owl_editwin *e)
    9161099{
    917   int i;
     1100  char *p, *p_n, *p_p;
     1101  gunichar c;
    9181102
    9191103  if (!e->dotsend) return(0);
    920   for (i=e->bufflen-1; i>0; i--) {
    921     if (e->buff[i] == '.'
    922         && (e->buff[i-1] == '\n' || e->buff[i-1] == '\r')
    923         && (e->buff[i+1] == '\n' || e->buff[i+1] == '\r')) {
    924       e->bufflen = i;
    925       e->buff[i] = '\0';
     1104
     1105  p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen);
     1106  p_n = g_utf8_find_next_char(p, NULL);
     1107  p_p = g_utf8_find_prev_char(e->buff, p);
     1108  c = g_utf8_get_char(p);
     1109  while (p != NULL) {
     1110    if (*p == '.'
     1111        && p_p != NULL && (*p_p == '\n' || *p_p == '\r')
     1112        && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) {
     1113      e->bufflen = p - e->buff;
     1114      e->buff[e->bufflen] = '\0';
    9261115      return(1);
    9271116    }
    928     if (!isspace((int) e->buff[i])) {
    929       return(0);
    930     }
     1117    if (c != '\0' && !g_unichar_isspace(c)) return(0);
     1118    p_n = p;
     1119    p = p_p;
     1120    c = g_utf8_get_char(p);
     1121    p_p = g_utf8_find_prev_char(e->buff, p);
    9311122  }
    9321123  return(0);
    9331124}
    9341125
    935 void owl_editwin_post_process_char(owl_editwin *e, int j)
     1126void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
    9361127{
    9371128  /* check if we need to scroll down */
     
    9391130    e->topline+=e->winlines/2;
    9401131  }
    941   if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) {
     1132  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
    9421133    owl_command_editmulti_done(e);
    9431134    return;
     
    9461137}
    9471138
    948 void owl_editwin_process_char(owl_editwin *e, int j)
    949 {
    950   if (j == ERR) return;
    951   if (j>127 || ((j<32) && (j!=10) && (j!=13))) {
    952     return;
    953   } else {
     1139void _owl_editwin_process_char(owl_editwin *e, gunichar j)
     1140{
     1141  if (!(g_unichar_iscntrl(j) && (j != 10) && (j != 13))) {
    9541142    owl_editwin_insert_char(e, j);
     1143  }
     1144}
     1145
     1146
     1147void owl_editwin_process_char(owl_editwin *e, owl_input j)
     1148{
     1149  if (j.ch == ERR) return;
     1150  /* Ignore ncurses control characters. */
     1151  if (j.ch < 0x100) {
     1152    _owl_editwin_process_char(e, j.uch);
    9551153  }
    9561154}
     
    9791177  }
    9801178
    981   /* now go to the xth character */
    982   ptr2=strchr(ptr1, '\n');
    983   if (!ptr2) {
    984     return(e->buff + e->bufflen - ptr1);
    985   }
    986   return(ptr2-ptr1); /* don't count the newline for now */
     1179  /* now count characters */
     1180  i = 0;
     1181  ptr2 = ptr1;
     1182  while (ptr2 - e->buff < e->bufflen
     1183         && *ptr2 != '\n') {
     1184    ++i;
     1185    ptr2 = g_utf8_next_char(ptr2);
     1186  }
     1187  return i;
     1188}
     1189
     1190int owl_editwin_get_numcells_on_line(owl_editwin *e, int line)
     1191{
     1192  int i;
     1193  char *ptr1, *ptr2;
     1194  gunichar c;
     1195
     1196  if (e->bufflen==0) return(0);
     1197 
     1198  /* first go to the yth line */
     1199  ptr1=e->buff;
     1200  for (i=0; i<line; i++) {
     1201    ptr2=strchr(ptr1, '\n');
     1202    if (!ptr2) {
     1203      /* we're already on the last line */
     1204      return(0);
     1205    }
     1206    ptr1=ptr2+1;
     1207  }
     1208
     1209  /* now count cells */
     1210  i = 0;
     1211  ptr2 = ptr1;
     1212  while (ptr2 - e->buff < e->bufflen
     1213         && *ptr2 != '\n') {
     1214    c = g_utf8_get_char(ptr2);
     1215    i += mk_wcwidth(c);
     1216    ptr2 = g_utf8_next_char(ptr2);
     1217  }
     1218  return i;
    9871219}
    9881220
Note: See TracChangeset for help on using the changeset viewer.