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