Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • editwin.c

    rb2c1bd4 rcc6f009  
    143143  e->buffy=0;
    144144  owl_editwin_overwrite_string(e, text);
    145   owl_editwin_overwrite_char(e, '\0');
    146145  e->lock=strlen(text);
    147146  /* if (text[e->lock-1]=='\n') e->lock--; */
    148   /*  e->buffx=x; */
    149   /*  e->buffy=y; */
    150   _owl_editwin_set_xy_by_index(e, e->lock);
     147  e->buffx=x;
     148  e->buffy=y;
     149  owl_editwin_adjust_for_locktext(e);
    151150  owl_editwin_redisplay(e, 0);
    152151}
     
    256255
    257256  /* start at topline */
    258   ptr1 = e->buff;
    259   for (i = 0; i < e->topline; i++) {
    260     ptr2 = strchr(ptr1, '\n');
     257  ptr1=e->buff;
     258  for (i=0; i<e->topline; i++) {
     259    ptr2=strchr(ptr1, '\n');
    261260    if (!ptr2) {
    262261      /* we're already on the last line */
    263262      break;
    264263    }
    265     ptr1 = ptr2 + 1;
     264    ptr1=ptr2+1;
    266265  }
    267266  /* ptr1 now stores the starting point */
    268267
    269268  /* find the ending point and store it in ptr3 */
    270   ptr2 = ptr1;
    271   ptr3 = ptr1;
    272   for (i = 0; i < e->winlines; i++) {
    273     ptr3 = strchr(ptr2, '\n');
     269  ptr2=ptr1;
     270  ptr3=ptr1;
     271  for (i=0; i<e->winlines; i++) {
     272    ptr3=strchr(ptr2, '\n');
    274273    if (!ptr3) {
    275274      /* we've hit the last line */
    276275      /* print everything to the end */
    277       ptr3 = e->buff + e->bufflen - 1;
     276      ptr3=e->buff+e->bufflen-1;
    278277      ptr3--;
    279278      break;
    280279    }
    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') {
     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') {
    289288    waddstr(e->curswin, buff);
    290289  } else {
    291290    /* translate to echochar, *except* for the locktext */
    292291    int len;
    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++) {
     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++) {
    305299      waddch(e->curswin, e->echochar);
    306300    }
    307301  }
    308   wmove(e->curswin, e->buffy-e->topline, e->buffx + _owl_editwin_cursor_adjustment(e));
     302  wmove(e->curswin, e->buffy-e->topline, e->buffx);
    309303  wnoutrefresh(e->curswin);
    310   if (update == 1) {
     304  if (update==1) {
    311305    doupdate();
    312306  }
    313307  owl_free(buff);
    314 }
    315 
    316 /* Remove n bytes at cursor. */
    317 void _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.*/
    329 void _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   }
    344308}
    345309
     
    351315int _owl_editwin_linewrap_word(owl_editwin *e)
    352316{
    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;
     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  }
    382337}
    383338
     
    385340 * characters over)
    386341 */
    387 void 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);
     342void owl_editwin_insert_char(owl_editwin *e, char c)
     343{
     344 
     345  int z, i, ret;
    392346
    393347  /* \r is \n */
    394   if (c == '\r') {
    395     c = '\n';
    396   }
    397 
    398   if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
     348  if (c=='\r') {
     349    c='\n';
     350  }
     351
     352  if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {
    399353    /* perhaps later this will change some state that allows the string
    400354       to be read */
     
    402356  }
    403357
    404   g_unichar_to_utf8(c, tmp);
    405   len = strlen(tmp);
    406 
    407358  /* make sure there is enough memory for the new text */
    408   if ((e->bufflen + len) > (e->allocated - 5)) {
     359  if ((e->bufflen+1) > (e->allocated-5)) {
    409360    _owl_editwin_addspace(e);
    410361  }
    411362
    412363  /* get the insertion point */
    413   z = _owl_editwin_get_index_from_xy(e);
     364  z=_owl_editwin_get_index_from_xy(e);
    414365
    415366  /* If we're going to insert at the last column do word wrapping, unless it's a \n */
    416   if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) {
    417     ret = _owl_editwin_linewrap_word(e);
    418     if (ret == -1) {
     367  if ((e->buffx+1==e->wrapcol) && (c!='\n')) {
     368    ret=_owl_editwin_linewrap_word(e);
     369    if (ret==-1) {
    419370      /* we couldn't wrap, insert a hard newline instead */
    420371      owl_editwin_insert_char(e, '\n');
     
    422373  }
    423374
     375  z=_owl_editwin_get_index_from_xy(e);
    424376  /* shift all the other characters right */
    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   }
     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;
    433383
    434384  /* housekeeping */
    435   e->bufflen += len;
    436   e->buff[e->bufflen] = '\0';
     385  e->bufflen++;
     386  e->buff[e->bufflen]='\0';
     387
     388  /* advance the cursor */
     389  if (c=='\n') {
     390    e->buffx=0;
     391    e->buffy++;
     392  } else {
     393    e->buffx++;
     394  }
     395}
     396
     397/* overwrite the character at the current point with 'c' */
     398void owl_editwin_overwrite_char(owl_editwin *e, char c)
     399{
     400  int z;
    437401 
    438   /* advance the cursor */
    439   z += len;
    440   _owl_editwin_set_xy_by_index(e, z);
    441 }
    442 
    443 /* overwrite the character at the current point with 'c' */
    444 void 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 
    450402  /* \r is \n */
    451   if (c == '\r') {
    452     c = '\n';
    453   }
    454  
    455   if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
     403  if (c=='\r') {
     404    c='\n';
     405  }
     406
     407  if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {
    456408    /* perhaps later this will change some state that allows the string
    457409       to be read */
     
    459411  }
    460412
    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)) {
     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)) {
    473418      _owl_editwin_addspace(e);
    474419    }
    475420  }
    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   }
    493421 
     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
    494430  /* advance the cursor */
    495   z += newlen;
    496   _owl_editwin_set_xy_by_index(e, z);
     431  if (c=='\n') {
     432    e->buffx=0;
     433    e->buffy++;
     434  } else {
     435    e->buffx++;
     436  }
     437
    497438}
    498439
     
    502443void owl_editwin_delete_char(owl_editwin *e)
    503444{
    504   int z;
    505   char *p1, *p2;
    506   gunichar c;
    507 
    508   if (e->bufflen == 0) return;
     445  int z, i;
     446
     447  if (e->bufflen==0) return;
    509448 
    510449  /* get the deletion point */
    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);
     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';
    523459}
    524460
     
    531467{
    532468  int z;
    533   char *p1, *p2, *p3, *tmp;
    534 
    535   if (e->bufflen == 0) return;
     469  char tmp;
     470
     471  if (e->bufflen==0) return;
    536472 
    537473  /* get the cursor point */
    538   z = _owl_editwin_get_index_from_xy(e);
    539 
    540   if (z == e->bufflen) {
     474  z=_owl_editwin_get_index_from_xy(e);
     475
     476  if (z==e->bufflen) {
    541477    /* point is after last character */
    542478    z--;
    543479  } 
    544480
    545   if (z - 1 < e->lock) {
     481  if (z-1 < e->lock) {
    546482    /* point is at beginning of buffer, do nothing */
    547483    return;
    548484  }
    549485
    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);
     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);
    572490}
    573491
     
    577495void owl_editwin_insert_string(owl_editwin *e, char *string)
    578496{
    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);
     497  int i, j;
     498
     499  j=strlen(string);
     500  for (i=0; i<j; i++) {
     501    owl_editwin_insert_char(e, string[i]);
    591502  }
    592503}
     
    598509void owl_editwin_overwrite_string(owl_editwin *e, char *string)
    599510{
    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);
     511  int i, j;
     512
     513  j=strlen(string);
     514  for (i=0; i<j; i++) {
     515    owl_editwin_overwrite_char(e, string[i]);
    613516  }
    614517}
     
    621524  int i;
    622525  char *ptr1, *ptr2;
    623   gunichar c;
    624 
    625   if (e->bufflen == 0) return(0);
     526
     527  if (e->bufflen==0) return(0);
    626528 
    627529  /* first go to the yth line */
    628   ptr1 = e->buff;
    629   for (i = 0; i < e->buffy; i++) {
    630     ptr2= strchr(ptr1, '\n');
     530  ptr1=e->buff;
     531  for (i=0; i<e->buffy; i++) {
     532    ptr2=strchr(ptr1, '\n');
    631533    if (!ptr2) {
    632534      /* we're already on the last line */
    633535      break;
    634536    }
    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 */
    654 gunichar _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 
     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}
    659555
    660556void _owl_editwin_set_xy_by_index(owl_editwin *e, int index)
    661557{
    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 
    699 int _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;
     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  }
    725570}
    726571
     
    729574  /* if we happen to have the cursor over locked text
    730575   * move it to be out of the locktext region */
    731   if (_owl_editwin_get_index_from_xy(e) < e->lock) {
     576  if (_owl_editwin_get_index_from_xy(e)<e->lock) {
    732577    _owl_editwin_set_xy_by_index(e, e->lock);
    733578  }
     
    749594{
    750595  if (e->buffy > 0) 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);
     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);
    753598  }
    754599
     
    767612
    768613  /* if we're past the last character move back */
    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);
     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);
    771616  }
    772617
     
    782627void owl_editwin_key_left(owl_editwin *e)
    783628{
    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;
     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;
    797640  }
    798641
     
    804647{
    805648  int i;
    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);
     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    }
    818660  }
    819661
    820662  /* do we need to scroll down? */
    821   if (e->buffy - e->topline >= e->winlines) {
    822     e->topline += e->winlines / 2;
     663  if (e->buffy-e->topline >= e->winlines) {
     664    e->topline+=e->winlines/2;
    823665  }
    824666}
     
    827669{
    828670  int i, x;
    829   gunichar c = '\0';
    830671
    831672  /* if we're starting on a space, find the first non-space */
     
    840681  }
    841682
    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);
     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);
    846686  if (e->buffx < i) {
    847687    /* move right till end of line */
    848688    while (e->buffx < i) {
    849       owl_editwin_key_right(e);
    850       c = _owl_editwin_get_char_at_xy(e);
    851       if (c == ' ') return;
     689      e->buffx++;
     690      if (e->buff[_owl_editwin_get_index_from_xy(e)]==' ') return;
    852691      if (e->buffx == i) return;
    853692    }
     
    855694    /* try to move down */
    856695    if (e->style==OWL_EDITWIN_STYLE_MULTILINE) {
    857       if (e->buffy+1 < owl_editwin_get_numlines(e)) {
     696      if (e->buffy+1 <  owl_editwin_get_numlines(e)) {
    858697        e->buffx=0;
    859698        e->buffy++;
     
    868707void owl_editwin_move_to_previousword(owl_editwin *e)
    869708{
    870   int i;
    871   gunichar c;
    872   char *ptr1, *ptr2;
     709  int i, x;
    873710
    874711  /* are we already at the beginning of the word? */
    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 
     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') ) {
     715    owl_editwin_key_left(e);
     716  }
     717   
    886718  /* 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') {
     719  i=_owl_editwin_get_index_from_xy(e);
     720  if (e->buff[i]==' ' || e->buff[i]=='\n' || e->buff[i]=='\0') {
    889721    /* find the first non-space */
    890     owl_editwin_key_left(e);     
    891     i = _owl_editwin_get_index_from_xy(e);
     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    }
    892728  }
    893729
    894730  /* 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'){
     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);
    903735      break;
    904736    }
    905     owl_editwin_key_left(e);
    906     ptr1 = e->buff + _owl_editwin_get_index_from_xy(e);
    907   }
     737  }
     738  _owl_editwin_set_xy_by_index(e, x);
    908739}
    909740
     
    911742void owl_editwin_delete_nextword(owl_editwin *e)
    912743{
    913   char *ptr1, *start;
    914   gunichar c;
     744  int z;
    915745
    916746  if (e->bufflen==0) return;
    917747
    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;
     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);
    945770}
    946771
     
    948773{
    949774  /* go backwards to the last non-space character, then delete chars */
    950   int startpos, endpos;
     775  int i, startpos, endpos;
    951776
    952777  startpos = _owl_editwin_get_index_from_xy(e);
    953778  owl_editwin_move_to_previousword(e);
    954779  endpos = _owl_editwin_get_index_from_xy(e);
    955   _owl_editwin_remove_bytes(e, startpos-endpos);
     780  for (i=0; i<startpos-endpos; i++) {
     781    owl_editwin_delete_char(e);
     782  }
    956783}
    957784
     
    960787  int i;
    961788
    962   if (owl_editwin_get_numchars_on_line(e, e->buffy) > e->buffx) {
     789  if (owl_editwin_get_numchars_on_line(e, e->buffy)>e->buffx) {
    963790    /* normal line */
    964791    i=_owl_editwin_get_index_from_xy(e);
     
    981808void owl_editwin_move_to_line_end(owl_editwin *e)
    982809{
    983   e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
     810  e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
    984811}
    985812
     
    994821  /* go to last char */
    995822  e->buffy=owl_editwin_get_numlines(e)-1;
    996   e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
     823  e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy);
    997824  owl_editwin_key_right(e);
    998825
     
    1034861  /* main loop */
    1035862  while (1) {
    1036     i = _owl_editwin_get_index_from_xy(e);
     863    i=_owl_editwin_get_index_from_xy(e);
    1037864
    1038865    /* bail if we hit the end of the buffer */
    1039     if (i >= e->bufflen || e->buff[i] == '\0') break;
     866    if (i>=e->bufflen) break;
    1040867
    1041868    /* bail if we hit the end of the paragraph */
    1042     if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break;
     869    if (e->buff[i]=='\n' && e->buff[i+1]=='\n') break;
    1043870
    1044871    /* if we've travelled too far, linewrap */
    1045872    if ((e->buffx) >= e->fillcol) {
    1046       int len = e->bufflen;
    1047873      _owl_editwin_linewrap_word(e);
    1048       /* we may have added a character. */
    1049       if (i < save) save += e->bufflen - len;
    1050874    }
    1051875
    1052876    /* did we hit the end of a line too soon? */
    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) {
     877    i=_owl_editwin_get_index_from_xy(e);
     878    if (e->buff[i]=='\n' && e->buffx<e->fillcol-1) {
    1058879      /* ********* we need to make sure we don't pull in a word that's too long ***********/
    1059880      e->buff[i]=' ';
    1060881    }
    1061 
     882   
    1062883    /* fix spacing */
    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] == '?') {
     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]=='?') {
    1066887        owl_editwin_key_right(e);
    1067888      } else {
    1068889        owl_editwin_delete_char(e);
    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--;
     890        /* if we did this ahead of the save point, adjust it */
     891        if (i<save) save--;
    1073892      }
    1074893    } else {
    1075894      owl_editwin_key_right(e);
    1076895    }
     896
    1077897  }
    1078898
     
    1095915int owl_editwin_check_dotsend(owl_editwin *e)
    1096916{
    1097   char *p, *p_n, *p_p;
    1098   gunichar c;
     917  int i;
    1099918
    1100919  if (!e->dotsend) return(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';
     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';
    1112926      return(1);
    1113927    }
    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);
     928    if (!isspace((int) e->buff[i])) {
     929      return(0);
     930    }
    1119931  }
    1120932  return(0);
    1121933}
    1122934
    1123 void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
     935void owl_editwin_post_process_char(owl_editwin *e, int j)
    1124936{
    1125937  /* check if we need to scroll down */
     
    1127939    e->topline+=e->winlines/2;
    1128940  }
    1129   if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
     941  if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) {
    1130942    owl_command_editmulti_done(e);
    1131943    return;
     
    1134946}
    1135947
    1136 void _owl_editwin_process_char(owl_editwin *e, gunichar j)
    1137 {
    1138   if (!(g_unichar_iscntrl(j) && (j != 10) && (j != 13))) {
     948void 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 {
    1139954    owl_editwin_insert_char(e, j);
    1140   }
    1141 }
    1142 
    1143 
    1144 void 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);
    1150955  }
    1151956}
     
    1174979  }
    1175980
    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 
    1187 int 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;
     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 */
    1216987}
    1217988
Note: See TracChangeset for help on using the changeset viewer.