2005-02-23 [colin] 1.0.1cvs15.6
[claws.git] / src / compose.c
index f3789f0ace9144c66362768b676426a700e1dc72..aab311b75685385477ff289ff58752deb6315b5b 100644 (file)
 
 #include "defs.h"
 
+#ifndef PANGO_ENABLE_ENGINE
+#  define PANGO_ENABLE_ENGINE
+#endif
+
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
@@ -53,6 +57,7 @@
 
 #include <gtk/gtkdnd.h>
 #include <gtk/gtkclipboard.h>
+#include <pango/pango-break.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -220,21 +225,26 @@ static void compose_reply_set_entry               (Compose        *compose,
                                                 followup_and_reply_to);
 static void compose_reedit_set_entry           (Compose        *compose,
                                                 MsgInfo        *msginfo);
+
 static void compose_insert_sig                 (Compose        *compose,
                                                 gboolean        replace);
 static gchar *compose_get_signature_str                (Compose        *compose);
 static ComposeInsertResult compose_insert_file (Compose        *compose,
                                                 const gchar    *file);
+
 static void compose_attach_append              (Compose        *compose,
                                                 const gchar    *file,
                                                 const gchar    *type,
                                                 const gchar    *content_type);
 static void compose_attach_parts               (Compose        *compose,
                                                 MsgInfo        *msginfo);
