newsgroups dialog / enhanced mailto
[claws.git] / src / compose.c
index 7dd6439991d150ffaceb4d0ad73b6e96072e64ba..4c51dc772bb1b130277e77bee6c77f13ddf6c1b3 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},
@@ -494,11 +494,94 @@ Compose * compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
        return compose_generic_new(account, NULL, item);
 }
 
+static void set_compose_entries(Compose * compose, const gchar * mailto)
+{
+       gchar * subject = NULL;
+       gchar * to = NULL;
+       gchar * cc = NULL;
+       gchar * bcc = NULL;
+       gchar * body = NULL;
+       gchar * p;
+       gchar * tmp_mailto;
+       gchar * cur;
+       
+       Xstrdup_a(tmp_mailto, mailto, return);
+
+       cur = tmp_mailto;
+
+       p = strchr(cur, '?');
+       if (p != NULL) {
+               * p = 0;
+               cur = p + 1;
+       }
+       to = tmp_mailto;
+
+       while (p) {
+               char *field, *value;
+               
+               field = cur;
+
+               p = strchr(cur, '=');
+               if (p == NULL)
+                       break;
+               * p = 0;
+               cur = p + 1;
+
+               value = cur;
+
+               p = strchr(cur, '&');
+               if (p != NULL) {
+                       * p = 0;
+                       cur = p + 1;
+               }
+
+               if (value) {
+                       if (g_strcasecmp(field, "subject")==0) {
+                               Xstrdup_a(subject, value, );
+                               if (subject != NULL)
+                                       decode_uri(subject, value);
+                       }
+                       else if (g_strcasecmp(field, "cc")==0)
+                               cc = value;
+                       else if (g_strcasecmp(field, "bcc")==0)
+                               bcc = value;
+                       else if (g_strcasecmp(field, "body")==0) {
+                               Xstrdup_a(body, value, );
+                               if (body != NULL)
+                                       decode_uri(body, value);
+                       }
+               }
+       }                       
+
+       if (to) {
+               compose_entry_append(compose, to, COMPOSE_TO);
+               /*
+               gtk_widget_grab_focus(compose->text);
+               */
+       }
+
+       if (subject)
+               gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
+       if (cc)
+               compose_entry_append(compose, cc, COMPOSE_CC);
+       if (bcc)
+               compose_entry_append(compose, bcc, COMPOSE_BCC);
+       if (body) {
+               gtk_text_insert(GTK_TEXT(compose->text),
+                               NULL, NULL, NULL, body, -1);
+               gtk_text_insert(GTK_TEXT(compose->text),
+                               NULL, NULL, NULL, "\n", 1);
+       }
+}      
+
 Compose * compose_generic_new(PrefsAccount *account, const gchar *to, FolderItem *item)
 {
        Compose *compose;
 
-       if (!account) account = cur_account;
+       if (item && item->prefs && item->prefs->enable_default_account)
+               account = account_find_from_id(item->prefs->default_account);
+
+       if (!account) account = cur_account;
        g_return_val_if_fail(account != NULL, NULL);
 
        compose = compose_create(account, COMPOSE_NEW);
@@ -511,14 +594,10 @@ Compose * compose_generic_new(PrefsAccount *account, const gchar *to, FolderItem
 
        if (account->protocol != A_NNTP) {
                if (to) {
-                       compose_entry_append(compose, to, COMPOSE_TO);
-                       gtk_widget_grab_focus(compose->subject_entry);
-               } else {
-                       if(item && item->prefs->enable_default_to) {
-                               compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
-                       } else {
-                               gtk_widget_grab_focus(compose->header_last->entry);
-                       }
+                       set_compose_entries(compose, to);
+
+               } else if(item && item->prefs->enable_default_to) {
+                       compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
                }
                if (item && item->ret_rcpt) {
                        GtkItemFactory *ifactory;
@@ -529,10 +608,9 @@ Compose * compose_generic_new(PrefsAccount *account, const gchar *to, FolderItem
        } else {
                if (to) {
                        compose_entry_append(compose, to, COMPOSE_NEWSGROUPS);
-                       gtk_widget_grab_focus(compose->subject_entry);
-               } else
-                       gtk_widget_grab_focus(compose->header_last->entry);
+               }
        }
+       gtk_widget_grab_focus(compose->subject_entry);
 
        if (prefs_common.auto_exteditor)
                compose_exec_ext_editor(compose);
@@ -599,21 +677,33 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
        g_return_if_fail(msginfo != NULL);
        g_return_if_fail(msginfo->folder != NULL);
 
-       account = msginfo->folder->folder->account;
-       if (!account && msginfo->to && prefs_common.reply_account_autosel) {
-               gchar *to;
-               Xstrdup_a(to, msginfo->to, return);
-               extract_address(to);
-               account = account_find_from_address(to);
-       }
-        if(!account&& prefs_common.reply_account_autosel) {
-                       gchar cc[BUFFSIZE];
-               if(!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
-                       extract_address(cc);
-                       account = account_find_from_address(cc);
-                }        
+       account = NULL;
+       /* select the account set in folderitem's property (if enabled) */
+       if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
+               account = account_find_from_id(msginfo->folder->prefs->default_account);
+       
+       /* select the account for the whole folder (IMAP / NNTP) */
+       if (!account)
+               account = msginfo->folder->folder->account;
+
+       /* select account by to: and cc: header if enabled */
+       if (prefs_common.reply_account_autosel) {
+               if (!account && msginfo->to) {
+                       gchar *to;
+                       Xstrdup_a(to, msginfo->to, return);
+                       extract_address(to);
+                       account = account_find_from_address(to);
+               }
+               if (!account) {
+                       gchar cc[BUFFSIZE];
+                       if(!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
+                               extract_address(cc);
+                               account = account_find_from_address(cc);
+                       }        
+               }
        }
 
+       /* select current account */
        if (!account) account = cur_account;
        g_return_if_fail(account != NULL);
 
@@ -627,6 +717,7 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
                        return;
        } else
                reply_account = account;
+
        MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
        MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
 
@@ -694,6 +785,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);
 
@@ -1027,6 +1122,7 @@ Compose *compose_forward(PrefsAccount * account, MsgInfo *msginfo,
        else
                gtk_widget_grab_focus(compose->newsgroups_entry);
 #endif
+       gtk_widget_grab_focus(compose->header_last->entry);
 
        if (prefs_common.auto_exteditor)
                compose_exec_ext_editor(compose);
@@ -1649,11 +1745,23 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
                                     : msginfo->from ? msginfo->from : ""),
                                     COMPOSE_TO);
        if (compose->account->protocol == A_NNTP)
+               if (ignore_replyto)
+                       compose_entry_append(compose,
+                                            msginfo->from ? msginfo->from : "",
+                                            COMPOSE_TO);
+               else
+                       compose_entry_append(compose,
+                                            compose->followup_to ? compose->followup_to
+                                            : compose->newsgroups ? compose->newsgroups
+                                            : "",
+                                            COMPOSE_NEWSGROUPS);
+       /*
                compose_entry_append(compose,
                                     compose->followup_to ? compose->followup_to
                                     : compose->newsgroups ? compose->newsgroups
                                     : "",
                                     COMPOSE_NEWSGROUPS);
+       */
 
        if (msginfo->subject && *msginfo->subject) {
                gchar *buf, *buf2, *p;
@@ -1840,6 +1948,7 @@ static void compose_insert_file(Compose *compose, const gchar *file)
 {
        GtkSText *text = GTK_STEXT(compose->text);
        gchar buf[BUFFSIZE];
+       gint len;
        FILE *fp;
 
        g_return_if_fail(file != NULL);
@@ -1851,8 +1960,20 @@ static void compose_insert_file(Compose *compose, const gchar *file)
 
        gtk_stext_freeze(text);
 
-       while (fgets(buf, sizeof(buf), fp) != NULL)
-               gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               /* Strip <CR> if DOS/Windoze file, replace <CR> with <LF> if MAC file */
+               len = strlen(buf);
+               if (len > 1 && buf[len - 2] == '\r' && buf[len - 1] == '\n') {
+                       buf[len - 2] = '\n';
+                       buf[len - 1] = '\0';
+               } else {
+                       while (--len > 0)
+                               if (buf[len] == '\r')
+                                       buf[len] = '\n';
+               }
+
+               gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
+       }
 
        gtk_stext_thaw(text);
 
@@ -1983,7 +2104,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;
@@ -2148,55 +2269,115 @@ compose_end:
 #undef GET_STEXT
 }
 
-/* return str length if text at start_pos matches str else return zero */
-static gint is_gtkstext_string(GtkSText *text, guint start_pos,
-                              guint text_len, gchar *str) {
-       gint is_str, i, str_len;
-       gchar str_ch;
+/* 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];
 
-       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;
-                       }
+       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 ins_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);
                }
-               if (i == 0 || i < str_len)
-                       is_str = 0;
+               ins_len = indent_len;
+       }
+       else {
+               gtk_stext_insert(text, NULL, NULL, NULL, quote_fmt, quote_len);
+               ins_len = quote_len;
        }
 
-       return is_str ? str_len : 0;
+       return ins_len;
 }
 
+#undef WRAP_DEBUG
+#ifdef WRAP_DEBUG
+/* Darko: used when I debug wrapping */
+void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
+{
+    int i;
+    char ch;
+
+    printf("%d [", pos);
+    for (i = pos; i < tlen; i++)
+    {
+        ch = GTK_STEXT_INDEX(text, i);
+        if (breakoncr && ch == '\n')
+            break;
+        printf("%c", ch);
+    }
+    printf("]\n");
+}
+#endif
+
 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 tlen;
+       guint line_pos = 0, cur_pos = 0, p_pos = 0;
        gint line_len = 0, cur_len = 0;
        gint ch_len;
-       gint is_new_line = 1;
-       guint quote_len = 0;
+       gint is_new_line = 1, do_delete = 0;
+       guint qlen = 0, i_len = 0;
        guint linewrap_quote = prefs_common.linewrap_quote;
-       gchar *quote_fmt = prefs_common.quotemark;
-       gchar cbuf[MB_CUR_MAX];
+       guint linewrap_len = prefs_common.linewrap_len;
+       gchar *qfmt = 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);
+       tlen = gtk_stext_get_length(text);
 
-       for (; cur_pos < text_len; cur_pos++) {
+       for (; cur_pos < tlen; cur_pos++) {
+               /* mark position of new line - needed for quotation wrap */
                if (linewrap_quote && is_new_line) {
-                       quote_len = is_gtkstext_string(text, cur_pos,
-                                                      text_len, quote_fmt);
+                       qlen = gtkstext_str_strcmp(text, cur_pos, tlen, qfmt);
                        is_new_line = 0;
+                       if (qlen) {
+                               i_len = get_indent_length(text, cur_pos, tlen);
+                       }
+                       else
+                               i_len = 0;
+                       p_pos = cur_pos;
+#ifdef WRAP_DEBUG
+                       printf("new line i_len=%d qlen=%d p_pos=", i_len, qlen);
+                       dump_text(text, p_pos, tlen, 1);
+#endif
                }
+
+               /* get character(s) at current position */
                if (text->use_wchar)
                        ch_len = wctomb
                                (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
@@ -2205,10 +2386,120 @@ 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;
+
+#ifdef WRAP_DEBUG
+                       printf("found tab at pos=%d line_len=%d ", cur_pos,
+                               line_len);
+#endif
+                       if (tab_offset) {
+                               line_len += tab_width - tab_offset - 1;
+                               cur_len = line_len;
+                       }
+#ifdef WRAP_DEBUG
+                       printf("new_len=%d\n", line_len);
+#endif
+               }
+
+               /* we have encountered line break */
                if (ch_len == 1 && *cbuf == '\n') {
+                       gint clen;
+                       guint ilen;
+                       gchar cb[MB_CUR_MAX];
+
+#ifdef WRAP_DEBUG
+                       printf("found CR at %d\n", cur_pos);
+#endif
+                       /* if it's just quotation + newline skip it */
+                       if (i_len && (cur_pos + 1 < tlen)) {
+                               /* check if text at new line matches indent */
+                               ilen =  gtkstext_strncmp(text, cur_pos + 1,
+                                                        p_pos, i_len, tlen);
+                               if (cur_pos + ilen < tlen) {
+                                       if (text->use_wchar)
+                                               clen = wctomb(cb, (wchar_t)GTK_STEXT_INDEX(text, cur_pos + ilen + 1));
+                                       else {
+                                               cb[0] = GTK_STEXT_INDEX(text,
+                                                           cur_pos + ilen + 1);
+                                               clen = 1;
+                                       }
+                                       /* no need to join the lines */
+                                       if ((clen == 1) && (cb[0] == '\n'))
+                                               do_delete = 0;
+                               }
+                       }
+
+#ifdef WRAP_DEBUG
+                       printf("qlen=%d l_len=%d wrap_len=%d do_del=%d\n",
+                               qlen, line_len, linewrap_len, do_delete);
+#endif
+                       /* should we delete to perform smart wrapping */
+                       if (qlen && line_len < linewrap_len && do_delete) {
+                               /* get rid of newline */
+                               gtk_stext_set_point(text, cur_pos);
+                               gtk_stext_forward_delete(text, 1);
+                               tlen--;
+
+                               /* if text starts with quote fmt or with
+                                  indent string, delete them */
+                               if (i_len) {
+                                       ilen =  gtkstext_strncmp(text, cur_pos,
+                                                                p_pos, i_len,
+                                                                tlen);
+                                       if (ilen) {
+                                               gtk_stext_forward_delete(text,
+                                                                        ilen);
+                                               tlen -= ilen;
+                                       }
+                               }
+                               else if (qlen) {
+                                       if (gtkstext_str_strcmp(text, cur_pos,
+                                                               tlen, qfmt)) {
+                                               gtk_stext_forward_delete(text,
+                                                                qlen);
+                                               tlen -= qlen;
+                                       }
+                               }
+
+                               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);
+                                       tlen++;
+                               }
+
+                               /* and start over with current line */
+                               cur_pos = p_pos - 1;
+                               line_pos = cur_pos;
+                               line_len = cur_len = 0;
+                               qlen = 0;
+                               do_delete = 0;
+                               is_new_line = 1;
+#ifdef WRAP_DEBUG
+                               printf("after delete l_pos=");
+                               dump_text(text, line_pos, tlen, 1);
+#endif
+                               continue;
+                       }
+
+                       /* mark new line beginning */
                        line_pos = cur_pos + 1;
                        line_len = cur_len = 0;
-                       quote_len = 0;
+                       qlen = 0;
+                       do_delete = 0;
                        is_new_line = 1;
                        continue;
                }
@@ -2218,49 +2509,103 @@ static void compose_wrap_line_all(Compose *compose)
                        ch_len = 1;
                }
 
