Merged beta branch.
[elisp/wanderlust.git] / wl / wl-template.el
1 ;;; wl-template.el -- Draft template feature for Wanderlust.
2
3 ;; Copyright 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
4 ;;                          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
32 ;; Variables
33
34 (defvar wl-template-default-name "default")
35 (defvar wl-template-buffer-name "*WL-Template*")
36 (defvar wl-template-mode-map nil)
37
38 (defvar wl-template nil)
39 (defvar wl-template-cur-num 0)
40 (defvar wl-template-max-num 0)
41 (defvar wl-template-draft-buffer nil)
42
43 ;;; Code
44
45 (if wl-template-mode-map
46     nil
47   (setq wl-template-mode-map (make-sparse-keymap))
48   (define-key wl-template-mode-map "p"     'wl-template-prev)
49   (define-key wl-template-mode-map "n"     'wl-template-next)
50   (define-key wl-template-mode-map "q"     'wl-template-abort)
51   (define-key wl-template-mode-map "\r"    'wl-template-set)
52   (define-key wl-template-mode-map "\n"    'wl-template-set))
53
54 (defun wl-template-apply (name)
55   (let (template)
56     (when name
57       (if (string= name "")
58           (setq name wl-template-default-name))
59       (when (setq template (cdr (assoc name wl-template-alist)))
60         (save-excursion
61           (setq wl-draft-config-variables
62                 (elmo-uniq-list
63                  (nconc wl-draft-config-variables
64                         (save-excursion
65                           (wl-draft-config-exec-sub template)))))
66           ;; rehighlight
67           (if wl-highlight-body-too
68               (let ((beg (point-min))
69                     (end (point-max)))
70                 (put-text-property beg end 'face nil)
71                 (wl-highlight-message beg end t))))))))
72
73 (defun wl-template-mode ()
74   "Major mode for Wanderlust template.
75 See info under Wanderlust for full documentation.
76
77 \\{wl-template-mode}
78
79 Enterring WL-Template mode calls the value of `wl-template-mode-hook'."
80   (kill-all-local-variables)
81   (setq mode-name "Wl-Template"
82         major-mode 'wl-template-mode)
83   (use-local-map wl-template-mode-map)
84   (setq buffer-read-only t)
85   (run-hooks 'wl-template-mode-hook))
86
87 (defun wl-template-select ()
88   (interactive)
89   (if (not wl-template-visible-select)
90       (wl-template-apply
91        (completing-read (format "Template (%s): " wl-template-default-name)
92                         wl-template-alist))
93     (let* ((begin wl-template-default-name)
94            (work wl-template-alist))
95       (if (and begin (cdr (assoc begin wl-template-alist)))
96           (while (not (string= (car (car work)) begin))
97             (setq wl-template-cur-num (1+ wl-template-cur-num))
98             (setq work (cdr work))))
99       (setq wl-template nil
100             wl-template-cur-num 0
101             wl-template-max-num (length wl-template-alist))
102       (setq wl-template-draft-buffer (current-buffer))
103       (if (get-buffer-window wl-template-buffer-name)
104           (select-window (get-buffer-window wl-template-buffer-name))
105         (let* ((cur-win (selected-window))
106                (size (min
107                       (- (window-height cur-win)
108                          window-min-height 1)
109                       (- (window-height cur-win)
110                          (max window-min-height
111                               (1+ wl-template-buffer-lines))))))
112           (split-window cur-win (if (> size 0) size window-min-height))
113           ;; goto the bottom of the two...
114           (select-window (next-window))
115           ;; make it display...
116           (let ((pop-up-windows nil))
117             (switch-to-buffer (get-buffer-create wl-template-buffer-name)))))
118       (set-buffer wl-template-buffer-name)
119       (wl-template-mode)
120       (wl-template-show))))
121
122 (defun wl-template-show (&optional arg)
123   "Show reference INDEX in wl-template-alist."
124   (save-excursion
125     (set-buffer wl-template-buffer-name)
126     (let ((buffer-read-only nil)
127           (mail-header-separator  "--header separater--"))
128       (erase-buffer)
129       (goto-char (point-min))
130       (wl-template-insert
131        (setq wl-template (car (nth wl-template-cur-num wl-template-alist)))
132        mail-header-separator)
133       (wl-highlight-message (point-min) (point-max) t)
134       (and wl-highlight-x-face-func
135            (funcall
136             wl-highlight-x-face-func
137             (point-min) (re-search-forward mail-header-separator nil t)))
138       (setq mode-line-process (concat ":" wl-template))
139       (set-buffer-modified-p nil))))
140
141 (defun wl-template-next ()
142   "Display next reference in other buffer."
143   (interactive)
144   (if (eq wl-template-max-num
145           (setq wl-template-cur-num (1+ wl-template-cur-num)))
146       (setq wl-template-cur-num 0))
147   (wl-template-show))
148
149 (defun wl-template-prev ()
150   "Display previous reference in other buffer."
151   (interactive)
152   (setq wl-template-cur-num (if (eq wl-template-cur-num 0)
153                                 (1- wl-template-max-num)
154                               (1- wl-template-cur-num)))
155   (wl-template-show))
156
157 (defun wl-template-abort ()
158   "Exit from electric reference mode without inserting reference."
159   (interactive)
160   (setq wl-template nil)
161   (delete-window)
162   (kill-buffer wl-template-buffer-name)
163   (when (buffer-live-p wl-template-draft-buffer)
164     (set-buffer wl-template-draft-buffer)
165     (let ((win (get-buffer-window wl-template-draft-buffer)))
166       (if win (select-window win)))))
167
168 (defun wl-template-set ()
169   "Exit from electric reference mode and insert selected reference."
170   (interactive)
171   (if (and wl-template-confirm
172            (not (y-or-n-p "Are you sure ? ")))
173       (message "")
174     (delete-window)
175     (kill-buffer wl-template-buffer-name)
176     (when (buffer-live-p wl-template-draft-buffer)
177       (set-buffer wl-template-draft-buffer)
178       (wl-template-apply wl-template)
179       (let ((win (get-buffer-window wl-template-draft-buffer)))
180         (if win (select-window win))))))
181
182 (defun wl-template-insert (name &optional mail-header)
183   (let ((template (cdr (assoc name wl-template-alist)))
184         (mail-header-separator (or mail-header
185                                    mail-header-separator)))
186     (when template
187       (if mail-header
188           (insert mail-header-separator "\n"))
189       (wl-draft-config-exec-sub template))))
190
191 (require 'product)
192 (product-provide (provide 'wl-template) (require 'wl-version))
193
194 ;;; wl-template.el ends here