-static void compose_wrap_line                  (Compose        *compose);
-static void compose_wrap_line_all              (Compose        *compose);
-static void compose_wrap_line_all_full         (Compose        *compose,
+
+static void compose_wrap_paragraph             (Compose        *compose,
+                                                GtkTextIter    *par_iter);
+static void compose_wrap_all                   (Compose        *compose);
+static void compose_wrap_all_full              (Compose        *compose,
                                                 gboolean        autowrap);
+
 static void compose_set_title                  (Compose        *compose);
 static void compose_select_account             (Compose        *compose,
                                                 PrefsAccount   *account,
@@ -300,10 +310,6 @@ static void compose_undo_state_changed             (UndoMain       *undostruct,
                                                 gint            redo_state,
                                                 gpointer        data);
 
-static gint calc_cursor_xpos   (GtkTextView    *text,
-                                gint            extra,
-                                gint            char_width);
-
 static void compose_create_header_entry        (Compose *compose);
 static void compose_add_header_entry   (Compose *compose, gchar *header, gchar *text);
 static void compose_update_priority_menu_item(Compose * compose);
@@ -389,6 +395,9 @@ static void compose_grab_focus_before_cb(GtkWidget  *widget,
 static void compose_changed_cb         (GtkTextBuffer  *textbuf,
                                         Compose        *compose);
 
+static void compose_wrap_cb            (gpointer        data,
+                                        guint           action,
+                                        GtkWidget      *widget);
 static void compose_toggle_autowrap_cb (gpointer        data,
                                         guint           action,
                                         GtkWidget      *widget);
@@ -629,9 +638,9 @@ static GtkItemFactoryEntry compose_entries[] =
                                        NULL},
        {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
        {N_("/_Edit/_Wrap current paragraph"),
-                                       "<control>L", compose_wrap_line, 0, NULL},
+                                       "<control>L", compose_wrap_cb, 0, NULL},
        {N_("/_Edit/Wrap all long _lines"),
-                                       "<control><alt>L", compose_wrap_line_all, 0, NULL},
+                                       "<control><alt>L", compose_wrap_cb, 1, NULL},
        {N_("/_Edit/Aut_o wrapping"),   "<shift><control>L", compose_toggle_autowrap_cb, 0, "<ToggleItem>"},
        {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
        {N_("/_Edit/Edit with e_xternal editor"),
@@ -1035,7 +1044,7 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
        gtk_text_buffer_place_cursor(textbuf, &iter);
        
        if (quote && prefs_common.linewrap_quote)
-               compose_wrap_line_all(compose);
+               compose_wrap_all(compose);
 
        gtk_widget_grab_focus(compose->text);
 
@@ -1129,7 +1138,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                compose_insert_sig(compose, FALSE);
 
        if (prefs_common.linewrap_quote)
-               compose_wrap_line_all(compose);
+               compose_wrap_all(compose);
 
        gtk_text_buffer_get_start_iter(textbuf, &iter);
        gtk_text_buffer_place_cursor(textbuf, &iter);
@@ -1207,7 +1216,7 @@ Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
                compose_insert_sig(compose, FALSE);
 
        if (prefs_common.linewrap_quote)
-               compose_wrap_line_all(compose);
+               compose_wrap_all(compose);
 
        gtk_text_buffer_get_start_iter(textbuf, &iter);
        gtk_text_buffer_place_cursor(textbuf, &iter);
@@ -1557,10 +1566,10 @@ void compose_toolbar_cb(gint action, gpointer data)
                compose_ext_editor_cb(compose, 0, NULL);
                break;
        case A_LINEWRAP_CURRENT:
-               compose_wrap_line(compose);
+               compose_wrap_paragraph(compose, NULL);
                break;
        case A_LINEWRAP_ALL:
-               compose_wrap_line_all(compose);
+               compose_wrap_all(compose);
                break;
        case A_ADDRBOOK:
                compose_address_cb(compose, 0, NULL);
@@ -2435,341 +2444,170 @@ static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
        procmime_mimeinfo_free_all(mimeinfo);
 }
 
-  
-#define CHAR_BUF_SIZE 8
 #undef NEXT_PART_NOT_CHILD
 
 
-#define GET_CHAR(iter_p, buf, len)                             \
-{                                                              \
-       gunichar uc;                                            \
-                                                               \
-       uc = gtk_text_iter_get_char(iter_p);                    \
-       if (uc != 0)                                            \
-               len = g_unichar_to_utf8(uc, buf) > 1 ? 2 : 1;   \
-       else {                                                  \
-               buf[0] = '\0';                                  \
-               len = 1;                                        \
-       }                                                       \
-}
-#define DISP_WIDTH(len) \
-       ((len > 2 && conv_get_locale_charset() == C_UTF_8) ? 2 : \
-        (len == 2 && conv_get_locale_charset() == C_UTF_8) ? 1 : len)
-
-#define SPACE_CHARS    " \t"
-
-static void compose_wrap_line(Compose *compose)
-{
-       GtkTextView *text = GTK_TEXT_VIEW(compose->text);
-       GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
-       GtkTextMark *mark;
-       GtkTextIter insert_iter, iter;
-       gint ch_len, last_ch_len;
-       gchar cbuf[CHAR_BUF_SIZE], last_ch;
-       guint text_len;
-       gint p_start, p_end;
-       gint line_pos, cur_pos;
-       gint line_len, cur_len;
-       gboolean line_end, quoted;
-
-       text_len = gtk_text_buffer_get_char_count(textbuf);
-       mark = gtk_text_buffer_get_insert(textbuf);
-       gtk_text_buffer_get_iter_at_mark(textbuf, &insert_iter, mark);
-       cur_pos = gtk_text_iter_get_offset(&insert_iter);
-       GET_CHAR(&insert_iter, cbuf, ch_len);
-       if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
-               GtkTextIter prev_iter;
-               if (cur_pos == 0)
-                       return; /* on the paragraph mark */
-               prev_iter = insert_iter;
-               gtk_text_iter_backward_char(&prev_iter);
-               GET_CHAR(&prev_iter, cbuf, ch_len);
-               if (ch_len == 1 && *cbuf == '\n')
-                       return; /* on the paragraph mark */
-       }
-
-       /* find paragraph start. */
-       line_end = quoted = FALSE;
-       for (iter = insert_iter; gtk_text_iter_backward_char(&iter);) {
-               GET_CHAR(&iter, cbuf, ch_len);
-               if (ch_len == 1 && *cbuf == '\n') {
-                       if (quoted)
-                               return; /* quoted part */
-                       if (line_end) {
-                               gtk_text_iter_forward_chars(&iter, 2);
-                               break;
-                       }
-                       line_end = TRUE;
-               } else {
-                       if (ch_len == 1 
-                           && strchr(prefs_common.quote_chars, *cbuf))
-                               quoted = 1;
-                       else if (ch_len != 1 || !isspace(*(guchar *)cbuf))
-                               quoted = 0;
-
-                       line_end = FALSE;
-               }
-       }
-       p_start = gtk_text_iter_get_offset(&iter);
-
-       /* find paragraph end. */
-       line_end = FALSE;
-       for (iter = insert_iter; gtk_text_iter_forward_char(&iter);) {
-               GET_CHAR(&iter, cbuf, ch_len);
-               if (ch_len == 1 && *cbuf == '\n') {
-                       if (line_end) {
-                               p_end -= 1;
-                               gtk_text_iter_backward_char(&iter);
-                               break;
-                       }
-                       line_end = TRUE;
-               } else {
-                       if (line_end && ch_len == 1 &&
-                           strchr(prefs_common.quote_chars, *cbuf))
-                               return; /* quoted part */
-
-                       line_end = FALSE;
-               }
-       }
-       p_end = gtk_text_iter_get_offset(&iter);
-
-       if (p_end >= text_len)
-               p_end = text_len;
-
-       if (p_start >= p_end)
-               return;
-
-       line_len = cur_len = 0;
-       last_ch_len = 0;
-       last_ch = '\0';
-       line_pos = p_start;
-       for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
-               gboolean space = FALSE;
-
-               gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cur_pos);
-
-               GET_CHAR(&iter, cbuf, ch_len);
-
-               if (ch_len < 0) {
-                       cbuf[0] = '\0';
-                       ch_len = 1;
-               }
-
-               if (ch_len == 1 && isspace(*(guchar *)cbuf))
-                       space = 1;
-
-               if (ch_len == 1 && *cbuf == '\n') {
-                       gboolean replace = FALSE;
-                       GtkTextIter next_iter = iter;
-
-                       gtk_text_iter_forward_char(&next_iter);
-
-                       if (last_ch_len == 1 && !isspace((guchar)last_ch)) {
-                               if (cur_pos + 1 < p_end) {
-                                       GET_CHAR(&next_iter, cbuf, ch_len);
-                                       if (ch_len == 1 &&
-                                           !isspace(*(guchar *)cbuf))
-                                               replace = TRUE;
-                               }
-                       }
-                       gtk_text_buffer_delete(textbuf, &iter, &next_iter);
-                       if (replace) {
-                               gtk_text_buffer_insert(textbuf, &iter, " ", 1);
-                               space = TRUE;
-                       }
-                       else {
-                               p_end--;
-                               cur_pos--;
-                               gtk_text_buffer_get_iter_at_offset
-                                       (textbuf, &iter, cur_pos);
-                               continue;
-                       }
-               }
-
-               last_ch_len = ch_len;
-               last_ch = *cbuf;
-
-               if (space) {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
-               }
-
-               gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
-
-               if (cur_len + ch_len > prefs_common.linewrap_len &&
-                   line_len > 0) {
-                       gint tlen = ch_len;
-                       GtkTextIter prev_iter = iter;
-
-                       gtk_text_iter_backward_char(&prev_iter);
-
-                       GET_CHAR(&prev_iter, cbuf, ch_len);
-                       if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
-                               gtk_text_buffer_delete(textbuf, &prev_iter, &iter);
-                               iter = prev_iter;
-                               p_end--;
-                               cur_pos--;
-                               line_pos--;
-                               cur_len--;
-                               line_len--;
-                       }
-                       ch_len = tlen;
-
-                       gtk_text_buffer_insert(textbuf, &iter, "\n", 1);
-                       p_end++;
-                       cur_pos++;
-                       line_pos++;
-                       cur_len = cur_len - line_len + ch_len;
-                       line_len = 0;
-                       continue;
-               }
-
-               if (ch_len > 1) {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
-               }
-               cur_len += ch_len;
-       }
-}
-
-#undef WRAP_DEBUG
-#ifdef WRAP_DEBUG
-/* Darko: used when I debug wrapping */
-void dump_text(GtkTextBuffer *textbuf, int pos, int tlen, int breakoncr)
-{
-       GtkTextIter iter, end_iter;
-       gint clen;
-       gchar cbuf[CHAR_BUF_SIZE];
-
-       printf("%d [", pos);
-       gtk_text_buffer_get_iter_at_offset(textbuf, &iter, pos);
-       gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + tlen);
-       for (; gtk_text_iter_forward_char(&iter) &&
-            gtk_text_iter_compare(&iter, &end_iter) < 0; ) {
-               GET_CHAR(&iter, cbuf, clen);
-               if (clen < 0) break;
-               if (breakoncr && clen == 1 && cbuf[0] == '\n')
-                       break;
-               fwrite(cbuf, clen, 1, stdout);
-       }
-       printf("]\n");
-}
-#endif
 
 typedef enum {
-       WAIT_FOR_SPACE,
        WAIT_FOR_INDENT_CHAR,
-       WAIT_FOR_INDENT_CHAR_OR_SPACE
+       WAIT_FOR_INDENT_CHAR_OR_SPACE,
 } IndentState;
 
 /* return indent length, we allow:
-   > followed by spaces/tabs
-   | followed by spaces/tabs
-   uppercase characters immediately followed by >,
-   and the repeating sequences of the above */
-/* return indent length */
-static guint get_indent_length(GtkTextBuffer *textbuf, guint start_pos,
-                              guint text_len)
-{
-       GtkTextIter iter;
-       guint i_len = 0;
-       guint ch_len, alnum_cnt = 0;
+   indent characters followed by indent characters or spaces/tabs,
+   alphabets and numbers immediately followed by indent characters,
+   and the repeating sequences of the above
+   If quote ends with multiple spaces, only the first one is included. */
+static gchar *compose_get_quote_str(GtkTextBuffer *buffer,
+                                   const GtkTextIter *start, gint *len)
+{
+       GtkTextIter iter = *start;
+       gunichar wc;
+       gchar ch[6];
+       gint clen;
        IndentState state = WAIT_FOR_INDENT_CHAR;
-       gchar cbuf[CHAR_BUF_SIZE];
        gboolean is_space;
        gboolean is_indent;
-       gboolean iter_next = TRUE;
+       gint alnum_count = 0;
+       gint space_count = 0;
+       gint quote_len = 0;
 
        if (prefs_common.quote_chars == NULL) {
                return 0 ;
        }
 
-       gtk_text_buffer_get_iter_at_offset(textbuf, &iter, start_pos);
-
-       while (iter_next == TRUE) {
-               GET_CHAR(&iter, cbuf, ch_len);
-               if (ch_len > 1)
+       while (!gtk_text_iter_ends_line(&iter)) {
+               wc = gtk_text_iter_get_char(&iter);
+               if (g_unichar_iswide(wc))
                        break;
-
-               if (cbuf[0] == '\n')
+               clen = g_unichar_to_utf8(wc, ch);
+               if (clen != 1)
                        break;
 
-               is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
-               is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
+               is_indent = strchr(prefs_common.quote_chars, ch[0]) ? TRUE : FALSE;
+               is_space = g_unichar_isspace(wc);
 
-               switch (state) {
-               case WAIT_FOR_SPACE:
-                       if (is_space == FALSE)
-                               goto out;
-                       state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
-                       break;
-               case WAIT_FOR_INDENT_CHAR_OR_SPACE:
-                       if (is_indent == FALSE && is_space == FALSE &&
-                           !isupper((guchar)cbuf[0]))
-                               goto out;
-                       if (is_space == TRUE) {
-                               alnum_cnt = 0;
+               if (state == WAIT_FOR_INDENT_CHAR) {
+                       if (!is_indent && !g_unichar_isalnum(wc))
+                               break;
+                       if (is_indent) {
+                               quote_len += alnum_count + space_count + 1;
+                               alnum_count = space_count = 0;
                                state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
-                       } else if (is_indent == TRUE) {
-                               alnum_cnt = 0;
-                               state = WAIT_FOR_SPACE;
+                       } else
+                               alnum_count++;
+               } else if (state == WAIT_FOR_INDENT_CHAR_OR_SPACE) {
+                       if (!is_indent && !is_space && !g_unichar_isalnum(wc))
+                               break;
+                       if (is_space)
+                               space_count++;
+                       else if (is_indent) {
+                               quote_len += alnum_count + space_count + 1;
+                               alnum_count = space_count = 0;
                        } else {
-                               alnum_cnt++;
+                               alnum_count++;
                                state = WAIT_FOR_INDENT_CHAR;
                        }
-                       break;
-               case WAIT_FOR_INDENT_CHAR:
-                       if (is_indent == FALSE && !isupper((guchar)cbuf[0]))
-                               goto out;
-                       if (is_indent == TRUE) {
-                               if (alnum_cnt > 0 
-                                   && !strchr(prefs_common.quote_chars, cbuf[0]))
-                                       goto out;
-                               alnum_cnt = 0;
-                               state = WAIT_FOR_SPACE;
-                       } else {
-                               alnum_cnt++;
-                       }
-                       break;
                }
 
-               i_len++;
-               iter_next = gtk_text_iter_forward_char(&iter);
+               gtk_text_iter_forward_char(&iter);
        }
 
-out:
-       if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
-               i_len -= alnum_cnt;
+       if (quote_len > 0 && space_count > 0)
+               quote_len++;
+
+       if (len)
+               *len = quote_len;
+
+       if (quote_len > 0) {
+               iter = *start;
+               gtk_text_iter_forward_chars(&iter, quote_len);
+               return gtk_text_buffer_get_text(buffer, start, &iter, FALSE);
+       }
 
-       return i_len;
+       return NULL;
 }
 
-/* insert quotation string when line was wrapped */
-static guint ins_quote(GtkTextBuffer *textbuf, GtkTextIter *iter,
-                      guint indent_len,
-                      guint prev_line_pos, guint text_len,
-                      gchar *quote_fmt)
+static gboolean compose_get_line_break_pos(GtkTextBuffer *buffer,
+                                          const GtkTextIter *start,
+                                          GtkTextIter *break_pos,
+                                          gint max_col,
+                                          gint quote_len)
 {
-       guint ins_len = 0;
+       GtkTextIter iter = *start, line_end = *start;
+       PangoLogAttr *attrs;
+       gchar *str;
+       gchar *p;
+       gint len;
+       gint i;
+       gint col = 0;
+       gint pos = 0;
+       gboolean can_break = FALSE;
+       gboolean do_break = FALSE;
 
-       if (indent_len) {
-               GtkTextIter iter1, iter2;
-               gchar *text;
+       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);
+       /* 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);
+
+       p = str;
+
+       /* skip quote and leading spaces */
+       for (i = 0; *p != '\0' && i < len; i++) {
+               gunichar wc;
+
+               wc = g_utf8_get_char(p);
+               if (i >= quote_len && !g_unichar_isspace(wc))
+                       break;
+               col += g_unichar_iswide(wc) ? 2 : 1;
+               p = g_utf8_next_char(p);
+       }
 
-               gtk_text_buffer_get_iter_at_offset(textbuf, &iter1,
-                                                  prev_line_pos);
-               gtk_text_buffer_get_iter_at_offset(textbuf, &iter2,
-                                                  prev_line_pos + indent_len);
-               text = gtk_text_buffer_get_text(textbuf, &iter1, &iter2, FALSE);
-               if (!text) return 0;
+       for (; *p != '\0' && i < len; i++) {
+               PangoLogAttr *attr = attrs + i;
+               gunichar wc;
+               gint uri_len;
 
-               gtk_text_buffer_insert(textbuf, iter, text, -1);
-               ins_len = g_utf8_strlen(text, -1);
+               if (attr->is_line_break && can_break)
+                       pos = i;
 
-               g_free(text);
+               /* don't wrap URI */
+               if ((uri_len = get_uri_len(p)) > 0) {
+                       col += uri_len;
+                       if (pos > 0 && col > max_col) {
+                               do_break = TRUE;
+                               break;
+                       }
+                       i += uri_len - 1;
+                       p += uri_len;
+                       can_break = TRUE;
+                       continue;
+               }
+
+               wc = g_utf8_get_char(p);
+               col += g_unichar_iswide(wc) ? 2 : 1;
+               if (pos > 0 && col > max_col) {
+                       do_break = TRUE;
+                       break;
+               }
+
+               p = g_utf8_next_char(p);
+               can_break = TRUE;
        }
 
-       return ins_len;
+       debug_print("compose_get_line_break_pos(): do_break = %d, pos = %d, col = %d\n", do_break, pos, col);
+
+       g_free(attrs);
+       g_free(str);
+
+       *break_pos = *start;
+       gtk_text_iter_set_line_offset(break_pos, pos);
+
+       return do_break;
 }
 
 static gboolean is_sig_separator(Compose *compose, GtkTextBuffer *textbuf, guint start_pos) 
@@ -2801,325 +2639,172 @@ static gboolean is_sig_separator(Compose *compose, GtkTextBuffer *textbuf, guint
        return FALSE;
 }
 
-/* check if we should join the next line */
-static gboolean join_next_line_is_needed(GtkTextBuffer *textbuf,
-                                        guint start_pos, guint tlen,
-                                        guint prev_ilen, gboolean autowrap)
+static gboolean compose_join_next_line(GtkTextBuffer *buffer,
+                                      GtkTextIter *iter,
+                                      const gchar *quote_str)
 {
-       guint indent_len, ch_len;
-       gboolean do_join = FALSE;
-       gchar cbuf[CHAR_BUF_SIZE];
+       GtkTextIter iter_ = *iter, cur, prev, next;
+       PangoLogAttr attrs[3];
+       gchar *str;
+       gchar *next_quote_str;
+       gunichar wc1, wc2;
+       gint quote_len;
 
-       indent_len = get_indent_length(textbuf, start_pos, tlen);
+       if (!gtk_text_iter_forward_line(&iter_) ||
+           gtk_text_iter_ends_line(&iter_))
+               return FALSE;
 
-       if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
-               GtkTextIter iter;
-               gtk_text_buffer_get_iter_at_offset(textbuf, &iter,
-                                                  start_pos + indent_len);
-               GET_CHAR(&iter, cbuf, ch_len);
-               
-               if (ch_len > 0 && (cbuf[0] != '\n'))
-                       do_join = TRUE;
+       next_quote_str = compose_get_quote_str(buffer, &iter_, &quote_len);
+
+       if ((quote_str || next_quote_str) &&
+           strcmp2(quote_str, next_quote_str) != 0) {
+               g_free(next_quote_str);
+               return FALSE;
        }
+       g_free(next_quote_str);
 
-       return do_join;
-}
+       /* delete quote str */
+       if (quote_len > 0) {
+               GtkTextIter end = iter_;
 
-static void compose_wrap_line_all(Compose *compose)
-{
-       compose_wrap_line_all_full(compose, FALSE);
+               gtk_text_iter_forward_chars(&end, quote_len);
+               if (gtk_text_iter_ends_line(&end))
+                       return FALSE;
+               gtk_text_buffer_delete(buffer, &iter_, &end);
+       }
+
+       /* delete linebreak and extra spaces */
+       prev = cur = iter_;
+       while (gtk_text_iter_backward_char(&cur)) {
+               wc1 = gtk_text_iter_get_char(&cur);
+               if (!g_unichar_isspace(wc1))
+                       break;
+               prev = cur;
+       }
+       next = cur = iter_;
+       while (!gtk_text_iter_ends_line(&cur)) {
+               wc1 = gtk_text_iter_get_char(&cur);
+               if (!g_unichar_isspace(wc1))
+                       break;
+               gtk_text_iter_forward_char(&cur);
+               next = cur;
+       }
+       if (!gtk_text_iter_equal(&prev, &next))
+               gtk_text_buffer_delete(buffer, &prev, &next);
+       iter_ = prev;
+
+       /* insert space if required */
+       gtk_text_iter_backward_char(&prev);
+       wc1 = gtk_text_iter_get_char(&prev);
+       wc2 = gtk_text_iter_get_char(&next);
+       gtk_text_iter_forward_char(&next);
+       str = gtk_text_buffer_get_text(buffer, &prev, &next, FALSE);
+       pango_default_break(str, -1, NULL, attrs, 3);
+       if (!attrs[1].is_line_break ||
+           (!g_unichar_iswide(wc1) || !g_unichar_iswide(wc2)))
+               gtk_text_buffer_insert(buffer, &iter_, " ", 1);
+       g_free(str);
+
+       *iter = iter_;
+       return TRUE;
 }
 
-static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
+static void compose_wrap_paragraph(Compose *compose, GtkTextIter *par_iter)
 {
        GtkTextView *text = GTK_TEXT_VIEW(compose->text);
-       GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
-       GtkTextIter iter, end_iter;
-       guint tlen;
-       guint line_pos = 0, cur_pos = 0, p_pos = 0;
-       gint line_len = 0, cur_len = 0;
-       gint ch_len;
-       gboolean is_new_line = TRUE, do_delete = FALSE;
-       guint i_len = 0;
-       gboolean linewrap_quote = prefs_common.linewrap_quote;
-       gboolean set_editable_pos = FALSE;
-       gint editable_pos = 0;
-       guint linewrap_len = prefs_common.linewrap_len;
-       gchar *qfmt = prefs_common.quotemark;
-       gchar cbuf[CHAR_BUF_SIZE];
-       GtkTextMark *cursor_mark = gtk_text_buffer_get_insert(textbuf);
-       
-       tlen = gtk_text_buffer_get_char_count(textbuf);
-
-       for (; cur_pos < tlen; cur_pos++) {
-               /* mark position of new line - needed for quotation wrap */
-               if (is_new_line) {
-                       if (linewrap_quote)
-                               i_len = get_indent_length(textbuf, cur_pos, tlen);
-
-                       is_new_line = FALSE;
-                       p_pos = cur_pos;
-               }
+       GtkTextBuffer *buffer;
+       GtkTextIter iter, break_pos;
+       gchar *quote_str = NULL;
+       gint quote_len;
+       gboolean wrap_quote = prefs_common.linewrap_quote;
 
-               gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cur_pos);
-               GET_CHAR(&iter, cbuf, ch_len);
+       buffer = gtk_text_view_get_buffer(text);
 
-               /* fix line length for tabs */
-               if (ch_len == 1 && *cbuf == '\t') {
-                       guint tab_width = 8;
-                       guint tab_offset = line_len % tab_width;
+       if (par_iter) {
+               iter = *par_iter;
+       } else {
+               GtkTextMark *mark;
+               mark = gtk_text_buffer_get_insert(buffer);
+               gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
+       }
 
-                       line_len += tab_width - tab_offset - 1;
-                       cur_len = line_len;
+       /* 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;
+                       }
                }
+       }
 
-               /* we have encountered line break */
-               if (ch_len == 1 && *cbuf == '\n') {
-                       gint clen;
-                       gchar cb[CHAR_BUF_SIZE];
-
-                       /* should we join the next line */
-                       if ((autowrap || i_len != cur_len) && do_delete &&
-                       !is_sig_separator(compose, textbuf, cur_pos+i_len) &&
-                           join_next_line_is_needed
-                               (textbuf, cur_pos + 1, tlen, i_len, autowrap)) {
-                               do_delete = TRUE;
-                       } else
-                               do_delete = FALSE;
-
-                       /* skip delete if it is continuous URL */
-                       if (do_delete && (line_pos - p_pos <= i_len) &&
-                           gtkut_text_buffer_is_uri_string(textbuf, line_pos,
-                                                           tlen))
-                               do_delete = FALSE;
-
-                       /* should we delete to perform smart wrapping */
-                       if (line_len < linewrap_len && do_delete) {
-                               /* get rid of newline */
-                               gtk_text_buffer_get_iter_at_offset(textbuf,
-                                                                  &iter,
-                                                                  cur_pos);
-                               end_iter = iter;
-                               gtk_text_iter_forward_char(&end_iter);
-                               gtk_text_buffer_delete(textbuf, &iter, &end_iter);
-                               tlen--;
-
-                               /* if text starts with quote fmt or with
-                                  indent string, delete them */
-                               if (i_len) {
-                                       guint ilen;
-                                       ilen =  gtkut_text_buffer_str_compare_n
-                                               (textbuf, cur_pos, p_pos, i_len,
-                                                tlen);
-                                       if (ilen) {
-                                               end_iter = iter;
-                                               gtk_text_iter_forward_chars
-                                                       (&end_iter, ilen);
-                                               gtk_text_buffer_delete(textbuf,
-                                                                      &iter,
-                                                                      &end_iter);
-                                               tlen -= ilen;
-                                       }
-                               }
-
-                               gtk_text_buffer_get_iter_at_offset(textbuf,
-                                                                  &iter,
-                                                                  cur_pos);
-                               GET_CHAR(&iter, cb, clen);
-
-                               /* insert space if it's alphanumeric */
-                               if ((cur_pos != line_pos) &&
-                                   ((clen > 1) || isalnum((guchar)cb[0]))) {
-                                       GtkTextIter cursor_iter;
-                                       gboolean go_back = FALSE;
-                                       gtk_text_buffer_get_iter_at_mark(textbuf, &cursor_iter, cursor_mark);
-                                       if (gtk_text_iter_get_offset(&iter) ==
-                                                gtk_text_iter_get_offset(&cursor_iter))
-                                               go_back = TRUE;
-                                       
-                                       gtk_text_buffer_insert(textbuf, &iter,
-                                                              " ", 1);
-                                       if (go_back) {
-                                               gtk_text_buffer_get_iter_at_mark(textbuf, &cursor_iter, cursor_mark);
-                                               gtk_text_iter_backward_chars(&cursor_iter, 1);
-                                               gtk_text_buffer_place_cursor(textbuf, &cursor_iter);
-                                       }
-                                       tlen++;
-                               }
-
-                               /* and start over with current line */
-                               cur_pos = p_pos - 1;
-                               line_pos = cur_pos;
-                               line_len = cur_len = 0;
-                               do_delete = FALSE;
-                               is_new_line = TRUE;
-#ifdef WRAP_DEBUG
-                               g_print("after delete l_pos=");
-                               dump_text(textbuf, line_pos, tlen, 1);
-#endif
-                               /* move beginning of line if we are on LF */
-                               gtk_text_buffer_get_iter_at_offset(textbuf,
-                                                                  &iter,
-                                                                  line_pos);
-                               GET_CHAR(&iter, cb, clen);
-                               if (clen == 1 && *cb == '\n')
-                                       line_pos++;
-#ifdef WRAP_DEBUG
-                               g_print("new line_pos=%d\n", line_pos);
-#endif
+       if (gtk_text_iter_is_end(&iter)) {
+               if (par_iter)
+                       *par_iter = iter;
+               return;
+       }
 
+       do {
+               quote_str = compose_get_quote_str(buffer, &iter, &quote_len);
+               if (quote_str) {
+                       if (!wrap_quote) {
+                               gtk_text_iter_forward_line(&iter);
+                               g_free(quote_str);
                                continue;
                        }
-
-                       /* mark new line beginning */
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len = 0;
-                       do_delete = FALSE;
-                       is_new_line = TRUE;
-                       continue;
-               }
-
-               if (ch_len < 0) {
-                       cbuf[0] = '\0';
-                       ch_len = 1;
+                       debug_print("compose_wrap_paragraph(): quote_str = '%s'\n", quote_str);
                }
 
-               /* possible line break */
-               if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
-               }
+               if (compose_get_line_break_pos(buffer, &iter, &break_pos,
+                                              prefs_common.linewrap_len,
+                                              quote_len)) {
+                       gtk_text_buffer_insert(buffer, &break_pos, "\n", 1);
+                       if (quote_str)
+                               gtk_text_buffer_insert(buffer, &break_pos,
+                                                      quote_str, -1);
 
-               /* are we over wrapping length set in preferences ? */
-               if (cur_len + DISP_WIDTH(ch_len) > linewrap_len) {
-                       gint clen;
+                       iter = break_pos;
+                       compose_join_next_line(buffer, &iter, quote_str);
 
-#ifdef WRAP_DEBUG
-                       g_print("should wrap cur_pos=%d ", cur_pos);
-                       dump_text(textbuf, p_pos, tlen, 1);
-                       dump_text(textbuf, line_pos, tlen, 1);
-#endif
-                       /* force wrapping if it is one long word but not URL */
-                       if (line_pos - p_pos <= i_len)
-                               if (!gtkut_text_buffer_is_uri_string
-                                   (textbuf, line_pos, tlen))
-                                       line_pos = cur_pos - 1;
-#ifdef WRAP_DEBUG
-                       g_print("new line_pos=%d\n", line_pos);
-#endif
+                       /* move iter to current line start */
+                       gtk_text_iter_set_line_offset(&iter, 0);
+               } else {
+                       /* move iter to next line start */
+                       iter = break_pos;
+                       gtk_text_iter_forward_line(&iter);
+               }
 
-                       gtk_text_buffer_get_iter_at_offset(textbuf,
-                                                          &iter,
-                                                          line_pos - 1);
-                       GET_CHAR(&iter, cbuf, clen);
-
-                       /* if next character is space delete it */
-                       if (clen == 1 && isspace(*(guchar *)cbuf)) {
-                               if (p_pos + i_len != line_pos ||
-                                   !gtkut_text_buffer_is_uri_string
-                                       (textbuf, line_pos, tlen)) {
-                                       /* workaround for correct cursor
-                                          position */
-                                       if (set_editable_pos == FALSE) {
-                                               GtkTextMark *ins = gtk_text_buffer_get_insert(textbuf);
-                                               gtk_text_buffer_get_iter_at_mark(textbuf, &iter, ins);
-                                               editable_pos = gtk_text_iter_get_offset(&iter);
-                                               if (editable_pos == line_pos)
-                                                       set_editable_pos = TRUE;
-                                       }
-                                       gtk_text_buffer_get_iter_at_offset(textbuf,
-                                                          &iter,
-                                                          line_pos-1);
-                                       gtk_text_buffer_get_iter_at_offset(textbuf,
-                                                          &end_iter,
-                                                          line_pos);
-                                       gtk_text_buffer_delete(textbuf, &iter, &end_iter);
-                                       //gtk_stext_set_point(text, line_pos);
-                                       //gtk_stext_backward_delete(text, 1);
-                                       tlen--;
-                                       cur_pos--;
-                                       line_pos--;
-                                       cur_len--;
-                                       line_len--;
-                               }
-                       }
+               g_free(quote_str);
+       } while (!gtk_text_iter_ends_line(&iter));
+       /* stop if paragraph end (empty line) */
 
-                       /* if it is URL at beginning of line don't wrap */
-                       if (p_pos + i_len == line_pos &&
-                           gtkut_text_buffer_is_uri_string(textbuf, line_pos, tlen)) {
-#ifdef WRAP_DEBUG
-                               g_print("found URL at ");
-                               dump_text(textbuf, line_pos, tlen, 1);
-#endif
-                               continue;
-                       }
+       if (par_iter)
+               *par_iter = iter;
+}
 
-                       /* insert CR */
-                       gtk_text_buffer_get_iter_at_offset(textbuf,
-                                                          &iter,
-                                                          line_pos);
-                       gtk_text_buffer_insert(textbuf, &iter, "\n", 1);
-                       //gtk_stext_set_point(text, line_pos);
-                       //gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
-                       //gtk_stext_compact_buffer(text);
-                       tlen++;
-                       line_pos++;
-                       /* for loop will increase it */
-                       cur_pos = line_pos - 1;
-                       /* start over with current line */
-                       is_new_line = TRUE;
-                       line_len = cur_len = 0;
-                       if (autowrap || i_len > 0) {
-                               do_delete = TRUE;
-                       } else
-                               do_delete = FALSE;
-#ifdef WRAP_DEBUG
-                       g_print("after CR insert ");
-                       dump_text(textbuf, line_pos, tlen, 1);
-                       dump_text(textbuf, cur_pos, tlen, 1);
-#endif
+static void compose_wrap_all(Compose *compose)
+{
+       compose_wrap_all_full(compose, FALSE);
+}
 
-                       /* should we insert quotation ? */
-                       if (linewrap_quote && i_len) {
-                               /* only if line is not already quoted  */
-                               if (!gtkut_text_buffer_str_compare
-                                       (textbuf, line_pos, tlen, qfmt)) {
-                                       guint ins_len;
-                                       
-                                       gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
-                                       
-                                       if (line_pos - p_pos > i_len) {
-                                               ins_len = ins_quote
-                                                       (textbuf, &iter, i_len, p_pos,
-                                                        tlen, qfmt);
-                                               tlen += ins_len;
-                                       }
-#ifdef WRAP_DEBUG
-                                       g_print("after quote insert ");
-                                       dump_text(textbuf, line_pos, tlen, 1);
-#endif
-                               }
-                       }
-                       continue;
-               }
+static void compose_wrap_all_full(Compose *compose, gboolean autowrap)
+{
+       GtkTextView *text = GTK_TEXT_VIEW(compose->text);
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
 
-               if (ch_len > 1) {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
-               }
-               /* advance to next character in buffer */
-               cur_len += ch_len;
-       }
+       buffer = gtk_text_view_get_buffer(text);
 
-       if (set_editable_pos && editable_pos <= tlen) {
-               gtk_text_buffer_get_iter_at_offset(textbuf, &iter, editable_pos);
-               gtk_text_buffer_place_cursor(textbuf, &iter);
-       }
+       gtk_text_buffer_get_start_iter(buffer, &iter);
+       while (!gtk_text_iter_is_end(&iter))
+               compose_wrap_paragraph(compose, &iter);
 }
 
-#undef GET_CHAR
-#undef CHAR_BUF_SIZE
-
 static void compose_set_title(Compose *compose)
 {
        gchar *str;
@@ -3625,6 +3310,7 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action)
 
                if (action == COMPOSE_WRITE_FOR_SEND) {
                        buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
+                       
                        if (!buf) {
                                AlertValue aval;
                                gchar *msg;
@@ -3653,6 +3339,13 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action)
        }
        g_free(chars);
 
+       if (encoding == ENC_8BIT || encoding == ENC_7BIT) {
+               if (!strncmp(buf, "From ", sizeof("From ")-1) ||
+                   strstr(buf, "\nFrom ") != NULL) {
+                       encoding = ENC_QUOTED_PRINTABLE;
+               }
+       }
+
        mimetext = procmime_mimeinfo_new();
        mimetext->content = MIMECONTENT_MEM;
        mimetext->data.mem = buf;
@@ -3697,6 +3390,8 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action)
                        return -1;
 
        procmime_write_mimeinfo(mimemsg, fp);
+       
+       procmime_mimeinfo_free_all(mimemsg);
 
        return 0;
 }
@@ -3840,7 +3535,7 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        }
 
         if (prefs_common.linewrap_at_send)
-               compose_wrap_line_all(compose);
+               compose_wrap_all(compose);
 
        /* write queue header */
        tmp = g_strdup_printf("%s%cqueue.%p", get_tmp_dir(),
@@ -4931,7 +4626,8 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        /* text widget */
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
-                                      GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+                                      GTK_POLICY_AUTOMATIC,
+                                      GTK_POLICY_AUTOMATIC);
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
                                            GTK_SHADOW_IN);
        gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
@@ -4943,10 +4639,6 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), TRUE);
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
        gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
