[2558] in BarnOwl Developers
[PATCH 1/3] Add owl_fmtext_expand_tabs with test case
daemon@ATHENA.MIT.EDU (David Benjamin)
Sun Jul 25 20:19:41 2010
From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Sun, 25 Jul 2010 17:19:30 -0700
In-Reply-To: <1280103572-6009-1-git-send-email-davidben@mit.edu>
---
fmtext.c | 37 +++++++++++++++++++++++++++++++++++++
owl.h | 2 ++
tester.c | 23 +++++++++++++++++++++++
text.c | 2 --
4 files changed, 62 insertions(+), 2 deletions(-)
diff --git a/fmtext.c b/fmtext.c
index 04e470e..eb01942 100644
--- a/fmtext.c
+++ b/fmtext.c
@@ -371,6 +371,43 @@ void owl_fmtext_curs_waddstr_without_search(const owl_fmtext *f, WINDOW *w)
_owl_fmtext_curs_waddstr(f, w, 0);
}
+/* Expands tabs. Tabs are expanded as if given an initial indent of start. */
+void owl_fmtext_expand_tabs(const owl_fmtext *in, owl_fmtext *out, int start) {
+ int col = start, numcopied = 0;
+ char *ptr;
+
+ /* Copy the default attributes. */
+ out->default_attrs = in->default_attrs;
+ out->default_fgcolor = in->default_fgcolor;
+ out->default_bgcolor = in->default_bgcolor;
+
+ for (ptr = in->textbuff;
+ ptr < in->textbuff + in->textlen;
+ ptr = g_utf8_next_char(ptr)) {
+ gunichar c = g_utf8_get_char(ptr);
+ int chwidth;
+ if (c == '\t') {
+ /* Copy up to this tab */
+ _owl_fmtext_append_fmtext(out, in, numcopied, ptr - in->textbuff - 1);
+ /* and then copy spaces for the tab. */
+ chwidth = OWL_TAB_WIDTH - (col % OWL_TAB_WIDTH);
+ owl_fmtext_append_spaces(out, chwidth);
+ col += chwidth;
+ numcopied = g_utf8_next_char(ptr) - in->textbuff;
+ } else {
+ /* Just update col. We'll append later. */
+ if (c == '\n') {
+ col = start;
+ } else if (!owl_fmtext_is_format_char(c)) {
+ col += mk_wcwidth(c);
+ }
+ }
+ }
+ /* Append anything we've missed. */
+ if (numcopied < in->textlen)
+ _owl_fmtext_append_fmtext(out, in, numcopied, in->textlen - 1);
+}
+
/* start with line 'aline' (where the first line is 0) and print
* 'lines' number of lines into 'out'
*/
diff --git a/owl.h b/owl.h
index c64b402..2b54a3d 100644
--- a/owl.h
+++ b/owl.h
@@ -113,6 +113,8 @@ typedef void HV;
#define OWL_COLOR_DEFAULT -1
#define OWL_COLOR_INVALID -2
+#define OWL_TAB_WIDTH 8
+
#define OWL_EDITWIN_STYLE_MULTILINE 0
#define OWL_EDITWIN_STYLE_ONELINE 1
diff --git a/tester.c b/tester.c
index c63aeec..2fb124e 100644
--- a/tester.c
+++ b/tester.c
@@ -413,6 +413,7 @@ int owl_fmtext_regtest(void) {
int numfailed = 0;
owl_fmtext fm1;
owl_fmtext fm2;
+ char *str;
printf("# BEGIN testing owl_fmtext\n");
@@ -435,6 +436,28 @@ int owl_fmtext_regtest(void) {
owl_fmtext_append_bold(&fm1, "");
FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
+ /* Test owl_fmtext_expand_tabs. */
+ owl_fmtext_clear(&fm1);
+ owl_fmtext_append_normal(&fm1, "12\t1234");
+ owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
+ owl_fmtext_clear(&fm2);
+ owl_fmtext_expand_tabs(&fm1, &fm2, 0);
+ str = owl_fmtext_print_plain(&fm2);
+ FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
+ FAIL_UNLESS("tabs corrected expanded",
+ str && !strcmp(str, "12 1234567 1\n"
+ "12345678 1"));
+ owl_free(str);
+
+ owl_fmtext_clear(&fm2);
+ owl_fmtext_expand_tabs(&fm1, &fm2, 1);
+ str = owl_fmtext_print_plain(&fm2);
+ FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
+ FAIL_UNLESS("tabs corrected expanded",
+ str && !strcmp(str, "12 1234567 1\n"
+ "12345678 1"));
+ owl_free(str);
+
owl_fmtext_cleanup(&fm1);
owl_fmtext_cleanup(&fm2);
diff --git a/text.c b/text.c
index 4afe2f1..97f858b 100644
--- a/text.c
+++ b/text.c
@@ -125,8 +125,6 @@ char *owl_text_htmlstrip(const char *in)
return(out2);
}
-#define OWL_TAB_WIDTH 8
-
/* Caller must free return */
char *owl_text_expand_tabs(const char *in)
{
--
1.7.2.rc2.1.gaace1