sync with sylpheed 0.6.3cvs7
[claws.git] / src / compose.c
index 62729f792afabea9dc5b5c945ac251148c41206f..0ba52c5b9cf5ac36c2c92428e3b40092ff3f04e5 100644 (file)
@@ -442,7 +442,7 @@ static GtkItemFactoryEntry compose_entries[] =
                        "<alt>X", compose_ext_editor_cb, 0, NULL},
 
        {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
-       {N_("/_Message/_Send"),         "<shift><control>S",
+       {N_("/_Message/_Send"),         "<control>Return",
                                        compose_send_cb, 0, NULL},
        {N_("/_Message/Send _later"),   "<shift><alt>S",
                                        compose_send_later_cb,  0, NULL},
@@ -497,7 +497,21 @@ Compose * compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
 Compose * compose_generic_new(PrefsAccount *account, const gchar *to, FolderItem *item)
 {
        Compose *compose;
-
+       GList *cur_ac;
+       GList *account_list;
+       PrefsAccount *ac_prefs;
+
+       if (item && item->prefs->enable_default_account) {
+               /* get a PrefsAccount *pointer on the wished account */
+               account_list=account_get_list();
+               for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
+                       ac_prefs = (PrefsAccount *)cur_ac->data;
+                       if (ac_prefs->account_id == item->prefs->default_account) {
+                               account = ac_prefs;
+                               break;
+                       }
+               }
+       }
        if (!account) account = cur_account;
        g_return_val_if_fail(account != NULL, NULL);
 
@@ -694,6 +708,10 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
        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_stext_thaw(text);
        gtk_widget_grab_focus(compose->text);
 
@@ -1204,22 +1222,26 @@ void compose_entry_append(Compose *compose, const gchar *address,
 
 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
 {
-       static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
-                                      {"Cc:",          NULL, FALSE},
-                                      {"References:",  NULL, FALSE},
-                                      {"Bcc:",         NULL, FALSE},
-                                      {"Newsgroups:",  NULL, FALSE},
-                                      {"Followup-To:", NULL, FALSE},
-                                      {NULL,           NULL, FALSE}};
+       static HeaderEntry hentry[] = {{"Reply-To:",       NULL, TRUE},
+                                      {"Cc:",             NULL, FALSE},
+                                      {"References:",     NULL, FALSE},
+                                      {"Bcc:",            NULL, FALSE},
+                                      {"Newsgroups:",     NULL, FALSE},
+                                      {"Followup-To:",    NULL, FALSE},
+                                      {"X-Mailing-List:", NULL, FALSE},
+                                      {"X-BeenThere:",    NULL, FALSE},
+                                      {NULL,              NULL, FALSE}};
 
        enum
        {
-               H_REPLY_TO      = 0,
-               H_CC            = 1,
-               H_REFERENCES    = 2,
-               H_BCC           = 3,
-               H_NEWSGROUPS    = 4,
-               H_FOLLOWUP_TO   = 5
+               H_REPLY_TO       = 0,
+               H_CC             = 1,
+               H_REFERENCES     = 2,
+               H_BCC            = 3,
+               H_NEWSGROUPS     = 4,
+               H_FOLLOWUP_TO    = 5,
+               H_X_MAILING_LIST = 6,
+               H_X_BEENTHERE    = 7
        };
 
        FILE *fp;
@@ -1240,6 +1262,26 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
                compose->cc = hentry[H_CC].body;
                hentry[H_CC].body = NULL;
        }
+       if (hentry[H_X_MAILING_LIST].body != NULL) {
+               /* this is good enough to parse debian-devel */
+               char * buf = g_malloc(strlen(hentry[H_X_MAILING_LIST].body) + 1);
+               g_return_val_if_fail(buf != NULL, -1 );
+               if (1 == sscanf(hentry[H_X_MAILING_LIST].body, "<%[^>]>", buf))
+                       compose->mailinglist = g_strdup(buf);
+               g_free(buf);
+               g_free(hentry[H_X_MAILING_LIST].body);
+               hentry[H_X_MAILING_LIST].body = NULL ;
+       }
+       if (hentry[H_X_BEENTHERE].body != NULL) {
+               /* this is good enough to parse the sylpheed-claws lists */
+               char * buf = g_malloc(strlen(hentry[H_X_BEENTHERE].body) + 1);
+               g_return_val_if_fail(buf != NULL, -1 );
+               if (1 == sscanf(hentry[H_X_BEENTHERE].body, "%[^>]", buf))
+                       compose->mailinglist = g_strdup(buf);
+               g_free(buf);
+               g_free(hentry[H_X_BEENTHERE].body);
+               hentry[H_X_BEENTHERE].body = NULL ;
+       }
        if (hentry[H_REFERENCES].body != NULL) {
                if (compose->mode == COMPOSE_REEDIT)
                        compose->references = hentry[H_REFERENCES].body;
@@ -1618,7 +1660,9 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
 
        if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
                compose_entry_append(compose,
-                                    ((compose->replyto && !ignore_replyto) 
+                                   ((compose->mailinglist && !ignore_replyto)
+                                    ? compose->mailinglist
+                                    : (compose->replyto && !ignore_replyto)
                                     ? compose->replyto
                                     : msginfo->from ? msginfo->from : ""),
                                     COMPOSE_TO);
@@ -1957,7 +2001,7 @@ static void compose_wrap_line(Compose *compose)
 {
        GtkSText *text = GTK_STEXT(compose->text);
        gint ch_len, last_ch_len;
-       gchar cbuf[MB_CUR_MAX], last_ch;
+       gchar cbuf[MB_LEN_MAX], last_ch;
        guint text_len;
        guint line_end;
        guint quoted;
@@ -2122,20 +2166,149 @@ compose_end:
 #undef GET_STEXT
 }
 
+/* return str length if text at start_pos matches str else return zero */
+static guint gtkstext_str_strcmp(GtkSText *text, guint start_pos,
+                                guint text_len, gchar *str) {
+       guint is_str, i, str_len;
+       gchar str_ch;
+
+       is_str = 0;
+       if (str) {
+               str_len = strlen(str);
+               is_str = 1;
+               for (i = 0; (i < str_len) && (start_pos + i < text_len); i++) {
+                       str_ch = GTK_STEXT_INDEX(text, start_pos + i);
+                       if (*(str + i) != str_ch) {
+                               break;
+                       }
+               }
+               if (i == 0 || i < str_len)
+                       is_str = 0;
+       }
+
+       return is_str ? str_len : 0;
+}
+
+/* return indent length */
+static guint get_indent_length(GtkSText *text, guint start_pos,
+                              guint text_len) {
+       gint indent_len = 0;
+       gint i, ch_len;
+       gchar cbuf[MB_CUR_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;
+               }
+               if (ch_len > 1)
+                       break;
+               /* allow space, tab, > or | */
+               if (cbuf[0] != ' ' && cbuf[0] != '\t' &&
+                   cbuf[0] != '>' && cbuf[0] != '|')
+                       break;
+               indent_len++;
+       }
+
+       return indent_len;
+}
+
+/* insert quotation string when line was wrapped, we know these
+   are single byte characters */
+static guint insert_quote(GtkSText *text, guint quote_len, guint indent_len,
+                         guint prev_line_pos, guint text_len,
+                         gchar *quote_fmt) {
+       guint i, ins_len;
+       gchar ch;
+
+       if (indent_len) {
+               for (i = 0; i < indent_len; i++) {
+                       ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
+                       gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
+               }
+               ins_len = indent_len;
+       }
+       else {
+               gtk_stext_insert(text, NULL, NULL, NULL, quote_fmt, quote_len);
+               ins_len = quote_len;
+       }
+
+       return ins_len;
+}
+
+/* compare gtkstext string at pos1 with string at pos2 for equality
+   (max. len chars) - we treat characters as single byte */
+static gint gtkstext_strncmp(GtkSText *text, guint pos1, guint pos2, guint len,
+                            guint tlen)
+{
+       guint i = 0;
+       gchar ch1, ch2;
+
+       for (; (i < len) && (pos1 + i < tlen) && (pos2 + i < tlen); i++) {
+               ch1 = GTK_STEXT_INDEX(text, pos1 + i);
+               ch2 = GTK_STEXT_INDEX(text, pos2 + i);
+               if (ch1 != ch2)
+                       break;
+       }
+
+       return i;
+}
+
+/* return true if text at pos is URL */
+static guint is_url_string(GtkSText *text, guint start_pos, guint text_len)
+{
+       guint len;
+
+       len = gtkstext_str_strcmp(text, start_pos, text_len, "ftp://");
+       if (len == 6)
+               return 1;
+       len = gtkstext_str_strcmp(text, start_pos, text_len, "http://");
+       if (len == 7)
+               return 1;
+       len = gtkstext_str_strcmp(text, start_pos, text_len, "https://");
+       if (len == 8)
+               return 1;
+
+       return 0;
+}
+
 static void compose_wrap_line_all(Compose *compose)
 {
        GtkSText *text = GTK_STEXT(compose->text);
        guint text_len;
-       guint line_pos = 0, cur_pos = 0;
+       guint line_pos = 0, cur_pos = 0, prev_line_pos = 0;
        gint line_len = 0, cur_len = 0;
        gint ch_len;
-       gchar cbuf[MB_CUR_MAX];
+       gint is_new_line = 1, do_delete = 0;
+       guint quote_len = 0, indent_len = 0;
+       guint linewrap_quote = prefs_common.linewrap_quote;
+       guint linewrap_len = prefs_common.linewrap_len;
+       gchar *quote_fmt = prefs_common.quotemark;
+       gchar cbuf[MB_LEN_MAX];
 
        gtk_stext_freeze(text);
 
+       /* make text buffer contiguous */
+       gtk_stext_compact_buffer(text);
+
        text_len = gtk_stext_get_length(text);
 
        for (; cur_pos < text_len; cur_pos++) {
+               if (linewrap_quote && is_new_line) {
+                       quote_len = gtkstext_str_strcmp(text, cur_pos,
+                                                       text_len, quote_fmt);
+                       is_new_line = 0;
+                       if (quote_len) {
+                               indent_len = get_indent_length(text, cur_pos,
+                                                              text_len);
+                       }
+                       else
+                               indent_len = 0;
+                       prev_line_pos = cur_pos;
+               }
                if (text->use_wchar)
                        ch_len = wctomb
                                (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
@@ -2144,9 +2317,103 @@ static void compose_wrap_line_all(Compose *compose)
                        ch_len = 1;
                }
 
+               /* fix line length for tabs */
+               if (ch_len == 1 && *cbuf == '\t') {
+                       guint tab_width = text->default_tab_width;
+                       guint tab_offset = line_len % tab_width;
+
+                       if (tab_offset) {
+                               line_len += tab_width - tab_offset - 1;
+                               cur_len = line_len;
+                       }
+               }
+
                if (ch_len == 1 && *cbuf == '\n') {
+                       gint clen;
+                       guint ilen;
+                       gchar cb[MB_CUR_MAX];
+
+                       /* if it's just quotation + newline skip it */
+                       if (indent_len && (cur_pos + 1 < text_len)) {
+                               ilen =  gtkstext_strncmp(text,
+                                                        cur_pos + 1,
+                                                        prev_line_pos,
+                                                        indent_len,
+                                                        text_len);
+                               if (cur_pos + ilen < text_len) {
+                                       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;
+                                       }
+                                       if ((clen == 1) && (cb[0] == '\n'))
+                                               do_delete = 0;
+                               }
+                       }
+
+                       /* should we delete to perform smart wrapping */
+                       if (quote_len && line_len < linewrap_len && do_delete) {
+                               /* get rid of newline */
+                               gtk_stext_set_point(text, cur_pos);
+                               gtk_stext_forward_delete(text, 1);
+
+                               /* if text starts with quote_fmt or with
+                                  indent string, delete them */
+                               if (indent_len) {
+                                       ilen =  gtkstext_strncmp(text, cur_pos,
+                                                                prev_line_pos,
+                                                                indent_len,
+                                                                text_len);
+                                       if (ilen) {
+                                               gtk_stext_forward_delete(text,
+                                                                        ilen);
+                                               text_len -= ilen;
+                                       }
+                               }
+                               else if (quote_len) {
+                                       if (gtkstext_str_strcmp(text, cur_pos,
+                                                               text_len,
+                                                               quote_fmt)) {
+                                               gtk_stext_forward_delete(text,
+                                                                quote_len);
+                                               text_len-=quote_len;
+                                       }
+                               }
+
+                               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;
+                               }
+                               /* insert space if it's alphanumeric */
+                               if ((cur_pos != line_pos) &&
+                                   ((clen > 1) || isalnum(cb[0]))) {
+                                       gtk_stext_insert(text, NULL, NULL,
+                                                        NULL, " ", 1);
+                                       gtk_stext_compact_buffer(text);
+                                       text_len++;
+                               }
+
+                               /* and start over with current line */
+                               cur_pos = prev_line_pos - 1;
+                               line_pos = cur_pos;
+                               line_len = cur_len = 0;
+                               quote_len = 0;
+                               do_delete = 0;
+                               is_new_line = 1;
+                               continue;
+                       }
+
                        line_pos = cur_pos + 1;
                        line_len = cur_len = 0;
+                       quote_len = 0;
+                       do_delete = 0;
+                       is_new_line = 1;
                        continue;
                }
 
@@ -2160,10 +2427,17 @@ static void compose_wrap_line_all(Compose *compose)
                        line_len = cur_len + ch_len;
                }
 
-               if (cur_len + ch_len > prefs_common.linewrap_len &&
-                   line_len > 0) {
+               if (cur_len + ch_len > linewrap_len) {
                        gint tlen;
 
+                       if (line_len == 0) {
+                               /* don't wrap URLs */
+                               if (is_url_string(text, line_pos, text_len))
+                                       continue;
+                               line_len = cur_pos - line_pos;
+                               line_pos = cur_pos;
+                       }
+
                        if (text->use_wchar)
                                tlen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
                        else {
@@ -2182,11 +2456,35 @@ static void compose_wrap_line_all(Compose *compose)
 
                        gtk_stext_set_point(text, line_pos);
                        gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
+                       gtk_stext_compact_buffer(text);
                        text_len++;
                        cur_pos++;
                        line_pos++;
                        cur_len = cur_len - line_len + ch_len;
                        line_len = 0;
+                       if (linewrap_quote && quote_len) {
+                               /* only if line is not already quoted */
+                               if (!gtkstext_str_strcmp(text, line_pos,
+                                                        text_len, quote_fmt)) {
+                                       guint i_len;
+
+                                       i_len = insert_quote(text, quote_len,
+                                                            indent_len,
+                                                            prev_line_pos,
+                                                            text_len,
+                                                            quote_fmt);
+
+                                       gtk_stext_compact_buffer(text);
+                                       text_len += i_len;
+                                       /* for loop above will increase it */
+                                       cur_pos = line_pos - 1;
+                                       cur_len = 0;
+                                       line_len = 0;
+                                       /* start over with current line */
+                                       is_new_line = 1;
+                                       do_delete = 1;
+                               }
+                       }
                        continue;
                }
 
@@ -2413,18 +2711,22 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
        len = strlen(chars);
        if (is_ascii_str(chars)) {
                buf = g_strdup(chars);
-               out_codeset = "US-ASCII";
+               out_codeset = CS_US_ASCII;
                encoding = ENC_7BIT;
        } else {
                const gchar *src_codeset;
 
                out_codeset = conv_get_outgoing_charset_str();
-               if (!strcasecmp(out_codeset, "US-ASCII"))
-                       out_codeset = "ISO-8859-1";
+               if (!strcasecmp(out_codeset, CS_US_ASCII))
+                       out_codeset = CS_ISO_8859_1;
                encoding = procmime_get_encoding_for_charset(out_codeset);
+
                src_codeset = conv_get_current_charset_str();
-               if (!strcasecmp(src_codeset, "US-ASCII"))
-                       src_codeset = "ISO-8859-1";
+               /* if current encoding is US-ASCII, set it the same as
+                  outgoing one to prevent code conversion failure */
+               if (!strcasecmp(src_codeset, CS_US_ASCII))
+                       src_codeset = out_codeset;
+
                debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
                            src_codeset, out_codeset, procmime_get_encoding_str(encoding));
 
@@ -3743,6 +4045,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        compose->mode = mode;
 
        compose->replyto     = NULL;
+       compose->mailinglist = NULL;
        compose->cc          = NULL;
        compose->bcc         = NULL;
        compose->followup_to = NULL;
@@ -4623,7 +4926,8 @@ static void compose_set_ext_editor_sensitive(Compose *compose,
                           sensitive);
        menu_set_sensitive(ifactory, "/File/Insert file", sensitive);
        menu_set_sensitive(ifactory, "/File/Insert signature", sensitive);
-       menu_set_sensitive(ifactory, "/Edit/Wrap long lines", sensitive);
+       menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", sensitive);
+       menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", sensitive);
        menu_set_sensitive(ifactory, "/Edit/Edit with external editor",
                           sensitive);