5279b97b81f6d96adaccaf59d89dd32eb9b1d3d9
[claws.git] / src / textview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkvbox.h>
30 #include <gtk/gtkscrolledwindow.h>
31 #include <gtk/gtktext.h>
32 #include <gtk/gtksignal.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include "intl.h"
39 #include "main.h"
40 #include "summaryview.h"
41 #include "procheader.h"
42 #include "prefs_common.h"
43 #include "codeconv.h"
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "procmime.h"
47 #include "html.h"
48 #include "compose.h"
49 #include "addressbook.h"
50 #include "displayheader.h"
51
52 #define FONT_LOAD(font, s) \
53 { \
54         gchar *fontstr, *p; \
55  \
56         Xstrdup_a(fontstr, s, ); \
57         if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
58         font = gdk_font_load(fontstr); \
59 }
60
61 typedef struct _RemoteURI       RemoteURI;
62
63 struct _RemoteURI
64 {
65         gchar *uri;
66
67         guint start;
68         guint end;
69 };
70
71 static GdkColor quote_colors[3] = {
72         {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
73         {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
74         {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
75 };
76
77 static GdkColor uri_color = {
78         (gulong)0,
79         (gushort)0,
80         (gushort)0,
81         (gushort)0
82 };
83
84 static GdkColor emphasis_color = {
85         (gulong)0,
86         (gushort)0,
87         (gushort)0,
88         (gushort)0xcfff
89 };
90
91 static GdkColor error_color = {
92         (gulong)0,
93         (gushort)0xefff,
94         (gushort)0,
95         (gushort)0
96 };
97
98 static GdkFont *spacingfont;
99
100 static void textview_show_html          (TextView       *textview,
101                                          FILE           *fp,
102                                          CodeConverter  *conv);
103 static void textview_write_line         (TextView       *textview,
104                                          const gchar    *str,
105                                          CodeConverter  *conv);
106 static GPtrArray *textview_scan_header  (TextView       *textview,
107                                          FILE           *fp);
108 static void textview_show_header        (TextView       *textview,
109                                          GPtrArray      *headers);
110
111 static void textview_key_pressed        (GtkWidget      *widget,
112                                          GdkEventKey    *event,
113                                          TextView       *textview);
114 static void textview_button_pressed     (GtkWidget      *widget,
115                                          GdkEventButton *event,
116                                          TextView       *textview);
117
118 static void textview_uri_list_remove_all(GSList         *uri_list);
119
120 static void textview_smooth_scroll_do           (TextView       *textview,
121                                                  gfloat          old_value,
122                                                  gfloat          last_value,
123                                                  gint            step);
124 static void textview_smooth_scroll_one_line     (TextView       *textview,
125                                                  gboolean        up);
126 static gboolean textview_smooth_scroll_page     (TextView       *textview,
127                                                  gboolean        up);
128
129
130 TextView *textview_create(void)
131 {
132         TextView *textview;
133         GtkWidget *vbox;
134         GtkWidget *scrolledwin_sb;
135         GtkWidget *scrolledwin_mb;
136         GtkWidget *text_sb;
137         GtkWidget *text_mb;
138
139         debug_print(_("Creating text view...\n"));
140         textview = g_new0(TextView, 1);
141
142         scrolledwin_sb = gtk_scrolled_window_new(NULL, NULL);
143         scrolledwin_mb = gtk_scrolled_window_new(NULL, NULL);
144         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_sb),
145                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
146         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_mb),
147                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
148         gtk_widget_set_usize(scrolledwin_sb, prefs_common.mainview_width, -1);
149         gtk_widget_set_usize(scrolledwin_mb, prefs_common.mainview_width, -1);
150
151         /* create GtkText widgets for single-byte and multi-byte character */
152         text_sb = gtk_text_new(NULL, NULL);
153         text_mb = gtk_text_new(NULL, NULL);
154         GTK_TEXT(text_sb)->default_tab_width = 8;
155         GTK_TEXT(text_mb)->default_tab_width = 8;
156         gtk_widget_show(text_sb);
157         gtk_widget_show(text_mb);
158         gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
159         gtk_text_set_word_wrap(GTK_TEXT(text_mb), TRUE);
160         gtk_widget_ensure_style(text_sb);
161         gtk_widget_ensure_style(text_mb);
162         if (text_sb->style && text_sb->style->font->type == GDK_FONT_FONTSET) {
163                 GtkStyle *style;
164
165                 style = gtk_style_copy(text_sb->style);
166                 gdk_font_unref(style->font);
167                 FONT_LOAD(style->font, NORMAL_FONT);
168                 gtk_widget_set_style(text_sb, style);
169         }
170         if (text_mb->style && text_mb->style->font->type == GDK_FONT_FONT) {
171                 GtkStyle *style;
172
173                 style = gtk_style_copy(text_mb->style);
174                 gdk_font_unref(style->font);
175                 style->font = gdk_fontset_load(NORMAL_FONT);
176                 gtk_widget_set_style(text_mb, style);
177         }
178         gtk_widget_ref(scrolledwin_sb);
179         gtk_widget_ref(scrolledwin_mb);
180
181         gtk_container_add(GTK_CONTAINER(scrolledwin_sb), text_sb);
182         gtk_container_add(GTK_CONTAINER(scrolledwin_mb), text_mb);
183         gtk_signal_connect(GTK_OBJECT(text_sb), "key_press_event",
184                            GTK_SIGNAL_FUNC(textview_key_pressed),
185                            textview);
186         gtk_signal_connect_after(GTK_OBJECT(text_sb), "button_press_event",
187                                  GTK_SIGNAL_FUNC(textview_button_pressed),
188                                  textview);
189         gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
190                            GTK_SIGNAL_FUNC(textview_key_pressed),
191                            textview);
192         gtk_signal_connect_after(GTK_OBJECT(text_mb), "button_press_event",
193                                  GTK_SIGNAL_FUNC(textview_button_pressed),
194                                  textview);
195
196         gtk_widget_show(scrolledwin_sb);
197         gtk_widget_show(scrolledwin_mb);
198
199         vbox = gtk_vbox_new(FALSE, 0);
200         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin_sb, TRUE, TRUE, 0);
201
202         textview->vbox           = vbox;
203         textview->scrolledwin    = scrolledwin_sb;
204         textview->scrolledwin_sb = scrolledwin_sb;
205         textview->scrolledwin_mb = scrolledwin_mb;
206         textview->text           = text_sb;
207         textview->text_sb        = text_sb;
208         textview->text_mb        = text_mb;
209         textview->text_is_mb     = FALSE;
210         textview->uri_list       = NULL;
211         textview->body_pos       = 0;
212         textview->cur_pos        = 0;
213
214         return textview;
215 }
216
217 void textview_init(TextView *textview)
218 {
219         gtkut_widget_disable_theme_engine(textview->text_sb);
220         gtkut_widget_disable_theme_engine(textview->text_mb);
221         textview_update_message_colors();
222         textview_set_font(textview, NULL);
223 }
224
225 void textview_update_message_colors(void)
226 {
227         GdkColor black = {0, 0, 0, 0};
228
229         if (prefs_common.enable_color) {
230                 /* grab the quote colors, converting from an int to a GdkColor */
231                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
232                                                &quote_colors[0]);
233                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
234                                                &quote_colors[1]);
235                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
236                                                &quote_colors[2]);
237                 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
238                                                &uri_color);
239         } else {
240                 quote_colors[0] = quote_colors[1] = quote_colors[2] = 
241                         uri_color = emphasis_color = black;
242         }
243 }
244
245 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
246                            const gchar *file)
247 {
248         FILE *fp;
249
250         if ((fp = fopen(file, "r")) == NULL) {
251                 FILE_OP_ERROR(file, "fopen");
252                 return;
253         }
254
255         textview_show_part(textview, mimeinfo, fp);
256
257         fclose(fp);
258 }
259
260 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
261 {
262         GtkText *text;
263         gchar buf[BUFFSIZE];
264         const gchar *boundary = NULL;
265         gint boundary_len = 0;
266         const gchar *charset = NULL;
267         FILE *tmpfp;
268         GPtrArray *headers = NULL;
269         CodeConverter *conv;
270
271         g_return_if_fail(mimeinfo != NULL);
272         g_return_if_fail(fp != NULL);
273
274         if (mimeinfo->mime_type == MIME_MULTIPART) {
275                 if (mimeinfo->sub) {
276                         mimeinfo = mimeinfo->sub;
277                         if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
278                                 perror("fseek");
279                                 return;
280                         }
281                 } else
282                         return;
283         }
284         if (mimeinfo->parent && mimeinfo->parent->boundary) {
285                 boundary = mimeinfo->parent->boundary;
286                 boundary_len = strlen(boundary);
287         }
288
289         if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
290                 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
291                         perror("fseek");
292                 headers = textview_scan_header(textview, fp);
293         } else {
294                 if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
295                         glong fpos;
296
297                         if ((fpos = ftell(fp)) < 0)
298                                 perror("ftell");
299                         else if (fseek(fp, mimeinfo->parent->fpos, SEEK_SET)
300                                  < 0)
301                                 perror("fseek");
302                         else {
303                                 headers = textview_scan_header(textview, fp);
304                                 if (fseek(fp, fpos, SEEK_SET) < 0)
305                                         perror("fseek");
306                         }
307                 }
308                 while (fgets(buf, sizeof(buf), fp) != NULL)
309                         if (buf[0] == '\r' || buf[0] == '\n') break;
310         }
311
312         /* display attached RFC822 single text message */
313         if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
314                 if (headers) procheader_header_array_destroy(headers);
315                 if (!mimeinfo->sub || mimeinfo->sub->children) return;
316                 headers = textview_scan_header(textview, fp);
317                 mimeinfo = mimeinfo->sub;
318         } else if (!mimeinfo->parent &&
319                    mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
320                 if (headers) procheader_header_array_destroy(headers);
321                 if (!mimeinfo->sub) return;
322                 headers = textview_scan_header(textview, fp);
323                 mimeinfo = mimeinfo->sub;
324         }
325
326         if (prefs_common.force_charset)
327                 charset = prefs_common.force_charset;
328         else if (mimeinfo->charset)
329                 charset = mimeinfo->charset;
330         textview_set_font(textview, charset);
331
332         conv = conv_code_converter_new(charset);
333
334         textview_clear(textview);
335         text = GTK_TEXT(textview->text);
336         gtk_text_freeze(text);
337
338         textview->body_pos = 0;
339         textview->cur_pos  = 0;
340
341         if (headers) {
342                 textview_show_header(textview, headers);
343                 procheader_header_array_destroy(headers);
344         }
345
346         tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
347         if (tmpfp) {
348                 if (mimeinfo->mime_type == MIME_TEXT_HTML)
349                         textview_show_html(textview, tmpfp, conv);
350                 else
351                         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
352                                 textview_write_line(textview, buf, conv);
353                 fclose(tmpfp);
354         }
355
356         conv_code_converter_destroy(conv);
357
358         gtk_text_thaw(text);
359 }
360
361 #define TEXT_INSERT(str) \
362         gtk_text_insert(text, textview->msgfont, NULL, NULL, str, -1)
363
364 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
365 {
366         GtkText *text;
367
368         if (!partinfo) return;
369
370         textview_set_font(textview, NULL);
371         text = GTK_TEXT(textview->text);
372         textview_clear(textview);
373
374         gtk_text_freeze(text);
375
376         TEXT_INSERT(_("To save this part, pop up the context menu with "));
377         TEXT_INSERT(_("right click and select `Save as...', "));
378         TEXT_INSERT(_("or press `y' key.\n\n"));
379
380         TEXT_INSERT(_("To display this part as a text message, select "));
381         TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
382
383         TEXT_INSERT(_("To open this part with external program, select "));
384         TEXT_INSERT(_("`Open' or `Open with...', "));
385         TEXT_INSERT(_("or double-click, or click the center button, "));
386         TEXT_INSERT(_("or press `l' key."));
387
388         gtk_text_thaw(text);
389 }
390
391 #if USE_GPGME
392 void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
393 {
394         GtkText *text;
395
396         if (!partinfo) return;
397
398         textview_set_font(textview, NULL);
399         text = GTK_TEXT(textview->text);
400         textview_clear(textview);
401
402         gtk_text_freeze(text);
403
404         if (partinfo->sigstatus_full == NULL) {
405                 TEXT_INSERT(_("This signature has not been checked yet.\n"));
406                 TEXT_INSERT(_("To check it, pop up the context menu with\n"));
407                 TEXT_INSERT(_("right click and select `Check signature'.\n"));
408         } else {
409                 TEXT_INSERT(partinfo->sigstatus_full);
410         }
411
412         gtk_text_thaw(text);
413 }
414 #endif /* USE_GPGME */
415
416 #undef TEXT_INSERT
417
418 static void textview_show_html(TextView *textview, FILE *fp,
419                                CodeConverter *conv)
420 {
421         HTMLParser *parser;
422         gchar *str;
423
424         parser = html_parser_new(fp, conv);
425         g_return_if_fail(parser != NULL);
426
427         while ((str = html_parse(parser)) != NULL) {
428                 textview_write_line(textview, str, NULL);
429         }
430         html_parser_destroy(parser);
431 }
432
433 /* get_uri_part() - retrieves a URI starting from scanpos.
434                     Returns TRUE if succesful */
435 static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
436                              const gchar **bp, const gchar **ep)
437 {
438         const gchar *ep_;
439
440         g_return_val_if_fail(start != NULL, FALSE);
441         g_return_val_if_fail(scanpos != NULL, FALSE);
442         g_return_val_if_fail(bp != NULL, FALSE);
443         g_return_val_if_fail(ep != NULL, FALSE);
444
445         *bp = scanpos;
446
447         /* find end point of URI */
448         for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
449                 if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
450                         break;
451         }
452
453         /* no punctuation at end of string */
454
455         /* FIXME: this stripping of trailing punctuations may bite with other URIs.
456          * should pass some URI type to this function and decide on that whether
457          * to perform punctuation stripping */
458
459 #define IS_REAL_PUNCT(ch) \
460         (ispunct(ch) && ((ch) != '/')) 
461
462         for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
463                 ;
464
465 #undef IS_REAL_PUNCT
466
467         *ep = ep_;
468
469         return TRUE;            
470 }
471
472 static gchar *make_uri_string(const gchar *bp, const gchar *ep)
473 {
474         return g_strndup(bp, ep - bp);
475 }
476
477 /* valid mail address characters */
478 #define IS_RFC822_CHAR(ch) \
479         (isascii(ch) && \
480          (ch) > 32   && \
481          (ch) != 127 && \
482          !isspace(ch) && \
483          !strchr("()<>\"", (ch)))
484
485 #define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
486
487 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
488 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
489                                const gchar **bp, const gchar **ep)
490 {
491         /* more complex than the uri part because we need to scan back and forward starting from
492          * the scan position. */
493         gboolean result = FALSE;
494         const gchar *bp_ = NULL;
495         const gchar *ep_ = NULL;
496
497         /* the informative part of the email address (describing the name
498          * of the email address owner) may contain quoted parts. the
499          * closure stack stores the last encountered quotes. */
500         gchar closure_stack[128];
501         gchar *ptr = closure_stack;
502
503         g_return_val_if_fail(start != NULL, FALSE);
504         g_return_val_if_fail(scanpos != NULL, FALSE);
505         g_return_val_if_fail(bp != NULL, FALSE);
506         g_return_val_if_fail(ep != NULL, FALSE);
507
508         /* scan start of address */
509         for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
510                 ;
511
512         /* TODO: should start with an alnum? */
513         bp_++;
514         for (; bp_ < scanpos && !isalnum(*bp_); bp_++)
515                 ;
516
517         if (bp_ != scanpos) {
518                 /* scan end of address */
519                 for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
520                         ;
521
522                 /* TODO: really should terminate with an alnum? */
523                 for (; ep_ > scanpos  && !isalnum(*ep_); --ep_)
524                         ;
525                 ep_++;
526
527                 if (ep_ > scanpos + 1) {
528                         *ep = ep_;
529                         *bp = bp_;
530                         result = TRUE;
531                 }
532         }
533
534         if (!result) return FALSE;
535
536         /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
537         if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_)) 
538                 return FALSE;
539
540         /* see if this is <bracketed>; in this case we also scan for the informative part. */
541         if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
542                 return TRUE;
543
544 #define FULL_STACK()    ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
545 #define IN_STACK()      (ptr > closure_stack)
546 /* has underrun check */
547 #define POP_STACK()     if(IN_STACK()) --ptr
548 /* has overrun check */
549 #define PUSH_STACK(c)   if(!FULL_STACK()) *ptr++ = (c); else return TRUE
550 /* has underrun check */
551 #define PEEK_STACK()    (IN_STACK() ? *(ptr - 1) : 0)
552
553         ep_++;
554
555         /* scan for the informative part. */
556         for (bp_ -= 2; bp_ >= start; bp_--) {
557                 /* if closure on the stack keep scanning */
558                 if (PEEK_STACK() == *bp_) {
559                         POP_STACK();
560                         continue;
561                 }
562                 if (*bp_ == '\'' || *bp_ == '"') {
563                         PUSH_STACK(*bp_);
564                         continue;
565                 }
566
567                 /* if nothing in the closure stack, do the special conditions
568                  * the following if..else expression simply checks whether 
569                  * a token is acceptable. if not acceptable, the clause
570                  * should terminate the loop with a 'break' */
571                 if (!PEEK_STACK()) {
572                         if (*bp_ == '-'
573                         && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
574                         && (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1)))) {
575                                 /* hyphens are allowed, but only in
576                                    between alnums */
577                         } else if (!ispunct(*bp_)) {
578                                 /* but anything not being a punctiation
579                                    is ok */
580                         } else {
581                                 break; /* anything else is rejected */
582                         }
583                 }
584         }
585
586         bp_++;
587
588 #undef PEEK_STACK
589 #undef PUSH_STACK
590 #undef POP_STACK
591 #undef IN_STACK
592 #undef FULL_STACK
593
594         /* scan forward (should start with an alnum) */
595         for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
596                 ;
597
598         *ep = ep_;
599         *bp = bp_;
600
601         return result;
602 }
603
604 #undef IS_QUOTE
605 #undef IS_RFC822_CHAR
606
607 static gchar *make_email_string(const gchar *bp, const gchar *ep)
608 {
609         /* returns a mailto: URI; mailto: is also used to detect the
610          * uri type later on in the button_pressed signal handler */
611         gchar *tmp;
612         gchar *result;
613
614         tmp = g_strndup(bp, ep - bp);
615         result = g_strconcat("mailto:", tmp, NULL);
616         g_free(tmp);
617
618         return result;
619 }
620
621 #define ADD_TXT_POS(bp_, ep_, pti_) \
622         if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
623                 last = last->next; \
624                 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
625                 last->next = NULL; \
626         } else { \
627                 g_warning("alloc error scanning URIs\n"); \
628                 gtk_text_insert(text, textview->msgfont, fg_color, NULL, \
629                                 linebuf, -1); \
630                 return; \
631         }
632
633 /* textview_make_clickable_parts() - colorizes clickable parts */
634 static void textview_make_clickable_parts(TextView *textview,
635                                           GdkFont *font,
636                                           GdkColor *fg_color,
637                                           GdkColor *uri_color,
638                                           const gchar *linebuf)
639 {
640         /* parse table - in order of priority */
641         struct table {
642                 const gchar *needle; /* token */
643
644                 /* token search function */
645                 gchar    *(*search)     (const gchar *haystack,
646                                          const gchar *needle);
647                 /* part parsing function */
648                 gboolean  (*parse)      (const gchar *start,
649                                          const gchar *scanpos,
650                                          const gchar **bp_,
651                                          const gchar **ep_);
652                 /* part to URI function */
653                 gchar    *(*build_uri)  (const gchar *bp,
654                                          const gchar *ep);
655         };
656
657         static struct table parser[] = {
658                 {"http://",  strcasestr, get_uri_part,   make_uri_string},
659                 {"https://", strcasestr, get_uri_part,   make_uri_string},
660                 {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
661                 {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
662                 {"@",        strcasestr, get_email_part, make_email_string}
663         };
664         const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
665
666         gint  n;
667         const gchar *walk, *bp, *ep;
668
669         struct txtpos {
670                 const gchar     *bp, *ep;       /* text position */
671                 gint             pti;           /* index in parse table */
672                 struct txtpos   *next;          /* next */
673         } head = {NULL, NULL, 0,  NULL}, *last = &head;
674
675         GtkText *text = GTK_TEXT(textview->text);
676
677         /* parse for clickable parts, and build a list of begin and end positions  */
678         for (walk = linebuf, n = 0;;) {
679                 gint last_index = PARSE_ELEMS;
680                 gchar *scanpos = NULL;
681
682                 /* FIXME: this looks phony. scanning for anything in the parse table */
683                 for (n = 0; n < PARSE_ELEMS; n++) {
684                         gchar *tmp;
685
686                         tmp = parser[n].search(walk, parser[n].needle);
687                         if (tmp) {
688                                 if (scanpos == NULL || tmp < scanpos) {
689                                         scanpos = tmp;
690                                         last_index = n;
691                                 }
692                         }                                       
693                 }
694
695                 if (scanpos) {
696                         /* check if URI can be parsed */
697                         if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
698                             && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
699                                         ADD_TXT_POS(bp, ep, last_index);
700                                         walk = ep;
701                         } else
702                                 walk = scanpos +
703                                         strlen(parser[last_index].needle);
704                 } else
705                         break;
706         }
707
708         /* colorize this line */
709         if (head.next) {
710                 const gchar *normal_text = linebuf;
711
712                 /* insert URIs */
713                 for (last = head.next; last != NULL;
714                      normal_text = last->ep, last = last->next) {
715                         RemoteURI *uri;
716
717                         uri = g_new(RemoteURI, 1);
718                         if (last->bp - normal_text > 0)
719                                 gtk_text_insert(text, font,
720                                                 fg_color, NULL,
721                                                 normal_text,
722                                                 last->bp - normal_text);
723                         uri->uri = parser[last->pti].build_uri(last->bp, 
724                                                                last->ep);
725                         uri->start = gtk_text_get_point(text);
726                         gtk_text_insert(text, font, uri_color,
727                                         NULL, last->bp, last->ep - last->bp);
728                         uri->end = gtk_text_get_point(text);
729                         textview->uri_list =
730                                 g_slist_append(textview->uri_list, uri);
731                 }
732
733                 if (*normal_text)
734                         gtk_text_insert(text, font, fg_color,
735                                         NULL, normal_text, -1);
736         } else
737                 gtk_text_insert(text, font, fg_color, NULL, linebuf, -1);
738 }
739
740 #undef ADD_TXT_POS
741
742 static void textview_write_line(TextView *textview, const gchar *str,
743                                 CodeConverter *conv)
744 {
745         GtkText *text = GTK_TEXT(textview->text);
746         gchar buf[BUFFSIZE];
747         size_t len;
748         GdkColor *fg_color;
749         gint quotelevel = -1;
750
751         if (!conv)
752                 strncpy2(buf, str, sizeof(buf));
753         else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
754                 gtk_text_insert(text, textview->msgfont,
755                                 prefs_common.enable_color
756                                 ? &error_color : NULL, NULL,
757                                 "*** Warning: code conversion failed ***\n",
758                                 -1);
759                 return;
760         }
761
762         len = strlen(buf);
763         if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
764                 buf[len - 2] = '\n';
765                 buf[len - 1] = '\0';
766         }
767         if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
768         fg_color = NULL;
769
770         /* change color of quotation
771            >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
772            Up to 3 levels of quotations are detected, and each
773            level is colored using a different color. */
774         if (prefs_common.enable_color && strchr(buf, '>')) {
775                 quotelevel = get_quote_level(buf);
776
777                 /* set up the correct foreground color */
778                 if (quotelevel > 2) {
779                         /* recycle colors */
780                         if (prefs_common.recycle_quote_colors)
781                                 quotelevel %= 3;
782                         else
783                                 quotelevel = 2;
784                 }
785         }
786
787         if (quotelevel == -1)
788                 fg_color = NULL;
789         else
790                 fg_color = &quote_colors[quotelevel];
791
792         if (prefs_common.head_space && spacingfont && buf[0] != '\n')
793                 gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
794
795         if (prefs_common.enable_color)
796                 textview_make_clickable_parts(textview, textview->msgfont,
797                                               fg_color, &uri_color, buf);
798         else
799                 textview_make_clickable_parts(textview, textview->msgfont,
800                                               fg_color, NULL, buf);
801 }
802
803 void textview_clear(TextView *textview)
804 {
805         GtkText *text = GTK_TEXT(textview->text);
806
807         gtk_text_freeze(text);
808         gtk_text_set_point(text, 0);
809         gtk_text_forward_delete(text, gtk_text_get_length(text));
810         gtk_text_thaw(text);
811
812         textview_uri_list_remove_all(textview->uri_list);
813         textview->uri_list = NULL;
814 }
815
816 void textview_destroy(TextView *textview)
817 {
818         textview_uri_list_remove_all(textview->uri_list);
819         textview->uri_list = NULL;
820
821         if (!textview->scrolledwin_sb->parent)
822                 gtk_widget_destroy(textview->scrolledwin_sb);
823         if (!textview->scrolledwin_mb->parent)
824                 gtk_widget_destroy(textview->scrolledwin_mb);
825
826         if (textview->msgfont) {
827                 textview->msgfont->ascent = textview->prev_ascent;
828                 textview->msgfont->descent = textview->prev_descent;
829                 gdk_font_unref(textview->msgfont);
830         }
831         if (textview->boldfont)
832                 gdk_font_unref(textview->boldfont);
833
834         g_free(textview);
835 }
836
837 void textview_set_font(TextView *textview, const gchar *codeset)
838 {
839         gboolean use_fontset = TRUE;
840
841         /* In multi-byte mode, GtkText can't display 8bit characters
842            correctly, so it must be single-byte mode. */
843         if (MB_CUR_MAX > 1) {
844                 if (codeset) {
845                         if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
846                             !g_strncasecmp(codeset, "KOI8-", 5)     ||
847                             !g_strncasecmp(codeset, "CP", 2)        ||
848                             !g_strncasecmp(codeset, "WINDOWS-", 8)  ||
849                             !g_strcasecmp(codeset, "BALTIC"))
850                                 use_fontset = FALSE;
851                 }
852         } else
853                 use_fontset = FALSE;
854
855         if (textview->text_is_mb && !use_fontset) {
856                 GtkWidget *parent;
857
858                 parent = textview->scrolledwin_mb->parent;
859                 gtk_editable_select_region
860                         (GTK_EDITABLE(textview->text_mb), 0, 0);
861                 gtk_container_remove(GTK_CONTAINER(parent),
862                                      textview->scrolledwin_mb);
863                 gtk_container_add(GTK_CONTAINER(parent),
864                                   textview->scrolledwin_sb);
865
866                 textview->text = textview->text_sb;
867                 textview->text_is_mb = FALSE;
868         } else if (!textview->text_is_mb && use_fontset) {
869                 GtkWidget *parent;
870
871                 parent = textview->scrolledwin_sb->parent;
872                 gtk_editable_select_region
873                         (GTK_EDITABLE(textview->text_sb), 0, 0);
874                 gtk_container_remove(GTK_CONTAINER(parent),
875                                      textview->scrolledwin_sb);
876                 gtk_container_add(GTK_CONTAINER(parent),
877                                   textview->scrolledwin_mb);
878
879                 textview->text = textview->text_mb;
880                 textview->text_is_mb = TRUE;
881         }
882
883         if (prefs_common.textfont) {
884                 if (textview->msgfont) {
885                         textview->msgfont->ascent = textview->prev_ascent;
886                         textview->msgfont->descent = textview->prev_descent;
887                         gdk_font_unref(textview->msgfont);
888                         textview->msgfont = NULL;
889                 }
890                 if (use_fontset)
891                         textview->msgfont =
892                                 gdk_fontset_load(prefs_common.textfont);
893                 else {
894                         if (MB_CUR_MAX > 1) {
895                                 FONT_LOAD(textview->msgfont,
896                                           "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
897                         } else {
898                                 FONT_LOAD(textview->msgfont,
899                                           prefs_common.textfont);
900                         }
901                 }
902
903                 if (textview->msgfont) {
904                         gint ascent, descent;
905
906                         textview->prev_ascent = textview->msgfont->ascent;
907                         textview->prev_descent = textview->msgfont->descent;
908                         descent = prefs_common.line_space / 2;
909                         ascent  = prefs_common.line_space - descent;
910                         textview->msgfont->ascent  += ascent;
911                         textview->msgfont->descent += descent;
912                 }
913         }
914
915         if (!textview->boldfont)
916                 FONT_LOAD(textview->boldfont, BOLD_FONT);
917         if (!spacingfont)
918                 spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
919 }
920
921 enum
922 {
923         H_DATE          = 0,
924         H_FROM          = 1,
925         H_TO            = 2,
926         H_NEWSGROUPS    = 3,
927         H_SUBJECT       = 4,
928         H_CC            = 5,
929         H_REPLY_TO      = 6,
930         H_FOLLOWUP_TO   = 7,
931         H_X_MAILER      = 8,
932         H_X_NEWSREADER  = 9,
933         H_USER_AGENT    = 10,
934         H_ORGANIZATION  = 11,
935 };
936
937 void textview_set_position(TextView *textview, gint pos)
938 {
939         if (pos < 0) {
940                 textview->cur_pos =
941                         gtk_text_get_length(GTK_TEXT(textview->text));
942         } else {
943                 textview->cur_pos = pos;
944         }
945 }
946
947 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
948 {
949         gchar buf[BUFFSIZE];
950         GPtrArray *headers, *sorted_headers;
951         GSList *disphdr_list;
952         Header *header;
953         gint i;
954
955         g_return_val_if_fail(fp != NULL, NULL);
956
957         if (!prefs_common.display_header) {
958                 while (fgets(buf, sizeof(buf), fp) != NULL)
959                         if (buf[0] == '\r' || buf[0] == '\n') break;
960                 return NULL;
961         }
962
963         headers = procheader_get_header_array_asis(fp);
964
965         sorted_headers = g_ptr_array_new();
966
967         for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
968              disphdr_list = disphdr_list->next) {
969                 DisplayHeaderProp *dp =
970                         (DisplayHeaderProp *)disphdr_list->data;
971
972                 for (i = 0; i < headers->len; i++) {
973                         header = g_ptr_array_index(headers, i);
974
975                         if (procheader_headername_equal(header->name,
976                                                         dp->name)) {
977                                 if (dp->hidden)
978                                         procheader_header_free(header);
979                                 else
980                                         g_ptr_array_add(sorted_headers, header);
981
982                                 g_ptr_array_remove_index(headers, i);
983                                 i--;
984                         }
985                 }
986         }
987
988         if (prefs_common.show_other_header) {
989                 for (i = 0; i < headers->len; i++) {
990                         header = g_ptr_array_index(headers, i);
991                         g_ptr_array_add(sorted_headers, header);
992                 }
993         }
994
995         g_ptr_array_free(headers, FALSE);
996
997         return sorted_headers;
998 }
999
1000 static void textview_show_header(TextView *textview, GPtrArray *headers)
1001 {
1002         GtkText *text = GTK_TEXT(textview->text);
1003         Header *header;
1004         gint i;
1005
1006         g_return_if_fail(headers != NULL);
1007
1008         gtk_text_freeze(text);
1009
1010         for (i = 0; i < headers->len; i++) {
1011                 header = g_ptr_array_index(headers, i);
1012                 g_return_if_fail(header->name != NULL);
1013
1014                 gtk_text_insert(text, textview->boldfont, NULL, NULL,
1015                                 header->name, -1);
1016                 if (header->name[strlen(header->name) - 1] != ' ')
1017                         gtk_text_insert(text, textview->boldfont,
1018                                         NULL, NULL, " ", 1);
1019
1020                 if (procheader_headername_equal(header->name, "Subject") ||
1021                     procheader_headername_equal(header->name, "From")    ||
1022                     procheader_headername_equal(header->name, "To")      ||
1023                     procheader_headername_equal(header->name, "Cc"))
1024                         unfold_line(header->body);
1025
1026                 if (prefs_common.enable_color &&
1027                     (procheader_headername_equal(header->name, "X-Mailer") ||
1028                      procheader_headername_equal(header->name,
1029                                                  "X-Newsreader")) &&
1030                     strstr(header->body, "Sylpheed") != NULL)
1031                         gtk_text_insert(text, NULL, &emphasis_color, NULL,
1032                                         header->body, -1);
1033                 else if (prefs_common.enable_color) {
1034                         textview_make_clickable_parts(textview,
1035                                                       NULL, NULL, &uri_color,
1036                                                       header->body);
1037                 } else {
1038                         textview_make_clickable_parts(textview,
1039                                                       NULL, NULL, NULL,
1040                                                       header->body);
1041                 }
1042                 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1043         }
1044
1045         gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1046         gtk_text_thaw(text);
1047         textview->body_pos = gtk_text_get_length(text);
1048 }
1049
1050 gboolean textview_search_string(TextView *textview, const gchar *str,
1051                                 gboolean case_sens)
1052 {
1053         GtkText *text = GTK_TEXT(textview->text);
1054         gint pos;
1055         wchar_t *wcs;
1056         gint len;
1057         gint text_len;
1058         gboolean found = FALSE;
1059
1060         g_return_val_if_fail(str != NULL, FALSE);
1061
1062         wcs = strdup_mbstowcs(str);
1063         g_return_val_if_fail(wcs != NULL, FALSE);
1064         len = wcslen(wcs);
1065         pos = textview->cur_pos;
1066         if (pos < textview->body_pos)
1067                 pos = textview->body_pos;
1068         text_len = gtk_text_get_length(text);
1069         if (text_len - pos < len) {
1070                 g_free(wcs);
1071                 return FALSE;
1072         }
1073
1074         for (; pos < text_len; pos++) {
1075                 if (text_len - pos < len) break;
1076                 if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
1077                     == TRUE) {
1078                         gtk_editable_set_position(GTK_EDITABLE(text),
1079                                                   pos + len);
1080                         gtk_editable_select_region(GTK_EDITABLE(text),
1081                                                    pos, pos + len);
1082                         textview_set_position(textview, pos + len);
1083                         found = TRUE;
1084                         break;
1085                 }
1086                 if (text_len - pos == len) break;
1087         }
1088
1089         g_free(wcs);
1090         return found;
1091 }
1092
1093 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
1094                                          gboolean case_sens)
1095 {
1096         GtkText *text = GTK_TEXT(textview->text);
1097         gint pos;
1098         wchar_t *wcs;
1099         gint len;
1100         gint text_len;
1101         gboolean found = FALSE;
1102
1103         g_return_val_if_fail(str != NULL, FALSE);
1104
1105         wcs = strdup_mbstowcs(str);
1106         g_return_val_if_fail(wcs != NULL, FALSE);
1107         len = wcslen(wcs);
1108         pos = textview->cur_pos;
1109         text_len = gtk_text_get_length(text);
1110         if (text_len - textview->body_pos < len) {
1111                 g_free(wcs);
1112                 return FALSE;
1113         }
1114         if (pos <= textview->body_pos || text_len - pos < len)
1115                 pos = text_len - len;
1116
1117         for (; pos >= textview->body_pos; pos--) {
1118                 if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
1119                     == TRUE) {
1120                         gtk_editable_set_position(GTK_EDITABLE(text), pos);
1121                         gtk_editable_select_region(GTK_EDITABLE(text),
1122                                                    pos, pos + len);
1123                         textview_set_position(textview, pos - 1);
1124                         found = TRUE;
1125                         break;
1126                 }
1127                 if (pos == textview->body_pos) break;
1128         }
1129
1130         g_free(wcs);
1131         return found;
1132 }
1133
1134 void textview_scroll_one_line(TextView *textview, gboolean up)
1135 {
1136         GtkText *text = GTK_TEXT(textview->text);
1137         gfloat upper;
1138
1139         if (prefs_common.enable_smooth_scroll) {
1140                 textview_smooth_scroll_one_line(textview, up);
1141                 return;
1142         }
1143
1144         if (!up) {
1145                 upper = text->vadj->upper - text->vadj->page_size;
1146                 if (text->vadj->value < upper) {
1147                         text->vadj->value +=
1148                                 text->vadj->step_increment * 4;
1149                         text->vadj->value =
1150                                 MIN(text->vadj->value, upper);
1151                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1152                                                 "value_changed");
1153                 }
1154         } else {
1155                 if (text->vadj->value > 0.0) {
1156                         text->vadj->value -=
1157                                 text->vadj->step_increment * 4;
1158                         text->vadj->value =
1159                                 MAX(text->vadj->value, 0.0);
1160                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1161                                                 "value_changed");
1162                 }
1163         }
1164 }
1165
1166 gboolean textview_scroll_page(TextView *textview, gboolean up)
1167 {
1168         GtkText *text = GTK_TEXT(textview->text);
1169         gfloat upper;
1170         gfloat page_incr;
1171
1172         if (prefs_common.enable_smooth_scroll)
1173                 return textview_smooth_scroll_page(textview, up);
1174
1175         if (prefs_common.scroll_halfpage)
1176                 page_incr = text->vadj->page_increment / 2;
1177         else
1178                 page_incr = text->vadj->page_increment;
1179
1180         if (!up) {
1181                 upper = text->vadj->upper - text->vadj->page_size;
1182                 if (text->vadj->value < upper) {
1183                         text->vadj->value += page_incr;
1184                         text->vadj->value = MIN(text->vadj->value, upper);
1185                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1186                                                 "value_changed");
1187                 } else
1188                         return FALSE;
1189         } else {
1190                 if (text->vadj->value > 0.0) {
1191                         text->vadj->value -= page_incr;
1192                         text->vadj->value = MAX(text->vadj->value, 0.0);
1193                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1194                                                 "value_changed");
1195                 } else
1196                         return FALSE;
1197         }
1198
1199         return TRUE;
1200 }
1201
1202 static void textview_smooth_scroll_do(TextView *textview,
1203                                       gfloat old_value, gfloat last_value,
1204                                       gint step)
1205 {
1206         GtkText *text = GTK_TEXT(textview->text);
1207         gint change_value;
1208         gboolean up;
1209         gint i;
1210
1211         if (old_value < last_value) {
1212                 change_value = last_value - old_value;
1213                 up = FALSE;
1214         } else {
1215                 change_value = old_value - last_value;
1216                 up = TRUE;
1217         }
1218
1219         gdk_key_repeat_disable();
1220
1221         for (i = step; i <= change_value; i += step) {
1222                 text->vadj->value = old_value + (up ? -i : i);
1223                 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1224                                         "value_changed");
1225         }
1226
1227         text->vadj->value = last_value;
1228         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
1229
1230         gdk_key_repeat_restore();
1231 }
1232
1233 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1234 {
1235         GtkText *text = GTK_TEXT(textview->text);
1236         gfloat upper;
1237         gfloat old_value;
1238         gfloat last_value;
1239
1240         if (!up) {
1241                 upper = text->vadj->upper - text->vadj->page_size;
1242                 if (text->vadj->value < upper) {
1243                         old_value = text->vadj->value;
1244                         last_value = text->vadj->value +
1245                                 text->vadj->step_increment * 4;
1246                         last_value = MIN(last_value, upper);
1247
1248                         textview_smooth_scroll_do(textview, old_value,
1249                                                   last_value,
1250                                                   prefs_common.scroll_step);
1251                 }
1252         } else {
1253                 if (text->vadj->value > 0.0) {
1254                         old_value = text->vadj->value;
1255                         last_value = text->vadj->value -
1256                                 text->vadj->step_increment * 4;
1257                         last_value = MAX(last_value, 0.0);
1258
1259                         textview_smooth_scroll_do(textview, old_value,
1260                                                   last_value,
1261                                                   prefs_common.scroll_step);
1262                 }
1263         }
1264 }
1265
1266 static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1267 {
1268         GtkText *text = GTK_TEXT(textview->text);
1269         gfloat upper;
1270         gfloat page_incr;
1271         gfloat old_value;
1272         gfloat last_value;
1273
1274         if (prefs_common.scroll_halfpage)
1275                 page_incr = text->vadj->page_increment / 2;
1276         else
1277                 page_incr = text->vadj->page_increment;
1278
1279         if (!up) {
1280                 upper = text->vadj->upper - text->vadj->page_size;
1281                 if (text->vadj->value < upper) {
1282                         old_value = text->vadj->value;
1283                         last_value = text->vadj->value + page_incr;
1284                         last_value = MIN(last_value, upper);
1285
1286                         textview_smooth_scroll_do(textview, old_value,
1287                                                   last_value,
1288                                                   prefs_common.scroll_step);
1289                 } else
1290                         return FALSE;
1291         } else {
1292                 if (text->vadj->value > 0.0) {
1293                         old_value = text->vadj->value;
1294                         last_value = text->vadj->value - page_incr;
1295                         last_value = MAX(last_value, 0.0);
1296
1297                         textview_smooth_scroll_do(textview, old_value,
1298                                                   last_value,
1299                                                   prefs_common.scroll_step);
1300                 } else
1301                         return FALSE;
1302         }
1303
1304         return TRUE;
1305 }
1306
1307 static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1308                                  TextView *textview)
1309 {
1310         SummaryView *summaryview = NULL;
1311
1312         if (!event) return;
1313         if (textview->messageview->mainwin)
1314                 summaryview = textview->messageview->mainwin->summaryview;
1315
1316         switch (event->keyval) {
1317         case GDK_Tab:
1318         case GDK_Home:
1319         case GDK_Left:
1320         case GDK_Up:
1321         case GDK_Right:
1322         case GDK_Down:
1323         case GDK_Page_Up:
1324         case GDK_Page_Down:
1325         case GDK_End:
1326         case GDK_Control_L:
1327         case GDK_Control_R:
1328                 break;
1329         case GDK_space:
1330                 if (summaryview)
1331                         summary_pass_key_press_event(summaryview, event);
1332                 else
1333                         textview_scroll_page(textview, FALSE);
1334                 break;
1335         case GDK_BackSpace:
1336         case GDK_Delete:
1337                 textview_scroll_page(textview, TRUE);
1338                 break;
1339         case GDK_Return:
1340                 textview_scroll_one_line(textview,
1341                                          (event->state & GDK_MOD1_MASK) != 0);
1342                 break;
1343         default:
1344                 if (summaryview)
1345                         summary_pass_key_press_event(summaryview, event);
1346                 break;
1347         }
1348 }
1349
1350 static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1351                                     TextView *textview)
1352 {
1353         textview->cur_pos = 
1354                 gtk_editable_get_position(GTK_EDITABLE(textview->text));
1355
1356         if (event &&
1357             ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1358              || event->button == 2 || event->button == 3)) {
1359                 GSList *cur;
1360
1361                 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1362                         RemoteURI *uri = (RemoteURI *)cur->data;
1363
1364                         if (textview->cur_pos >= uri->start &&
1365                             textview->cur_pos <  uri->end) {
1366                                 if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
1367                                         if (event->button == 3) {
1368                                                 gchar *fromname, *fromaddress;
1369                                                 /* extract url */
1370                                                 fromaddress = g_strdup(uri->uri + 7);
1371                                                 /* Hiroyuki: please put this function in utils.c! */
1372                                                 fromname = procheader_get_fromname(fromaddress);
1373                                                 extract_address(fromaddress);
1374                                                 g_message("adding from textview %s <%s>", fromname, fromaddress);
1375                                                 // Add to address book - Match
1376                                                 addressbook_add_contact( fromname, fromaddress, NULL );
1377                                                 g_free(fromaddress);
1378                                                 g_free(fromname);
1379                                         } else {
1380                                                 compose_new_with_recipient
1381                                                         (NULL, uri->uri + 7);
1382                                         }
1383                                 } else {
1384                                         open_uri(uri->uri,
1385                                                  prefs_common.uri_cmd);
1386                                 }
1387                         }
1388                 }
1389         }
1390 }
1391
1392 static void textview_uri_list_remove_all(GSList *uri_list)
1393 {
1394         GSList *cur;
1395
1396         for (cur = uri_list; cur != NULL; cur = cur->next) {
1397                 if (cur->data) {
1398                         g_free(((RemoteURI *)cur->data)->uri);
1399                         g_free(cur->data);
1400                 }
1401         }
1402
1403         g_slist_free(uri_list);
1404 }