]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-scaffold.el
Add scaffolding words for tags, summary and authors and hook these up to FUEL. Modifi...
[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 (defsubst fuel-scaffold--creaet-
100
101 (defun fuel-scaffold--help (parent)
102   (when (and parent (fuel-scaffold--check-auto fuel-scaffold-help-autoinsert-p))
103     (let* ((ret (fuel-scaffold--create-docs (fuel-scaffold--vocab parent)))
104            (file (fuel-eval--retort-result ret)))
105       (when file
106         (revert-buffer t t t)
107         (when (and fuel-scaffold-help-header-only-p
108                    (fuel-scaffold--first-vocab))
109           (delete-region (1+ (point)) (point-max))
110           (save-buffer))
111         (message "Inserting template ... done."))
112       (goto-char (point-min)))))
113
114 (defun fuel-scaffold--maybe-insert ()
115   (ignore-errors
116     (or (fuel-scaffold--tests (factor-mode--in-tests))
117         (fuel-scaffold--help (factor-mode--in-docs)))))
118
119 \f
120 ;;; User interface:
121
122 (defun fuel-scaffold-vocab (&optional other-window name-hint root-hint)
123   "Creates a directory in the given root for a new vocabulary and
124 adds source and authors.txt files. Prompts the user for optional summary,
125 tags, help, and test file creation.
126
127 You can configure `fuel-scaffold-developer-name' (set by default to
128 `user-full-name') for the name to be inserted in the generated files."
129   (interactive)
130   (let* ((name (read-string "Vocab name: " name-hint))
131          (root (completing-read "Vocab root: "
132                                 (fuel-scaffold--vocab-roots)
133                                 nil t (or root-hint "resource:")))
134          (summary (read-string "Vocab summary (empty for none): "))
135          (tags (read-string "Vocab tags (empty for none): "))
136          (help (y-or-n-p "Scaffold help? "))
137          (tests (y-or-n-p "Scaffold tests? "))
138          (cmd `(:fuel* ((,root ,name ,fuel-scaffold-developer-name)
139                         (fuel-scaffold-vocab)) "fuel"))
140          (ret (fuel-eval--send/wait cmd))
141          (file (fuel-eval--retort-result ret)))
142     (unless file
143       (error "Error creating vocab (%s)" (car (fuel-eval--retort-error ret))))
144     (when (not (equal "" summary))
145       (fuel-scaffold--create-summary name summary))
146     (when (not (equal "" tags))
147       (fuel-scaffold--create-tags name tags))
148     (when help
149          (fuel-scaffold--create-docs name))
150     (when tests
151          (fuel-scaffold--create-tests name))
152     (if other-window (find-file-other-window file) (find-file file))
153     (goto-char (point-max))
154     name))
155
156 (defun fuel-scaffold-help (&optional arg)
157   "Creates, if it does not already exist, a help file with
158 scaffolded help for each word in the current vocabulary.
159
160 With prefix argument, ask for the vocabulary name.
161 You can configure `fuel-scaffold-developer-name' (set by default to
162 `user-full-name') for the name to be inserted in the generated file."
163   (interactive "P")
164   (let* ((vocab (or (and (not arg) (fuel-syntax--current-vocab))
165                     (fuel-completion--read-vocab nil)))
166          (ret (fuel-scaffold--create-docs vocab))
167          (file (fuel-eval--retort-result ret)))
168         (unless file
169           (error "Error creating help file" (car (fuel-eval--retort-error ret))))
170         (find-file file)))
171
172 (defun fuel-scaffold-tests (&optional arg)
173   "Creates, if it does not already exist, a tests file for the current vocabulary.
174
175 With prefix argument, ask for the vocabulary name.
176 You can configure `fuel-scaffold-developer-name' (set by default to
177 `user-full-name') for the name to be inserted in the generated file."
178   (interactive "P")
179   (let* ((vocab (or (and (not arg) (fuel-syntax--current-vocab))
180                     (fuel-completion--read-vocab nil)))
181          (ret (fuel-scaffold--create-tests vocab))
182          (file (fuel-eval--retort-result ret)))
183         (unless file
184           (error "Error creating tests file" (car (fuel-eval--retort-error ret))))
185         (find-file file)))
186
187 (defun fuel-scaffold-authors (&optional arg)
188   "Creates, if it does not already exist, an authors file for the current vocabulary.
189
190 With prefix argument, ask for the vocabulary name.
191 You can configure `fuel-scaffold-developer-name' (set by default to
192 `user-full-name') for the name to be inserted in the generated file."
193   (interactive "P")
194   (let* ((vocab (or (and (not arg) (fuel-syntax--current-vocab))
195                     (fuel-completion--read-vocab nil)))
196          (ret (fuel-scaffold--create-authors vocab))
197          (file (fuel-eval--retort-result ret)))
198         (unless file
199           (error "Error creating authors file" (car (fuel-eval--retort-error ret))))
200         (find-file file)))
201
202 (defun fuel-scaffold-tags (&optional arg)
203   "Creates, if it does not already exist, a tags file for the current vocabulary."
204   (interactive "P")
205   (let* ((vocab (or (and (not arg) (fuel-syntax--current-vocab))
206                     (fuel-completion--read-vocab nil)))
207          (tags (read-string "Tags: "))
208          (ret (fuel-scaffold--create-tags vocab tags))
209          (file (fuel-eval--retort-result ret)))
210         (unless file
211           (error "Error creating tags file" (car (fuel-eval--retort-error ret))))
212         (find-file file)))
213
214 (defun fuel-scaffold-summary (&optional arg)
215   "Creates, if it does not already exist, a summary file for the current vocabulary."
216   (interactive "P")
217   (let* ((vocab (or (and (not arg ) (fuel-syntax--current-vocab))
218                     (fuel-completion--read-vocab nil)))
219          (summary (read-string "Summary: "))
220          (ret (fuel-scaffold--create-summary vocab summary))
221          (file (fuel-eval--retort-result ret)))
222         (unless file
223           (error "Error creating summary file" (car (fuel-eval--retort-error ret))))
224         (find-file file)))
225
226 \f
227 (provide 'fuel-scaffold)
228 ;;; fuel-scaffold.el ends here