3 /* insert (c)/licensing information) */
11 #endif /*HAVE_CONFIG_H*/
16 get_next_line(GtkTextBuffer *b, GtkTextIter *iter);
19 get_indent_level(const char *l);
22 unquote_line(GString *l);
25 append_quoted(GString *buf, const int indent, const GString *str, const int cutpoint);
28 get_breakpoint_utf8(const gchar *s, const gint indent, const gint limit);
31 get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit);
34 get_breakpoint(const gchar *s, const gint indent, const gint limit);
37 get_next_line(GtkTextBuffer *b, GtkTextIter *iter)
42 gtk_text_buffer_get_iter_at_line_offset(b,
44 gtk_text_iter_get_line(iter),
45 gtk_text_iter_get_chars_in_line(iter) -1
47 tmp = gtk_text_buffer_get_text(b, &iter2, iter, FALSE);
48 gtk_text_iter_forward_line(iter);
49 return g_string_new(tmp);
53 get_indent_level(const char *l)
72 unquote_line(GString *l) {
87 g_string_erase (l, 0, p - l->str);
91 append_quoted(GString *buf, const int indent, const GString *str, const int cutpoint) {
94 for (i=0; i<=indent; i++) {
95 g_string_append(buf, "> ");
98 g_string_append_len(buf, str->str, cutpoint);
100 g_string_append(buf, str->str);
102 g_string_append(buf, "\n");
106 get_breakpoint_utf8(const gchar *s, const gint indent, const gint limit) {
108 const gchar *pos, *last;
113 uni = g_utf8_to_ucs4_fast(s, -1, NULL);
115 if ((index + 2 * indent > limit) && last) {
119 if (g_unichar_isspace(uni[index])) {
122 pos = g_utf8_next_char(pos);
130 get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit) {
134 if (last + 2 * indent < limit)
137 for ( i=strlen(s) ; i>0; i-- ) {
139 if (i + 2 * indent <= limit) {
150 get_breakpoint(const gchar *s, const gint indent, const gint limit) {
152 if (g_utf8_validate(s, -1, NULL)) {
153 return get_breakpoint_utf8(s, indent, limit);
154 } else { /* assume ASCII */
155 g_warning("invalid UTF-8 in msg");
156 return get_breakpoint_ascii(s, indent, limit);
161 modest_text_utils_quote(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit)
164 gint indent, breakpoint, rem_indent;
166 GString *q, *l, *remaining; /* quoted msg, line */
169 /* format sent_date */
170 strftime(sent_str, 100, "%c", localtime(&sent_date));
171 q = g_string_new("");
172 g_string_printf(q, "On %s, %s wrote:\n", sent_str, from);
174 /* remaining will store the rest of the line if we have to break it */
175 remaining = g_string_new("");
176 gtk_text_buffer_get_iter_at_line(buf, &iter, 0);
178 l = get_next_line(buf, &iter);
179 indent = get_indent_level(l->str);
182 if (remaining->len) {
183 if (l->len && indent == rem_indent) {
184 g_string_prepend(l, " ");
185 g_string_prepend(l, remaining->str);
188 breakpoint = get_breakpoint(remaining->str, rem_indent, limit);
189 append_quoted(q, rem_indent, remaining, breakpoint);
190 g_string_erase(remaining, 0, breakpoint);
191 if (remaining->str[0] == ' ') {
192 g_string_erase(remaining, 0, 1);
194 } while (remaining->len);
197 g_string_free(remaining, TRUE);
198 breakpoint = get_breakpoint(l->str, indent, limit);
199 remaining = g_string_new(l->str + breakpoint);
200 if (remaining->str[0] == ' ') {
201 g_string_erase(remaining, 0, 1);
204 append_quoted(q, indent, l, breakpoint);
205 g_string_free(l, TRUE);
206 } while (!gtk_text_iter_is_end(&iter));
208 return g_string_free(q, FALSE);