Changes from arch/CVS synchronization
[gnus] / lisp / assistant.el
1 ;;; assistant.el --- guiding users through Emacs setup
2 ;; Copyright (C) 2004 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: util
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile
29   (require 'cl))
30
31 (defvar assistant-readers
32   '(("variable" assistant-variable-reader)
33     ("validate" assistant-sexp-reader)
34     ("result" assistant-list-reader)
35     ("next" assistant-list-reader)
36     ("text" assistant-text-reader)))
37
38 (defface assistant-field-face '((t (:bold t)))
39   "Face used for editable fields."
40   :group 'gnus-article-emphasis)
41
42 ;;; Internal variables
43
44 (defvar assistant-data nil)
45 (defvar assistant-current-node nil)
46 (defvar assistant-previous-node nil)
47 (defvar assistant-widgets nil)
48
49 (defun assistant-parse-buffer ()
50   (let (results command value)
51     (goto-char (point-min))
52     (while (search-forward "@" nil t)
53       (if (not (looking-at "[^ \t\n]+"))
54           (error "Dangling @")
55         (setq command (downcase (match-string 0)))
56         (goto-char (match-end 0)))
57       (setq value
58             (if (looking-at "[ \t]*\n")
59                 (let (start)
60                   (forward-line 1)
61                   (setq start (point))
62                   (unless (re-search-forward (concat "^@end " command) nil t)
63                     (error "No @end %s found" command))
64                   (beginning-of-line)
65                   (prog1
66                       (buffer-substring start (point))
67                     (forward-line 1)))
68               (skip-chars-forward " \t")
69               (prog1
70                   (buffer-substring (point) (line-end-position))
71                 (forward-line 1))))
72       (push (list command (assistant-reader command value))
73             results))
74     (assistant-segment (nreverse results))))
75
76 (defun assistant-text-reader (text)
77   (with-temp-buffer
78     (insert text)
79     (goto-char (point-min))
80     (let ((start (point))
81           (sections nil))
82       (while (re-search-forward "@\\([^{]+\\){\\([^}]+\\)}" nil t)
83         (push (buffer-substring start (match-beginning 0))
84               sections)
85         (push (list (match-string 1) (match-string 2))
86               sections)
87         (setq start (point)))
88       (push (buffer-substring start (point-max))
89             sections)
90       (nreverse sections))))
91
92 ;; Segment the raw assistant data into a list of nodes.
93 (defun assistant-segment (list)
94   (let ((ast nil)
95         (node nil)
96         (title (pop list)))
97     (dolist (elem list)
98       (when (and (equal (car elem) "node")
99                  node)
100         (push (list "save" nil) node)
101         (push (nreverse node) ast)
102         (setq node nil))
103       (push elem node))
104     (when node
105       (push (list "save" nil) node)
106       (push (nreverse node) ast))
107     (cons title (nreverse ast))))
108
109 (defun assistant-reader (command value)
110   (let ((formatter (cadr (assoc command assistant-readers))))
111     (if (not formatter)
112         value
113       (funcall formatter value))))
114
115 (defun assistant-list-reader (value)
116   (car (read-from-string (concat "(" value ")"))))
117
118 (defun assistant-variable-reader (value)
119   (let ((section (car (read-from-string (concat "(" value ")")))))
120     (append section (list 'default))))
121
122 (defun assistant-sexp-reader (value)
123   (if (zerop (length value))
124       nil
125     (car (read-from-string value))))
126
127 (defun assistant-buffer-name (title)
128   (format "*Assistant %s*" title))
129
130 (defun assistant-get (ast command)
131   (cadr (assoc command ast)))
132
133 (defun assistant-set (ast command value)
134   (let ((elem (assoc command ast)))
135     (when elem
136       (setcar (cdr elem) value))))
137
138 (defun assistant-get-list (ast command)
139   (let ((result nil))
140     (dolist (elem ast)
141       (when (equal (car elem) command)
142         (push elem result)))
143     (nreverse result)))
144
145 ;;;###autoload
146 (defun assistant (file)
147   "Assist setting up Emacs based on FILE."
148   (interactive "fAssistant file name: ")
149   (let ((ast
150          (with-temp-buffer
151            (insert-file-contents file)
152            (assistant-parse-buffer))))
153     (pop-to-buffer (assistant-buffer-name (assistant-get ast "title")))
154     (assistant-render ast)))
155
156 (defun assistant-render (ast)
157   (let ((first-node (assistant-get (nth 1 ast) "node")))
158     (set (make-local-variable 'assistant-data) ast)
159     (set (make-local-variable 'assistant-current-node) first-node)
160     (set (make-local-variable 'assistant-previous-node) nil)
161     (assistant-render-node first-node)))
162
163 (defun assistant-find-node (node-name)
164   (let ((ast (cdr assistant-data)))
165     (while (and ast
166                 (not (string= node-name (assistant-get (car ast) "node"))))
167       (pop ast))
168     (car ast)))
169
170 (defun assistant-previous-node-text (node)
171   (format "[ << Go back to %s ]  " node))
172
173 (defun assistant-next-node-text (node)
174   (if node
175       (format "[ Proceed to %s >> ]" node)
176     "[ Finish ]"))
177
178 (defun assistant-set-defaults (node)
179   (dolist (variable (assistant-get-list node "variable"))
180     (setq variable (cadr variable))
181     (when (eq (nth 3 variable) 'default)
182       (setcar (nthcdr 3 variable)
183               (eval (nth 2 variable))))))
184
185 (defun assistant-get-variable (node variable)
186   (let ((variables (assistant-get-list node "variable"))
187         (result nil))
188     (while (and (setq elem (pop variables))
189                 (not result))
190       (setq elem (cadr elem))
191       (when (eq (intern variable) (car elem))
192         (setq result (format "%s" (nth 3 elem)))))
193     result))
194     
195 (defun assistant-set-variable (node variable value)
196   (let ((variables (assistant-get-list node "variable")))
197     (while (setq elem (pop variables))
198       (setq elem (cadr elem))
199       (when (eq (intern variable) (car elem))
200         (setcar (nthcdr 3 elem) value)))))
201     
202 (defun assistant-render-text (text node)
203   (dolist (elem text)
204     (if (stringp elem)
205         (insert elem)
206       (push 
207        (widget-create
208         'editable-field
209         :value-face 'assistant-field-face
210         :assistant-variable (cadr elem)
211         (assistant-get-variable node (cadr elem)))
212        assistant-widgets))))
213
214 (defun assistant-render-node (node-name)
215   (let ((node (assistant-find-node node-name)))
216     (set (make-local-variable 'assistant-widgets) nil)
217     (assistant-set-defaults node)
218     (setq assistant-current-node node-name)
219     (erase-buffer)
220     (insert (cadar assistant-data) "\n\n")
221     (insert node-name "\n\n")
222     (assistant-render-text (assistant-get node "text") node)
223     (insert "\n\n")
224     (when assistant-previous-node
225       (assistant-node-button 'previous assistant-previous-node))
226     (assistant-node-button 'next (assistant-find-next-node))
227     (insert "\n")))
228
229 (defun assistant-node-button (type node)
230   (let ((text (if (eq type 'next)
231                   (assistant-next-node-text node)
232                 (assistant-previous-node-text node))))
233     (widget-create
234      'push-button
235      :assistant-node node
236      :assistant-type type
237      :notify (lambda (widget &rest ignore)
238                (let* ((node (widget-get widget :assistant-node))
239                       (type (widget-get widget :assistant-type)))
240                  (when (eq type 'next)
241                    (assistant-get-widget-values)
242                    (assistant-validate))
243                  (if (null node)
244                      (assistant-finish)
245                    (assistant-render-node node))))
246      text)
247     (use-local-map widget-keymap)))
248
249 (defun assistant-validate-types (node)
250   (dolist (variable (assistant-get-list node "variable"))
251     (setq variable (cadr variable))
252     (let ((type (nth 1 variable))
253           (value (nth 3 variable)))
254       (when 
255           (cond
256            ((eq type :number)
257             (string-match "[^0-9]" value))
258            (t
259             nil))
260         (error "%s is not of type %s: %s"
261                (car variable) type value)))))
262
263 (defun assistant-get-widget-values ()
264   (let ((node (assistant-find-node assistant-current-node)))
265     (dolist (widget assistant-widgets)
266       (assistant-set-variable
267        node (widget-get widget :assistant-variable)
268        (widget-value widget)))))
269
270 (defun assistant-validate ()
271   (let* ((node (assistant-find-node assistant-current-node))
272          (validation (assistant-get node "validate"))
273          result)
274     (assistant-validate-types node)
275     (when validation
276       (when (setq result (assistant-eval validation node))
277         (unless (y-or-n-p (format "Error: %s.  Continue? " result))
278           (error "%s" result))))
279     (assistant-set node "save" t)))
280
281 (defun assistant-find-next-node ()
282   (let* ((node (assistant-find-node assistant-current-node))
283          (nexts (assistant-get-list node "next"))
284          next elem)
285     (while (and (setq elem (pop nexts))
286                 (not next))
287       (when (assistant-eval (car elem) node)
288         (setq next (cadr elem))))
289     next))
290       
291 (defun assistant-eval (form node)
292   (let ((bindings nil))
293     (dolist (variable (assistant-get-list node "variable"))
294       (setq variable (cadr variable))
295       (push (list (car variable) (nth 3 variable))
296             bindings))
297     (eval
298      `(let ,bindings
299         ,form))))
300
301 (defun assistant-finish ()
302   (let ((results nil)
303         result)
304     (dolist (node (cdr assistant-data))
305       (when (assistant-get node "save")
306         (setq result (assistant-get node "result"))
307         (push (list (car result)
308                     (assistant-eval (cadr result) node))
309               results)))
310     (message "Results: %s"
311              (nreverse results))))
312
313 (provide 'assistant)
314
315 ;;; arch-tag: 0404bfa2-9226-4611-8d3f-335c2416175b
316 ;;; assistant.el ends here