Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • editwin.c

    rcc6f009 rb2c1bd4  
    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    ptr1 = g_utf8_find_prev_char(start, ptr1);
     380  }
     381  return -1;
    337382}
    338383
     
    340385 * characters over)
    341386 */
    342 void owl_editwin_insert_char(owl_editwin *e, char c)
    343 {
    344  
    345   int z, i, ret;
     387void owl_editwin_insert_char(owl_editwin *e, gunichar c)
     388{
     389  int z, i, ret, len;
     390  char tmp[6];
     391  memset(tmp, '\0', 6);
    346392
    347393  /* \r is \n */
    348   if (c=='\r') {
    349     c='\n';
    350   }
    351 
    352   if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {
     394  if (c == '\r') {
     395    c = '\n';
     396  }
     397
     398  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
    353399    /* perhaps later this will change some state that allows the string
    354400       to be read */
     
    356402  }
    357403
     404  g_unichar_to_utf8(c, tmp);
     405  len = strlen(tmp);
     406
    358407  /* make sure there is enough memory for the new text */
    359   if ((e->bufflen+1) > (e->allocated-5)) {
     408  if ((e->bufflen + len) > (e->allocated - 5)) {
    360409    _owl_editwin_addspace(e);
    361410  }
    362411
    363412  /* get the insertion point */
    364   z=_owl_editwin_get_index_from_xy(e);
     413  z = _owl_editwin_get_index_from_xy(e);
    365414
    366415  /* 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) {
     416  if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) {
     417    ret = _owl_editwin_linewrap_word(e);
     418    if (ret == -1) {
    370419      /* we couldn't wrap, insert a hard newline instead */
    371420      owl_editwin_insert_char(e, '\n');
     
    373422  }
    374423
    375   z=_owl_editwin_get_index_from_xy(e);
    376424  /* 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;
     425  if (z != e->bufflen) {
     426    _owl_editwin_insert_bytes(e, len);
     427  }
     428
     429  /* insert the new character */
     430  for(i = 0; i < len; i++) {
     431    e->buff[z + i] = tmp[i];
     432  }
    383433
    384434  /* housekeeping */
    385   e->bufflen++;
    386   e->buff[e->bufflen]='\0';
    387 
     435  e->bufflen += len;
     436  e->buff[e->bufflen] = '\0';
     437 
    388438  /* advance the cursor */
    389   if (c=='\n') {
    390     e->buffx=0;
    391     e->buffy++;
    392   } else {
    393     e->buffx++;
    394   }
     439  z += len;
     440  _owl_editwin_set_xy_by_index(e, z);
    395441}
    396442
    397443/* 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  
     444void owl_editwin_overwrite_char(owl_editwin *e, gunichar c)
     445{
     446  int z, oldlen, newlen, i;
     447  char tmp[6];
     448  memset(tmp, '\0', 6);
     449
    402450  /* \r is \n */
    403   if (c=='\r') {
    404     c='\n';
    405   }
    406 
    407   if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {
     451  if (c == '\r') {
     452    c = '\n';
     453  }
     454 
     455  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
    408456    /* perhaps later this will change some state that allows the string
    409457       to be read */
     
    411459  }
    412460
    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)) {
     461  g_unichar_to_utf8(c, tmp);
     462  newlen = strlen(tmp);
     463
     464  z = _owl_editwin_get_index_from_xy(e);
     465  {
     466    char *t = g_utf8_find_next_char(e->buff + z, NULL);
     467    oldlen = (t ? (t - (e->buff + z)) : 0);
     468  }
     469
     470  /* only if we are at the end of the buffer do we create new space here */
     471  if (z == e->bufflen) {
     472    if ((e->bufflen+newlen) > (e->allocated-5)) {
    418473      _owl_editwin_addspace(e);
    419474    }
    420475  }
    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 
     476  /* if not at the end of the buffer, adjust based in char size difference. */
     477  else if (oldlen > newlen) {
     478    _owl_editwin_remove_bytes(e, oldlen-newlen);
     479  }
     480  else /* oldlen < newlen */ {
     481    _owl_editwin_insert_bytes(e, newlen-oldlen);
     482  }
     483  /* Overwrite the old char*/
     484  for (i = 0; i < newlen; i++) {
     485    e->buff[z+i] = tmp[i];
     486  }
     487       
     488  /* housekeeping */
     489  if (z == e->bufflen) {
     490    e->bufflen += newlen;
     491    e->buff[e->bufflen] = '\0';
     492  }
     493 
    430494  /* advance the cursor */
    431   if (c=='\n') {
    432     e->buffx=0;
    433     e->buffy++;
    434   } else {
    435     e->buffx++;
    436   }
    437 
     495  z += newlen;
     496  _owl_editwin_set_xy_by_index(e, z);
    438497}
    439498
     
    443502void owl_editwin_delete_char(owl_editwin *e)
    444503{
    445   int z, i;
    446 
    447   if (e->bufflen==0) return;
     504  int z;
     505  char *p1, *p2;
     506  gunichar c;
     507
     508  if (e->bufflen == 0) return;
    448509 
    449510  /* 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';
     511  z = _owl_editwin_get_index_from_xy(e);
     512
     513  if (z == e->bufflen) return;
     514
     515  p1 = e->buff + z;
     516  p2 = g_utf8_next_char(p1);
     517  c = g_utf8_get_char(p2);
     518  while (g_unichar_ismark(c)) {
     519    p2 = g_utf8_next_char(p2);
     520    c = g_utf8_get_char(p2);
     521  }
     522  _owl_editwin_remove_bytes(e, p2-p1);
    459523}
    460524
     
    467531{
    468532  int z;
    469   char tmp;
    470 
    471   if (e->bufflen==0) return;
     533  char *p1, *p2, *p3, *tmp;
     534
     535  if (e->bufflen == 0) return;
    472536 
    473537  /* get the cursor point */
    474   z=_owl_editwin_get_index_from_xy(e);
    475 
    476   if (z==e->bufflen) {
     538  z = _owl_editwin_get_index_from_xy(e);
     539
     540  if (z == e->bufflen) {
    477541    /* point is after last character */
    478542    z--;
    479543  } 
    480544
    481   if (z-1 < e->lock) {
     545  if (z - 1 < e->lock) {
    482546    /* point is at beginning of buffer, do nothing */
    483547    return;
    484548  }
    485549
    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);
     550  /* Transpose two utf-8 unicode glyphs. */
     551  p1 = e->buff + z;
     552
     553  p2 = g_utf8_find_next_char(p1, NULL);
     554  while (p2 != NULL && g_unichar_ismark(g_utf8_get_char(p2))) {
     555    p2 = g_utf8_find_next_char(p2, NULL);
     556  }
     557  if (p2 == NULL) return;
     558
     559  p3 = g_utf8_find_prev_char(e->buff, p1);
     560  while (p3 != NULL && g_unichar_ismark(g_utf8_get_char(p3))) {
     561    p3 = g_utf8_find_prev_char(p3, NULL);
     562  }
     563  if (p3 == NULL) return;
     564
     565  tmp = owl_malloc(p2 - p3 + 5);
     566  *tmp = '\0';
     567  strncat(tmp, p1, p2 - p1);
     568  strncat(tmp, p3, p1 - p3);
     569  strncpy(p3, tmp, p2 - p3);
     570  owl_free(tmp);
     571  _owl_editwin_set_xy_by_index(e, p3 - e->buff);
    490572}
    491573
     
    495577void owl_editwin_insert_string(owl_editwin *e, char *string)
    496578{
    497   int i, j;
    498 
    499   j=strlen(string);
    500   for (i=0; i<j; i++) {
    501     owl_editwin_insert_char(e, string[i]);
     579  char *p;
     580  gunichar c;
     581  if (!g_utf8_validate(string, -1, NULL)) {
     582    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
     583    return;
     584  }
     585  p = string;
     586  c = g_utf8_get_char(p);
     587  while (c) {
     588    _owl_editwin_process_char(e, c);
     589    p = g_utf8_next_char(p);
     590    c = g_utf8_get_char(p);
    502591  }
    503592}
     
    509598void owl_editwin_overwrite_string(owl_editwin *e, char *string)
    510599{
    511   int i, j;
    512 
    513   j=strlen(string);
    514   for (i=0; i<j; i++) {
    515     owl_editwin_overwrite_char(e, string[i]);
     600  char *p;
     601  gunichar c;
     602
     603  if (!g_utf8_validate(string, -1, NULL)) {
     604    owl_function_debugmsg("owl_editwin_overwrite_string: received non-utf-8 string.");
     605    return;
     606  }
     607  p = string;
     608  c = g_utf8_get_char(p);
     609  while (c) {
     610    owl_editwin_overwrite_char(e, c);
     611    p = g_utf8_next_char(p);
     612    c = g_utf8_get_char(p);
    516613  }
    517614}
     
    524621  int i;
    525622  char *ptr1, *ptr2;
    526 
    527   if (e->bufflen==0) return(0);
     623  gunichar c;
     624
     625  if (e->bufflen == 0) return(0);
    528626 
    529627  /* first go to the yth line */
    530   ptr1=e->buff;
    531   for (i=0; i<e->buffy; i++) {
    532     ptr2=strchr(ptr1, '\n');
     628  ptr1 = e->buff;
     629  for (i = 0; i < e->buffy; i++) {
     630    ptr2= strchr(ptr1, '\n');
    533631    if (!ptr2) {
    534632      /* we're already on the last line */
    535633      break;
    536634    }
    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 }
     635    ptr1 = ptr2 + 1;
     636  }
     637
     638  /* now go to the xth cell */
     639  ptr2 = ptr1;
     640  i = 0;
     641  while (ptr2 != NULL && i < e->buffx && (ptr2 - e->buff) < e->bufflen) {
     642    c = g_utf8_get_char(ptr2);
     643    i += (c == '\n' ? 1 : mk_wcwidth(c));
     644    ptr2 = g_utf8_next_char(ptr2);
     645  }
     646  while(ptr2 != NULL && g_unichar_ismark(g_utf8_get_char(ptr2))) {
     647    ptr2 = g_utf8_next_char(ptr2);
     648  }
     649  if (ptr2 == NULL) return e->bufflen;
     650  return(ptr2 - e->buff);
     651}
     652
     653/* We assume x,y are not set to point to a mid-char */
     654gunichar _owl_editwin_get_char_at_xy(owl_editwin *e)
     655{
     656  return g_utf8_get_char(e->buff + _owl_editwin_get_index_from_xy(e));
     657}
     658
    555659
    556660void _owl_editwin_set_xy_by_index(owl_editwin *e, int index)
    557661{
    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   }
     662  char *ptr1, *ptr2, *target;
     663  gunichar c;
     664
     665  e->buffx = 0;
     666  e->buffy = 0;
     667
     668  ptr1 = e->buff;
     669  target = ptr1 + index;
     670  /* target sanitizing */
     671  if ((target[0] & 0x80) && (~target[0] & 0x40)) {
     672    /* middle of a utf-8 character, back up to previous character. */
     673    target = g_utf8_find_prev_char(e->buff, target);
     674  }
     675  c = g_utf8_get_char(target);
     676  while (g_unichar_ismark(c) && target > e->buff) {
     677    /* Adjust the target off of combining characters and the like. */
     678    target = g_utf8_find_prev_char(e->buff, target);
     679    c = g_utf8_get_char(target);
     680  }
     681  /* If we start with a mark, something is wrong.*/
     682  if (g_unichar_ismark(c)) return;
     683
     684  /* Now our target should be acceptable. */
     685  ptr2 = strchr(ptr1, '\n');
     686  while (ptr2 != NULL && ptr2 < target) {
     687    e->buffy++;
     688    ptr1 = ptr2 + 1;
     689    ptr2 = strchr(ptr1, '\n');
     690  }
     691  ptr2 = ptr1;
     692  while (ptr2 != NULL && ptr2 < target) {
     693    c = g_utf8_get_char(ptr2);
     694    e->buffx += mk_wcwidth(c);
     695    ptr2 = g_utf8_next_char(ptr2);
     696  }
     697}
     698
     699int _owl_editwin_cursor_adjustment(owl_editwin *e)
     700{
     701  char *ptr1, *ptr2;
     702  gunichar c;
     703  int x, i;
     704
     705  /* Find line */
     706  ptr1 = e->buff;
     707  ptr2 = strchr(ptr1, '\n');
     708  for (i = 0; ptr2 != NULL && i < e->buffy; i++) {
     709    ptr1 = ptr2 + 1;
     710    ptr2 = strchr(ptr1, '\n');
     711  }
     712  ptr2 = ptr1;
     713
     714  /* Find char */
     715  x = 0;
     716  while (ptr2 != NULL && x < e->buffx) {
     717    if (*ptr2 == '\n') return 0;
     718    c = g_utf8_get_char(ptr2);
     719    x += mk_wcwidth(c);
     720    ptr2 = g_utf8_next_char(ptr2);
     721  }
     722 
     723  /* calculate x offset */
     724  return x - e->buffx;
    570725}
    571726
     
    574729  /* if we happen to have the cursor over locked text
    575730   * move it to be out of the locktext region */
    576   if (_owl_editwin_get_index_from_xy(e)<e->lock) {
     731  if (_owl_editwin_get_index_from_xy(e) < e->lock) {
    577732    _owl_editwin_set_xy_by_index(e, e->lock);
    578733  }
     
    594749{
    595750  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);
     751  if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) {
     752    e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    598753  }
    599754
     
    612767
    613768  /* 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);
     769  if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) {
     770    e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    616771  }
    617772
     
    627782void owl_editwin_key_left(owl_editwin *e)
    628783{
    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;
     784  int i;
     785  char * p;
     786  i = _owl_editwin_get_index_from_xy(e);
     787  p = e->buff + i;
     788  p = g_utf8_find_prev_char(e->buff, p);
     789  while (p && g_unichar_ismark(g_utf8_get_char(p))) {
     790    p = g_utf8_find_prev_char(e->buff, p);
     791  }
     792  if (p == NULL) p = e->buff;
     793  _owl_editwin_set_xy_by_index(e, p - e->buff);
     794
     795  if (e->buffy - e->topline < 0) {
     796    e->topline -= e->winlines / 2;
    640797  }
    641798
     
    647804{
    648805  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     }
     806  char * p;
     807  i = _owl_editwin_get_index_from_xy(e);
     808  p = e->buff + i;
     809  p = g_utf8_find_next_char(p, NULL);
     810  while (p && g_unichar_ismark(g_utf8_get_char(p))) {
     811    p = g_utf8_find_next_char(p, NULL);
     812  }
     813  if (p == NULL) {
     814    _owl_editwin_set_xy_by_index(e, e->bufflen);
     815  }
     816  else {
     817    _owl_editwin_set_xy_by_index(e, p - e->buff);
    660818  }
    661819
    662820  /* do we need to scroll down? */
    663   if (e->buffy-e->topline >= e->winlines) {
    664     e->topline+=e->winlines/2;
     821  if (e->buffy - e->topline >= e->winlines) {
     822    e->topline += e->winlines / 2;
    665823  }
    666824}
     
    669827{
    670828  int i, x;
     829  gunichar c = '\0';
    671830
    672831  /* if we're starting on a space, find the first non-space */
     
    681840  }
    682841
    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);
     842  /* find the next space, newline or end of line and go
     843     there, if already at the end of the line, continue on to the next */
     844  i=owl_editwin_get_numcells_on_line(e, e->buffy);
     845  c = _owl_editwin_get_char_at_xy(e);
    686846  if (e->buffx < i) {
    687847    /* move right till end of line */
    688848    while (e->buffx < i) {
    689       e->buffx++;
    690       if (e->buff[_owl_editwin_get_index_from_xy(e)]==' ') return;
     849      owl_editwin_key_right(e);
     850      c = _owl_editwin_get_char_at_xy(e);
     851      if (c == ' ') return;
    691852      if (e->buffx == i) return;
    692853    }
     
    694855    /* try to move down */
    695856    if (e->style==OWL_EDITWIN_STYLE_MULTILINE) {
    696       if (e->buffy+1 <  owl_editwin_get_numlines(e)) {
     857      if (e->buffy+1 < owl_editwin_get_numlines(e)) {
    697858        e->buffx=0;
    698859        e->buffy++;
     
    707868void owl_editwin_move_to_previousword(owl_editwin *e)
    708869{
    709   int i, x;
     870  int i;
     871  gunichar c;
     872  char *ptr1, *ptr2;
    710873
    711874  /* 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') ) {
     875  c = _owl_editwin_get_char_at_xy(e);
     876  i = _owl_editwin_get_index_from_xy(e);
     877  ptr1 = e->buff + i;
     878  if (*ptr1 != ' ' && *ptr1 != '\n' && *ptr1 != '\0' ) {
     879    ptr1 = g_utf8_find_prev_char(e->buff, ptr1);
     880    c = g_utf8_get_char(ptr1);
     881    if (c == ' ' || c == '\n') {
     882      owl_editwin_key_left(e);     
     883    }
     884  }
     885
     886  /* are we starting on a space character? */
     887  i = _owl_editwin_get_index_from_xy(e);
     888  while (e->buff[i] == ' ' || e->buff[i] == '\n' || e->buff[i] == '\0') {
     889    /* find the first non-space */
     890    owl_editwin_key_left(e);     
     891    i = _owl_editwin_get_index_from_xy(e);
     892  }
     893
     894  /* find the last non-space */
     895  owl_editwin_key_left(e);
     896  ptr1 = e->buff + _owl_editwin_get_index_from_xy(e);
     897  while (ptr1 >= e->buff + e->lock) {
     898    ptr2 = g_utf8_find_prev_char(e->buff, ptr1);
     899    if (!ptr2) break;
     900   
     901    c = g_utf8_get_char(ptr2);
     902    if (c == ' ' || c == '\n'){
     903      break;
     904    }
    715905    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);
     906    ptr1 = e->buff + _owl_editwin_get_index_from_xy(e);
     907  }
    739908}
    740909
     
    742911void owl_editwin_delete_nextword(owl_editwin *e)
    743912{
    744   int z;
     913  char *ptr1, *start;
     914  gunichar c;
    745915
    746916  if (e->bufflen==0) return;
    747917
    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);
     918  start = ptr1 = e->buff + _owl_editwin_get_index_from_xy(e);
     919  /* if we start out on a space character then jump past all the
     920     spaces up first */
     921  while (*ptr1 == ' ' || *ptr1 == '\n') {
     922    ++ptr1;
     923  }
     924
     925  /* then jump past the next word */
     926 
     927  while (ptr1 && ptr1 - e->buff < e->bufflen) {
     928    c = g_utf8_get_char(ptr1);
     929    if (c == ' ' || c == '\n' || c == '\0') break;
     930    ptr1 = g_utf8_find_next_char(ptr1, NULL);
     931  }
     932
     933  if (ptr1) { /* We broke on a space, */
     934    ptr1 = g_utf8_find_next_char(ptr1, NULL);
     935    if (ptr1) { /* and there's a character after it, */
     936      /* nuke everything back to our starting point. */
     937      _owl_editwin_remove_bytes(e, ptr1 - start);
     938      return;
     939    }
     940  }
     941 
     942  /* If we get here, we ran out of string, drop what's left. */
     943  *start = '\0';
     944  e->bufflen = start - e->buff;
    770945}
    771946
     
    773948{
    774949  /* go backwards to the last non-space character, then delete chars */
    775   int i, startpos, endpos;
     950  int startpos, endpos;
    776951
    777952  startpos = _owl_editwin_get_index_from_xy(e);
    778953  owl_editwin_move_to_previousword(e);
    779954  endpos = _owl_editwin_get_index_from_xy(e);
    780   for (i=0; i<startpos-endpos; i++) {
    781     owl_editwin_delete_char(e);
    782   }
     955  _owl_editwin_remove_bytes(e, startpos-endpos);
    783956}
    784957
     
    787960  int i;
    788961
    789   if (owl_editwin_get_numchars_on_line(e, e->buffy)>e->buffx) {
     962  if (owl_editwin_get_numchars_on_line(e, e->buffy) > e->buffx) {
    790963    /* normal line */
    791964    i=_owl_editwin_get_index_from_xy(e);
     
    808981void owl_editwin_move_to_line_end(owl_editwin *e)
    809982{
    810   e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     983  e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    811984}
    812985
     
    821994  /* go to last char */
    822995  e->buffy=owl_editwin_get_numlines(e)-1;
    823   e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     996  e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    824997  owl_editwin_key_right(e);
    825998
     
    8611034  /* main loop */
    8621035  while (1) {
    863     i=_owl_editwin_get_index_from_xy(e);
     1036    i = _owl_editwin_get_index_from_xy(e);
    8641037
    8651038    /* bail if we hit the end of the buffer */
    866     if (i>=e->bufflen) break;
     1039    if (i >= e->bufflen || e->buff[i] == '\0') break;
    8671040
    8681041    /* bail if we hit the end of the paragraph */
    869     if (e->buff[i]=='\n' && e->buff[i+1]=='\n') break;
     1042    if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break;
    8701043
    8711044    /* if we've travelled too far, linewrap */
    8721045    if ((e->buffx) >= e->fillcol) {
     1046      int len = e->bufflen;
    8731047      _owl_editwin_linewrap_word(e);
     1048      /* we may have added a character. */
     1049      if (i < save) save += e->bufflen - len;
    8741050    }
    8751051
    8761052    /* 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) {
     1053    /* asedeno: Here we replace a newline with a space. We may want to
     1054       consider removing the space if the characters to either side
     1055       are CJK ideograms.*/
     1056    i = _owl_editwin_get_index_from_xy(e);
     1057    if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) {
    8791058      /* ********* we need to make sure we don't pull in a word that's too long ***********/
    8801059      e->buff[i]=' ';
    8811060    }
    882    
     1061
    8831062    /* 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]=='?') {
     1063    i = _owl_editwin_get_index_from_xy(e);
     1064    if (e->buff[i] == ' ' && e->buff[i+1] == ' ') {
     1065      if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') {
    8871066        owl_editwin_key_right(e);
    8881067      } else {
    8891068        owl_editwin_delete_char(e);
    890         /* if we did this ahead of the save point, adjust it */
    891         if (i<save) save--;
     1069        /* if we did this ahead of the save point, adjust it. Changing
     1070           by one is fine here because we're only removing an ASCII
     1071           space. */
     1072        if (i < save) save--;
    8921073      }
    8931074    } else {
    8941075      owl_editwin_key_right(e);
    8951076    }
    896 
    8971077  }
    8981078
     
    9151095int owl_editwin_check_dotsend(owl_editwin *e)
    9161096{
    917   int i;
     1097  char *p, *p_n, *p_p;
     1098  gunichar c;
    9181099
    9191100  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';
     1101
     1102  p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen);
     1103  p_n = g_utf8_find_next_char(p, NULL);
     1104  p_p = g_utf8_find_prev_char(e->buff, p);
     1105  c = g_utf8_get_char(p);
     1106  while (p != NULL) {
     1107    if (*p == '.'
     1108        && p_p != NULL && (*p_p == '\n' || *p_p == '\r')
     1109        && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) {
     1110      e->bufflen = p - e->buff;
     1111      e->buff[e->bufflen] = '\0';
    9261112      return(1);
    9271113    }
    928     if (!isspace((int) e->buff[i])) {
    929       return(0);
    930     }
     1114    if (c != '\0' && !g_unichar_isspace(c)) return(0);
     1115    p_n = p;
     1116    p = p_p;
     1117    c = g_utf8_get_char(p);
     1118    p_p = g_utf8_find_prev_char(e->buff, p);
    9311119  }
    9321120  return(0);
    9331121}
    9341122
    935 void owl_editwin_post_process_char(owl_editwin *e, int j)
     1123void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
    9361124{
    9371125  /* check if we need to scroll down */
     
    9391127    e->topline+=e->winlines/2;
    9401128  }
    941   if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) {
     1129  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
    9421130    owl_command_editmulti_done(e);
    9431131    return;
     
    9461134}
    9471135
    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 {
     1136void _owl_editwin_process_char(owl_editwin *e, gunichar j)
     1137{
     1138  if (!(g_unichar_iscntrl(j) && (j != 10) && (j != 13))) {
    9541139    owl_editwin_insert_char(e, j);
     1140  }
     1141}
     1142
     1143
     1144void owl_editwin_process_char(owl_editwin *e, owl_input j)
     1145{
     1146  if (j.ch == ERR) return;
     1147  /* Ignore ncurses control characters. */
     1148  if (j.ch < 0x100) {
     1149    _owl_editwin_process_char(e, j.uch);
    9551150  }
    9561151}
     
    9791174  }
    9801175
    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 */
     1176  /* now count characters */
     1177  i = 0;
     1178  ptr2 = ptr1;
     1179  while (ptr2 - e->buff < e->bufflen
     1180         && *ptr2 != '\n') {
     1181    ++i;
     1182    ptr2 = g_utf8_next_char(ptr2);
     1183  }
     1184  return i;
     1185}
     1186
     1187int owl_editwin_get_numcells_on_line(owl_editwin *e, int line)
     1188{
     1189  int i;
     1190  char *ptr1, *ptr2;
     1191  gunichar c;
     1192
     1193  if (e->bufflen==0) return(0);
     1194 
     1195  /* first go to the yth line */
     1196  ptr1=e->buff;
     1197  for (i=0; i<line; i++) {
     1198    ptr2=strchr(ptr1, '\n');
     1199    if (!ptr2) {
     1200      /* we're already on the last line */
     1201      return(0);
     1202    }
     1203    ptr1=ptr2+1;
     1204  }
     1205
     1206  /* now count cells */
     1207  i = 0;
     1208  ptr2 = ptr1;
     1209  while (ptr2 - e->buff < e->bufflen
     1210         && *ptr2 != '\n') {
     1211    c = g_utf8_get_char(ptr2);
     1212    i += mk_wcwidth(c);
     1213    ptr2 = g_utf8_next_char(ptr2);
     1214  }
     1215  return i;
    9871216}
    9881217
Note: See TracChangeset for help on using the changeset viewer.