* wl-template.el (wl-template-select): Added optional argument.
[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
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   "Apply NAME template to draft."
56   (let (template)
57     (when name
58       (if (string= name "")
59           (setq name wl-template-default-name))
60       (when (setq template (cdr (assoc name wl-template-alist)))
61         (save-excursion
62           (setq wl-draft-config-variables
63                 (elmo-uniq-list
64                  (nconc wl-draft-config-variables
65                         (save-excursion
66                           (wl-draft-config-exec-sub template)))))
67           ;; rehighlight
68           (if wl-highlight-body-too
69               (let ((beg (point-min))
70                     (end (point-max)))
71                 (put-text-property beg end 'face nil)
72                 (wl-highlight-message beg end t))))))))
73
74 (defun wl-template-mode ()
75   "Major mode for Wanderlust template.
76 See info under Wanderlust for full documentation.
77
78 \\{wl-template-mode}
79
80 Entering WL-Template mode calls the value of `wl-template-mode-hook'."
81   (kill-all-local-variables)
82   (setq mode-name "Wl-Template"
83         major-mode 'wl-template-mode)
84   (use-local-map wl-template-mode-map)
85   (setq buffer-read-only t)
86   (run-hooks 'wl-template-mode-hook))
87
88 (defun wl-template-select (&optional arg)
89   "Select template from `wl-template-alist'."
90   (interactive "P")
91   (if (not (if arg
92                (not wl-template-visible-select)
93              wl-template-visible-select))
94       (wl-template-apply
95        (completing-read (format "Template (%s): " wl-template-default-name)
96                         wl-template-alist))
97     (let* ((begin wl-template-default-name)
98            (work wl-template-alist))
99       (if (and begin (cdr (assoc begin wl-template-alist)))
100           (while (not (string= (car (car work)) begin))
101             (setq wl-template-cur-num (1+ wl-template-cur-num))
102             (setq work (cdr work))))
103       (setq wl-template nil
104             wl-template-cur-num 0
105             wl-template-max-num (length wl-template-alist))
106       (setq wl-template-draft-buffer (current-buffer))
107       (if (get-buffer-window wl-template-buffer-name)
108           (select-window (get-buffer-window wl-template-buffer-name))
109         (let* ((cur-win (selected-window))
110                (size (min
111                       (- (window-height cur-win)
112                          window-min-height 1)
113                       (- (window-height cur-win)
114                          (max window-min-height
115                               (1+ wl-template-buffer-lines))))))
116           (split-window cur-win (if (> size 0) size window-min-height))
117           ;; goto the bottom of the two...
118           (select-window (next-window))
119           ;; make it display...
120           (let ((pop-up-windows nil))
121             (switch-to-buffer (get-buffer-create wl-template-buffer-name)))))
122       (set-buffer wl-template-buffer-name)
123       (wl-template-mode)
124       (wl-template-show))))
125
126 (defun wl-template-show (&optional arg)
127   "Show reference INDEX in `wl-template-alist'.
128 ARG is ignored."                        ; ARG ignored this version (?)
129   (save-excursion
130     (set-buffer wl-template-buffer-name)
131     (let ((buffer-read-only nil)
132           (mail-header-separator  "--header separater--"))
133       (erase-buffer)
134       (goto-char (point-min))
135       (wl-template-insert
136        (setq wl-template (car (nth wl-template-cur-num wl-template-alist)))
137        mail-header-separator)
138       (wl-highlight-message (point-min) (point-max) t)
139       (when wl-highlight-x-face-function
140         (funcall wl-highlight-x-face-function
141                  (point-min) (re-search-forward mail-header-separator nil t)))
142       (setq mode-line-process (concat ":" wl-template))
143       (set-buffer-modified-p nil))))
144
145 (defun wl-template-next ()
146   "Display next reference in other buffer."
147   (interactive)
148   (if (= wl-template-max-num
149          (setq wl-template-cur-num (1+ wl-template-cur-num)))
150       (setq wl-template-cur-num 0))
151   (wl-template-show))
152
153 (defun wl-template-prev ()
154   "Display previous reference in other buffer."
155   (interactive)
156   (setq wl-template-cur-num (if (zerop wl-template-cur-num)
157                                 (1- wl-template-max-num)
158                               (1- wl-template-cur-num)))
159   (wl-template-show))
160
161 (defun wl-template-abort ()
162   "Exit from electric reference mode without inserting reference."
163   (interactive)
164   (setq wl-template nil)
165   (delete-window)
166   (kill-buffer wl-template-buffer-name)
167   (when (buffer-live-p wl-template-draft-buffer)
168     (set-buffer wl-template-draft-buffer)
169     (let ((win (get-buffer-window wl-template-draft-buffer)))
170       (if win (select-window win)))))
171
172 (defun wl-template-set ()
173   "Exit from electric reference mode and insert selected reference."
174   (interactive)
175   (if (and wl-template-confirm
176            (not (y-or-n-p "Are you sure ? ")))
177       (message "")
178     (delete-window)
179     (kill-buffer wl-template-buffer-name)
180     (when (buffer-live-p wl-template-draft-buffer)
181       (set-buffer wl-template-draft-buffer)
182       (wl-template-apply wl-template)
183       (let ((win (get-buffer-window wl-template-draft-buffer)))
184         (if win (select-window win))))))
185
186 (defun wl-template-insert (name &optional mail-header)
187   "Insert NAME template.
188 Set header-separator is MAIL-HEADER."
189   (let ((template (cdr (assoc name wl-template-alist)))
190         (mail-header-separator (or mail-header
191                                    mail-header-separator)))
192     (when template
193       (if mail-header
194           (insert mail-header-separator "\n"))
195       (wl-draft-config-exec-sub template))))
196
197 (require 'product)
198 (product-provide (provide 'wl-template) (require 'wl-version))
199
200 ;;; wl-template.el ends here