sync with sylpheed 0.6.4cvs1
[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_strcasecmp(codeset, "BALTIC"))
847                                 use_fontset = FALSE;
848                         else if (conv_get_current_charset() != C_EUC_JP &&
849                                  (!g_strncasecmp(codeset, "KOI8-", 5) ||
850                                   !g_strncasecmp(codeset, "CP", 2)    ||
851                                   !g_strncasecmp(codeset, "WINDOWS-", 8)))
852                                 use_fontset = FALSE;
853                 }
854         } else
855                 use_fontset = FALSE;
856
857         if (textview->text_is_mb && !use_fontset) {
858                 GtkWidget *parent;
859
860                 parent = textview->scrolledwin_mb->parent;
861                 gtk_editable_select_region
862                         (GTK_EDITABLE(textview->text_mb), 0, 0);
863                 gtk_container_remove(GTK_CONTAINER(parent),
864                                      textview->scrolledwin_mb);
865                 gtk_container_add(GTK_CONTAINER(parent),
866                                   textview->scrolledwin_sb);
867
868                 textview->text = textview->text_sb;
869                 textview->text_is_mb = FALSE;
870         } else if (!textview->text_is_mb && use_fontset) {
871                 GtkWidget *parent;
872
873                 parent = textview->scrolledwin_sb->parent;
874                 gtk_editable_select_region
875                         (GTK_EDITABLE(textview->text_sb), 0, 0);
876                 gtk_container_remove(GTK_CONTAINER(parent),
877                                      textview->scrolledwin_sb);
878                 gtk_container_add(GTK_CONTAINER(parent),
879                                   textview->scrolledwin_mb);
880
881                 textview->text = textview->text_mb;
882                 textview->text_is_mb = TRUE;
883         }
884
885         if (prefs_common.textfont) {
886                 if (textview->msgfont) {
887                         textview->msgfont->ascent = textview->prev_ascent;
888                         textview->msgfont->descent = textview->prev_descent;
889                         gdk_font_unref(textview->msgfont);
890                         textview->msgfont = NULL;
891                 }
892                 if (use_fontset)
893                         textview->msgfont =
894                                 gdk_fontset_load(prefs_common.textfont);
895                 else {
896                         if (MB_CUR_MAX > 1) {
897                                 FONT_LOAD(textview->msgfont,
898                                           "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
899                         } else {
900                                 FONT_LOAD(textview->msgfont,
901                                           prefs_common.textfont);
902                         }
903                 }
904
905                 if (textview->msgfont) {
906                         gint ascent, descent;
907
908                         textview->prev_ascent = textview->msgfont->ascent;
909                         textview->prev_descent = textview->msgfont->descent;
910                         descent = prefs_common.line_space / 2;
911                         ascent  = prefs_common.line_space - descent;
912                         textview->msgfont->ascent  += ascent;
913                         textview->msgfont->descent += descent;
914                 }
915         }
916
917         if (!textview->boldfont)
918                 FONT_LOAD(textview->boldfont, BOLD_FONT);
919         if (!spacingfont)
920                 spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
921 }
922
923 enum
924 {
925         H_DATE          = 0,
926         H_FROM          = 1,
927         H_TO            = 2,
928         H_NEWSGROUPS    = 3,
929         H_SUBJECT       = 4,
930         H_CC            = 5,
931         H_REPLY_TO      = 6,
932         H_FOLLOWUP_TO   = 7,
933         H_X_MAILER      = 8,
934         H_X_NEWSREADER  = 9,
935         H_USER_AGENT    = 10,
936         H_ORGANIZATION  = 11,
937 };
938
939 void textview_set_position(TextView *textview, gint pos)
940 {
941         if (pos < 0) {
942                 textview->cur_pos =
943                         gtk_text_get_length(GTK_TEXT(textview->text));
944         } else {
945                 textview->cur_pos = pos;
946         }
947 }
948
949 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
950 {
951         gchar buf[BUFFSIZE];
952         GPtrArray *headers, *sorted_headers;
953         GSList *disphdr_list;
954         Header *header;
955         gint i;
956
957         g_return_val_if_fail(fp != NULL, NULL);
958
959         if (!prefs_common.display_header) {
960                 while (fgets(buf, sizeof(buf), fp) != NULL)
961                         if (buf[0] == '\r' || buf[0] == '\n') break;
962                 return NULL;
963         }
964
965         headers = procheader_get_header_array_asis(fp);
966
967         sorted_headers = g_ptr_array_new();
968
969         for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
970              disphdr_list = disphdr_list->next) {
971                 DisplayHeaderProp *dp =
972                         (DisplayHeaderProp *)disphdr_list->data;
973
974                 for (i = 0; i < headers->len; i++) {
975                         header = g_ptr_array_index(headers, i);
976
977                         if (procheader_headername_equal(header->name,
978                                                         dp->name)) {
979                                 if (dp->hidden)
980                                         procheader_header_free(header);
981                                 else
982                                         g_ptr_array_add(sorted_headers, header);
983
984                                 g_ptr_array_remove_index(headers, i);
985                                 i--;
986                         }
987                 }
988         }
989
990         if (prefs_common.show_other_header) {
991                 for (i = 0; i < headers->len; i++) {
992                         header = g_ptr_array_index(headers, i);
993                         g_ptr_array_add(sorted_headers, header);
994                 }
995         }
996
997         g_ptr_array_free(headers, FALSE);
998
999         return sorted_headers;
1000 }
1001
1002 static void textview_show_header(TextView *textview, GPtrArray *headers)
1003 {
1004         GtkText *text = GTK_TEXT(textview->text);
1005         Header *header;
1006         gint i;
1007
1008         g_return_if_fail(headers != NULL);
1009
1010         gtk_text_freeze(text);
1011
1012         for (i = 0; i < headers->len; i++) {
1013                 header = g_ptr_array_index(headers, i);
1014                 g_return_if_fail(header->name != NULL);
1015
1016                 gtk_text_insert(text, textview->boldfont, NULL, NULL,
1017                                 header->name, -1);
1018                 if (header->name[strlen(header->name) - 1] != ' ')
1019                         gtk_text_insert(text, textview->boldfont,
1020                                         NULL, NULL, " ", 1);
1021
1022                 if (procheader_headername_equal(header->name, "Subject") ||
1023                     procheader_headername_equal(header->name, "From")    ||
1024                     procheader_headername_equal(header->name, "To")      ||
1025                     procheader_headername_equal(header->name, "Cc"))
1026                         unfold_line(header->body);
1027
1028                 if (prefs_common.enable_color &&
1029                     (procheader_headername_equal(header->name, "X-Mailer") ||
1030                      procheader_headername_equal(header->name,
1031                                                  "X-Newsreader")) &&
1032                     strstr(header->body, "Sylpheed") != NULL)
1033                         gtk_text_insert(text, NULL, &emphasis_color, NULL,
1034                                         header->body, -1);
1035                 else if (prefs_common.enable_color) {
1036                         textview_make_clickable_parts(textview,
1037                                                       NULL, NULL, &uri_color,
1038                                                       header->body);
1039                 } else {
1040                         textview_make_clickable_parts(textview,
1041                                                       NULL, NULL, NULL,
1042                                                       header->body);
1043                 }
1044                 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1045         }
1046
1047         gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1048         gtk_text_thaw(text);
1049         textview->body_pos = gtk_text_get_length(text);
1050 }
1051
1052 gboolean textview_search_string(TextView *textview, const gchar *str,
1053                                 gboolean case_sens)
1054 {
1055         GtkText *text = GTK_TEXT(textview->text);
1056         gint pos;
1057         wchar_t *wcs;
1058         gint len;
1059         gint text_len;
1060         gboolean found = FALSE;
1061
1062         g_return_val_if_fail(str != NULL, FALSE);
1063
1064         wcs = strdup_mbstowcs(str);
1065         g_return_val_if_fail(wcs != NULL, FALSE);
1066         len = wcslen(wcs);
1067         pos = textview->cur_pos;
1068         if (pos < textview->body_pos)
1069                 pos = textview->body_pos;
1070         text_len = gtk_text_get_length(text);
1071         if (text_len - pos < len) {
1072                 g_free(wcs);
1073                 return FALSE;
1074         }
1075
1076         for (; pos < text_len; pos++) {
1077                 if (text_len - pos < len) break;
1078                 if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
1079                     == TRUE) {
1080                         gtk_editable_set_position(GTK_EDITABLE(text),
1081                                                   pos + len);
1082                         gtk_editable_select_region(GTK_EDITABLE(text),
1083                                                    pos, pos + len);
1084                         textview_set_position(textview, pos + len);
1085                         found = TRUE;
1086                         break;
1087                 }
1088                 if (text_len - pos == len) break;
1089         }
1090
1091         g_free(wcs);
1092         return found;
1093 }
1094
1095 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
1096                                          gboolean case_sens)
1097 {
1098         GtkText *text = GTK_TEXT(textview->text);
1099         gint pos;
1100         wchar_t *wcs;
1101         gint len;
1102         gint text_len;
1103         gboolean found = FALSE;
1104
1105         g_return_val_if_fail(str != NULL, FALSE);
1106
1107         wcs = strdup_mbstowcs(str);
1108         g_return_val_if_fail(wcs != NULL, FALSE);
1109         len = wcslen(wcs);
1110         pos = textview->cur_pos;
1111         text_len = gtk_text_get_length(text);
1112         if (text_len - textview->body_pos < len) {
1113                 g_free(wcs);
1114                 return FALSE;
1115         }
1116         if (pos <= textview->body_pos || text_len - pos < len)
1117                 pos = text_len - len;
1118
1119         for (; pos >= textview->body_pos; pos--) {
1120                 if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
1121                     == TRUE) {
1122                         gtk_editable_set_position(GTK_EDITABLE(text), pos);
1123                         gtk_editable_select_region(GTK_EDITABLE(text),
1124                                                    pos, pos + len);
1125                         textview_set_position(textview, pos - 1);
1126                         found = TRUE;
1127                         break;
1128                 }
1129                 if (pos == textview->body_pos) break;
1130         }
1131
1132         g_free(wcs);
1133         return found;
1134 }
1135
1136 void textview_scroll_one_line(TextView *textview, gboolean up)
1137 {
1138         GtkText *text = GTK_TEXT(textview->text);
1139         gfloat upper;
1140
1141         if (prefs_common.enable_smooth_scroll) {
1142                 textview_smooth_scroll_one_line(textview, up);
1143                 return;
1144         }
1145
1146         if (!up) {
1147                 upper = text->vadj->upper - text->vadj->page_size;
1148                 if (text->vadj->value < upper) {
1149                         text->vadj->value +=
1150                                 text->vadj->step_increment * 4;
1151                         text->vadj->value =
1152                                 MIN(text->vadj->value, upper);
1153                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1154                                                 "value_changed");
1155                 }
1156         } else {
1157                 if (text->vadj->value > 0.0) {
1158                         text->vadj->value -=
1159                                 text->vadj->step_increment * 4;
1160                         text->vadj->value =
1161                                 MAX(text->vadj->value, 0.0);
1162                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1163                                                 "value_changed");
1164                 }
1165         }
1166 }
1167
1168 gboolean textview_scroll_page(TextView *textview, gboolean up)
1169 {
1170         GtkText *text = GTK_TEXT(textview->text);
1171         gfloat upper;
1172         gfloat page_incr;
1173
1174         if (prefs_common.enable_smooth_scroll)
1175                 return textview_smooth_scroll_page(textview, up);
1176
1177         if (prefs_common.scroll_halfpage)
1178                 page_incr = text->vadj->page_increment / 2;
1179         else
1180                 page_incr = text->vadj->page_increment;
1181
1182         if (!up) {
1183                 upper = text->vadj->upper - text->vadj->page_size;
1184                 if (text->vadj->value < upper) {
1185                         text->vadj->value += page_incr;
1186                         text->vadj->value = MIN(text->vadj->value, upper);
1187                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1188                                                 "value_changed");
1189                 } else
1190                         return FALSE;
1191         } else {
1192                 if (text->vadj->value > 0.0) {
1193                         text->vadj->value -= page_incr;
1194                         text->vadj->value = MAX(text->vadj->value, 0.0);
1195                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1196                                                 "value_changed");
1197                 } else
1198                         return FALSE;
1199         }
1200
1201         return TRUE;
1202 }
1203
1204 static void textview_smooth_scroll_do(TextView *textview,
1205                                       gfloat old_value, gfloat last_value,
1206                                       gint step)
1207 {
1208         GtkText *text = GTK_TEXT(textview->text);
1209         gint change_value;
1210         gboolean up;
1211         gint i;
1212
1213         if (old_value < last_value) {
1214                 change_value = last_value - old_value;
1215                 up = FALSE;
1216         } else {
1217                 change_value = old_value - last_value;
1218                 up = TRUE;
1219         }
1220
1221         gdk_key_repeat_disable();
1222
1223         for (i = step; i <= change_value; i += step) {
1224                 text->vadj->value = old_value + (up ? -i : i);
1225                 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1226                                         "value_changed");
1227         }
1228
1229         text->vadj->value = last_value;
1230         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
1231
1232         gdk_key_repeat_restore();
1233 }
1234
1235 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1236 {
1237         GtkText *text = GTK_TEXT(textview->text);
1238         gfloat upper;
1239         gfloat old_value;
1240         gfloat last_value;
1241
1242         if (!up) {
1243                 upper = text->vadj->upper - text->vadj->page_size;
1244                 if (text->vadj->value < upper) {
1245                         old_value = text->vadj->value;
1246                         last_value = text->vadj->value +
1247                                 text->vadj->step_increment * 4;
1248                         last_value = MIN(last_value, upper);
1249
1250                         textview_smooth_scroll_do(textview, old_value,
1251                                                   last_value,
1252                                                   prefs_common.scroll_step);
1253                 }
1254         } else {
1255                 if (text->vadj->value > 0.0) {
1256                         old_value = text->vadj->value;
1257                         last_value = text->vadj->value -
1258                                 text->vadj->step_increment * 4;
1259                         last_value = MAX(last_value, 0.0);
1260
1261                         textview_smooth_scroll_do(textview, old_value,
1262                                                   last_value,
1263                                                   prefs_common.scroll_step);
1264                 }
1265         }
1266 }
1267
1268 static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1269 {
1270         GtkText *text = GTK_TEXT(textview->text);
1271         gfloat upper;
1272         gfloat page_incr;
1273         gfloat old_value;
1274         gfloat last_value;
1275
1276         if (prefs_common.scroll_halfpage)
1277                 page_incr = text->vadj->page_increment / 2;
1278         else
1279                 page_incr = text->vadj->page_increment;
1280
1281         if (!up) {
1282                 upper = text->vadj->upper - text->vadj->page_size;
1283                 if (text->vadj->value < upper) {
1284                         old_value = text->vadj->value;
1285                         last_value = text->vadj->value + page_incr;
1286                         last_value = MIN(last_value, upper);
1287
1288                         textview_smooth_scroll_do(textview, old_value,
1289                                                   last_value,
1290                                                   prefs_common.scroll_step);
1291                 } else
1292                         return FALSE;
1293         } else {
1294                 if (text->vadj->value > 0.0) {
1295                         old_value = text->vadj->value;
1296                         last_value = text->vadj->value - page_incr;
1297                         last_value = MAX(last_value, 0.0);
1298
1299                         textview_smooth_scroll_do(textview, old_value,
1300                                                   last_value,
1301                                                   prefs_common.scroll_step);
1302                 } else
1303                         return FALSE;
1304         }
1305
1306         return TRUE;
1307 }
1308
1309 static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1310                                  TextView *textview)
1311 {
1312         SummaryView *summaryview = NULL;
1313
1314         if (!event) return;
1315         if (textview->messageview->mainwin)
1316                 summaryview = textview->messageview->mainwin->summaryview;
1317
1318         switch (event->keyval) {
1319         case GDK_Tab:
1320         case GDK_Home:
1321         case GDK_Left:
1322         case GDK_Up:
1323         case GDK_Right:
1324         case GDK_Down:
1325         case GDK_Page_Up:
1326         case GDK_Page_Down:
1327         case GDK_End:
1328         case GDK_Control_L:
1329         case GDK_Control_R:
1330                 break;
1331         case GDK_space:
1332                 if (summaryview)
1333                         summary_pass_key_press_event(summaryview, event);
1334                 else
1335                         textview_scroll_page(textview, FALSE);
1336                 break;
1337         case GDK_BackSpace:
1338         case GDK_Delete:
1339                 textview_scroll_page(textview, TRUE);
1340                 break;
1341         case GDK_Return:
1342                 textview_scroll_one_line(textview,
1343                                          (event->state & GDK_MOD1_MASK) != 0);
1344                 break;
1345         default:
1346                 if (summaryview)
1347                         summary_pass_key_press_event(summaryview, event);
1348                 break;
1349         }
1350 }
1351
1352 static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1353                                     TextView *textview)
1354 {
1355         textview->cur_pos = 
1356                 gtk_editable_get_position(GTK_EDITABLE(textview->text));
1357
1358         if (event &&
1359             ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1360              || event->button == 2 || event->button == 3)) {
1361                 GSList *cur;
1362
1363                 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1364                         RemoteURI *uri = (RemoteURI *)cur->data;
1365
1366                         if (textview->cur_pos >= uri->start &&
1367                             textview->cur_pos <  uri->end) {
1368                                 if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
1369                                         if (event->button == 3) {
1370                                                 gchar *fromname, *fromaddress;
1371                                                 /* extract url */
1372                                                 fromaddress = g_strdup(uri->uri + 7);
1373                                                 /* Hiroyuki: please put this function in utils.c! */
1374                                                 fromname = procheader_get_fromname(fromaddress);
1375                                                 extract_address(fromaddress);
1376                                                 g_message("adding from textview %s <%s>", fromname, fromaddress);
1377                                                 // Add to address book - Match
1378                                                 addressbook_add_contact( fromname, fromaddress, NULL );
1379                                                 g_free(fromaddress);
1380                                                 g_free(fromname);
1381                                         } else {
1382                                                 compose_new_with_recipient
1383                                                         (NULL, uri->uri + 7);
1384                                         }
1385                                 } else {
1386                                         open_uri(uri->uri,
1387                                                  prefs_common.uri_cmd);
1388                                 }
1389                         }
1390                 }
1391         }
1392 }
1393
1394 static void textview_uri_list_remove_all(GSList *uri_list)
1395 {
1396         GSList *cur;
1397
1398         for (cur = uri_list; cur != NULL; cur = cur->next) {
1399                 if (cur->data) {
1400                         g_free(((RemoteURI *)cur->data)->uri);
1401                         g_free(cur->data);
1402                 }
1403         }
1404
1405         g_slist_free(uri_list);
1406 }