2007-08-18 [colin] 2.10.0cvs130
[claws.git] / src / compose.c
index d94cdb24784bf89e099418eca965dfdb9d70e7c9..401635076df0bd582ed3838beb85fb8c5dca8b81 100644 (file)
@@ -4,7 +4,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #include "combobox.h"
 #include "hooks.h"
 #include "privacy.h"
+#include "timing.h"
 
 enum
 {
@@ -199,6 +200,7 @@ static Compose *compose_generic_new                 (PrefsAccount   *account,
                                                 GList          *listAddress );
 
 static Compose *compose_create                 (PrefsAccount   *account,
+                                                FolderItem              *item,
                                                 ComposeMode     mode,
                                                 gboolean batch);
 
@@ -271,7 +273,7 @@ static gboolean compose_attach_append               (Compose        *compose,
 static void compose_attach_parts               (Compose        *compose,
                                                 MsgInfo        *msginfo);
 
-static void compose_beautify_paragraph         (Compose        *compose,
+static gboolean compose_beautify_paragraph     (Compose        *compose,
                                                 GtkTextIter    *par_iter,
                                                 gboolean        force);
 static void compose_wrap_all                   (Compose        *compose);
@@ -899,26 +901,26 @@ static void compose_create_tags(GtkTextView *text, Compose *compose)
        }
 
        if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
-               gtk_text_buffer_create_tag(buffer, "quote0",
+               compose->quote0_tag = gtk_text_buffer_create_tag(buffer, "quote0",
                                           "foreground-gdk", &quote_color1,
                                           "paragraph-background-gdk", &quote_bgcolor1,
                                           NULL);
-               gtk_text_buffer_create_tag(buffer, "quote1",
+               compose->quote1_tag = gtk_text_buffer_create_tag(buffer, "quote1",
                                           "foreground-gdk", &quote_color2,
                                           "paragraph-background-gdk", &quote_bgcolor2,
                                           NULL);
-               gtk_text_buffer_create_tag(buffer, "quote2",
+               compose->quote2_tag = gtk_text_buffer_create_tag(buffer, "quote2",
                                           "foreground-gdk", &quote_color3,
                                           "paragraph-background-gdk", &quote_bgcolor3,
                                           NULL);
        } else {
-               gtk_text_buffer_create_tag(buffer, "quote0",
+               compose->quote0_tag = gtk_text_buffer_create_tag(buffer, "quote0",
                                           "foreground-gdk", &quote_color1,
                                           NULL);
-               gtk_text_buffer_create_tag(buffer, "quote1",
+               compose->quote1_tag = gtk_text_buffer_create_tag(buffer, "quote1",
                                           "foreground-gdk", &quote_color2,
                                           NULL);
-               gtk_text_buffer_create_tag(buffer, "quote2",
+               compose->quote2_tag = gtk_text_buffer_create_tag(buffer, "quote2",
                                           "foreground-gdk", &quote_color3,
                                           NULL);
        }
@@ -926,7 +928,8 @@ static void compose_create_tags(GtkTextView *text, Compose *compose)
        gtk_text_buffer_create_tag(buffer, "signature",
                                   "foreground-gdk", &signature_color,
                                   NULL);
-       gtk_text_buffer_create_tag(buffer, "link",
+       
+       compose->uri_tag = gtk_text_buffer_create_tag(buffer, "link",
                                        "foreground-gdk", &uri_color,
                                         NULL);
        compose->no_wrap_tag = gtk_text_buffer_create_tag(buffer, "no_wrap", NULL);
@@ -980,8 +983,8 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        GtkTextBuffer *textbuf;
        GtkTextIter iter;
        GtkItemFactory *ifactory;
-       gchar *subject_format = NULL;
-       gchar *body_format = NULL;
+       const gchar *subject_format = NULL;
+       const gchar *body_format = NULL;
 
        if (item && item->prefs && item->prefs->enable_default_account)
                account = account_find_from_id(item->prefs->default_account);
@@ -989,7 +992,7 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        if (!account) account = cur_account;
        g_return_val_if_fail(account != NULL, NULL);
 
-       compose = compose_create(account, COMPOSE_NEW, FALSE);
+       compose = compose_create(account, item, COMPOSE_NEW, FALSE);
 
        ifactory = gtk_item_factory_from_widget(compose->menubar);
 
@@ -1141,6 +1144,7 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        if (prefs_common.auto_exteditor)
                compose_exec_ext_editor(compose);
 
+       compose->draft_timeout_tag = -1;
        compose->modified = FALSE;
        compose_set_title(compose);
         return compose;
@@ -1357,7 +1361,7 @@ static void compose_extract_original_charset(Compose *compose)
                info = compose->targetinfo;
        }
        if (info) {
-               MimeInfo *mimeinfo = procmime_scan_message(info);
+               MimeInfo *mimeinfo = procmime_scan_message_short(info);
                MimeInfo *partinfo = mimeinfo;
                while (partinfo && partinfo->type != MIMETYPE_TEXT)
                        partinfo = procmime_mimeinfo_next(partinfo);
@@ -1401,9 +1405,9 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
        GtkTextView *textview;
        GtkTextBuffer *textbuf;
        gboolean quote = FALSE;
-       gchar *qmark = NULL;
-       gchar *body_fmt = NULL;
-
+       const gchar *qmark = NULL;
+       const gchar *body_fmt = NULL;
+       START_TIMING("");
        g_return_val_if_fail(msginfo != NULL, NULL);
        g_return_val_if_fail(msginfo->folder != NULL, NULL);
 
@@ -1411,7 +1415,7 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
 
        g_return_val_if_fail(account != NULL, NULL);
 
-       compose = compose_create(account, COMPOSE_REPLY, FALSE);
+       compose = compose_create(account, msginfo->folder, COMPOSE_REPLY, FALSE);
 
        compose->updating = TRUE;
 
@@ -1480,7 +1484,8 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
                                          _("Message reply format error at line %d."));
                quote_fmt_reset_vartable();
        }
-       if (procmime_msginfo_is_encrypted(compose->replyinfo)) {
+
+       if (MSG_IS_ENCRYPTED(compose->replyinfo->flags)) {
                compose_force_encryption(compose, account, FALSE);
        }
 
@@ -1504,12 +1509,13 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
        compose_set_title(compose);
 
        compose->updating = FALSE;
+       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
                return NULL;
        }
-
+       END_TIMING();
        return compose;
 }
 
@@ -1538,7 +1544,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                                (msginfo)))
                account = cur_account;
 
-       compose = compose_create(account, COMPOSE_FORWARD, batch);
+       compose = compose_create(account, msginfo->folder, COMPOSE_FORWARD, batch);
 
        compose->updating = TRUE;
        compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
@@ -1578,8 +1584,8 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 
                g_free(msgfile);
        } else {
-               gchar *qmark = NULL;
-               gchar *body_fmt = prefs_common.fw_quotefmt;
+               const gchar *qmark = NULL;
+               const gchar *body_fmt = prefs_common.fw_quotefmt;
                MsgInfo *full_msginfo;
 
                full_msginfo = procmsg_msginfo_get_full_info(msginfo);
@@ -1649,6 +1655,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
        compose_set_title(compose);
 
        compose->updating = FALSE;
+       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -1692,7 +1699,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
                MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
        }
 
-       compose = compose_create(account, COMPOSE_FORWARD, FALSE);
+       compose = compose_create(account, ((MsgInfo *)msginfo_list->data)->folder, COMPOSE_FORWARD, FALSE);
 
        compose->updating = TRUE;
 
@@ -1750,6 +1757,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
        compose_set_title(compose);
 
        compose->updating = FALSE;
+       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -1806,6 +1814,36 @@ static void compose_colorize_signature(Compose *compose)
                }
 }
 
