added support to view text/enriched
[claws.git] / src / textview.c
index 947f2e9bb761fc06b8ac3cc5cb0fcb6f8b66a17b..70e878aef5e5fec910eba3dc609d34ff694e77fc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2001 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -45,6 +45,7 @@
 #include "gtkutils.h"
 #include "procmime.h"
 #include "html.h"
+#include "enriched.h"
 #include "compose.h"
 #include "addressbook.h"
 #include "displayheader.h"
@@ -97,12 +98,19 @@ static GdkColor error_color = {
 
 static GdkFont *spacingfont;
 
+static void textview_show_ertf         (TextView       *textview,
+                                        FILE           *fp,
+                                        CodeConverter  *conv);
 static void textview_show_html         (TextView       *textview,
                                         FILE           *fp,
                                         CodeConverter  *conv);
 static void textview_write_line                (TextView       *textview,
                                         const gchar    *str,
                                         CodeConverter  *conv);
+static void textview_write_link         (TextView      *textview,
+                                         const gchar    *url,
+                                        const gchar    *str,
+                                        CodeConverter  *conv);
 static GPtrArray *textview_scan_header (TextView       *textview,
                                         FILE           *fp);
 static void textview_show_header       (TextView       *textview,
@@ -151,6 +159,8 @@ TextView *textview_create(void)
        /* create GtkText widgets for single-byte and multi-byte character */
        text_sb = gtk_text_new(NULL, NULL);
        text_mb = gtk_text_new(NULL, NULL);
+       GTK_TEXT(text_sb)->default_tab_width = 8;
+       GTK_TEXT(text_mb)->default_tab_width = 8;
        gtk_widget_show(text_sb);
        gtk_widget_show(text_mb);
        gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
@@ -181,15 +191,15 @@ TextView *textview_create(void)
        gtk_signal_connect(GTK_OBJECT(text_sb), "key_press_event",
                           GTK_SIGNAL_FUNC(textview_key_pressed),
                           textview);
-       gtk_signal_connect(GTK_OBJECT(text_sb), "button_press_event",
-                          GTK_SIGNAL_FUNC(textview_button_pressed),
-                          textview);
+       gtk_signal_connect_after(GTK_OBJECT(text_sb), "button_press_event",
+                                GTK_SIGNAL_FUNC(textview_button_pressed),
+                                textview);
        gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
                           GTK_SIGNAL_FUNC(textview_key_pressed),
                           textview);
-       gtk_signal_connect(GTK_OBJECT(text_mb), "button_press_event",
-                          GTK_SIGNAL_FUNC(textview_button_pressed),
-                          textview);
+       gtk_signal_connect_after(GTK_OBJECT(text_mb), "button_press_event",
+                                GTK_SIGNAL_FUNC(textview_button_pressed),
+                                textview);
 
        gtk_widget_show(scrolledwin_sb);
        gtk_widget_show(scrolledwin_mb);
@@ -206,6 +216,8 @@ TextView *textview_create(void)
        textview->text_mb        = text_mb;
        textview->text_is_mb     = FALSE;
        textview->uri_list       = NULL;
+       textview->body_pos       = 0;
+       textview->cur_pos        = 0;
 
        return textview;
 }
@@ -282,18 +294,27 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                boundary_len = strlen(boundary);
        }
 
-       if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
+       if (!boundary && (mimeinfo->mime_type == MIME_TEXT || mimeinfo->mime_type == MIME_TEXT_HTML || mimeinfo->mime_type == MIME_TEXT_ENRICHED)) {
+       
                if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
                        perror("fseek");
                headers = textview_scan_header(textview, fp);
        } else {
                if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
                        glong fpos;
+                       MimeInfo *parent = mimeinfo->parent;
+
+                       while (parent->parent) {
+                               if (parent->main &&
+                                   parent->main->mime_type ==
+                                       MIME_MESSAGE_RFC822)
+                                       break;
+                               parent = parent->parent;
+                       }
 
                        if ((fpos = ftell(fp)) < 0)
                                perror("ftell");
-                       else if (fseek(fp, mimeinfo->parent->fpos, SEEK_SET)
-                                < 0)
+                       else if (fseek(fp, parent->fpos, SEEK_SET) < 0)
                                perror("fseek");
                        else {
                                headers = textview_scan_header(textview, fp);
@@ -301,17 +322,20 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                                        perror("fseek");
                        }
                }