+               /* possible line break */
                if (ch_len == 1 && isspace(*cbuf)) {
                        line_pos = cur_pos + 1;
                        line_len = cur_len + ch_len;
                }
 
-               if (cur_len + ch_len > prefs_common.linewrap_len &&
-                   line_len > 0) {
-                       gint tlen;
+               /* are we over wrapping length set in preferences ? */
+               if (cur_len + ch_len > linewrap_len) {
+                       gint clen;
+
+#ifdef WRAP_DEBUG
+                       printf("should wrap cur_pos=%d ", cur_pos);
+                       dump_text(text, p_pos, tlen, 1);
+                       dump_text(text, line_pos, tlen, 1);
+#endif
+                       /* force wrapping if it is one long word but not URL */
+                       if (p_pos + i_len == line_pos)
+                               if (!is_url_string(text, line_pos, tlen))
+                                       line_pos = cur_pos - 1;
+#ifdef WRAP_DEBUG
+                       printf("new line_pos=%d\n", line_pos);
+#endif
 
+                       /* if next character is space delete it */
                        if (text->use_wchar)
-                               tlen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
+                               clen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
                        else {
                                cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
-                               tlen = 1;
+                               clen = 1;
                        }
-                       if (tlen == 1 && isspace(*cbuf)) {
-                               gtk_stext_set_point(text, line_pos);
-                               gtk_stext_backward_delete(text, 1);
-                               text_len--;
-                               cur_pos--;
-                               line_pos--;
-                               cur_len--;
-                               line_len--;
+                        if (clen == 1 && isspace(*cbuf)) {
+                               if (p_pos + i_len != line_pos ||
+                                   !is_url_string(text, line_pos, tlen)) {
+                                       gtk_stext_set_point(text, line_pos);
+                                       gtk_stext_backward_delete(text, 1);
+                                       tlen--;
+                                       cur_pos--;
+                                       line_pos--;
+                                       cur_len--;
+                                       line_len--;
+                               }
                        }
 
+                       /* if it is URL at beginning of line don't wrap */
+                       if (p_pos + i_len == line_pos &&
+                            is_url_string(text, line_pos, tlen)) {
+#ifdef WRAP_DEBUG
+                               printf("found URL at ");
+                               dump_text(text, line_pos, tlen, 1);
+#endif
+                               continue;
+                       }
+
+                       /* insert CR */
                        gtk_stext_set_point(text, line_pos);
                        gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
-                       text_len++;
+                       gtk_stext_compact_buffer(text);
+                       tlen++;
                        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 (!is_gtkstext_string(text, line_pos,
-                                                       text_len, quote_fmt)) {
-                                       gtk_stext_insert(text, NULL, NULL, NULL,
-                                                       quote_fmt, quote_len);
-                                       gtk_stext_compact_buffer(text);
-                                       text_len += quote_len;
-                                       cur_pos += quote_len;
-                                       cur_len += quote_len;
-                                       line_len = quote_len;
+#ifdef WRAP_DEBUG
+                       printf("after CR insert ");
+                       dump_text(text, line_pos, tlen, 1);
+#endif
+
+                       /* should we insert quotation ? */
+                       if (linewrap_quote && qlen) {
+                               /* only if line is not already quoted  */
+                               if (!gtkstext_str_strcmp(text, line_pos,
+                                                        tlen, qfmt)) {
+                                       guint ins_len;
+
+                                       if (line_pos - p_pos > i_len) {
+                                               ins_len = ins_quote(text,
+                                                                   qlen,
+                                                                   i_len,
+                                                                   p_pos,
+                                                                   tlen,
+                                                                   qfmt);
+
+                                               gtk_stext_compact_buffer(text);
+                                               tlen += ins_len;
+                                       }
+
+                                       /* for loop 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;
+#ifdef WRAP_DEBUG
+                                       printf("after quote insert ");
+                                       dump_text(text, line_pos, tlen, 1);
+#endif
                                }
                        }
                        continue;
@@ -2270,6 +2615,7 @@ static void compose_wrap_line_all(Compose *compose)
                        line_pos = cur_pos + 1;
                        line_len = cur_len + ch_len;
                }
+               /* advance to next character in buffer */
                cur_len += ch_len;
        }
 
@@ -2328,14 +2674,16 @@ gboolean compose_check_for_valid_recipient(Compose *compose) {
                gchar *header;
                gchar *entry;
                header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(((compose_headerentry *)list->data)->combo)->entry));
-               entry = gtk_entry_get_text(GTK_ENTRY(((compose_headerentry *)list->data)->entry));
-               if(strlen(entry)) {
+               entry = gtk_editable_get_chars(GTK_EDITABLE(((compose_headerentry *)list->data)->entry), 0, -1);
+               g_strstrip(entry);
+               if(entry[0] != '\0') {
                        for(strptr = recipient_headers; *strptr != NULL; strptr++) {
                                if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
                                        recipient_found = TRUE;
                                }
                        }
                }
+               g_free(entry);
        }
        return recipient_found;
 }
@@ -2489,18 +2837,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));
 
