3 /* insert (c)/licensing information) */
8 #include "modest-text-utils.h"
13 #endif /*HAVE_CONFIG_H*/
18 get_next_line(GtkTextBuffer *b, GtkTextIter *iter);
21 get_indent_level(const char *l);
24 unquote_line(GString *l);
27 append_quoted(GString *buf, const int indent, const GString *str, const int cutpoint);
30 get_breakpoint_utf8(const gchar *s, const gint indent, const gint limit);
33 get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit);
36 get_breakpoint(const gchar *s, const gint indent, const gint limit);
39 get_next_line(GtkTextBuffer *b, GtkTextIter *iter)
41 GtkTextIter iter2, iter3;
45 if (gtk_text_iter_is_end(iter))
46 return g_string_new("");
48 gtk_text_buffer_get_iter_at_line_offset(b,
50 gtk_text_iter_get_line(iter),
51 gtk_text_iter_get_chars_in_line(iter) - 1
54 gtk_text_iter_forward_char(&iter2);
55 if (gtk_text_iter_is_end(&iter2)) {
56 tmp = gtk_text_buffer_get_text(b, &iter2, iter, FALSE);
58 tmp = gtk_text_buffer_get_text(b, &iter3, iter, FALSE);
60 line = g_string_new(tmp);
62 gtk_text_iter_forward_line(iter);
68 get_indent_level(const char *l)
85 /* if we hit the signature marker "-- ", we return -(indent + 1). This
88 if (strcmp(l, "-- ") == 0) {
96 unquote_line(GString *l) {
111 g_string_erase (l, 0, p - l->str);
115 append_quoted(GString *buf, int indent, const GString *str, const int cutpoint) {
118 indent = indent < 0? abs(indent) -1 : indent;
119 for (i=0; i<=indent; i++) {
120 g_string_append(buf, "> ");
123 g_string_append_len(buf, str->str, cutpoint);
125 g_string_append(buf, str->str);
127 g_string_append(buf, "\n");
131 get_breakpoint_utf8(const gchar *s, gint indent, const gint limit) {
133 const gchar *pos, *last;
136 indent = indent < 0? abs(indent) -1 : indent;
140 uni = g_utf8_to_ucs4_fast(s, -1, NULL);
142 if ((index + 2 * indent > limit) && last) {
146 if (g_unichar_isspace(uni[index])) {
149 pos = g_utf8_next_char(pos);
157 get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit) {
161 if (last + 2 * indent < limit)
164 for ( i=strlen(s) ; i>0; i-- ) {
166 if (i + 2 * indent <= limit) {
177 get_breakpoint(const gchar *s, const gint indent, const gint limit) {
179 if (g_utf8_validate(s, -1, NULL)) {
180 return get_breakpoint_utf8(s, indent, limit);
181 } else { /* assume ASCII */
182 g_warning("invalid UTF-8 in msg");
183 return get_breakpoint_ascii(s, indent, limit);
189 modest_text_utils_quote(const gchar *to_quote, const gchar *from, const time_t sent_date, const int limit)
193 buf = gtk_text_buffer_new (NULL);
194 gtk_text_buffer_set_text(buf, to_quote, -1);
195 return modest_text_utils_quote_text_buffer(buf, from, sent_date, limit);
199 modest_text_utils_quote_text_buffer(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit)
203 gint indent, breakpoint, rem_indent;
205 GString *q, *l, *remaining; /* quoted msg, line */
207 /* format sent_date */
208 strftime(sent_str, 100, "%c", localtime(&sent_date));
209 q = g_string_new("");
210 g_string_printf(q, "On %s, %s wrote:\n", sent_str, from);
212 /* remaining will store the rest of the line if we have to break it */
213 remaining = g_string_new("");
216 gtk_text_buffer_get_iter_at_line(buf, &iter, 0);
218 l = get_next_line(buf, &iter);
219 indent = get_indent_level(l->str);
222 if (remaining->len) {
223 if (l->len && indent == rem_indent) {
224 g_string_prepend(l, " ");
225 g_string_prepend(l, remaining->str);
228 breakpoint = get_breakpoint(remaining->str, rem_indent, limit);
229 append_quoted(q, rem_indent, remaining, breakpoint);
230 g_string_erase(remaining, 0, breakpoint);
231 if (remaining->str[0] == ' ') {
232 g_string_erase(remaining, 0, 1);
234 } while (remaining->len);
237 g_string_free(remaining, TRUE);
238 breakpoint = get_breakpoint(l->str, indent, limit);
239 remaining = g_string_new(l->str + breakpoint);
240 if (remaining->str[0] == ' ') {
241 g_string_erase(remaining, 0, 1);
244 append_quoted(q, indent, l, breakpoint);
245 g_string_free(l, TRUE);
246 } while (remaining->str[0] || !gtk_text_iter_is_end(&iter));
248 return g_string_free(q, FALSE);