+               /* skip MIME part headers */
                while (fgets(buf, sizeof(buf), fp) != NULL)
                        if (buf[0] == '\r' || buf[0] == '\n') break;
        }
 
        /* display attached RFC822 single text message */
        if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
+               if (headers) procheader_header_array_destroy(headers);
                if (!mimeinfo->sub || mimeinfo->sub->children) return;
                headers = textview_scan_header(textview, fp);
                mimeinfo = mimeinfo->sub;
        } else if (!mimeinfo->parent &&
                   mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
+               if (headers) procheader_header_array_destroy(headers);
                if (!mimeinfo->sub) return;
                headers = textview_scan_header(textview, fp);
                mimeinfo = mimeinfo->sub;
@@ -329,15 +353,20 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
        text = GTK_TEXT(textview->text);
        gtk_text_freeze(text);
 
+       textview->body_pos = 0;
+       textview->cur_pos  = 0;
+
        if (headers) {
                textview_show_header(textview, headers);
-               procheader_header_array_destroy(headers);               
+               procheader_header_array_destroy(headers);
        }
 
        tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
        if (tmpfp) {
                if (mimeinfo->mime_type == MIME_TEXT_HTML)
                        textview_show_html(textview, tmpfp, conv);
+               else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED)
+                       textview_show_ertf(textview, tmpfp, conv);
                else
                        while (fgets(buf, sizeof(buf), tmpfp) != NULL)
                                textview_write_line(textview, buf, conv);
@@ -349,6 +378,9 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
        gtk_text_thaw(text);
 }
 
+#define TEXT_INSERT(str) \
+       gtk_text_insert(text, textview->msgfont, NULL, NULL, str, -1)
+
 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
 {
        GtkText *text;
@@ -361,26 +393,17 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
 
        gtk_text_freeze(text);
 
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("To save this part, pop up the context menu with\n"), -1);
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("right click and select `Save as...', or press `y' key.\n\n"), -1);
-
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("To display this part as a text message, select\n"), -1);
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("`Display as text', or press `t' key.\n\n"), -1);
-
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("To open this part with external program, select `Open',\n"), -1);
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("or double-click, or click the center button, or press `l' key."), -1);
+       TEXT_INSERT(_("To save this part, pop up the context menu with "));
+       TEXT_INSERT(_("right click and select `Save as...', "));
+       TEXT_INSERT(_("or press `y' key.\n\n"));
+
+       TEXT_INSERT(_("To display this part as a text message, select "));
+       TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
+
+       TEXT_INSERT(_("To open this part with external program, select "));
+       TEXT_INSERT(_("`Open' or `Open with...', "));
+       TEXT_INSERT(_("or double-click, or click the center button, "));
+       TEXT_INSERT(_("or press `l' key."));
 
        gtk_text_thaw(text);
 }
@@ -399,39 +422,73 @@ void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
        gtk_text_freeze(text);
 
        if (partinfo->sigstatus_full == NULL) {
-               gtk_text_insert
-                       (text, textview->msgfont, NULL, NULL,
-                        _("This signature has not been checked yet.\n"), -1);
-               gtk_text_insert
-                       (text, textview->msgfont, NULL, NULL,
-                        _("To check it, pop up the context menu with\n"), -1);
-               gtk_text_insert
-                       (text, textview->msgfont, NULL, NULL,
-                        _("right click and select `Check signature'.\n"), -1);
+               TEXT_INSERT(_("This signature has not been checked yet.\n"));
+               TEXT_INSERT(_("To check it, pop up the context menu with\n"));
+               TEXT_INSERT(_("right click and select `Check signature'.\n"));
        } else {
-               gtk_text_insert(text, textview->msgfont, NULL, NULL,
-                               partinfo->sigstatus_full, -1);
+               TEXT_INSERT(partinfo->sigstatus_full);
        }
 
        gtk_text_thaw(text);
 }
 #endif /* USE_GPGME */
 