@@ -2915,18 +3267,20 @@ static gint compose_write_headers_from_headerlist(Compose *compose,
                        str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
                        Xstrdup_a(str, str, return -1);
                        g_strstrip(str);
-                       if(list_append_func)
-                               *dest_list = list_append_func(*dest_list, str);
-                       compose_convert_header
-                               (buf, sizeof(buf), str,
-                               strlen(header) + 2);
-                       if(first_address) {
-                               fprintf(fp, "%s: ", header);
-                               first_address = FALSE;
-                       } else {
-                               fprintf(fp, ", ");
+                       if(str[0] != '\0') {
+                               if(list_append_func)
+                                       *dest_list = list_append_func(*dest_list, str);
+                               compose_convert_header
+                                       (buf, sizeof(buf), str,
+                                       strlen(header) + 2);
+                               if(first_address) {
+                                       fprintf(fp, "%s: ", header);
+                                       first_address = FALSE;
+                               } else {
+                                       fprintf(fp, ", ");
+                               }
+                               fprintf(fp, "%s", buf);
                        }
-                       fprintf(fp, "%s", buf);
                }
        }
        if(!first_address) {
@@ -3903,6 +4257,12 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
                                   account->auto_replyto);
 #endif
        }
+       if (account->protocol != A_NNTP) {
+               gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry), _("To:"));
+       } else {
+               gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry), _("Newsgroups:"));
+       }
+
        menuitem = gtk_item_factory_get_item(ifactory, "/Tool/Show ruler");
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
                                       prefs_common.show_ruler);
@@ -4700,7 +5060,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);