2004-08-31 [colin] 0.9.12cvs84.3
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 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/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkmenu.h>
30 #include <gtk/gtkmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkcheckmenuitem.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkclist.h>
36 #include <gtk/gtkctree.h>
37 #include <gtk/gtkvpaned.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkeditable.h>
40 #include <gtk/gtkwindow.h>
41 #include <gtk/gtksignal.h>
42 #include <gtk/gtkvbox.h>
43 #include <gtk/gtkcontainer.h>
44 #include <gtk/gtkhandlebox.h>
45 #include <gtk/gtktoolbar.h>
46 #include <gtk/gtktable.h>
47 #include <gtk/gtkhbox.h>
48 #include <gtk/gtklabel.h>
49 #include <gtk/gtkscrolledwindow.h>
50 #include <gtk/gtktreeview.h>
51
52 #include <gtk/gtkdnd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <unistd.h>
60 #include <time.h>
61 /* #include <sys/utsname.h> */
62 #include <stdlib.h>
63 #include <sys/wait.h>
64 #include <signal.h>
65 #include <errno.h>
66 #include <libgen.h>
67
68 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
69 #  include <wchar.h>
70 #  include <wctype.h>
71 #endif
72
73 #include "intl.h"
74 #include "main.h"
75 #include "mainwindow.h"
76 #include "compose.h"
77 #include "addressbook.h"
78 #include "folderview.h"
79 #include "procmsg.h"
80 #include "menu.h"
81 #include "stock_pixmap.h"
82 #include "send_message.h"
83 #include "imap.h"
84 #include "news.h"
85 #include "customheader.h"
86 #include "prefs_common.h"
87 #include "prefs_account.h"
88 #include "action.h"
89 #include "account.h"
90 #include "filesel.h"
91 #include "procheader.h"
92 #include "procmime.h"
93 #include "statusbar.h"
94 #include "about.h"
95 #include "base64.h"
96 #include "quoted-printable.h"
97 #include "codeconv.h"
98 #include "utils.h"
99 #include "gtkutils.h"
100 #include "socket.h"
101 #include "alertpanel.h"
102 #include "manage_window.h"
103 #include "gtkshruler.h"
104 #include "folder.h"
105 #include "addr_compl.h"
106 #include "quote_fmt.h"
107 #include "template.h"
108 #include "undo.h"
109 #include "foldersel.h"
110 #include "toolbar.h"
111
112 #if USE_GPGME
113 #  include "rfc2015.h"
114 #endif
115
116 typedef enum
117 {
118         COL_MIMETYPE = 0,
119         COL_SIZE     = 1,
120         COL_NAME     = 2
121 } AttachColumnPos;
122
123 #define N_ATTACH_COLS           3
124
125 typedef enum
126 {
127         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
128         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
129         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
130         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
131         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
132         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
133         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
134         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
135         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
136         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
137         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
138         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
139         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
140         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
141         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END
142 } ComposeCallAdvancedAction;
143
144 typedef enum
145 {
146         PRIORITY_HIGHEST = 1,
147         PRIORITY_HIGH,
148         PRIORITY_NORMAL,
149         PRIORITY_LOW,
150         PRIORITY_LOWEST
151 } PriorityLevel;
152
153 typedef enum
154 {
155         COMPOSE_INSERT_SUCCESS,
156         COMPOSE_INSERT_READ_ERROR,
157         COMPOSE_INSERT_INVALID_CHARACTER,
158         COMPOSE_INSERT_NO_FILE
159 } ComposeInsertResult;
160
161 #define B64_LINE_SIZE           57
162 #define B64_BUFFSIZE            77
163
164 #define MAX_REFERENCES_LEN      999
165
166 static GdkColor quote_color = {0, 0, 0, 0xbfff};
167
168 static GList *compose_list = NULL;
169
170 Compose *compose_generic_new                    (PrefsAccount   *account,
171                                                  const gchar    *to,
172                                                  FolderItem     *item,
173                                                  GPtrArray      *attach_files,
174                                                  GList          *listAddress );
175
176 static Compose *compose_create                  (PrefsAccount   *account,
177                                                  ComposeMode     mode);
178
179 static GtkWidget *compose_account_option_menu_create
180                                                 (Compose        *compose);
181 static void compose_set_template_menu           (Compose        *compose);
182 static void compose_template_apply              (Compose        *compose,
183                                                  Template       *tmpl,
184                                                  gboolean        replace);
185 static void compose_destroy                     (Compose        *compose);
186
187 static void compose_entries_set                 (Compose        *compose,
188                                                  const gchar    *mailto);
189 static gint compose_parse_header                (Compose        *compose,
190                                                  MsgInfo        *msginfo);
191 static gchar *compose_parse_references          (const gchar    *ref,
192                                                  const gchar    *msgid);
193
194 static gchar *compose_quote_fmt                 (Compose        *compose,
195                                                  MsgInfo        *msginfo,
196                                                  const gchar    *fmt,
197                                                  const gchar    *qmark,
198                                                  const gchar    *body);
199
200 static void compose_reply_set_entry             (Compose        *compose,
201                                                  MsgInfo        *msginfo,
202                                                  gboolean        to_all,
203                                                  gboolean        to_ml,
204                                                  gboolean        to_sender,
205                                                  gboolean
206                                                  followup_and_reply_to);
207 static void compose_reedit_set_entry            (Compose        *compose,
208                                                  MsgInfo        *msginfo);
209 static void compose_insert_sig                  (Compose        *compose,
210                                                  gboolean        replace);
211 static gchar *compose_get_signature_str         (Compose        *compose);
212 static ComposeInsertResult compose_insert_file  (Compose        *compose,
213                                                  const gchar    *file);
214 static void compose_attach_append               (Compose        *compose,
215                                                  const gchar    *file,
216                                                  const gchar    *type,
217                                                  const gchar    *content_type);
218 static void compose_attach_parts                (Compose        *compose,
219                                                  MsgInfo        *msginfo);
220 static void compose_wrap_line                   (Compose        *compose);
221 static void compose_wrap_line_all               (Compose        *compose);
222 static void compose_wrap_line_all_full          (Compose        *compose,
223                                                  gboolean        autowrap);
224 static void compose_set_title                   (Compose        *compose);
225 static void compose_select_account              (Compose        *compose,
226                                                  PrefsAccount   *account,
227                                                  gboolean        init);
228
229 static PrefsAccount *compose_current_mail_account(void);
230 /* static gint compose_send                     (Compose        *compose); */
231 static gboolean compose_check_for_valid_recipient
232                                                 (Compose        *compose);
233 static gboolean compose_check_entries           (Compose        *compose,
234                                                  gboolean       check_subject);
235 static gint compose_write_to_file               (Compose        *compose,
236                                                  const gchar    *file,
237                                                  gboolean        is_draft);
238 static gint compose_write_body_to_file          (Compose        *compose,
239                                                  const gchar    *file);
240 static gint compose_remove_reedit_target        (Compose        *compose);
241 void compose_remove_draft                       (Compose        *compose);
242 static gint compose_queue                       (Compose        *compose,
243                                                  gint           *msgnum,
244                                                  FolderItem     **item);
245 static gint compose_queue_sub                   (Compose        *compose,
246                                                  gint           *msgnum,
247                                                  FolderItem     **item,
248                                                  gboolean       check_subject);
249 static void compose_write_attach                (Compose        *compose,
250                                                  FILE           *fp);
251 static gchar *compose_get_header                (Compose        *compose,
252                                                  const gchar    *charset,
253                                                  EncodingType    encoding,
254                                                  gboolean        is_draft);
255
256 static void compose_convert_header              (gchar          *dest,
257                                                  gint            len,
258                                                  gchar          *src,
259                                                  gint            header_len,
260                                                  gboolean        addr_field);
261 static void compose_generate_msgid              (gchar          *buf,
262                                                  gint            len);
263
264 static void compose_attach_info_free            (AttachInfo     *ainfo);
265 static void compose_attach_remove_selected      (Compose        *compose);
266
267 static void compose_attach_property             (Compose        *compose);
268 static void compose_attach_property_create      (gboolean       *cancelled);
269 static void attach_property_ok                  (GtkWidget      *widget,
270                                                  gboolean       *cancelled);
271 static void attach_property_cancel              (GtkWidget      *widget,
272                                                  gboolean       *cancelled);
273 static gint attach_property_delete_event        (GtkWidget      *widget,
274                                                  GdkEventAny    *event,
275                                                  gboolean       *cancelled);
276 static gboolean attach_property_key_pressed     (GtkWidget      *widget,
277                                                  GdkEventKey    *event,
278                                                  gboolean       *cancelled);
279
280 static void compose_exec_ext_editor             (Compose           *compose);
281 static gint compose_exec_ext_editor_real        (const gchar       *file);
282 static gboolean compose_ext_editor_kill         (Compose           *compose);
283 static void compose_input_cb                    (gpointer           data,
284                                                  gint               source,
285                                                  GdkInputCondition  condition);
286 static void compose_set_ext_editor_sensitive    (Compose           *compose,
287                                                  gboolean           sensitive);
288
289 static void compose_undo_state_changed          (UndoMain       *undostruct,
290                                                  gint            undo_state,
291                                                  gint            redo_state,
292                                                  gpointer        data);
293
294 static gint calc_cursor_xpos    (GtkTextView    *text,
295                                  gint            extra,
296                                  gint            char_width);
297
298 static void compose_create_header_entry (Compose *compose);
299 static void compose_add_header_entry    (Compose *compose, gchar *header, gchar *text);
300 static void compose_update_priority_menu_item(Compose * compose);
301
302 static void compose_add_field_list      ( Compose *compose,
303                                           GList *listAddress );
304
305 /* callback functions */
306
307 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
308                                          GtkAllocation  *allocation,
309                                          GtkSHRuler     *shruler);
310 static void account_activated           (GtkMenuItem    *menuitem,
311                                          gpointer        data);
312 static void attach_selected             (GtkCList       *clist,
313                                          gint            row,
314                                          gint            column,
315                                          GdkEvent       *event,
316                                          gpointer        data);
317 static gboolean attach_button_pressed   (GtkWidget      *widget,
318                                          GdkEventButton *event,
319                                          gpointer        data);
320 static gboolean attach_key_pressed      (GtkWidget      *widget,
321                                          GdkEventKey    *event,
322                                          gpointer        data);
323
324 static void compose_send_cb             (gpointer        data,
325                                          guint           action,
326                                          GtkWidget      *widget);
327 static void compose_send_later_cb       (gpointer        data,
328                                          guint           action,
329                                          GtkWidget      *widget);
330
331 static void compose_draft_cb            (gpointer        data,
332                                          guint           action,
333                                          GtkWidget      *widget);
334
335 static void compose_attach_cb           (gpointer        data,
336                                          guint           action,
337                                          GtkWidget      *widget);
338 static void compose_insert_file_cb      (gpointer        data,
339                                          guint           action,
340                                          GtkWidget      *widget);
341 static void compose_insert_sig_cb       (gpointer        data,
342                                          guint           action,
343                                          GtkWidget      *widget);
344
345 static void compose_close_cb            (gpointer        data,
346                                          guint           action,
347                                          GtkWidget      *widget);
348
349 static void compose_address_cb          (gpointer        data,
350                                          guint           action,
351                                          GtkWidget      *widget);
352 static void compose_template_activate_cb(GtkWidget      *widget,
353                                          gpointer        data);
354
355 static void compose_ext_editor_cb       (gpointer        data,
356                                          guint           action,
357                                          GtkWidget      *widget);
358
359 static gint compose_delete_cb           (GtkWidget      *widget,
360                                          GdkEventAny    *event,
361                                          gpointer        data);
362 static void compose_destroy_cb          (GtkWidget      *widget,
363                                          Compose        *compose);
364
365 static void compose_undo_cb             (Compose        *compose);
366 static void compose_redo_cb             (Compose        *compose);
367 static void compose_cut_cb              (Compose        *compose);
368 static void compose_copy_cb             (Compose        *compose);
369 static void compose_paste_cb            (Compose        *compose);
370 static void compose_paste_as_quote_cb   (Compose        *compose);
371 static void compose_allsel_cb           (Compose        *compose);
372
373 static void compose_advanced_action_cb  (Compose                   *compose,
374                                          ComposeCallAdvancedAction  action);
375
376 static gboolean compose_grab_focus_cb   (GtkWidget      *widget,
377                                          Compose        *compose);
378 static gboolean compose_grab_focus_before_cb    (GtkWidget      *widget,
379                                          Compose        *compose);
380
381 static void compose_changed_cb          (GtkTextBuffer  *textbuf,
382                                          Compose        *compose);
383
384 static void compose_toggle_autowrap_cb  (gpointer        data,
385                                          guint           action,
386                                          GtkWidget      *widget);
387
388 #if 0
389 static void compose_toggle_to_cb        (gpointer        data,
390                                          guint           action,
391                                          GtkWidget      *widget);
392 static void compose_toggle_cc_cb        (gpointer        data,
393                                          guint           action,
394                                          GtkWidget      *widget);
395 static void compose_toggle_bcc_cb       (gpointer        data,
396                                          guint           action,
397                                          GtkWidget      *widget);
398 static void compose_toggle_replyto_cb   (gpointer        data,
399                                          guint           action,
400                                          GtkWidget      *widget);
401 static void compose_toggle_followupto_cb(gpointer        data,
402                                          guint           action,
403                                          GtkWidget      *widget);
404 static void compose_toggle_attach_cb    (gpointer        data,
405                                          guint           action,
406                                          GtkWidget      *widget);
407 #endif
408 static void compose_toggle_ruler_cb     (gpointer        data,
409                                          guint           action,
410                                          GtkWidget      *widget);
411 #if USE_GPGME
412 static void compose_toggle_sign_cb      (gpointer        data,
413                                          guint           action,
414                                          GtkWidget      *widget);
415 static void compose_toggle_encrypt_cb   (gpointer        data,
416                                          guint           action,
417                                          GtkWidget      *widget);
418 static void compose_set_gnupg_mode_cb   (gpointer        data,
419                                          guint           action,
420                                          GtkWidget      *widget);
421 static void compose_update_gnupg_mode_menu_item(Compose * compose);
422 static void activate_gnupg_mode         (Compose *compose, 
423                                          PrefsAccount *account);
424 static void compose_use_signing(Compose *compose, gboolean use_signing);
425 static void compose_use_encryption(Compose *compose, gboolean use_encryption);
426 #endif
427 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
428                                              GtkWidget *widget);
429 static void compose_toggle_remove_refs_cb(gpointer data, guint action,
430                                              GtkWidget *widget);
431 static void compose_set_priority_cb     (gpointer        data,
432                                          guint           action,
433                                          GtkWidget      *widget);
434
435 static void compose_attach_drag_received_cb (GtkWidget          *widget,
436                                              GdkDragContext     *drag_context,
437                                              gint                x,
438                                              gint                y,
439                                              GtkSelectionData   *data,
440                                              guint               info,
441                                              guint               time,
442                                              gpointer            user_data);
443 static void compose_insert_drag_received_cb (GtkWidget          *widget,
444                                              GdkDragContext     *drag_context,
445                                              gint                x,
446                                              gint                y,
447                                              GtkSelectionData   *data,
448                                              guint               info,
449                                              guint               time,
450                                              gpointer            user_data);
451
452 #if 0
453 static void to_activated                (GtkWidget      *widget,
454                                          Compose        *compose);
455 static void newsgroups_activated        (GtkWidget      *widget,
456                                          Compose        *compose);
457 static void cc_activated                (GtkWidget      *widget,
458                                          Compose        *compose);
459 static void bcc_activated               (GtkWidget      *widget,
460                                          Compose        *compose);
461 static void replyto_activated           (GtkWidget      *widget,
462                                          Compose        *compose);
463 static void followupto_activated        (GtkWidget      *widget,
464                                          Compose        *compose);
465 static void subject_activated           (GtkWidget      *widget,
466                                          Compose        *compose);
467 #endif
468
469 static void text_activated              (GtkWidget      *widget,
470                                          Compose        *compose);
471 static void text_inserted               (GtkTextBuffer  *buffer,
472                                          GtkTextIter    *iter,
473                                          const gchar    *text,
474                                          gint            len,
475                                          Compose        *compose);
476 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
477                                   gboolean to_all, gboolean to_ml,
478                                   gboolean to_sender,
479                                   gboolean followup_and_reply_to,
480                                   const gchar *body);
481
482 gboolean compose_headerentry_changed_cb    (GtkWidget          *entry,
483                                             ComposeHeaderEntry *headerentry);
484 gboolean compose_headerentry_key_press_event_cb(GtkWidget              *entry,
485                                             GdkEventKey        *event,
486                                             ComposeHeaderEntry *headerentry);
487 static gboolean compose_headerentry_button_pressed (GtkWidget *entry, 
488                                                     GdkEventButton *event,
489                                                     gpointer data);
490
491 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
492
493 static void compose_allow_user_actions (Compose *compose, gboolean allow);
494
495 #if USE_ASPELL
496 static void compose_check_all              (Compose *compose);
497 static void compose_highlight_all          (Compose *compose);
498 static void compose_check_backwards        (Compose *compose);
499 static void compose_check_forwards_go      (Compose *compose);
500 #endif
501
502 static gboolean compose_send_control_enter      (Compose        *compose);
503 static gint compose_defer_auto_save_draft       (Compose        *compose);
504 static PrefsAccount *compose_guess_forward_account_from_msginfo (MsgInfo *msginfo);
505
506 static GtkItemFactoryEntry compose_popup_entries[] =
507 {
508         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
509         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
510         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
511         {N_("/_Properties..."), NULL, compose_attach_property, 0, NULL}
512 };
513
514 static GtkItemFactoryEntry compose_entries[] =
515 {
516         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
517         {N_("/_File/_Save"),
518                                                 "<control>S", compose_draft_cb, 1, NULL},
519         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
520         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb,      0, NULL},
521         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
522         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig_cb,  0, NULL},
523         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
524         {N_("/_File/_Close"),                   "<control>W", compose_close_cb, 0, NULL},
525
526         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
527         {N_("/_Edit/_Undo"),            "<control>Z", compose_undo_cb, 0, NULL},
528         {N_("/_Edit/_Redo"),            "<control>Y", compose_redo_cb, 0, NULL},
529         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
530         {N_("/_Edit/Cu_t"),             "<control>X", compose_cut_cb,    0, NULL},
531         {N_("/_Edit/_Copy"),            "<control>C", compose_copy_cb,   0, NULL},
532         {N_("/_Edit/_Paste"),           "<control>V", compose_paste_cb,  0, NULL},
533         {N_("/_Edit/Paste as _quotation"),
534                                         NULL, compose_paste_as_quote_cb, 0, NULL},
535         {N_("/_Edit/Select _all"),      "<control>A", compose_allsel_cb, 0, NULL},
536         {N_("/_Edit/A_dvanced"),        NULL, NULL, 0, "<Branch>"},
537         {N_("/_Edit/A_dvanced/Move a character backward"),
538                                         "<control>B",
539                                         compose_advanced_action_cb,
540                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
541                                         NULL},
542         {N_("/_Edit/A_dvanced/Move a character forward"),
543                                         "<control>F",
544                                         compose_advanced_action_cb,
545                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
546                                         NULL},
547         {N_("/_Edit/A_dvanced/Move a word backward"),
548                                         NULL, /* "<alt>B" */
549                                         compose_advanced_action_cb,
550                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
551                                         NULL},
552         {N_("/_Edit/A_dvanced/Move a word forward"),
553                                         NULL, /* "<alt>F" */
554                                         compose_advanced_action_cb,
555                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
556                                         NULL},
557         {N_("/_Edit/A_dvanced/Move to beginning of line"),
558                                         NULL, /* "<control>A" */
559                                         compose_advanced_action_cb,
560                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
561                                         NULL},
562         {N_("/_Edit/A_dvanced/Move to end of line"),
563                                         "<control>E",
564                                         compose_advanced_action_cb,
565                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
566                                         NULL},
567         {N_("/_Edit/A_dvanced/Move to previous line"),
568                                         "<control>P",
569                                         compose_advanced_action_cb,
570                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
571                                         NULL},
572         {N_("/_Edit/A_dvanced/Move to next line"),
573                                         "<control>N",
574                                         compose_advanced_action_cb,
575                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
576                                         NULL},
577         {N_("/_Edit/A_dvanced/Delete a character backward"),
578                                         "<control>H",
579                                         compose_advanced_action_cb,
580                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
581                                         NULL},
582         {N_("/_Edit/A_dvanced/Delete a character forward"),
583                                         "<control>D",
584                                         compose_advanced_action_cb,
585                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
586                                         NULL},
587         {N_("/_Edit/A_dvanced/Delete a word backward"),
588                                         NULL, /* "<control>W" */
589                                         compose_advanced_action_cb,
590                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
591                                         NULL},
592         {N_("/_Edit/A_dvanced/Delete a word forward"),
593                                         NULL, /* "<alt>D", */
594                                         compose_advanced_action_cb,
595                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
596                                         NULL},
597         {N_("/_Edit/A_dvanced/Delete line"),
598                                         "<control>U",
599                                         compose_advanced_action_cb,
600                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
601                                         NULL},
602         {N_("/_Edit/A_dvanced/Delete entire line"),
603                                         NULL,
604                                         compose_advanced_action_cb,
605                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
606                                         NULL},
607         {N_("/_Edit/A_dvanced/Delete to end of line"),
608                                         "<control>K",
609                                         compose_advanced_action_cb,
610                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END,
611                                         NULL},
612         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
613         {N_("/_Edit/_Wrap current paragraph"),
614                                         "<control>L", compose_wrap_line, 0, NULL},
615         {N_("/_Edit/Wrap all long _lines"),
616                                         "<control><alt>L", compose_wrap_line_all, 0, NULL},
617         {N_("/_Edit/Aut_o wrapping"),   "<shift><control>L", compose_toggle_autowrap_cb, 0, "<ToggleItem>"},
618         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
619         {N_("/_Edit/Edit with e_xternal editor"),
620                                         "<shift><control>X", compose_ext_editor_cb, 0, NULL},
621 #if USE_ASPELL
622         {N_("/_Spelling"),              NULL, NULL, 0, "<Branch>"},
623         {N_("/_Spelling/_Check all or check selection"),
624                                         NULL, compose_check_all, 0, NULL},
625         {N_("/_Spelling/_Highlight all misspelled words"),
626                                         NULL, compose_highlight_all, 0, NULL},
627         {N_("/_Spelling/Check _backwards misspelled word"),
628                                         NULL, compose_check_backwards , 0, NULL},
629         {N_("/_Spelling/_Forward to next misspelled word"),
630                                         NULL, compose_check_forwards_go, 0, NULL},
631         {N_("/_Spelling/---"),          NULL, NULL, 0, "<Separator>"},
632         {N_("/_Spelling/_Spelling Configuration"),
633                                         NULL, NULL, 0, "<Branch>"},
634 #endif
635 #if 0 /* NEW COMPOSE GUI */
636         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
637         {N_("/_View/_To"),              NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
638         {N_("/_View/_Cc"),              NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
639         {N_("/_View/_Bcc"),             NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
640         {N_("/_View/_Reply to"),        NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
641         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
642         {N_("/_View/_Followup to"),     NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
643         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
644         {N_("/_View/R_uler"),           NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
645         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
646         {N_("/_View/_Attachment"),      NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
647 #endif
648         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
649         {N_("/_Message/_Send"),         "<control>Return",
650                                         compose_send_cb, 0, NULL},
651         {N_("/_Message/Send _later"),   "<shift><control>S",
652                                         compose_send_later_cb,  0, NULL},
653 #if 0 /* NEW COMPOSE GUI */
654         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
655         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
656         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
657         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
658         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
659         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
660         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
661         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
662         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
663 #endif
664 #if USE_GPGME
665         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
666         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
667         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
668         {N_("/_Message/Mode"),          NULL, NULL,   0, "<Branch>"},
669         {N_("/_Message/Mode/MIME"),     NULL, compose_set_gnupg_mode_cb,   GNUPG_MODE_DETACH, "<RadioItem>"},   
670         {N_("/_Message/Mode/Inline"),   NULL, compose_set_gnupg_mode_cb,   GNUPG_MODE_INLINE, "/Message/Mode/MIME"},    
671 #endif /* USE_GPGME */
672         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
673         {N_("/_Message/_Priority"),     NULL,           NULL,   0, "<Branch>"},
674         {N_("/_Message/Priority/_Highest"), NULL, compose_set_priority_cb, PRIORITY_HIGHEST, "<RadioItem>"},
675         {N_("/_Message/Priority/Hi_gh"),    NULL, compose_set_priority_cb, PRIORITY_HIGH, "/Message/Priority/Highest"},
676         {N_("/_Message/Priority/_Normal"),  NULL, compose_set_priority_cb, PRIORITY_NORMAL, "/Message/Priority/Highest"},
677         {N_("/_Message/Priority/Lo_w"),    NULL, compose_set_priority_cb, PRIORITY_LOW, "/Message/Priority/Highest"},
678         {N_("/_Message/Priority/_Lowest"),  NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Message/Priority/Highest"},
679         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
680         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
681         {N_("/_Message/Remo_ve references"),    NULL, compose_toggle_remove_refs_cb, 0, "<ToggleItem>"},
682         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
683         {N_("/_Tools/Show _ruler"),     NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
684         {N_("/_Tools/_Address book"),   "<shift><control>A", compose_address_cb , 0, NULL},
685         {N_("/_Tools/_Template"),       NULL, NULL, 0, "<Branch>"},
686         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
687         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
688         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
689 };
690
691 static GtkTargetEntry compose_mime_types[] =
692 {
693         {"text/uri-list", 0, 0}
694 };
695
696 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
697                      GPtrArray *attach_files)
698 {
699         return compose_generic_new(account, mailto, NULL, attach_files, NULL);
700 }
701
702 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
703 {
704         return compose_generic_new(account, NULL, item, NULL, NULL);
705 }
706
707 Compose *compose_new_with_list( PrefsAccount *account, GList *listAddress )
708 {
709         return compose_generic_new( account, NULL, NULL, NULL, listAddress );
710 }
711
712 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item,
713                              GPtrArray *attach_files, GList *listAddress )
714 {
715         Compose *compose;
716         GtkTextView *textview;
717         GtkTextBuffer *textbuf;
718         GtkTextIter iter;
719         GtkItemFactory *ifactory;
720
721         if (item && item->prefs && item->prefs->enable_default_account)
722                 account = account_find_from_id(item->prefs->default_account);
723
724         if (!account) account = cur_account;
725         g_return_val_if_fail(account != NULL, NULL);
726
727         compose = compose_create(account, COMPOSE_NEW);
728         ifactory = gtk_item_factory_from_widget(compose->menubar);
729
730         compose->replyinfo = NULL;
731         compose->fwdinfo   = NULL;
732
733         textview = GTK_TEXT_VIEW(compose->text);
734         textbuf = gtk_text_view_get_buffer(textview);
735
736         undo_block(compose->undostruct);
737 #ifdef USE_ASPELL
738         if (item && item->prefs && item->prefs->enable_default_dictionary &&
739             compose->gtkaspell) 
740                 gtkaspell_change_dict(compose->gtkaspell, 
741                     item->prefs->default_dictionary);
742 #endif
743
744         if (account->auto_sig)
745                 compose_insert_sig(compose, FALSE);
746         gtk_text_buffer_get_start_iter(textbuf, &iter);
747         gtk_text_buffer_place_cursor(textbuf, &iter);
748
749         if (account->protocol != A_NNTP) {
750                 if (mailto && *mailto != '\0') {
751                         compose_entries_set(compose, mailto);
752
753                 } else if (item && item->prefs->enable_default_to) {
754                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
755                         compose_entry_mark_default_to(compose, item->prefs->default_to);
756                 }
757                 if (item && item->ret_rcpt) {
758                         menu_set_active(ifactory, "/Message/Request Return Receipt", TRUE);
759                 }
760         } else {
761                 if (mailto) {
762                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
763                 }
764                 /*
765                  * CLAWS: just don't allow return receipt request, even if the user
766                  * may want to send an email. simple but foolproof.
767                  */
768                 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE); 
769         }
770         compose_add_field_list( compose, listAddress );
771
772         if (attach_files) {
773                 gint i;
774                 gchar *file;
775
776                 for (i = 0; i < attach_files->len; i++) {
777                         file = g_ptr_array_index(attach_files, i);
778                         compose_attach_append(compose, file, file, NULL);
779                 }
780         }
781
782         compose_show_first_last_header(compose, TRUE);
783
784         /* Set save folder */
785         if (item && item->prefs && item->prefs->save_copy_to_folder) {
786                 gchar *folderidentifier;
787
788                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
789                 folderidentifier = folder_item_get_identifier(item);
790                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
791                 g_free(folderidentifier);
792         }
793         
794         gtk_widget_grab_focus(compose->header_last->entry);
795
796         undo_unblock(compose->undostruct);
797
798         if (prefs_common.auto_exteditor)
799                 compose_exec_ext_editor(compose);
800
801         return compose;
802 }
803
804 /*
805 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
806                                            const gchar *followupto, gchar * to)
807 {
808         Compose *compose;
809
810         if (!account) account = cur_account;
811         g_return_val_if_fail(account != NULL, NULL);
812         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
813
814         compose = compose_create(account, COMPOSE_NEW);
815
816         if (prefs_common.auto_sig)
817                 compose_insert_sig(compose);
818         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
819         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
820
821         compose_entry_append(compose, to, COMPOSE_TO);
822         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
823         gtk_widget_grab_focus(compose->subject_entry);
824
825         return compose;
826 }
827 */
828
829 void compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
830 {
831         MsgInfo *msginfo;
832         guint list_len;
833
834         g_return_if_fail(msginfo_list != NULL);
835
836         msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
837         g_return_if_fail(msginfo != NULL);
838
839         list_len = g_slist_length(msginfo_list);
840
841         switch (mode) {
842         case COMPOSE_REPLY:
843                 compose_reply(msginfo, prefs_common.reply_with_quote,
844                               FALSE, prefs_common.default_reply_list, FALSE, body);
845                 break;
846         case COMPOSE_REPLY_WITH_QUOTE:
847                 compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, body);
848                 break;
849         case COMPOSE_REPLY_WITHOUT_QUOTE:
850                 compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
851                 break;
852         case COMPOSE_REPLY_TO_SENDER:
853                 compose_reply(msginfo, prefs_common.reply_with_quote,
854                               FALSE, FALSE, TRUE, body);
855                 break;
856         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
857                 compose_followup_and_reply_to(msginfo,
858                                               prefs_common.reply_with_quote,
859                                               FALSE, FALSE, body);
860                 break;
861         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
862                 compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, body);
863                 break;
864         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
865                 compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
866                 break;
867         case COMPOSE_REPLY_TO_ALL:
868                 compose_reply(msginfo, prefs_common.reply_with_quote,
869                               TRUE, FALSE, FALSE, body);
870                 break;
871         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
872                 compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, body);
873                 break;
874         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
875                 compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
876                 break;
877         case COMPOSE_REPLY_TO_LIST:
878                 compose_reply(msginfo, prefs_common.reply_with_quote,
879                               FALSE, TRUE, FALSE, body);
880                 break;
881         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
882                 compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, body);
883                 break;
884         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
885                 compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
886                 break;
887         case COMPOSE_FORWARD:
888                 if (prefs_common.forward_as_attachment) {
889                         compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
890                         return;
891                 } else {
892                         compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
893                         return;
894                 }
895                 break;
896         case COMPOSE_FORWARD_INLINE:
897                 /* check if we reply to more than one Message */
898                 if (list_len == 1) {
899                         compose_forward(NULL, msginfo, FALSE, body, FALSE);
900                         break;
901                 } 
902                 /* more messages FALL THROUGH */
903         case COMPOSE_FORWARD_AS_ATTACH:
904                 compose_forward_multiple(NULL, msginfo_list);
905                 break;
906         case COMPOSE_REDIRECT:
907                 compose_redirect(NULL, msginfo);
908                 break;
909         default:
910                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
911         }
912 }
913
914 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
915                    gboolean to_ml, gboolean to_sender, 
916                    const gchar *body)
917 {
918         compose_generic_reply(msginfo, quote, to_all, to_ml, 
919                               to_sender, FALSE, body);
920 }
921
922 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
923                                    gboolean to_all,
924                                    gboolean to_sender,
925                                    const gchar *body)
926 {
927         compose_generic_reply(msginfo, quote, to_all, FALSE, 
928                               to_sender, TRUE, body);
929 }
930
931 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
932                                   gboolean to_all, gboolean to_ml,
933                                   gboolean to_sender,
934                                   gboolean followup_and_reply_to,
935                                   const gchar *body)
936 {
937         GtkItemFactory *ifactory;
938         Compose *compose;
939         PrefsAccount *account = NULL;
940         PrefsAccount *reply_account;
941         GtkTextView *textview;
942         GtkTextBuffer *textbuf;
943         GtkTextIter iter;
944         int cursor_pos;
945
946         g_return_if_fail(msginfo != NULL);
947         g_return_if_fail(msginfo->folder != NULL);
948
949         account = account_get_reply_account(msginfo, prefs_common.reply_account_autosel);
950         
951         g_return_if_fail(account != NULL);
952
953         if (to_sender && account->protocol == A_NNTP &&
954             !followup_and_reply_to) {
955                 reply_account =
956                         account_find_from_address(account->address);
957                 if (!reply_account)
958                         reply_account = compose_current_mail_account();
959                 if (!reply_account)
960                         return;
961         } else
962                 reply_account = account;
963
964         compose = compose_create(account, COMPOSE_REPLY);
965         ifactory = gtk_item_factory_from_widget(compose->menubar);
966
967         menu_set_active(ifactory, "/Message/Remove references", FALSE);
968         menu_set_sensitive(ifactory, "/Message/Remove references", TRUE);
969
970         compose->replyinfo = procmsg_msginfo_get_full_info(msginfo);
971         if (!compose->replyinfo)
972                 compose->replyinfo = procmsg_msginfo_copy(msginfo);
973
974         if (msginfo->folder && msginfo->folder->ret_rcpt)
975                 menu_set_active(ifactory, "/Message/Request Return Receipt", TRUE);
976
977         /* Set save folder */
978         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
979                 gchar *folderidentifier;
980
981                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
982                 folderidentifier = folder_item_get_identifier(msginfo->folder);
983                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
984                 g_free(folderidentifier);
985         }
986
987         if (compose_parse_header(compose, msginfo) < 0) return;
988         compose_reply_set_entry(compose, msginfo, to_all, to_ml, 
989                                 to_sender, followup_and_reply_to);
990         compose_show_first_last_header(compose, TRUE);
991
992         textview = (GTK_TEXT_VIEW(compose->text));
993         textbuf = gtk_text_view_get_buffer(textview);
994         
995         undo_block(compose->undostruct);
996 #ifdef USE_ASPELL
997         if (msginfo->folder && msginfo->folder->prefs && 
998             msginfo->folder->prefs && 
999             msginfo->folder->prefs->enable_default_dictionary &&
1000             compose->gtkaspell)
1001                 gtkaspell_change_dict(compose->gtkaspell, 
1002                     msginfo->folder->prefs->default_dictionary);
1003 #endif
1004
1005         if (quote) {
1006                 gchar *qmark;
1007
1008                 if (prefs_common.quotemark && *prefs_common.quotemark)
1009                         qmark = prefs_common.quotemark;
1010                 else
1011                         qmark = "> ";
1012
1013                 compose_quote_fmt(compose, compose->replyinfo,
1014                                   prefs_common.quotefmt,
1015                                   qmark, body);
1016         }
1017
1018         if (account->auto_sig)
1019                 compose_insert_sig(compose, FALSE);
1020
1021         if (quote && prefs_common.linewrap_quote)
1022                 compose_wrap_line_all(compose);
1023
1024         cursor_pos = quote_fmt_get_cursor_pos();
1025         gtk_text_buffer_get_start_iter(textbuf, &iter);
1026         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cursor_pos);
1027         gtk_text_buffer_place_cursor(textbuf, &iter);
1028
1029         if (quote && prefs_common.linewrap_quote) {
1030                 compose_wrap_line_all(compose);
1031                 gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), TRUE);
1032         }
1033
1034         gtk_widget_grab_focus(compose->text);
1035
1036         undo_unblock(compose->undostruct);
1037
1038         if (prefs_common.auto_exteditor)
1039                 compose_exec_ext_editor(compose);
1040 }
1041
1042 #define INSERT_FW_HEADER(var, hdr) \
1043 if (msginfo->var && *msginfo->var) { \
1044         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1045         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1046         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1047 }
1048
1049 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1050                          gboolean as_attach, const gchar *body,
1051                          gboolean no_extedit)
1052 {
1053         Compose *compose;
1054         GtkTextView *textview;
1055         GtkTextBuffer *textbuf;
1056         GtkTextIter iter;
1057
1058         g_return_val_if_fail(msginfo != NULL, NULL);
1059         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1060
1061         if (!account && 
1062             !(account = compose_guess_forward_account_from_msginfo
1063                                 (msginfo)))
1064                 account = cur_account;
1065
1066         compose = compose_create(account, COMPOSE_FORWARD);
1067
1068         compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
1069         if (!compose->fwdinfo)
1070                 compose->fwdinfo = procmsg_msginfo_copy(msginfo);
1071
1072         if (msginfo->subject && *msginfo->subject) {
1073                 gchar *buf, *buf2, *p;
1074
1075                 buf = p = g_strdup(msginfo->subject);
1076                 p += subject_get_prefix_length(p);
1077                 memmove(buf, p, strlen(p) + 1);
1078
1079                 buf2 = g_strdup_printf("Fw: %s", buf);
1080                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1081                 
1082                 g_free(buf);
1083                 g_free(buf2);
1084         }
1085
1086         textview = GTK_TEXT_VIEW(compose->text);
1087         textbuf = gtk_text_view_get_buffer(textview);
1088
1089         if (as_attach) {
1090                 gchar *msgfile;
1091
1092                 msgfile = procmsg_get_message_file_path(msginfo);
1093                 if (!is_file_exist(msgfile))
1094                         g_warning("%s: file not exist\n", msgfile);
1095                 else
1096                         compose_attach_append(compose, msgfile, msgfile,
1097                                               "message/rfc822");
1098
1099                 g_free(msgfile);
1100         } else {
1101                 gchar *qmark;
1102                 MsgInfo *full_msginfo;
1103
1104                 full_msginfo = procmsg_msginfo_get_full_info(msginfo);
1105                 if (!full_msginfo)
1106                         full_msginfo = procmsg_msginfo_copy(msginfo);
1107
1108                 if (prefs_common.fw_quotemark &&
1109                     *prefs_common.fw_quotemark)
1110                         qmark = prefs_common.fw_quotemark;
1111                 else
1112                         qmark = "> ";
1113
1114                 compose_quote_fmt(compose, full_msginfo,
1115                                   prefs_common.fw_quotefmt,
1116                                   qmark, body);
1117                 compose_attach_parts(compose, msginfo);
1118
1119                 procmsg_msginfo_free(full_msginfo);
1120         }
1121
1122         if (account->auto_sig)
1123                 compose_insert_sig(compose, FALSE);
1124
1125         if (prefs_common.linewrap_quote)
1126                 compose_wrap_line_all(compose);
1127
1128         gtk_text_buffer_get_start_iter(textbuf, &iter);
1129         gtk_text_buffer_place_cursor(textbuf, &iter);
1130
1131 #if 0 /* NEW COMPOSE GUI */
1132         if (account->protocol != A_NNTP)
1133                 gtk_widget_grab_focus(compose->to_entry);
1134         else
1135                 gtk_widget_grab_focus(compose->newsgroups_entry);
1136 #endif
1137         gtk_widget_grab_focus(compose->header_last->entry);
1138
1139         if (!no_extedit && prefs_common.auto_exteditor)
1140                 compose_exec_ext_editor(compose);
1141         
1142         /*save folder*/
1143         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1144                 gchar *folderidentifier;
1145
1146                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1147                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1148                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1149                 g_free(folderidentifier);
1150         }
1151
1152         return compose;
1153 }
1154
1155 #undef INSERT_FW_HEADER
1156
1157 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1158 {
1159         Compose *compose;
1160         GtkTextView *textview;
1161         GtkTextBuffer *textbuf;
1162         GtkTextIter iter;
1163         GSList *msginfo;
1164         gchar *msgfile;
1165
1166         g_return_val_if_fail(msginfo_list != NULL, NULL);
1167
1168         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next)
1169                 if (((MsgInfo *)msginfo->data)->folder == NULL)
1170                         return NULL;
1171
1172         /* guess account from first selected message */
1173         if (!account && 
1174             !(account = compose_guess_forward_account_from_msginfo
1175                                 (msginfo_list->data)))
1176                 account = cur_account;
1177
1178         g_return_val_if_fail(account != NULL, NULL);
1179
1180         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1181                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1182                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1183         }
1184
1185         compose = compose_create(account, COMPOSE_FORWARD);
1186
1187         textview = GTK_TEXT_VIEW(compose->text);
1188         textbuf = gtk_text_view_get_buffer(textview);
1189
1190         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1191                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1192                 if (!is_file_exist(msgfile))
1193                         g_warning("%s: file not exist\n", msgfile);
1194                 else
1195                         compose_attach_append(compose, msgfile, msgfile,
1196                                 "message/rfc822");
1197                 g_free(msgfile);
1198         }
1199
1200         if (account->auto_sig)
1201                 compose_insert_sig(compose, FALSE);
1202
1203         if (prefs_common.linewrap_quote)
1204                 compose_wrap_line_all(compose);
1205
1206         gtk_text_buffer_get_start_iter(textbuf, &iter);
1207         gtk_text_buffer_place_cursor(textbuf, &iter);
1208
1209         gtk_widget_grab_focus(compose->header_last->entry);
1210         
1211 #if 0 /* NEW COMPOSE GUI */
1212         if (account->protocol != A_NNTP)
1213                 gtk_widget_grab_focus(compose->to_entry);
1214         else
1215                 gtk_widget_grab_focus(compose->newsgroups_entry);
1216 #endif
1217
1218         return compose;
1219 }
1220
1221 void compose_reedit(MsgInfo *msginfo)
1222 {
1223         Compose *compose;
1224         PrefsAccount *account = NULL;
1225         GtkTextView *textview;
1226         GtkTextBuffer *textbuf;
1227         GtkTextMark *mark;
1228         GtkTextIter iter;
1229         FILE *fp;
1230         gchar buf[BUFFSIZE];
1231 #ifdef USE_GPGME
1232         gboolean use_signing = FALSE;
1233         gboolean use_encryption = FALSE;
1234         gboolean gnupg_mode = FALSE;
1235 #endif
1236
1237         g_return_if_fail(msginfo != NULL);
1238         g_return_if_fail(msginfo->folder != NULL);
1239
1240         if (msginfo->folder->stype == F_QUEUE || msginfo->folder->stype == F_DRAFT) {
1241                 gchar queueheader_buf[BUFFSIZE];
1242                 gint id, param;
1243
1244                 /* Select Account from queue headers */
1245                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1246                                              sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
1247                         id = atoi(&queueheader_buf[strlen("X-Sylpheed-Account-Id:")]);
1248                         account = account_find_from_id(id);
1249                 }
1250                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1251                                              sizeof(queueheader_buf), "NAID:")) {
1252                         id = atoi(&queueheader_buf[strlen("NAID:")]);
1253                         account = account_find_from_id(id);
1254                 }
1255                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1256                                                     sizeof(queueheader_buf), "MAID:")) {
1257                         id = atoi(&queueheader_buf[strlen("MAID:")]);
1258                         account = account_find_from_id(id);
1259                 }
1260                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1261                                                                 sizeof(queueheader_buf), "S:")) {
1262                         account = account_find_from_address(queueheader_buf);
1263                 }
1264 #ifdef USE_GPGME                
1265                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1266                                              sizeof(queueheader_buf), "X-Sylpheed-Sign:")) {
1267                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Sign:")]);
1268                         use_signing = param;
1269                         
1270                 }
1271                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1272                                              sizeof(queueheader_buf), "X-Sylpheed-Encrypt:")) {
1273                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Encrypt:")]);
1274                         use_encryption = param;
1275                 }
1276                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1277                                              sizeof(queueheader_buf), "X-Sylpheed-Gnupg-Mode:")) {
1278                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Gnupg-Mode:")]);
1279                         gnupg_mode = param;
1280                 }
1281 #endif          
1282                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1283                                              sizeof(queueheader_buf), "X-Priority: ")) {
1284                         param = atoi(&queueheader_buf[strlen("X-Priority: ")]); /* mind the space */
1285                         compose->priority = param;
1286                 }
1287         } else 
1288                 account = msginfo->folder->folder->account;
1289
1290         if (!account && prefs_common.reedit_account_autosel) {
1291                 gchar from[BUFFSIZE];
1292                 if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1293                         extract_address(from);
1294                         account = account_find_from_address(from);
1295                 }
1296         }
1297         if (!account) account = cur_account;
1298         g_return_if_fail(account != NULL);
1299
1300         compose = compose_create(account, COMPOSE_REEDIT);
1301 #ifdef USE_GPGME
1302         compose->gnupg_mode = gnupg_mode;
1303         compose_use_signing(compose, use_signing);
1304         compose_use_encryption(compose, use_encryption);
1305 #endif
1306         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1307
1308         if (msginfo->folder->stype == F_QUEUE
1309         ||  msginfo->folder->stype == F_DRAFT) {
1310                 gchar queueheader_buf[BUFFSIZE];
1311
1312                 /* Set message save folder */
1313                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1314                         gint startpos = 0;
1315
1316                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1317                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1318                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1319                 }
1320         }
1321         
1322         if (compose_parse_header(compose, msginfo) < 0) return;
1323         compose_reedit_set_entry(compose, msginfo);
1324
1325         textview = GTK_TEXT_VIEW(compose->text);
1326         textbuf = gtk_text_view_get_buffer(textview);
1327         mark = gtk_text_buffer_get_insert(textbuf);
1328         gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1329
1330         g_signal_handlers_block_by_func(G_OBJECT(textbuf),
1331                                         G_CALLBACK(compose_changed_cb),
1332                                         compose);
1333
1334         if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1335                 g_warning("Can't get text part\n");
1336         else {
1337                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1338                         strcrchomp(buf);
1339                         gtk_text_buffer_insert(textbuf, &iter, buf, -1);
1340                 }
1341                 fclose(fp);
1342         }
1343         
1344         compose_attach_parts(compose, msginfo);
1345
1346         g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
1347                                         G_CALLBACK(compose_changed_cb),
1348                                         compose);
1349
1350         gtk_widget_grab_focus(compose->text);
1351
1352         if (prefs_common.auto_exteditor)
1353                 compose_exec_ext_editor(compose);
1354 }
1355
1356 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
1357 {
1358         Compose *compose;
1359         gchar *filename;
1360         GtkItemFactory *ifactory;
1361         FolderItem *item;
1362
1363         g_return_val_if_fail(msginfo != NULL, NULL);
1364
1365         if (!account)
1366                 account = account_get_reply_account(msginfo,
1367                                         prefs_common.reply_account_autosel);
1368         g_return_val_if_fail(account != NULL, NULL);
1369
1370         compose = compose_create(account, COMPOSE_REDIRECT);
1371         ifactory = gtk_item_factory_from_widget(compose->menubar);
1372
1373         compose->replyinfo = NULL;
1374         compose->fwdinfo = NULL;
1375
1376         compose_show_first_last_header(compose, TRUE);
1377
1378         gtk_widget_grab_focus(compose->header_last->entry);
1379
1380         filename = procmsg_get_message_file(msginfo);
1381         if (filename == NULL)
1382                 return NULL;
1383
1384         compose->redirect_filename = filename;
1385         
1386         /* Set save folder */
1387         item = msginfo->folder;
1388         if (item && item->prefs && item->prefs->save_copy_to_folder) {
1389                 gchar *folderidentifier;
1390
1391                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
1392                 folderidentifier = folder_item_get_identifier(item);
1393                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1394                 g_free(folderidentifier);
1395         }
1396
1397         compose_attach_parts(compose, msginfo);
1398
1399         if (msginfo->subject)
1400                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1401                                    msginfo->subject);
1402         gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
1403
1404         compose_quote_fmt(compose, msginfo, "%M", NULL, NULL);
1405         gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), TRUE);
1406
1407         ifactory = gtk_item_factory_from_widget(compose->popupmenu);
1408         menu_set_sensitive(ifactory, "/Add...", FALSE);
1409         menu_set_sensitive(ifactory, "/Remove", FALSE);
1410         menu_set_sensitive(ifactory, "/Property...", FALSE);
1411
1412         ifactory = gtk_item_factory_from_widget(compose->menubar);
1413         menu_set_sensitive(ifactory, "/File/Save", FALSE);
1414         menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
1415         menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
1416         menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
1417         menu_set_sensitive(ifactory, "/Edit", FALSE);
1418 #if USE_GPGME
1419         menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
1420         menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
1421         menu_set_sensitive(ifactory, "/Message/Mode/MIME", FALSE);
1422         menu_set_sensitive(ifactory, "/Message/Mode/Inline", FALSE);
1423 #endif
1424         menu_set_sensitive(ifactory, "/Message/Priority", FALSE);
1425         menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
1426         menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
1427         menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
1428         
1429         gtk_widget_set_sensitive(compose->toolbar->draft_btn, FALSE);
1430         gtk_widget_set_sensitive(compose->toolbar->insert_btn, FALSE);
1431         gtk_widget_set_sensitive(compose->toolbar->attach_btn, FALSE);
1432         gtk_widget_set_sensitive(compose->toolbar->sig_btn, FALSE);
1433         gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, FALSE);
1434         gtk_widget_set_sensitive(compose->toolbar->linewrap_current_btn, FALSE);
1435         gtk_widget_set_sensitive(compose->toolbar->linewrap_all_btn, FALSE);
1436
1437         return compose;
1438 }
1439
1440 GList *compose_get_compose_list(void)
1441 {
1442         return compose_list;
1443 }
1444
1445 void compose_entry_append(Compose *compose, const gchar *address,
1446                           ComposeEntryType type)
1447 {
1448         gchar *header;
1449
1450         if (!address || *address == '\0') return;
1451
1452 #if 0 /* NEW COMPOSE GUI */
1453         switch (type) {
1454         case COMPOSE_CC:
1455                 entry = GTK_ENTRY(compose->cc_entry);
1456                 break;
1457         case COMPOSE_BCC:
1458                 entry = GTK_ENTRY(compose->bcc_entry);
1459                 break;
1460         case COMPOSE_NEWSGROUPS:
1461                 entry = GTK_ENTRY(compose->newsgroups_entry);
1462                 break;
1463         case COMPOSE_TO:
1464         default:
1465                 entry = GTK_ENTRY(compose->to_entry);
1466                 break;
1467         }
1468
1469         text = gtk_entry_get_text(entry);
1470         if (*text != '\0')
1471                 gtk_entry_append_text(entry, ", ");
1472         gtk_entry_append_text(entry, address);
1473 #endif
1474
1475         switch (type) {
1476         case COMPOSE_CC:
1477                 header = N_("Cc:");
1478                 break;
1479         case COMPOSE_BCC:
1480                 header = N_("Bcc:");
1481                 break;
1482         case COMPOSE_REPLYTO:
1483                 header = N_("Reply-To:");
1484                 break;
1485         case COMPOSE_NEWSGROUPS:
1486                 header = N_("Newsgroups:");
1487                 break;
1488         case COMPOSE_FOLLOWUPTO:
1489                 header = N_( "Followup-To:");
1490                 break;
1491         case COMPOSE_TO:
1492         default:
1493                 header = N_("To:");
1494                 break;
1495         }
1496         header = prefs_common.trans_hdr ? gettext(header) : header;
1497
1498         compose_add_header_entry(compose, header, (gchar *)address);
1499 }
1500
1501 void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
1502 {
1503         static GtkStyle *bold_style = NULL;
1504         static GdkColor bold_color;
1505         GSList *h_list;
1506         GtkEntry *entry;
1507                 
1508         for (h_list = compose->header_list; h_list != NULL; h_list = h_list->next) {
1509                 entry = GTK_ENTRY(((ComposeHeaderEntry *)h_list->data)->entry);
1510                 if (gtk_entry_get_text(entry) && 
1511                     !g_strcasecmp(gtk_entry_get_text(entry), mailto)) {
1512                         gtk_widget_ensure_style(GTK_WIDGET(entry));
1513                         if (!bold_style) {
1514                                 PangoFontDescription *font_desc = NULL;
1515                                 gtkut_convert_int_to_gdk_color
1516                                         (prefs_common.color_new, &bold_color);
1517                                 bold_style = gtk_style_copy(gtk_widget_get_style
1518                                         (GTK_WIDGET(entry)));
1519                                 if (BOLD_FONT)
1520                                         font_desc = pango_font_description_from_string
1521                                                         (BOLD_FONT);
1522                                 if (font_desc) {
1523                                         if (bold_style->font_desc)
1524                                                 pango_font_description_free
1525                                                         (bold_style->font_desc);
1526                                         bold_style->font_desc = font_desc;
1527                                 }
1528                                 bold_style->fg[GTK_STATE_NORMAL] = bold_color;
1529                         }
1530                         gtk_widget_set_style(GTK_WIDGET(entry), bold_style);
1531                 }
1532         }
1533 }
1534
1535 void compose_toolbar_cb(gint action, gpointer data)
1536 {
1537         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1538         Compose *compose = (Compose*)toolbar_item->parent;
1539         
1540         g_return_if_fail(compose != NULL);
1541
1542         switch(action) {
1543         case A_SEND:
1544                 compose_send_cb(compose, 0, NULL);
1545                 break;
1546         case A_SENDL:
1547                 compose_send_later_cb(compose, 0, NULL);
1548                 break;
1549         case A_DRAFT:
1550                 compose_draft_cb(compose, 0, NULL);
1551                 break;
1552         case A_INSERT:
1553                 compose_insert_file_cb(compose, 0, NULL);
1554                 break;
1555         case A_ATTACH:
1556                 compose_attach_cb(compose, 0, NULL);
1557                 break;
1558         case A_SIG:
1559                 compose_insert_sig(compose, FALSE);
1560                 break;
1561         case A_EXTEDITOR:
1562                 compose_ext_editor_cb(compose, 0, NULL);
1563                 break;
1564         case A_LINEWRAP_CURRENT:
1565                 compose_wrap_line(compose);
1566                 break;
1567         case A_LINEWRAP_ALL:
1568                 compose_wrap_line_all(compose);
1569                 break;
1570         case A_ADDRBOOK:
1571                 compose_address_cb(compose, 0, NULL);
1572                 break;
1573 #ifdef USE_ASPELL
1574         case A_CHECK_SPELLING:
1575                 compose_check_all(compose);
1576                 break;
1577 #endif
1578         default:
1579                 break;
1580         }
1581 }
1582
1583 static void compose_entries_set(Compose *compose, const gchar *mailto)
1584 {
1585         gchar *to = NULL;
1586         gchar *cc = NULL;
1587         gchar *bcc = NULL;
1588         gchar *subject = NULL;
1589         gchar *body = NULL;
1590
1591         scan_mailto_url(mailto, &to, &cc, &bcc, &subject, &body);
1592
1593         if (to)
1594                 compose_entry_append(compose, to, COMPOSE_TO);
1595         if (cc)
1596                 compose_entry_append(compose, cc, COMPOSE_CC);
1597         if (bcc)
1598                 compose_entry_append(compose, bcc, COMPOSE_BCC);
1599         if (subject)
1600                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1601         if (body) {
1602                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
1603                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1604                 GtkTextMark *mark;
1605                 GtkTextIter iter;
1606
1607                 mark = gtk_text_buffer_get_insert(buffer);
1608                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1609
1610                 gtk_text_buffer_insert(buffer, &iter, body, -1);
1611                 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
1612         }
1613
1614         g_free(to);
1615         g_free(cc);
1616         g_free(bcc);
1617         g_free(subject);
1618         g_free(body);
1619 }
1620
1621 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1622 {
1623         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1624                                        {"Cc:",          NULL, TRUE},
1625                                        {"References:",  NULL, FALSE},
1626                                        {"Bcc:",         NULL, TRUE},
1627                                        {"Newsgroups:",  NULL, TRUE},
1628                                        {"Followup-To:", NULL, TRUE},
1629                                        {"List-Post:",   NULL, FALSE},
1630                                        {"X-Priority:",  NULL, FALSE},
1631                                        {NULL,           NULL, FALSE}};
1632
1633         enum
1634         {
1635                 H_REPLY_TO      = 0,
1636                 H_CC            = 1,
1637                 H_REFERENCES    = 2,
1638                 H_BCC           = 3,
1639                 H_NEWSGROUPS    = 4,
1640                 H_FOLLOWUP_TO   = 5,
1641                 H_LIST_POST     = 6,
1642                 H_X_PRIORITY    = 7
1643         };
1644
1645         FILE *fp;
1646
1647         g_return_val_if_fail(msginfo != NULL, -1);
1648
1649         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1650         procheader_get_header_fields(fp, hentry);
1651         fclose(fp);
1652
1653         if (hentry[H_REPLY_TO].body != NULL) {
1654                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1655                 compose->replyto = hentry[H_REPLY_TO].body;
1656                 hentry[H_REPLY_TO].body = NULL;
1657         }
1658         if (hentry[H_CC].body != NULL) {
1659                 conv_unmime_header_overwrite(hentry[H_CC].body);
1660                 compose->cc = hentry[H_CC].body;
1661                 hentry[H_CC].body = NULL;
1662         }
1663         if (hentry[H_REFERENCES].body != NULL) {
1664                 if (compose->mode == COMPOSE_REEDIT)
1665                         compose->references = hentry[H_REFERENCES].body;
1666                 else {
1667                         compose->references = compose_parse_references
1668                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1669                         g_free(hentry[H_REFERENCES].body);
1670                 }
1671                 hentry[H_REFERENCES].body = NULL;
1672         }
1673         if (hentry[H_BCC].body != NULL) {
1674                 if (compose->mode == COMPOSE_REEDIT) {
1675                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1676                         compose->bcc = hentry[H_BCC].body;
1677                 } else
1678                         g_free(hentry[H_BCC].body);
1679                 hentry[H_BCC].body = NULL;
1680         }
1681         if (hentry[H_NEWSGROUPS].body != NULL) {
1682                 conv_unmime_header_overwrite(hentry[H_NEWSGROUPS].body);
1683                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1684                 hentry[H_NEWSGROUPS].body = NULL;
1685         }
1686         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1687                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1688                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1689                 hentry[H_FOLLOWUP_TO].body = NULL;
1690         }
1691         if (hentry[H_LIST_POST].body != NULL) {
1692                 gchar *to = NULL;
1693
1694                 extract_address(hentry[H_LIST_POST].body);
1695                 if (hentry[H_LIST_POST].body[0] != '\0') {
1696                         scan_mailto_url(hentry[H_LIST_POST].body,
1697                                         &to, NULL, NULL, NULL, NULL);
1698                         if (to) {
1699                                 g_free(compose->ml_post);
1700                                 compose->ml_post = to;
1701                         }
1702                 }
1703                 g_free(hentry[H_LIST_POST].body);
1704                 hentry[H_LIST_POST].body = NULL;
1705         }
1706
1707         /* CLAWS - X-Priority */
1708         if (compose->mode == COMPOSE_REEDIT)
1709                 if (hentry[H_X_PRIORITY].body != NULL) {
1710                         gint priority;
1711                         
1712                         priority = atoi(hentry[H_X_PRIORITY].body);
1713                         g_free(hentry[H_X_PRIORITY].body);
1714                         
1715                         hentry[H_X_PRIORITY].body = NULL;
1716                         
1717                         if (priority < PRIORITY_HIGHEST || 
1718                             priority > PRIORITY_LOWEST)
1719                                 priority = PRIORITY_NORMAL;
1720                         
1721                         compose->priority =  priority;
1722                 }
1723  
1724         if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1725                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1726         else if (compose->mode != COMPOSE_REEDIT &&
1727                  msginfo->msgid && *msginfo->msgid) {
1728                 compose->inreplyto = g_strdup(msginfo->msgid);
1729
1730                 if (!compose->references) {
1731                         if (msginfo->inreplyto && *msginfo->inreplyto)
1732                                 compose->references =
1733                                         g_strdup_printf("<%s>\n\t<%s>",
1734                                                         msginfo->inreplyto,
1735                                                         msginfo->msgid);
1736                         else
1737                                 compose->references =
1738                                         g_strconcat("<", msginfo->msgid, ">",
1739                                                     NULL);
1740                 }
1741         }
1742
1743         return 0;
1744 }
1745
1746 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1747 {
1748         GSList *ref_id_list, *cur;
1749         GString *new_ref;
1750         gchar *new_ref_str;
1751
1752         ref_id_list = references_list_append(NULL, ref);
1753         if (!ref_id_list) return NULL;
1754         if (msgid && *msgid)
1755                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1756
1757         for (;;) {
1758                 gint len = 0;
1759
1760                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1761                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1762                         len += strlen((gchar *)cur->data) + 5;
1763
1764                 if (len > MAX_REFERENCES_LEN) {
1765                         /* remove second message-ID */
1766                         if (ref_id_list && ref_id_list->next &&
1767                             ref_id_list->next->next) {
1768                                 g_free(ref_id_list->next->data);
1769                                 ref_id_list = g_slist_remove
1770                                         (ref_id_list, ref_id_list->next->data);
1771                         } else {
1772                                 slist_free_strings(ref_id_list);
1773                                 g_slist_free(ref_id_list);
1774                                 return NULL;
1775                         }
1776                 } else
1777                         break;
1778         }
1779
1780         new_ref = g_string_new("");
1781         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1782                 if (new_ref->len > 0)
1783                         g_string_append(new_ref, "\n\t");
1784                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1785         }
1786
1787         slist_free_strings(ref_id_list);
1788         g_slist_free(ref_id_list);
1789
1790         new_ref_str = new_ref->str;
1791         g_string_free(new_ref, FALSE);
1792
1793         return new_ref_str;
1794 }
1795
1796 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1797                                 const gchar *fmt, const gchar *qmark,
1798                                 const gchar *body)
1799 {
1800         static MsgInfo dummyinfo;
1801         gchar *quote_str = NULL;
1802         gchar *buf;
1803         gchar *p, *lastp;
1804         gint len;
1805         const gchar *trimmed_body = body;
1806         
1807         if (!msginfo)
1808                 msginfo = &dummyinfo;
1809
1810         if (qmark != NULL) {
1811                 quote_fmt_init(msginfo, NULL, NULL);
1812                 quote_fmt_scan_string(qmark);
1813                 quote_fmt_parse();
1814
1815                 buf = quote_fmt_get_buffer();
1816                 if (buf == NULL)
1817                         alertpanel_error(_("Quote mark format error."));
1818                 else
1819                         Xstrdup_a(quote_str, buf, return NULL)
1820         }
1821
1822         if (fmt && *fmt != '\0') {
1823                 while (trimmed_body && strlen(trimmed_body) > 1
1824                         && trimmed_body[0]=='\n')
1825                         *trimmed_body++;
1826
1827                 quote_fmt_init(msginfo, quote_str, trimmed_body);
1828                 quote_fmt_scan_string(fmt);
1829                 quote_fmt_parse();
1830
1831                 buf = quote_fmt_get_buffer();
1832                 if (buf == NULL) {
1833                         alertpanel_error(_("Message reply/forward format error."));
1834                         return NULL;
1835                 }
1836         } else
1837                 buf = "";
1838
1839         for (p = buf; *p != '\0'; ) {
1840                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
1841                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1842                 GtkTextMark *mark;
1843                 GtkTextIter iter;
1844
1845                 g_signal_handlers_block_by_func(G_OBJECT(buffer),
1846                                         G_CALLBACK(compose_changed_cb),
1847                                         compose);
1848                 g_signal_handlers_block_by_func(G_OBJECT(buffer),
1849                                         G_CALLBACK(text_inserted),
1850                                         compose);
1851                 
1852                 mark = gtk_text_buffer_get_insert(buffer);
1853                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1854
1855                 lastp = strchr(p, '\n');
1856                 len = lastp ? lastp - p + 1 : -1;
1857
1858                 gtk_text_buffer_insert(buffer, &iter, p, len);
1859
1860                 g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
1861                                         G_CALLBACK(compose_changed_cb),
1862                                         compose);
1863                 g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
1864                                         G_CALLBACK(text_inserted),
1865                                         compose);
1866                 
1867                 if (lastp)
1868                         p = lastp + 1;
1869                 else
1870                         break;
1871         }
1872
1873         return buf;
1874 }
1875
1876 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1877                                     gboolean to_all, gboolean to_ml,
1878                                     gboolean to_sender,
1879                                     gboolean followup_and_reply_to)
1880 {
1881         GSList *cc_list = NULL;
1882         GSList *cur;
1883         gchar *from = NULL;
1884         gchar *replyto = NULL;
1885         GHashTable *to_table;
1886
1887         g_return_if_fail(compose->account != NULL);
1888         g_return_if_fail(msginfo != NULL);
1889
1890         if (compose->account->protocol != A_NNTP) {
1891                 if (!compose->replyto && to_ml && compose->ml_post
1892                     && !(msginfo->folder && msginfo->folder->prefs->enable_default_reply_to))
1893                         compose_entry_append(compose,
1894                                            compose->ml_post,
1895                                            COMPOSE_TO);
1896                 else if (!(to_all || to_sender)
1897                          && msginfo->folder
1898                          && msginfo->folder->prefs->enable_default_reply_to) {
1899                         compose_entry_append(compose,
1900                             msginfo->folder->prefs->default_reply_to,
1901                             COMPOSE_TO);
1902                 } else
1903                         compose_entry_append(compose,
1904                                  (compose->replyto && !to_sender)
1905                                   ? compose->replyto :
1906                                   msginfo->from ? msginfo->from : "",
1907                                   COMPOSE_TO);
1908         } else {
1909                 if (to_sender || (compose->followup_to && 
1910                         !strncmp(compose->followup_to, "poster", 6)))
1911                         compose_entry_append
1912                                 (compose, 
1913                                  (compose->replyto ? compose->replyto :
1914                                         msginfo->from ? msginfo->from : ""),
1915                                  COMPOSE_TO);
1916                                  
1917                 else if (followup_and_reply_to || to_all) {
1918                         compose_entry_append
1919                                 (compose,
1920                                  (compose->replyto ? compose->replyto :
1921                                  msginfo->from ? msginfo->from : ""),
1922                                  COMPOSE_TO);                           
1923                 
1924                         compose_entry_append
1925                                 (compose,
1926                                  compose->followup_to ? compose->followup_to :
1927                                  compose->newsgroups ? compose->newsgroups : "",
1928                                  COMPOSE_NEWSGROUPS);
1929                 } 
1930                 else 
1931                         compose_entry_append
1932                                 (compose,
1933                                  compose->followup_to ? compose->followup_to :
1934                                  compose->newsgroups ? compose->newsgroups : "",
1935                                  COMPOSE_NEWSGROUPS);
1936         }
1937
1938         if (msginfo->subject && *msginfo->subject) {
1939                 gchar *buf, *buf2;
1940                 guchar *p;
1941
1942                 buf = p = g_strdup(msginfo->subject);
1943                 p += subject_get_prefix_length(p);
1944                 memmove(buf, p, strlen(p) + 1);
1945
1946                 buf2 = g_strdup_printf("Re: %s", buf);
1947                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1948
1949                 g_free(buf2);
1950                 g_free(buf);
1951         } else
1952                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1953
1954         if (to_ml && compose->ml_post) return;
1955         if (!to_all || compose->account->protocol == A_NNTP) return;
1956
1957         if (compose->replyto) {
1958                 Xstrdup_a(replyto, compose->replyto, return);
1959                 extract_address(replyto);
1960         }
1961         if (msginfo->from) {
1962                 Xstrdup_a(from, msginfo->from, return);
1963                 extract_address(from);
1964         }
1965
1966         if (replyto && from)
1967                 cc_list = address_list_append_with_comments(cc_list, from);
1968         if (to_all && msginfo->folder && 
1969             msginfo->folder->prefs->enable_default_reply_to)
1970                 cc_list = address_list_append_with_comments(cc_list,
1971                                 msginfo->folder->prefs->default_reply_to);
1972         cc_list = address_list_append_with_comments(cc_list, msginfo->to);
1973         cc_list = address_list_append_with_comments(cc_list, compose->cc);
1974
1975         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1976         if (replyto)
1977                 g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
1978         if (compose->account)
1979                 g_hash_table_insert(to_table, g_strdup(compose->account->address),
1980                                     GINT_TO_POINTER(1));
1981
1982         /* remove address on To: and that of current account */
1983         for (cur = cc_list; cur != NULL; ) {
1984                 GSList *next = cur->next;
1985                 gchar *addr;
1986
1987                 addr = g_strdup(cur->data);
1988                 extract_address(addr);
1989
1990                 if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
1991                         cc_list = g_slist_remove(cc_list, cur->data);
1992                 else
1993                         g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
1994
1995                 cur = next;
1996         }
1997         hash_free_strings(to_table);
1998         g_hash_table_destroy(to_table);
1999
2000         if (cc_list) {
2001                 for (cur = cc_list; cur != NULL; cur = cur->next)
2002                         compose_entry_append(compose, (gchar *)cur->data,
2003                                              COMPOSE_CC);
2004                 slist_free_strings(cc_list);
2005                 g_slist_free(cc_list);
2006         }
2007
2008 }
2009
2010 #define SET_ENTRY(entry, str) \
2011 { \
2012         if (str && *str) \
2013                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
2014 }
2015
2016 #define SET_ADDRESS(type, str) \
2017 { \
2018         if (str && *str) \
2019                 compose_entry_append(compose, str, type); \
2020 }
2021
2022 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
2023 {
2024         g_return_if_fail(msginfo != NULL);
2025
2026         SET_ENTRY(subject_entry, msginfo->subject);
2027         SET_ADDRESS(COMPOSE_TO, msginfo->to);
2028         SET_ADDRESS(COMPOSE_CC, compose->cc);
2029         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
2030         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
2031         SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
2032         SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
2033
2034         compose_update_priority_menu_item(compose);
2035 #if USE_GPGME   
2036         compose_update_gnupg_mode_menu_item(compose);
2037 #endif
2038         compose_show_first_last_header(compose, TRUE);
2039
2040 }
2041
2042 #undef SET_ENTRY
2043 #undef SET_ADDRESS
2044
2045 static void compose_insert_sig(Compose *compose, gboolean replace)
2046 {
2047         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2048         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2049         GtkTextMark *mark;
2050         GtkTextIter iter;
2051         gint cur_pos;
2052
2053         
2054         g_return_if_fail(compose->account != NULL);
2055
2056         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2057                                         G_CALLBACK(compose_changed_cb),
2058                                         compose);
2059         
2060         mark = gtk_text_buffer_get_insert(buffer);
2061         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2062         cur_pos = gtk_text_iter_get_offset (&iter);
2063
2064         gtk_text_buffer_get_end_iter(buffer, &iter);
2065
2066         if (replace && compose->sig_str) {
2067                 gboolean found;
2068                 GtkTextIter first_iter, start_iter, end_iter;
2069
2070                 gtk_text_buffer_get_start_iter(buffer, &first_iter);
2071
2072                 if (compose->sig_str[0] == '\0')
2073                         found = FALSE;
2074                 else
2075                         found = gtk_text_iter_forward_search(&first_iter,
2076                                                              compose->sig_str,
2077                                                              GTK_TEXT_SEARCH_TEXT_ONLY,
2078                                                              &start_iter, &end_iter,
2079                                                              NULL);
2080
2081                 if (found) {
2082                         gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
2083                         iter = start_iter;
2084                 }
2085         }
2086
2087         g_free(compose->sig_str);
2088         compose->sig_str = compose_get_signature_str(compose);
2089         if (!compose->sig_str || (replace && !compose->account->auto_sig))
2090                 compose->sig_str = g_strdup("");
2091
2092         gtk_text_buffer_insert(buffer, &iter, compose->sig_str, -1);
2093
2094         if (cur_pos > gtk_text_buffer_get_char_count (buffer))
2095                 cur_pos = gtk_text_buffer_get_char_count (buffer);
2096
2097         gtk_text_buffer_get_iter_at_offset (buffer, &iter, cur_pos);
2098         gtk_text_buffer_place_cursor (buffer, &iter);
2099
2100         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2101                                         G_CALLBACK(compose_changed_cb),
2102                                         compose);
2103         
2104 }
2105
2106 static gchar *compose_get_signature_str(Compose *compose)
2107 {
2108         gchar *sig_body = NULL;
2109         gchar *sig_str = NULL;
2110
2111         g_return_val_if_fail(compose->account != NULL, NULL);
2112
2113         if (!compose->account->sig_path)
2114                 return NULL;
2115
2116         if (compose->account->sig_type == SIG_FILE) {
2117                 if (!is_file_or_fifo_exist(compose->account->sig_path)) {
2118                         g_warning("can't open signature file: %s\n",
2119                                   compose->account->sig_path);
2120                         return NULL;
2121                 }
2122         }
2123
2124         if (compose->account->sig_type == SIG_COMMAND)
2125                 sig_body = get_command_output(compose->account->sig_path);
2126         else {
2127                 gchar *tmp;
2128
2129                 tmp = file_read_to_str(compose->account->sig_path);
2130                 if (!tmp)
2131                         return NULL;
2132                 sig_body = normalize_newlines(tmp);
2133                 g_free(tmp);
2134         }
2135
2136         if (compose->account->sig_sep)
2137                 sig_str = g_strconcat("\n\n", compose->account->sig_sep, "\n", sig_body,
2138                                       NULL);
2139         else
2140                 sig_str = g_strconcat("\n\n", sig_body, NULL);
2141
2142         g_free(sig_body);
2143         
2144         return sig_str;
2145 }
2146
2147 static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
2148 {
2149         GtkTextView *text;
2150         GtkTextBuffer *buffer;
2151         GtkTextMark *mark;
2152         GtkTextIter iter;
2153         gchar buf[BUFFSIZE];
2154         gint len;
2155         FILE *fp;
2156         gboolean badtxt = FALSE;
2157
2158         g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
2159
2160         if ((fp = fopen(file, "rb")) == NULL) {
2161                 FILE_OP_ERROR(file, "fopen");
2162                 return COMPOSE_INSERT_READ_ERROR;
2163         }
2164
2165         text = GTK_TEXT_VIEW(compose->text);
2166         buffer = gtk_text_view_get_buffer(text);
2167         mark = gtk_text_buffer_get_insert(buffer);
2168         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2169
2170         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2171                                         G_CALLBACK(text_inserted),
2172                                         compose);
2173
2174         while (fgets(buf, sizeof(buf), fp) != NULL) {
2175                 const gchar *cur_encoding = conv_get_current_charset_str();
2176                 gchar *str = NULL;
2177                 if (!g_utf8_validate(buf, -1, NULL))
2178                         str = conv_codeset_strdup(buf, cur_encoding, CS_UTF_8);
2179                 else
2180                         str = g_strdup(buf);
2181
2182                 if (!str) continue;
2183
2184                 /* strip <CR> if DOS/Windows file,
2185                    replace <CR> with <LF> if Macintosh file. */
2186                 strcrchomp(str);
2187                 len = strlen(str);
2188                 if (len > 0 && str[len - 1] != '\n') {
2189                         while (--len >= 0)
2190                                 if (str[len] == '\r') str[len] = '\n';
2191                 }
2192                 gtk_text_buffer_insert(buffer, &iter, str, -1);
2193
2194                 g_free (str);
2195         }
2196
2197         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2198                                           G_CALLBACK(text_inserted),
2199                                           compose);
2200
2201         fclose(fp);
2202
2203         if (badtxt)
2204                 return COMPOSE_INSERT_INVALID_CHARACTER;
2205         else 
2206                 return COMPOSE_INSERT_SUCCESS;
2207 }
2208
2209 static void compose_attach_append(Compose *compose, const gchar *file,
2210                                   const gchar *filename,
2211                                   const gchar *content_type)
2212 {
2213         AttachInfo *ainfo;
2214         gchar *text[N_ATTACH_COLS];
2215         FILE *fp;
2216         off_t size;
2217         gint row;
2218
2219         if (!is_file_exist(file)) {
2220                 g_warning("File %s doesn't exist\n", file);
2221                 return;
2222         }
2223         if ((size = get_file_size(file)) < 0) {
2224                 g_warning("Can't get file size of %s\n", file);
2225                 return;
2226         }
2227         if (size == 0) {
2228                 alertpanel_notice(_("File %s is empty."), file);
2229                 return;
2230         }
2231         if ((fp = fopen(file, "rb")) == NULL) {
2232                 alertpanel_error(_("Can't read %s."), file);
2233                 return;
2234         }
2235         fclose(fp);
2236
2237 #if 0 /* NEW COMPOSE GUI */
2238         if (!compose->use_attach) {
2239                 GtkItemFactory *ifactory;
2240
2241                 ifactory = gtk_item_factory_from_widget(compose->menubar);
2242                 menu_set_active(ifactory, "/View/Attachment", TRUE);
2243         }
2244 #endif
2245         ainfo = g_new0(AttachInfo, 1);
2246         ainfo->file = g_strdup(file);
2247
2248         if (content_type) {
2249                 ainfo->content_type = g_strdup(content_type);
2250                 if (!strcasecmp(content_type, "message/rfc822")) {
2251                         MsgInfo *msginfo;
2252                         MsgFlags flags = {0, 0};
2253                         const gchar *name;
2254
2255                         if (procmime_get_encoding_for_file(file) == ENC_7BIT)
2256                                 ainfo->encoding = ENC_7BIT;
2257                         else
2258                                 ainfo->encoding = ENC_8BIT;
2259
2260                         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
2261                         if (msginfo && msginfo->subject)
2262                                 name = msginfo->subject;
2263                         else
2264                                 name = g_basename(filename ? filename : file);
2265
2266                         ainfo->name = g_strdup_printf(_("Message: %s"), name);
2267
2268                         procmsg_msginfo_free(msginfo);
2269                 } else {
2270                         if (!g_strncasecmp(content_type, "text", 4))
2271                                 ainfo->encoding =
2272                                         procmime_get_encoding_for_file(file);
2273                         else
2274                                 ainfo->encoding = ENC_BASE64;
2275                         ainfo->name = g_strdup
2276                                 (g_basename(filename ? filename : file));
2277                 }
2278         } else {
2279                 ainfo->content_type = procmime_get_mime_type(file);
2280                 if (!ainfo->content_type) {
2281                         ainfo->content_type =
2282                                 g_strdup("application/octet-stream");
2283                         ainfo->encoding = ENC_BASE64;
2284                 } else if (!g_strncasecmp(ainfo->content_type, "text", 4))
2285                         ainfo->encoding = procmime_get_encoding_for_file(file);
2286                 else
2287                         ainfo->encoding = ENC_BASE64;
2288                 ainfo->name = g_strdup(g_basename(filename ? filename : file)); 
2289         }
2290         ainfo->size = size;
2291
2292         text[COL_MIMETYPE] = ainfo->content_type;
2293         text[COL_SIZE] = to_human_readable(size);
2294         text[COL_NAME] = ainfo->name;
2295
2296         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
2297         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
2298 }
2299
2300 #ifdef USE_GPGME
2301 static void compose_use_signing(Compose *compose, gboolean use_signing)
2302 {
2303         GtkItemFactory *ifactory;
2304         GtkWidget *menuitem = NULL;
2305
2306         compose->use_signing = use_signing;
2307         ifactory = gtk_item_factory_from_widget(compose->menubar);
2308         menuitem = gtk_item_factory_get_item
2309                 (ifactory, "/Message/Sign");
2310
2311         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
2312                                        use_signing);
2313         compose_update_gnupg_mode_menu_item(compose);
2314 }
2315
2316 static void compose_use_encryption(Compose *compose, gboolean use_encryption)
2317 {
2318         GtkItemFactory *ifactory;
2319         GtkWidget *menuitem = NULL;
2320
2321         compose->use_encryption = use_encryption;
2322         ifactory = gtk_item_factory_from_widget(compose->menubar);
2323         menuitem = gtk_item_factory_get_item
2324                 (ifactory, "/Message/Encrypt");
2325
2326         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
2327                                        use_encryption);
2328         compose_update_gnupg_mode_menu_item(compose);
2329 }
2330 #endif /* USE_GPGME */
2331
2332 #define NEXT_PART_NOT_CHILD(info)  \
2333 {  \
2334         node = info->node;  \
2335         while (node->children)  \
2336                 node = g_node_last_child(node);  \
2337         info = procmime_mimeinfo_next((MimeInfo *)node->data);  \
2338 }
2339
2340 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
2341 {
2342         MimeInfo *mimeinfo;
2343         MimeInfo *child;
2344         MimeInfo *firsttext = NULL;
2345         MimeInfo *encrypted = NULL;
2346         GNode    *node;
2347         gchar *outfile;
2348         const gchar *partname = NULL;
2349
2350         mimeinfo = procmime_scan_message(msginfo);
2351         if (!mimeinfo) return;
2352
2353         if (mimeinfo->node->children == NULL) {
2354                 procmime_mimeinfo_free_all(mimeinfo);
2355                 return;
2356         }
2357
2358         /* find first content part */
2359         child = (MimeInfo *) mimeinfo->node->children->data;
2360         while (child && child->node->children && (child->type == MIMETYPE_MULTIPART))
2361                 child = (MimeInfo *)child->node->children->data;
2362
2363         if (child->type == MIMETYPE_TEXT) {
2364                 firsttext = child;
2365                 debug_print("First text part found\n");
2366         } else if (compose->mode == COMPOSE_REEDIT &&
2367                  child->type == MIMETYPE_APPLICATION &&
2368                  !strcasecmp(child->subtype, "pgp-encrypted")) {
2369                 AlertValue val;
2370                 val = alertpanel(_("Encrypted message"),
2371                                  _("Cannot re-edit an encrypted message. \n"
2372                                    "Discard encrypted part?"),
2373                                  _("Yes"), _("No"), NULL);
2374                 if (val == G_ALERTDEFAULT) 
2375                         encrypted = (MimeInfo *)child->node->parent->data;
2376         }
2377      
2378         child = (MimeInfo *) mimeinfo->node->children->data;
2379         while (child != NULL) {
2380                 if (child == encrypted) {
2381                         /* skip this part of tree */
2382                         NEXT_PART_NOT_CHILD(child);
2383                         continue;
2384                 }
2385
2386                 if (child->type == MIMETYPE_MULTIPART) {
2387                         /* get the actual content */
2388                         child = procmime_mimeinfo_next(child);
2389                         continue;
2390                 }
2391                     
2392                 if (child == firsttext) {
2393                         child = procmime_mimeinfo_next(child);
2394                         continue;
2395                 }
2396
2397                 if ((compose->mode == COMPOSE_REEDIT || 
2398                      compose->mode == COMPOSE_FORWARD_INLINE ||
2399                      compose->mode == COMPOSE_FORWARD )  &&
2400                     (child->type == MIMETYPE_APPLICATION) && 
2401                     !strcmp(child->subtype, "pgp-signature")) {
2402 #ifdef USE_GPGME
2403                         if (compose->mode == COMPOSE_REEDIT) {
2404                                 compose->gnupg_mode  = GNUPG_MODE_DETACH;
2405                                 compose_use_signing(compose, TRUE);
2406                         }
2407 #endif
2408                         child = procmime_mimeinfo_next(child);
2409                         continue;
2410                 }
2411                 outfile = procmime_get_tmp_file_name(child);
2412                 if (procmime_get_part(outfile, child) < 0)
2413                         g_warning("Can't get the part of multipart message.");
2414                 else {
2415                         gchar *content_type;
2416
2417                         content_type = g_strdup_printf("%s/%s", procmime_get_type_str(child->type), child->subtype);
2418                         partname = procmime_mimeinfo_get_parameter(child, "name");
2419                         if (partname == NULL)
2420                                 partname = "";
2421                         compose_attach_append(compose, outfile, 
2422                                               partname, content_type);
2423                         g_free(content_type);
2424                 }
2425                 g_free(outfile);
2426                 NEXT_PART_NOT_CHILD(child);
2427         }
2428         procmime_mimeinfo_free_all(mimeinfo);
2429 }
2430
2431   
2432 #define CHAR_BUF_SIZE 8
2433 #undef NEXT_PART_NOT_CHILD
2434
2435 #define GET_CHAR(iter_p, buf, len)                                           \
2436 {                                                                            \
2437         GtkTextIter end_iter;                                                \
2438         gchar *tmp;                                                          \
2439         end_iter = *iter_p;                                                  \
2440         gtk_text_iter_forward_char(&end_iter);                               \
2441         tmp = gtk_text_buffer_get_text(textbuf, iter_p, &end_iter, FALSE);   \
2442         if (tmp) {                                                           \
2443                 glong items_read, items_witten;                              \
2444                 GError *error = NULL;                                        \
2445                 gunichar *wide_char;                                         \
2446                 strncpy2(buf, tmp, CHAR_BUF_SIZE);                           \
2447                 wide_char = g_utf8_to_ucs4(tmp, -1,                          \
2448                                            &items_read, &items_witten,       \
2449                                            &error);                          \
2450                 if (error != NULL) {                                         \
2451                         g_warning("%s\n", error->message);                   \
2452                         g_error_free(error);                                 \
2453                 }                                                            \
2454                 len = wide_char && g_unichar_iswide(*wide_char) ? 2 : 1;     \
2455                 g_free(wide_char);                                           \
2456         } else {                                                             \
2457                 buf[0] = '\0';                                               \
2458                 len = 1;                                                     \
2459         }                                                                    \
2460         g_free(tmp);                                                         \
2461 }
2462
2463 #define DISP_WIDTH(len) \
2464         ((len > 2 && conv_get_current_charset() == C_UTF_8) ? 2 : \
2465          (len == 2 && conv_get_current_charset() == C_UTF_8) ? 1 : len)
2466
2467 #define SPACE_CHARS     " \t"
2468
2469 static void compose_wrap_line(Compose *compose)
2470 {
2471         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2472         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
2473         GtkTextMark *mark;
2474         GtkTextIter insert_iter, iter;
2475         gint ch_len, last_ch_len;
2476         gchar cbuf[CHAR_BUF_SIZE], last_ch;
2477         guint text_len;
2478         gint p_start, p_end;
2479         gint line_pos, cur_pos;
2480         gint line_len, cur_len;
2481         gboolean line_end, quoted;
2482
2483         text_len = gtk_text_buffer_get_char_count(textbuf);
2484         mark = gtk_text_buffer_get_insert(textbuf);
2485         gtk_text_buffer_get_iter_at_mark(textbuf, &insert_iter, mark);
2486         cur_pos = gtk_text_iter_get_offset(&insert_iter);
2487         GET_CHAR(&insert_iter, cbuf, ch_len);
2488         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2489                 GtkTextIter prev_iter;
2490                 if (cur_pos == 0)
2491                         return; /* on the paragraph mark */
2492                 prev_iter = insert_iter;
2493                 gtk_text_iter_backward_char(&prev_iter);
2494                 GET_CHAR(&prev_iter, cbuf, ch_len);
2495                 if (ch_len == 1 && *cbuf == '\n')
2496                         return; /* on the paragraph mark */
2497         }
2498
2499         /* find paragraph start. */
2500         line_end = quoted = FALSE;
2501         for (iter = insert_iter; gtk_text_iter_backward_char(&iter);) {
2502                 GET_CHAR(&iter, cbuf, ch_len);
2503                 if (ch_len == 1 && *cbuf == '\n') {
2504                         if (quoted)
2505                                 return; /* quoted part */
2506                         if (line_end) {
2507                                 gtk_text_iter_forward_chars(&iter, 2);
2508                                 break;
2509                         }
2510                         line_end = TRUE;
2511                 } else {
2512                         if (ch_len == 1 
2513                             && strchr(prefs_common.quote_chars, *cbuf))
2514                                 quoted = 1;
2515                         else if (ch_len != 1 || !isspace(*(guchar *)cbuf))
2516                                 quoted = 0;
2517
2518                         line_end = FALSE;
2519                 }
2520         }
2521         p_start = gtk_text_iter_get_offset(&iter);
2522
2523         /* find paragraph end. */
2524         line_end = FALSE;
2525         for (iter = insert_iter; gtk_text_iter_forward_char(&iter);) {
2526                 GET_CHAR(&iter, cbuf, ch_len);
2527                 if (ch_len == 1 && *cbuf == '\n') {
2528                         if (line_end) {
2529                                 p_end -= 1;
2530                                 gtk_text_iter_backward_char(&iter);
2531                                 break;
2532                         }
2533                         line_end = TRUE;
2534                 } else {
2535                         if (line_end && ch_len == 1 &&
2536                             strchr(prefs_common.quote_chars, *cbuf))
2537                                 return; /* quoted part */
2538
2539                         line_end = FALSE;
2540                 }
2541         }
2542         p_end = gtk_text_iter_get_offset(&iter);
2543
2544         if (p_end >= text_len)
2545                 p_end = text_len;
2546
2547         if (p_start >= p_end)
2548                 return;
2549
2550         line_len = cur_len = 0;
2551         last_ch_len = 0;
2552         last_ch = '\0';
2553         line_pos = p_start;
2554         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2555                 gboolean space = FALSE;
2556
2557                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cur_pos);
2558
2559                 GET_CHAR(&iter, cbuf, ch_len);
2560
2561                 if (ch_len < 0) {
2562                         cbuf[0] = '\0';
2563                         ch_len = 1;
2564                 }
2565
2566                 if (ch_len == 1 && isspace(*(guchar *)cbuf))
2567                         space = 1;
2568
2569                 if (ch_len == 1 && *cbuf == '\n') {
2570                         gboolean replace = FALSE;
2571                         GtkTextIter next_iter = iter;
2572
2573                         gtk_text_iter_forward_char(&next_iter);
2574
2575                         if (last_ch_len == 1 && !isspace((guchar)last_ch)) {
2576                                 if (cur_pos + 1 < p_end) {
2577                                         GET_CHAR(&next_iter, cbuf, ch_len);
2578                                         if (ch_len == 1 &&
2579                                             !isspace(*(guchar *)cbuf))
2580                                                 replace = TRUE;
2581                                 }
2582                         }
2583                         gtk_text_buffer_delete(textbuf, &iter, &next_iter);
2584                         if (replace) {
2585                                 gtk_text_buffer_insert(textbuf, &iter, " ", 1);
2586                                 space = TRUE;
2587                         }
2588                         else {
2589                                 p_end--;
2590                                 cur_pos--;
2591                                 gtk_text_buffer_get_iter_at_offset
2592                                         (textbuf, &iter, cur_pos);
2593                                 continue;
2594                         }
2595                 }
2596
2597                 last_ch_len = ch_len;
2598                 last_ch = *cbuf;
2599
2600                 if (space) {
2601                         line_pos = cur_pos + 1;
2602                         line_len = cur_len + ch_len;
2603                 }
2604
2605                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
2606
2607                 if (cur_len + ch_len > prefs_common.linewrap_len &&
2608                     line_len > 0) {
2609                         gint tlen = ch_len;
2610                         GtkTextIter prev_iter = iter;
2611
2612                         gtk_text_iter_backward_char(&prev_iter);
2613
2614                         GET_CHAR(&prev_iter, cbuf, ch_len);
2615                         if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
2616                                 gtk_text_buffer_delete(textbuf, &prev_iter, &iter);
2617                                 iter = prev_iter;
2618                                 p_end--;
2619                                 cur_pos--;
2620                                 line_pos--;
2621                                 cur_len--;
2622                                 line_len--;
2623                         }
2624                         ch_len = tlen;
2625
2626                         gtk_text_buffer_insert(textbuf, &iter, "\n", 1);
2627                         p_end++;
2628                         cur_pos++;
2629                         line_pos++;
2630                         cur_len = cur_len - line_len + ch_len;
2631                         line_len = 0;
2632                         continue;
2633                 }
2634
2635                 if (ch_len > 1) {
2636                         line_pos = cur_pos + 1;
2637                         line_len = cur_len + ch_len;
2638                 }
2639                 cur_len += ch_len;
2640         }
2641 }
2642
2643 #undef WRAP_DEBUG
2644 #ifdef WRAP_DEBUG
2645 /* Darko: used when I debug wrapping */
2646 void dump_text(GtkTextBuffer textbuf, int pos, int tlen, int breakoncr)
2647 {
2648         gint i, clen;
2649         gchar cbuf[CHAR_BUF_SIZE];
2650
2651         printf("%d [", pos);
2652         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, pos);
2653         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + tlen);
2654         for (; gtk_text_iter_forward_char(&iter) &&
2655                      gtk_text_iter_compare(&iter, &end_iter) < 0;)
2656                 GET_CHAR(&iter, cbuf, clen);
2657                 if (clen < 0) break;
2658                 if (breakoncr && clen == 1 && cbuf[0] == '\n')
2659                         break;
2660                 fwrite(cbuf, clen, 1, stdout);
2661         }
2662         printf("]\n");
2663 }
2664 #endif
2665
2666 typedef enum {
2667         WAIT_FOR_SPACE,
2668         WAIT_FOR_INDENT_CHAR,
2669         WAIT_FOR_INDENT_CHAR_OR_SPACE
2670 } IndentState;
2671
2672 /* return indent length, we allow:
2673    > followed by spaces/tabs
2674    | followed by spaces/tabs
2675    uppercase characters immediately followed by >,
2676    and the repeating sequences of the above */
2677 /* return indent length */
2678 static guint get_indent_length(GtkTextBuffer *textbuf, guint start_pos, guint text_len)
2679 {
2680         guint i_len = 0;
2681         guint i, ch_len, alnum_cnt = 0;
2682         IndentState state = WAIT_FOR_INDENT_CHAR;
2683         gchar cbuf[CHAR_BUF_SIZE];
2684         gboolean is_space;
2685         gboolean is_indent;
2686
2687         if (prefs_common.quote_chars == NULL) {
2688                 return 0 ;
2689         }
2690
2691         for (i = start_pos; i < text_len; i++) {
2692                 GtkTextIter iter;
2693
2694                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, i);
2695                 GET_CHAR(&iter, cbuf, ch_len);
2696                 if (ch_len > 1)
2697                         break;
2698
2699                 if (cbuf[0] == '\n')
2700                         break;
2701
2702                 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2703                 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2704
2705                 switch (state) {
2706                 case WAIT_FOR_SPACE:
2707                         if (is_space == FALSE)
2708                                 goto out;
2709                         state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2710                         break;
2711                 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2712                         if (is_indent == FALSE && is_space == FALSE &&
2713                             !isupper((guchar)cbuf[0]))
2714                                 goto out;
2715                         if (is_space == TRUE) {
2716                                 alnum_cnt = 0;
2717                                 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2718                         } else if (is_indent == TRUE) {
2719                                 alnum_cnt = 0;
2720                                 state = WAIT_FOR_SPACE;
2721                         } else {
2722                                 alnum_cnt++;
2723                                 state = WAIT_FOR_INDENT_CHAR;
2724                         }
2725                         break;
2726                 case WAIT_FOR_INDENT_CHAR:
2727                         if (is_indent == FALSE && !isupper((guchar)cbuf[0]))
2728                                 goto out;
2729                         if (is_indent == TRUE) {
2730                                 if (alnum_cnt > 0 
2731                                     && !strchr(prefs_common.quote_chars, cbuf[0]))
2732                                         goto out;
2733                                 alnum_cnt = 0;
2734                                 state = WAIT_FOR_SPACE;
2735                         } else {
2736                                 alnum_cnt++;
2737                         }
2738                         break;
2739                 }
2740
2741                 i_len++;
2742         }
2743
2744 out:
2745         if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2746                 i_len -= alnum_cnt;
2747
2748         return i_len;
2749 }
2750
2751 /* insert quotation string when line was wrapped */
2752 static guint ins_quote(GtkTextBuffer *textbuf, GtkTextIter *iter,
2753                        guint indent_len,
2754                        guint prev_line_pos, guint text_len,
2755                        gchar *quote_fmt)
2756 {
2757         guint ins_len = 0;
2758
2759         if (indent_len) {
2760                 GtkTextIter iter1, iter2;
2761                 gchar *text;
2762
2763                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter1,
2764                                                    prev_line_pos);
2765                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter2,
2766                                                    prev_line_pos + indent_len);
2767                 text = gtk_text_buffer_get_text(textbuf, &iter1, &iter2, FALSE);
2768                 if (!text) return 0;
2769
2770                 gtk_text_buffer_insert(textbuf, iter, text, -1);
2771                 ins_len = g_utf8_strlen(text, -1);
2772
2773                 g_free(text);
2774         }
2775
2776         return ins_len;
2777 }
2778
2779 static gboolean is_sig_separator(Compose *compose, GtkTextBuffer *textbuf, guint start_pos) 
2780 {
2781         char *text = NULL;
2782         GtkTextIter iter; 
2783         GtkTextIter end_iter;
2784         if (!compose->account->sig_sep)
2785                 return FALSE;
2786         
2787         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, start_pos+1);
2788         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
2789                 start_pos+strlen(compose->account->sig_sep)+1);
2790
2791         if (!strcmp(gtk_text_iter_get_text(&iter, &end_iter),
2792                         compose->account->sig_sep)) {
2793                 /* check \n */
2794                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter,
2795                         start_pos+strlen(compose->account->sig_sep)+1);
2796                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
2797                         start_pos+strlen(compose->account->sig_sep)+2);
2798
2799                 if (!strcmp(gtk_text_iter_get_text(&iter, &end_iter),"\n"));
2800                         return TRUE;
2801                 
2802
2803         }
2804
2805         return FALSE;
2806 }
2807
2808 /* check if we should join the next line */
2809 static gboolean join_next_line_is_needed(GtkTextBuffer *textbuf,
2810                                          guint start_pos, guint tlen,
2811                                          guint prev_ilen, gboolean autowrap)
2812 {
2813         guint indent_len, ch_len;
2814         gboolean do_join = FALSE;
2815         gchar cbuf[CHAR_BUF_SIZE];
2816
2817         indent_len = get_indent_length(textbuf, start_pos, tlen);
2818
2819         if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
2820                 GtkTextIter iter;
2821                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter,
2822                                                    start_pos + indent_len);
2823                 GET_CHAR(&iter, cbuf, ch_len);
2824                 
2825                 if (ch_len > 0 && (cbuf[0] != '\n'))
2826                         do_join = TRUE;
2827         }
2828
2829         return do_join;
2830 }
2831
2832 static void compose_wrap_line_all(Compose *compose)
2833 {
2834         compose_wrap_line_all_full(compose, FALSE);
2835 }
2836
2837 static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
2838 {
2839         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2840         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
2841         GtkTextIter iter, end_iter;
2842         guint tlen;
2843         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2844         gint line_len = 0, cur_len = 0;
2845         gint ch_len;
2846         gboolean is_new_line = TRUE, do_delete = FALSE;
2847         guint i_len = 0;
2848         gboolean linewrap_quote = TRUE;
2849         gboolean set_editable_pos = FALSE;
2850         gint editable_pos = 0;
2851         guint linewrap_len = prefs_common.linewrap_len;
2852         gchar *qfmt = prefs_common.quotemark;
2853         gchar cbuf[CHAR_BUF_SIZE];
2854         GtkTextMark *cursor_mark = gtk_text_buffer_get_insert(textbuf);
2855         
2856         tlen = gtk_text_buffer_get_char_count(textbuf);
2857
2858         for (; cur_pos < tlen; cur_pos++) {
2859                 /* mark position of new line - needed for quotation wrap */
2860                 if (is_new_line) {
2861                         if (linewrap_quote)
2862                                 i_len = get_indent_length(textbuf, cur_pos, tlen);
2863
2864                         is_new_line = FALSE;
2865                         p_pos = cur_pos;
2866                 }
2867
2868                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cur_pos);
2869                 GET_CHAR(&iter, cbuf, ch_len);
2870
2871                 /* fix line length for tabs */
2872                 if (ch_len == 1 && *cbuf == '\t') {
2873                         guint tab_width = 8;
2874                         guint tab_offset = line_len % tab_width;
2875
2876                         line_len += tab_width - tab_offset - 1;
2877                         cur_len = line_len;
2878                 }
2879
2880                 /* we have encountered line break */
2881                 if (ch_len == 1 && *cbuf == '\n') {
2882                         gint clen;
2883                         gchar cb[CHAR_BUF_SIZE];
2884
2885                         /* should we join the next line */
2886                         if ((autowrap || i_len != cur_len) && do_delete &&
2887                         !is_sig_separator(compose, textbuf, cur_pos+i_len) &&
2888                             join_next_line_is_needed
2889                                 (textbuf, cur_pos + 1, tlen, i_len, autowrap)) {
2890                                 do_delete = TRUE;
2891                         } else
2892                                 do_delete = FALSE;
2893
2894                         /* skip delete if it is continuous URL */
2895                         if (do_delete && (line_pos - p_pos <= i_len) &&
2896                             gtkut_text_buffer_is_uri_string(textbuf, line_pos,
2897                                                             tlen))
2898                                 do_delete = FALSE;
2899
2900                         /* should we delete to perform smart wrapping */
2901                         if (line_len < linewrap_len && do_delete) {
2902                                 /* get rid of newline */
2903                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2904                                                                    &iter,
2905                                                                    cur_pos);
2906                                 end_iter = iter;
2907                                 gtk_text_iter_forward_char(&end_iter);
2908                                 gtk_text_buffer_delete(textbuf, &iter, &end_iter);
2909                                 tlen--;
2910
2911                                 /* if text starts with quote fmt or with
2912                                    indent string, delete them */
2913                                 if (i_len) {
2914                                         guint ilen;
2915                                         ilen =  gtkut_text_buffer_str_compare_n
2916                                                 (textbuf, cur_pos, p_pos, i_len,
2917                                                  tlen);
2918                                         if (ilen) {
2919                                                 end_iter = iter;
2920                                                 gtk_text_iter_forward_chars
2921                                                         (&end_iter, ilen);
2922                                                 gtk_text_buffer_delete(textbuf,
2923                                                                        &iter,
2924                                                                        &end_iter);
2925                                                 tlen -= ilen;
2926                                         }
2927                                 }
2928
2929                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2930                                                                    &iter,
2931                                                                    cur_pos);
2932                                 GET_CHAR(&iter, cb, clen);
2933
2934                                 /* insert space if it's alphanumeric */
2935                                 if ((cur_pos != line_pos) &&
2936                                     ((clen > 1) || isalnum((guchar)cb[0]))) {
2937                                         GtkTextIter cursor_iter;
2938                                         gboolean go_back = FALSE;
2939                                         gtk_text_buffer_get_iter_at_mark(textbuf, &cursor_iter, cursor_mark);
2940                                         if (gtk_text_iter_get_offset(&iter) ==
2941                                                  gtk_text_iter_get_offset(&cursor_iter))
2942                                                 go_back = TRUE;
2943                                         
2944                                         gtk_text_buffer_insert(textbuf, &iter,
2945                                                                " ", 1);
2946                                         if (go_back) {
2947                                                 gtk_text_buffer_get_iter_at_mark(textbuf, &cursor_iter, cursor_mark);
2948                                                 gtk_text_iter_backward_chars(&cursor_iter, 1);
2949                                                 gtk_text_buffer_place_cursor(textbuf, &cursor_iter);
2950                                         }
2951                                         tlen++;
2952                                 }
2953
2954                                 /* and start over with current line */
2955                                 cur_pos = p_pos - 1;
2956                                 line_pos = cur_pos;
2957                                 line_len = cur_len = 0;
2958                                 do_delete = FALSE;
2959                                 is_new_line = TRUE;
2960 #ifdef WRAP_DEBUG
2961                                 g_print("after delete l_pos=");
2962                                 dump_text(text, line_pos, tlen, 1);
2963 #endif
2964                                 /* move beginning of line if we are on LF */
2965                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2966                                                                    &iter,
2967                                                                    line_pos);
2968                                 GET_CHAR(&iter, cb, clen);
2969                                 if (clen == 1 && *cb == '\n')
2970                                         line_pos++;
2971 #ifdef WRAP_DEBUG
2972                                 g_print("new line_pos=%d\n", line_pos);
2973 #endif
2974
2975                                 continue;
2976                         }
2977
2978                         /* mark new line beginning */
2979                         line_pos = cur_pos + 1;
2980                         line_len = cur_len = 0;
2981                         do_delete = FALSE;
2982                         is_new_line = TRUE;
2983                         continue;
2984                 }
2985
2986                 if (ch_len < 0) {
2987                         cbuf[0] = '\0';
2988                         ch_len = 1;
2989                 }
2990
2991                 /* possible line break */
2992                 if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
2993                         line_pos = cur_pos + 1;
2994                         line_len = cur_len + ch_len;
2995                 }
2996
2997                 /* are we over wrapping length set in preferences ? */
2998                 if (cur_len + DISP_WIDTH(ch_len) > linewrap_len) {
2999                         gint clen;
3000
3001 #ifdef WRAP_DEBUG
3002                         g_print("should wrap cur_pos=%d ", cur_pos);
3003                         dump_text(text, p_pos, tlen, 1);
3004                         dump_text(text, line_pos, tlen, 1);
3005 #endif
3006                         /* force wrapping if it is one long word but not URL */
3007                         if (line_pos - p_pos <= i_len)
3008                                 if (!gtkut_text_buffer_is_uri_string
3009                                     (textbuf, line_pos, tlen))
3010                                         line_pos = cur_pos - 1;
3011 #ifdef WRAP_DEBUG
3012                         g_print("new line_pos=%d\n", line_pos);
3013 #endif
3014
3015                         gtk_text_buffer_get_iter_at_offset(textbuf,
3016                                                            &iter,
3017                                                            line_pos - 1);
3018                         GET_CHAR(&iter, cbuf, clen);
3019
3020                         /* if next character is space delete it */
3021                         if (clen == 1 && isspace(*(guchar *)cbuf)) {
3022                                 if (p_pos + i_len != line_pos ||
3023                                     !gtkut_text_buffer_is_uri_string
3024                                         (textbuf, line_pos, tlen)) {
3025                                         /* workaround for correct cursor
3026                                            position */
3027                                         if (set_editable_pos == FALSE) {
3028                                                 GtkTextMark *ins = gtk_text_buffer_get_insert(textbuf);
3029                                                 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, ins);
3030                                                 editable_pos = gtk_text_iter_get_offset(&iter);
3031                                                 if (editable_pos == line_pos)
3032                                                         set_editable_pos = TRUE;
3033                                         }
3034                                         gtk_text_buffer_get_iter_at_offset(textbuf,
3035                                                            &iter,
3036                                                            line_pos-1);
3037                                         gtk_text_buffer_get_iter_at_offset(textbuf,
3038                                                            &end_iter,
3039                                                            line_pos);
3040                                         gtk_text_buffer_delete(textbuf, &iter, &end_iter);
3041                                         //gtk_stext_set_point(text, line_pos);
3042                                         //gtk_stext_backward_delete(text, 1);
3043                                         tlen--;
3044                                         cur_pos--;
3045                                         line_pos--;
3046                                         cur_len--;
3047                                         line_len--;
3048                                 }
3049                         }
3050
3051                         /* if it is URL at beginning of line don't wrap */
3052                         if (p_pos + i_len == line_pos &&
3053                             gtkut_text_buffer_is_uri_string(textbuf, line_pos, tlen)) {
3054 #ifdef WRAP_DEBUG
3055                                 g_print("found URL at ");
3056                                 dump_text(text, line_pos, tlen, 1);
3057 #endif
3058                                 continue;
3059                         }
3060
3061                         /* insert CR */
3062                         gtk_text_buffer_get_iter_at_offset(textbuf,
3063                                                            &iter,
3064                                                            line_pos);
3065                         gtk_text_buffer_insert(textbuf, &iter, "\n", 1);
3066                         //gtk_stext_set_point(text, line_pos);
3067                         //gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
3068                         //gtk_stext_compact_buffer(text);
3069                         tlen++;
3070                         line_pos++;
3071                         /* for loop will increase it */
3072                         cur_pos = line_pos - 1;
3073                         /* start over with current line */
3074                         is_new_line = TRUE;
3075                         line_len = cur_len = 0;
3076                         if (autowrap || i_len > 0) {
3077                                 do_delete = TRUE;
3078                         } else
3079                                 do_delete = FALSE;
3080 #ifdef WRAP_DEBUG
3081                         g_print("after CR insert ");
3082                         dump_text(text, line_pos, tlen, 1);
3083                         dump_text(text, cur_pos, tlen, 1);
3084 #endif
3085
3086                         /* should we insert quotation ? */
3087                         if (linewrap_quote && i_len) {
3088                                 /* only if line is not already quoted  */
3089                                 if (!gtkut_text_buffer_str_compare
3090                                         (textbuf, line_pos, tlen, qfmt)) {
3091                                         guint ins_len;
3092                                         
3093                                         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
3094                                         
3095                                         if (line_pos - p_pos > i_len) {
3096                                                 ins_len = ins_quote
3097                                                         (textbuf, &iter, i_len, p_pos,
3098                                                          tlen, qfmt);
3099                                                 tlen += ins_len;
3100                                         }
3101 #ifdef WRAP_DEBUG
3102                                         g_print("after quote insert ");
3103                                         dump_text(text, line_pos, tlen, 1);
3104 #endif
3105                                 }
3106                         }
3107                         continue;
3108                 }
3109
3110                 if (ch_len > 1) {
3111                         line_pos = cur_pos + 1;
3112                         line_len = cur_len + ch_len;
3113                 }
3114                 /* advance to next character in buffer */
3115                 cur_len += ch_len;
3116         }
3117
3118         if (set_editable_pos && editable_pos <= tlen) {
3119                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, editable_pos);
3120                 gtk_text_buffer_place_cursor(textbuf, &iter);
3121         }
3122 }
3123
3124 #undef GET_CHAR
3125 #undef CHAR_BUF_SIZE
3126
3127 static void compose_set_title(Compose *compose)
3128 {
3129         gchar *str;
3130         gchar *edited;
3131
3132         edited = compose->modified ? _(" [Edited]") : "";
3133         if (compose->account && compose->account->address)
3134                 str = g_strdup_printf(_("%s - Compose message%s"),
3135                                       compose->account->address, edited);
3136         else
3137                 str = g_strdup_printf(_("Compose message%s"), edited);
3138         gtk_window_set_title(GTK_WINDOW(compose->window), str);
3139         g_free(str);
3140 }
3141
3142 /**
3143  * compose_current_mail_account:
3144  * 
3145  * Find a current mail account (the currently selected account, or the