New feature: toggle visibility of mime buttons.
[more-wl.git] / wl / wl-draft.el
blob1b40de7612a3296bd8768d4d92add66f2bbc64fd
1 ;;; wl-draft.el --- Message draft mode for Wanderlust.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;; Masahiro MURATA <muse@ba2.so-net.ne.jp>
8 ;; Keywords: mail, net news
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
31 ;;; Code:
33 (require 'elmo)
34 (require 'sendmail)
35 (require 'wl-template)
36 (require 'emu)
37 (condition-case nil (require 'timezone) (error nil))
38 (require 'std11)
39 (require 'eword-encode)
40 (require 'wl-vars)
42 (defvar x-face-add-x-face-version-header)
43 (defvar mail-reply-buffer)
44 (defvar mail-from-style)
46 (eval-when-compile
47 (require 'cl)
48 (require 'static)
49 (require 'elmo-pop3)
50 (defalias-maybe 'x-face-insert 'ignore)
51 (defalias-maybe 'x-face-insert-version-header 'ignore)
52 (defalias-maybe 'wl-init 'ignore)
53 (defalias-maybe 'wl-draft-mode 'ignore))
55 (eval-and-compile
56 (autoload 'wl-addrmgr "wl-addrmgr"))
58 (autoload 'open-ssl-stream "ssl")
60 (defvar wl-draft-buffer-message-number nil)
61 (defvar wl-draft-field-completion-list nil)
62 (defvar wl-draft-verbose-send t)
63 (defvar wl-draft-verbose-msg nil)
64 (defvar wl-draft-queue-flushing nil)
65 (defvar wl-draft-config-variables nil)
66 (defvar wl-draft-config-exec-flag t)
67 (defvar wl-draft-buffer-cur-summary-buffer nil)
68 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
69 (defvar wl-draft-sendlog-filename "sendlog")
70 (defvar wl-draft-queue-save-filename "qinfo")
71 (defvar wl-draft-config-save-filename "config")
72 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
73 (defvar wl-sent-message-via nil)
74 (defvar wl-sent-message-modified nil)
75 (defvar wl-sent-message-queued nil)
76 (defvar wl-draft-fcc-list nil)
77 (defvar wl-draft-reedit nil)
78 (defvar wl-draft-reply-buffer nil)
79 (defvar wl-draft-forward nil)
80 (defvar wl-draft-doing-mime-bcc nil)
82 (defvar wl-draft-parent-folder nil
83 "Folder name of the summary in which current draft is invoked.
84 This variable is local in each draft buffer.
85 You can refer its value in `wl-draft-config-alist'.
87 e.g.
88 \(setq wl-draft-config-alist
89 '(((string-match \".*@domain1$\" wl-draft-parent-folder)
90 (\"From\" . \"user@domain1\"))
91 ((string-match \".*@domain2$\" wl-draft-parent-folder)
92 (\"From\" . \"user@domain2\"))))")
94 (defvar wl-draft-parent-number nil)
95 (defvar wl-draft-parent-flag nil)
97 (defconst wl-draft-parent-variables
98 '(wl-draft-parent-folder
99 wl-draft-parent-number
100 wl-draft-parent-flag))
102 (defvar wl-draft-config-sub-func-alist
103 '((body . wl-draft-config-sub-body)
104 (top . wl-draft-config-sub-top)
105 (bottom . wl-draft-config-sub-bottom)
106 (header . wl-draft-config-sub-header)
107 (header-top . wl-draft-config-sub-header-top)
108 (header-bottom . wl-draft-config-sub-header)
109 (part-top . wl-draft-config-sub-part-top)
110 (part-bottom . wl-draft-config-sub-part-bottom)
111 (body-file . wl-draft-config-sub-body-file)
112 (top-file . wl-draft-config-sub-top-file)
113 (bottom-file . wl-draft-config-sub-bottom-file)
114 (header-file . wl-draft-config-sub-header-file)
115 (template . wl-draft-config-sub-template)
116 (x-face . wl-draft-config-sub-x-face)))
118 (make-variable-buffer-local 'wl-draft-buffer-message-number)
119 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
120 (make-variable-buffer-local 'wl-draft-config-variables)
121 (make-variable-buffer-local 'wl-draft-config-exec-flag)
122 (make-variable-buffer-local 'wl-sent-message-via)
123 (make-variable-buffer-local 'wl-sent-message-queued)
124 (make-variable-buffer-local 'wl-draft-fcc-list)
125 (make-variable-buffer-local 'wl-draft-reply-buffer)
126 (make-variable-buffer-local 'wl-draft-parent-folder)
127 (make-variable-buffer-local 'wl-draft-parent-number)
128 (make-variable-buffer-local 'wl-draft-parent-flag)
130 (defvar wl-draft-folder-internal nil
131 "Internal variable for caching `opened' draft folder.")
133 (defsubst wl-smtp-password-key (user mechanism server)
134 (format "SMTP:%s/%s@%s"
135 user mechanism server))
137 (defmacro wl-smtp-extension-bind (&rest body)
138 `(let* ((smtp-sasl-mechanisms
139 (if wl-smtp-authenticate-type
140 (mapcar 'upcase
141 (if (listp wl-smtp-authenticate-type)
142 wl-smtp-authenticate-type
143 (list wl-smtp-authenticate-type)))))
144 (smtp-use-sasl (and smtp-sasl-mechanisms t))
145 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
146 (smtp-open-connection-function
147 (if (eq wl-smtp-connection-type 'ssl)
148 #'open-ssl-stream
149 smtp-open-connection-function))
150 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
151 (setq smtp-sasl-user-name wl-smtp-posting-user
152 smtp-sasl-properties (when wl-smtp-authenticate-realm
153 (list 'realm
154 wl-smtp-authenticate-realm)))
155 (setq sasl-read-passphrase
156 (function
157 (lambda (prompt)
158 (elmo-get-passwd
159 (wl-smtp-password-key
160 smtp-sasl-user-name
161 (car smtp-sasl-mechanisms)
162 smtp-server)))))
163 ,@body))
165 (defun wl-draft-insert-date-field ()
166 "Insert Date field."
167 (insert "Date: " (wl-make-date-string) "\n"))
169 (defun wl-draft-insert-from-field ()
170 "Insert From field."
171 ;; Put the "From:" field in unless for some odd reason
172 ;; they put one in themselves.
173 (let (from)
174 (condition-case err
175 (setq from (wl-draft-eword-encode-address-list wl-from))
176 (error (error "Please look at `wl-from' again")))
177 (insert "From: " from "\n")))
179 (defun wl-draft-insert-x-face-field ()
180 "Insert X-Face header."
181 (interactive)
182 (if (not (file-exists-p wl-x-face-file))
183 (error "File %s does not exist" wl-x-face-file)
184 (goto-char (point-min))
185 (search-forward mail-header-separator nil t)
186 (beginning-of-line)
187 (wl-draft-insert-x-face-field-here)
188 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
190 (defun wl-draft-insert-x-face-field-here ()
191 "Insert X-Face field at point."
192 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
193 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
194 (setq x-face-string (substring x-face-string (match-end 0))))
195 (insert "X-Face: " x-face-string))
196 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
197 (insert ?\n))
198 ;; Insert X-Face-Version: field
199 (when (and (fboundp 'x-face-insert-version-header)
200 (boundp 'x-face-add-x-face-version-header)
201 x-face-add-x-face-version-header)
202 (x-face-insert-version-header)))
204 (defun wl-draft-setup ()
205 (let ((field wl-draft-fields)
207 (while field
208 (setq cl (append cl
209 (list (cons (concat (car field) " ")
210 (concat (car field) " ")))))
211 (setq field (cdr field)))
212 (setq cl
213 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
214 (concat wl-draft-mime-bcc-field-name ": "))
215 cl))
216 (setq wl-draft-field-completion-list cl)
217 (setq wl-address-complete-header-regexp
218 (wl-regexp-opt
219 (append wl-address-complete-header-list
220 (list (concat wl-draft-mime-bcc-field-name ":")))))))
222 (defun wl-draft-make-mail-followup-to (recipients)
223 (let ((rlist (wl-address-delete-user-mail-addresses recipients)))
224 (if (elmo-list-member rlist (mapcar 'downcase
225 wl-subscribed-mailing-list))
226 rlist
227 (append rlist (list (wl-address-header-extract-address
228 wl-from))))))
230 (defun wl-draft-delete-myself-from-cc (to cc)
231 (cond (wl-draft-always-delete-myself ; always-delete option
232 (wl-address-delete-user-mail-addresses cc))
233 ((elmo-list-member (append to cc) ; subscribed mailing-list
234 (mapcar 'downcase wl-subscribed-mailing-list))
235 (wl-address-delete-user-mail-addresses cc))
236 (t cc)))
238 (defsubst wl-draft-strip-subject-regexp (subject regexp)
239 "Remove REGEXP from SUBJECT string."
240 (if (string-match regexp subject)
241 (substring subject (match-end 0))
242 subject))
244 (defun wl-draft-forward-make-subject (original-subject)
245 "Generate subject string for forwarding."
246 (cond ((functionp wl-forward-subject-prefix)
247 (concat (funcall wl-forward-subject-prefix)
248 original-subject))
249 ((stringp wl-forward-subject-prefix)
250 (concat wl-forward-subject-prefix
251 (wl-draft-strip-subject-regexp
252 (or original-subject "")
253 wl-subject-forward-prefix-regexp)))
254 (t original-subject)))
256 (defun wl-draft-reply-make-subject (original-subject)
257 "Generate subject string for replying."
258 (cond ((functionp wl-reply-subject-prefix)
259 (concat (funcall wl-reply-subject-prefix)
260 original-subject))
261 ((stringp wl-reply-subject-prefix)
262 (concat wl-reply-subject-prefix
263 (wl-draft-strip-subject-regexp
264 (or original-subject "")
265 wl-subject-re-prefix-regexp)))
266 (t original-subject)))
268 (defun wl-draft-forward (original-subject summary-buf &optional number)
269 (let (references parent-folder subject)
270 (with-current-buffer summary-buf
271 (setq parent-folder (wl-summary-buffer-folder-name)))
272 (let ((decoder (mime-find-field-decoder 'Subject 'plain)))
273 (setq subject (if (and original-subject decoder)
274 (funcall decoder original-subject) original-subject)))
275 (with-current-buffer (wl-message-get-original-buffer)
276 (setq subject (wl-draft-forward-make-subject subject))
277 (setq references (nconc
278 (std11-field-bodies '("References" "In-Reply-To"))
279 (list (std11-field-body "Message-Id"))))
280 (setq references (delq nil references)
281 references (mapconcat 'identity references " ")
282 references (wl-draft-parse-msg-id-list-string references)
283 references (wl-delete-duplicates references)
284 references (when references
285 (mapconcat 'identity references "\n\t"))))
286 (and wl-draft-use-frame
287 (get-buffer-window summary-buf)
288 (select-window (get-buffer-window summary-buf)))
289 (wl-draft (list (cons 'To "")
290 (cons 'Subject subject)
291 (cons 'References references))
292 nil nil nil nil parent-folder number))
293 (goto-char (point-max))
294 (wl-draft-insert-message)
295 (mail-position-on-field "To")
296 (setq wl-draft-config-variables
297 (append wl-draft-parent-variables
298 wl-draft-config-variables))
299 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
300 (run-hooks 'wl-draft-forward-hook))
302 (defun wl-draft-self-reply-p ()
303 "Return t when From address in the current message is user's self one or not."
304 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
306 (defun wl-draft-find-reply-headers (rule-symbol)
307 (let ((rule-list (symbol-value rule-symbol))
308 condition-match-p result)
309 (setq condition-match-p
310 (lambda (condition)
311 (cond ((stringp condition)
312 (std11-field-body condition))
313 ((functionp condition)
314 (funcall condition))
315 ((consp condition)
316 (and (funcall condition-match-p (car condition))
317 (funcall condition-match-p (cdr condition))))
318 ((null condition))
320 (error "Unkown condition in `%s'" rule-symbol)))))
321 (while (and (null result) rule-list)
322 (let ((rule (car rule-list)))
323 (when (funcall condition-match-p (car rule))
324 (setq result (cdr rule)))
325 (setq rule-list (cdr rule-list))))
326 result))
328 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
329 "Create draft for replying to the message in buffer BUF.
330 Recipients are prepared along `wl-draft-reply-without-argument-list',
331 or `wl-draft-reply-with-argument-list' if WITH-ARG argument is non-nil."
332 ;;;(save-excursion
333 (let ((rule-list (if with-arg
334 'wl-draft-reply-with-argument-list
335 'wl-draft-reply-without-argument-list))
336 reply-headers
337 to mail-followup-to cc subject in-reply-to references newsgroups
338 to-alist cc-alist decoder parent-folder)
339 (when (buffer-live-p summary-buf)
340 (with-current-buffer summary-buf
341 (setq parent-folder (wl-summary-buffer-folder-name))))
342 (set-buffer (or buf mime-mother-buffer))
343 (setq reply-headers
344 (or (wl-draft-find-reply-headers rule-list)
345 (error "No match field: check your `%s'" rule-list)))
346 (let ((r-to-list (nth 0 reply-headers))
347 (r-cc-list (nth 1 reply-headers))
348 (r-ng-list (nth 2 reply-headers)))
349 (setq to (wl-concat-list
350 (nconc
351 (if (functionp r-to-list)
352 (funcall r-to-list)
353 (elmo-multiple-fields-body-list r-to-list))
354 (and (member "Followup-To" r-ng-list)
355 (string= (std11-field-body "Followup-To") "poster")
356 (progn
357 (setq r-ng-list (delete "Followup-To"
358 (copy-sequence r-ng-list)))
359 (elmo-multiple-fields-body-list '("From")))))
360 ","))
361 (setq cc (wl-concat-list
362 (if (functionp r-cc-list)
363 (funcall r-cc-list)
364 (elmo-multiple-fields-body-list r-cc-list))
365 ","))
366 (setq newsgroups (wl-concat-list
367 (if (functionp r-ng-list)
368 (funcall r-ng-list)
369 (std11-field-bodies r-ng-list))
370 ",")))
371 (setq subject (std11-field-body "Subject"))
372 (setq to (wl-parse-addresses to)
373 cc (wl-parse-addresses cc))
374 (with-temp-buffer ; to keep raw buffer unibyte.
375 (set-buffer-multibyte default-enable-multibyte-characters)
376 (setq decoder (mime-find-field-decoder 'Subject 'plain))
377 (setq subject (if (and subject decoder)
378 (funcall decoder subject) subject))
379 (setq to-alist
380 (mapcar
381 (lambda (addr)
382 (setq decoder (mime-find-field-decoder 'To 'plain))
383 (cons (nth 1 (std11-extract-address-components addr))
384 (if decoder (funcall decoder addr) addr)))
385 to))
386 (setq cc-alist
387 (mapcar
388 (lambda (addr)
389 (setq decoder (mime-find-field-decoder 'Cc 'plain))
390 (cons (nth 1 (std11-extract-address-components addr))
391 (if decoder (funcall decoder addr) addr)))
392 cc)))
393 (setq subject (wl-draft-reply-make-subject subject))
394 (setq in-reply-to (std11-field-body "Message-Id"))
395 (setq references (nconc
396 (std11-field-bodies '("References" "In-Reply-To"))
397 (list in-reply-to)))
398 (setq to (delq nil (mapcar 'car to-alist)))
399 (setq cc (delq nil (mapcar 'car cc-alist)))
400 ;; if subscribed mailing list is contained in cc or to
401 ;; and myself is contained in cc,
402 ;; delete myself from cc.
403 (setq cc (wl-draft-delete-myself-from-cc to cc))
404 (when wl-insert-mail-followup-to
405 (setq mail-followup-to
406 (wl-draft-make-mail-followup-to (append to cc)))
407 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
408 (with-temp-buffer ; to keep raw buffer unibyte.
409 (set-buffer-multibyte default-enable-multibyte-characters)
410 (setq newsgroups (wl-parse newsgroups
411 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
412 newsgroups (wl-delete-duplicates newsgroups)
413 newsgroups
414 (if newsgroups
415 (mapconcat
416 (lambda (grp)
417 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
418 (if decoder (funcall decoder grp) grp))
419 newsgroups ","))))
420 (setq to (wl-delete-duplicates to nil t))
421 (setq cc (wl-delete-duplicates
422 (append (wl-delete-duplicates cc nil t)
423 to (copy-sequence to))
424 t t))
425 (and to (setq to (mapconcat
426 '(lambda (addr)
427 (if wl-draft-reply-use-address-with-full-name
428 (or (cdr (assoc addr to-alist)) addr)
429 addr))
430 to ",\n\t")))
431 (and cc (setq cc (mapconcat
432 '(lambda (addr)
433 (if wl-draft-reply-use-address-with-full-name
434 (or (cdr (assoc addr cc-alist)) addr)
435 addr))
436 cc ",\n\t")))
437 (and mail-followup-to
438 (setq mail-followup-to
439 (mapconcat
440 '(lambda (addr)
441 (if wl-draft-reply-use-address-with-full-name
442 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
443 addr))
444 mail-followup-to ",\n\t")))
445 (and (null to) (setq to cc cc nil))
446 (setq references (delq nil references)
447 references (mapconcat 'identity references " ")
448 references (wl-draft-parse-msg-id-list-string references)
449 references (wl-delete-duplicates references)
450 references (if references
451 (mapconcat 'identity references "\n\t")))
452 (and wl-draft-use-frame
453 (get-buffer-window summary-buf)
454 (select-window (get-buffer-window summary-buf)))
455 (wl-draft (list (cons 'To to)
456 (cons 'Cc cc)
457 (cons 'Newsgroups newsgroups)
458 (cons 'Subject subject)
459 (cons 'In-Reply-To in-reply-to)
460 (cons 'References references)
461 (cons 'Mail-Followup-To mail-followup-to))
462 nil nil nil nil parent-folder number)
463 (setq wl-draft-reply-buffer buf)
464 (setq wl-draft-config-variables
465 (append wl-draft-parent-variables
466 wl-draft-config-variables))
467 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save))
468 (run-hooks 'wl-draft-reply-hook))
470 (defun wl-draft-reply-position (position)
471 (cond ((eq position 'body)
472 (wl-draft-body-goto-top))
473 ((eq position 'bottom)
474 (wl-draft-body-goto-bottom))
475 ((eq position 'top)
476 (goto-char (point-min)))
477 ((and (stringp position)
478 (std11-field-body position))
479 (progn (mail-position-on-field position)
480 (wl-draft-beginning-of-line)))
481 ((listp position)
482 (while (car position)
483 (wl-draft-reply-position (car position))
484 (setq position (cdr position))))))
486 (defun wl-draft-add-references ()
487 (wl-draft-add-in-reply-to "References"))
489 (defun wl-draft-add-in-reply-to (&optional alt-field)
490 (let* ((mes-id (save-excursion
491 (set-buffer mail-reply-buffer)
492 (std11-field-body "message-id")))
493 (field (or alt-field "In-Reply-To"))
494 (ref (std11-field-body field))
495 (ref-list nil) (st nil))
496 (when (and mes-id ref)
497 (while (string-match "<[^>]+>" ref st)
498 (setq ref-list
499 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
500 ref-list)))
501 (when (and ref-list
502 (member mes-id ref-list))
503 (setq mes-id nil)))
504 (when mes-id
505 (save-excursion
506 (when (mail-position-on-field field)
507 (forward-line)
508 (while (looking-at "^[ \t]")
509 (forward-line))
510 (setq mes-id (concat "\t" mes-id "\n")))
511 (insert mes-id))
512 t)))
514 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
515 &optional ignored-fields)
516 (interactive)
517 (save-restriction
518 (narrow-to-region (point)(point))
519 (insert
520 (string-as-multibyte
521 (with-current-buffer mail-reply-buffer
522 (when decode-it
523 (decode-mime-charset-region (point-min) (point-max)
524 wl-mime-charset))
525 (buffer-substring-no-properties
526 (point-min) (point-max)))))
527 (when ignored-fields
528 (goto-char (point-min))
529 (wl-draft-delete-fields ignored-fields))
530 (goto-char (point-max))
531 (push-mark (point) nil t)
532 (goto-char (point-min)))
533 (let ((beg (point)))
534 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
535 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
536 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
537 (run-hooks 'wl-draft-cited-hook)
538 (when (if wl-draft-add-references
539 (wl-draft-add-references)
540 (if wl-draft-add-in-reply-to
541 (wl-draft-add-in-reply-to)))
542 (wl-highlight-headers 'for-draft)) ; highlight when added References:
543 (when wl-highlight-body-too
544 (wl-highlight-body-region beg (point-max)))))
546 (defun wl-message-news-p ()
547 "If exist valid Newsgroups field, return non-nil."
548 (std11-field-body "Newsgroups"))
550 (defun wl-message-field-exists-p (field)
551 "If FIELD exist and FIELD value is not empty, return non-nil."
552 (let ((value (std11-field-body field)))
553 (and value
554 (not (string= value "")))))
556 (defun wl-message-mail-p ()
557 "If exist To, Cc or Bcc field, return non-nil."
558 (or (wl-message-field-exists-p "To")
559 (wl-message-field-exists-p "Resent-to")
560 (wl-message-field-exists-p "Cc")
561 (wl-message-field-exists-p "Bcc")
562 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
563 ;;; This may be needed..
564 ;;; (wl-message-field-exists-p "Fcc")
567 (defun wl-draft-edit-string (string)
568 (let ((cur-buf (current-buffer))
569 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
570 to subject in-reply-to cc references newsgroups mail-followup-to
571 content-type content-transfer-encoding from
572 body-beg)
573 (set-buffer tmp-buf)
574 (erase-buffer)
575 (insert string)
576 (setq to (std11-field-body "To"))
577 (setq to (and to
578 (eword-decode-string
579 (decode-mime-charset-string
581 wl-mime-charset))))
582 (setq subject (std11-field-body "Subject"))
583 (setq subject (and subject
584 (eword-decode-string
585 (decode-mime-charset-string
586 subject
587 wl-mime-charset))))
588 (setq from (std11-field-body "From")
589 from (and from
590 (eword-decode-string
591 (decode-mime-charset-string
592 from
593 wl-mime-charset))))
594 (setq in-reply-to (std11-field-body "In-Reply-To"))
595 (setq cc (std11-field-body "Cc"))
596 (setq cc (and cc
597 (eword-decode-string
598 (decode-mime-charset-string
600 wl-mime-charset))))
601 (setq references (std11-field-body "References"))
602 (setq newsgroups (std11-field-body "Newsgroups"))
603 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
604 (setq content-type (std11-field-body "Content-Type"))
605 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
606 (goto-char (point-min))
607 (or (re-search-forward "\n\n" nil t)
608 (search-forward (concat mail-header-separator "\n") nil t))
609 (unwind-protect
610 (set-buffer
611 (wl-draft (list
612 (cons 'From
613 (if (wl-address-user-mail-address-p from) from))
614 (cons 'To to)
615 (cons 'Cc cc)
616 (cons 'Subject subject)
617 (cons 'Newsgroups newsgroups)
618 (cons 'Mail-Followup-To mail-followup-to)
619 (cons 'In-Reply-To in-reply-to)
620 (cons 'References references))
621 content-type content-transfer-encoding
622 (buffer-substring (point) (point-max))
623 'edit-again))
624 (kill-buffer tmp-buf))
625 ;; Set cursor point to the top.
626 (goto-char (point-min))
627 (search-forward (concat mail-header-separator "\n") nil t)
628 (run-hooks 'wl-draft-reedit-hook)
629 (and to (mail-position-on-field "To"))))
631 (defun wl-draft-insert-current-message (dummy)
632 (interactive)
633 (let (original-buffer
634 mail-reply-buffer
635 mail-citation-hook mail-yank-hooks
636 wl-draft-add-references wl-draft-add-in-reply-to
637 wl-draft-cite-function)
638 (if (and wl-draft-buffer-cur-summary-buffer
639 (with-current-buffer wl-draft-buffer-cur-summary-buffer
640 (and wl-message-buffer
641 (with-current-buffer wl-message-buffer
642 (setq original-buffer (wl-message-get-original-buffer))
643 (not (zerop (with-current-buffer original-buffer
644 (buffer-size))))))))
645 (progn
646 (setq mail-reply-buffer original-buffer)
647 (wl-draft-yank-from-mail-reply-buffer
649 wl-ignored-forwarded-headers))
650 (when (string= (mime-make-tag "message" "rfc822")
651 (buffer-substring-no-properties (point-at-bol 0)(point-at-eol 0)))
652 (delete-region (point-at-bol 0) (1+ (point-at-eol 0))))
653 (error "No current message"))))
655 (defun wl-draft-insert-get-message (dummy)
656 (let ((fld (completing-read
657 "Folder name: "
658 (if (memq 'read-folder wl-use-folder-petname)
659 (wl-folder-get-entity-with-petname)
660 wl-folder-entity-hashtb)
661 nil nil wl-default-spec
662 'wl-read-folder-history))
663 (number (call-interactively
664 (function (lambda (num)
665 (interactive "nNumber: ")
666 num))))
667 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
668 mail-citation-hook mail-yank-hooks
669 wl-draft-cite-function)
670 (unwind-protect
671 (progn
672 (with-current-buffer mail-reply-buffer
673 (erase-buffer)
674 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
675 number
676 ;; No cache.
677 (elmo-make-fetch-strategy 'entire)))
678 (wl-draft-yank-from-mail-reply-buffer nil))
679 (kill-buffer mail-reply-buffer))))
682 ;; default body citation func
684 (defun wl-default-draft-cite ()
685 (let ((mail-yank-ignored-headers "[^:]+:")
686 (mail-yank-prefix "> ")
687 date from cite-title)
688 (save-restriction
689 (if (< (mark t) (point))
690 (exchange-point-and-mark))
691 (narrow-to-region (point)(point-max))
692 (setq date (std11-field-body "date")
693 from (std11-field-body "from")))
694 (when (or date from)
695 (insert (format "At %s,\n%s wrote:\n"
696 (or date "some time ago")
697 (if wl-default-draft-cite-decorate-author
698 (funcall wl-summary-from-function
699 (or from "you"))
700 (or from "you")))))
701 (mail-indent-citation)))
703 (defvar wl-draft-buffer nil "Draft buffer to yank content.")
704 (defun wl-draft-yank-to-draft-buffer (buffer)
705 "Yank BUFFER content to `wl-draft-buffer'."
706 (set-buffer wl-draft-buffer)
707 (let ((mail-reply-buffer buffer))
708 (wl-draft-yank-from-mail-reply-buffer nil)
709 (kill-buffer buffer)))
711 (defun wl-draft-yank-original (&optional arg)
712 "Yank original message."
713 (interactive "P")
714 (if arg
715 (let ((draft-buffer (current-buffer))
716 mail-reply-buffer)
717 (with-temp-buffer
718 (insert "\n")
719 (yank)
720 (setq mail-reply-buffer (current-buffer))
721 (with-current-buffer draft-buffer
722 (wl-draft-yank-from-mail-reply-buffer nil))))
723 (wl-draft-yank-current-message-entity)))
725 (defun wl-draft-hide (editing-buffer)
726 "Hide the editing draft buffer if possible."
727 (when (and editing-buffer
728 (buffer-live-p editing-buffer)
729 (get-buffer-window editing-buffer))
730 (select-window (get-buffer-window editing-buffer))
731 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
732 fld-buf sum-win fld-win)
733 (if (and wl-draft-use-frame
734 (> (length (visible-frame-list)) 1))
735 ;; hide draft frame
736 (delete-frame)
737 ;; hide draft window
738 (or (one-window-p)
739 (delete-window))
740 ;; stay folder window if required
741 (when wl-stay-folder-window
742 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
743 (if (setq fld-win (get-buffer-window fld-buf))
744 (select-window fld-win)
745 (if wl-draft-resume-folder-window ;; resume folder window
746 (switch-to-buffer fld-buf)))))
747 (if (buffer-live-p sum-buf)
748 (if (setq sum-win (get-buffer-window sum-buf t))
749 ;; if Summary is on the frame, select it.
750 (select-window sum-win)
751 ;; if summary is not on the frame, switch to it.
752 (if (and wl-stay-folder-window
753 (or wl-draft-resume-folder-window fld-win))
754 (wl-folder-select-buffer sum-buf)
755 (switch-to-buffer sum-buf))))))))
757 (defun wl-draft-delete (editing-buffer)
758 "Kill the editing draft buffer and delete the file corresponds to it."
759 (save-excursion
760 (when editing-buffer
761 (set-buffer editing-buffer)
762 (when wl-draft-buffer-message-number
763 (elmo-folder-delete-messages (wl-draft-get-folder)
764 (list
765 wl-draft-buffer-message-number))
766 (wl-draft-config-info-operation wl-draft-buffer-message-number
767 'delete))
768 (set-buffer-modified-p nil) ; force kill
769 (kill-buffer editing-buffer))))
771 (defun wl-draft-kill (&optional force-kill)
772 "Kill current draft buffer and quit editing."
773 (interactive "P")
774 (save-excursion
775 (when (and (or (eq major-mode 'wl-draft-mode)
776 (eq major-mode 'mail-mode))
777 (or force-kill
778 (yes-or-no-p "Kill Current Draft? ")))
779 (let ((cur-buf (current-buffer)))
780 (run-hooks 'wl-draft-kill-pre-hook)
781 (wl-draft-hide cur-buf)
782 (wl-draft-delete cur-buf)))
783 (message "")))
785 (defun wl-draft-fcc ()
786 "Add a new Fcc field, with file name completion."
787 (interactive)
788 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
789 (mail-position-on-field "to"))
790 (insert "\nFcc: "))
792 ;; Imported from message.el.
793 (defun wl-draft-elide-region (b e)
794 "Elide the text in the region.
795 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
796 text was killed."
797 (interactive "r")
798 (kill-region b e)
799 (insert wl-draft-elide-ellipsis))
801 ;; Imported from message.el.
802 (defun wl-draft-beginning-of-line (&optional n)
803 "Move point to beginning of header value or to beginning of line."
804 (interactive "p")
805 (let ((zrs 'zmacs-region-stays))
806 (when (and (interactive-p) (boundp zrs))
807 (set zrs t)))
808 (if (wl-draft-point-in-header-p)
809 (let* ((here (point))
810 (bol (progn (beginning-of-line n) (point)))
811 (eol (line-end-position))
812 (eoh (and (looking-at "[^ \t]")
813 (re-search-forward ": *" eol t))))
814 (if (and eoh (or (> here eoh) (= here bol)))
815 (goto-char eoh)
816 (goto-char bol)))
817 (beginning-of-line n)))
819 (defun wl-draft-point-in-header-p ()
820 "Return t if point is in the header."
821 (save-excursion
822 (let ((p (point)))
823 (goto-char (point-min))
824 (not (re-search-forward
825 (concat "^" (regexp-quote mail-header-separator) "\n")
826 p t)))))
828 ;; function for wl-sent-message-via
830 (defmacro wl-draft-sent-message-p (type)
831 `(eq (nth 1 (assq ,type wl-sent-message-via)) 'sent))
833 (defmacro wl-draft-set-sent-message (type result &optional server-port)
834 `(let ((element (assq ,type wl-sent-message-via)))
835 (if element
836 (unless (eq (nth 1 element) ,result)
837 (setcdr element (list ,result ,server-port))
838 (setq wl-sent-message-modified t))
839 (push (list ,type ,result ,server-port) wl-sent-message-via)
840 (setq wl-sent-message-modified t))))
842 (defun wl-draft-sent-message-results ()
843 (let ((results wl-sent-message-via)
844 unplugged-via sent-via)
845 (while results
846 (cond ((eq (nth 1 (car results)) 'unplugged)
847 (push (caar results) unplugged-via))
848 ((eq (nth 1 (car results)) 'sent)
849 (push (caar results) sent-via)))
850 (setq results (cdr results)))
851 (list unplugged-via sent-via)))
853 (defun wl-draft-write-sendlog (status proto server to id)
854 "Write send log file, if `wl-draft-sendlog' is non-nil."
855 (when wl-draft-sendlog
856 (with-temp-buffer
857 (let* ((filename (expand-file-name wl-draft-sendlog-filename
858 elmo-msgdb-directory))
859 (filesize (nth 7 (file-attributes filename)))
860 (server (if server (concat " server=" server) ""))
861 (to (if to (cond
862 ((memq proto '(fcc queue))
863 (format " folder=\"%s\"" to))
864 ((eq proto 'nntp)
865 (format " ng=<%s>" to))
867 (concat " to="
868 (mapconcat
869 'identity
870 (mapcar '(lambda(x) (format "<%s>" x)) to)
871 ","))))
872 ""))
873 (id (if id (concat " id=" id) ""))
874 (time (format-time-string "%Y/%m/%d %T")))
875 (insert (format "%s proto=%s stat=%s%s%s%s\n"
876 time proto status server to id))
877 (if (and wl-draft-sendlog-max-size filesize
878 (> filesize wl-draft-sendlog-max-size))
879 (rename-file filename (concat filename ".old") t))
880 (if (file-writable-p filename)
881 (write-region-as-binary (point-min) (point-max)
882 filename t 'no-msg)
883 (message "%s is not writable." filename))))))
885 (defun wl-draft-get-header-delimiter (&optional delete)
886 ;; If DELETE is non-nil, replace the header delimiter with a blank line
887 (let (delimline)
888 (goto-char (point-min))
889 (when (re-search-forward
890 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
891 (replace-match "")
892 (if delete
893 (forward-char -1))
894 (setq delimline (point-marker)))
895 delimline))
897 (defun wl-draft-send-mail-with-qmail ()
898 "Pass the prepared message buffer to qmail-inject.
899 Refer to the documentation for the variable `send-mail-function'
900 to find out how to use this."
901 (if (and wl-draft-qmail-send-plugged
902 (not (elmo-plugged-p)))
903 (wl-draft-set-sent-message 'mail 'unplugged)
904 ;; send the message
905 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
906 (let ((id (std11-field-body "Message-ID"))
907 (to (std11-field-body "To")))
908 (case
909 (as-binary-process
910 (apply
911 'call-process-region 1 (point-max) wl-qmail-inject-program
912 nil nil nil
913 wl-qmail-inject-args))
914 ;; qmail-inject doesn't say anything on it's stdout/stderr,
915 ;; we have to look at the retval instead
916 (0 (progn
917 (wl-draft-set-sent-message 'mail 'sent)
918 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
919 (1 (error "`qmail-inject' reported permanent failure"))
920 (111 (error "`qmail-inject' reported transient failure"))
921 ;; should never happen
922 (t (error "`qmail-inject' reported unknown failure"))))))
924 (defun wl-draft-parse-msg-id-list-string (string)
925 "Get msg-id list from STRING."
926 (let (msg-id-list)
927 (dolist (parsed-id (std11-parse-msg-ids-string string))
928 (when (eq (car parsed-id) 'msg-id)
929 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
930 msg-id-list))))
931 (nreverse msg-id-list)))
933 (defun wl-draft-eword-encode-address-list (string &optional column)
934 "Encode header field STRING as list of address, and return the result.
935 Cause an error when STRING contains invalid address.
936 Optional argument COLUMN is start-position of the field."
937 (car (eword-encode-rword-list
938 (or column eword-encode-default-start-column)
939 (eword-encode-addresses-to-rword-list
940 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
942 (defun wl-draft-std11-parse-addresses (lal)
943 (let ((ret (std11-parse-address lal)))
944 (when (and (not (and (eq (length lal) 1)
945 (eq (car (car lal)) 'spaces)))
946 (null ret))
947 (error "Error while parsing address"))
948 (if ret
949 (let ((dest (list (car ret))))
950 (setq lal (cdr ret))
951 (while (and (setq ret (std11-parse-ascii-token lal))
952 (string-equal (cdr (assq 'specials (car ret))) ",")
953 (setq ret (std11-parse-address (cdr ret)))
955 (setq dest (cons (car ret) dest))
956 (setq lal (cdr ret)))
957 (while (eq 'spaces (car (car lal)))
958 (setq lal (cdr lal)))
959 (if lal (error "Error while parsing address"))
960 (nreverse dest)))))
962 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
963 "Get mailbox list of FIELD from current buffer.
964 The buffer is expected to be narrowed to just the headers of the message.
965 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
966 from current buffer."
967 (save-excursion
968 (let ((case-fold-search t)
969 (inhibit-read-only t)
970 addresses address
971 mailbox-list beg seq has-group-list)
972 (goto-char (point-min))
973 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
974 nil t)
975 (setq beg (point))
976 (re-search-forward "^[^ \t]" nil 'move)
977 (beginning-of-line)
978 (skip-chars-backward "\n")
979 (setq seq (std11-lexical-analyze
980 (buffer-substring-no-properties beg (point))))
981 (setq addresses (wl-draft-std11-parse-addresses seq))
982 (while addresses
983 (cond ((eq (car (car addresses)) 'group)
984 (setq has-group-list t)
985 (setq mailbox-list
986 (nconc mailbox-list
987 (mapcar
988 'std11-address-string
989 (nth 2 (car addresses))))))
990 ((eq (car (car addresses)) 'mailbox)
991 (setq address (nth 1 (car addresses)))
992 (setq mailbox-list
993 (nconc mailbox-list
994 (list
995 (std11-addr-to-string
996 (if (eq (car address) 'phrase-route-addr)
997 (nth 2 address)
998 (cdr address))))))))
999 (setq addresses (cdr addresses)))
1000 (when (and remove-group-list has-group-list)
1001 (delete-region beg (point))
1002 (insert (wl-address-string-without-group-list-contents seq))))
1003 mailbox-list)))
1005 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1006 "Get address list suitable for smtp RCPT TO:<address>.
1007 Group list content is removed if `wl-draft-remove-group-list-contents' is
1008 non-nil."
1009 (let ((fields (if (and wl-draft-doing-mime-bcc
1010 wl-draft-disable-bcc-for-mime-bcc)
1011 '("to" "cc")
1012 '("to" "cc" "bcc")))
1013 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1014 (case-fold-search t)
1015 addrs recipients)
1016 (save-excursion
1017 (save-restriction
1018 (narrow-to-region header-start header-end)
1019 (goto-char (point-min))
1020 (save-excursion
1021 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1022 (setq fields resent-fields)))
1023 (while fields
1024 (setq recipients
1025 (nconc recipients
1026 (wl-draft-parse-mailbox-list
1027 (car fields)
1028 wl-draft-remove-group-list-contents)))
1029 (setq fields (cdr fields)))
1030 recipients))))
1033 ;; from Semi-gnus
1035 (defun wl-draft-send-mail-with-smtp ()
1036 "Send the prepared message buffer with SMTP."
1037 (require 'smtp)
1038 (let* ((errbuf (if mail-interactive
1039 (generate-new-buffer " smtp errors")
1041 (case-fold-search t)
1042 (default-case-fold-search t)
1043 (sender (or wl-envelope-from
1044 (wl-address-header-extract-address wl-from)))
1045 (delimline (save-excursion
1046 (goto-char (point-min))
1047 (re-search-forward
1048 (concat "^" (regexp-quote mail-header-separator)
1049 "$\\|^$") nil t)
1050 (point-marker)))
1051 (smtp-server
1052 (or wl-smtp-posting-server smtp-server "localhost"))
1053 (smtp-service (or wl-smtp-posting-port smtp-service))
1054 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1055 (id (std11-field-body "message-id"))
1056 recipients)
1057 (if (not (elmo-plugged-p smtp-server smtp-service))
1058 (wl-draft-set-sent-message 'mail 'unplugged
1059 (cons smtp-server smtp-service))
1060 (unwind-protect
1061 (save-excursion
1062 ;; Instead of `smtp-deduce-address-list'.
1063 (setq recipients (wl-draft-deduce-address-list
1064 (current-buffer) (point-min) delimline))
1065 (unless recipients (error "No recipients"))
1066 ;; Insert an extra newline if we need it to work around
1067 ;; Sun's bug that swallows newlines.
1068 (goto-char (1+ delimline))
1069 (if (eval mail-mailer-swallows-blank-line)
1070 (newline))
1071 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1072 (if mail-interactive
1073 (save-excursion
1074 (set-buffer errbuf)
1075 (erase-buffer)))
1076 (wl-draft-delete-field "bcc" delimline)
1077 (wl-draft-delete-field "resent-bcc" delimline)
1078 (let (process-connection-type)
1079 (as-binary-process
1080 (when recipients
1081 (wl-smtp-extension-bind
1082 (condition-case err
1083 (smtp-send-buffer sender recipients (current-buffer))
1084 (error
1085 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1086 recipients id)
1087 (if (and (eq (car err) 'smtp-response-error)
1088 (= (nth 1 err) 535))
1089 (elmo-remove-passwd
1090 (wl-smtp-password-key
1091 smtp-sasl-user-name
1092 (car smtp-sasl-mechanisms)
1093 smtp-server)))
1094 (signal (car err) (cdr err)))
1095 (quit
1096 (wl-draft-write-sendlog 'uncertain 'smtp smtp-server
1097 recipients id)
1098 (signal (car err) (cdr err)))))
1099 (wl-draft-set-sent-message 'mail 'sent)
1100 (wl-draft-write-sendlog
1101 'ok 'smtp smtp-server recipients id)))))
1102 (if (bufferp errbuf)
1103 (kill-buffer errbuf))))))
1105 (defun wl-draft-send-mail-with-pop-before-smtp ()
1106 "Send the prepared message buffer with POP-before-SMTP."
1107 (require 'elmo-pop3)
1108 (let ((folder
1109 (luna-make-entity
1110 'elmo-pop3-folder
1111 :user (or wl-pop-before-smtp-user
1112 elmo-pop3-default-user)
1113 :server (or wl-pop-before-smtp-server
1114 elmo-pop3-default-server)
1115 :port (or wl-pop-before-smtp-port
1116 elmo-pop3-default-port)
1117 :auth (or wl-pop-before-smtp-authenticate-type
1118 elmo-pop3-default-authenticate-type)
1119 :stream-type (elmo-get-network-stream-type
1120 (or wl-pop-before-smtp-stream-type
1121 elmo-pop3-default-stream-type))))
1122 session)
1123 (condition-case error
1124 (progn
1125 (setq session (elmo-pop3-get-session folder))
1126 (when session (elmo-network-close-session session)))
1127 (error
1128 (unless (string= (nth 1 error) "Unplugged")
1129 (signal (car error) (cdr error))))))
1130 (wl-draft-send-mail-with-smtp))
1132 (defun wl-draft-insert-required-fields (&optional force-msgid)
1133 "Insert Message-ID, Date, and From field.
1134 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1135 ;; Insert Message-Id field...
1136 (goto-char (point-min))
1137 (when (and (or force-msgid
1138 wl-insert-message-id)
1139 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1140 (insert (concat "Message-ID: "
1141 (funcall wl-message-id-function)
1142 "\n")))
1143 ;; Insert date field.
1144 (goto-char (point-min))
1145 (or (re-search-forward "^Date[ \t]*:" nil t)
1146 (wl-draft-insert-date-field))
1147 ;; Insert from field.
1148 (goto-char (point-min))
1149 (or (re-search-forward "^From[ \t]*:" nil t)
1150 (wl-draft-insert-from-field)))
1152 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1153 "Send the message in the current buffer."
1154 (save-restriction
1155 (narrow-to-region (goto-char (point-min))
1156 (if (re-search-forward
1157 (concat
1158 "^" (regexp-quote mail-header-separator) "$")
1159 nil t)
1160 (match-beginning 0)
1161 (point-max)))
1162 (wl-draft-insert-required-fields)
1163 ;; ignore any blank lines in the header
1164 (while (progn (goto-char (point-min))
1165 (re-search-forward "\n[ \t]*\n\n*" nil t))
1166 (replace-match "\n"))
1167 (goto-char (point-min))
1168 (while (re-search-forward
1169 "^[^ \t\n:]+:[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n"
1170 nil t)
1171 (when (string= "" (match-string 1))
1172 (replace-match ""))))
1173 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1174 (wl-draft-dispatch-message)
1175 (when kill-when-done
1176 ;; hide editing-buffer.
1177 (wl-draft-hide editing-buffer)
1178 ;; delete editing-buffer and its file.
1179 (wl-draft-delete editing-buffer)))
1181 (defun wl-draft-dispatch-message (&optional mes-string)
1182 "Send the message in the current buffer. Not modified the header fields."
1183 (let (delimline mime-bcc)
1184 (if (and wl-draft-verbose-send mes-string)
1185 (message "%s" mes-string))
1186 ;; get fcc folders.
1187 (setq delimline (wl-draft-get-header-delimiter t))
1188 (unless wl-draft-fcc-list
1189 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1191 (setq wl-sent-message-modified nil)
1192 (unwind-protect
1193 (progn
1194 (if (and (wl-message-mail-p)
1195 (not (wl-draft-sent-message-p 'mail)))
1196 (if (or (not (or wl-draft-force-queuing
1197 wl-draft-force-queuing-mail))
1198 (memq 'mail wl-sent-message-queued))
1199 (progn
1200 (setq mime-bcc (wl-draft-mime-bcc-field))
1201 (funcall wl-draft-send-mail-function)
1202 (when (not (zerop (length mime-bcc)))
1203 (wl-draft-do-mime-bcc mime-bcc)))
1204 (push 'mail wl-sent-message-queued)
1205 (wl-draft-set-sent-message 'mail 'unplugged)))
1206 (if (and (wl-message-news-p)
1207 (not (wl-draft-sent-message-p 'news))
1208 (not (wl-message-field-exists-p "Resent-to")))
1209 (if (or (not (or wl-draft-force-queuing
1210 wl-draft-force-queuing-news))
1211 (memq 'news wl-sent-message-queued))
1212 (funcall wl-draft-send-news-function)
1213 (push 'news wl-sent-message-queued)
1214 (wl-draft-set-sent-message 'news 'unplugged))))
1215 (let* ((status (wl-draft-sent-message-results))
1216 (unplugged-via (car status))
1217 (sent-via (nth 1 status)))
1218 ;; If one sent, process fcc folder.
1219 (if (and sent-via wl-draft-fcc-list)
1220 (progn
1221 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1222 wl-draft-fcc-list)
1223 (setq wl-draft-fcc-list nil)))
1224 (if wl-draft-use-cache
1225 (let ((id (std11-field-body "Message-ID"))
1226 (elmo-enable-disconnected-operation t))
1227 (elmo-file-cache-save (elmo-file-cache-get-path id)
1228 nil)))
1229 ;; If one unplugged, append queue.
1230 (when (and unplugged-via
1231 wl-sent-message-modified)
1232 (if wl-draft-enable-queuing
1233 (progn
1234 (wl-draft-queue-append wl-sent-message-via)
1235 (setq wl-sent-message-modified 'requeue))
1236 (error "Unplugged")))
1237 (when wl-draft-verbose-send
1238 (if (and unplugged-via sent-via);; combined message
1239 (progn
1240 (setq wl-draft-verbose-msg
1241 (format "Sending%s and Queuing%s..."
1242 sent-via unplugged-via))
1243 (message "%sdone" wl-draft-verbose-msg))
1244 (if mes-string
1245 (message "%s%s"
1246 mes-string
1247 (if sent-via "done" "failed"))))))))
1248 (not wl-sent-message-modified)) ;; return value
1250 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1251 "Force send current buffer as raw message."
1252 (interactive)
1253 (save-excursion
1254 (let (wl-interactive-send
1255 ;;; wl-draft-verbose-send
1256 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1257 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1258 mail-send-hook
1259 mail-send-actions)
1260 (wl-draft-send kill-when-done mes-string))))
1262 (defun wl-draft-clone-local-variables ()
1263 (let ((locals (buffer-local-variables))
1264 result)
1265 (while locals
1266 (when (and (consp (car locals))
1267 (car (car locals))
1268 (string-match wl-draft-clone-local-variable-regexp
1269 (symbol-name (car (car locals)))))
1270 (wl-append result (list (car (car locals)))))
1271 (setq locals (cdr locals)))
1272 result))
1274 (defcustom wl-draft-send-confirm-with-preview t
1275 "*Non-nil to invoke preview through confirmation of sending.
1276 This variable is valid when `wl-interactive-send' has non-nil value."
1277 :type 'boolean
1278 :group 'wl-draft)
1280 (defun wl-draft-send-confirm ()
1281 (unwind-protect
1282 (condition-case nil
1283 (progn
1284 (when wl-draft-send-confirm-with-preview
1285 (let (wl-draft-send-hook
1286 (pgg-decrypt-automatically nil))
1287 (wl-draft-preview-message)))
1288 (save-excursion
1289 (goto-char (point-min)) ; to show recipients in header
1290 (funcall
1291 (if (functionp wl-draft-send-confirm-type)
1292 wl-draft-send-confirm-type
1293 (lambda (prompt)
1294 (wl-y-or-n-p-with-scroll
1295 prompt
1296 (eq wl-draft-send-confirm-type 'scroll-by-SPC/BS))))
1297 "Send current draft? ")))
1298 (quit nil))
1299 (when (and wl-draft-send-confirm-with-preview
1300 (eq major-mode 'mime-view-mode))
1301 (wl-mime-quit-preview))))
1303 (defun wl-draft-send (&optional kill-when-done mes-string)
1304 "Send current draft message.
1305 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1306 (interactive)
1307 ;; Don't call this explicitly.
1308 ;; Added to 'wl-draft-send-hook (by teranisi)
1309 ;; (wl-draft-config-exec)
1310 (run-hooks 'wl-draft-send-hook)
1311 (when (or (not wl-interactive-send)
1312 (wl-draft-send-confirm))
1313 (let ((send-mail-function 'wl-draft-raw-send)
1314 (editing-buffer (current-buffer))
1315 (sending-buffer (wl-draft-generate-clone-buffer
1316 " *wl-draft-sending-buffer*"
1317 (append wl-draft-config-variables
1318 (wl-draft-clone-local-variables))))
1319 (parent-flag wl-draft-parent-flag)
1320 (parent-number wl-draft-parent-number)
1321 (parent-folder wl-draft-parent-folder)
1322 (wl-draft-verbose-msg nil)
1323 err)
1324 (unwind-protect
1325 (save-excursion
1326 (set-buffer sending-buffer)
1327 (if (and (not (wl-message-mail-p))
1328 (not (wl-message-news-p)))
1329 (error "No recipient is specified"))
1330 (expand-abbrev) ; for mail-abbrevs
1331 (let ((mime-header-encode-method-alist
1332 (append
1333 '((wl-draft-eword-encode-address-list
1334 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1335 (if (boundp 'mime-header-encode-method-alist)
1336 (symbol-value 'mime-header-encode-method-alist)))))
1337 (run-hooks 'mail-send-hook) ; translate buffer
1340 (if wl-draft-verbose-send
1341 (message "%s" (or mes-string "Sending...")))
1342 ;; Set flag before send-function because
1343 ;; there's no need to change current mailbox at this time.
1344 ;; If flag is set after send-function, the current mailbox
1345 ;; might changed by Fcc.
1346 ;; It causes a huge loss in the IMAP folder.
1347 (when (and parent-flag parent-number
1348 (not (eq (length parent-folder) 0)))
1349 (condition-case nil
1350 (wl-folder-set-persistent-mark
1351 parent-folder parent-number parent-flag)
1352 (error
1353 (message "Set mark (%s) failed" (symbol-name parent-flag)))))
1354 (funcall wl-draft-send-function editing-buffer kill-when-done)
1355 ;; Now perform actions on successful sending.
1356 (while mail-send-actions
1357 (condition-case ()
1358 (apply (car (car mail-send-actions))
1359 (cdr (car mail-send-actions)))
1360 (error))
1361 (setq mail-send-actions (cdr mail-send-actions)))
1362 (if wl-draft-verbose-send
1363 (message "%sdone"
1364 (or wl-draft-verbose-msg
1365 mes-string
1366 "Sending..."))))
1367 ;; kill sending buffer, anyway.
1368 (and (buffer-live-p sending-buffer)
1369 (kill-buffer sending-buffer))))))
1371 (defun wl-draft-mime-bcc-field ()
1372 "Return the MIME-Bcc field body. The field is deleted."
1373 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1374 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1376 (defun wl-draft-do-mime-bcc (field-body)
1377 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1378 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1379 'From))
1380 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1382 'Subject))
1383 (recipients (wl-parse-addresses field-body))
1384 (draft-buffer (current-buffer))
1385 wl-draft-use-frame)
1386 (save-window-excursion
1387 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1388 (not (zerop (length field-body))))
1389 (let ((wl-draft-doing-mime-bcc t))
1390 (dolist (recipient recipients)
1391 (wl-draft-create-buffer)
1392 (wl-draft-create-contents
1393 (append `((From . ,orig-from)
1394 (To . ,recipient)
1395 (Subject . ,(concat "A blind carbon copy ("
1396 orig-subj
1397 ")")))
1398 (wl-draft-default-headers)))
1399 (wl-draft-insert-mail-header-separator)
1400 (wl-draft-prepare-edit)
1401 (goto-char (point-max))
1402 (insert (or wl-draft-mime-bcc-body
1403 "This is a blind carbon copy.")
1404 "\n")
1405 (mime-edit-insert-tag "message" "rfc822")
1406 (insert-buffer-substring draft-buffer)
1407 (let (wl-interactive-send)
1408 (wl-draft-send 'kill-when-done))))))))
1410 (defun wl-draft-save ()
1411 "Save current draft."
1412 (interactive)
1413 (if (buffer-modified-p)
1414 (progn
1415 (message "Saving...")
1416 (let ((msg (buffer-substring-no-properties (point-min) (point-max)))
1417 next-number)
1418 (when wl-draft-buffer-message-number
1419 (elmo-folder-delete-messages (wl-draft-get-folder)
1420 (list wl-draft-buffer-message-number))
1421 (wl-draft-config-info-operation wl-draft-buffer-message-number
1422 'delete))
1423 (elmo-folder-check (wl-draft-get-folder))
1424 ;; If no header separator, insert it.
1425 (with-temp-buffer
1426 (insert msg)
1427 (goto-char (point-min))
1428 (unless (re-search-forward
1429 (concat "^" (regexp-quote mail-header-separator) "$")
1430 nil t)
1431 (goto-char (point-min))
1432 (if (re-search-forward "\n\n" nil t)
1433 (replace-match (concat "\n" mail-header-separator "\n"))
1434 (goto-char (point-max))
1435 (insert (if (eq (char-before) ?\n) "" "\n")
1436 mail-header-separator "\n")))
1437 (let ((mime-header-encode-method-alist
1438 (append
1439 '((eword-encode-unstructured-field-body
1440 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1441 (if (boundp 'mime-header-encode-method-alist)
1442 (symbol-value 'mime-header-encode-method-alist)))))
1443 (mime-edit-translate-buffer))
1444 (wl-draft-get-header-delimiter t)
1445 (setq next-number
1446 (elmo-folder-next-message-number (wl-draft-get-folder)))
1447 (elmo-folder-append-buffer (wl-draft-get-folder)))
1448 (elmo-folder-check (wl-draft-get-folder))
1449 (elmo-folder-commit (wl-draft-get-folder))
1450 (setq wl-draft-buffer-message-number next-number)
1451 (rename-buffer (format "%s/%d" wl-draft-folder next-number) t)
1452 (setq buffer-file-name (buffer-name))
1453 (set-buffer-modified-p nil)
1454 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
1455 (message "Saving...done")))
1456 (message "(No changes need to be saved)")))
1458 (defun wl-draft-mimic-kill-buffer ()
1459 "Kill the current (draft) buffer with query."
1460 (interactive)
1461 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1462 (buffer-name))))
1463 wl-draft-use-frame)
1464 (if (or (not bufname)
1465 (string-equal bufname "")
1466 (string-equal bufname (buffer-name)))
1467 (let ((bufname (current-buffer)))
1468 (when (or (not (buffer-modified-p))
1469 (yes-or-no-p
1470 (format "Buffer %s modified; kill anyway? " bufname)))
1471 (set-buffer-modified-p nil)
1472 (wl-draft-hide bufname)
1473 (kill-buffer bufname)))
1474 (kill-buffer bufname))))
1476 (defun wl-draft-save-and-exit ()
1477 "Save current draft and exit current draft mode."
1478 (interactive)
1479 (wl-draft-save)
1480 (let ((editing-buffer (current-buffer)))
1481 (wl-draft-hide editing-buffer)
1482 (kill-buffer editing-buffer)))
1484 (defun wl-draft-send-and-exit ()
1485 "Send current draft message and kill it."
1486 (interactive)
1487 (wl-draft-send t))
1489 (defun wl-draft-send-from-toolbar ()
1490 (interactive)
1491 (let ((wl-interactive-send t))
1492 (wl-draft-send-and-exit)))
1494 (defun wl-draft-delete-field (field &optional delimline replace)
1495 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1497 (defun wl-draft-delete-fields (field &optional delimline replace)
1498 (save-restriction
1499 (unless delimline
1500 (goto-char (point-min))
1501 (if (search-forward "\n\n" nil t)
1502 (setq delimline (point))
1503 (setq delimline (point-max))))
1504 (narrow-to-region (point-min) delimline)
1505 (goto-char (point-min))
1506 (let ((regexp (concat "^" field ":"))
1507 (case-fold-search t))
1508 (while (not (eobp))
1509 (if (looking-at regexp)
1510 (progn
1511 (delete-region
1512 (point)
1513 (progn
1514 (forward-line 1)
1515 (if (re-search-forward "^[^ \t]" nil t)
1516 (goto-char (match-beginning 0))
1517 (point-max))))
1518 (if replace
1519 (insert (concat field ": " replace "\n"))))
1520 (forward-line 1)
1521 (if (re-search-forward "^[^ \t]" nil t)
1522 (goto-char (match-beginning 0))
1523 (point-max)))))))
1525 (defun wl-draft-get-fcc-list (header-end)
1526 (if (and wl-draft-doing-mime-bcc
1527 wl-draft-disable-fcc-for-mime-bcc)
1528 (progn
1529 (wl-draft-delete-field "fcc")
1530 nil)
1531 (let (fcc-list
1532 (case-fold-search t))
1533 (or (markerp header-end) (error "HEADER-END must be a marker"))
1534 (save-excursion
1535 (goto-char (point-min))
1536 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1537 (save-match-data
1538 (setq fcc-list
1539 (append fcc-list
1540 (split-string
1541 (buffer-substring-no-properties
1542 (point)
1543 (progn
1544 (end-of-line)
1545 (skip-chars-backward " \t")
1546 (point)))
1547 ",[ \t]*")))
1548 (dolist (folder fcc-list)
1549 (wl-folder-confirm-existence
1550 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1551 (delete-region (match-beginning 0)
1552 (progn (forward-line 1) (point)))))
1553 fcc-list)))
1555 (defcustom wl-draft-fcc-append-read-folder-history t
1556 "Non-nil to append fcc'ed folder to `wl-read-folder-history'."
1557 :type 'boolean
1558 :group 'wl-draft)
1560 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1561 (let ((send-mail-buffer (current-buffer))
1562 (tembuf (generate-new-buffer " fcc output"))
1563 (case-fold-search t)
1564 beg end)
1565 (or (markerp header-end) (error "HEADER-END must be a marker"))
1566 (save-excursion
1567 (unless fcc-list
1568 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1569 (set-buffer tembuf)
1570 (erase-buffer)
1571 ;; insert just the headers to avoid moving the gap more than
1572 ;; necessary (the message body could be arbitrarily huge.)
1573 (insert-buffer-substring send-mail-buffer 1 header-end)
1574 (wl-draft-insert-required-fields t)
1575 (goto-char (point-max))
1576 (insert-buffer-substring send-mail-buffer header-end)
1577 (let ((id (std11-field-body "Message-ID"))
1578 (elmo-enable-disconnected-operation t))
1579 (while fcc-list
1580 (if (elmo-folder-append-buffer
1581 (wl-folder-get-elmo-folder
1582 (eword-decode-string (car fcc-list)))
1583 (and wl-fcc-force-as-read '(read)))
1584 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1585 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1586 (if (and wl-draft-fcc-append-read-folder-history
1587 (boundp 'wl-read-folder-history))
1588 (or (equal (car fcc-list) (car wl-read-folder-history))
1589 (setq wl-read-folder-history
1590 (append (list (car fcc-list)) wl-read-folder-history))))
1591 (setq fcc-list (cdr fcc-list)))))
1592 (kill-buffer tembuf)))
1594 (defun wl-draft-on-field-p ()
1595 (if (< (point)
1596 (save-excursion
1597 (goto-char (point-min))
1598 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1599 (point)))
1600 (if (bolp)
1601 (if (bobp)
1603 (save-excursion
1604 (forward-line -1)
1605 (if (or (looking-at ".*,[ \t]?$")
1606 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1607 nil t)))
1608 (let ((pos (point)))
1609 (save-excursion
1610 (beginning-of-line)
1611 (if (looking-at "^[ \t]")
1613 (if (re-search-forward ":" pos t) nil t)))))))
1615 ;;;;;;;;;;;;;;;;
1616 ;;;###autoload
1617 (defun wl-draft (&optional header-alist
1618 content-type content-transfer-encoding
1619 body edit-again
1620 parent-folder
1621 parent-number)
1622 "Write and send mail/news message with Wanderlust."
1623 (interactive)
1624 (require 'wl)
1625 (unless wl-init
1626 (wl-load-profile)
1627 (wl-folder-init)
1628 (elmo-init)
1629 (wl-plugged-init t))
1630 (let (wl-demo)
1631 (wl-init)) ; returns immediately if already initialized.
1633 (wl-start-save-drafts)
1634 (let (buffer header-alist-internal)
1635 (setq buffer (wl-draft-create-buffer parent-folder parent-number))
1636 (unless (cdr (assq 'From header-alist))
1637 (setq header-alist
1638 (append (list (cons 'From wl-from)) header-alist)))
1639 (unless (cdr (assq 'To header-alist))
1640 (let ((to))
1641 (when (setq to (and
1642 (interactive-p)
1643 ""))
1644 (if (assq 'To header-alist)
1645 (setcdr (assq 'To header-alist) to)
1646 (setq header-alist
1647 (append header-alist
1648 (list (cons 'To to))))))))
1649 (unless (cdr (assq 'Subject header-alist))
1650 (if (assq 'Subject header-alist)
1651 (setcdr (assq 'Subject header-alist) "")
1652 (setq header-alist
1653 (append header-alist (list (cons 'Subject ""))))))
1654 (setq header-alist (append header-alist
1655 (wl-draft-default-headers)
1656 wl-draft-additional-header-alist
1657 (if body (list "" (cons 'Body body)))))
1658 (wl-draft-create-contents header-alist)
1659 (if edit-again
1660 (wl-draft-decode-body
1661 content-type content-transfer-encoding))
1662 (wl-draft-insert-mail-header-separator)
1663 (wl-draft-prepare-edit)
1664 (if (interactive-p)
1665 (run-hooks 'wl-mail-setup-hook))
1666 (goto-char (point-min))
1667 (setq buffer-undo-list nil)
1668 (wl-user-agent-compose-internal) ;; user-agent
1669 (cond ((and
1670 (interactive-p)
1671 (string= (cdr (assq 'To header-alist)) ""))
1672 (mail-position-on-field "To"))
1674 (goto-char (point-max))))
1675 buffer))
1677 (defun wl-draft-create-buffer (&optional parent-folder parent-number)
1678 (let* ((draft-folder (wl-draft-get-folder))
1679 (reply-or-forward
1680 (or (eq this-command 'wl-summary-reply)
1681 (eq this-command 'wl-summary-reply-with-citation)
1682 (eq this-command 'wl-summary-forward)
1683 (eq this-command 'wl-summary-target-mark-forward)
1684 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1685 (buffer (generate-new-buffer "*draft*"))) ; Just for initial name.
1686 (set-buffer buffer)
1687 ;; switch-buffer according to draft buffer style.
1688 (if wl-draft-use-frame
1689 (switch-to-buffer-other-frame buffer)
1690 (if reply-or-forward
1691 (case wl-draft-reply-buffer-style
1692 (split
1693 (split-window-vertically)
1694 (other-window 1)
1695 (switch-to-buffer buffer))
1696 (keep
1697 (switch-to-buffer buffer))
1698 (full
1699 (delete-other-windows)
1700 (switch-to-buffer buffer))
1702 (if (functionp wl-draft-reply-buffer-style)
1703 (funcall wl-draft-reply-buffer-style buffer)
1704 (error "Invalid value for wl-draft-reply-buffer-style"))))
1705 (case wl-draft-buffer-style
1706 (split
1707 (when (eq major-mode 'wl-summary-mode)
1708 (wl-summary-toggle-disp-msg 'off))
1709 (split-window-vertically)
1710 (other-window 1)
1711 (switch-to-buffer buffer))
1712 (keep
1713 (switch-to-buffer buffer))
1714 (full
1715 (delete-other-windows)
1716 (switch-to-buffer buffer))
1717 (t (if (functionp wl-draft-buffer-style)
1718 (funcall wl-draft-buffer-style buffer)
1719 (error "Invalid value for wl-draft-buffer-style"))))))
1720 (auto-save-mode -1)
1721 (let (change-major-mode-hook)
1722 (wl-draft-mode))
1723 (set-buffer-multibyte t) ; draft buffer is always multibyte.
1724 (make-local-variable 'truncate-partial-width-windows)
1725 (setq truncate-partial-width-windows nil)
1726 (setq truncate-lines wl-draft-truncate-lines)
1727 (setq wl-sent-message-via nil)
1728 (setq wl-sent-message-queued nil)
1729 (setq wl-draft-config-exec-flag t)
1730 (setq wl-draft-parent-folder (or parent-folder ""))
1731 (setq wl-draft-parent-number parent-number)
1732 (or (eq this-command 'wl-folder-write-current-folder)
1733 (setq wl-draft-buffer-cur-summary-buffer
1734 (wl-summary-get-buffer parent-folder)))
1735 buffer))
1737 (defun wl-draft-create-contents (header-alist)
1738 "header-alist' sample
1739 '(function ;; funcall
1740 string ;; insert string
1741 (symbol . string) ;; insert symbol-value: string
1742 (symbol . function) ;; (funcall) and if it returns string,
1743 ;; insert symbol-value: string
1744 (symbol . nil) ;; do nothing
1745 nil ;; do nothing
1747 (unless (eq major-mode 'wl-draft-mode)
1748 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1749 (let ((halist header-alist)
1750 field value)
1751 (while halist
1752 (cond
1753 ;; function
1754 ((functionp (car halist)) (funcall (car halist)))
1755 ;; string
1756 ((stringp (car halist)) (insert (car halist) "\n"))
1757 ;; cons
1758 ((consp (car halist))
1759 (setq field (car (car halist)))
1760 (setq value (cdr (car halist)))
1761 (cond
1762 ((symbolp field)
1763 (cond
1764 ((eq field 'Body) ; body
1765 (insert value))
1766 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1767 ((functionp value)
1768 (let ((value-return (funcall value)))
1769 (when (stringp value-return)
1770 (insert (symbol-name field) ": " value-return "\n"))))
1771 ((not value))
1773 (debug))))
1775 ((not field))
1777 (debug))
1779 (setq halist (cdr halist)))))
1781 (defun wl-draft-prepare-edit ()
1782 (unless (eq major-mode 'wl-draft-mode)
1783 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1784 (let (change-major-mode-hook)
1785 (wl-draft-editor-mode)
1786 (static-when (boundp 'auto-save-file-name-transforms)
1787 (make-local-variable 'auto-save-file-name-transforms)
1788 (setq auto-save-file-name-transforms
1789 (cons (list (concat (regexp-quote wl-draft-folder)
1790 "/\\([0-9]+\\)")
1791 (concat (expand-file-name
1792 "auto-save-"
1793 (elmo-folder-msgdb-path
1794 (wl-draft-get-folder)))
1795 "\\1"))
1796 auto-save-file-name-transforms)))
1797 (when wl-draft-write-file-function
1798 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1799 (wl-draft-overload-functions)
1800 (wl-highlight-headers 'for-draft)
1801 (wl-draft-save)
1802 (clear-visited-file-modtime)))
1804 (defun wl-draft-decode-header ()
1805 (save-excursion
1806 (std11-narrow-to-header)
1807 (wl-draft-decode-message-in-buffer)
1808 (widen)))
1810 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1811 (let ((content-type
1812 (or content-type
1813 (std11-field-body "content-type")))
1814 (content-transfer-encoding
1815 (or content-transfer-encoding
1816 (std11-field-body "content-transfer-encoding")))
1817 delimline)
1818 (save-excursion
1819 (std11-narrow-to-header)
1820 (wl-draft-delete-field "content-type")
1821 (wl-draft-delete-field "content-transfer-encoding")
1822 (goto-char (point-max))
1823 (setq delimline (point-marker))
1824 (widen)
1825 (narrow-to-region delimline (point-max))
1826 (goto-char (point-min))
1827 (when content-type
1828 (insert "Content-type: " content-type "\n"))
1829 (when content-transfer-encoding
1830 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1831 (wl-draft-decode-message-in-buffer)
1832 (goto-char (point-min))
1833 (unless (re-search-forward "^$" (point-at-eol) t)
1834 (insert "\n"))
1835 (widen)
1836 delimline)))
1838 ;;; subroutine for wl-draft-create-contents
1839 ;;; must be used in wl-draft-mode
1840 (defun wl-draft-check-new-line ()
1841 (if (not (= (preceding-char) ?\n))
1842 (insert ?\n)))
1844 (defsubst wl-draft-trim-ccs (cc)
1845 (let ((field
1846 (if (functionp cc)
1847 (funcall cc)
1848 cc)))
1849 (if (and field
1850 (null (and wl-draft-delete-myself-from-bcc-fcc
1851 (elmo-list-member
1852 (mapcar 'wl-address-header-extract-address
1853 (append
1854 (wl-parse-addresses (std11-field-body "To"))
1855 (wl-parse-addresses (std11-field-body "Cc"))))
1856 (mapcar 'downcase wl-subscribed-mailing-list)))))
1857 field
1858 nil)))
1860 (defsubst wl-draft-default-headers ()
1861 (list
1862 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1863 (wl-address-header-extract-address
1864 wl-from)))
1865 (cons 'User-Agent wl-generate-mailer-string-function)
1866 (cons 'Reply-To mail-default-reply-to)
1867 (cons 'Bcc (function
1868 (lambda ()
1869 (wl-draft-trim-ccs
1870 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1871 (cons 'Fcc (function
1872 (lambda ()
1873 (wl-draft-trim-ccs wl-fcc))))
1874 (cons 'Organization wl-organization)
1875 (and wl-auto-insert-x-face
1876 (file-exists-p wl-x-face-file)
1877 'wl-draft-insert-x-face-field-here) ;; allow nil
1878 mail-default-headers
1879 ;; check \n at th end of line for `mail-default-headers'
1880 'wl-draft-check-new-line
1883 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1884 (save-excursion
1885 (if delimline
1886 (goto-char delimline)
1887 (goto-char (point-min))
1888 (if (search-forward "\n\n" nil t)
1889 (delete-backward-char 1)
1890 (goto-char (point-max))))
1891 (wl-draft-check-new-line)
1892 (put-text-property (point)
1893 (progn
1894 (insert mail-header-separator "\n")
1895 (1- (point)))
1896 'category 'mail-header-separator)
1897 (point)))
1899 ;;;;;;;;;;;;;;;;
1901 (defun wl-draft-elmo-nntp-send ()
1902 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1903 (elmo-nntp-default-user
1904 (or wl-nntp-posting-user elmo-nntp-default-user))
1905 (elmo-nntp-default-server
1906 (or wl-nntp-posting-server elmo-nntp-default-server))
1907 (elmo-nntp-default-port
1908 (or wl-nntp-posting-port elmo-nntp-default-port))
1909 (elmo-nntp-default-stream-type
1910 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1911 (elmo-nntp-default-function wl-nntp-posting-function)
1912 condition)
1913 (if (setq condition (cdr (elmo-string-matched-assoc
1914 (std11-field-body "Newsgroups")
1915 wl-nntp-posting-config-alist)))
1916 (if (stringp condition)
1917 (setq elmo-nntp-default-server condition)
1918 (while (car condition)
1919 (set (intern (format "elmo-nntp-default-%s"
1920 (symbol-name (caar condition))))
1921 (cdar condition))
1922 (setq condition (cdr condition)))))
1923 (unless elmo-nntp-default-function
1924 (error "wl-draft-nntp-send: posting-function is nil"))
1925 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1926 (wl-draft-set-sent-message 'news 'unplugged
1927 (cons elmo-nntp-default-server
1928 elmo-nntp-default-port))
1929 (funcall elmo-nntp-default-function
1930 elmo-nntp-default-server (current-buffer))
1931 (wl-draft-set-sent-message 'news 'sent)
1932 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1933 (std11-field-body "Newsgroups")
1934 (std11-field-body "Message-ID")))))
1936 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1937 "Generate clone of current buffer named NAME."
1938 (let ((editing-buffer (current-buffer)))
1939 (save-excursion
1940 (set-buffer (generate-new-buffer name))
1941 (erase-buffer)
1942 (wl-draft-mode)
1943 (wl-draft-editor-mode)
1944 (insert-buffer-substring editing-buffer)
1945 (message "")
1946 (while local-variables
1947 (make-local-variable (car local-variables))
1948 (set (car local-variables)
1949 (save-excursion
1950 (set-buffer editing-buffer)
1951 (symbol-value (car local-variables))))
1952 (setq local-variables (cdr local-variables)))
1953 (current-buffer))))
1955 (defun wl-draft-remove-text-plain-tag ()
1956 "Remove text/plain tag of mime-edit."
1957 (when (string= (mime-make-text-tag "plain")
1958 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1959 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1961 (defun wl-draft-reedit (number)
1962 (let ((draft-folder (wl-draft-get-folder))
1963 (wl-draft-reedit t)
1964 (num 0)
1965 buffer change-major-mode-hook body-top)
1966 (setq buffer (get-buffer-create (format "%s/%d" wl-draft-folder
1967 number)))
1968 (if wl-draft-use-frame
1969 (switch-to-buffer-other-frame buffer)
1970 (switch-to-buffer buffer))
1971 (set-buffer buffer)
1972 (elmo-message-fetch draft-folder number (elmo-make-fetch-strategy 'entire))
1973 (elmo-delete-cr-buffer)
1974 (let ((mime-edit-again-ignored-field-regexp
1975 "^\\(Content-.*\\|Mime-Version\\):"))
1976 (wl-draft-decode-message-in-buffer))
1977 (setq body-top (wl-draft-insert-mail-header-separator))
1978 (auto-save-mode -1)
1979 (wl-draft-mode)
1980 (make-local-variable 'truncate-partial-width-windows)
1981 (setq truncate-partial-width-windows nil)
1982 (setq truncate-lines wl-draft-truncate-lines)
1983 (setq wl-sent-message-via nil)
1984 (setq wl-sent-message-queued nil)
1985 (wl-draft-config-info-operation number 'load)
1986 (goto-char (point-min))
1987 (wl-draft-overload-functions)
1988 (wl-draft-editor-mode)
1989 (static-when (boundp 'auto-save-file-name-transforms)
1990 (make-local-variable 'auto-save-file-name-transforms)
1991 (setq auto-save-file-name-transforms
1992 (cons (list (concat (regexp-quote wl-draft-folder)
1993 "/\\([0-9]+\\)")
1994 (concat (expand-file-name
1995 "auto-save-"
1996 (elmo-folder-msgdb-path
1997 (wl-draft-get-folder)))
1998 "\\1"))
1999 auto-save-file-name-transforms)))
2000 (setq buffer-file-name (buffer-name)
2001 wl-draft-buffer-message-number number)
2002 (unless wl-draft-parent-folder
2003 (setq wl-draft-parent-folder ""))
2004 (when wl-draft-write-file-function
2005 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
2006 (wl-highlight-headers 'for-draft)
2007 (goto-char body-top)
2008 (run-hooks 'wl-draft-reedit-hook)
2009 (goto-char (point-max))
2010 buffer))
2012 (defmacro wl-draft-body-goto-top ()
2013 `(progn
2014 (goto-char (point-min))
2015 (if (re-search-forward mail-header-separator nil t)
2016 (forward-char 1)
2017 (goto-char (point-max)))))
2019 (defmacro wl-draft-body-goto-bottom ()
2020 `(goto-char (point-max)))
2022 (defmacro wl-draft-config-body-goto-header ()
2023 `(progn
2024 (goto-char (point-min))
2025 (if (re-search-forward mail-header-separator nil t)
2026 (beginning-of-line)
2027 (goto-char (point-max)))))
2029 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
2030 (let (content-value)
2031 (when (and content
2032 (stringp (setq content-value (eval content))))
2033 (insert content-value)
2034 (if newline (insert "\n")))))
2036 (defun wl-draft-config-sub-body (content)
2037 (wl-draft-body-goto-top)
2038 (delete-region (point) (point-max))
2039 (wl-draft-config-sub-eval-insert content))
2041 (defun wl-draft-config-sub-top (content)
2042 (wl-draft-body-goto-top)
2043 (wl-draft-config-sub-eval-insert content))
2045 (defun wl-draft-config-sub-bottom (content)
2046 (wl-draft-body-goto-bottom)
2047 (wl-draft-config-sub-eval-insert content))
2049 (defun wl-draft-config-sub-header (content)
2050 (wl-draft-config-body-goto-header)
2051 (wl-draft-config-sub-eval-insert content 'newline))
2053 (defun wl-draft-config-sub-header-top (content)
2054 (goto-char (point-min))
2055 (wl-draft-config-sub-eval-insert content 'newline))
2057 (defun wl-draft-config-sub-part-top (content)
2058 (goto-char (mime-edit-content-beginning))
2059 (wl-draft-config-sub-eval-insert content 'newline))
2061 (defun wl-draft-config-sub-part-bottom (content)
2062 (goto-char (mime-edit-content-end))
2063 (wl-draft-config-sub-eval-insert content 'newline))
2065 (defsubst wl-draft-config-sub-file (content)
2066 (let ((coding-system-for-read wl-cs-autoconv)
2067 (file (expand-file-name (eval content))))
2068 (if (file-exists-p file)
2069 (insert-file-contents file)
2070 (error "%s: no exists file" file))))
2072 (defun wl-draft-config-sub-body-file (content)
2073 (wl-draft-body-goto-top)
2074 (delete-region (point) (point-max))
2075 (wl-draft-config-sub-file content))
2077 (defun wl-draft-config-sub-top-file (content)
2078 (wl-draft-body-goto-top)
2079 (wl-draft-config-sub-file content))
2081 (defun wl-draft-config-sub-bottom-file (content)
2082 (wl-draft-body-goto-bottom)
2083 (wl-draft-config-sub-file content))
2085 (defun wl-draft-config-sub-header-file (content)
2086 (wl-draft-config-body-goto-header)
2087 (wl-draft-config-sub-file content))
2089 (defun wl-draft-config-sub-template (content)
2090 (setq wl-draft-config-variables
2091 (wl-template-insert (eval content))))
2093 (defun wl-draft-config-sub-x-face (content)
2094 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2095 (fboundp 'x-face-insert)) ; x-face.el is installed.
2096 (x-face-insert content)
2097 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2099 (defsubst wl-draft-config-sub-func (field content)
2100 (let (func)
2101 (if (setq func (assq field wl-draft-config-sub-func-alist))
2102 (let (wl-draft-config-variables)
2103 (funcall (cdr func) content)
2104 ;; for wl-draft-config-sub-template
2105 (cons t wl-draft-config-variables)))))
2107 (defsubst wl-draft-config-exec-sub (clist)
2108 (let (config local-variables)
2109 (while clist
2110 (setq config (car clist))
2111 (cond
2112 ((functionp config)
2113 (funcall config))
2114 ((consp config)
2115 (let ((field (car config))
2116 (content (cdr config))
2117 ret-val)
2118 (cond
2119 ((stringp field)
2120 (wl-draft-replace-field field (eval content) t))
2121 ((setq ret-val (wl-draft-config-sub-func field content))
2122 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2123 (wl-append local-variables (cdr ret-val))))
2124 ((boundp field) ;; variable
2125 (make-local-variable field)
2126 (set field (eval content))
2127 (wl-append local-variables (list field)))
2129 (error "%s: not variable" field)))))
2131 (error "%s: not supported type" config)))
2132 (setq clist (cdr clist)))
2133 local-variables))
2135 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2136 "Change headers in draft preparation time."
2137 (interactive)
2138 (unless wl-draft-reedit
2139 (let ((config-alist
2140 (or config-alist
2141 (and (boundp 'wl-draft-prepared-config-alist)
2142 wl-draft-prepared-config-alist) ;; For compatible.
2143 wl-draft-config-alist)))
2144 (if config-alist
2145 (wl-draft-config-exec config-alist reply-buf)))))
2147 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2148 "Change headers according to the value of `wl-draft-config-alist'.
2149 Automatically applied in draft sending time."
2150 (interactive)
2151 (let ((case-fold-search t)
2152 (alist (or config-alist wl-draft-config-alist))
2153 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2154 wl-draft-reply-buffer)))
2155 (local-variables wl-draft-config-variables)
2156 key clist found)
2157 (when (and (or (interactive-p)
2158 wl-draft-config-exec-flag)
2159 alist)
2160 (save-excursion
2161 (catch 'done
2162 (while alist
2163 (setq key (caar alist)
2164 clist (cdar alist))
2165 (cond
2166 ((eq key 'reply)
2167 (when (and
2168 reply-buf
2169 (save-excursion
2170 (set-buffer reply-buf)
2171 (save-restriction
2172 (std11-narrow-to-header)
2173 (goto-char (point-min))
2174 (re-search-forward (car clist) nil t))))
2175 (wl-draft-config-exec-sub (cdr clist))
2176 (setq found t)))
2177 ((stringp key)
2178 (when (save-restriction
2179 (std11-narrow-to-header mail-header-separator)
2180 (goto-char (point-min))
2181 (re-search-forward key nil t))
2182 (wl-append local-variables
2183 (wl-draft-config-exec-sub clist))
2184 (setq found t)))
2185 ((eval key)
2186 (wl-append local-variables
2187 (wl-draft-config-exec-sub clist))
2188 (setq found t)))
2189 (if (and found wl-draft-config-matchone)
2190 (throw 'done t))
2191 (setq alist (cdr alist)))))
2192 (if found
2193 (setq wl-draft-config-exec-flag nil))
2194 (run-hooks 'wl-draft-config-exec-hook)
2195 (put-text-property (point-min)(point-max) 'face nil)
2196 (wl-highlight-message (point-min)(point-max) t)
2197 (setq wl-draft-config-variables
2198 (elmo-uniq-list local-variables)))))
2200 (defun wl-draft-replace-field (field content &optional add)
2201 (save-excursion
2202 (save-restriction
2203 (let ((case-fold-search t)
2204 (inhibit-read-only t) ;; added by teranisi.
2205 beg)
2206 (std11-narrow-to-header mail-header-separator)
2207 (goto-char (point-min))
2208 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2209 (if content
2210 ;; replace field
2211 (progn
2212 (setq beg (point))
2213 (re-search-forward "^[^ \t]" nil 'move)
2214 (beginning-of-line)
2215 (skip-chars-backward "\n")
2216 (delete-region beg (point))
2217 (insert " " content))
2218 ;; delete field
2219 (save-excursion
2220 (beginning-of-line)
2221 (setq beg (point)))
2222 (re-search-forward "^[^ \t]" nil 'move)
2223 (beginning-of-line)
2224 (delete-region beg (point)))
2225 (when (and add content)
2226 ;; add field
2227 (goto-char (point-max))
2228 (insert (concat field ": " content "\n"))))))))
2230 (defsubst wl-draft-config-info-filename (number msgdb-dir)
2231 (expand-file-name
2232 (format "%s-%d" wl-draft-config-save-filename number)
2233 msgdb-dir))
2235 (defun wl-draft-config-info-operation (msg operation)
2236 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2237 (filename (wl-draft-config-info-filename msg msgdb-dir))
2238 element alist variable)
2239 (cond
2240 ((eq operation 'save)
2241 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2242 (while (setq variable (pop variables))
2243 (when (boundp variable)
2244 (wl-append alist
2245 (list (cons variable (eval variable))))))
2246 (elmo-object-save filename alist)))
2247 ((eq operation 'load)
2248 (setq alist (elmo-object-load filename))
2249 (while (setq element (pop alist))
2250 (set (make-local-variable (car element)) (cdr element))
2251 (wl-append wl-draft-config-variables (list (car element)))))
2252 ((eq operation 'delete)
2253 (if (file-exists-p filename)
2254 (delete-file filename))))))
2256 (defun wl-draft-queue-info-operation (msg operation
2257 &optional add-sent-message-via)
2258 (let* ((msgdb-dir (elmo-folder-msgdb-path
2259 (wl-folder-get-elmo-folder wl-queue-folder)))
2260 (filename
2261 (expand-file-name
2262 (format "%s-%d" wl-draft-queue-save-filename msg)
2263 msgdb-dir))
2264 element alist variable)
2265 (cond
2266 ((eq operation 'save)
2267 (let ((variables (elmo-uniq-list
2268 (append wl-draft-queue-save-variables
2269 wl-draft-config-variables
2270 (list 'wl-draft-fcc-list)))))
2271 (if add-sent-message-via
2272 (progn
2273 (push 'wl-sent-message-queued variables)
2274 (push 'wl-sent-message-via variables)))
2275 (while (setq variable (pop variables))
2276 (when (boundp variable)
2277 (wl-append alist
2278 (list (cons variable (eval variable))))))
2279 (elmo-object-save filename alist)))
2280 ((eq operation 'load)
2281 (setq alist (elmo-object-load filename))
2282 (while (setq element (pop alist))
2283 (set (make-local-variable (car element)) (cdr element))))
2284 ((eq operation 'get-sent-via)
2285 (setq alist (elmo-object-load filename))
2286 (cdr (assq 'wl-sent-message-via alist)))
2287 ((eq operation 'delete)
2288 (if (file-exists-p filename)
2289 (delete-file filename))))))
2291 (defun wl-draft-queue-append (wl-sent-message-via)
2292 (if wl-draft-verbose-send
2293 (message "Queuing..."))
2294 (let ((send-buffer (current-buffer))
2295 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2296 (message-id (std11-field-body "Message-ID")))
2297 (if (elmo-folder-append-buffer folder)
2298 (progn
2299 (wl-draft-queue-info-operation
2300 (car (elmo-folder-status folder))
2301 'save wl-sent-message-via)
2302 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2303 (when wl-draft-verbose-send
2304 (setq wl-draft-verbose-msg "Queuing...")
2305 (message "Queuing...done")))
2306 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2307 (error "Queuing failed"))))
2309 (defun wl-draft-queue-flush ()
2310 "Flush draft queue."
2311 (interactive)
2312 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2313 (msgs2 (progn
2314 (elmo-folder-open-internal queue-folder)
2315 (elmo-folder-list-messages queue-folder)))
2316 (i 0)
2317 (performed 0)
2318 (wl-draft-queue-flushing t)
2319 msgs failure len buffer msgid sent-via)
2320 ;; get plugged send message
2321 (while msgs2
2322 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2323 (catch 'found
2324 (while sent-via
2325 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2326 (or (not (nth 2 (car sent-via)))
2327 (elmo-plugged-p
2328 (car (nth 2 (car sent-via)))
2329 (cdr (nth 2 (car sent-via))))))
2330 (wl-append msgs (list (car msgs2)))
2331 (throw 'found t))
2332 (setq sent-via (cdr sent-via))))
2333 (setq msgs2 (cdr msgs2)))
2334 (when (> (setq len (length msgs)) 0)
2335 (if (elmo-y-or-n-p (format
2336 "%d message(s) are in the sending queue. Send now? "
2337 len)
2338 (not elmo-dop-flush-confirm) t)
2339 (progn
2340 (save-excursion
2341 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2342 (set-buffer buffer)
2343 (while msgs
2344 ;; reset buffer local variables
2345 (kill-all-local-variables)
2346 (erase-buffer)
2347 (setq i (+ 1 i)
2348 failure nil)
2349 (setq wl-sent-message-via nil)
2350 (wl-draft-queue-info-operation (car msgs) 'load)
2351 (elmo-message-fetch queue-folder
2352 (car msgs)
2353 (elmo-make-fetch-strategy 'entire))
2354 (condition-case err
2355 (setq failure (funcall
2356 wl-draft-queue-flush-send-function
2357 (format "Sending (%d/%d)..." i len)))
2358 ;;; (wl-draft-raw-send nil nil
2359 ;;; (format "Sending (%d/%d)..." i len))
2360 (error
2361 (elmo-display-error err t)
2362 (setq failure t))
2363 (quit
2364 (setq failure t)))
2365 (if (eq wl-sent-message-modified 'requeue)
2366 (progn
2367 (elmo-folder-delete-messages
2368 queue-folder (cons (car msgs) nil))
2369 (wl-draft-queue-info-operation (car msgs) 'delete))
2370 (unless failure
2371 (elmo-folder-delete-messages
2372 queue-folder (cons (car msgs) nil))
2373 (wl-draft-queue-info-operation (car msgs) 'delete)
2374 (setq performed (+ 1 performed))))
2375 (setq msgs (cdr msgs)))
2376 (kill-buffer buffer)
2377 (message "%d message(s) are sent." performed)))
2378 (message "%d message(s) are remained to be sent." len))
2379 (elmo-folder-close queue-folder)
2380 len)))
2382 (defun wl-jump-to-draft-buffer (&optional arg)
2383 "Jump to the draft if exists."
2384 (interactive "P")
2385 (if arg
2386 (wl-jump-to-draft-folder)
2387 (let ((draft-bufs (wl-collect-draft))
2388 buf)
2389 (cond
2390 ((null draft-bufs)
2391 (message "No draft buffer exist."))
2393 (setq draft-bufs
2394 (sort (mapcar 'buffer-name draft-bufs)
2395 (function (lambda (a b)
2396 (not (string< a b))))))
2397 (if (setq buf (cdr (member (buffer-name)
2398 draft-bufs)))
2399 (setq buf (car buf))
2400 (setq buf (car draft-bufs)))
2401 (switch-to-buffer buf))))))
2403 (defun wl-jump-to-draft-folder ()
2404 (let ((msgs (reverse (elmo-folder-list-messages (wl-draft-get-folder))))
2405 (mybuf (buffer-name))
2406 msg buf)
2407 (if (not msgs)
2408 (message "No draft message exist.")
2409 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2410 (setq msg (cadr (memq
2411 (string-to-number (substring mybuf (match-end 0)))
2412 msgs))))
2413 (or msg (setq msg (car msgs)))
2414 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2415 (switch-to-buffer buf)
2416 (wl-draft-reedit msg)))))
2418 (defun wl-draft-highlight-and-recenter (&optional n)
2419 (interactive "P")
2420 (when wl-highlight-body-too
2421 (let ((modified (buffer-modified-p)))
2422 (unwind-protect
2423 (progn
2424 (put-text-property (point-min) (point-max) 'face nil)
2425 (wl-highlight-message (point-min) (point-max) t))
2426 (set-buffer-modified-p modified))))
2427 (static-when (featurep 'xemacs)
2428 ;; Cope with one of many XEmacs bugs that `recenter' takes
2429 ;; a long time if there are a lot of invisible text lines.
2430 (redraw-frame))
2431 (recenter n))
2433 ;; insert element from history
2434 (defvar wl-draft-current-history-position nil)
2435 (defvar wl-draft-history-backup-word "")
2437 (defun wl-draft-previous-history-element (n)
2438 (interactive "p")
2439 (let (bol history beg end prev new)
2440 (when (and (not (wl-draft-on-field-p))
2441 (< (point)
2442 (save-excursion
2443 (goto-char (point-min))
2444 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
2445 (point)))
2446 (save-excursion
2447 (beginning-of-line)
2448 (while (and (looking-at "^[ \t]")
2449 (not (= (point) (point-min))))
2450 (forward-line -1))
2451 (cond
2452 ((looking-at wl-folder-complete-header-regexp)
2453 (and (boundp 'wl-read-folder-history)
2454 (setq history wl-read-folder-history)))
2455 ;; ((looking-at wl-address-complete-header-regexp)
2456 ;; (setq history .....))
2458 nil)))
2459 (eolp))
2460 (setq bol (save-excursion (beginning-of-line) (point)))
2461 (cond ((and (or (eq last-command 'wl-draft-previous-history-element)
2462 (eq last-command 'wl-draft-next-history-element))
2463 wl-draft-current-history-position)
2464 (setq end (point))
2465 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2466 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2467 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t))
2468 (setq prev (match-string 1))
2469 (goto-char (match-beginning 1))
2470 (setq beg (point))
2471 (if (cond ((< n 0)
2472 (>= (+ n wl-draft-current-history-position) 0))
2473 ((> n 0)
2474 (<= (+ n wl-draft-current-history-position)
2475 (length history))))
2476 (progn
2477 (setq wl-draft-current-history-position
2478 (+ n wl-draft-current-history-position))
2479 (setq new
2480 (nth wl-draft-current-history-position
2481 (append (list wl-draft-history-backup-word)
2482 history)))
2483 (delete-region beg end)
2484 (insert new))
2485 (goto-char end)
2486 (cond ((< n 0)
2487 (message "End of history; no next item"))
2488 ((> n 0)
2489 (message "Beginning of history; no preceding item")))))
2490 ((and (> n 0)
2491 (save-excursion
2492 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2493 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2494 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t)))
2495 (car history))
2496 (setq wl-draft-current-history-position 1)
2497 (setq wl-draft-history-backup-word (match-string 1))
2498 (delete-region (match-beginning 1) (match-end 1))
2499 (insert (car history)))
2501 (setq wl-draft-current-history-position nil))))))
2503 (defun wl-draft-next-history-element (n)
2504 (interactive "p")
2505 (wl-draft-previous-history-element (- n)))
2507 ;;;; user-agent support by Sen Nagata
2509 ;; this appears to be necessarily global...
2510 (defvar wl-user-agent-compose-p nil)
2511 (defvar wl-user-agent-headers-and-body-alist nil)
2513 ;; this should be a generic function for mail-mode -- i wish there was
2514 ;; something like it in sendmail.el
2515 (defun wl-user-agent-insert-header (header-name header-value)
2516 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2517 ;; it seems like overriding existing headers is acceptable -- should
2518 ;; we provide an option?
2520 ;; plan was: unfold header (might be folded), remove existing value, insert
2521 ;; new value
2522 ;; wl doesn't seem to fold header lines yet anyway :-)
2524 (let ((kill-whole-line t)
2525 end-of-line)
2526 (mail-position-on-field (capitalize header-name))
2527 (setq end-of-line (point))
2528 (beginning-of-line)
2529 (re-search-forward ":" end-of-line)
2530 (insert (concat " " header-value "\n"))
2531 (kill-line)))
2533 ;; this should be a generic function for mail-mode -- i wish there was
2534 ;; something like it in sendmail.el
2536 ;; ** haven't dealt w/ case where the body is already set **
2537 (defun wl-user-agent-insert-body (body-text)
2538 "Insert a body of text, BODY-TEXT, into a message."
2539 ;; code defensively... :-P
2540 (goto-char (point-min))
2541 (search-forward mail-header-separator)
2542 (forward-line 1)
2543 (insert body-text))
2545 ;;;###autoload
2546 (defun wl-user-agent-compose (&optional to subject other-headers continue
2547 switch-function yank-action
2548 send-actions)
2549 "Support the `compose-mail' interface for wl.
2550 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2551 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2552 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2554 (unless (featurep 'wl)
2555 (require 'wl))
2556 (or switch-function
2557 (setq switch-function 'keep))
2558 ;; protect these -- to and subject get bound at some point, so it looks
2559 ;; to be necessary to protect the values used w/in
2560 (let ((wl-user-agent-headers-and-body-alist other-headers)
2561 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2562 (wl-draft-buffer-style switch-function))
2563 (if to
2564 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2565 'ignore-case)
2566 (setcdr
2567 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2568 'ignore-case)
2570 (setq wl-user-agent-headers-and-body-alist
2571 (cons (cons "to" to)
2572 wl-user-agent-headers-and-body-alist))))
2573 (if subject
2574 (if (wl-string-match-assoc "subject"
2575 wl-user-agent-headers-and-body-alist
2576 'ignore-case)
2577 (setcdr
2578 (wl-string-match-assoc "subject"
2579 wl-user-agent-headers-and-body-alist
2580 'ignore-case)
2581 subject)
2582 (setq wl-user-agent-headers-and-body-alist
2583 (cons (cons "subject" subject)
2584 wl-user-agent-headers-and-body-alist))))
2585 ;; i think this is what we want to use...
2586 (unwind-protect
2587 (progn
2588 ;; tell the hook-function to do its stuff
2589 (setq wl-user-agent-compose-p t)
2590 ;; because to get the hooks working, wl-draft has to think it has
2591 ;; been called interactively
2592 (call-interactively 'wl-draft))
2593 (setq wl-user-agent-compose-p nil))))
2595 (defun wl-user-agent-compose-internal ()
2596 "Manipulate headers and/or a body of a draft message."
2597 ;; being called from wl-user-agent-compose?
2598 (if wl-user-agent-compose-p
2599 (progn
2600 ;; insert headers
2601 (let ((headers wl-user-agent-headers-and-body-alist)
2602 (case-fold-search t))
2603 (while headers
2604 ;; skip body
2605 (if (not (string-match "^body$" (car (car headers))))
2606 (wl-user-agent-insert-header
2607 (car (car headers)) (cdr (car headers)))
2609 (setq headers (cdr headers))))
2610 ;; highlight headers (from wl-draft in wl-draft.el)
2611 (wl-highlight-headers 'for-draft)
2612 ;; insert body
2613 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2614 'ignore-case)
2615 (wl-user-agent-insert-body
2616 (cdr (wl-string-match-assoc
2617 "body"
2618 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2621 (defun wl-draft-setup-parent-flag (flag)
2622 "Setup a FLAG for parent message."
2623 (when (and (> (length wl-draft-parent-folder) 0)
2624 wl-draft-parent-number)
2625 (setq wl-draft-parent-flag flag)
2626 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)))
2628 (defun wl-draft-buffer-change-number (old-number new-number)
2629 (when (eq wl-draft-buffer-message-number old-number)
2630 (setq wl-draft-buffer-message-number new-number)
2631 (rename-buffer (format "%s/%d" wl-draft-folder new-number) t)
2632 (setq buffer-file-name (buffer-name))
2633 (set-buffer-modified-p nil)))
2635 (defun wl-draft-rename-saved-config (old-number new-number)
2636 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2637 (old-name (wl-draft-config-info-filename old-number msgdb-dir))
2638 (new-name (wl-draft-config-info-filename new-number msgdb-dir)))
2639 (when (file-exists-p old-name)
2640 (rename-file old-name new-name 'ok-if-already-exists))))
2642 (require 'product)
2643 (product-provide (provide 'wl-draft) (require 'wl-version))
2645 ;;; wl-draft.el ends here