+#undef TEXT_INSERT
+
 static void textview_show_html(TextView *textview, FILE *fp,
                               CodeConverter *conv)
 {
        HTMLParser *parser;
        gchar *str;
+       gchar* url = NULL;
 
        parser = html_parser_new(fp, conv);
        g_return_if_fail(parser != NULL);
 
        while ((str = html_parse(parser)) != NULL) {
-               textview_write_line(textview, str, NULL);
+               if (parser->state == HTML_HREF) {
+                       /* first time : get and copy the URL */
+                       if (url == NULL) {
+                               /* ALF - the sylpheed html parser returns an empty string,
+                                * if still inside an <a>, but already parsed past HREF */
+                               str = strtok(str, " ");
+                               if (str) { 
+                                       url = strdup(str);
+                                       /* the URL may (or not) be followed by the
+                                        * referenced text */
+                                       str = strtok(NULL, "");
+                               }       
+                       }
+                       if (str != NULL) {
+                               textview_write_link(textview, url, str, NULL);
+                       }
+               } else {
+                       if (url != NULL) {
+                               free(url);
+                               url = NULL;
+                       }
+                       textview_write_line(textview, str, NULL);
+               }
        }
        html_parser_destroy(parser);
 }
 
+static void textview_show_ertf(TextView *textview, FILE *fp,
+                              CodeConverter *conv)
+{
+       ERTFParser *parser;
+       gchar *str;
+       gchar* url = NULL;
+
+       parser = ertf_parser_new(fp, conv);
+       g_return_if_fail(parser != NULL);
+
+       while ((str = ertf_parse(parser)) != NULL) {
+               textview_write_line(textview, str, NULL);
+       }
+       ertf_parser_destroy(parser);
+}
+
 /* get_uri_part() - retrieves a URI starting from scanpos.
                    Returns TRUE if succesful */
 static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
@@ -458,8 +515,7 @@ static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
         * should pass some URI type to this function and decide on that whether
         * to perform punctuation stripping */
 
-#define IS_REAL_PUNCT(ch) \
-       (ispunct(ch) && ((ch) != '/')) 
+#define IS_REAL_PUNCT(ch)      (ispunct(ch) && ((ch) != '/')) 
 
        for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
                ;
@@ -484,6 +540,10 @@ static gchar *make_uri_string(const gchar *bp, const gchar *ep)
         !isspace(ch) && \
         !strchr("()<>\"", (ch)))
 
+/* alphabet and number within 7bit ASCII */
+#define IS_ASCII_ALNUM(ch)     (isascii(ch) && isalnum(ch))
+#define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
+
 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
                               const gchar **bp, const gchar **ep)
@@ -491,8 +551,14 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
        /* more complex than the uri part because we need to scan back and forward starting from
         * the scan position. */
        gboolean result = FALSE;
-       const gchar *bp_;
-       const gchar *ep_;
+       const gchar *bp_ = NULL;
+       const gchar *ep_ = NULL;
+
+       /* the informative part of the email address (describing the name
+        * of the email address owner) may contain quoted parts. the
+        * closure stack stores the last encountered quotes. */
+       gchar closure_stack[128];
+       gchar *ptr = closure_stack;
 
        g_return_val_if_fail(start != NULL, FALSE);
        g_return_val_if_fail(scanpos != NULL, FALSE);
@@ -505,7 +571,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 
        /* TODO: should start with an alnum? */
        bp_++;
-       for (; bp_ < scanpos && !isalnum(*bp_); bp_++)
+       for (; bp_ < scanpos && !IS_ASCII_ALNUM(*bp_); bp_++)
                ;
 
        if (bp_ != scanpos) {
@@ -514,7 +580,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
                        ;
 
                /* TODO: really should terminate with an alnum? */
-               for (; ep_ > scanpos  && !isalnum(*ep_); --ep_)
+               for (; ep_ > scanpos && !IS_ASCII_ALNUM(*ep_); --ep_)
                        ;
                ep_++;
 
@@ -525,62 +591,59 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
                }
        }
 
