sync with sylpheed 0.6.5cvs20
[claws.git] / src / compose.c
index be897ddd5006ff4de3372f9c7477853e9255a219..76cfb86e37ace162a2255e170ea785864a655597 100644 (file)
@@ -124,7 +124,10 @@ static GdkColor quote_color = {0, 0, 0, 0xbfff};
 
 static GList *compose_list = NULL;
 
-static void compose_set_undo (UndoMain *undostruct, gint undo_state, gint redo_state, GtkWidget *changewidget);
+static void compose_set_undo                   (UndoMain       *undostruct, 
+                                                 gint            undo_state, 
+                                                gint            redo_state, 
+                                                GtkWidget      *changewidget);
 Compose *compose_generic_new                   (PrefsAccount   *account,
                                                 const gchar    *to,
                                                 FolderItem     *item);
@@ -746,12 +749,13 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
 
        if (prefs_common.auto_sig)
                compose_insert_sig(compose);
-       gtk_editable_set_position(GTK_EDITABLE(text), 0);
-       gtk_stext_set_point(text, 0);
 
        if (quote && prefs_common.linewrap_quote)
                compose_wrap_line_all(compose);
 
+       gtk_editable_set_position(GTK_EDITABLE(text), 0);
+       gtk_stext_set_point(text, 0);
+
        gtk_stext_thaw(text);
        gtk_widget_grab_focus(compose->text);
 
@@ -1508,15 +1512,18 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
                        Xstrdup_a(quote_str, buf, return NULL)
        }
 
-       quote_fmt_init(msginfo, quote_str);
-       quote_fmt_scan_string(fmt);
-       quote_fmt_parse();
+       if (fmt && *fmt != '\0') {
+               quote_fmt_init(msginfo, quote_str);
+               quote_fmt_scan_string(fmt);
+               quote_fmt_parse();
 
-       buf = quote_fmt_get_buffer();
-       if (buf == NULL) {
-               alertpanel_error(_("Message reply/forward format error."));
-               return NULL;
-       }
+               buf = quote_fmt_get_buffer();
+               if (buf == NULL) {
+                       alertpanel_error(_("Message reply/forward format error."));
+                       return NULL;
+               }
+       } else
+               buf = "";
 
        gtk_stext_freeze(text);
        gtk_stext_set_point(text, 0);
@@ -1908,6 +1915,16 @@ static void compose_attach_append(Compose *compose, const gchar *file,
        compose_attach_info(compose, ainfo, cnttype);
 }
 
+#define GET_CHAR(pos, buf, len)                                                     \
+{                                                                           \
+       if (text->use_wchar)                                                 \
+               len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos)));     \
+       else {                                                               \
+               buf[0] = GTK_STEXT_INDEX(text, (pos));                       \
+               len = 1;                                                     \
+       }                                                                    \
+}
+
 static void compose_wrap_line(Compose *compose)
 {
        GtkSText *text = GTK_STEXT(compose->text);
@@ -1920,13 +1937,6 @@ static void compose_wrap_line(Compose *compose)
        gint line_pos, cur_pos;
        gint line_len, cur_len;
 
-#define GET_STEXT(pos)                                                        \
-       if (text->use_wchar)                                                 \
-               ch_len = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, (pos))); \
-       else {                                                               \
-               cbuf[0] = GTK_STEXT_INDEX(text, (pos));                       \
-               ch_len = 1;                                                  \
-       }
 
        gtk_stext_freeze(text);
 
@@ -1934,11 +1944,11 @@ static void compose_wrap_line(Compose *compose)
 
        /* check to see if the point is on the paragraph mark (empty line). */
        cur_pos = gtk_stext_get_point(text);
-       GET_STEXT(cur_pos);
+       GET_CHAR(cur_pos, cbuf, ch_len);
        if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
                if (cur_pos == 0)
                        goto compose_end; /* on the paragraph mark */
-               GET_STEXT(cur_pos - 1);
+               GET_CHAR(cur_pos - 1, cbuf, ch_len);
                if (ch_len == 1 && *cbuf == '\n')
                        goto compose_end; /* on the paragraph mark */
        }
