(assistant-render-node): Fix up rendering and
[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       (let ((start (point)))
207         (push 
208          (widget-create
209           'editable-field
210           :value-face 'assistant-field-face
211           :assistant-variable (cadr elem)
212           (assistant-get-variable node (cadr elem)))
213          assistant-widgets)
214         ;; The editable-field widget apparently inserts a newline;
215         ;; remove it.
216         (delete-char -1)
217         (add-text-properties start (point)
218                              (list
219                               'bold t
220                               'face 'assistant-field-face
221                               'not-read-only t))))))
222
223 (defun assistant-render-node (node-name)
224   (let ((node (assistant-find-node node-name))
225         (inhibit-read-only t)
226         (buffer-read-only nil))
227     (set (make-local-variable 'assistant-widgets) nil)
228     (assistant-set-defaults node)
229     (setq assistant-current-node node-name)
230     (erase-buffer)
231     (insert (cadar assistant-data) "\n\n")
232     (insert node-name "\n\n")
233     (assistant-render-text (assistant-get node "text") node)
234     (insert "\n\n")
235     (when assistant-previous-node
236       (assistant-node-button 'previous assistant-previous-node))
237     (assistant-node-button 'next (assistant-find-next-node))
238     (insert "\n")
239     (assistant-make-read-only)))
240
241 (defun assistant-make-read-only ()
242   (let ((start (point-min))
243         end)
244     (while (setq end (text-property-any start (point-max) 'not-read-only t))
245       (put-text-property start end 'read-only t)
246       (while (get-text-property end 'not-read-only)
247         (incf end))
248       (setq start end))
249     (put-text-property start (point-max) 'read-only t)))
250
251 (defun assistant-node-button (type node)
252   (let ((text (if (eq type 'next)
253                   (assistant-next-node-text node)
254                 (assistant-previous-node-text node))))
255     (widget-create
256      'push-button
257      :assistant-node node
258      :assistant-type type
259      :notify (lambda (widget &rest ignore)
260                (let* ((node (widget-get widget :assistant-node))
261                       (type (widget-get widget :assistant-type)))
262                  (when (eq type 'next)
263                    (assistant-get-widget-values)
264                    (assistant-validate))
265                  (if (null node)
266                      (assistant-finish)
267                    (assistant-render-node node))))
268      text)
269     (use-local-map widget-keymap)))
270
271 (defun assistant-validate-types (node)
272   (dolist (variable (assistant-get-list node "variable"))
273     (setq variable (cadr variable))
274     (let ((type (nth 1 variable))
275           (value (nth 3 variable)))
276       (when 
277           (cond
278            ((eq type :number)
279             (string-match "[^0-9]" value))
280            (t
281             nil))
282         (error "%s is not of type %s: %s"
283                (car variable) type value)))))
284
285 (defun assistant-get-widget-values ()
286   (let ((node (assistant-find-node assistant-current-node)))
287     (dolist (widget assistant-widgets)
288       (assistant-set-variable
289        node (widget-get widget :assistant-variable)
290        (widget-value widget)))))
291
292 (defun assistant-validate ()
293   (let* ((node (assistant-find-node assistant-current-node))
294          (validation (assistant-get node "validate"))
295          result)
296     (assistant-validate-types node)
297     (when validation
298       (when (setq result (assistant-eval validation node))
299         (unless (y-or-n-p (format "Error: %s.  Continue? " result))
300           (error "%s" result))))
301     (assistant-set node "save" t)))
302
303 (defun assistant-find-next-node ()
304   (let* ((node (assistant-find-node assistant-current-node))
305          (nexts (assistant-get-list node "next"))
306          next elem)
307     (while (and (setq elem (pop nexts))
308                 (not next))
309       (when (assistant-eval (car elem) node)
310         (setq next (cadr elem))))
311     next))
312       
313 (defun assistant-eval (form node)
314   (let ((bindings nil))
315     (dolist (variable (assistant-get-list node "variable"))
316       (setq variable (cadr variable))
317       (push (list (car variable) (nth 3 variable))
318             bindings))
319     (eval
320      `(let ,bindings
321         ,form))))
322
323 (defun assistant-finish ()
324   (let ((results nil)
325         result)
326     (dolist (node (cdr assistant-data))
327       (when (assistant-get node "save")
328         (setq result (assistant-get node "result"))
329         (push (list (car result)
330                     (assistant-eval (cadr result) node))
331               results)))
332     (message "Results: %s"
333              (nreverse results))))
334
335 (provide 'assistant)
336
337 ;;; arch-tag: 0404bfa2-9226-4611-8d3f-335c2416175b
338 ;;; assistant.el ends here