+#define BLOCK_WRAP() {                                                 \
+       prev_autowrap = compose->autowrap;                              \
+       buffer = gtk_text_view_get_buffer(                              \
+                                       GTK_TEXT_VIEW(compose->text));  \
+       compose->autowrap = FALSE;                                      \
+                                                                       \
+       g_signal_handlers_block_by_func(G_OBJECT(buffer),               \
+                               G_CALLBACK(compose_changed_cb),         \
+                               compose);                               \
+       g_signal_handlers_block_by_func(G_OBJECT(buffer),               \
+                               G_CALLBACK(text_inserted),              \
+                               compose);                               \
+}
+#define UNBLOCK_WRAP() {                                               \
+       compose->autowrap = prev_autowrap;                              \
+       if (compose->autowrap) {                                        \
+               gint old = compose->draft_timeout_tag;                  \
+               compose->draft_timeout_tag = -2;                        \
+               compose_wrap_all(compose);                              \
+               compose->draft_timeout_tag = old;                       \
+       }                                                               \
+                                                                       \
+       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),             \
+                               G_CALLBACK(compose_changed_cb),         \
+                               compose);                               \
+       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),             \
+                               G_CALLBACK(text_inserted),              \
+                               compose);                               \
+}
+
 Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
 {
        Compose *compose = NULL;
@@ -1932,7 +1970,7 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
         }
        g_return_val_if_fail(account != NULL, NULL);
 
-       compose = compose_create(account, COMPOSE_REEDIT, batch);
+       compose = compose_create(account, msginfo->folder, COMPOSE_REEDIT, batch);
        
        compose->replyinfo = replyinfo;
        compose->fwdinfo = fwdinfo;