-       /* we now have at least the address. now see if this is <bracketed>; in this
-        * case we also scan for the informative part. we could make this a useful
-        * function because it tries to parse out an email address backwards. :) */
-       if (result) {
-               if ( ((bp_ - 1 > start) && *(bp_ - 1) == '<') &&  (*ep_ == '>')) {
-                       
-                       /* the informative part of the email address (describing the name
-                        * of the email address owner) may contain quoted parts. the
-                        * closure stack stores the last encountered quotes. */
-                       char    closure_stack[128];
-                       char   *ptr = closure_stack;
-#define FULL_STACK()   ((ptr - closure_stack) >= sizeof closure_stack) 
-#define IN_STACK()             (ptr > closure_stack)
+       if (!result) return FALSE;
+
+       /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
+       if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_)) 
+               return FALSE;
+
+       /* see if this is <bracketed>; in this case we also scan for the informative part. */
+       if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
+               return TRUE;
+
+#define FULL_STACK()   ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
+#define IN_STACK()     (ptr > closure_stack)
 /* has underrun check */
-#define POP_STACK()            if(IN_STACK()) --ptr            
+#define POP_STACK()    if(IN_STACK()) --ptr
 /* has overrun check */
-#define PUSH_STACK(c)  if(!FULL_STACK()) *ptr++ = (c); else return TRUE 
+#define PUSH_STACK(c)  if(!FULL_STACK()) *ptr++ = (c); else return TRUE
 /* has underrun check */
 #define PEEK_STACK()   (IN_STACK() ? *(ptr - 1) : 0)
 
-                       ep_++;
-
-                       /* scan for the informative part. */
-                       
-                       for (bp_ -= 2; bp_ >= start; bp_--) {
-                               /* if closure on the stack keep scanning */
-                               if (PEEK_STACK() == *bp_) {
-                                       POP_STACK();
-                               }                                       
-                               else {
-                                       if (*bp_ == '\'' || *bp_ == '"')  {
-                                               PUSH_STACK(*bp_);
-                                       }                                               
-                                       else {
-                                               /* if nothing in the closure stack, do the special conditions
-                                                * the following if..else expression simply checks whether 
-                                                * a token is acceptable. if not acceptable, the clause
-                                                * should terminate the loop with a 'break' */
-                                               if (!PEEK_STACK()) {
-                                                       if ( *bp_ == '-' 
-                                                       &&   (((bp_ - 1) >= start) && isalnum(*(bp_ - 1))) 
-                                                       &&   (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1))) ) {
-                                                               /* hyphens are allowed, but only in between alnums */
-                                                       }
-                                                       else if ( !ispunct(*bp_) ) {
-                                                               /* but anything not being a punctiation is ok */
-                                                       }
-                                                       else {
-                                                               break; /* anything else is rejected */
-                                                       }
-                                               }
-                                       }
-                               }
+       ep_++;
+
+       /* scan for the informative part. */
+       for (bp_ -= 2; bp_ >= start; bp_--) {
+               /* if closure on the stack keep scanning */
+               if (PEEK_STACK() == *bp_) {
+                       POP_STACK();
+                       continue;
+               }
+               if (*bp_ == '\'' || *bp_ == '"') {
+                       PUSH_STACK(*bp_);
+                       continue;
+               }
+
+               /* if nothing in the closure stack, do the special conditions
+                * the following if..else expression simply checks whether 
+                * a token is acceptable. if not acceptable, the clause
+                * should terminate the loop with a 'break' */
+               if (!PEEK_STACK()) {
+                       if (*bp_ == '-'
+                       && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
+                       && (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1)))) {
+                               /* hyphens are allowed, but only in
+                                  between alnums */
+                       } else if (!ispunct(*bp_)) {
+                               /* but anything not being a punctiation
+                                  is ok */
+                       } else {
+                               break; /* anything else is rejected */
                        }
-                       
-                       bp_++;
+               }
+       }
+
+       bp_++;
 
 #undef PEEK_STACK
 #undef PUSH_STACK
@@ -588,17 +651,17 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 #undef IN_STACK
 #undef FULL_STACK
 
