* wl-vars.el (wl-draft-send-hook): Changed default value to
[elisp/wanderlust.git] / wl / wl-xmas.el
1 ;;; wl-xmas.el -- Wanderlust modules for XEmacsen.
2
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright 2000 Katsumi Yamaoka <yamaoka@jpl.org>
5
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>,
7 ;;         Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, net news
9
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
11
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.
16 ;;
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.
21 ;;
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.
26 ;;
27
28 ;;; Commentary:
29 ;;
30
31 ;;; Code:
32 ;;
33
34 (eval-when-compile
35   (require 'wl-folder)
36   (require 'wl-summary)
37   (require 'wl-draft)
38   (require 'wl-message)
39   (require 'wl-highlight)
40   (defvar-maybe wl-draft-mode-map (make-sparse-keymap))
41   (defalias-maybe 'toolbar-make-button-list 'ignore))
42
43 (add-hook 'wl-folder-mode-hook 'wl-setup-folder)
44 (add-hook 'wl-folder-mode-hook 'wl-folder-init-icons)
45
46 (add-hook 'wl-init-hook 'wl-biff-init-icons)
47 (add-hook 'wl-init-hook 'wl-plugged-init-icons)
48 ;; (add-hook 'wl-make-plugged-hook 'wl-biff-init-icons)
49 ;; (add-hook 'wl-make-plugged-hook 'wl-plugged-init-icons)
50
51 (add-hook 'wl-summary-mode-hook 'wl-setup-summary)
52
53 (defvar wl-use-toolbar (if (featurep 'toolbar) 'default-toolbar nil))
54 (defvar wl-plugged-glyph nil)
55 (defvar wl-unplugged-glyph nil)
56 (defvar wl-biff-mail-glyph nil)
57 (defvar wl-biff-nomail-glyph nil)
58
59 (defvar wl-folder-toolbar
60   '([wl-folder-jump-to-current-entity
61      wl-folder-jump-to-current-entity t "Enter Current Folder"]
62     [wl-folder-next-entity
63      wl-folder-next-entity t "Next Folder"]
64     [wl-folder-prev-entity
65      wl-folder-prev-entity t "Previous Folder"]
66     [wl-folder-check-current-entity
67      wl-folder-check-current-entity t "Check Current Folder"]
68 ;;; [wl-draft
69 ;;;  wl-draft t "Write a New Message"]
70     [wl-folder-sync-current-entity
71      wl-folder-sync-current-entity t "Sync Current Folder"]
72     [wl-draft
73      wl-draft t "Write a New Message"]
74     [wl-folder-empty-trash
75      wl-folder-empty-trash t "Empty Trash"]
76     [wl-exit
77      wl-exit t "Quit Wanderlust"]
78     )
79   "The Folder buffer toolbar.")
80
81 (defvar wl-summary-toolbar
82   '([wl-summary-read
83      wl-summary-read t "Read Messages"]
84     [wl-summary-next
85      wl-summary-next t "Next Message"]
86     [wl-summary-prev
87      wl-summary-prev t "Previous Message"]
88     [wl-summary-jump-to-current-message
89      wl-summary-jump-to-current-message t "Jump to Current Message"]
90     [wl-summary-sync-force-update
91      wl-summary-sync-force-update t "Sync Current Folder"]
92     [wl-summary-delete
93      wl-summary-delete t "Delete Current Message"]
94     [wl-summary-mark-as-important
95      wl-summary-mark-as-important t "Mark Current Message as Important"]
96     [wl-draft
97      wl-draft t "Write a New Message"]
98     [wl-summary-reply
99      wl-summary-reply t "Reply to Current Message" ]
100     [wl-summary-reply-with-citation
101      wl-summary-reply-with-citation t "Reply to Current Message with Citation"]
102     [wl-summary-forward
103      wl-summary-forward t "Forward Current Message"]
104     [wl-summary-exit
105      wl-summary-exit t "Exit Current Summary"]
106     )
107   "The Summary buffer toolbar.")
108
109 (defvar wl-message-toolbar
110   '([wl-message-read
111      wl-message-read t "Read Contents"]
112     [wl-message-next-content
113      wl-message-next-content t "Next Content"]
114     [wl-message-prev-content
115      wl-message-prev-content t "Previous Content"]
116     [wl-message-quit
117      wl-message-quit t "Back to Summary"]
118     [wl-message-play-content
119      wl-message-play-content t "Play Content"]
120     [wl-message-extract-content
121      wl-message-extract-content t "Extract Content"]
122     )
123   "The Message buffer toolbar.")
124
125 (defalias 'wl-draft-insert-signature 'insert-signature);; for draft toolbar.
126
127 (defvar wl-draft-toolbar
128   '([wl-draft-send-from-toolbar
129      wl-draft-send-from-toolbar t "Send Current Draft"]
130     [wl-draft-yank-original
131      wl-draft-yank-original t "Yank Displaying Message"]
132     [wl-draft-insert-signature
133      wl-draft-insert-signature t "Insert Signature"]
134     [wl-draft-kill
135      wl-draft-kill t "Kill Current Draft"]
136     )
137   "The Draft buffer toolbar.")
138
139 (defun wl-xmas-setup-toolbar (bar)
140   (let ((dir wl-icon-dir)
141         icon up down disabled name)
142     (when dir
143       (while bar
144         (setq icon (aref (car bar) 0)
145               name (symbol-name icon)
146               bar (cdr bar))
147         (unless (boundp icon)
148           (setq up (expand-file-name (concat name "-up.xpm") dir)
149                 down (expand-file-name (concat name "-down.xpm") dir)
150                 disabled (expand-file-name (concat name "-disabled.xpm") dir))
151           (if (file-exists-p up)
152               (set icon (toolbar-make-button-list
153                          up (and (file-exists-p down) down)
154                          (and (file-exists-p disabled) disabled)))
155             (setq bar nil
156                   dir nil)))))
157     dir))
158
159 (defun wl-xmas-make-icon-glyph (icon-string icon-file
160                                             &optional locale tag-set)
161   (let ((glyph (make-glyph (vector 'string :data icon-string))))
162     (when wl-highlight-folder-with-icon
163       (set-glyph-image glyph
164                        (vector 'xpm :file (expand-file-name
165                                            icon-file wl-icon-dir))
166                        locale tag-set 'prepend))
167     glyph))
168
169 (eval-when-compile
170   (defsubst wl-xmas-setup-folder-toolbar ()
171     (and wl-use-toolbar
172          (wl-xmas-setup-toolbar wl-folder-toolbar)
173          (set-specifier (symbol-value wl-use-toolbar)
174                         (cons (current-buffer) wl-folder-toolbar))))
175
176   (defsubst wl-xmas-setup-summary-toolbar ()
177     (and wl-use-toolbar
178          (wl-xmas-setup-toolbar wl-summary-toolbar)
179          (set-specifier (symbol-value wl-use-toolbar)
180                         (cons (current-buffer) wl-summary-toolbar))))
181
182   (defsubst wl-xmas-setup-message-toolbar ()
183     (and wl-use-toolbar
184          (wl-xmas-setup-toolbar wl-message-toolbar)
185          (set-specifier (symbol-value wl-use-toolbar)
186                         (cons (current-buffer) wl-message-toolbar))))
187
188   (defsubst wl-xmas-setup-draft-toolbar ()
189     (and wl-use-toolbar
190          (wl-xmas-setup-toolbar wl-draft-toolbar)
191          (set-specifier (symbol-value wl-use-toolbar)
192                         (cons (current-buffer) wl-draft-toolbar)))))
193
194 (defvar wl-folder-toggle-icon-list
195   '((wl-folder-opened-glyph       . wl-opened-group-folder-icon)
196     (wl-folder-closed-glyph       . wl-closed-group-folder-icon)))
197
198 (eval-when-compile
199   (defsubst wl-xmas-highlight-folder-group-line (glyph text-face numbers)
200     (let ((start (match-beginning 1))
201           (end (match-end 1)))
202       (let (extent)
203         (while (and (setq extent (extent-at start nil nil extent 'at))
204                     (not (and (eq start (extent-start-position extent))
205                               (eq end (extent-end-position extent))
206                               (extent-end-glyph extent)))))
207         (unless extent
208           (setq extent (make-extent start end)))
209         (set-extent-properties extent `(end-open t start-closed t invisible t))
210         (set-extent-end-glyph
211          extent
212          (or (get glyph 'glyph)
213              (put glyph 'glyph
214                   (wl-xmas-make-icon-glyph
215                    (buffer-substring-no-properties start end)
216                    (symbol-value
217                     (cdr (assq glyph wl-folder-toggle-icon-list))))))))
218       (let ((inhibit-read-only t))
219         (when wl-use-highlight-mouse-line
220           (put-text-property start (point-at-eol) 'mouse-face 'highlight))
221         (setq start end
222               end (point-at-eol))
223         (if (and wl-highlight-folder-by-numbers
224                  numbers (nth 0 numbers) (nth 1 numbers)
225                  (re-search-forward "[0-9-]+/[0-9-]+/[0-9-]+" end t))
226             (let* ((unsync (nth 0 numbers))
227                    (unread (nth 1 numbers))
228                    (face (cond ((and unsync (zerop unsync))
229                                 (if (and unread (zerop unread))
230                                     'wl-highlight-folder-zero-face
231                                   'wl-highlight-folder-unread-face))
232                                ((and unsync
233                                      (>= unsync
234                                          wl-folder-many-unsync-threshold))
235                                 'wl-highlight-folder-many-face)
236                                (t
237                                 'wl-highlight-folder-few-face))))
238               (if (numberp wl-highlight-folder-by-numbers)
239                   (progn
240                     (put-text-property start (match-beginning 0)
241                                        'face text-face)
242                     (put-text-property (match-beginning 0) (point) 'face face))
243                 (put-text-property start end 'face face)))
244           (put-text-property start end 'face text-face))))))
245
246 (defun wl-highlight-folder-current-line (&optional numbers)
247   (interactive)
248   (save-excursion
249     (beginning-of-line)
250     (let (fld-name)
251       (cond
252        (;; opened folder group
253         (looking-at wl-highlight-folder-opened-regexp)
254         (wl-xmas-highlight-folder-group-line 'wl-folder-opened-glyph
255                                              'wl-highlight-folder-opened-face
256                                              numbers))
257        (;; closed folder group
258         (looking-at wl-highlight-folder-closed-regexp)
259         (wl-xmas-highlight-folder-group-line 'wl-folder-closed-glyph
260                                              'wl-highlight-folder-closed-face
261                                              numbers))
262        (;; basic folder
263         (and (setq fld-name (wl-folder-get-folder-name-by-id
264                              (get-text-property (point) 'wl-folder-entity-id)))
265              (looking-at "[ \t]+\\([^ \t]+\\)"))
266         (let ((start (match-beginning 1)))
267           (let (extent)
268             (while (and (setq extent (extent-at start nil nil extent 'at))
269                         (not (and (eq start (extent-start-position extent))
270                                   (eq start (extent-end-position extent))
271                                   (extent-begin-glyph extent)))))
272             (unless extent
273               (setq extent (make-extent start start)))
274             (let (type)
275               (set-extent-begin-glyph
276                extent
277                (cond
278                 ((string= fld-name wl-trash-folder);; trash folder
279                  (let ((num (nth 2 numbers)));; number of messages
280                    (get (if (or (not num) (zerop num))
281                             'wl-folder-trash-empty-glyph
282                           'wl-folder-trash-glyph)
283                         'glyph)))
284                 ((string= fld-name wl-draft-folder);; draft folder
285                  (get 'wl-folder-draft-glyph 'glyph))
286                 ((string= fld-name wl-queue-folder);; queue folder
287                  (get 'wl-folder-queue-glyph 'glyph))
288                 (;; and one of many other folders
289                  (setq type (elmo-folder-get-type fld-name))
290                  (get (intern (format "wl-folder-%s-glyph" type)) 'glyph))))))
291           (let ((end (point-at-eol)))
292             (when wl-use-highlight-mouse-line
293               (put-text-property start end 'mouse-face 'highlight))
294             (let ((text-face
295                    (if (looking-at (format "^[ \t]*\\(%s\\|%s\\)"
296                                            wl-folder-unsubscribe-mark
297                                            wl-folder-removed-mark))
298                        'wl-highlight-folder-killed-face
299                      'wl-highlight-folder-unknown-face)))
300               (if (and wl-highlight-folder-by-numbers
301                        numbers (nth 0 numbers) (nth 1 numbers)
302                        (re-search-forward "[0-9-]+/[0-9-]+/[0-9-]+" end t))
303                   (let* ((unsync (nth 0 numbers))
304                          (unread (nth 1 numbers))
305                          (face (cond
306                                 ((and unsync (zerop unsync))
307                                  (if (and unread (zerop unread))
308                                      'wl-highlight-folder-zero-face
309                                    'wl-highlight-folder-unread-face))
310                                 ((and unsync
311                                       (>= unsync
312                                           wl-folder-many-unsync-threshold))
313                                  'wl-highlight-folder-many-face)
314                                 (t
315                                  'wl-highlight-folder-few-face))))
316                     (if (numberp wl-highlight-folder-by-numbers)
317                         (progn
318                           (put-text-property start (match-beginning 0)
319                                              'face text-face)
320                           (put-text-property (match-beginning 0)
321                                              (match-end 0)
322                                              'face face))
323                       ;; Remove previous face.
324                       (put-text-property start (match-end 0) 'face nil)
325                       (put-text-property start (match-end 0) 'face face)))
326                 (put-text-property start end 'face text-face))))))))))
327
328 (defun wl-highlight-plugged-current-line ()
329   (interactive)
330   (save-excursion
331     (let ((inhibit-read-only t)
332           extent switch)
333       (beginning-of-line)
334       (when (looking-at "[ \t]*\\(\\[\\([^]]+\\)\\]\\)")
335         (setq switch (elmo-match-buffer 2))
336         (when (and (setq extent (extent-at (match-end 1) nil nil nil 'at))
337                    (extent-end-glyph extent))
338           (delete-extent extent))
339         (setq extent (make-extent (match-beginning 1) (match-end 1)))
340         (set-extent-property extent 'end-open t)
341         (set-extent-property extent 'start-closed t)
342         (set-extent-property extent 'invisible t)
343         (set-extent-end-glyph extent (if (string= switch wl-plugged-plug-on)
344                                          wl-plugged-glyph
345                                        wl-unplugged-glyph))))))
346
347 (defun wl-plugged-set-folder-icon (folder string)
348   (let ((string (copy-sequence string))
349         (len (length string))
350         type)
351     (if (string= folder wl-queue-folder)
352         (put-text-property 0 len 'begin-glyph
353                            (get 'wl-folder-queue-glyph 'glyph)
354                            string)
355       (if (setq type (elmo-folder-get-type folder))
356           (put-text-property 0 len
357                              'begin-glyph
358                              (get (intern (format "wl-folder-%s-glyph" type))
359                                   'glyph)
360                              string)))
361     string))
362
363 (defvar wl-folder-internal-icon-list
364   ;; alist of (glyph . icon-file)
365   '((wl-folder-nntp-glyph         . wl-nntp-folder-icon)
366     (wl-folder-imap4-glyph        . wl-imap-folder-icon)
367     (wl-folder-pop3-glyph         . wl-pop-folder-icon)
368     (wl-folder-localdir-glyph     . wl-localdir-folder-icon)
369     (wl-folder-localnews-glyph    . wl-localnews-folder-icon)
370     (wl-folder-internal-glyph     . wl-internal-folder-icon)
371     (wl-folder-multi-glyph        . wl-multi-folder-icon)
372     (wl-folder-filter-glyph       . wl-filter-folder-icon)
373     (wl-folder-archive-glyph      . wl-archive-folder-icon)
374     (wl-folder-pipe-glyph         . wl-pipe-folder-icon)
375     (wl-folder-maildir-glyph      . wl-maildir-folder-icon)
376     (wl-folder-trash-empty-glyph  . wl-empty-trash-folder-icon)
377     (wl-folder-draft-glyph        . wl-draft-folder-icon)
378     (wl-folder-queue-glyph        . wl-queue-folder-icon)
379     (wl-folder-trash-glyph        . wl-trash-folder-icon)))
380
381 (defun wl-folder-init-icons ()
382   (dolist (icon wl-folder-internal-icon-list)
383     (unless (get (car icon) 'glyph)
384       (put (car icon) 'glyph
385            (wl-xmas-make-icon-glyph "" (symbol-value (cdr icon)))))))
386
387 (defun wl-plugged-init-icons ()
388   (unless wl-plugged-glyph
389     (setq wl-plugged-glyph (wl-xmas-make-icon-glyph
390                             wl-plug-state-indicator-on wl-plugged-icon)
391           wl-unplugged-glyph (wl-xmas-make-icon-glyph
392                               wl-plug-state-indicator-off wl-unplugged-icon))
393     (let ((extent (make-extent nil nil)))
394       (let ((keymap (make-sparse-keymap)))
395         (define-key keymap 'button2
396           (make-modeline-command-wrapper 'wl-toggle-plugged))
397         (set-extent-keymap extent keymap)
398         (set-extent-property extent 'help-echo
399                              "button2 toggles plugged status"))
400       (setq wl-modeline-plug-state-on (cons extent wl-plugged-glyph)
401             wl-modeline-plug-state-off (cons extent wl-unplugged-glyph)))))
402
403 (defun wl-biff-init-icons ()
404   (unless wl-biff-mail-glyph
405     (setq wl-biff-mail-glyph (wl-xmas-make-icon-glyph
406                               wl-biff-state-indicator-on
407                               wl-biff-mail-icon)
408           wl-biff-nomail-glyph (wl-xmas-make-icon-glyph
409                                 wl-biff-state-indicator-off
410                                 wl-biff-nomail-icon))
411     (let ((extent (make-extent nil nil)))
412       (let ((keymap (make-sparse-keymap)))
413         (define-key keymap 'button2
414           (make-modeline-command-wrapper 'wl-biff-check-folders))
415         (set-extent-keymap extent keymap)
416         (set-extent-property extent 'help-echo "button2 checks new mails"))
417       (setq wl-modeline-biff-state-on (cons extent wl-biff-mail-glyph)
418             wl-modeline-biff-state-off (cons extent wl-biff-nomail-glyph)))))
419
420 (defun wl-make-date-string ()
421   (let ((s (current-time-string)))
422     (string-match "\\`\\([A-Z][a-z][a-z]\\) +[A-Z][a-z][a-z] +[0-9][0-9]? *[0-9][0-9]?:[0-9][0-9]:[0-9][0-9] *[0-9]?[0-9]?[0-9][0-9]"
423                   s)
424     (concat (wl-match-string 1 s) ", "
425             (timezone-make-date-arpa-standard s (current-time-zone)))))
426
427 (defun wl-setup-folder ()
428   (and (featurep 'scrollbar)
429        (set-specifier scrollbar-height (cons (current-buffer) 0)))
430   (wl-xmas-setup-folder-toolbar))
431
432 (defvar dragdrop-drop-functions)
433
434 (defun wl-setup-summary ()
435   (make-local-variable 'dragdrop-drop-functions)
436   (setq dragdrop-drop-functions '((wl-dnd-default-drop-message t t)))
437   (and (featurep 'scrollbar)
438        (set-specifier scrollbar-height (cons (current-buffer) 0)))
439   (wl-xmas-setup-summary-toolbar))
440
441 (defun wl-message-overload-functions ()
442   (wl-xmas-setup-message-toolbar)
443   (local-set-key "l" 'wl-message-toggle-disp-summary)
444   (local-set-key 'button2 'wl-message-refer-article-or-url)
445   (local-set-key 'button4 'wl-message-wheel-down)
446   (local-set-key 'button5 'wl-message-wheel-up)
447   (local-set-key [(shift button4)] 'wl-message-wheel-down)
448   (local-set-key [(shift button5)] 'wl-message-wheel-up)
449   (set-keymap-parent wl-message-button-map (current-local-map))
450   (define-key wl-message-button-map 'button2
451     'wl-message-button-dispatcher))
452
453 (defun wl-message-wheel-up (event)
454   (interactive "e")
455   (let ((cur-buf (current-buffer))
456         proceed)
457     (save-selected-window
458       (select-window (event-window event))
459       (set-buffer cur-buf)
460       (setq proceed (wl-message-next-page)))
461     (when proceed
462       (if (memq 'shift (event-modifiers event))
463           (wl-summary-down t)
464         (wl-summary-next t)))))
465
466 (defun wl-message-wheel-down (event)
467   (interactive "e")
468   (let ((cur-buf (current-buffer))
469         proceed)
470     (save-selected-window
471       (select-window (event-window event))
472       (set-buffer cur-buf)
473       (setq proceed (wl-message-prev-page)))
474     (when proceed
475       (if (memq 'shift (event-modifiers event))
476           (wl-summary-up t)
477         (wl-summary-prev t)))))
478
479 (defun wl-draft-overload-menubar ()
480   (when (featurep 'menubar)
481     (add-menu-item '("Mail") "Send, Keep Editing"
482                    'wl-draft-send t "Send Mail")
483     (add-menu-item '("Mail") "Send Message"
484                    'wl-draft-send-and-exit t "Send and Exit")
485     (delete-menu-item '("Mail" "Send Mail"))
486     (delete-menu-item '("Mail" "Send and Exit"))))
487
488 (defun wl-draft-mode-setup ()
489   (require 'derived)
490   (define-derived-mode wl-draft-mode mail-mode "Draft"
491     "draft mode for Wanderlust derived from mail mode.
492 See info under Wanderlust for full documentation.
493
494 Special commands:
495 \\{wl-draft-mode-map}"))
496
497 (defun wl-draft-key-setup ()
498   (define-key wl-draft-mode-map "\C-c\C-y" 'wl-draft-yank-original)
499   (define-key wl-draft-mode-map "\C-c\C-s" 'wl-draft-send)
500   (define-key wl-draft-mode-map "\C-c\C-a" 'wl-draft-insert-x-face-field)
501   (define-key wl-draft-mode-map "\C-c\C-c" 'wl-draft-send-and-exit)
502   (define-key wl-draft-mode-map "\C-c\C-z" 'wl-draft-save-and-exit)
503   (define-key wl-draft-mode-map "\C-c\C-k" 'wl-draft-kill)
504   (define-key wl-draft-mode-map "\C-l" 'wl-draft-highlight-and-recenter)
505   (define-key wl-draft-mode-map "\C-i" 'wl-complete-field-body-or-tab)
506   (define-key wl-draft-mode-map "\C-c\C-r" 'wl-draft-caesar-region)
507   (define-key wl-draft-mode-map "\M-t" 'wl-toggle-plugged)
508   (define-key wl-draft-mode-map "\C-c\C-o" 'wl-jump-to-draft-buffer)
509   (define-key wl-draft-mode-map "\C-c\C-e" 'wl-draft-config-exec)
510   (define-key wl-draft-mode-map "\C-c\C-j" 'wl-template-select)
511   (define-key wl-draft-mode-map "\C-c\C-p" 'wl-draft-preview-message)
512   (define-key wl-draft-mode-map "\C-x\C-s" 'wl-draft-save)
513   (define-key wl-draft-mode-map "\C-xk"    'wl-draft-mimic-kill-buffer))
514
515 (defun wl-draft-overload-functions ()
516   (wl-mode-line-buffer-identification)
517   (local-set-key "\C-c\C-s" 'wl-draft-send);; override
518   (wl-xmas-setup-draft-toolbar)
519   (wl-draft-overload-menubar))
520
521 (defalias 'wl-defface 'defface)
522
523 (require 'product)
524 (product-provide (provide 'wl-xmas) (require 'wl-version))
525
526 ;;; wl-xmas.el ends here