[2560] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[PATCH 3/3] Expand tabs first in owl_fmtext_truncate_cols

daemon@ATHENA.MIT.EDU (David Benjamin)
Sun Jul 25 20:22:14 2010

From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Sun, 25 Jul 2010 17:19:32 -0700
In-Reply-To: <1280103572-6009-1-git-send-email-davidben@mit.edu>

Although the previous commit already expanded tabs from messages, a
fmtext may still get tabs in it. For instance, a :pexec. So, before
truncating, we expand all tabs.

Also adds a test case for owl_fmtext_truncate_cols.
---
 fmtext.c |   42 +++++++++++++++++++++++++++++-------------
 tester.c |   28 ++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/fmtext.c b/fmtext.c
index eb01942..12de284 100644
--- a/fmtext.c
+++ b/fmtext.c
@@ -447,19 +447,8 @@ int owl_fmtext_truncate_lines(const owl_fmtext *in, int aline, int lines, owl_fm
   return(0);
 }
 
-/* Truncate the message so that each line begins at column 'acol' and
- * ends at 'bcol' or sooner.  The first column is number 0.  The new
- * message is placed in 'out'.  The message is expected to end in a
- * new line for now.
- *
- * NOTE: This needs to be modified to deal with backing up if we find
- * a SPACING COMBINING MARK at the end of a line. If that happens, we
- * should back up to the last non-mark character and stop there.
- *
- * NOTE: If a line ends at bcol, we omit the newline. This is so printing
- * to ncurses works.
- */
-void owl_fmtext_truncate_cols(const owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
+/* Implementation of owl_fmtext_truncate_cols. Does not support tabs in input. */
+void _owl_fmtext_truncate_cols_internal(const owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
 {
   const char *ptr_s, *ptr_e, *ptr_c, *last;
   int col, st, padding, chwidth;
@@ -534,6 +523,33 @@ void owl_fmtext_truncate_cols(const owl_fmtext *in, int acol, int bcol, owl_fmte
   }
 }
 
+/* Truncate the message so that each line begins at column 'acol' and
+ * ends at 'bcol' or sooner.  The first column is number 0.  The new
+ * message is placed in 'out'.  The message is expected to end in a
+ * new line for now.
+ *
+ * NOTE: This needs to be modified to deal with backing up if we find
+ * a SPACING COMBINING MARK at the end of a line. If that happens, we
+ * should back up to the last non-mark character and stop there.
+ *
+ * NOTE: If a line ends at bcol, we omit the newline. This is so printing
+ * to ncurses works.
+ */
+void owl_fmtext_truncate_cols(const owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
+{
+  owl_fmtext notabs;
+
+  /* _owl_fmtext_truncate_cols_internal cannot handle tabs. */
+  if (strchr(in->textbuff, '\t')) {
+    owl_fmtext_init_null(&notabs);
+    owl_fmtext_expand_tabs(in, &notabs, 0);
+    _owl_fmtext_truncate_cols_internal(&notabs, acol, bcol, out);
+    owl_fmtext_cleanup(&notabs);
+  } else {
+    _owl_fmtext_truncate_cols_internal(in, acol, bcol, out);
+  }
+}
+
 /* Return the number of lines in 'f' */
 int owl_fmtext_num_lines(const owl_fmtext *f)
 {
diff --git a/tester.c b/tester.c
index 2fb124e..a195ead 100644
--- a/tester.c
+++ b/tester.c
@@ -458,6 +458,34 @@ int owl_fmtext_regtest(void) {
                                   "12345678       1"));
   owl_free(str);
 
+  /* Test owl_fmtext_truncate_cols. */
+  owl_fmtext_clear(&fm1);
+  owl_fmtext_append_normal(&fm1, "123456789012345\n");
+  owl_fmtext_append_normal(&fm1, "123456789\n");
+  owl_fmtext_append_normal(&fm1, "1234567890\n");
+
+  owl_fmtext_clear(&fm2);
+  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
+  str = owl_fmtext_print_plain(&fm2);
+  FAIL_UNLESS("fmtext correctly truncated",
+              str && !strcmp(str, "567890"
+                                  "56789\n"
+                                  "567890"));
+  owl_free(str);
+
+  owl_fmtext_clear(&fm1);
+  owl_fmtext_append_normal(&fm1, "12\t1234");
+  owl_fmtext_append_bold(&fm1, "56\n");
+  owl_fmtext_append_bold(&fm1, "12345678\t\n");
+
+  owl_fmtext_clear(&fm2);
+  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
+  str = owl_fmtext_print_plain(&fm2);
+  FAIL_UNLESS("fmtext correctly truncated",
+              str && !strcmp(str, "    123456"
+                                  "5678      "));
+  owl_free(str);
+
   owl_fmtext_cleanup(&fm1);
   owl_fmtext_cleanup(&fm2);
 
-- 
1.7.2.rc2.1.gaace1


home help back first fref pref prev next nref lref last post