-       clipboard = gtk_clipboard_get(GDK_SELECTION_SECONDARY);
-       gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
-       clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
-       gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
        
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
 
@@ -5287,7 +4979,11 @@ static void compose_set_privacy_system_cb(GtkWidget *widget, gpointer data)
 
        ifactory = gtk_item_factory_from_widget(compose->menubar);
        menu_set_sensitive(ifactory, "/Options/Sign", can_sign);
+       if (!can_sign)
+               menu_set_active(ifactory, "/Options/Sign", FALSE);
        menu_set_sensitive(ifactory, "/Options/Encrypt", can_encrypt);
+       if (!can_encrypt)
+               menu_set_active(ifactory, "/Options/Encrypt", FALSE);
 }
 
 static void compose_update_privacy_system_menu_item(Compose * compose)
@@ -6231,12 +5927,6 @@ static void compose_undo_state_changed(UndoMain *undostruct, gint undo_state,
        }
 }
 
-static gint calc_cursor_xpos(GtkTextView *text, gint extra, gint char_width)
-{
-#warning FIXME_GTK2
-       return 0;
-}
-
 /* callback functions */
 
 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
@@ -6258,10 +5948,7 @@ static gboolean compose_edit_size_alloc(GtkEditable *widget,
 
                /* got the maximum */
                gtk_ruler_set_range(GTK_RULER(shruler),
-                                   0.0, line_width_in_chars,
-                                   calc_cursor_xpos(GTK_TEXT_VIEW(widget),
-                                                    allocation->x,
-                                                    char_width),
+                                   0.0, line_width_in_chars, 0,
                                    /*line_width_in_chars*/ char_width);
        }
 