@@ -1993,7 +2031,7 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
                                        G_CALLBACK(compose_changed_cb),
                                        compose);
        
-       if (procmime_msginfo_is_encrypted(msginfo)) {
+       if (MSG_IS_ENCRYPTED(msginfo->flags)) {
                fp = procmime_get_first_encrypted_text_content(msginfo);
                if (fp) {
                        compose_force_encryption(compose, account, TRUE);
@@ -2007,14 +2045,13 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
 
        if (fp != NULL) {
                gboolean prev_autowrap = compose->autowrap;
-
-               compose->autowrap = FALSE;
+               GtkTextBuffer *buffer = textbuf;
+               BLOCK_WRAP();
                while (fgets(buf, sizeof(buf), fp) != NULL) {
                        strcrchomp(buf);
                        gtk_text_buffer_insert(textbuf, &iter, buf, -1);
                }
-               compose_wrap_all_full(compose, FALSE);
-               compose->autowrap = prev_autowrap;
+               UNBLOCK_WRAP();
                fclose(fp);
        }
        
@@ -2035,6 +2072,7 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
        compose_set_title(compose);
 
        compose->updating = FALSE;
+       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -2061,7 +2099,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
                                        prefs_common.reply_account_autosel);
        g_return_val_if_fail(account != NULL, NULL);
 
-       compose = compose_create(account, COMPOSE_REDIRECT, batch);
+       compose = compose_create(account, msginfo->folder, COMPOSE_REDIRECT, batch);
 
        compose->updating = TRUE;
 
@@ -2143,6 +2181,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        compose->modified = FALSE;
        compose_set_title(compose);
        compose->updating = FALSE;
+       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -2974,8 +3013,7 @@ static void compose_insert_sig(Compose *compose, gboolean replace)
        
        g_return_if_fail(compose->account != NULL);
 
-       prev_autowrap = compose->autowrap;
-       compose->autowrap = FALSE;
+       BLOCK_WRAP();
 
        g_signal_handlers_block_by_func(G_OBJECT(buffer),
                                        G_CALLBACK(compose_changed_cb),
@@ -3048,9 +3086,7 @@ again:
                                        G_CALLBACK(compose_changed_cb),
                                        compose);
                
-       compose->autowrap = prev_autowrap;
-       if (compose->autowrap)
-               compose_wrap_all(compose);
+       UNBLOCK_WRAP();
 }
 
 static gchar *compose_get_signature_str(Compose *compose)
@@ -3564,8 +3600,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);
@@ -3755,8 +3799,7 @@ static gboolean compose_join_next_line(Compose *compose,
                g_warning("alloc error scanning URIs\n"); \
        }
 
-static gboolean automatic_break = FALSE;
-static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter, gboolean force)
+static gboolean compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter, gboolean force)
 {
        GtkTextView *text = GTK_TEXT_VIEW(compose->text);
        GtkTextBuffer *buffer;
@@ -3770,6 +3813,18 @@ static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter,
        gint nouri_start = -1, nouri_stop = -1;
        gint num_blocks = 0;
        gint quotelevel = -1;
+       gboolean modified = force;
+       gboolean removed = FALSE;
+       gboolean modified_before_remove = FALSE;
+       gint lines = 0;
+       gboolean start = TRUE;
+
+       if (force) {
+               modified = TRUE;
+       }
+       if (compose->draft_timeout_tag == -2) {
+               modified = TRUE;
+       }
 
        compose->autowrap = FALSE;
 
@@ -3783,24 +3838,26 @@ static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter,
                gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
        }
 