@@ -1946,7 +1956,7 @@ static void compose_wrap_line(Compose *compose)
        /* find paragraph start. */
        line_end = quoted = 0;
        for (p_start = cur_pos; p_start >= 0; --p_start) {
-               GET_STEXT(p_start);
+               GET_CHAR(p_start, cbuf, ch_len);
                if (ch_len == 1 && *cbuf == '\n') {
                        if (quoted)
                                goto compose_end; /* quoted part */
@@ -1970,7 +1980,7 @@ static void compose_wrap_line(Compose *compose)
        /* find paragraph end. */
        line_end = 0;
        for (p_end = cur_pos; p_end < text_len; p_end++) {
-               GET_STEXT(p_end);
+               GET_CHAR(p_end, cbuf, ch_len);
                if (ch_len == 1 && *cbuf == '\n') {
                        if (line_end) {
                                p_end -= 1;
@@ -1997,7 +2007,7 @@ static void compose_wrap_line(Compose *compose)
        for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
                guint space = 0;
 
-               GET_STEXT(cur_pos);
+               GET_CHAR(cur_pos, cbuf, ch_len);
 
                if (ch_len < 0) {
                        cbuf[0] = '\0';
@@ -2011,7 +2021,7 @@ static void compose_wrap_line(Compose *compose)
                        guint replace = 0;
                        if (last_ch_len == 1 && !isspace(last_ch)) {
                                if (cur_pos + 1 < p_end) {
-                                       GET_STEXT(cur_pos + 1);
+                                       GET_CHAR(cur_pos + 1, cbuf, ch_len);
                                        if (ch_len == 1 && !isspace(*cbuf))
                                                replace = 1;
                                }
@@ -2042,7 +2052,7 @@ static void compose_wrap_line(Compose *compose)
                    line_len > 0) {
                        gint tlen = ch_len;
 
-                       GET_STEXT(line_pos - 1);
+                       GET_CHAR(line_pos - 1, cbuf, ch_len);
                        if (ch_len == 1 && isspace(*cbuf)) {
                                gtk_stext_set_point(text, line_pos);
                                gtk_stext_backward_delete(text, 1);
@@ -2073,8 +2083,6 @@ static void compose_wrap_line(Compose *compose)
 
 compose_end:
        gtk_stext_thaw(text);
-
-#undef GET_STEXT
 }
 
 /* return indent length */
@@ -2085,13 +2093,7 @@ static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
        gchar cbuf[MB_LEN_MAX];
 
        for (i = start_pos; i < text_len; i++) {
-               if (text->use_wchar)
-                       ch_len = wctomb
-                               (cbuf, (wchar_t)GTK_STEXT_INDEX(text, i));
-               else {
-                       cbuf[0] = GTK_STEXT_INDEX(text, i);
-                       ch_len = 1;
-               }
+               GET_CHAR(i, cbuf, ch_len);
                if (ch_len > 1)
                        break;
                /* allow space, tab, > or | */
@@ -2118,8 +2120,7 @@ static guint ins_quote(GtkSText *text, guint quote_len, guint indent_len,
                        gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
                }
                ins_len = indent_len;
-       }
-       else {
+       } else {
                gtk_stext_insert(text, NULL, NULL, NULL, quote_fmt, quote_len);
                ins_len = quote_len;
        }
@@ -2132,8 +2133,8 @@ static guint ins_quote(GtkSText *text, guint quote_len, guint indent_len,
 /* Darko: used when I debug wrapping */
 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
 {
-       int i;
-       char ch;
+       gint i;
+       gchar ch;
 
        printf("%d [", pos);
        for (i = pos; i < tlen; i++) {
@@ -2153,7 +2154,7 @@ static void compose_wrap_line_all(Compose *compose)
        guint line_pos = 0, cur_pos = 0, p_pos = 0;
        gint line_len = 0, cur_len = 0;
        gint ch_len;
-       gint is_new_line = 1, do_delete = 0;
+       gboolean is_new_line = TRUE, do_delete = FALSE;
        guint qlen = 0, i_len = 0;
        guint linewrap_quote = prefs_common.linewrap_quote;
        guint linewrap_len = prefs_common.linewrap_len;
@@ -2172,7 +2173,7 @@ static void compose_wrap_line_all(Compose *compose)
                if (linewrap_quote && is_new_line) {
                        qlen = gtkut_text_str_compare
                                (text, cur_pos, tlen, qfmt);
-                       is_new_line = 0;
+                       is_new_line = FALSE;
                        if (qlen)
                                i_len = get_indent_length(text, cur_pos, tlen);
                        else
@@ -2184,14 +2185,7 @@ static void compose_wrap_line_all(Compose *compose)
 #endif
                }
 
-               /* get character(s) at current position */
-               if (text->use_wchar)
-                       ch_len = wctomb
-                               (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
-               else {
-                       cbuf[0] = GTK_STEXT_INDEX(text, cur_pos);
-                       ch_len = 1;
-               }
+               GET_CHAR(cur_pos, cbuf, ch_len);
 
                /* fix line length for tabs */
                if (ch_len == 1 && *cbuf == '\t') {
@@ -2227,30 +2221,17 @@ static void compose_wrap_line_all(Compose *compose)
                                ilen =  gtkut_text_str_compare_n
                                        (text, cur_pos + 1, p_pos, i_len, tlen);
                                if (cur_pos + ilen < tlen) {
-                                       if (text->use_wchar)
-                                               clen = wctomb(cb, (wchar_t)GTK_STEXT_INDEX(text, cur_pos + ilen + 1));
-                                       else {
-                                               cb[0] = GTK_STEXT_INDEX(text,
-                                                           cur_pos + ilen + 1);
-                                               clen = 1;
-                                       }
+                                       GET_CHAR(cur_pos + ilen + 1, cb, clen);
                                        /* no need to join the lines */
-                                       if ((clen == 1) && (cb[0] == '\n'))
-                                               do_delete = 0;
+                                       if (clen == 1 && cb[0] == '\n')
+                                               do_delete = FALSE;
                                }
-                       }
                        /* if it's just newline skip it */
-                       else if (do_delete && (cur_pos + 1 < tlen)) {
-                               if (text->use_wchar)
-                                       clen = wctomb(cb, (wchar_t)GTK_STEXT_INDEX(text, cur_pos + 1));
-                               else {
-                                       cb[0] = GTK_STEXT_INDEX(text,
-                                                               cur_pos + 1);
-                                       clen = 1;
-                               }
+                       } else if (do_delete && (cur_pos + 1 < tlen)) {
+                               GET_CHAR(cur_pos + 1, cb, clen);
                                /* no need to join the lines */
-                               if ((clen == 1) && (cb[0] == '\n'))
-                                       do_delete = 0;
+                               if (clen == 1 && cb[0] == '\n')
+                                       do_delete = FALSE;
                        }
 
 #ifdef WRAP_DEBUG
@@ -2284,13 +2265,7 @@ static void compose_wrap_line_all(Compose *compose)
                                        }
                                }
 
-                               if (text->use_wchar)
-                                       clen = wctomb
-                                               (cb, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
-                               else {
-                                       cb[0] = GTK_STEXT_INDEX(text, cur_pos);
-                                       clen = 1;
-                               }
+                               GET_CHAR(cur_pos, cb, clen);
 
                                /* insert space if it's alphanumeric */
                                if ((cur_pos != line_pos) &&
@@ -2306,8 +2281,8 @@ static void compose_wrap_line_all(Compose *compose)
                                line_pos = cur_pos;
                                line_len = cur_len = 0;
                                qlen = 0;
-                               do_delete = 0;
-                               is_new_line = 1;
+                               do_delete = FALSE;
+                               is_new_line = TRUE;
 #ifdef WRAP_DEBUG
                                printf("after delete l_pos=");
                                dump_text(text, line_pos, tlen, 1);
@@ -2319,8 +2294,8 @@ static void compose_wrap_line_all(Compose *compose)
                        line_pos = cur_pos + 1;
                        line_len = cur_len = 0;
                        qlen = 0;
-                       do_delete = 0;
-                       is_new_line = 1;
+                       do_delete = FALSE;
+                       is_new_line = TRUE;
                        continue;
                }
 
@@ -2353,13 +2328,9 @@ static void compose_wrap_line_all(Compose *compose)
                        printf("new line_pos=%d\n", line_pos);
 #endif
 
+                       GET_CHAR(line_pos - 1, cbuf, clen);
+
                        /* if next character is space delete it */
-                       if (text->use_wchar)
-                               clen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
-                       else {
-                               cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
-                               clen = 1;
-                       }
                         if (clen == 1 && isspace(*cbuf)) {
                                if (p_pos + i_len != line_pos ||
                                    !gtkut_text_is_uri_string
@@ -2393,10 +2364,10 @@ static void compose_wrap_line_all(Compose *compose)
                        /* for loop will increase it */
                        cur_pos = line_pos - 1;
                        /* start over with current line */
-                       is_new_line = 1;
+                       is_new_line = TRUE;
                        line_len = 0;
                        cur_len = 0;
-                       do_delete = 1;
+                       do_delete = TRUE;
 #ifdef WRAP_DEBUG
                        printf("after CR insert ");
                        dump_text(text, line_pos, tlen, 1);
@@ -2437,86 +2408,7 @@ static void compose_wrap_line_all(Compose *compose)
 
        gtk_stext_thaw(text);
 }
-
-#if 0
-static void compose_wrap_line_all(Compose *compose)
-{
-       GtkText *text = GTK_STEXT(compose->text);
-       guint text_len;
-       guint line_pos = 0, cur_pos = 0;
-       gint line_len = 0, cur_len = 0;
-       gint ch_len;
-       gchar cbuf[MB_LEN_MAX];
-
-       gtk_stext_freeze(text);
-
-       text_len = gtk_stext_get_length(text);
-
-       for (; cur_pos < text_len; cur_pos++) {
-               if (text->use_wchar)
-                       ch_len = wctomb
-                               (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
-               else {
-                       cbuf[0] = GTK_STEXT_INDEX(text, cur_pos);
-                       ch_len = 1;
-               }
-
-               if (ch_len == 1 && *cbuf == '\n') {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len = 0;
-                       continue;
-               }
-
-               if (ch_len < 0) {
-                       cbuf[0] = '\0';
-                       ch_len = 1;
-               }
-
-               if (ch_len == 1 && isspace(*cbuf)) {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
-               }
-
-               if (cur_len + ch_len > prefs_common.linewrap_len &&
-                   line_len > 0) {
-                       gint tlen;
-
-                       if (text->use_wchar)
-                               tlen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
-                       else {
-                               cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
-                               tlen = 1;
-                       }
-                       if (tlen == 1 && isspace(*cbuf)) {
-                               gtk_stext_set_point(text, line_pos);
-                               gtk_stext_backward_delete(text, 1);
-                               text_len--;
-                               cur_pos--;
-                               line_pos--;
-                               cur_len--;
-                               line_len--;
-                       }
-
-                       gtk_stext_set_point(text, line_pos);
-                       gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
-                       text_len++;
-                       cur_pos++;
-                       line_pos++;
-                       cur_len = cur_len - line_len + ch_len;
-                       line_len = 0;
-                       continue;
-               }
-
-               if (ch_len > 1) {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
-               }
-               cur_len += ch_len;
-       }
-
-       gtk_stext_thaw(text);
-}
-#endif
+#undef GET_CHAR
 
 static void compose_set_title(Compose *compose)
 {
@@ -2863,8 +2755,26 @@ static gint compose_bounce_write_to_file(Compose *compose, const gchar *file)
                    || (g_strncasecmp(buf, "Delivered-To:",
                                      strlen("Delivered-To:")) == 0))
                        continue;
+
                if (fputs(buf, fdest) == -1)
                        goto error;
+
+               if (g_strncasecmp(buf, "From:", strlen("From:")) == 0) {
+                       fputs(" (by way of ", fdest);
+                       if (compose->account->name
+                           && *compose->account->name) {
+                               compose_convert_header
+                                       (buf, sizeof(buf),
+                                        compose->account->name,
+                                        strlen("From: "));
+                               fprintf(fdest, "%s <%s>",
+                                       buf, compose->account->address);
+                       } else
+                               fprintf(fdest, "%s",
+                                       compose->account->address);
+                       fputs(")", fdest);
+               }
+
                if (fputs("\n", fdest) == -1)
                        goto error;
        }
@@ -3829,10 +3739,10 @@ static void compose_create_header_entry(Compose *compose) {
        } else {
                switch(compose->account->protocol) {
                        case A_NNTP:
-                               header = _("Newsgroups:");
+                               header = prefs_common.trans_hdr ? _("Newsgroups:") : "Newsgroups:";
                                break;
                        default:
-                               header = _("To:");
+                               header = prefs_common.trans_hdr ? _("To:") : "To:";
                                break;
                }                                                                   
        }
@@ -4714,6 +4624,8 @@ static void compose_template_apply(Compose *compose, Template *tmpl)
        if (tmpl->subject)
                gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
                                   tmpl->subject);
+       if (tmpl->to)
+               compose_entry_append(compose, tmpl->to, COMPOSE_TO);
 
        if (compose->replyinfo == NULL) {
                MsgInfo dummyinfo;
@@ -6140,7 +6052,9 @@ void compose_headerentry_changed_cb(GtkWidget *entry,
  *
  * Change the sensivity of the menuentries undo and redo
  **/
-static void compose_set_undo (UndoMain *undostruct, gint undo_state, gint redo_state, GtkWidget *changewidget) {
+static void compose_set_undo(UndoMain *undostruct, gint undo_state, 
+                            gint redo_state, GtkWidget *changewidget) 
+{
        GtkItemFactory *ifactory;
         debug_print ("Set_undo.  UNDO:%i  REDO:%i\n",
                  undo_state,
@@ -6172,6 +6086,7 @@ static void compose_set_undo (UndoMain *undostruct, gint undo_state, gint redo_s
                 break;
         default:
                 g_warning ("Undo state not recognized");
+               break;
         }
 
         /* Set redo*/
@@ -6195,5 +6110,6 @@ static void compose_set_undo (UndoMain *undostruct, gint undo_state, gint redo_s
                 break;
         default:
                 g_warning ("Redo state not recognized");
+               break;
         }
 }