]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-scaffold.el
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / misc / fuel / fuel-scaffold.el
1 ;;; fuel-scaffold.el -- interaction with tools.scaffold
2
3 ;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
5
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Sun Jan 11, 2009 18:40
9
10 ;;; Comentary:
11
12 ;; Utilities for creating new vocabulary files and other boilerplate.
13 ;; Mainly, an interface to Factor's tools.scaffold.
14
15 ;;; Code:
16
17 (require 'fuel-eval)
18 (require 'fuel-edit)
19 (require 'fuel-syntax)
20 (require 'fuel-base)
21
22 \f
23 ;;; Customisation:
24
25 (defgroup fuel-scaffold nil
26   "Options for FUEL's scaffolding."
27   :group 'fuel)
28
29 (defcustom fuel-scaffold-developer-name nil
30   "The name to be inserted as yours in scaffold templates."
31   :type '(choice string
32                  (const :tag "Factor's value for developer-name" nil))
33   :group 'fuel-scaffold)
34
35 \f
36 ;;; Auxiliary functions:
37
38 (defun fuel-scaffold--vocab-roots ()
39   (let ((cmd '(:fuel* (vocab-roots get :get) "fuel")))
40     (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
41
42 (defun fuel-scaffold--dev-name ()
43   (or fuel-scaffold-developer-name
44       (let ((cmd '(:fuel* (developer-name get :get) "fuel")))
45         (fuel-eval--retort-result (fuel-eval--send/wait cmd)))
46       "Your name"))
47
48 (defun fuel-scaffold--first-vocab ()
49   (goto-char (point-min))
50   (re-search-forward fuel-syntax--current-vocab-regex nil t))
51
52 (defsubst fuel-scaffold--vocab (file)
53   (save-excursion
54     (set-buffer (find-file-noselect file))
55     (fuel-scaffold--first-vocab)
56     (fuel-syntax--current-vocab)))
57
58 (defconst fuel-scaffold--tests-header-format
59   "! Copyright (C) %s %s
60 ! See http://factorcode.org/license.txt for BSD license.
61 USING: %s tools.test ;
62 IN: %s
63 ")
64
65 (defsubst fuel-scaffold--check-auto (var)
66   (and var (or (eq var 'always) (y-or-n-p "Insert template? "))))
67
68 (defun fuel-scaffold--tests (parent)
69   (when (and parent (fuel-scaffold--check-auto fuel-scaffold-test-autoinsert-p))
70     (let ((year (format-time-string "%Y"))
71           (name (fuel-scaffold--dev-name))
72           (vocab (fuel-scaffold--vocab parent)))
73       (insert (format fuel-scaffold--tests-header-format
74                       year name vocab vocab))
75       t)))
76
77 (defsubst fuel-scaffold--create-docs (vocab)
78   (let ((cmd `(:fuel* (,vocab ,fuel-scaffold-developer-name fuel-scaffold-help)
79                       "fuel")))
80     (fuel-eval--send/wait cmd)))
81
82 (defsubst fuel-scaffold--create-tests (vocab)
83   (let ((cmd `(:fuel* (,vocab ,fuel-scaffold-developer-name fuel-scaffold-tests)
84                       "fuel")))
85     (fuel-eval--send/wait cmd)))
86
87 (defsubst fuel-scaffold--create-authors (vocab)
88   (let ((cmd `(:fuel* (,vocab ,fuel-scaffold-developer-name fuel-scaffold-authors) "fuel")))
89     (fuel-eval--send/wait cmd)))
90
91 (defsubst fuel-scaffold--create-tags (vocab tags)
92   (let ((cmd `(:fuel* (,vocab ,tags fuel-scaffold-tags) "fuel")))
93     (fuel-eval--send/wait cmd)))
94
95 (defsubst fuel-scaffold--create-summary (vocab summary)
96   (let ((cmd `(:fuel* (,vocab ,summary fuel-scaffold-summary) "fuel")))
97     (fuel-eval--send/wait cmd)))
98
99 (defun fuel-scaffold--help (parent)
100   (when (and parent (fuel-scaffold--check-auto fuel-scaffold-help-autoinsert-p))
101     (let* ((ret (fuel-scaffold--create-docs (fuel-scaffold--vocab parent)))
102            (file (fuel-eval--retort-result ret)))
103       (when file
104         (revert-buffer t t t)
105         (when (and fuel-scaffold-help-header-only-p
106                    (fuel-scaffold--first-vocab))
107           (delete-region (1+ (point)) (point-max))
108           (save-buffer))
109         (message "Inserting template ... done."))
110       (goto-char (point-min)))))
111
112 (defun fuel-scaffold--maybe-insert ()
113   (ignore-errors
114     (or (fuel-scaffold--tests (factor-mode--in-tests))
115         (fuel-scaffold--help (factor-mode--in-docs)))))
116
117 \f
118 ;;; User interface:
119
120 (defun fuel-scaffold-vocab (&optional other-window name-hint root-hint)
121   "Creates a directory in the given root for a new vocabulary and
122 adds source and authors.txt files. Prompts the user for optional summary,
123 tags, help, and test file creation.
124
125 You can configure `fuel-scaffold-developer-name' (set by default to
126 `user-full-name') for the name to be inserted in the generated files."
127   (interactive)
128   (let* ((name (read-string "Vocab name: " name-hint))
129          (root (completing-read "Vocab root: "
130                                 (fuel-scaffold--vocab-roots)
131                                 nil t (or root-hint "resource:")))
132          (summary (read-string "Vocab summary (empty for none): "))
133          (tags (read-string "Vocab tags (empty for none): "))
134          (help (y-or-n-p "Scaffold help? "))
135          (tests (y-or-n-p "Scaffold tests? "))
136          (cmd `(:fuel* ((,root ,name ,fuel-scaffold-developer-name)
137                         (fuel-scaffold-vocab)) "fuel"))
138          (ret (fuel-eval--send/wait cmd))
139          (file (fuel-eval--retort-result ret)))
140     (unless file
141       (error "Error creating vocab (%s)" (car (fuel-eval--retort-error ret))))
142     (when (not (equal "" summary))
143       (fuel-scaffold--create-summary name summary))
144     (when (not (equal "" tags))
145       (fuel-scaffold--create-tags name tags))
146     (when help
147          (fuel-scaffold--create-docs name))
148     (when tests
149          (fuel-scaffold--create-tests name))
150     (if other-window (find-file-other-window file) (find-file file))
151     (goto-char (point-max))
152     name))
153
154 (defun fuel-scaffold-help (&optional arg)
155   "Creates, if it does not already exist, a help file with
156 scaffolded help for each word in the current vocabulary.
157
158 With prefix argument, ask for the vocabulary name.
159 You can configure `fuel-scaffold-developer-name' (set by default to
160 `user-full-name') for the name to be inserted in the generated file."
161   (interactive "P")
162   (let* ((vocab (or (and (not arg) (fuel-syntax--current-vocab))
163                     (fuel-completion--read-vocab nil)))
164          (ret (fuel-scaffold--create-docs vocab))
165          (file (fuel-eval--retort-result ret)))
166         (unless file
167           (error "Error creating help file" (car (fuel-eval--retort-error ret))))
168         (find-file file)))
169
170 (defun fuel-scaffold-tests (&optional arg)
171   "Creates, if it does not already exist, a tests file for the current vocabulary.
172
173 With prefix argument, ask for the vocabulary name.
174 You can configure `fuel-scaffold-developer-name' (set by default to
175 `user-full-name') for the name to be inserted in the generated file."
176   (interactive "P")
177   (let* ((vocab (or (and (not arg) (fuel-syntax--current-vocab))
178                     (fuel-completion--read-vocab nil)))
179          (ret (fuel-scaffold--create-tests vocab))
180          (file (fuel-eval--retort-result ret)))
181         (unless file
182           (error "Error creating tests file" (car (fuel-eval--retort-error ret))))
183         (find-file file)))
184
185 (defun fuel-scaffold-authors (&optional arg)
186   "Creates, if it does not already exist, an authors file for the current vocabulary.
187
188 With prefix argument, ask for the vocabulary name.
189 You can configure `fuel-scaffold-developer-name' (set by default to
190 `user-full-name') for the name to be inserted in the generated file."
191   (interactive "P")
192   (let* ((vocab (or (and (not arg) (fuel-syntax--current-vocab))
193                     (fuel-completion--read-vocab nil)))
194          (ret (fuel-scaffold--create-authors vocab))
195          (file (fuel-eval--retort-result ret)))
196         (unless file
197           (error "Error creating authors file" (car (fuel-eval--retort-error ret))))
198         (find-file file)))
199
200 (defun fuel-scaffold-tags (&optional arg)
201   "Creates, if it does not already exist, a tags file for the current vocabulary."
202   (interactive "P")
203   (let* ((vocab (or (and (not arg) (fuel-syntax--current-vocab))
204                     (fuel-completion--read-vocab nil)))
205          (tags (read-string "Tags: "))
206          (ret (fuel-scaffold--create-tags vocab tags))
207          (file (fuel-eval--retort-result ret)))
208         (unless file
209           (error "Error creating tags file" (car (fuel-eval--retort-error ret))))
210         (find-file file)))
211
212 (defun fuel-scaffold-summary (&optional arg)
213   "Creates, if it does not already exist, a summary file for the current vocabulary."
214   (interactive "P")
215   (let* ((vocab (or (and (not arg ) (fuel-syntax--current-vocab))
216                     (fuel-completion--read-vocab nil)))
217          (summary (read-string "Summary: "))
218          (ret (fuel-scaffold--create-summary vocab summary))
219          (file (fuel-eval--retort-result ret)))
220         (unless file
221           (error "Error creating summary file" (car (fuel-eval--retort-error ret))))
222         (find-file file)))
223
224 \f
225 (provide 'fuel-scaffold)
226 ;;; fuel-scaffold.el ends here