-       /* move to paragraph start */
-       gtk_text_iter_set_line_offset(&iter, 0);
-       if (gtk_text_iter_ends_line(&iter)) {
-               while (gtk_text_iter_ends_line(&iter) &&
-                      gtk_text_iter_forward_line(&iter))
-                       ;
-       } else {
-               while (gtk_text_iter_backward_line(&iter)) {
-                       if (gtk_text_iter_ends_line(&iter)) {
-                               gtk_text_iter_forward_line(&iter);
-                               break;
+
+       if (compose->draft_timeout_tag == -2) {
+               if (gtk_text_iter_ends_line(&iter)) {
+                       while (gtk_text_iter_ends_line(&iter) &&
+                              gtk_text_iter_forward_line(&iter))
+                               ;
+               } else {
+                       while (gtk_text_iter_backward_line(&iter)) {
+                               if (gtk_text_iter_ends_line(&iter)) {
+                                       gtk_text_iter_forward_line(&iter);
+                                       break;
+                               }
                        }
                }
+       } else {
+               /* move to line start */
+               gtk_text_iter_set_line_offset(&iter, 0);
        }
-
        /* go until paragraph end (empty line) */
-       
-       while (!gtk_text_iter_ends_line(&iter)) {
+       while (start || !gtk_text_iter_ends_line(&iter)) {
                gchar *scanpos = NULL;
                /* parse table - in order of priority */
                struct table {
@@ -3835,6 +3892,7 @@ static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter,
                gchar *o_walk = NULL, *walk = NULL, *bp = NULL, *ep = NULL;
                gint walk_pos;
                
+               start = FALSE;
                if (!prev_autowrap && num_blocks == 0) {
                        num_blocks++;
                        g_signal_handlers_block_by_func(G_OBJECT(buffer),
@@ -3872,19 +3930,23 @@ static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter,
                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;
-                       
+
                        if (prev_autowrap != FALSE || force) {
-                               automatic_break = TRUE;
+                               compose->automatic_break = TRUE;
+                               modified = TRUE;
                                gtk_text_buffer_insert(buffer, &break_pos, "\n", 1);
-                               automatic_break = FALSE;
+                               compose->automatic_break = FALSE;
                        } else if (quote_str && wrap_quote) {
-                               automatic_break = TRUE;
+                               compose->automatic_break = TRUE;
+                               modified = TRUE;
                                gtk_text_buffer_insert(buffer, &break_pos, "\n", 1);
-                               automatic_break = FALSE;
+                               compose->automatic_break = FALSE;
                        } else 
                                goto colorize;
                        /* remove trailing spaces */
@@ -3911,7 +3973,7 @@ static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter,
                                                       quote_str, -1);
 
                        iter = break_pos;
-                       compose_join_next_line(compose, buffer, &iter, quote_str);
+                       modified |= compose_join_next_line(compose, buffer, &iter, quote_str);
 
                        /* move iter to current line start */
                        gtk_text_iter_set_line_offset(&iter, 0);
@@ -3919,10 +3981,11 @@ static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter,
                                g_free(quote_str);
                                quote_str = NULL;
                        }
-                       continue;
+                       continue;       
                } else {
                        /* move iter to next line start */
                        iter = break_pos;
+                       lines++;
                }
 
 colorize:
@@ -3982,14 +4045,41 @@ colorize:
                        endquote = iter;
 
                        switch (quotelevel) {
-                       case 0: gtk_text_buffer_apply_tag_by_name(
-                                       buffer, "quote0", &startquote, &endquote);
+                       case 0: 
+                               if (!gtk_text_iter_has_tag(&startquote, compose->quote0_tag) ||
+                                   !gtk_text_iter_has_tag(&end_of_line, compose->quote0_tag)) {
+                                       gtk_text_buffer_apply_tag_by_name(
+                                               buffer, "quote0", &startquote, &endquote);
+                                       gtk_text_buffer_remove_tag_by_name(
+                                               buffer, "quote1", &startquote, &endquote);
+                                       gtk_text_buffer_remove_tag_by_name(
+                                               buffer, "quote2", &startquote, &endquote);
+                                       modified = TRUE;
+                               }
                                break;
-                       case 1: gtk_text_buffer_apply_tag_by_name(
-                                       buffer, "quote1", &startquote, &endquote);
+                       case 1: 
+                               if (!gtk_text_iter_has_tag(&startquote, compose->quote1_tag) ||
+                                   !gtk_text_iter_has_tag(&end_of_line, compose->quote1_tag)) {
+                                       gtk_text_buffer_apply_tag_by_name(
+                                               buffer, "quote1", &startquote, &endquote);
+                                       gtk_text_buffer_remove_tag_by_name(
+                                               buffer, "quote0", &startquote, &endquote);
+                                       gtk_text_buffer_remove_tag_by_name(
+                                               buffer, "quote2", &startquote, &endquote);
+                                       modified = TRUE;
+                               }
                                break;
-                       case 2: gtk_text_buffer_apply_tag_by_name(
-                                       buffer, "quote2", &startquote, &endquote);
+                       case 2: 
+                               if (!gtk_text_iter_has_tag(&startquote, compose->quote2_tag) ||
+                                   !gtk_text_iter_has_tag(&end_of_line, compose->quote2_tag)) {
+                                       gtk_text_buffer_apply_tag_by_name(
+                                               buffer, "quote2", &startquote, &endquote);
+                                       gtk_text_buffer_remove_tag_by_name(
+                                               buffer, "quote0", &startquote, &endquote);
+                                       gtk_text_buffer_remove_tag_by_name(
+                                               buffer, "quote1", &startquote, &endquote);
+                                       modified = TRUE;
+                               }
                                break;
                        }
                        startq_offset = -1;
@@ -3998,39 +4088,71 @@ colorize:
                        gtk_text_buffer_get_iter_at_offset(
                                buffer, &startnoquote, noq_offset);
                        endnoquote = iter;
-                       gtk_text_buffer_remove_tag_by_name(
-                               buffer, "quote0", &startnoquote, &endnoquote);
-                       gtk_text_buffer_remove_tag_by_name(
-                               buffer, "quote1", &startnoquote, &endnoquote);
-                       gtk_text_buffer_remove_tag_by_name(
-                               buffer, "quote2", &startnoquote, &endnoquote);
+
+                       if ((gtk_text_iter_has_tag(&startnoquote, compose->quote0_tag)
+                         && gtk_text_iter_has_tag(&end_of_line, compose->quote0_tag)) ||
+                           (gtk_text_iter_has_tag(&startnoquote, compose->quote1_tag)
+                         && gtk_text_iter_has_tag(&end_of_line, compose->quote1_tag)) ||
+                           (gtk_text_iter_has_tag(&startnoquote, compose->quote2_tag)
+                         && gtk_text_iter_has_tag(&end_of_line, compose->quote2_tag))) {
+                               gtk_text_buffer_remove_tag_by_name(
+                                       buffer, "quote0", &startnoquote, &endnoquote);
+                               gtk_text_buffer_remove_tag_by_name(
+                                       buffer, "quote1", &startnoquote, &endnoquote);
+                               gtk_text_buffer_remove_tag_by_name(
+                                       buffer, "quote2", &startnoquote, &endnoquote);
+                               modified = TRUE;
+                       }
                        noq_offset = -1;
                }
                
-               /* always */ {
+               if (uri_start != nouri_start && uri_stop != nouri_stop) {
                        GtkTextIter nouri_start_iter, nouri_end_iter;
                        gtk_text_buffer_get_iter_at_offset(
                                buffer, &nouri_start_iter, nouri_start);
                        gtk_text_buffer_get_iter_at_offset(
                                buffer, &nouri_end_iter, nouri_stop);
-                       gtk_text_buffer_remove_tag_by_name(
-                               buffer, "link", &nouri_start_iter, &nouri_end_iter);
+                       if (gtk_text_iter_has_tag(&nouri_start_iter, compose->uri_tag) &&
+                           gtk_text_iter_has_tag(&nouri_end_iter, compose->uri_tag)) {
+                               gtk_text_buffer_remove_tag_by_name(
+                                       buffer, "link", &nouri_start_iter, &nouri_end_iter);
+                               modified_before_remove = modified;
+                               modified = TRUE;
+                               removed = TRUE;
+                       }
                }
                if (uri_start > 0 && uri_stop > 0) {
-                       GtkTextIter uri_start_iter, uri_end_iter;
+                       GtkTextIter uri_start_iter, uri_end_iter, back;
                        gtk_text_buffer_get_iter_at_offset(
                                buffer, &uri_start_iter, uri_start);
                        gtk_text_buffer_get_iter_at_offset(
                                buffer, &uri_end_iter, uri_stop);
-                       gtk_text_buffer_apply_tag_by_name(
-                               buffer, "link", &uri_start_iter, &uri_end_iter);
+                       back = uri_end_iter;
+                       gtk_text_iter_backward_char(&back);
+                       if (!gtk_text_iter_has_tag(&uri_start_iter, compose->uri_tag) ||
+                           !gtk_text_iter_has_tag(&back, compose->uri_tag)) {
+                               gtk_text_buffer_apply_tag_by_name(
+                                       buffer, "link", &uri_start_iter, &uri_end_iter);
+                               modified = TRUE;
+                               if (removed && !modified_before_remove) {
+                                       modified = FALSE;
+                               } 
+                       }
+               }
+               if (!modified) {
+                       debug_print("not modified, out after %d lines\n", lines);
+                       goto end;
                }
        }
 
+       debug_print("modified, out after %d lines\n", lines);
+end:
        if (par_iter)
                *par_iter = iter;
        undo_wrapping(compose->undostruct, FALSE);
        compose->autowrap = prev_autowrap;
+       
+       return modified;
 }
 
 void compose_action_cb(void *data)
@@ -4049,12 +4171,13 @@ static void compose_wrap_all_full(Compose *compose, gboolean force)
        GtkTextView *text = GTK_TEXT_VIEW(compose->text);
        GtkTextBuffer *buffer;
        GtkTextIter iter;
+       gboolean modified = TRUE;
 
        buffer = gtk_text_view_get_buffer(text);
 
        gtk_text_buffer_get_start_iter(buffer, &iter);
-       while (!gtk_text_iter_is_end(&iter))
-               compose_beautify_paragraph(compose, &iter, force);
+       while (!gtk_text_iter_is_end(&iter) && modified)
+               modified = compose_beautify_paragraph(compose, &iter, force);
 
 }
 
@@ -4385,7 +4508,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;
 
@@ -5024,6 +5147,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);
                }
        }
 
