This commit was manufactured by cvs2svn to create branch 'elmo-imap4-new-
[elisp/wanderlust.git] / wl / wl-template.el
1 ;;; wl-template.el --- Draft template feature for Wanderlust.
2
3 ;; Copyright (C) 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
4 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5
6 ;; Author: Masahiro MURATA <muse@ba2.so-net.ne.jp>
7 ;; Keywords: mail, net news
8
9 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15 ;;
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 ;;
26
27 ;;; Commentary:
28
29 ;;; Code:
30 ;;
31 (require 'elmo-util)
32 (require 'wl-vars)
33
34 ;; Variables
35
36 (defvar wl-template-default-name "default")
37 (defvar wl-template-buffer-name "*WL-Template*")
38 (defvar wl-template-mode-map nil)
39
40 (defvar wl-template nil)
41 (defvar wl-template-cur-num 0)
42 (defvar wl-template-max-num 0)
43 (defvar wl-template-draft-buffer nil)
44 (defvar wl-template-preview nil)
45
46 ;;; Code
47
48 (if wl-template-mode-map
49     nil
50   (setq wl-template-mode-map (make-sparse-keymap))
51   (define-key wl-template-mode-map "p"     'wl-template-prev)
52   (define-key wl-template-mode-map "n"     'wl-template-next)
53   (define-key wl-template-mode-map "q"     'wl-template-abort)
54   (define-key wl-template-mode-map "\r"    'wl-template-set)
55   (define-key wl-template-mode-map "\n"    'wl-template-set))
56
57 (defun wl-template-preview-p ()
58   "Return non-nil when preview template."
59   wl-template-preview)
60
61 (defun wl-template-apply (name)
62   "Apply NAME template to draft."
63   (let (template)
64     (when name
65       (if (string= name "")
66           (setq name wl-template-default-name))
67       (when (setq template (cdr (assoc name wl-template-alist)))
68         (save-excursion
69           (setq wl-draft-config-variables
70                 (elmo-uniq-list
71                  (nconc wl-draft-config-variables
72                         (save-excursion
73                           (wl-draft-config-exec-sub template)))))
74           ;; rehighlight
75           (if wl-highlight-body-too
76               (let ((beg (point-min))
77                     (end (point-max)))
78                 (put-text-property beg end 'face nil)
79                 (wl-highlight-message beg end t))))))))
80
81 (defun wl-template-mode ()
82   "Major mode for Wanderlust template.
83 See info under Wanderlust for full documentation.
84
85 \\{wl-template-mode}
86
87 Entering WL-Template mode calls the value of `wl-template-mode-hook'."
88   (kill-all-local-variables)
89   (setq mode-name "Wl-Template"
90         major-mode 'wl-template-mode)
91   (use-local-map wl-template-mode-map)
92   (setq buffer-read-only t)
93   (run-hooks 'wl-template-mode-hook))
94
95 (defun wl-template-select (&optional arg)
96   "Select template from `wl-template-alist'."
97   (interactive "P")
98   (unless wl-template-alist
99     (error "Please set `wl-template-alist'"))
100   (setq wl-draft-config-exec-flag t)
101   (if (not (if arg
102                (not wl-template-visible-select)
103              wl-template-visible-select))
104       (wl-template-apply
105        (completing-read (format "Template (%s): " wl-template-default-name)
106                         wl-template-alist))
107     (let* ((begin wl-template-default-name)
108            (work wl-template-alist))
109       (if (and begin (cdr (assoc begin wl-template-alist)))
110           (while (not (string= (car (car work)) begin))
111             (setq wl-template-cur-num (1+ wl-template-cur-num))
112             (setq work (cdr work))))
113       (setq wl-template nil
114             wl-template-cur-num 0
115             wl-template-max-num (length wl-template-alist))
116       (setq wl-template-draft-buffer (current-buffer))
117       (if (get-buffer-window wl-template-buffer-name)
118           (select-window (get-buffer-window wl-template-buffer-name))
119         (let* ((cur-win (selected-window))
120                (size (min
121                       (- (window-height cur-win)
122                          window-min-height 1)
123                       (- (window-height cur-win)
124                          (max window-min-height
125                               (1+ wl-template-buffer-lines))))))
126           (split-window cur-win (if (> size 0) size window-min-height))
127           ;; goto the bottom of the two...
128           (select-window (next-window))
129           ;; make it display...
130           (let ((pop-up-windows nil))
131             (switch-to-buffer (get-buffer-create wl-template-buffer-name)))))
132       (set-buffer wl-template-buffer-name)
133       (wl-template-mode)
134       (wl-template-show))))
135
136 (defun wl-template-show (&optional arg)
137   "Show reference INDEX in `wl-template-alist'.
138 ARG is ignored."                        ; ARG ignored this version (?)
139   (with-current-buffer wl-template-buffer-name
140     (let ((buffer-read-only nil)
141           (wl-template-preview t)
142           (mail-header-separator  "--header separater--"))
143       (erase-buffer)
144       (goto-char (point-min))
145       (wl-template-insert
146        (setq wl-template (car (nth wl-template-cur-num wl-template-alist)))
147        mail-header-separator)
148       (wl-highlight-message (point-min) (point-max) t)
149       (when wl-highlight-x-face-function
150         (funcall wl-highlight-x-face-function
151                  (point-min) (re-search-forward mail-header-separator nil t)))
152       (setq mode-line-process (concat ":" wl-template))
153       (set-buffer-modified-p nil))))
154
155 (defun wl-template-next ()
156   "Display next reference in other buffer."
157   (interactive)
158   (if (= wl-template-max-num
159          (setq wl-template-cur-num (1+ wl-template-cur-num)))
160       (setq wl-template-cur-num 0))
161   (wl-template-show))
162
163 (defun wl-template-prev ()
164   "Display previous reference in other buffer."
165   (interactive)
166   (setq wl-template-cur-num (if (zerop wl-template-cur-num)
167                                 (1- wl-template-max-num)
168                               (1- wl-template-cur-num)))
169   (wl-template-show))
170
171 (defun wl-template-abort ()
172   "Exit from electric reference mode without inserting reference."
173   (interactive)
174   (setq wl-template nil)
175   (delete-window)
176   (kill-buffer wl-template-buffer-name)
177   (when (buffer-live-p wl-template-draft-buffer)
178     (set-buffer wl-template-draft-buffer)
179     (let ((win (get-buffer-window wl-template-draft-buffer)))
180       (if win (select-window win)))))
181
182 (defun wl-template-set ()
183   "Exit from electric reference mode and insert selected reference."
184   (interactive)
185   (if (and wl-template-confirm
186            (not (y-or-n-p "Are you sure ? ")))
187       (message "")
188     (delete-window)
189     (kill-buffer wl-template-buffer-name)
190     (when (buffer-live-p wl-template-draft-buffer)
191       (set-buffer wl-template-draft-buffer)
192       (wl-template-apply wl-template)
193       (let ((win (get-buffer-window wl-template-draft-buffer)))
194         (if win (select-window win))))))
195
196 (defun wl-template-insert (name &optional mail-header)
197   "Insert NAME template.
198 Set header-separator is MAIL-HEADER."
199   (let ((template (cdr (assoc name wl-template-alist)))
200         (mail-header-separator (or mail-header
201                                    mail-header-separator)))
202     (when template
203       (if mail-header
204           (insert mail-header-separator "\n"))
205       (wl-draft-config-exec-sub template))))
206
207 (require 'product)
208 (product-provide (provide 'wl-template) (require 'wl-version))
209
210 ;;; wl-template.el ends here