-                       /* scan forward (should start with an alnum) */
-                       for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
-                               ;
+       /* scan forward (should start with an alnum) */
+       for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
+               ;
+
+       *ep = ep_;
+       *bp = bp_;
 
-                       *ep = ep_;
-                       *bp = bp_;
-               }
-       }
        return result;
 }
 
+#undef IS_QUOTE
 #undef IS_RFC822_CHAR
 
 static gchar *make_email_string(const gchar *bp, const gchar *ep)
@@ -629,7 +692,7 @@ static gchar *make_email_string(const gchar *bp, const gchar *ep)
 
 /* textview_make_clickable_parts() - colorizes clickable parts */
 static void textview_make_clickable_parts(TextView *textview,
-                                         GdkFont  *font,
+                                         GdkFont *font,
                                          GdkColor *fg_color,
                                          GdkColor *uri_color,
                                          const gchar *linebuf)
@@ -692,7 +755,7 @@ static void textview_make_clickable_parts(TextView *textview,
                if (scanpos) {
                        /* check if URI can be parsed */
                        if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
-                           && (ep - bp - 1) > strlen(parser[last_index].needle)) {
+                           && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
                                        ADD_TXT_POS(bp, ep, last_index);
                                        walk = ep;
                        } else
@@ -718,7 +781,7 @@ static void textview_make_clickable_parts(TextView *textview,
                                                normal_text,
                                                last->bp - normal_text);
                        uri->uri = parser[last->pti].build_uri(last->bp, 
-                                               last->ep);
+                                                              last->ep);
                        uri->start = gtk_text_get_point(text);
                        gtk_text_insert(text, font, uri_color,
                                        NULL, last->bp, last->ep - last->bp);
@@ -736,12 +799,46 @@ static void textview_make_clickable_parts(TextView *textview,
 
 #undef ADD_TXT_POS
 
+/* This function writes str as a double-clickable link with the given url. */ 
+static void textview_write_link(TextView *textview, const gchar *url,
+                                const gchar *str, CodeConverter *conv)
+{
+    GdkColor *link_color = NULL;
+    RemoteURI* uri;
+    GtkText *text = GTK_TEXT(textview->text);
+    gchar buf[BUFFSIZE];
+
+    /* this part is taken from textview_write_line. Right now the only place
+     * that calls this function passes NULL for conv, but you never know. */
+    if (!conv)
+           strncpy2(buf, str, sizeof(buf));
+    else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
+                   gtk_text_insert(text, textview->msgfont,
+                           prefs_common.enable_color
+                           ? &error_color : NULL, NULL,
+                           "*** Warning: code conversion failed ***\n",
+                           -1);
+           return;
+    }
+
+    /* this part is based on the code in make_clickable_parts */
+    if (prefs_common.enable_color) {
+       link_color = &uri_color;
+    }
+    uri = g_new(RemoteURI, 1);
+    uri->uri = g_strdup(url);
+    uri->start = gtk_text_get_point(text);
+    gtk_text_insert(text, textview->msgfont, link_color, NULL, buf,
+                   strlen(buf));
+    uri->end = gtk_text_get_point(text);
+    textview->uri_list = g_slist_append(textview->uri_list, uri);
+}
+
 static void textview_write_line(TextView *textview, const gchar *str,
                                CodeConverter *conv)
 {
        GtkText *text = GTK_TEXT(textview->text);
        gchar buf[BUFFSIZE];
-       size_t len;
        GdkColor *fg_color;
        gint quotelevel = -1;
 
@@ -756,11 +853,7 @@ static void textview_write_line(TextView *textview, const gchar *str,
                return;
        }
 
-       len = strlen(buf);
-       if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
-               buf[len - 2] = '\n';
-               buf[len - 1] = '\0';
-       }
+       strcrchomp(buf);
        if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
        fg_color = NULL;
 
@@ -793,8 +886,8 @@ static void textview_write_line(TextView *textview, const gchar *str,
                textview_make_clickable_parts(textview, textview->msgfont,
                                              fg_color, &uri_color, buf);
        else
-               gtk_text_insert(text, textview->msgfont, fg_color, NULL,
-                               buf, -1);
+               textview_make_clickable_parts(textview, textview->msgfont,
+                                             fg_color, NULL, buf);
 }
 
 void textview_clear(TextView *textview)