@@ -6151,32 +6276,6 @@ static void compose_savemsg_select_cb(GtkWidget *widget, Compose *compose)
 static void entry_paste_clipboard(Compose *compose, GtkWidget *entry, gboolean wrap,
                                  GdkAtom clip, GtkTextIter *insert_place);
 
-#define BLOCK_WRAP() {                                                 \
-       prev_autowrap = compose->autowrap;                              \
-       buffer = gtk_text_view_get_buffer(                              \
-                                       GTK_TEXT_VIEW(compose->text));  \
-       compose->autowrap = FALSE;                                      \
-                                                                       \
-       g_signal_handlers_block_by_func(G_OBJECT(buffer),               \
-                               G_CALLBACK(compose_changed_cb),         \
-                               compose);                               \
-       g_signal_handlers_block_by_func(G_OBJECT(buffer),               \
-                               G_CALLBACK(text_inserted),              \
-                               compose);                               \
-}
-#define UNBLOCK_WRAP() {                                               \
-       compose->autowrap = prev_autowrap;                              \
-       if (compose->autowrap)                                          \
-               compose_wrap_all(compose);                              \
-                                                                       \
-       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),             \
-                               G_CALLBACK(compose_changed_cb),         \
-                               compose);                               \
-       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),             \
-                               G_CALLBACK(text_inserted),              \
-                               compose);                               \
-}
-
 
 static gboolean text_clicked(GtkWidget *text, GdkEventButton *event,
                                        Compose *compose)