@@ -6871,7 +6558,6 @@ static void textview_move_next_line (GtkTextView *text)
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
        mark = gtk_text_buffer_get_insert(buffer);
        gtk_text_buffer_get_iter_at_mark(buffer, &ins, mark);
-#warning FIXME_GTK2 /* should regist current line offset */
        offset = gtk_text_iter_get_line_offset(&ins);
        if (gtk_text_iter_forward_line(&ins)) {
                gtk_text_iter_set_line_offset(&ins, offset);
@@ -6891,7 +6577,6 @@ static void textview_move_previous_line (GtkTextView *text)
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
        mark = gtk_text_buffer_get_insert(buffer);
        gtk_text_buffer_get_iter_at_mark(buffer, &ins, mark);
-#warning FIXME_GTK2 /* should regist current line offset */
        offset = gtk_text_iter_get_line_offset(&ins);
        if (gtk_text_iter_backward_line(&ins)) {
                gtk_text_iter_set_line_offset(&ins, offset);
@@ -7090,13 +6775,23 @@ static void compose_changed_cb(GtkTextBuffer *textbuf, Compose *compose)
        }
 }
 
+static void compose_wrap_cb(gpointer data, guint action, GtkWidget *widget)
+{
+       Compose *compose = (Compose *)data;
+
+       if (action == 1)
+               compose_wrap_all(compose);
+       else
+               compose_wrap_paragraph(compose, NULL);
+}
+
 static void compose_toggle_autowrap_cb(gpointer data, guint action,
                                       GtkWidget *widget)
 {
        Compose *compose = (Compose *)data;
        compose->autowrap = GTK_CHECK_MENU_ITEM(widget)->active;
        if (compose->autowrap)
-               compose_wrap_line_all_full(compose, TRUE);
+               compose_wrap_all_full(compose, TRUE);
 }
 
 static void compose_toggle_sign_cb(gpointer data, guint action,
@@ -7429,7 +7124,7 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
                gtk_text_buffer_insert(buffer, iter, text, len);
 
        mark = gtk_text_buffer_create_mark(buffer, NULL, iter, FALSE);
-       compose_wrap_line_all_full(compose, TRUE);
+       compose_wrap_all_full(compose, TRUE);
        gtk_text_buffer_get_iter_at_mark(buffer, iter, mark);
        gtk_text_buffer_delete_mark(buffer, mark);