@@ -802,7 +895,8 @@ void textview_clear(TextView *textview)
        GtkText *text = GTK_TEXT(textview->text);
 
        gtk_text_freeze(text);
-       gtk_text_backward_delete(text, gtk_text_get_length(text));
+       gtk_text_set_point(text, 0);
+       gtk_text_forward_delete(text, gtk_text_get_length(text));
        gtk_text_thaw(text);
 
        textview_uri_list_remove_all(textview->uri_list);
@@ -839,11 +933,13 @@ void textview_set_font(TextView *textview, const gchar *codeset)
        if (MB_CUR_MAX > 1) {
                if (codeset) {
                        if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
-                           !g_strncasecmp(codeset, "KOI8-", 5)     ||
-                           !g_strncasecmp(codeset, "CP", 2)        ||
-                           !g_strncasecmp(codeset, "WINDOWS-", 8)  ||
                            !g_strcasecmp(codeset, "BALTIC"))
                                use_fontset = FALSE;
+                       else if (conv_get_current_charset() != C_EUC_JP &&
+                                (!g_strncasecmp(codeset, "KOI8-", 5) ||
+                                 !g_strncasecmp(codeset, "CP", 2)    ||
+                                 !g_strncasecmp(codeset, "WINDOWS-", 8)))
+                               use_fontset = FALSE;
                }
        } else
                use_fontset = FALSE;
@@ -930,6 +1026,16 @@ enum
        H_ORGANIZATION  = 11,
 };
 
+void textview_set_position(TextView *textview, gint pos)
+{
+       if (pos < 0) {
+               textview->cur_pos =
+                       gtk_text_get_length(GTK_TEXT(textview->text));
+       } else {
+               textview->cur_pos = pos;
+       }
+}
+
 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
 {
        gchar buf[BUFFSIZE];
@@ -978,7 +1084,8 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
                }
        }
 
-       g_ptr_array_free(headers, TRUE);
+       g_ptr_array_free(headers, FALSE);
+
        return sorted_headers;
 }
 
@@ -998,7 +1105,9 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
 
                gtk_text_insert(text, textview->boldfont, NULL, NULL,
                                header->name, -1);
-               gtk_text_insert(text, textview->boldfont, NULL, NULL, ":", 2);
+               if (header->name[strlen(header->name) - 1] != ' ')
+                       gtk_text_insert(text, textview->boldfont,
+                                       NULL, NULL, " ", 1);
 
                if (procheader_headername_equal(header->name, "Subject") ||
                    procheader_headername_equal(header->name, "From")    ||
@@ -1018,14 +1127,100 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                                                      NULL, NULL, &uri_color,
                                                      header->body);
                } else {
-                       gtk_text_insert(text, NULL, NULL, NULL,
-                                       header->body, -1);
+                       textview_make_clickable_parts(textview,
+                                                     NULL, NULL, NULL,
+                                                     header->body);
                }
                gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
        }
 
        gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
        gtk_text_thaw(text);
