Changeset 4815

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