PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/misc/fuel/fuel-scaffold.el

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