Changeset 4815

Show
Ignore:
Timestamp:
04/25/09 18:58:16 (11 months ago)
Author:
darkjames
Message:

rewrite binding_complete() and close #25

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/plugins/ncurses/bindings.c

    r4746 r4815  
    364364        if (!lines) { 
    365365#if USE_UNICODE 
    366                 /* PLEASE REPORT ALL BUGS CONNECTED WITH THIS CODE TO <darkjames@darkjames.ath.cx> THX. */ 
    367                         char *nline = xmalloc((LINE_MAXLEN+1) /* * MB_CUR_MAX */); 
    368                         size_t len, i, cnt; 
    369                         int lns_start, lns_index;  
    370  
    371                         if ((wcstombs(nline, (wchar_t *) line, LINE_MAXLEN) == -1)) { 
    372                                 debug("[%s:%d] wcstombs() failed.\n"); 
    373                                 xfree(nline); 
    374                                 return; 
    375                         } 
    376                         ncurses_complete(&line_start, &line_index, nline); 
    377  
    378                 /* here we need to update line_start && line_index cause of unicode chars.. */ 
    379                         len = xstrlen(nline);           i = 0;          cnt = 0; 
    380                         lns_start = 0; lns_index = 0; 
    381  
    382 /*                      debug("line_start: %d line_index: %d len: %d\n", line_start, line_index, len);*/ 
    383                         while (1) { 
    384                                 int tmp = mblen(&nline[i], len-i); 
    385 /*                              debug("[%d] cur: %d nextlen: %d\n", cnt, i, tmp);*/ 
    386                                 if (!lns_start && line_start == i) { lns_start = 1; line_start = cnt; } 
    387                                 if (!lns_index && line_index == i) { lns_index = 1; line_index = cnt; }  
    388                                 cnt++; 
    389                                 i += tmp; 
    390                                 if (lns_start && lns_index) break; 
    391                                 if (tmp <= 0) break; 
    392                         } 
    393 /*                      debug("lns_start: %d lns_index: %d (%d,%d)\n", lns_start, lns_index, line_start, line_index);*/ 
    394  
    395                         if (!lns_start) line_start = 0; 
    396                         if (!lns_index) line_index = 0; 
    397  
    398                         if ((mbstowcs(line, nline, LINE_MAXLEN) == -1)) /* if it's failed the result will be unpredictable \o/ */ 
    399                                 debug("[%s:%d] mbstowcs() failed.\n"); 
    400                         xfree(nline); 
     366                        int line_start_tmp, line_index_tmp; 
     367                        char nline[LINE_MAXLEN + 1];    /* (* MB_CUR_MAX)? No, it would be anyway truncated by completion */ 
     368                        int i, j; 
     369                        int nlen; 
     370 
     371                        line_start_tmp = line_index_tmp = 0; 
     372                        for (i = 0, j = 0; line[i] && i < LINE_MAXLEN; i++) { 
     373                                char buf[MB_CUR_MAX+1]; 
     374                                int tmp; 
     375                                int k; 
     376 
     377                                tmp = wctomb(buf, line[i]); 
     378 
     379                                if (tmp <= 0 || tmp >= MB_CUR_MAX) { 
     380                                        debug_error("binding_complete() wctomb() failed (%d)\n", tmp); 
     381                                        return; 
     382                                } 
     383 
     384                                if (j+tmp >= LINE_MAXLEN) { 
     385                                        debug_error("binding_complete() buffer might be truncated, aborting\n"); 
     386                                        return; 
     387                                } 
     388 
     389                                if (line_start == i) 
     390                                        line_start_tmp = j; 
     391                                if (line_index == i) 
     392                                        line_index_tmp = j; 
     393 
     394                                for (k = 0; k < tmp && buf[k]; k++) 
     395                                        nline[j++] = buf[k]; 
     396                        } 
     397                        /* XXX, put into loop, wcslen()+1? */ 
     398                        if (line_start == i) 
     399                                line_start_tmp = j; 
     400                        if (line_index == i) 
     401                                line_index_tmp = j; 
     402 
     403                        nline[j] = '\0'; 
     404 
     405                        debug("wcs-completion WC->MB (%d,%d) => (%d,%d) [%d;%d]\n", line_start, line_index, line_start_tmp, line_index_tmp, j, i); 
     406                        ncurses_complete(&line_start_tmp, &line_index_tmp, nline); 
     407 
     408                        nlen = strlen(nline); 
     409 
     410                        line_start = line_index = 0; 
     411                        for (i = 0, j = 0; j < nlen; i++) { 
     412                                int tmp; 
     413 
     414                                tmp = mbtowc(&line[i], &nline[j], nlen-j); 
     415 
     416                                if (tmp <= 0) { 
     417                                        debug_error("binding_complete() mbtowc() failed (%d)\n", tmp); 
     418                                        break;  /* return; */ 
     419                                } 
     420 
     421                                if (line_start_tmp == j) 
     422                                        line_start = i; 
     423                                if (line_index_tmp == j) 
     424                                        line_index = i; 
     425 
     426                                j += tmp; 
     427                        } 
     428 
     429                        /* XXX, put into loop, <= nlen? */ 
     430                        if (line_start_tmp == j) 
     431                                line_start = i; 
     432                        if (line_index_tmp == j) 
     433                                line_index = i; 
     434 
     435                        debug("wcs-completion MB->WC (%d,%d) => (%d,%d) [%d;%d]\n", line_start_tmp, line_index_tmp, line_start, line_index, j, i); 
     436                        line[i] = '\0'; 
    401437#else 
    402438                        ncurses_complete(&line_start, &line_index, (char *) line);