Changeset 2b83ad6


Ignore:
Timestamp:
Jul 31, 2010, 7:12:15 PM (14 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.7, release-1.8, release-1.9
Children:
30da473
Parents:
c93b8b5
git-author:
David Benjamin <davidben@mit.edu> (07/25/10 18:36:54)
git-committer:
David Benjamin <davidben@mit.edu> (07/31/10 19:12:15)
Message:
Add owl_fmtext_expand_tabs with test case
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • fmtext.c

    rc93b8b5 r2b83ad6  
    370370{
    371371  _owl_fmtext_curs_waddstr(f, w, 0);
     372}
     373
     374/* Expands tabs. Tabs are expanded as if given an initial indent of start. */
     375void owl_fmtext_expand_tabs(const owl_fmtext *in, owl_fmtext *out, int start) {
     376  int col = start, numcopied = 0;
     377  char *ptr;
     378
     379  /* Copy the default attributes. */
     380  out->default_attrs = in->default_attrs;
     381  out->default_fgcolor = in->default_fgcolor;
     382  out->default_bgcolor = in->default_bgcolor;
     383
     384  for (ptr = in->textbuff;
     385       ptr < in->textbuff + in->textlen;
     386       ptr = g_utf8_next_char(ptr)) {
     387    gunichar c = g_utf8_get_char(ptr);
     388    int chwidth;
     389    if (c == '\t') {
     390      /* Copy up to this tab */
     391      _owl_fmtext_append_fmtext(out, in, numcopied, ptr - in->textbuff - 1);
     392      /* and then copy spaces for the tab. */
     393      chwidth = OWL_TAB_WIDTH - (col % OWL_TAB_WIDTH);
     394      owl_fmtext_append_spaces(out, chwidth);
     395      col += chwidth;
     396      numcopied = g_utf8_next_char(ptr) - in->textbuff;
     397    } else {
     398      /* Just update col. We'll append later. */
     399      if (c == '\n') {
     400        col = start;
     401      } else if (!owl_fmtext_is_format_char(c)) {
     402        col += mk_wcwidth(c);
     403      }
     404    }
     405  }
     406  /* Append anything we've missed. */
     407  if (numcopied < in->textlen)
     408    _owl_fmtext_append_fmtext(out, in, numcopied, in->textlen - 1);
    372409}
    373410
  • owl.h

    re488ec5 r2b83ad6  
    113113#define OWL_COLOR_DEFAULT   -1
    114114#define OWL_COLOR_INVALID   -2
     115
     116#define OWL_TAB_WIDTH 8
    115117
    116118#define OWL_EDITWIN_STYLE_MULTILINE 0
  • tester.c

    r6772d19 r2b83ad6  
    470470  owl_free(str);
    471471
     472  /* Test owl_fmtext_expand_tabs. */
     473  owl_fmtext_clear(&fm1);
     474  owl_fmtext_append_normal(&fm1, "12\t1234");
     475  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
     476  owl_fmtext_clear(&fm2);
     477  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
     478  str = owl_fmtext_print_plain(&fm2);
     479  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
     480  FAIL_UNLESS("tabs corrected expanded",
     481              str && !strcmp(str, "12      1234567 1\n"
     482                                  "12345678        1"));
     483  owl_free(str);
     484
     485  owl_fmtext_clear(&fm2);
     486  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
     487  str = owl_fmtext_print_plain(&fm2);
     488  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
     489  FAIL_UNLESS("tabs corrected expanded",
     490              str && !strcmp(str, "12     1234567 1\n"
     491                                  "12345678       1"));
     492  owl_free(str);
     493
    472494  owl_fmtext_cleanup(&fm1);
    473495  owl_fmtext_cleanup(&fm2);
  • text.c

    ref4700c r2b83ad6  
    125125  return(out2);
    126126}
    127 
    128 #define OWL_TAB_WIDTH 8
    129127
    130128/* Caller must free return */
Note: See TracChangeset for help on using the changeset viewer.