Debian Codegraph @

mew-draft.el

raw
1 ;;; mew-draft.el --- Draft mode for Mew
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Oct 2, 1996
5
6 ;;; Code:
7
8 (require 'mew)
9 (eval-when-compile
10 (require 'mew-env0))
11
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 ;;;
14 ;;; Draft info
15 ;;;
16
17 (defvar mew-tinfo-list
18 '("header-keymap" "attach-keymap" "case" "encrypted-p" "privacy-err"
19 "encode-err" "privacy-type" "hdr-file" "field-del" "other-frame"
20 "preserved-header" "src-folder" "flowed" "use-flowed"))
21
22 (mew-blinfo-defun 'mew-tinfo mew-tinfo-list)
23
24
25 (defvar mew-draft-mode-syntax-table nil
26 "*Syntax table used while in Draft mode.")
27
28 (unless mew-draft-mode-syntax-table
29 (setq mew-draft-mode-syntax-table (make-syntax-table text-mode-syntax-table))
30 (modify-syntax-entry ?% "." mew-draft-mode-syntax-table))
31
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;;;
34 ;;; Draft mode
35 ;;;
36
37 (defun mew-draft-set-local-variables ()
38 (auto-save-mode mew-draft-mode-auto-save)
39 (make-local-variable 'completion-ignore-case)
40 (make-local-variable 'paragraph-start)
41 (setq paragraph-start (concat mew-eoh "\\|[ \t]*$\\|" page-delimiter))
42 (make-local-variable 'paragraph-separate)
43 (setq paragraph-separate paragraph-start)
44 (make-local-variable 'mail-header-separator)
45 (setq mail-header-separator mew-header-separator)
46 (make-local-variable 'comment-start)
47 (setq comment-start mew-comment-start)
48 (make-local-variable 'comment-start-skip)
49 (setq comment-start-skip mew-comment-start-skip)
50 (add-hook 'after-change-functions 'mew-draft-dynamic-highlight nil 'local)
51 (add-hook 'write-file-functions 'mew-encode-make-backup nil 'local)
52 (make-local-variable 'after-save-hook)
53 (when mew-require-final-newline
54 (make-local-variable 'require-final-newline)
55 (setq require-final-newline t))
56 (when (featurep 'dnd)
57 (make-local-variable 'dnd-protocol-alist)
58 (setq dnd-protocol-alist
59 (append '(("^file:///" . mew-draft-dnd-handle-local-file)
60 ("^file://" . mew-draft-dnd-handle-file)
61 ("^file:" . mew-draft-dnd-handle-local-file))
62 dnd-protocol-alist))))
63
64 (defun mew-draft-mode (&optional encrypted)
65 "A major mode for composing a MIME message.
66
67 \\{mew-draft-mode-map}"
68 (interactive)
69 (setq major-mode 'mew-draft-mode)
70 (setq mode-line-buffer-identification (mew-mode-line-id))
71 (mew-draft-set-local-variables)
72 (use-local-map mew-draft-mode-map)
73 (set-syntax-table mew-draft-mode-syntax-table)
74 (let ((default-directory (expand-file-name mew-home)))
75 (unless (file-exists-p default-directory)
76 (mkdir default-directory))
77 (mew-draft-setup-decoration)
78 (mew-ainfo-set-icon (file-name-nondirectory (buffer-file-name)))
79 (mew-tinfo-set-encrypted-p encrypted)
80 (mew-tinfo-set-privacy-err nil)
81 (mew-tinfo-set-privacy-type nil)
82 (mew-tinfo-set-use-flowed (mew-use-format-flowed (mew-tinfo-get-case)))
83 (mew-draft-mode-name) ;; must be after (mew-tinfo-set-encrypted-p encrypted)
84 (mew-run-mode-hooks 'text-mode-hook 'mew-draft-mode-hook)
85 ;; auto-fill-function is set by mew-draft-mode-hook
86 (when auto-fill-function
87 (make-local-variable 'auto-fill-function)
88 (setq auto-fill-function 'mew-draft-auto-fill))
89 (setq buffer-undo-list nil)))
90
91 (defun mew-draft-mode-name (&optional header)
92 (let ((case (mew-tinfo-get-case))
93 pcdb sub)
94 (cond
95 ((or (mew-tinfo-get-privacy-type) (mew-tinfo-get-privacy-err))
96 ;; If privacy err, don't display mew-protect-privacy-always-type etc.
97 (setq pcdb (mew-pcdb-by-service (mew-tinfo-get-privacy-type)))
98 (setq sub (mew-pcdb-mark pcdb)))
99 ((and (mew-tinfo-get-encrypted-p) (mew-protect-privacy-encrypted case))
100 (setq pcdb (mew-pcdb-by-service (mew-protect-privacy-encrypted-type case)))
101 (setq sub (mew-pcdb-mark pcdb)))
102 ((mew-protect-privacy-always case)
103 (setq pcdb (mew-pcdb-by-service (mew-protect-privacy-always-type case)))
104 (setq sub (mew-pcdb-mark pcdb))))
105 (setq mode-name (if header mew-mode-name-header mew-mode-name-draft))
106 (if sub (setq mode-name (concat mode-name " " sub)))
107 (unless (mew-case-default-p (mew-tinfo-get-case))
108 (setq mode-name (concat mode-name " " (mew-tinfo-get-case))))
109 (if (mew-tinfo-get-use-flowed)
110 (setq mode-name (concat mode-name " F")))
111 (force-mode-line-update)))
112
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114 ;;;
115 ;;; Draft subfunctions
116 ;;;
117
118 (defun mew-draft-dynamic-highlight (_beg _end _len)
119 (when (mew-in-header-p)
120 (save-match-data
121 (mew-highlight-header)
122 (when (mew-draft-p)
123 (mew-draft-header-keymap)))))
124
125 (defun mew-draft-auto-fill ()
126 (let ((ret1 (do-auto-fill)) ret2)
127 (when (mew-in-header-p)
128 (save-excursion
129 (beginning-of-line)
130 (while (not (or (looking-at "[^ \t\n]+:\\|[ \t]") (bobp)))
131 (setq ret2 t)
132 (insert "\t")
133 (forward-line -1)
134 (beginning-of-line))))
135 (or ret1 ret2))) ;; if modifies, return t.
136
137 (defun mew-draft-find-and-switch (draft-path &optional switch-func)
138 ;; switch-func = nil :: switch-to-buffer
139 ;; switch-func = t :: switch-to-buffer-other-window
140 (let* ((display-buffer-alist nil)
141 (same-window-buffer-names nil)
142 (same-window-regexps nil)
143 (draftname (mew-path-to-folder draft-path)))
144 (when (get-buffer draftname)
145 (with-current-buffer draftname
146 (clear-visited-file-modtime)
147 (set-buffer-modified-p nil) ;; just in case
148 (mew-delete-file buffer-auto-save-file-name)
149 (mew-remove-buffer draftname)))
150 (cond
151 (mew-use-other-frame-for-draft
152 (setq switch-func 'switch-to-buffer-other-frame))
153 ((eq switch-func nil)
154 (setq switch-func 'switch-to-buffer))
155 ((eq switch-func t)
156 (setq switch-func 'switch-to-buffer-other-window)))
157 (mew-frwlet mew-cs-m17n mew-cs-dummy
158 (funcall switch-func (mew-find-file-noselect draft-path)))
159 ;; draft buffer
160 (mew-set-buffer-cs mew-cs-m17n)
161 ;; copy config, first
162 (mew-tinfo-set-case mew-case)
163 (when mew-use-other-frame-for-draft
164 (mew-tinfo-set-other-frame t)
165 ;; to ensure to cite a message from summary frame.
166 (mew-remove-buffer (mew-buffer-message)))
167 (rename-buffer draftname)))
168
169 (defun mew-draft-to-attach (draft)
170 "Converting draft to attach. E.g. +draft/1 -> +attach/1"
171 (mew-concat-folder mew-attach-folder (file-name-nondirectory draft)))
172
173 (defun mew-attachdir (&optional draft)
174 (mew-expand-folder (mew-draft-to-attach (or draft (buffer-name)))))
175
176 (defun mew-draft-header-insert-alist (halist)
177 "Insert field-body: and field-value. Return the value of
178 the Body: field."
179 (let ((case-fold-search t)
180 key val ret)
181 (dolist (ent halist)
182 (setq key (mew-alist-get-key ent))
183 (setq val (mew-alist-get-value ent))
184 (unless (string-match ":$" key)
185 (setq key (concat key ":")))
186 (if (string-match mew-body: key)
187 (setq ret val)
188 (mew-draft-header-insert key val)))
189 ret))
190
191 (defun mew-insert-address-list (field adrs del force-insert)
192 (let ((cnt 0) (beg (point)) med)
193 (dolist (adr adrs)
194 (unless (mew-is-my-address del adr)
195 (if (= cnt 0)
196 (insert adr)
197 (insert ", " adr))
198 (setq del (cons (concat "^" (regexp-quote adr) "$") del))
199 (setq cnt (1+ cnt))))
200 (when (or force-insert (> cnt 0))
201 (beginning-of-line)
202 (insert field " ")
203 (setq med (point))
204 (end-of-line)
205 (insert "\n")
206 (mew-header-fold-region beg (point) med 'use-tab))
207 del))
208
209 (defun mew-insert-address-list2 (field adrs)
210 (when adrs
211 (let ((beg (point)) med)
212 (insert field " ")
213 (setq med (point))
214 (insert (car adrs))
215 (setq adrs (cdr adrs))
216 (dolist (adr adrs)
217 (insert ", " adr))
218 (insert "\n")
219 (mew-header-fold-region beg (point) med 'use-tab))))
220
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 ;;;
223 ;;; Draft header
224 ;;;
225
226 (defun mew-draft-header (&optional subject nl to cc newsgroups in-reply-to references other-headers fromme)
227 ;; to -- string or list
228 ;; cc -- string or list
229 ;; nl -- one empty line under "----", which is necessary if
230 ;; attachment is prepared
231 (let ((del (unless fromme mew-regex-my-address-list)) ;; deleting list
232 case body)
233 (goto-char (point-min))
234 (if newsgroups
235 (cond
236 ((stringp newsgroups)
237 (mew-draft-header-insert mew-newsgroups: newsgroups))
238 ((listp newsgroups)
239 (mew-insert-address-list2 mew-newsgroups: newsgroups)))
240 ;; Insert To: first.
241 ;; All addresses inserted on To: are appended to del.
242 (cond
243 ((null to) (mew-draft-header-insert mew-to: ""))
244 ((stringp to) ;; To: specified from the mini-buffer.
245 ;; do not check to is mine. Cc: is also string
246 ;; We believe that user never specifies the same address of To: to Cc:.
247 (mew-draft-header-insert mew-to: to))
248 ;; To: collected by reply
249 ((listp to)
250 (setq del (mew-insert-address-list mew-to: to del t))))
251 (cond
252 ((null cc) ()) ;; do nothing
253 ((stringp cc) ;; Cc: specified from the mini-buffer.
254 (mew-draft-header-insert mew-cc: cc))
255 ((listp cc) ;; Cc: collected by reply.
256 (mew-insert-address-list mew-cc: cc del nil))))
257 (if mew-case-guess-when-prepared
258 (mew-draft-set-case-by-guess))
259 (setq case (mew-tinfo-get-case))
260 (unless newsgroups
261 (mew-draft-header-insert mew-cc: (mew-cc case)))
262 (mew-draft-header-insert mew-subj: (or subject ""))
263 (mew-draft-header-insert mew-from: (mew-from case))
264 (mew-draft-header-insert mew-fcc: (mew-fcc case))
265 (unless newsgroups
266 (mew-draft-header-insert mew-bcc: (mew-bcc case))
267 (mew-draft-header-insert mew-dcc: (mew-dcc case)))
268 (mew-draft-header-insert mew-reply-to: (mew-reply-to case))
269 (unless newsgroups
270 (mew-draft-header-insert mew-in-reply-to: in-reply-to))
271 (mew-draft-header-insert mew-references: references)
272 (mew-draft-header-insert-xface)
273 (mew-draft-header-insert mew-organization: (mew-organization case))
274 (setq body (mew-draft-header-insert-alist other-headers))
275 ;; Deleting fields defined in mew-header-alist to replace them.
276 (mew-header-delete-lines (mapcar 'mew-alist-get-key (mew-header-alist case)))
277 (mew-header-goto-end)
278 (mew-draft-header-insert-alist (mew-header-alist case))
279 ;; X-Mailer: must be the last
280 (if (mew-use-x-mailer case)
281 (mew-draft-header-insert mew-x-mailer: mew-x-mailer))
282 ;; (mew-header-set "\n") is enough. But highlighting delayed.
283 (mew-header-prepared)
284 ;; on the body
285 (if nl (insert "\n"))
286 (if body (save-excursion (insert body)))
287 ;; move the cursor after "To: "
288 (goto-char (point-min))
289 (search-forward ": " nil t)))
290
291 (defun mew-draft-header-insert-xface ()
292 (if (and mew-x-face-file
293 (file-exists-p (expand-file-name mew-x-face-file)))
294 (let (xface)
295 (with-temp-buffer
296 (mew-insert-file-contents (expand-file-name mew-x-face-file))
297 (setq xface (mew-buffer-substring (point-min)
298 (max (buffer-size) 1))))
299 (mew-draft-header-insert mew-x-face: xface))))
300
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302 ;;;
303 ;;; Citation
304 ;;;
305
306 (defun mew-draft-auto-set-input-method ()
307 (if (and (fboundp 'activate-input-method)
308 mew-charset-input-method-alist)
309 (let* ((charset (mew-charset-guess-region
310 (mew-header-end) (or (mew-attach-begin) (point-max))))
311 (method (mew-charset-to-input-method charset)))
312 (when (stringp method)
313 (activate-input-method method)
314 (message "Set input method to %s" method)))))
315
316 (defun mew-draft-yank (&optional arg force)
317 "Copy and paste a part of message from Message mode WITHOUT
318 citation prefix and label.
319 1. Roughly speaking, it copies the body in Message mode. For example,
320 if text/plain is displayed, the entire Message mode is copied.
321 If message/rfc822 is displayed, the body without the header is copied.
322 2. If called with '\\[universal-argument]', the header is also copied if exists.
323 3. If an Emacs mark exists, the target is the region between the mark and
324 the cursor."
325 ;; MUST take care of C-x C-x
326 ;; MUST be able to cancel by C-x u
327 (interactive "P")
328 (if (and (not force) (or (mew-in-header-p) (mew-in-attach-p)))
329 (message "Cannot cite a message here")
330 (let (cite beg end)
331 (save-excursion
332 (cond
333 ((get-buffer (mew-buffer-message))
334 (set-buffer (mew-buffer-message)))
335 ((get-buffer mew-message-last-buffer)
336 (set-buffer mew-message-last-buffer)))
337 (set-buffer (mew-buffer-message))
338 (save-restriction
339 (widen)
340 (let ((mark-active t))
341 (cond
342 (arg
343 (setq beg (point-min) end (point-max)))
344 ((and (not mew-cite-ignore-region)
345 (mew-mark)
346 (/= (point) (mew-mark))
347 (not (and mew-cite-ignore-mouse-region
348 (mew-mouse-region-p))))
349 (setq beg (region-beginning) end (region-end)))
350 ((mew-msghdr-p)
351 ;; header exists in Message mode
352 (mew-header-goto-body)
353 (setq beg (point) end (point-max)))
354 (t
355 (setq beg (point-min) end (point-max)))))
356 (setq cite (mew-buffer-substring beg end))))
357 (mew-push-mark)
358 (insert cite)
359 (mew-draft-auto-set-input-method))))
360
361 (defvar mew-message-citation-buffer nil
362 "This value is used by mew-gnus.el to specify a buffer from where
363 you can cite.")
364
365 (defvar mew-message-citation-frame-id nil)
366
367 (defun mew-draft-cite (&optional arg force)
368 "Copy and paste a part of message from Message mode with
369 citation prefix and label.
370 1. Roughly speaking, it copies the body in Message mode. For example,
371 if text/plain is displayed, the entire Message mode is copied.
372 If message/rfc822 is displayed, the body without the header is copied.
373 2. If called with '\\[universal-argument]', the header is also copied if exists.
374 3. If an Emacs mark exists, the target is the region between the mark and
375 the cursor."
376 ;; MUST take care of C-x C-x
377 ;; MUST be able to cancel by C-x u
378 (interactive "P")
379 (if (and (not force) (or (mew-in-header-p) (mew-in-attach-p)))
380 (message "Cannot cite a message here")
381 (let* ((nonmewbuf mew-message-citation-buffer) ;; may be buffer local
382 (fid (or mew-message-citation-frame-id (mew-frame-id)))
383 (fld (mew-current-get-fld fid))
384 (msg (mew-current-get-msg fid))
385 (msg-buf (mew-buffer-message))
386 cite beg end tbuf irt-msgid)
387 (unless (get-buffer msg-buf)
388 (setq msg-buf mew-message-last-buffer))
389 (save-excursion
390 ;;
391 ;; extract the body without header
392 ;;
393 (setq tbuf (or nonmewbuf msg-buf))
394 (if (get-buffer tbuf)
395 (set-buffer tbuf)
396 (error "No buffer to be cited"))
397 (save-restriction
398 ;; first prepare "cite"
399 (widen)
400 (let ((mark-active t))
401 (cond
402 ;; arg will be effect in mew-cite-original
403 ((and (not mew-cite-ignore-region)
404 (mew-mark)
405 (/= (point) (mew-mark))
406 (not (and mew-cite-ignore-mouse-region
407 (mew-mouse-region-p))))
408 (setq beg (region-beginning) end (region-end)))
409 ((mew-msghdr-p)
410 ;; header exists in Message mode. Skip the header
411 ;; because we will concatenate it to cite later.
412 (mew-header-goto-body)
413 (setq beg (point) end (point-max)))
414 (t
415 (setq beg (point-min) end (point-max)))))
416 (setq cite (mew-buffer-substring beg end)))
417 ;;
418 ;; concat the header
419 ;;
420 (setq tbuf (or nonmewbuf
421 (save-excursion
422 (when (get-buffer msg-buf)
423 (set-buffer msg-buf)
424 (if (mew-msghdr-p) (current-buffer))))
425 ;; header exists only in cache if multipart
426 (mew-cache-hit fld msg)))
427 (if (get-buffer tbuf)
428 (set-buffer tbuf)
429 (error "No buffer to be cited"))
430 (save-restriction
431 (widen)
432 (mew-header-goto-end)
433 (setq cite (concat (mew-buffer-substring (point-min) (point))
434 "\n" cite))
435 (setq irt-msgid (mew-idstr-get-first-id
436 (mew-header-get-value mew-message-id:)))))
437 ;;
438 ;; Draft mode, insert the header and the body.
439 ;;
440
441 ;; Append message-id to In-Reply-To:
442 (if (and irt-msgid (mew-msghdr-p))
443 (save-excursion
444 (let* ((mew-references-max-count nil)
445 (irt (mew-header-get-value mew-in-reply-to:))
446 (irtl (mew-idstr-to-id-list irt 'rev))
447 irtstr)
448 (mew-addq irtl irt-msgid)
449 (setq irtl (nreverse irtl))
450 (setq irtstr (mew-id-list-to-idstr irtl))
451 (mew-header-delete-lines (list mew-in-reply-to:))
452 (unless irt (goto-char (mew-header-end)))
453 (mew-draft-header-insert mew-in-reply-to: irtstr))))
454 (save-restriction
455 ;; this gets complicated due to supercite, please do not care
456 (narrow-to-region (point) (point)) ;; for (goto-char (point-min))
457 (insert cite)
458 ;; not for C-x C-x. Do not use mew-push-mark.
459 (push-mark (point) t t)
460 (goto-char (point-min)))
461 (cond
462 (mew-cite-hook
463 (run-hooks 'mew-cite-hook))
464 (t (mew-cite-original arg)))
465 ;; (mark-marker) indicates the point after label.
466 ;; Should we include the label too?
467 (or force (mew-highlight-body-region (mark-marker) (point) 'draft))
468 (mew-draft-auto-set-input-method))))
469
470 (defconst mew-cite-default-prefix "> ")
471
472 (defun mew-cite-original (&optional arg)
473 (if (< (marker-position (mark-marker)) (point))
474 (exchange-point-and-mark))
475 (let ((beg (point)) (end (marker-position (mark-marker)))
476 label prefix)
477 (save-restriction
478 (narrow-to-region beg end)
479 (condition-case nil
480 (setq label (funcall mew-cite-strings-function))
481 (error
482 (error "Syntax of mew-cite-format was changed. Read explanation of mew-cite-fields")))
483 (cond
484 (mew-cite-prefix-function
485 (setq prefix (funcall mew-cite-prefix-function)))
486 (mew-cite-prefix
487 (setq prefix mew-cite-prefix))
488 (t
489 (setq prefix mew-cite-default-prefix)))
490 (if (and mew-cite-prefix-confirmp (not mew-use-format-flowed))
491 (let ((ask (read-string
492 (format "Prefix (\"%s\"): " prefix) "")))
493 (if (not (string= ask "")) (setq prefix ask))))
494 ;; C-u C-c C-y cites body with header.
495 (if (eq arg nil)
496 ;; header has been already cited. So, delete it.
497 (delete-region beg (progn (mew-header-goto-body) (point))))
498 (insert label)
499 (mew-push-mark)
500 (if (or mew-cite-prefix-function mew-cite-prefix)
501 (progn
502 (and (bolp) (insert prefix))
503 (while (= 0 (forward-line))
504 (or (= (point) (point-max))
505 (insert prefix))))
506 (if (bolp) (mew-cite-format-flowed))
507 (while (= 0 (forward-line))
508 (unless (= (point) (point-max))
509 (mew-cite-format-flowed)))))))
510
511 (defun mew-cite-format-flowed ()
512 (insert mew-flowed-quoted)
513 (unless (char-equal (char-after) mew-flowed-quoted)
514 (insert mew-flowed-stuffed)))
515
516 (defun mew-cite-get-value (field)
517 (let ((value (mew-header-get-value field))
518 repl func)
519 (when (and (string= mew-from: field) value
520 (setq func (mew-addrbook-func mew-addrbook-for-cite-label)))
521 (setq repl (funcall func (mew-addrstr-parse-address value)))
522 (if repl (setq value repl)))
523 (or value "")))
524
525 (defun mew-cite-strings ()
526 "A function to create cite labels according to
527 'mew-cite-format' and 'mew-cite-fields'."
528 (if (null mew-cite-fields)
529 ""
530 (let* ((vals (mapcar 'mew-cite-get-value mew-cite-fields))
531 (label (apply 'format mew-cite-format vals))
532 (ellipses (if (stringp mew-draft-cite-ellipses)
533 mew-draft-cite-ellipses ""))
534 beg eol)
535 (if (not (or (eq mew-draft-cite-fill-mode 'truncate)
536 (eq mew-draft-cite-fill-mode 'wrap)))
537 label
538 (with-temp-buffer
539 (let ((fill-column
540 (or mew-draft-cite-label-fill-column fill-column)))
541 (insert label)
542 (goto-char (point-min))
543 (while (not (eobp))
544 (cond
545 ((eq mew-draft-cite-fill-mode 'truncate)
546 (end-of-line)
547 (if (>= fill-column (current-column))
548 ()
549 (setq eol (point))
550 (insert ellipses)
551 (goto-char eol)
552 (while (< fill-column (current-column))
553 (delete-char -1))))
554 ((eq mew-draft-cite-fill-mode 'wrap)
555 (setq beg (point))
556 (end-of-line)
557 (if (= (current-column) 0)
558 ()
559 (fill-region beg (point)))))
560 (forward-line)))
561 (buffer-string))))))
562
563 (defun mew-cite-prefix-username ()
564 "A good candidate for mew-cite-prefix-function.
565 The citation style is 'from_address> ', e.g. 'kazu> '"
566 (let* ((from (mew-header-parse-address mew-from:))
567 (user (mew-addrstr-extract-user from))
568 (func (mew-addrbook-func mew-addrbook-for-cite-prefix))
569 nickname prefix)
570 (if func (setq nickname (funcall func from)))
571 (setq prefix (or nickname user))
572 (if mew-ask-cite-prefix
573 (setq prefix (read-string "Citation prefix: " prefix)))
574 (concat prefix mew-cite-default-prefix)))
575
576 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
577 ;;;
578 ;;; format=flowed
579 ;;;
580
581 (defun mew-draft-encode-flowed (&optional arg)
582 "Manually encode the body with format=flowed.
583 If called with '\\[universal-argument]', toggle whether or not
584 format=flowed is used on composing."
585 (interactive "P")
586 (if arg
587 (progn
588 (mew-tinfo-set-use-flowed (not (mew-tinfo-get-use-flowed)))
589 (mew-draft-mode-name))
590 (save-excursion
591 (goto-char (mew-header-end))
592 (forward-line)
593 (if (mew-tinfo-get-flowed)
594 (progn
595 (mew-decode-flowed (point) (point-max)
596 (if (string= (mew-tinfo-get-flowed) "yes") t nil))
597 (mew-tinfo-set-flowed nil))
598 (let* ((charset (mew-charset-guess-region (point) (point-max)))
599 (flowed-delsp (mew-encode-flowed (point) (point-max) charset))
600 flowed delsp)
601 (mew-set '(flowed delsp) flowed-delsp)
602 (if (not flowed)
603 (message "No line folded")
604 (mew-tinfo-set-flowed (if delsp "yes" "no")))))
605 (mew-draft-rehighlight)
606 (setq buffer-undo-list nil))))
607
608 (defun mew-draft-use-format-flowed (&optional arg)
609 "Toggle the use of format=flowed for the current draft.
610 If called with '\\[universal-argument]', enable format=flowed if the argument
611 is positive. You can use `mew-draft-use-format-flowed-hooks' to
612 enable interesting minor modes according to whether the message is
613 flowed or not. Here is an example:
614
615 \(add-hook 'mew-draft-use-format-flowed-hooks
616 '(lambda()
617 (if mew-use-format-flowed
618 (progn
619 (auto-fill-mode 0)
620 (visual-line-mode 1))
621 (progn
622 (auto-fill-mode 1)
623 (visual-line-mode 0)))
624 ))"
625 (interactive "P")
626 (set (make-local-variable 'mew-use-format-flowed)
627 (if (null arg)
628 (not (mew-use-format-flowed))
629 (> (prefix-numeric-value arg) 0)))
630 (mew-tinfo-set-use-flowed mew-use-format-flowed)
631 (mew-draft-mode-name) ;; Display "F" if Flowed
632 (run-hooks 'mew-draft-use-format-flowed-hooks))
633
634 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
635 ;;;
636 ;;; Misc
637 ;;;
638
639 (defun mew-draft-save-buffer ()
640 "Save this draft."
641 (interactive)
642 (let ((after-change-functions nil))
643 (save-excursion
644 (mew-header-clear 'keep-read-only)
645 (insert-before-markers "\n") ;; for mew-summary-reply
646 (save-buffer)
647 (delete-region (1- (point)) (point))
648 (mew-header-prepared)
649 (set-buffer-modified-p nil))))
650
651 (defun mew-draft-kill ()
652 "Kill this draft."
653 (interactive)
654 (if (not (y-or-n-p "Kill draft message? "))
655 (message "Draft was not killed")
656 (let* ((attachdir (mew-attachdir)) ;; attachdir must be here
657 (draft (buffer-file-name))
658 (buf (current-buffer))
659 (mdi (concat draft mew-draft-info-suffix)))
660 (mew-elet
661 (mew-overlay-delete-buffer))
662 (save-buffer)
663 (mew-delete-file draft)
664 (mew-delete-file mdi)
665 (if (and (mew-tinfo-get-other-frame) (> (length (frame-list)) 1))
666 (delete-frame)
667 (mew-current-get-window-config))
668 (mew-delete-directory-recursively attachdir)
669 (mew-remove-buffer buf)
670 (message "Draft was killed"))))
671
672 (defun mew-draft-insert-signature (&optional arg)
673 "Insert the signature file specified by mew-signature-file.
674 If attachments exist and mew-signature-as-lastpart is *non-nil*,
675 the file is attached to the last part. Otherwise, the file is
676 inserted into the body. If mew-signature-insert-last is *non-nil*,
677 the file is inserted to the end of the body. Otherwise, inserted
678 the cursor position. If executed with '\\[universal-argument]',
679 you can set the case."
680 (interactive "P")
681 (let (case sigfile)
682 (cond
683 ((stringp arg)
684 (setq case arg))
685 (arg
686 (setq case (mew-input-case (mew-tinfo-get-case) "Signature")))
687 (t
688 (setq case (mew-tinfo-get-case))))
689 (setq sigfile (expand-file-name (mew-signature-file case)))
690 (if (not (file-exists-p sigfile))
691 (message "No signature file %s" sigfile)
692 (if (and (mew-attach-p) mew-signature-as-lastpart)
693 (progn
694 (goto-char (point-max))
695 (forward-line -2)
696 (mew-attach-forward)
697 (mew-attach-copy sigfile "Signature")
698 (let* ((nums (mew-syntax-nums))
699 (syntax (mew-syntax-get-entry mew-encode-syntax nums)))
700 (mew-syntax-set-cdp syntax nil)
701 (mew-syntax-set-cd syntax mew-signature-description))
702 (mew-encode-syntax-print mew-encode-syntax))
703 (when mew-signature-insert-last
704 (if (null (mew-attach-p))
705 (goto-char (point-max))
706 (goto-char (1- (mew-attach-begin))))
707 (end-of-line)
708 (unless (bolp) (insert "\n")))
709 (mew-insert-file-contents sigfile)))))
710
711 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
712 ;;;
713 ;;; Re-highlight
714 ;;;
715
716 (defun mew-draft-rehighlight-body ()
717 (save-excursion
718 (let ((beg (progn (goto-char (mew-header-end)) (forward-line) (point)))
719 (end (or (mew-attach-begin) (point-max))))
720 (mew-highlight-body-region beg end 'draft 'rehighlight))))
721
722 (defun mew-draft-rehighlight ()
723 "Highlight header and body again."
724 (interactive)
725 (let ((mod (buffer-modified-p)))
726 (mew-highlight-header)
727 (mew-draft-header-keymap)
728 (mew-draft-rehighlight-body)
729 (set-buffer-modified-p mod)))
730
731 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
732 ;;;
733 ;;; Privacy
734 ;;;
735
736 (defun mew-draft-toggle-privacy-always ()
737 "Toggle whether or not all drafts are protected."
738 (interactive)
739 (setq mew-protect-privacy-always (not mew-protect-privacy-always))
740 (message "Set mew-protect-privacy-always to %s"
741 mew-protect-privacy-always)
742 (mew-draft-mode-name))
743
744 (defun mew-draft-toggle-privacy-encrypted ()
745 "Toggle whether or not drafts replying to encrypted messages are
746 protected."
747 (interactive)
748 (setq mew-protect-privacy-encrypted (not mew-protect-privacy-encrypted))
749 (message "Set mew-protect-privacy-encrypted to %s"
750 mew-protect-privacy-encrypted)
751 (mew-draft-mode-name))
752
753 (defun mew-draft-set-privacy-type ()
754 "\\<mew-draft-mode-map>
755 Set privacy service which will be effective when \\[mew-draft-make-message]."
756 (interactive)
757 (let* ((services (mew-pcdb-services))
758 (alist (mapcar (lambda (x) (cons (symbol-name x) x)) services))
759 str)
760 (setq str (completing-read "Input privacy services: " alist nil t))
761 (when (stringp str)
762 (mew-tinfo-set-privacy-type (cdr (assoc str alist)))
763 (mew-tinfo-set-privacy-err nil)))
764 (mew-draft-mode-name))
765
766 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
767 ;;;
768 ;;; Sending and Queuing
769 ;;;
770
771 (defun mew-draft-make-message (&optional privacy signer)
772 "Compose a MIME message then put it into a queue folder."
773 (interactive)
774 (if (string= mode-name "Edit")
775 (mew-edit-make)
776 (if (and (mew-called-interactively-p) ;; prevent the loop
777 mew-use-old-pgp
778 mew-protect-privacy-with-old-pgp-signature)
779 (mew-pgp-sign-message)
780 (mew-draft-process-message 'queue privacy signer))))
781
782 (defun mew-draft-send-message ()
783 "Compose a MIME message then send it."
784 (interactive)
785 (if (string= mode-name "Edit")
786 (mew-edit-make)
787 (if (and (mew-called-interactively-p) ;; just in case
788 mew-use-old-pgp
789 mew-protect-privacy-with-old-pgp-signature)
790 (mew-pgp-sign-message)
791 (mew-draft-process-message 'send))))
792
793 (defun mew-draft-process-message (action &optional privacy signer)
794 (if (and (boundp 'visual-line-mode) visual-line-mode) (visual-line-mode -1))
795 (run-hooks 'mew-make-message-hook)
796 (let* ((case (or (mew-tinfo-get-case) mew-case-default))
797 (old-case case)
798 guessed-case)
799 (when mew-case-guess-when-composed
800 (setq guessed-case (mew-draft-get-case-by-guess))
801 (when guessed-case
802 (if mew-case-guess-addition
803 (setq case (mew-draft-add-case case guessed-case))
804 (setq case guessed-case))))
805 (unless (string= old-case case)
806 (mew-tinfo-set-case case)
807 (mew-draft-mode-name (mew-tinfo-get-hdr-file))
808 (mew-draft-replace-fields old-case)
809 (when (eq action 'send)
810 (mew-highlight-header)
811 (unless (mew-tinfo-get-hdr-file) (mew-draft-header-keymap)))
812 (save-buffer))
813 (if (mew-header-existp mew-newsgroups:)
814 (mew-draft-nntp-process-message case action privacy signer)
815 (mew-draft-smtp-process-message case action privacy signer))))
816
817 (defun mew-draft-resent-p (end)
818 (let ((case-fold-search t))
819 (save-excursion
820 (re-search-forward mew-resent-regex end t))))
821
822 (defun mew-draft-smtp-process-message (case action &optional privacy signer)
823 (run-hooks 'mew-send-hook)
824 (let* ((buf (current-buffer))
825 (pnm (mew-smtp-info-name case))
826 (queue (mew-queue-folder case))
827 resentp fcc sendit msg err)
828 (if (get-process pnm)
829 (message "Another message is being sent. Try later")
830 (mew-draft-remove-invalid-fields)
831 ;; Check resentp
832 (save-excursion
833 (goto-char (point-min))
834 (setq resentp (mew-draft-resent-p (mew-header-end))))
835 ;; Ask Subject: before the query of "Really send".
836 ;; Typing C-g here gets back to the draft.
837 (mew-encode-ask-subject)
838 (setq fcc (mew-encode-ask-fcc resentp))
839 (if (eq action 'queue)
840 (setq sendit t)
841 (if mew-ask-send
842 (setq sendit (y-or-n-p "Really send this message? "))
843 (setq sendit t)))
844 (when sendit
845 ;; password should be asked in Summary mode.
846 (if (and (mew-tinfo-get-other-frame) (> (length (frame-list)) 1))
847 (delete-frame)
848 (mew-current-get-window-config)
849 (delete-windows-on buf)) ;; just in case
850 (save-excursion
851 (save-window-excursion
852 (set-buffer buf)
853 (if (mew-smtp-encode pnm case resentp fcc privacy signer)
854 (let ((mdi (concat (buffer-file-name) mew-draft-info-suffix)))
855 (mew-delete-file mdi)
856 (setq msg (mew-smtp-queue case "from Draft mode"))
857 (mew-remove-buffer buf)
858 (if (eq action 'send)
859 (mew-smtp-send-message case queue (list msg))))
860 (setq err t))))
861 ;; now +queue/1 exists
862 (if err
863 (progn
864 (mew-current-set-window-config)
865 (switch-to-buffer buf)
866 (delete-other-windows))
867 (if (and (eq action 'queue) mew-visit-queue-after-sending)
868 (mew-summary-visit-folder queue))
869 (run-hooks 'mew-real-send-hook))))))
870
871 (defun mew-draft-nntp-process-message (case action &optional privacy signer)
872 (run-hooks 'mew-post-hook)
873 (let* ((buf (current-buffer))
874 (pnm (mew-nntp2-info-name case))
875 (postq (mew-postq-folder case))
876 fcc sendit msg err)
877 (if (get-process pnm)
878 (message "Another message is being posted. Try later")
879 (mew-draft-remove-invalid-fields)
880 ;; Ask Subject: before the query of "Really post".
881 ;; Typing C-g here gets back to the draft.
882 (mew-encode-ask-subject)
883 (setq fcc (mew-encode-ask-fcc nil))
884 (if (eq action 'queue)
885 (setq sendit t)
886 (if mew-ask-post
887 (setq sendit (y-or-n-p "Really post this message? "))
888 (setq sendit t)))
889 (when sendit
890 ;; password should be asked in Summary mode.
891 (if (and (mew-tinfo-get-other-frame) (> (length (frame-list)) 1))
892 (delete-frame)
893 (mew-current-get-window-config)
894 (delete-windows-on buf)) ;; just in case
895 (save-excursion
896 (save-window-excursion
897 (set-buffer buf)
898 (if (mew-nntp2-encode pnm case fcc privacy signer)
899 (let ((mdi (concat (buffer-file-name) mew-draft-info-suffix)))
900 (mew-delete-file mdi)
901 (setq msg (mew-nntp2-queue case "from Draft mode"))
902 (mew-remove-buffer buf)
903 (if (eq action 'send)
904 (mew-nntp2-send-message case postq (list msg))))
905 (setq err t))))
906 (if err
907 (progn
908 (mew-current-set-window-config)
909 (switch-to-buffer buf)
910 (delete-other-windows))
911 (if (and (eq action 'queue) mew-visit-queue-after-sending)
912 (mew-summary-visit-folder postq))
913 (run-hooks 'mew-real-post-hook))))))
914
915 (defun mew-draft-remove-invalid-fields ()
916 (when (mew-header-end)
917 (save-excursion
918 (save-restriction
919 (goto-char (mew-header-end))
920 (if (not (bolp)) (insert "\n"))
921 (narrow-to-region (point-min) (mew-header-end))
922 (let (beg med str)
923 (mew-elet
924 ;; removing null lines
925 (goto-char (point-min))
926 (while (and (re-search-forward "^$" nil t)
927 (not (eobp)))
928 (delete-char 1))
929 ;; removing fields which do not have value.
930 (goto-char (point-min))
931 (while (not (eobp))
932 (if (not (looking-at mew-keyval))
933 (forward-line)
934 (setq beg (match-beginning 0))
935 (setq med (match-end 0))
936 (forward-line)
937 (mew-header-goto-next)
938 (setq str (mew-buffer-substring med (1- (point))))
939 ;; str may consists of multiple lines
940 ;; So, "$" does not work. We need to use "[^ ]".
941 (unless (string-match "[^ \t\n]" str)
942 (delete-region beg (point)))))))))))
943
944 ;; backward-compatibility
945 (defalias 'mew-draft-send-letter 'mew-draft-send-message)
946
947 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
948 ;;;
949 ;;; Privacy
950 ;;;
951
952 (defvar mew-draft-privacy-method-alist '(("pgp" . pgp) ("smime" . smime)))
953
954 (defun mew-draft-set-privacy-method ()
955 "Set mew-draft-privacy-method. 'pgp or 'smime."
956 (interactive)
957 (let ((method (completing-read "Privacy method: " mew-draft-privacy-method-alist nil t)))
958 (setq mew-draft-privacy-method
959 (cdr (assoc method mew-draft-privacy-method-alist)))))
960
961 (defmacro mew-draft-privacy-switch (&rest form)
962 `(let ((method (mew-draft-privacy-method (mew-tinfo-get-case))))
963 (cond
964 ,@(mapcar
965 (lambda (x)
966 (if (eq (car x) t)
967 x
968 `((eq method ',(car x)) ,(car (cdr x)))))
969 form)
970 (t (message "'%s' is not supported" method)))))
971
972 (defun mew-draft-sign-message (&optional arg)
973 "Sign the entire draft. Input your passphrase."
974 (interactive "P")
975 (mew-draft-privacy-switch
976 (pgp (mew-pgp-sign-message arg))
977 (smime (mew-smime-sign-message arg))))
978
979 (defun mew-draft-encrypt-message ()
980 "Encrypt the entire draft with PGP."
981 (interactive)
982 (mew-draft-privacy-switch
983 (pgp (mew-pgp-encrypt-message))
984 (smime (mew-smime-encrypt-message))))
985
986 (defun mew-draft-sign-encrypt-message (&optional arg)
987 "Sign then encrypt the entire draft. Input your passphrase."
988 (interactive "P")
989 (mew-draft-privacy-switch
990 (pgp (mew-pgp-sign-encrypt-message arg))
991 (smime (mew-smime-sign-encrypt-message arg))))
992
993 (defun mew-draft-encrypt-sign-message (&optional arg)
994 "Encrypt then sign the entire draft. Input your passphrase."
995 (interactive "P")
996 (mew-draft-privacy-switch
997 (pgp (mew-pgp-encrypt-sign-message arg))
998 (smime (mew-smime-encrypt-sign-message arg))))
999
1000 (provide 'mew-draft)
1001
1002 ;;; Copyright Notice:
1003
1004 ;; Copyright (C) 1996-2015 Mew developing team.
1005 ;; All rights reserved.
1006
1007 ;; Redistribution and use in source and binary forms, with or without
1008 ;; modification, are permitted provided that the following conditions
1009 ;; are met:
1010 ;;
1011 ;; 1. Redistributions of source code must retain the above copyright
1012 ;; notice, this list of conditions and the following disclaimer.
1013 ;; 2. Redistributions in binary form must reproduce the above copyright
1014 ;; notice, this list of conditions and the following disclaimer in the
1015 ;; documentation and/or other materials provided with the distribution.
1016 ;; 3. Neither the name of the team nor the names of its contributors
1017 ;; may be used to endorse or promote products derived from this software
1018 ;; without specific prior written permission.
1019 ;;
1020 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
1021 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1022 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1023 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
1024 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1025 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1026 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
1027 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
1028 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
1029 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
1030 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1031
1032 ;;; mew-draft.el ends here