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)
70 /* if we hit the signature marker "-- ", we return -(indent + 1). This
73 if (strcmp(l, "-- ") == 0) {
81 unquote_line(GString *l) {
96 g_string_erase (l, 0, p - l->str);
100 append_quoted(GString *buf, int indent, const GString *str, const int cutpoint) {
103 indent = indent < 0? abs(indent) -1 : indent;
104 for (i=0; i<=indent; i++) {
105 g_string_append(buf, "> ");
108 g_string_append_len(buf, str->str, cutpoint);
110 g_string_append(buf, str->str);
112 g_string_append(buf, "\n");
116 get_breakpoint_utf8(const gchar *s, gint indent, const gint limit) {
118 const gchar *pos, *last;
121 indent = indent < 0? abs(indent) -1 : indent;
125 uni = g_utf8_to_ucs4_fast(s, -1, NULL);
127 if ((index + 2 * indent > limit) && last) {
131 if (g_unichar_isspace(uni[index])) {
134 pos = g_utf8_next_char(pos);
142 get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit) {
146 if (last + 2 * indent < limit)
149 for ( i=strlen(s) ; i>0; i-- ) {
151 if (i + 2 * indent <= limit) {
162 get_breakpoint(const gchar *s, const gint indent, const gint limit) {
164 if (g_utf8_validate(s, -1, NULL)) {
165 return get_breakpoint_utf8(s, indent, limit);
166 } else { /* assume ASCII */
167 g_warning("invalid UTF-8 in msg");
168 return get_breakpoint_ascii(s, indent, limit);
173 modest_text_utils_quote(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit)
176 gint indent, breakpoint, rem_indent;
178 GString *q, *l, *remaining; /* quoted msg, line */
181 /* format sent_date */
182 strftime(sent_str, 100, "%c", localtime(&sent_date));
183 q = g_string_new("");
184 g_string_printf(q, "On %s, %s wrote:\n", sent_str, from);
186 /* remaining will store the rest of the line if we have to break it */
187 remaining = g_string_new("");
188 gtk_text_buffer_get_iter_at_line(buf, &iter, 0);
190 l = get_next_line(buf, &iter);
191 indent = get_indent_level(l->str);
194 if (remaining->len) {
195 if (l->len && indent == rem_indent) {
196 g_string_prepend(l, " ");
197 g_string_prepend(l, remaining->str);
200 breakpoint = get_breakpoint(remaining->str, rem_indent, limit);
201 append_quoted(q, rem_indent, remaining, breakpoint);
202 g_string_erase(remaining, 0, breakpoint);
203 if (remaining->str[0] == ' ') {
204 g_string_erase(remaining, 0, 1);
206 } while (remaining->len);
209 g_string_free(remaining, TRUE);
210 breakpoint = get_breakpoint(l->str, indent, limit);
211 remaining = g_string_new(l->str + breakpoint);
212 if (remaining->str[0] == ' ') {
213 g_string_erase(remaining, 0, 1);
216 append_quoted(q, indent, l, breakpoint);
217 g_string_free(l, TRUE);
218 } while (!gtk_text_iter_is_end(&iter));
220 return g_string_free(q, FALSE);