2007-09-24 [paul] 3.0.1cvs26
[claws.git] / src / compose.c
index 072730928b08957ed0d7aca4fee41b7d0c835abc..9c777933a5ea5a894a32010af90e21eca42e23ef 100644 (file)
@@ -728,75 +728,81 @@ static GtkItemFactoryEntry compose_entries[] =
         ENC_ACTION(C_UTF_8)},
        {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
-       {N_("/_Options/Character _encoding/Western European (ISO-8859-_1)"),
+       {N_("/_Options/Character _encoding/Western European"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Western European/ISO-8859-_1"),
         ENC_ACTION(C_ISO_8859_1)},
-       {N_("/_Options/Character _encoding/Western European (ISO-8859-15)"),
+       {N_("/_Options/Character _encoding/Western European/ISO-8859-15"),
         ENC_ACTION(C_ISO_8859_15)},
-       {N_("/_Options/Character _encoding/Western European (Windows-1252)"),
+       {N_("/_Options/Character _encoding/Western European/Windows-1252"),
         ENC_ACTION(C_WINDOWS_1252)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
        {N_("/_Options/Character _encoding/Central European (ISO-8859-_2)"),
         ENC_ACTION(C_ISO_8859_2)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
-       {N_("/_Options/Character _encoding/_Baltic (ISO-8859-13)"),
+       {N_("/_Options/Character _encoding/Baltic"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Baltic/ISO-8859-13"),
         ENC_ACTION(C_ISO_8859_13)},
-       {N_("/_Options/Character _encoding/Baltic (ISO-8859-_4)"),
+       {N_("/_Options/Character _encoding/Baltic/ISO-8859-_4"),
         ENC_ACTION(C_ISO_8859_4)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
        {N_("/_Options/Character _encoding/Greek (ISO-8859-_7)"),
         ENC_ACTION(C_ISO_8859_7)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
-       {N_("/_Options/Character _encoding/Hebrew (ISO-8859-_8)"),
+       {N_("/_Options/Character _encoding/Hebrew"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Hebrew/ISO-8859-_8"),
         ENC_ACTION(C_ISO_8859_8)},
-       {N_("/_Options/Character _encoding/Hebrew (Windows-1255)"),
+       {N_("/_Options/Character _encoding/Hebrew/Windows-1255"),
         ENC_ACTION(C_WINDOWS_1255)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
-       {N_("/_Options/Character _encoding/Arabic (ISO-8859-_6)"),
+       {N_("/_Options/Character _encoding/Arabic"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Arabic/ISO-8859-_6"),
         ENC_ACTION(C_ISO_8859_6)},
-       {N_("/_Options/Character _encoding/Arabic (Windows-1256)"),
+       {N_("/_Options/Character _encoding/Arabic/Windows-1256"),
         ENC_ACTION(C_CP1256)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
        {N_("/_Options/Character _encoding/Turkish (ISO-8859-_9)"),
         ENC_ACTION(C_ISO_8859_9)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
-       {N_("/_Options/Character _encoding/Cyrillic (ISO-8859-_5)"),
+       {N_("/_Options/Character _encoding/Cyrillic"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Cyrillic/ISO-8859-_5"),
         ENC_ACTION(C_ISO_8859_5)},
-       {N_("/_Options/Character _encoding/Cyrillic (KOI8-_R)"),
+       {N_("/_Options/Character _encoding/Cyrillic/KOI8-_R"),
         ENC_ACTION(C_KOI8_R)},
-       {N_("/_Options/Character _encoding/Cyrillic (KOI8-U)"),
+       {N_("/_Options/Character _encoding/Cyrillic/KOI8-U"),
         ENC_ACTION(C_KOI8_U)},
-       {N_("/_Options/Character _encoding/Cyrillic (Windows-1251)"),
+       {N_("/_Options/Character _encoding/Cyrillic/Windows-1251"),
         ENC_ACTION(C_WINDOWS_1251)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
-       {N_("/_Options/Character _encoding/Japanese (ISO-2022-_JP)"),
+       {N_("/_Options/Character _encoding/Japanese"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Japanese/ISO-2022-_JP"),
         ENC_ACTION(C_ISO_2022_JP)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
-
-       {N_("/_Options/Character _encoding/Simplified Chinese (_GB2312)"),
+       {N_("/_Options/Character _encoding/Japanese/ISO-2022-JP-2"),
+        ENC_ACTION(C_ISO_2022_JP_2)},
+       {N_("/_Options/Character _encoding/Japanese/_EUC-JP"),
+        ENC_ACTION(C_EUC_JP)},
+       {N_("/_Options/Character _encoding/Japanese/_Shift__JIS"),
+        ENC_ACTION(C_SHIFT_JIS)},
+
+       {N_("/_Options/Character _encoding/Chinese"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Chinese/Simplified (_GB2312)"),
         ENC_ACTION(C_GB2312)},
-       {N_("/_Options/Character _encoding/Simplified Chinese (GBK)"),
+       {N_("/_Options/Character _encoding/Chinese/Simplified (GBK)"),
         ENC_ACTION(C_GBK)},
-       {N_("/_Options/Character _encoding/Traditional Chinese (_Big5)"),
+       {N_("/_Options/Character _encoding/Chinese/Traditional (_Big5)"),
         ENC_ACTION(C_BIG5)},
-       {N_("/_Options/Character _encoding/Traditional Chinese (EUC-_TW)"),
+       {N_("/_Options/Character _encoding/Chinese/Traditional (EUC-_TW)"),
         ENC_ACTION(C_EUC_TW)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
 
-       {N_("/_Options/Character _encoding/Korean (EUC-_KR)"),
+       {N_("/_Options/Character _encoding/Korean"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Korean/EUC-_KR"),
         ENC_ACTION(C_EUC_KR)},
-       {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
+       {N_("/_Options/Character _encoding/Korean/ISO-2022-KR"),
+        ENC_ACTION(C_ISO_2022_KR)},
 
-       {N_("/_Options/Character _encoding/Thai (TIS-620)"),
+       {N_("/_Options/Character _encoding/Thai"), NULL, NULL, 0, "<Branch>"},
+       {N_("/_Options/Character _encoding/Thai/TIS-620"),
         ENC_ACTION(C_TIS_620)},
-       {N_("/_Options/Character _encoding/Thai (Windows-874)"),
+       {N_("/_Options/Character _encoding/Thai/Windows-874"),
         ENC_ACTION(C_WINDOWS_874)},
 
        {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
@@ -925,7 +931,7 @@ static void compose_create_tags(GtkTextView *text, Compose *compose)
                                           NULL);
        }
        
-       gtk_text_buffer_create_tag(buffer, "signature",
+       compose->signature_tag = gtk_text_buffer_create_tag(buffer, "signature",
                                   "foreground-gdk", &signature_color,
                                   NULL);
        
@@ -975,6 +981,15 @@ Compose *compose_new_with_list( PrefsAccount *account, GList *listAddress )
        return compose_generic_new( account, NULL, NULL, NULL, listAddress );
 }
 
+#define SCROLL_TO_CURSOR(compose) {                            \
+       GtkTextMark *cmark = gtk_text_buffer_get_insert(        \
+               gtk_text_view_get_buffer(                       \
+                       GTK_TEXT_VIEW(compose->text)));         \
+       gtk_text_view_scroll_mark_onscreen(                     \
+               GTK_TEXT_VIEW(compose->text),                   \
+               cmark);                                         \
+}
+
 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item,
                             GPtrArray *attach_files, GList *listAddress )
 {
@@ -1145,6 +1160,8 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
                compose_exec_ext_editor(compose);
 
        compose->draft_timeout_tag = -1;
+       SCROLL_TO_CURSOR(compose);
+
        compose->modified = FALSE;
        compose_set_title(compose);
         return compose;
@@ -1293,6 +1310,10 @@ static Compose *compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar
                g_warning("compose_reply_mode(): invalid Compose Mode: %d\n", mode);
        }
        
+       if (compose == NULL) {
+               alertpanel_error(_("Unable to reply. The original email probably doesn't exist."));
+               return NULL;
+       }
        ifactory = gtk_item_factory_from_widget(compose->menubar);
 
        compose->rmode = mode;
@@ -1510,7 +1531,8 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
 
        compose->updating = FALSE;
        compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
-
+       SCROLL_TO_CURSOR(compose);
+       
        if (compose->deferred_destroy) {
                compose_destroy(compose);
                return NULL;
@@ -1656,6 +1678,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 
        compose->updating = FALSE;
        compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -1758,6 +1781,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
 
        compose->updating = FALSE;
        compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -2073,6 +2097,7 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
 
        compose->updating = FALSE;
        compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -2182,6 +2207,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        compose_set_title(compose);
        compose->updating = FALSE;
        compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -2671,9 +2697,7 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
                buf = quote_fmt_get_buffer();
                if (buf == NULL) {
                        gint line = quote_fmt_get_line();
-                       gchar *msg = g_strdup_printf(err_msg, line);
-                       alertpanel_error(msg);
-                       g_free(msg);
+                       alertpanel_error(err_msg, line);
                        goto error;
                }
        } else
@@ -3006,10 +3030,9 @@ static void compose_insert_sig(Compose *compose, gboolean replace)
        GtkTextMark *mark;
        GtkTextIter iter, iter_end;
        gint cur_pos;
-       gchar *search = NULL;
        gboolean prev_autowrap;
-       gboolean found = FALSE, shift = FALSE;
-
+       gboolean found = FALSE;
+       gboolean exists = FALSE;
        
        g_return_if_fail(compose->account != NULL);
 
@@ -3025,54 +3048,55 @@ static void compose_insert_sig(Compose *compose, gboolean replace)
 
        gtk_text_buffer_get_end_iter(buffer, &iter);
 
-       search = compose->sig_str;
-again:
-       if (replace && search) {
+       exists = (compose->sig_str != NULL);
+
+       if (replace) {
                GtkTextIter first_iter, start_iter, end_iter;
 
                gtk_text_buffer_get_start_iter(buffer, &first_iter);
 
-               if (compose->sig_str[0] == '\0')
+               if (!exists || compose->sig_str[0] == '\0')
                        found = FALSE;
                else
-                       found = gtk_text_iter_forward_search(&first_iter,
-                                                            search,
-                                                            GTK_TEXT_SEARCH_TEXT_ONLY,
-                                                            &start_iter, &end_iter,
-                                                            NULL);
+                       found = gtk_text_iter_forward_to_tag_toggle(&first_iter,
+                                       compose->signature_tag);
 
                if (found) {
-                       gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
-                       iter = start_iter;
-               }
+                       /* include previous \n\n */
+                       gtk_text_iter_backward_chars(&first_iter, 2);
+                       start_iter = first_iter;
+                       end_iter = first_iter;
+                       /* skip re-start */
+                       found = gtk_text_iter_forward_to_tag_toggle(&end_iter,
+                                       compose->signature_tag);
+                       found &= gtk_text_iter_forward_to_tag_toggle(&end_iter,
+                                       compose->signature_tag);
+                       if (found) {
+                               gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
+                               iter = start_iter;
+                       }
+               } 
        } 
-       if (replace && !found && search && strlen(search) > 2
-       &&  search[0] == '\n' && search[1] == '\n') {
-               search ++;
-               shift = TRUE;
-               goto again;
-       }
 
        g_free(compose->sig_str);
        compose->sig_str = compose_get_signature_str(compose);
-       if (!compose->sig_str || (replace && !compose->account->auto_sig))
-               compose->sig_str = g_strdup("");
 
        cur_pos = gtk_text_iter_get_offset(&iter);
-       if (shift && found)
-               gtk_text_buffer_insert(buffer, &iter, compose->sig_str + 1, -1);
-       else
-               gtk_text_buffer_insert(buffer, &iter, compose->sig_str, -1);
-       /* skip \n\n */
-       gtk_text_buffer_get_iter_at_offset(buffer, &iter, cur_pos);
-       gtk_text_iter_forward_char(&iter);
-       gtk_text_iter_forward_char(&iter);
-       gtk_text_buffer_get_end_iter(buffer, &iter_end);
-       gtk_text_buffer_apply_tag_by_name(buffer,"signature",&iter, &iter_end);
 
-       if (cur_pos > gtk_text_buffer_get_char_count (buffer))
-               cur_pos = gtk_text_buffer_get_char_count (buffer);
+       if (!compose->sig_str || (replace && !compose->account->auto_sig)) {
+               g_free(compose->sig_str);
+               compose->sig_str = NULL;
+       } else {
+               gtk_text_buffer_insert(buffer, &iter, compose->sig_str, -1);
+               /* remove \n\n */
+               gtk_text_buffer_get_iter_at_offset(buffer, &iter, cur_pos);
+               gtk_text_iter_forward_chars(&iter, 2);
+               gtk_text_buffer_get_end_iter(buffer, &iter_end);
+               gtk_text_buffer_apply_tag_by_name(buffer,"signature",&iter, &iter_end);
 
+               if (cur_pos > gtk_text_buffer_get_char_count (buffer))
+                       cur_pos = gtk_text_buffer_get_char_count (buffer);
+       }
        /* put the cursor where it should be 
         * either where the quote_fmt says, either before the signature */
        if (compose->set_cursor_pos < 0)
@@ -3600,8 +3624,16 @@ static gboolean compose_get_line_break_pos(GtkTextBuffer *buffer,
        gtk_text_iter_forward_to_line_end(&line_end);
        str = gtk_text_buffer_get_text(buffer, &iter, &line_end, FALSE);
        len = g_utf8_strlen(str, -1);
+       
+       if (len == 0) {
+               g_free(str);
+               g_warning("compose_get_line_break_pos: len = 0!\n");
+               return FALSE;
+       }
+
        /* g_print("breaking line: %d: %s (len = %d)\n",
                gtk_text_iter_get_line(&iter), str, len); */
+
        attrs = g_new(PangoLogAttr, len + 1);
 
        pango_default_break(str, -1, NULL, attrs, len + 1);
@@ -3922,7 +3954,9 @@ static gboolean compose_beautify_paragraph(Compose *compose, GtkTextIter *par_it
                if (prev_autowrap == FALSE && !force && !wrap_quote) {
                        goto colorize;
                }
-               if (compose_get_line_break_pos(buffer, &iter, &break_pos,
+               if (gtk_text_iter_ends_line(&iter)) {
+                       goto colorize;
+               } else if (compose_get_line_break_pos(buffer, &iter, &break_pos,
                                               prefs_common.linewrap_len,
                                               quote_len)) {
                        GtkTextIter prev, next, cur;
@@ -4471,10 +4505,19 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
                if (*str == '\0' && check_everything == TRUE && 
                    compose->mode != COMPOSE_REDIRECT) {
                        AlertValue aval;
+                       gchar *button_label;
+                       gchar *message;
 
-                       aval = alertpanel(_("Send"),
-                                         _("Subject is empty. Send it anyway?"),
-                                         GTK_STOCK_CANCEL, _("+_Send"), NULL);
+                       if (compose->sending)
+                               button_label = _("+_Send");
+                       else
+                               button_label = _("+_Queue");
+                       message = g_strdup_printf(_("Subject is empty. %s it anyway?"),
+                                       compose->sending?_("Send"):_("Queue"));
+
+                       aval = alertpanel(compose->sending?_("Send"):_("Send later"), message,
+                                         GTK_STOCK_CANCEL, button_label, NULL);
+                       g_free(message);
                        if (aval != G_ALERTALTERNATE)
                                return FALSE;
                }
@@ -4498,7 +4541,7 @@ gint compose_send(Compose *compose)
        MainWindow *mainwin = mainwindow_get_mainwindow();
        gboolean queued_removed = FALSE;
 
-       if (prefs_common.send_dialog_mode != SEND_DIALOG_ALWAYS
+       if (prefs_common.send_dialog_invisible
                        || compose->batch == TRUE)
                discard_window = TRUE;
 
@@ -4605,11 +4648,9 @@ gint compose_send(Compose *compose)
                }
        } else {
                if (errstr) {
-                       gchar *tmp = g_strdup_printf(_("%s\nUse \"Send queued messages\" from "
+                       alertpanel_error_log(_("%s\nUse \"Send queued messages\" from "
                                   "the main window to retry."), errstr);
                        g_free(errstr);
-                       alertpanel_error_log(tmp);
-                       g_free(tmp);
                } else {
                        alertpanel_error_log(_("The message was queued but could not be "
                                   "sent.\nUse \"Send queued messages\" from "
@@ -4762,11 +4803,19 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
        }
 
        /* Resent-Message-ID */
-       if (compose->account->gen_msgid) {
-               generate_msgid(buf, sizeof(buf));
-               fprintf(fp, "Resent-Message-ID: <%s>\n", buf);
-               compose->msgid = g_strdup(buf);
+       if (compose->account->set_domain && compose->account->domain) {
+               g_snprintf(buf, sizeof(buf), "%s", compose->account->domain); 
+       } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) {
+               g_snprintf(buf, sizeof(buf), "%s", 
+                       strchr(compose->account->address, '@') ?
+                               strchr(compose->account->address, '@')+1 :
+                               compose->account->address);
+       } else {
+               g_snprintf(buf, sizeof(buf), "%s", "");
        }
+       generate_msgid(buf, sizeof(buf));
+       fprintf(fp, "Resent-Message-ID: <%s>\n", buf);
+       compose->msgid = g_strdup(buf);
 
        compose_redirect_write_headers_from_headerlist(compose, fp);
 
@@ -5137,6 +5186,8 @@ static gint compose_remove_reedit_target(Compose *compose, gboolean force)
                if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
                        g_warning("can't remove the old message\n");
                        return -1;
+               } else {
+                       debug_print("removed reedit target %d\n", msginfo->msgnum);
                }
        }
 
@@ -5660,11 +5711,19 @@ static gchar *compose_get_header(Compose *compose)
        g_free(str);
 
        /* Message-ID */
-       if (compose->account->gen_msgid) {
-               generate_msgid(buf, sizeof(buf));
-               g_string_append_printf(header, "Message-ID: <%s>\n", buf);
-               compose->msgid = g_strdup(buf);
+       if (compose->account->set_domain && compose->account->domain) {
+               g_snprintf(buf, sizeof(buf), "%s", compose->account->domain); 
+       } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) {
+               g_snprintf(buf, sizeof(buf), "%s", 
+                       strchr(compose->account->address, '@') ?
+                               strchr(compose->account->address, '@')+1 :
+                               compose->account->address);
+       } else {
+               g_snprintf(buf, sizeof(buf), "%s", "");
        }
+       generate_msgid(buf, sizeof(buf));
+       g_string_append_printf(header, "Message-ID: <%s>\n", buf);
+       compose->msgid = g_strdup(buf);
 
        if (compose->remove_references == FALSE) {
                /* In-Reply-To */
@@ -7049,12 +7108,9 @@ static void compose_update_privacy_system_menu_item(Compose * compose, gboolean
                        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
                
                if (warn && !found && strlen(compose->privacy_system)) {
-                       gchar *tmp = g_strdup_printf(
-                               _("The privacy system '%s' cannot be loaded. You "
+                       alertpanel_warning(_("The privacy system '%s' cannot be loaded. You "
                                  "will not be able to sign or encrypt this message."),
                                  compose->privacy_system);
-                       alertpanel_warning(tmp);
-                       g_free(tmp);
                }
        } 
 
@@ -8465,6 +8521,22 @@ gboolean compose_draft (gpointer data, guint action)
 
        folder_item_scan(draft);
        if ((msgnum = folder_item_add_msg(draft, tmp, &flag, TRUE)) < 0) {
+               MsgInfo *tmpinfo = NULL;
+               debug_print("didn't get msgnum after adding draft [%s]\n", compose->msgid?compose->msgid:"no msgid");
+               if (compose->msgid) {
+                       tmpinfo = folder_item_get_msginfo_by_msgid(draft, compose->msgid);
+               }
+               if (tmpinfo) {
+                       msgnum = tmpinfo->msgnum;
+                       procmsg_msginfo_free(tmpinfo);
+                       debug_print("got draft msgnum %d from scanning\n", msgnum);
+               } else {
+                       debug_print("didn't get draft msgnum after scanning\n");
+               }
+       } else {
+               debug_print("got draft msgnum %d from adding\n", msgnum);
+       }
+       if (msgnum < 0) {
                g_unlink(tmp);
                g_free(tmp);
                if (action != COMPOSE_AUTO_SAVE) {
@@ -8499,11 +8571,7 @@ gboolean compose_draft (gpointer data, guint action)
        }
 
        newmsginfo = folder_item_get_msginfo(draft, msgnum);
-       if (!newmsginfo && compose->msgid) {
-               newmsginfo = folder_item_get_msginfo_by_msgid(draft, compose->msgid);
-               if (newmsginfo)
-                       msgnum = newmsginfo->msgnum;
-       }
+
        if (newmsginfo) {
                procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
                if (target_locked)
@@ -8533,7 +8601,7 @@ gboolean compose_draft (gpointer data, guint action)
 
                path = folder_item_fetch_msg(draft, msgnum);
                if (path == NULL) {
-                       debug_print("can't fetch %s:%d\n",draft->path, msgnum);
+                       debug_print("can't fetch %s:%d\n", draft->path, msgnum);
                        goto unlock;
                }
                if (g_stat(path, &s) < 0) {
@@ -9671,6 +9739,7 @@ static void compose_show_first_last_header(Compose *compose, gboolean show_first
 
        vadj = gtk_viewport_get_vadjustment(GTK_VIEWPORT(compose->header_table->parent));
        gtk_adjustment_set_value(vadj, (show_first ? vadj->lower : vadj->upper));
+       gtk_adjustment_changed(vadj);
 }
 
 static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
@@ -9688,6 +9757,8 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
        if (paste_as_quotation) {
                gchar *new_text;
                const gchar *qmark;
+               guint pos = 0;
+               GtkTextIter start_iter;
 
                if (len < 0)
                        len = strlen(text);
@@ -9699,6 +9770,8 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
                mark = gtk_text_buffer_create_mark(buffer, NULL, iter, FALSE);
                gtk_text_buffer_place_cursor(buffer, iter);
 
+               pos = gtk_text_iter_get_offset(iter);
+
                compose_quote_fmt(compose, NULL, "%Q", qmark, new_text, TRUE, FALSE,
                                                  _("Quote format error at line %d."));
                quote_fmt_reset_vartable();
@@ -9708,6 +9781,13 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
                                  
                gtk_text_buffer_get_iter_at_mark(buffer, iter, mark);
                gtk_text_buffer_place_cursor(buffer, iter);
+               gtk_text_buffer_delete_mark(buffer, mark);
+
+               gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, pos);
+               mark = gtk_text_buffer_create_mark(buffer, NULL, &start_iter, FALSE);
+               compose_beautify_paragraph(compose, &start_iter, FALSE);
+               gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark);
+               gtk_text_buffer_delete_mark(buffer, mark);
        } else {
                if (strcmp(text, "\n") || compose->automatic_break
                || gtk_text_iter_starts_line(iter))
@@ -9719,12 +9799,12 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
                }
        }
        
-       mark = gtk_text_buffer_create_mark(buffer, NULL, iter, FALSE);
-       
-       compose_beautify_paragraph(compose, iter, FALSE);
-
-       gtk_text_buffer_get_iter_at_mark(buffer, iter, mark);
-       gtk_text_buffer_delete_mark(buffer, mark);
+       if (!paste_as_quotation) {
+               mark = gtk_text_buffer_create_mark(buffer, NULL, iter, FALSE);
+               compose_beautify_paragraph(compose, iter, FALSE);
+               gtk_text_buffer_get_iter_at_mark(buffer, iter, mark);
+               gtk_text_buffer_delete_mark(buffer, mark);
+       }
 
        g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
                                          G_CALLBACK(text_inserted),
@@ -9911,7 +9991,7 @@ static void compose_reply_from_messageview_real(MessageView *msgview, GSList *ms
        } else
                compose = compose_reply_mode((ComposeMode)action, msginfo_list, body);
 
-       if (originally_enc) {
+       if (compose && originally_enc) {
                compose_force_encryption(compose, compose->account, FALSE);
        }