Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • editwin.c

    r9a6cc40 rfac5463  
    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;
     148  /*  e->buffx=x; */
     149  /*  e->buffy=y; */
    149150  owl_editwin_adjust_for_locktext(e);
    150151  owl_editwin_redisplay(e, 0);
     
    251252
    252253  /* start at topline */
    253   ptr1=e->buff;
    254   for (i=0; i<e->topline; i++) {
    255     ptr2=strchr(ptr1, '\n');
     254  ptr1 = e->buff;
     255  for (i = 0; i < e->topline; i++) {
     256    ptr2 = strchr(ptr1, '\n');
    256257    if (!ptr2) {
    257258      /* we're already on the last line */
    258259      break;
    259260    }
    260     ptr1=ptr2+1;
     261    ptr1 = ptr2 + 1;
    261262  }
    262263  /* ptr1 now stores the starting point */
    263264
    264265  /* find the ending point and store it in ptr3 */
    265   ptr2=ptr1;
    266   ptr3=ptr1;
    267   for (i=0; i<e->winlines; i++) {
    268     ptr3=strchr(ptr2, '\n');
     266  ptr2 = ptr1;
     267  ptr3 = ptr1;
     268  for (i = 0; i < e->winlines; i++) {
     269    ptr3 = strchr(ptr2, '\n');
    269270    if (!ptr3) {
    270271      /* we've hit the last line */
    271272      /* print everything to the end */
    272       ptr3=e->buff+e->bufflen-1;
     273      ptr3 = e->buff + e->bufflen - 1;
    273274      ptr3--;
    274275      break;
    275276    }
    276     ptr2=ptr3+1;
    277   }
    278   ptr3+=2;
    279 
    280   buff=owl_malloc(ptr3-ptr1+50);
    281   strncpy(buff, ptr1, ptr3-ptr1);
    282   buff[ptr3-ptr1]='\0';
    283   if (e->echochar=='\0') {
     277    ptr2 = ptr3 + 1;
     278  }
     279  ptr3 += 2;
     280
     281  buff = owl_malloc(ptr3 - ptr1 + 50);
     282  strncpy(buff, ptr1, ptr3 - ptr1);
     283  buff[ptr3 - ptr1] = '\0';
     284  if (e->echochar == '\0') {
    284285    waddstr(e->curswin, buff);
    285286  } else {
    286287    /* translate to echochar, *except* for the locktext */
    287288    int len;
    288     int dolocklen=e->lock-(ptr1-e->buff);
    289 
    290     for (i=0; i<dolocklen; i++) {
     289    int dolocklen = e->lock - (ptr1 - e->buff);
     290
     291    for (i = 0; i < dolocklen; i++) {
    291292      waddch(e->curswin, buff[i]);
    292293    }
    293     len=strlen(buff);
    294     for (i=0; i<len-dolocklen; i++) {
     294    len = strlen(buff);
     295    for (i = 0; i < len-dolocklen; i++) {
    295296      waddch(e->curswin, e->echochar);
    296297    }
    297298  }
    298   wmove(e->curswin, e->buffy-e->topline, e->buffx);
     299  wmove(e->curswin, e->buffy-e->topline, e->buffx + _owl_editwin_cursor_adjustment(e));
    299300  wnoutrefresh(e->curswin);
    300   if (update==1) {
     301  if (update == 1) {
    301302    doupdate();
    302303  }
    303304  owl_free(buff);
     305}
     306
     307/* Remove n bytes at cursor. */
     308void _owl_editwin_remove_bytes(owl_editwin *e, int n) /*noproto*/
     309{
     310  int i = _owl_editwin_get_index_from_xy(e) + n;
     311  for (; i < e->bufflen; i++) {
     312    e->buff[i-n] = e->buff[i];
     313  }
     314 
     315  e->bufflen -= n;
     316  e->buff[e->bufflen] = '\0';
     317}
     318
     319/* Insert n bytes at cursor.*/
     320void _owl_editwin_insert_bytes(owl_editwin *e, int n) /*noproto*/
     321{
     322  int i, z;
     323 
     324  if ((e->bufflen + n) > (e->allocated - 5)) {
     325    _owl_editwin_addspace(e);
     326  }
     327
     328  e->bufflen += n;
     329  e->buff[e->bufflen] = '\0';
     330 
     331  z = _owl_editwin_get_index_from_xy(e);
     332  for (i = e->bufflen - 1; i > z; i--) {
     333    e->buff[i] = e->buff[i - n];
     334  }
    304335}
    305336
     
    313344  int i, z;
    314345
    315   z=_owl_editwin_get_index_from_xy(e);
     346  z = _owl_editwin_get_index_from_xy(e);
    316347  /* move back and line wrap the previous word */
    317   for (i=z-1; ; i--) {
     348  for (i = z - 1; ; i--) {
    318349    /* move back until you find a space or hit the beginning of the line */
    319     if (e->buff[i]==' ') {
     350    if (e->buff[i] == ' ') {
    320351      /* replace the space with a newline */
    321       e->buff[i]='\n';
     352      e->buff[i] = '\n';
    322353      e->buffy++;
    323       e->buffx=z-i-1;
     354      e->buffx = z - i - 1;
    324355      /* were we on the last line */
    325356      return(0);
    326     } else if (e->buff[i]=='\n' || i<=e->lock) {
    327       /* we hit the begginning of the line or the buffer, we cannot
     357    } else if (e->buff[i] == '\n' || i <= e->lock) {
     358      /* we hit the beginning of the line or the buffer, we cannot
    328359       * wrap.
    329360       */
     
    336367 * characters over)
    337368 */
    338 void owl_editwin_insert_char(owl_editwin *e, char c)
    339 {
    340  
    341   int z, i, ret;
     369void owl_editwin_insert_char(owl_editwin *e, gunichar c)
     370{
     371  int z, i, ret, len;
     372  char tmp[6];
     373  memset(tmp, '\0', 6);
    342374
    343375  /* \r is \n */
    344   if (c=='\r') {
    345     c='\n';
    346   }
    347 
    348   if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {
     376  if (c == '\r') {
     377    c = '\n';
     378  }
     379
     380  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
    349381    /* perhaps later this will change some state that allows the string
    350382       to be read */
     
    352384  }
    353385
     386  g_unichar_to_utf8(c, tmp);
     387  len = strlen(tmp);
     388
    354389  /* make sure there is enough memory for the new text */
    355   if ((e->bufflen+1) > (e->allocated-5)) {
     390  if ((e->bufflen + len) > (e->allocated - 5)) {
    356391    _owl_editwin_addspace(e);
    357392  }
    358393
    359394  /* get the insertion point */
    360   z=_owl_editwin_get_index_from_xy(e);
     395  z = _owl_editwin_get_index_from_xy(e);
    361396
    362397  /* If we're going to insert at the last column do word wrapping, unless it's a \n */
    363   if ((e->buffx+1==e->wrapcol) && (c!='\n')) {
    364     ret=_owl_editwin_linewrap_word(e);
    365     if (ret==-1) {
     398  if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) {
     399    ret = _owl_editwin_linewrap_word(e);
     400    if (ret == -1) {
    366401      /* we couldn't wrap, insert a hard newline instead */
    367402      owl_editwin_insert_char(e, '\n');
     
    369404  }
    370405
    371   z=_owl_editwin_get_index_from_xy(e);
    372406  /* shift all the other characters right */
    373   for (i=e->bufflen; i>z; i--) {
    374     e->buff[i]=e->buff[i-1];
    375   }
    376 
    377   /* insert the new one */
    378   e->buff[z]=c;
     407  if (z != e->bufflen) {
     408    _owl_editwin_insert_bytes(e, len);
     409  }
     410
     411  /* insert the new character */
     412  for(i = 0; i < len; i++) {
     413    e->buff[z + i] = tmp[i];
     414  }
    379415
    380416  /* housekeeping */
    381   e->bufflen++;
    382   e->buff[e->bufflen]='\0';
    383 
     417  e->bufflen += len;
     418  e->buff[e->bufflen] = '\0';
     419 
    384420  /* advance the cursor */
    385   if (c=='\n') {
    386     e->buffx=0;
    387     e->buffy++;
    388   } else {
    389     e->buffx++;
    390   }
     421  z += len;
     422  _owl_editwin_set_xy_by_index(e, z);
    391423}
    392424
    393425/* overwrite the character at the current point with 'c' */
    394 void owl_editwin_overwrite_char(owl_editwin *e, char c)
    395 {
    396   int z;
    397  
     426void owl_editwin_overwrite_char(owl_editwin *e, gunichar c)
     427{
     428  int z, oldlen, newlen, i;
     429  char tmp[6];
     430  memset(tmp, '\0', 6);
     431
    398432  /* \r is \n */
    399   if (c=='\r') {
    400     c='\n';
    401   }
    402 
    403   if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {
     433  if (c == '\r') {
     434    c = '\n';
     435  }
     436 
     437  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
    404438    /* perhaps later this will change some state that allows the string
    405439       to be read */
     
    407441  }
    408442
    409   z=_owl_editwin_get_index_from_xy(e);
    410 
    411   /* only if we are at the end of the buffer do we create new space */
    412   if (z==e->bufflen) {
    413     if ((e->bufflen+1) > (e->allocated-5)) {
     443  g_unichar_to_utf8(c, tmp);
     444  newlen = strlen(tmp);
     445
     446  z = _owl_editwin_get_index_from_xy(e);
     447  {
     448    char *t = g_utf8_find_next_char(e->buff + z, NULL);
     449    oldlen = (t ? (t - (e->buff + z)) : 0);
     450  }
     451
     452  /* only if we are at the end of the buffer do we create new space here */
     453  if (z == e->bufflen) {
     454    if ((e->bufflen+newlen) > (e->allocated-5)) {
    414455      _owl_editwin_addspace(e);
    415456    }
    416457  }
    417  
    418   e->buff[z]=c;
    419 
    420   /* housekeeping if we are at the end of the buffer */
    421   if (z==e->bufflen) {
    422     e->bufflen++;
    423     e->buff[e->bufflen]='\0';
    424   }
    425 
     458  /* if not at the end of the buffer, adjust based in char size difference. */
     459  else if (oldlen > newlen) {
     460    _owl_editwin_remove_bytes(e, oldlen-newlen);
     461  }
     462  else /* oldlen < newlen */ {
     463    _owl_editwin_insert_bytes(e, newlen-oldlen);
     464  }
     465  /* Overwrite the old char*/
     466  for (i = 0; i < newlen; i++) {
     467    e->buff[z+i] = tmp[i];
     468  }
     469       
     470  /* housekeeping */
     471  if (z == e->bufflen) {
     472    e->bufflen += newlen;
     473    e->buff[e->bufflen] = '\0';
     474  }
     475 
    426476  /* advance the cursor */
    427   if (c=='\n') {
    428     e->buffx=0;
    429     e->buffy++;
    430   } else {
    431     e->buffx++;
    432   }
    433 
     477  z += newlen;
     478  _owl_editwin_set_xy_by_index(e, z);
    434479}
    435480
     
    439484void owl_editwin_delete_char(owl_editwin *e)
    440485{
    441   int z, i;
    442 
    443   if (e->bufflen==0) return;
     486  int z;
     487  char *p1, *p2;
     488  gunichar c;
     489
     490  if (e->bufflen == 0) return;
    444491 
    445492  /* get the deletion point */
    446   z=_owl_editwin_get_index_from_xy(e);
    447 
    448   if (z==e->bufflen) return;
    449 
    450   for (i=z; i<e->bufflen; i++) {
    451     e->buff[i]=e->buff[i+1];
    452   }
    453   e->bufflen--;
    454   e->buff[e->bufflen]='\0';
     493  z = _owl_editwin_get_index_from_xy(e);
     494
     495  if (z == e->bufflen) return;
     496
     497  p1 = e->buff + z;
     498  p2 = g_utf8_next_char(p1);
     499  c = g_utf8_get_char(p2);
     500  while (g_unichar_ismark(c)) {
     501    p2 = g_utf8_next_char(p2);
     502    c = g_utf8_get_char(p2);
     503  }
     504  _owl_editwin_remove_bytes(e, p2-p1);
    455505}
    456506
     
    463513{
    464514  int z;
    465   char tmp;
    466 
    467   if (e->bufflen==0) return;
     515  char *p1, *p2, *p3, *tmp;
     516
     517  if (e->bufflen == 0) return;
    468518 
    469519  /* get the cursor point */
    470   z=_owl_editwin_get_index_from_xy(e);
    471 
    472   if (z==e->bufflen) {
     520  z = _owl_editwin_get_index_from_xy(e);
     521
     522  if (z == e->bufflen) {
    473523    /* point is after last character */
    474524    z--;
    475525  } 
    476526
    477   if (z-1 < e->lock) {
     527  if (z - 1 < e->lock) {
    478528    /* point is at beginning of buffer, do nothing */
    479529    return;
    480530  }
    481531
    482   tmp=e->buff[z];
    483   e->buff[z]=e->buff[z-1];
    484   e->buff[z-1]=tmp;
    485   owl_editwin_key_right(e);
     532  /* Transpose two utf-8 unicode glyphs. */
     533  p1 = e->buff + z;
     534
     535  p2 = g_utf8_find_next_char(p1, NULL);
     536  while (p2 != NULL && g_unichar_ismark(g_utf8_get_char(p2))) {
     537    p2 = g_utf8_find_next_char(p2, NULL);
     538  }
     539  if (p2 == NULL) return;
     540
     541  p3 = g_utf8_find_prev_char(e->buff, p1);
     542  while (p3 != NULL && g_unichar_ismark(g_utf8_get_char(p3))) {
     543    p3 = g_utf8_find_prev_char(p3, NULL);
     544  }
     545  if (p3 == NULL) return;
     546
     547  tmp = owl_malloc(p2 - p3 + 5);
     548  *tmp = '\0';
     549  strncat(tmp, p1, p2 - p1);
     550  strncat(tmp, p3, p1 - p3);
     551  strncpy(p3, tmp, p2 - p3);
     552  owl_free(tmp);
     553  _owl_editwin_set_xy_by_index(e, p3 - e->buff);
    486554}
    487555
     
    491559void owl_editwin_insert_string(owl_editwin *e, char *string)
    492560{
    493   int i, j;
    494 
    495   j=strlen(string);
    496   for (i=0; i<j; i++) {
    497     owl_editwin_insert_char(e, string[i]);
     561  char *p;
     562  gunichar c;
     563  if (!g_utf8_validate(string, -1, NULL)) {
     564    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
     565    return;
     566  }
     567  p = string;
     568  c = g_utf8_get_char(p);
     569  while (c) {
     570    _owl_editwin_process_char(e, c);
     571    p = g_utf8_next_char(p);
     572    c = g_utf8_get_char(p);
    498573  }
    499574}
     
    505580void owl_editwin_overwrite_string(owl_editwin *e, char *string)
    506581{
    507   int i, j;
    508 
    509   j=strlen(string);
    510   for (i=0; i<j; i++) {
    511     owl_editwin_overwrite_char(e, string[i]);
     582  char *p;
     583  gunichar c;
     584
     585  if (!g_utf8_validate(string, -1, NULL)) {
     586    owl_function_debugmsg("owl_editwin_overwrite_string: received non-utf-8 string.");
     587    return;
     588  }
     589  p = string;
     590  c = g_utf8_get_char(p);
     591  while (c) {
     592    owl_editwin_overwrite_char(e, c);
     593    p = g_utf8_next_char(p);
     594    c = g_utf8_get_char(p);
    512595  }
    513596}
     
    520603  int i;
    521604  char *ptr1, *ptr2;
    522 
    523   if (e->bufflen==0) return(0);
     605  gunichar c;
     606
     607  if (e->bufflen == 0) return(0);
    524608 
    525609  /* first go to the yth line */
    526   ptr1=e->buff;
    527   for (i=0; i<e->buffy; i++) {
    528     ptr2=strchr(ptr1, '\n');
     610  ptr1 = e->buff;
     611  for (i = 0; i < e->buffy; i++) {
     612    ptr2= strchr(ptr1, '\n');
    529613    if (!ptr2) {
    530614      /* we're already on the last line */
    531615      break;
    532616    }
    533     ptr1=ptr2+1;
    534   }
    535 
    536   /* now go to the xth character */
    537   ptr2=strchr(ptr1, '\n');
    538   if (!ptr2) {
    539     ptr2=e->buff+e->bufflen;
    540   }
    541 
    542   if ((ptr2-ptr1) < e->buffx) {
    543     ptr1=ptr2-1;
    544   } else {
    545     ptr1+=e->buffx;
    546   }
    547 
    548   /* printf("DEBUG: index is %i\r\n", ptr1-e->buff); */
    549   return(ptr1-e->buff);
     617    ptr1 = ptr2 + 1;
     618  }
     619
     620  /* now go to the xth cell */
     621  ptr2 = ptr1;
     622  i = 0;
     623  while (ptr2 != NULL && i < e->buffx && (ptr2 - e->buff) < e->bufflen) {
     624    c = g_utf8_get_char(ptr2);
     625    i += (c == '\n' ? 1 : mk_wcwidth(c));
     626    ptr2 = g_utf8_next_char(ptr2);
     627  }
     628  while(ptr2 != NULL && g_unichar_ismark(g_utf8_get_char(ptr2))) {
     629    ptr2 = g_utf8_next_char(ptr2);
     630  }
     631  if (ptr2 == NULL) return e->bufflen;
     632  return(ptr2 - e->buff);
    550633}
    551634
    552635void _owl_editwin_set_xy_by_index(owl_editwin *e, int index)
    553636{
    554   int z, i;
    555 
    556   z=_owl_editwin_get_index_from_xy(e);
    557   if (index>z) {
    558     for (i=0; i<index-z; i++) {
    559       owl_editwin_key_right(e);
    560     }
    561   } else if (index<z) {
    562     for (i=0; i<z-index; i++) {
    563       owl_editwin_key_left(e);
    564     }
    565   }
     637  char *ptr1, *ptr2, *target;
     638  gunichar c;
     639
     640  e->buffx = 0;
     641  e->buffy = 0;
     642
     643  ptr1 = e->buff;
     644  target = ptr1 + index;
     645  /* target sanitizing */
     646  if ((target[0] & 0x80) && (~target[0] & 0x40)) {
     647    /* middle of a utf-8 character, back up to previous character. */
     648    target = g_utf8_find_prev_char(e->buff, target);
     649  }
     650  c = g_utf8_get_char(target);
     651  while (g_unichar_ismark(c) && target > e->buff) {
     652    /* Adjust the target off of combining characters and the like. */
     653    target = g_utf8_find_prev_char(e->buff, target);
     654    c = g_utf8_get_char(target);
     655  }
     656  /* If we start with a mark, something is wrong.*/
     657  if (g_unichar_ismark(c)) return;
     658
     659  /* Now our target should be acceptable. */
     660  ptr2 = strchr(ptr1, '\n');
     661  while (ptr2 != NULL && ptr2 < target) {
     662    e->buffy++;
     663    ptr1 = ptr2 + 1;
     664    ptr2 = strchr(ptr1, '\n');
     665  }
     666  ptr2 = ptr1;
     667  while (ptr2 != NULL && ptr2 < target) {
     668    c = g_utf8_get_char(ptr2);
     669    e->buffx += mk_wcwidth(c);
     670    ptr2 = g_utf8_next_char(ptr2);
     671  }
     672}
     673
     674int _owl_editwin_cursor_adjustment(owl_editwin *e)
     675{
     676  char *ptr1, *ptr2;
     677  gunichar c;
     678  int x, i;
     679
     680  ptr1 = e->buff;
     681  ptr2 = strchr(ptr1, '\n');
     682  for (i = 0; ptr2 != NULL && i < e->buffy; i++) {
     683    ptr1 = ptr2 + 1;
     684    ptr2 = strchr(ptr1, '\n');
     685  }
     686  ptr2 = ptr1;
     687  x = 0;
     688  while (ptr2 != NULL && x < e->buffx) {
     689    if (*ptr2 == '\n') return 0;
     690    c = g_utf8_get_char(ptr2);
     691    x += mk_wcwidth(c);
     692    ptr2 = g_utf8_next_char(ptr2);
     693  }
     694  return x - e->buffx;
    566695}
    567696
     
    570699  /* if we happen to have the cursor over locked text
    571700   * move it to be out of the locktext region */
    572   if (_owl_editwin_get_index_from_xy(e)<e->lock) {
     701  if (_owl_editwin_get_index_from_xy(e) < e->lock) {
    573702    _owl_editwin_set_xy_by_index(e, e->lock);
    574703  }
     
    590719{
    591720  if (e->buffy > 0) e->buffy--;
    592   if (e->buffx >= owl_editwin_get_numchars_on_line(e, e->buffy)) {
    593     e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     721  if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) {
     722    e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    594723  }
    595724
     
    608737
    609738  /* if we're past the last character move back */
    610   if (e->buffx >= owl_editwin_get_numchars_on_line(e, e->buffy)) {
    611     e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     739  if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) {
     740    e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    612741  }
    613742
     
    623752void owl_editwin_key_left(owl_editwin *e)
    624753{
    625   /* move left if we can, and maybe up a line */
    626   if (e->buffx>0) {
    627     e->buffx--;
    628   } else if (e->buffy>0) {
    629     e->buffy--;
    630     e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
    631   }
    632 
    633   /* do we need to scroll up? */
    634   if (e->buffy-e->topline < 0) {
    635     e->topline-=e->winlines/2;
     754  int i;
     755  char * p;
     756  i = _owl_editwin_get_index_from_xy(e);
     757  p = e->buff + i;
     758  p = g_utf8_find_prev_char(e->buff, p);
     759  while (p && g_unichar_ismark(g_utf8_get_char(p))) {
     760    p = g_utf8_find_prev_char(e->buff, p);
     761  }
     762  if (p == NULL) p = e->buff;
     763  _owl_editwin_set_xy_by_index(e, p - e->buff);
     764
     765  if (e->buffy - e->topline < 0) {
     766    e->topline -= e->winlines / 2;
    636767  }
    637768
     
    643774{
    644775  int i;
    645 
    646   /* move right if we can, and skip down a line if needed */
    647   i=owl_editwin_get_numchars_on_line(e, e->buffy);
    648   if (e->buffx < i) {
    649     e->buffx++;
    650     /*  } else if (e->buffy+1 < owl_editwin_get_numlines(e)) { */
    651   } else if (_owl_editwin_get_index_from_xy(e) < e->bufflen) {
    652     if (e->style==OWL_EDITWIN_STYLE_MULTILINE) {
    653       e->buffx=0;
    654       e->buffy++;
    655     }
     776  char * p;
     777  i = _owl_editwin_get_index_from_xy(e);
     778  p = e->buff + i;
     779  p = g_utf8_find_next_char(p, NULL);
     780  while (p && g_unichar_ismark(g_utf8_get_char(p))) {
     781    p = g_utf8_find_next_char(p, NULL);
     782  }
     783  if (p == NULL) {
     784    _owl_editwin_set_xy_by_index(e, e->bufflen);
     785  }
     786  else {
     787    _owl_editwin_set_xy_by_index(e, p - e->buff);
    656788  }
    657789
    658790  /* do we need to scroll down? */
    659   if (e->buffy-e->topline >= e->winlines) {
    660     e->topline+=e->winlines/2;
     791  if (e->buffy - e->topline >= e->winlines) {
     792    e->topline += e->winlines / 2;
    661793  }
    662794}
     
    664796void owl_editwin_move_to_nextword(owl_editwin *e)
    665797{
     798  /* asedeno: needs fixing for utf-8*/
    666799  int i, x;
    667800
     
    703836void owl_editwin_move_to_previousword(owl_editwin *e)
    704837{
     838  /* asedeno: needs fixing for utf-8*/
    705839  int i, x;
    706840
     
    738872void owl_editwin_delete_nextword(owl_editwin *e)
    739873{
     874  /* asedeno: needs fixing for utf-8*/
    740875  int z;
    741876
     
    768903void owl_editwin_delete_previousword(owl_editwin *e)
    769904{
     905  /* asedeno: needs fixing for utf-8*/
    770906  /* go backwards to the last non-space character, then delete chars */
    771907  int i, startpos, endpos;
     
    781917void owl_editwin_delete_to_endofline(owl_editwin *e)
    782918{
     919  /* asedeno: needs fixing for utf-8*/
    783920  int i;
    784921
    785   if (owl_editwin_get_numchars_on_line(e, e->buffy)>e->buffx) {
     922  if (owl_editwin_get_numchars_on_line(e, e->buffy) > e->buffx) {
    786923    /* normal line */
    787924    i=_owl_editwin_get_index_from_xy(e);
     
    804941void owl_editwin_move_to_line_end(owl_editwin *e)
    805942{
    806   e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     943  e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    807944}
    808945
     
    817954  /* go to last char */
    818955  e->buffy=owl_editwin_get_numlines(e)-1;
    819   e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
     956  e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
    820957  owl_editwin_key_right(e);
    821958
     
    841978void owl_editwin_fill_paragraph(owl_editwin *e)
    842979{
     980  /* asedeno: needs fixing for utf-8*/
    843981  int i, save;
    844982
     
    857995  /* main loop */
    858996  while (1) {
    859     i=_owl_editwin_get_index_from_xy(e);
     997    i = _owl_editwin_get_index_from_xy(e);
    860998
    861999    /* bail if we hit the end of the buffer */
    862     if (i>=e->bufflen) break;
     1000    if (i >= e->bufflen || e->buff[i] == '\0') break;
    8631001
    8641002    /* bail if we hit the end of the paragraph */
    865     if (e->buff[i]=='\n' && e->buff[i+1]=='\n') break;
     1003    if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break;
    8661004
    8671005    /* if we've travelled too far, linewrap */
     
    8711009
    8721010    /* did we hit the end of a line too soon? */
    873     i=_owl_editwin_get_index_from_xy(e);
    874     if (e->buff[i]=='\n' && e->buffx<e->fillcol-1) {
     1011    i = _owl_editwin_get_index_from_xy(e);
     1012    if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) {
    8751013      /* ********* we need to make sure we don't pull in a word that's too long ***********/
    8761014      e->buff[i]=' ';
     
    8781016   
    8791017    /* fix spacing */
    880     i=_owl_editwin_get_index_from_xy(e);
    881     if (e->buff[i]==' ' && e->buff[i+1]==' ') {
    882       if (e->buff[i-1]=='.' || e->buff[i-1]=='!' || e->buff[i-1]=='?') {
     1018    i = _owl_editwin_get_index_from_xy(e);
     1019    if (e->buff[i] == ' ' && e->buff[i+1] == ' ') {
     1020      if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') {
    8831021        owl_editwin_key_right(e);
    8841022      } else {
    8851023        owl_editwin_delete_char(e);
    8861024        /* if we did this ahead of the save point, adjust it */
    887         if (i<save) save--;
     1025        if (i < save) save--;
    8881026      }
    8891027    } else {
     
    9111049int owl_editwin_check_dotsend(owl_editwin *e)
    9121050{
    913   int i;
     1051  char *p, *p_n, *p_p;
     1052  gunichar c;
    9141053
    9151054  if (!e->dotsend) return(0);
    916   for (i=e->bufflen-1; i>0; i--) {
    917     if (e->buff[i] == '.'
    918         && (e->buff[i-1] == '\n' || e->buff[i-1] == '\r')
    919         && (e->buff[i+1] == '\n' || e->buff[i+1] == '\r')) {
    920       e->bufflen = i;
    921       e->buff[i] = '\0';
     1055
     1056  p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen);
     1057  p_n = g_utf8_find_next_char(p, NULL);
     1058  p_p = g_utf8_find_prev_char(e->buff, p);
     1059  c = g_utf8_get_char(p);
     1060  while (p != NULL) {
     1061    if (*p == '.'
     1062        && p_p != NULL && (*p_p == '\n' || *p_p == '\r')
     1063        && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) {
     1064      e->bufflen = p - e->buff;
     1065      e->buff[e->bufflen] = '\0';
    9221066      return(1);
    9231067    }
    924     if (!isspace((int) e->buff[i])) {
    925       return(0);
    926     }
     1068    if (c != '\0' && !g_unichar_isspace(c)) return(0);
     1069    p_n = p;
     1070    p = p_p;
     1071    c = g_utf8_get_char(p);
     1072    p_p = g_utf8_find_prev_char(e->buff, p);
    9271073  }
    9281074  return(0);
    9291075}
    9301076
    931 void owl_editwin_post_process_char(owl_editwin *e, int j)
     1077void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
    9321078{
    9331079  /* check if we need to scroll down */
     
    9351081    e->topline+=e->winlines/2;
    9361082  }
    937   if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) {
     1083  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
    9381084    owl_command_editmulti_done(e);
    9391085    return;
     
    9421088}
    9431089
    944 void owl_editwin_process_char(owl_editwin *e, int j)
    945 {
    946   if (j == ERR) return;
    947   if (j>127 || ((j<32) && (j!=10) && (j!=13))) {
    948     return;
    949   } else {
     1090void _owl_editwin_process_char(owl_editwin *e, gunichar j)
     1091{
     1092  if (!(g_unichar_iscntrl(j) && (j != 10) && (j != 13))) {
    9501093    owl_editwin_insert_char(e, j);
     1094  }
     1095}
     1096
     1097
     1098void owl_editwin_process_char(owl_editwin *e, owl_input j)
     1099{
     1100  if (j.ch == ERR) return;
     1101  /* Ignore ncurses control characters. */
     1102  if (j.ch < 0x100) {
     1103    _owl_editwin_process_char(e, j.uch);
    9511104  }
    9521105}
     
    9751128  }
    9761129
    977   /* now go to the xth character */
    978   ptr2=strchr(ptr1, '\n');
    979   if (!ptr2) {
    980     return(e->buff + e->bufflen - ptr1);
    981   }
    982   return(ptr2-ptr1); /* don't count the newline for now */
     1130  /* now count characters */
     1131  i = 0;
     1132  ptr2 = ptr1;
     1133  while (ptr2 - e->buff < e->bufflen
     1134         && *ptr2 != '\n') {
     1135    ++i;
     1136    ptr2 = g_utf8_next_char(ptr2);
     1137  }
     1138  return i;
     1139}
     1140
     1141int owl_editwin_get_numcells_on_line(owl_editwin *e, int line)
     1142{
     1143  int i;
     1144  char *ptr1, *ptr2;
     1145  gunichar c;
     1146
     1147  if (e->bufflen==0) return(0);
     1148 
     1149  /* first go to the yth line */
     1150  ptr1=e->buff;
     1151  for (i=0; i<line; i++) {
     1152    ptr2=strchr(ptr1, '\n');
     1153    if (!ptr2) {
     1154      /* we're already on the last line */
     1155      return(0);
     1156    }
     1157    ptr1=ptr2+1;
     1158  }
     1159
     1160  /* now count cells */
     1161  i = 0;
     1162  ptr2 = ptr1;
     1163  while (ptr2 - e->buff < e->bufflen
     1164         && *ptr2 != '\n') {
     1165    c = g_utf8_get_char(ptr2);
     1166    i += mk_wcwidth(c);
     1167    ptr2 = g_utf8_next_char(ptr2);
     1168  }
     1169  return i;
    9831170}
    9841171
Note: See TracChangeset for help on using the changeset viewer.