1 ;;; assistant.el --- guiding users through Emacs setup
2 ;; Copyright (C) 2004 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
31 (defvar assistant-readers
32 '(("variable" assistant-variable-reader)
33 ("validate" assistant-sexp-reader)
34 ("result" assistant-list-reader)
35 ("next" assistant-list-reader)))
37 ;;; Internal variables
39 (defvar assistant-data nil)
40 (defvar assistant-current-node nil)
41 (defvar assistant-previous-node nil)
43 (defun assistant-parse-buffer ()
44 (let (results command value)
45 (goto-char (point-min))
46 (while (search-forward "@" nil t)
47 (if (not (looking-at "[^ \t\n]+"))
49 (setq command (downcase (match-string 0)))
50 (goto-char (match-end 0)))
52 (if (looking-at "[ \t]*\n")
56 (unless (re-search-forward (concat "^@end " command) nil t)
57 (error "No @end %s found" command))
60 (buffer-substring start (point))
62 (skip-chars-forward " \t")
64 (buffer-substring (point) (line-end-position))
66 (push (list command (assistant-reader command value))
68 (assistant-segment (nreverse results))))
70 ;; Segment the raw assistant data into a list of nodes.
71 (defun assistant-segment (list)
76 (when (and (equal (car elem) "node")
78 (push (list "save" nil) node)
79 (push (nreverse node) ast)
83 (push (list "save" nil) node)
84 (push (nreverse node) ast))
85 (cons title (nreverse ast))))
87 (defun assistant-reader (command value)
88 (let ((formatter (cadr (assoc command assistant-readers))))
91 (funcall formatter value))))
93 (defun assistant-list-reader (value)
94 (car (read-from-string (concat "(" value ")"))))
96 (defun assistant-variable-reader (value)
97 (let ((section (car (read-from-string (concat "(" value ")")))))
98 (append section (list 'default))))
100 (defun assistant-sexp-reader (value)
101 (if (zerop (length value))
103 (car (read-from-string value))))
105 (defun assistant-buffer-name (title)
106 (format "*Assistant %s*" title))
108 (defun assistant-get (ast command)
109 (cadr (assoc command ast)))
111 (defun assistant-set (ast command value)
112 (let ((elem (assoc command ast)))
114 (setcar (cdr elem) value))))
116 (defun assistant-get-list (ast command)
119 (when (equal (car elem) command)
123 (defun assistant (file)
124 "Assist setting up Emacs based on FILE."
125 (interactive "fAssistant file name: ")
128 (insert-file-contents file)
129 (assistant-parse-buffer))))
130 (pop-to-buffer (assistant-buffer-name (assistant-get ast "title")))
131 (assistant-render ast)))
133 (defun assistant-render (ast)
134 (let ((first-node (assistant-get (nth 1 ast) "node")))
135 (set (make-local-variable 'assistant-data) ast)
136 (set (make-local-variable 'assistant-current-node) first-node)
137 (set (make-local-variable 'assistant-previous-node) nil)
138 (assistant-render-node first-node)))
140 (defun assistant-find-node (node-name)
141 (let ((ast (cdr assistant-data)))
143 (not (string= node-name (assistant-get (car ast) "node"))))
147 (defun assistant-previous-node-text (node)
148 (format "[ << Go back to %s ] " node))
150 (defun assistant-next-node-text (node)
152 (format "[ Proceed to %s >> ]" node)
155 (defun assistant-set-defaults (node)
156 (dolist (variable (assistant-get-list node "variable"))
157 (setq variable (cadr variable))
158 (when (eq (nth 3 variable) 'default)
159 (setcar (nthcdr 3 variable)
160 (eval (nth 2 variable))))))
162 (defun assistant-render-node (node-name)
163 (let ((node (assistant-find-node node-name)))
164 (assistant-set-defaults node)
165 (setq assistant-current-node node-name)
167 (insert (cadar assistant-data) "\n\n")
168 (insert node-name "\n\n")
169 (insert (assistant-get node "text") "\n\n")
170 (when assistant-previous-node
171 (assistant-node-button 'previous assistant-previous-node))
172 (assistant-node-button 'next (assistant-find-next-node))
175 (defun assistant-node-button (type node)
176 (let ((text (if (eq type 'next)
177 (assistant-next-node-text node)
178 (assistant-previous-node-text node))))
183 :notify (lambda (widget &rest ignore)
184 (let* ((node (widget-get widget :assistant-node))
185 (type (widget-get widget :assistant-type)))
186 (when (eq type 'next)
187 (assistant-validate))
190 (assistant-render-node node))))
192 (use-local-map widget-keymap)))
194 (defun assistant-validate-types (node)
195 (dolist (variable (assistant-get-list node "variable"))
196 (setq variable (cadr variable))
197 (let ((type (nth 1 variable))
198 (value (nth 3 variable)))
202 (not (numberp value)))
205 (error "%s is not of type %s: %s"
206 (car variable) type value)))))
208 (defun assistant-validate ()
209 (let* ((node (assistant-find-node assistant-current-node))
210 (validation (assistant-get node "validate"))
212 (assistant-validate-types node)
214 (when (setq result (assistant-eval validation node))
215 (unless (y-or-n-p (format "Error: %s. Continue? " result))
216 (error "%s" result))))
217 (assistant-set node "save" t)))
219 (defun assistant-find-next-node ()
220 (let* ((node (assistant-find-node assistant-current-node))
221 (nexts (assistant-get-list node "next"))
223 (while (and (setq elem (pop nexts))
225 (when (assistant-eval (car elem) node)
226 (setq next (cadr elem))))
229 (defun assistant-eval (form node)
230 (let ((bindings nil))
231 (dolist (variable (assistant-get-list node "variable"))
232 (setq variable (cadr variable))
233 (push (list (car variable) (nth 3 variable))
239 (defun assistant-finish ()
242 (dolist (node (cdr assistant-data))
243 (when (assistant-get node "save")
244 (setq result (assistant-get node "result"))
245 (push (list (car result)
246 (assistant-eval (cadr result) node))
248 (message "Results: %s"
249 (nreverse results))))