@@ -6286,7 +6385,9 @@ static gboolean compose_popup_menu(GtkWidget *widget, gpointer data)
 }
 
 static gboolean compose_force_window_origin = TRUE;
-static Compose *compose_create(PrefsAccount *account, ComposeMode mode,
+static Compose *compose_create(PrefsAccount *account,
+                                                FolderItem *folder,
+                                                ComposeMode mode,
                                                 gboolean batch)
 {
        Compose   *compose;
@@ -6342,6 +6443,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode,
 
        compose->batch = batch;
        compose->account = account;
+       compose->folder = folder;
        
        compose->mutex = g_mutex_new();
        compose->set_cursor_pos = -1;
@@ -6395,8 +6497,13 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode,
        gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
 
        gtk_widget_realize(handlebox);
+#ifdef MAEMO
+       compose->toolbar = toolbar_create(TOOLBAR_COMPOSE, window,
+                                         (gpointer)compose);
+#else
        compose->toolbar = toolbar_create(TOOLBAR_COMPOSE, handlebox,
                                          (gpointer)compose);
+#endif
 
        vbox2 = gtk_vbox_new(FALSE, 2);
        gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
@@ -6619,7 +6726,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode,
        compose->exteditor_file    = NULL;
        compose->exteditor_pid     = -1;
        compose->exteditor_tag     = -1;
-       compose->draft_timeout_tag = -1;
+       compose->draft_timeout_tag = -2; /* inhibit auto-drafting while loading */
 
 #if USE_ASPELL
        menu_set_sensitive(ifactory, "/Spelling", FALSE);
@@ -7107,6 +7214,44 @@ void compose_reflect_prefs_pixmap_theme(void)
        }
 }
 
+static const gchar *compose_quote_char_from_context(Compose *compose)
+{
+       const gchar *qmark = NULL;
+
+       g_return_val_if_fail(compose != NULL, NULL);
+
+       switch (compose->mode) {
+               /* use forward-specific quote char */
+               case COMPOSE_FORWARD:
+               case COMPOSE_FORWARD_AS_ATTACH:
+               case COMPOSE_FORWARD_INLINE:
+                       if (compose->folder && compose->folder->prefs &&
+                                       compose->folder->prefs->forward_with_format)
+                               qmark = compose->folder->prefs->forward_quotemark;
+                       else if (compose->account->forward_with_format)
+                               qmark = compose->account->forward_quotemark;
+                       else
+                               qmark = prefs_common.fw_quotemark;
+                       break;
+
+               /* use reply-specific quote char in all other modes */
+               default:
+                       if (compose->folder && compose->folder->prefs &&
+                                       compose->folder->prefs->reply_with_format)
+                               qmark = compose->folder->prefs->reply_quotemark;
+                       else if (compose->account->reply_with_format)
+                               qmark = compose->account->reply_quotemark;
+                       else
+                               qmark = prefs_common.quotemark;
+                       break;
+       }
+
+       if (qmark == NULL || *qmark == '\0')
+               qmark = "> ";
+
+       return qmark;
+}
+
 static void compose_template_apply(Compose *compose, Template *tmpl,
                                   gboolean replace)
 {
@@ -7114,7 +7259,7 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
        GtkTextBuffer *buffer;
        GtkTextMark *mark;
        GtkTextIter iter;
-       gchar *qmark;
+       const gchar *qmark;
        gchar *parsed_str = NULL;
        gint cursor_pos = 0;
        const gchar *err_msg = _("Template body format error at line %d.");
@@ -7126,10 +7271,7 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
        buffer = gtk_text_view_get_buffer(text);
 
        if (tmpl->value) {
-               if (prefs_common.quotemark && *prefs_common.quotemark)
-                       qmark = prefs_common.quotemark;
-               else
-                       qmark = "> ";
+               qmark = compose_quote_char_from_context(compose);
 
                if (compose->replyinfo != NULL) {
 
@@ -8179,7 +8321,7 @@ static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
                  "to send this email.")))
                return;
        