+       textview->body_pos = gtk_text_get_length(text);
+}
+
+gboolean textview_search_string(TextView *textview, const gchar *str,
+                               gboolean case_sens)
+{
+       GtkText *text = GTK_TEXT(textview->text);
+       gint pos;
+       wchar_t *wcs;
+       gint len;
+       gint text_len;
+       gboolean found = FALSE;
+
+       g_return_val_if_fail(str != NULL, FALSE);
+
+       wcs = strdup_mbstowcs(str);
+       g_return_val_if_fail(wcs != NULL, FALSE);
+       len = wcslen(wcs);
+       pos = textview->cur_pos;
+       if (pos < textview->body_pos)
+               pos = textview->body_pos;
+       text_len = gtk_text_get_length(text);
+       if (text_len - pos < len) {
+               g_free(wcs);
+               return FALSE;
+       }
+
+       for (; pos < text_len; pos++) {
+               if (text_len - pos < len) break;
+               if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
+                   == TRUE) {
+                       gtk_editable_set_position(GTK_EDITABLE(text),
+                                                 pos + len);
+                       gtk_editable_select_region(GTK_EDITABLE(text),
+                                                  pos, pos + len);
+                       textview_set_position(textview, pos + len);
+                       found = TRUE;
+                       break;
+               }
+               if (text_len - pos == len) break;
+       }
+
+       g_free(wcs);
+       return found;
+}
+
+gboolean textview_search_string_backward(TextView *textview, const gchar *str,
+                                        gboolean case_sens)
+{
+       GtkText *text = GTK_TEXT(textview->text);
+       gint pos;
+       wchar_t *wcs;
+       gint len;
+       gint text_len;
+       gboolean found = FALSE;
+
+       g_return_val_if_fail(str != NULL, FALSE);
+
+       wcs = strdup_mbstowcs(str);
+       g_return_val_if_fail(wcs != NULL, FALSE);
+       len = wcslen(wcs);
+       pos = textview->cur_pos;
+       text_len = gtk_text_get_length(text);
+       if (text_len - textview->body_pos < len) {
+               g_free(wcs);
+               return FALSE;
+       }
+       if (pos <= textview->body_pos || text_len - pos < len)
+               pos = text_len - len;
+
+       for (; pos >= textview->body_pos; pos--) {
+               if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
+                   == TRUE) {
+                       gtk_editable_set_position(GTK_EDITABLE(text), pos);
+                       gtk_editable_select_region(GTK_EDITABLE(text),
+                                                  pos, pos + len);
+                       textview_set_position(textview, pos - 1);
+                       found = TRUE;
+                       break;
+               }
+               if (pos == textview->body_pos) break;
+       }
+
+       g_free(wcs);
+       return found;
 }
 
 void textview_scroll_one_line(TextView *textview, gboolean up)
@@ -1230,7 +1425,6 @@ static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                        textview_scroll_page(textview, FALSE);
                break;
        case GDK_BackSpace:
-       case GDK_Delete:
                textview_scroll_page(textview, TRUE);
                break;
        case GDK_Return:
@@ -1247,28 +1441,48 @@ static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
 static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
                                    TextView *textview)
 {
+       textview->cur_pos = 
+               gtk_editable_get_position(GTK_EDITABLE(textview->text));
+
        if (event &&
-           ((event->button == 1 && event->type == GDK_2BUTTON_PRESS) 
-               || event->button == 2
-               || event->button == 3)) {
+           ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
+            || event->button == 2 || event->button == 3)) {
                GSList *cur;
-               guint current_pos;
 
-               current_pos = GTK_EDITABLE(textview->text)->current_pos;
+               /* double click seems to set the cursor after the current
+                * word. The cursor position needs fixing, otherwise the
+                * last word of a clickable zone will not work */
+               if (event->button == 1 && event->type == GDK_2BUTTON_PRESS) {
+                       textview->cur_pos--;
+               }
 
                for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
                        RemoteURI *uri = (RemoteURI *)cur->data;
 
-                       if (current_pos >= uri->start &&
-                           current_pos <  uri->end) {
+                       if (textview->cur_pos >= uri->start &&
+                           textview->cur_pos <  uri->end) {
                                if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
-                                       compose_new_with_recipient
-                                               (NULL, uri->uri + 7);
-                               }                                               
-                               else
+                                       if (event->button == 3) {
+                                               gchar *fromname, *fromaddress;
+                                               /* extract url */
+                                               fromaddress = g_strdup(uri->uri + 7);
+                                               /* Hiroyuki: please put this function in utils.c! */
+                                               fromname = procheader_get_fromname(fromaddress);
+                                               extract_address(fromaddress);
+                                               g_message("adding from textview %s <%s>", fromname, fromaddress);
+                                               // Add to address book - Match
+                                               addressbook_add_contact( fromname, fromaddress, NULL );
+                                               g_free(fromaddress);
+                                               g_free(fromname);
+                                       } else {
+                                               compose_new_with_recipient
+                                                       (NULL, uri->uri + 7);
+                                       }
+                               } else {
                                        open_uri(uri->uri,
                                                 prefs_common.uri_cmd);
-                       }                               
+                               }
+                       }
                }
        }
 }