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)
39 GtkTextIter iter2, iter3;
43 if (gtk_text_iter_is_end(iter))
44 return g_string_new("");
46 gtk_text_buffer_get_iter_at_line_offset(b,
48 gtk_text_iter_get_line(iter),
49 gtk_text_iter_get_chars_in_line(iter) - 1
52 gtk_text_iter_forward_char(&iter2);
53 if (gtk_text_iter_is_end(&iter2)) {
54 tmp = gtk_text_buffer_get_text(b, &iter2, iter, FALSE);
56 tmp = gtk_text_buffer_get_text(b, &iter3, iter, FALSE);
58 line = g_string_new(tmp);
60 gtk_text_iter_forward_line(iter);
66 get_indent_level(const char *l)
83 /* if we hit the signature marker "-- ", we return -(indent + 1). This
86 if (strcmp(l, "-- ") == 0) {
94 unquote_line(GString *l) {
109 g_string_erase (l, 0, p - l->str);
113 append_quoted(GString *buf, int indent, const GString *str, const int cutpoint) {
116 indent = indent < 0? abs(indent) -1 : indent;
117 for (i=0; i<=indent; i++) {
118 g_string_append(buf, "> ");
121 g_string_append_len(buf, str->str, cutpoint);
123 g_string_append(buf, str->str);
125 g_string_append(buf, "\n");
129 get_breakpoint_utf8(const gchar *s, gint indent, const gint limit) {
131 const gchar *pos, *last;
134 indent = indent < 0? abs(indent) -1 : indent;
138 uni = g_utf8_to_ucs4_fast(s, -1, NULL);
140 if ((index + 2 * indent > limit) && last) {
144 if (g_unichar_isspace(uni[index])) {
147 pos = g_utf8_next_char(pos);
155 get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit) {
159 if (last + 2 * indent < limit)
162 for ( i=strlen(s) ; i>0; i-- ) {
164 if (i + 2 * indent <= limit) {
175 get_breakpoint(const gchar *s, const gint indent, const gint limit) {
177 if (g_utf8_validate(s, -1, NULL)) {
178 return get_breakpoint_utf8(s, indent, limit);
179 } else { /* assume ASCII */
180 g_warning("invalid UTF-8 in msg");
181 return get_breakpoint_ascii(s, indent, limit);
186 modest_text_utils_quote(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit)
189 gint indent, breakpoint, rem_indent;
191 GString *q, *l, *remaining; /* quoted msg, line */
194 /* format sent_date */
195 strftime(sent_str, 100, "%c", localtime(&sent_date));
196 q = g_string_new("");
197 g_string_printf(q, "On %s, %s wrote:\n", sent_str, from);
199 /* remaining will store the rest of the line if we have to break it */
200 remaining = g_string_new("");
201 gtk_text_buffer_get_iter_at_line(buf, &iter, 0);
203 l = get_next_line(buf, &iter);
204 indent = get_indent_level(l->str);
207 if (remaining->len) {
208 if (l->len && indent == rem_indent) {
209 g_string_prepend(l, " ");
210 g_string_prepend(l, remaining->str);
213 breakpoint = get_breakpoint(remaining->str, rem_indent, limit);
214 append_quoted(q, rem_indent, remaining, breakpoint);
215 g_string_erase(remaining, 0, breakpoint);
216 if (remaining->str[0] == ' ') {
217 g_string_erase(remaining, 0, 1);
219 } while (remaining->len);
222 g_string_free(remaining, TRUE);
223 breakpoint = get_breakpoint(l->str, indent, limit);
224 remaining = g_string_new(l->str + breakpoint);
225 if (remaining->str[0] == ' ') {
226 g_string_erase(remaining, 0, 1);
229 append_quoted(q, indent, l, breakpoint);
230 g_string_free(l, TRUE);
231 } while (remaining->str[0] || !gtk_text_iter_is_end(&iter));
233 return g_string_free(q, FALSE);