-       if (compose->draft_timeout_tag != -1) { /* CLAWS: disable draft timeout */
+       if (compose->draft_timeout_tag >= 0) { /* CLAWS: disable draft timeout */
                g_source_remove(compose->draft_timeout_tag);
                compose->draft_timeout_tag = -1;
        }
@@ -8219,22 +8361,7 @@ static void compose_send_later_cb(gpointer data, guint action,
        toolbar_main_set_sensitive(mainwindow_get_mainwindow());
 }
 
-void compose_draft (gpointer data, guint action) 
-{
-       compose_draft_cb(data, action, NULL);   
-}
-
 #define DRAFTED_AT_EXIT "drafted_at_exit"
-void compose_clear_exit_drafts(void)
-{
-       gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                                     DRAFTED_AT_EXIT, NULL);
-       if (is_file_exist(filepath))
-               g_unlink(filepath);
-       
-       g_free(filepath);
-}
-
 static void compose_register_draft(MsgInfo *info)
 {
        gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
@@ -8250,34 +8377,7 @@ static void compose_register_draft(MsgInfo *info)
        g_free(filepath);       
 }
 
-void compose_reopen_exit_drafts(void)
-{
-       gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                                     DRAFTED_AT_EXIT, NULL);
-       FILE *fp = fopen(filepath, "rb");
-       gchar buf[1024];
-       
-       if (fp) {
-               while (fgets(buf, sizeof(buf), fp)) {
-                       gchar **parts = g_strsplit(buf, "\t", 2);
-                       const gchar *folder = parts[0];
-                       int msgnum = parts[1] ? atoi(parts[1]):-1;
-                       
-                       if (folder && *folder && msgnum > -1) {
-                               FolderItem *item = folder_find_item_from_identifier(folder);
-                               MsgInfo *info = folder_item_get_msginfo(item, msgnum);
-                               if (info)
-                                       compose_reedit(info, FALSE);
-                       }
-                       g_strfreev(parts);
-               }       
-               fclose(fp);
-       }       
-       g_free(filepath);
-       compose_clear_exit_drafts();
-}
-
-static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
+gboolean compose_draft (gpointer data, guint action) 
 {
        Compose *compose = (Compose *)data;
        FolderItem *draft;
@@ -8289,10 +8389,13 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
        FILE *fp;
        gboolean target_locked = FALSE;
        
-       if (lock) return;
+       if (lock) return FALSE;
+
+       if (compose->sending)
+               return TRUE;
 
        draft = account_get_special_folder(compose->account, F_DRAFT);
-       g_return_if_fail(draft != NULL);
+       g_return_val_if_fail(draft != NULL, FALSE);
        
        if (!g_mutex_trylock(compose->mutex)) {
                /* we don't want to lock the mutex once it's available,
@@ -8300,7 +8403,7 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
                 * it is compose_close - which means once unlocked,
                 * the compose struct will be freed */
                debug_print("couldn't lock mutex, probably sending\n");
-               return;
+               return FALSE;
        }
        
        lock = TRUE;
@@ -8374,10 +8477,47 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
 
        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)
-                       alertpanel_error(_("Could not save draft."));
+               if (action != COMPOSE_AUTO_SAVE) {
+                       if (action != COMPOSE_DRAFT_FOR_EXIT)
+                               alertpanel_error(_("Could not save draft."));
+                       else {
+                               AlertValue val;
+                               gtkut_window_popup(compose->window);
+                               val = alertpanel_full(_("Could not save draft"),
+                                       _("Could not save draft.\n"
+                                       "Do you want to cancel exit or discard this email?"),
+                                         _("_Cancel exit"), _("_Discard email"), NULL,
+                                         FALSE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
+                               if (val == G_ALERTALTERNATE) {
+                                       lock = FALSE;
+                                       g_mutex_unlock(compose->mutex); /* must be done before closing */
+                                       compose_close(compose);
+                                       return TRUE;
+                               } else {
+                                       lock = FALSE;
+                                       g_mutex_unlock(compose->mutex); /* must be done before closing */
+                                       return FALSE;
+                               }
+                       }
+               }
                goto unlock;
        }
        g_free(tmp);
