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