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)
69 if (strcmp("-- ", l) == 0) {
77 unquote_line(GString *l) {
92 g_string_erase (l, 0, p - l->str);
96 append_quoted(GString *buf, int indent, const GString *str, const int cutpoint) {
99 indent = indent < 0? abs(indent) -1 : indent;
100 for (i=0; i<=indent; i++) {
101 g_string_append(buf, "> ");
104 g_string_append_len(buf, str->str, cutpoint);
106 g_string_append(buf, str->str);
108 g_string_append(buf, "\n");
112 get_breakpoint_utf8(const gchar *s, gint indent, const gint limit) {
114 const gchar *pos, *last;
117 indent = indent < 0? abs(indent) -1 : indent;
121 uni = g_utf8_to_ucs4_fast(s, -1, NULL);
123 if ((index + 2 * indent > limit) && last) {
127 if (g_unichar_isspace(uni[index])) {
130 pos = g_utf8_next_char(pos);
138 get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit) {
142 if (last + 2 * indent < limit)
145 for ( i=strlen(s) ; i>0; i-- ) {
147 if (i + 2 * indent <= limit) {
158 get_breakpoint(const gchar *s, const gint indent, const gint limit) {
160 if (g_utf8_validate(s, -1, NULL)) {
161 return get_breakpoint_utf8(s, indent, limit);
162 } else { /* assume ASCII */
163 g_warning("invalid UTF-8 in msg");
164 return get_breakpoint_ascii(s, indent, limit);
169 modest_text_utils_quote(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit)
172 gint indent, breakpoint, rem_indent;
174 GString *q, *l, *remaining; /* quoted msg, line */
177 /* format sent_date */
178 strftime(sent_str, 100, "%c", localtime(&sent_date));
179 q = g_string_new("");
180 g_string_printf(q, "On %s, %s wrote:\n", sent_str, from);
182 /* remaining will store the rest of the line if we have to break it */
183 remaining = g_string_new("");
184 gtk_text_buffer_get_iter_at_line(buf, &iter, 0);
186 l = get_next_line(buf, &iter);
187 indent = get_indent_level(l->str);
190 if (remaining->len) {
191 if (l->len && indent == rem_indent) {
192 g_string_prepend(l, " ");
193 g_string_prepend(l, remaining->str);
196 breakpoint = get_breakpoint(remaining->str, rem_indent, limit);
197 append_quoted(q, rem_indent, remaining, breakpoint);
198 g_string_erase(remaining, 0, breakpoint);
199 if (remaining->str[0] == ' ') {
200 g_string_erase(remaining, 0, 1);
202 } while (remaining->len);
205 g_string_free(remaining, TRUE);
206 breakpoint = get_breakpoint(l->str, indent, limit);
207 remaining = g_string_new(l->str + breakpoint);
208 if (remaining->str[0] == ' ') {
209 g_string_erase(remaining, 0, 1);
212 append_quoted(q, indent, l, breakpoint);
213 g_string_free(l, TRUE);
214 } while (!gtk_text_iter_is_end(&iter));
216 return g_string_free(q, FALSE);