@@ -8387,6 +8527,7 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
        }
 
        newmsginfo = folder_item_get_msginfo(draft, msgnum);
+
        if (newmsginfo) {
                procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
                if (target_locked)
@@ -8409,14 +8550,14 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
                lock = FALSE;
                g_mutex_unlock(compose->mutex); /* must be done before closing */
                compose_close(compose);
-               return;
+               return TRUE;
        } else {
                struct stat s;
                gchar *path;
 
                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) {
@@ -8445,6 +8586,49 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
 unlock:
        lock = FALSE;
        g_mutex_unlock(compose->mutex);
+       return TRUE;
+}
+
+void compose_clear_exit_drafts(void)
+{
+       gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+                                     DRAFTED_AT_EXIT, NULL);
+       if (is_file_exist(filepath))
+               g_unlink(filepath);
+       
+       g_free(filepath);
+}
+
+void compose_reopen_exit_drafts(void)
+{
+       gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+                                     DRAFTED_AT_EXIT, NULL);
+       FILE *fp = fopen(filepath, "rb");
+       gchar buf[1024];
+       
+       if (fp) {
+               while (fgets(buf, sizeof(buf), fp)) {
+                       gchar **parts = g_strsplit(buf, "\t", 2);
+                       const gchar *folder = parts[0];
+                       int msgnum = parts[1] ? atoi(parts[1]):-1;
+                       
+                       if (folder && *folder && msgnum > -1) {
+                               FolderItem *item = folder_find_item_from_identifier(folder);
+                               MsgInfo *info = folder_item_get_msginfo(item, msgnum);
+                               if (info)
+                                       compose_reedit(info, FALSE);
+                       }
+                       g_strfreev(parts);
+               }       
+               fclose(fp);
+       }       
+       g_free(filepath);
+       compose_clear_exit_drafts();
+}
+
+static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
+{
+       compose_draft(data, action);
 }
 
 static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
@@ -9527,16 +9711,14 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
                                        compose);
        if (paste_as_quotation) {
                gchar *new_text;
-               gchar *qmark;
+               const gchar *qmark;
 
                if (len < 0)
                        len = strlen(text);
 
                new_text = g_strndup(text, len);
-               if (prefs_common.quotemark && *prefs_common.quotemark)
-                       qmark = prefs_common.quotemark;
-               else
-                       qmark = "> ";
+
+               qmark = compose_quote_char_from_context(compose);
 
                mark = gtk_text_buffer_create_mark(buffer, NULL, iter, FALSE);
                gtk_text_buffer_place_cursor(buffer, iter);
@@ -9551,7 +9733,7 @@ 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);
        } else {
-               if (strcmp(text, "\n") || automatic_break
+               if (strcmp(text, "\n") || compose->automatic_break
                || gtk_text_iter_starts_line(iter))
                        gtk_text_buffer_insert(buffer, iter, text, len);
                else {
@@ -9574,7 +9756,8 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
        g_signal_stop_emission_by_name(G_OBJECT(buffer), "insert-text");
 
        if (prefs_common.autosave && 
-           gtk_text_buffer_get_char_count(buffer) % prefs_common.autosave_length == 0)
+           gtk_text_buffer_get_char_count(buffer) % prefs_common.autosave_length == 0 &&
+           compose->draft_timeout_tag != -2 /* disabled while loading */)
                compose->draft_timeout_tag = g_timeout_add
                        (500, (GtkFunction) compose_defer_auto_save_draft, compose);
 }
@@ -9680,6 +9863,7 @@ gboolean compose_close(Compose *compose)
                 * drafting to be done, before destroying the compose under
                 * it. */
                debug_print("waiting for drafting to finish...\n");
+               compose_allow_user_actions(compose, FALSE);
                g_timeout_add (500, (GSourceFunc) compose_close, compose);
                return FALSE;
        }
@@ -9731,11 +9915,12 @@ static void compose_reply_from_messageview_real(MessageView *msgview, GSList *ms
                                                orig_msginfo, mimeinfo);
                        if (tmp_msginfo != NULL) {
                                new_msglist = g_slist_append(NULL, tmp_msginfo);
-                               if (procmime_msginfo_is_encrypted(orig_msginfo)) {
-                                       originally_enc = TRUE;
-                               }
+
+                               originally_enc = MSG_IS_ENCRYPTED(orig_msginfo->flags);
                                tmp_msginfo->folder = orig_msginfo->folder;
                                tmp_msginfo->msgnum = orig_msginfo->msgnum; 
+                               if (orig_msginfo->tags)
+                                       tmp_msginfo->tags = g_slist_copy(orig_msginfo->tags);
                        }
                }
        }