* wesnoth-mode.el (wesnoth-complete-macro): Don't prompt when macro takes no
[wesnoth-mode.git] / wesnoth-update.el
blob798b592feb4b984ea667c23bbc7c4e0aec46fcd9
1 ;;; wesnoth-update.el --- Update known WML data via existing valid WML.
2 ;; Copyright (C) 2008 Chris Mann
4 ;; This file is part of wesnoth-mode.
6 ;; This program is free software; you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation; either version 2 of the
9 ;; License, or (at your option) any later version.
11 ;; This program is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with this program; see the file COPYING. If not, write to the
18 ;; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 ;; MA 02139, USA.
21 ;;; Commentary:
22 ;; Update WML information using WML built-in to Wesnoth.
24 ;; The following should be added to your .emacs so that `wesnoth-update' can
25 ;; correctly generate WML data:
26 ;; (setq wesnoth-root-directory "/path/to/wesnoth/"
27 ;; wesnoth-update-output-directory "/path/to/wesnoth-mode/"
28 ;; wesnoth-addition-file "/path/to/wesnoth-mode/wesnoth-wml-additions.cfg")
29 ;; Specifying the appropriate path in each case.
31 ;; Although WML data is provided along with wesnoth-mode, you can generate
32 ;; update-to-date, version-specific WML reference data for `wesnoth-mode'
33 ;; using `wesnoth-update'. This requires Wesnoth to be install and its
34 ;; pathname set for this to behave correctly. for example:
35 ;; (setq wesnoth-root-directory "/usr/share/wesnoth/")
37 ;; Then set the output directory for `wesnoth-update's results:
38 ;; (setq wesnoth-update-output-directory "/path/to/wesnoth-mode/")
39 ;; This is recommended to be in the same directory as `wesnoth-mode' and
40 ;; must be in `load-path'.
42 ;; Once set, `wesnoth-update' will produce 'wesnoth-wml-data.el' in
43 ;; `wesnoth-update-output-directory' and the information will automatically
44 ;; be available in the future sessions.
46 ;; Although much data is retreived, it is unlikely to be completely
47 ;; comprehensive. wesnoth-mode can be taught about additional tags,
48 ;; attributes and macros using the current project, or a single file, using
49 ;; `wesnoth-update-wml-additions'.
51 ;; To teach wesnoth-mode about elements it may have missed, you can extend the
52 ;; sample additions included with wesnoth-mode; namely
53 ;; wesnoth-wml-additions.cfg (although any source of WML can be used). To
54 ;; enable this, do the following:
55 ;; Set `wesnoth-addition-file' appropriately, for example:
56 ;; (setq wesnoth-addition-file "/path/to/wesnoth-wml-additions.cfg")
58 ;; Once set correctly, running M-x wesnoth-update will update the WML data
59 ;; available to `wesnoth-mode'.
61 ;;; History:
62 ;; 0.1.2
63 ;; * Allow forced updating of the hash table.
64 ;; * Allow clearing of local macro data via a prefix argument.
65 ;; 0.1.1
66 ;; * Provide means for increased performance when referencing attributes and
67 ;; tags.
68 ;; * Gather project macro information for the local buffer only, instead of
69 ;; from files in the directory.
70 ;; 0.1
71 ;; * Initial version
73 ;;; Code:
74 (defvar wesnoth-update-version "0.1.1"
75 "Version of `wesnoth-update'.")
77 (defcustom wesnoth-root-directory nil
78 "Root directory of wesnoth."
79 :type 'directory
80 :group 'wesnoth-mode)
82 (defcustom wesnoth-addition-file nil
83 "Filename to the file containing additional WML information."
84 :type 'file
85 :group 'wesnoth-mode)
87 (defcustom wesnoth-update-output-directory nil
88 "Directory to write discovered WML syntax information.
89 Ensure this directory is in your `load-path'."
90 :type 'directory
91 :group 'wesnoth-mode)
93 (defconst wesnoth-macro-directory "data/core/macros"
94 "Directory which built-in macros are stored.
95 This is relative to the wesnoth directory in `wesnoth-root-directory.'.")
97 (defvar wesnoth-found-cfgs '()
98 "Temporary list of all .cfg files found.")
100 (defvar wesnoth-tag-data '()
101 "All information regarding the relation of tags and attributes.")
103 (defvar wesnoth-macro-data '()
104 "Information regarding built-in macros.")
106 (defvar wesnoth-local-macro-data '()
107 "All macro definitions available in the current project.")
109 (defvar wesnoth-tag-hash-table (make-hash-table :test 'equal
110 :size 350)
111 "Hash table of known WML tag data.")
113 (defun wesnoth-create-wml-hash-table (&optional force)
114 "Handle generation of `wesnoth-tag-hash-table'."
115 (when (or (= (hash-table-count wesnoth-tag-hash-table) 0)
116 force)
117 (clrhash wesnoth-tag-hash-table)
118 (dolist (tag wesnoth-tag-data)
119 (puthash (car tag) (cdr tag) wesnoth-tag-hash-table))))
121 (defun wesnoth-file-cfg-p (file)
122 "Return non-nil if FILE has a '.cfg' extension."
123 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
125 (defun wesnoth-fetch-all-dirs (dir)
126 "Retrieve a list of subdirectories to scan.
127 DIR is the directory to check."
128 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
129 (while dirs-to-scan
130 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
131 dirs-to-scan)))))
133 (defun wesnoth-files-in-dir (dir)
134 "Add cfgs to `wesnoth-files-in-dir'.
135 Returns a list of sub-directories in DIR."
136 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
137 (when cfgs
138 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
139 (let ((dirs '()))
140 (dolist (file (directory-files dir t))
141 (unless (string-match "^\\..*" (file-name-nondirectory file))
142 (cond ((file-directory-p file)
143 (add-to-list 'dirs file))
144 ((wesnoth-file-cfg-p file)
145 (add-to-list 'wesnoth-found-cfgs file)))))
146 dirs))
148 (defun wesnoth-cfg-files-in-dir (dir)
149 "Return all cfg files in DIR."
150 (let ((result '()))
151 (dolist (file (directory-files dir t))
152 (and (wesnoth-file-cfg-p file)
153 (add-to-list 'result file)))
154 result))
156 (defun wesnoth-determine-details (dir-or-file function)
157 "Process .cfg files in DIR-OR-FILE using FUNCTION.
158 DIR-OR-FILE can be a file, a directory, or a list of files."
159 (cond ((listp dir-or-file)
160 (dolist (file dir-or-file)
161 (wesnoth-handle-file function file)))
162 ((and (file-exists-p dir-or-file)
163 (not (file-directory-p dir-or-file)))
164 (wesnoth-handle-file function dir-or-file))
166 (wesnoth-fetch-all-dirs dir-or-file)
167 (while wesnoth-found-cfgs
168 (unless (string-match "^\\..+" (file-name-nondirectory
169 (car wesnoth-found-cfgs)))
170 (wesnoth-handle-file function (car wesnoth-found-cfgs))
171 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))))
173 (defun wesnoth-handle-file (function file)
174 "Perform FUNCTION on FILE."
175 (with-temp-buffer
176 (when (file-exists-p file)
177 (insert-file-contents file)
178 (funcall function))))
180 (defun wesnoth-extract-tag-information ()
181 "Retrieve relevant tag and attribute information."
182 (let ((unmatched-tag-list '()))
183 (goto-char (point-min))
184 (while (search-forward-regexp
185 "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|\\(\\w\\|_\\)+=\\)"
186 (point-max) t)
187 (beginning-of-line)
188 (cond
189 ((and (save-excursion
190 (search-backward-regexp
191 "^[\t ]*\\(\\[[^/]]?\\|#define \\|#enddef \\)"
192 (point-min) t))
193 (string-match "#define " (match-string 1))
194 (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]"))
195 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
196 (setq unmatched-tag-list (cons (match-string-no-properties 1)
197 unmatched-tag-list)))
198 ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
199 (wesnoth-append-tag-information (car unmatched-tag-list)
200 (match-string-no-properties 1)
201 nil)
202 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
203 (setq unmatched-tag-list (cons (match-string-no-properties 1)
204 unmatched-tag-list)))
205 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=")
206 (wesnoth-append-tag-information (car unmatched-tag-list)
207 nil (match-string-no-properties 1)))
208 ((looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|")
209 (when (string= (match-string-no-properties 1)
210 (car unmatched-tag-list))
211 (setq unmatched-tag-list (cdr unmatched-tag-list)))))
212 (end-of-line))))
214 (defun wesnoth-append-tag-information (tag subtag attribute)
215 "Add the information regarding TAG to the list.
216 SUBTAG and ATTRIBUTE are a children of TAG to be added."
217 (let ((match (assoc tag wesnoth-tag-data)))
218 (if (not match)
219 (add-to-list 'wesnoth-tag-data (list tag (and subtag (list subtag))
220 (and attribute (list attribute))))
221 (if subtag
222 (let ((tmp (nth 1 match)))
223 (when (not (member subtag tmp))
224 (add-to-list 'tmp subtag)
225 (setq match (list tag tmp (car (last match))))))
226 (when attribute (let ((tmp (nth 2 match)))
227 (when (not (member attribute tmp))
228 (add-to-list 'tmp attribute)
229 (setq match (list tag (nth 1 match) tmp))))))
230 (setq wesnoth-tag-data
231 (remove (assoc tag wesnoth-tag-data)
232 wesnoth-tag-data))
233 (add-to-list 'wesnoth-tag-data match))))
235 (defmacro wesnoth-determine-macro-information (macro-list)
236 "Process the buffer, retrieving macro definition information.
237 MACRO-LIST is the variable to append macro information."
238 `(save-excursion
239 (goto-char (point-min))
240 (while (search-forward-regexp
241 "#define \\(\\(?:\\w\\|_\\)+\\)\\(\\([\t ]+\\(\\w\\|_\\)+\\)*\\)"
242 (point-max) t)
243 (beginning-of-line)
244 (add-to-list ,macro-list (list (match-string-no-properties 1)
245 (and (match-string 2)
246 (split-string
247 (match-string-no-properties 2)))))
248 (end-of-line))))
250 (defun wesnoth-determine-macro-builtins ()
251 "Retrieve built-in macro definition information."
252 (wesnoth-determine-macro-information 'wesnoth-macro-data))
254 (defun wesnoth-output-path ()
255 "Determine the path to output wml information via `wesnoth-update'."
256 (or wesnoth-update-output-directory
257 (and (boundp 'user-emacs-directory)
258 user-emacs-directory)
259 "~/.emacs.d/"))
261 (defun wesnoth-update-wml-additions ()
262 "Update WML information contained in `wesnoth-addition-file'."
263 (wesnoth-determine-details wesnoth-addition-file
264 'wesnoth-extract-tag-information)
265 (wesnoth-determine-details wesnoth-addition-file
266 'wesnoth-determine-macro-builtins))
268 (defun wesnoth-update ()
269 "Update WML information.
270 Path to WML information included in wesnoth is set by
271 `wesnoth-root-directory.'."
272 (interactive)
273 (setq wesnoth-tag-data nil
274 wesnoth-macro-data nil
275 wesnoth-found-cfgs nil)
276 (unless (and (stringp wesnoth-root-directory)
277 (file-exists-p wesnoth-root-directory))
278 ;; Update failed; restore data.
279 (load "wesnoth-wml-data")
280 (error "%s: directory does not exist"
281 wesnoth-root-directory))
282 (message "Updating WML information...")
283 (wesnoth-determine-details wesnoth-root-directory
284 'wesnoth-extract-tag-information)
285 (wesnoth-update-wml-additions)
286 (wesnoth-determine-details (concat wesnoth-root-directory
287 wesnoth-macro-directory)
288 'wesnoth-determine-macro-builtins)
289 (with-temp-buffer
290 (insert (format "(setq wesnoth-tag-data '%S)\n\n" wesnoth-tag-data))
291 (insert (format "(setq wesnoth-macro-data '%S)\n\n" wesnoth-macro-data))
292 (insert "(provide 'wesnoth-wml-data)\n")
293 (write-file (expand-file-name (format "wesnoth-wml-data.el")
294 (wesnoth-output-path)))
295 (load "wesnoth-wml-data"))
296 (wesnoth-create-wml-hash-table t)
297 (message "Updating WML information...done"))
299 (defun wesnoth-update-project-information (&optional clear)
300 "Update WML macro information for the current project."
301 (interactive "P")
302 (if clear
303 (setq wesnoth-local-macro-data nil)
304 (wesnoth-determine-macro-information 'wesnoth-local-macro-data)))
306 (defun wesnoth-update-teach-wesnoth-mode (file-or-dir)
307 "Update WML tag and attribute information for the current project.
308 If FILE-OR-DIR is provided, perform the update using only that location."
309 (interactive)
310 (wesnoth-determine-details
311 file-or-dir
312 (lambda ()
313 (wesnoth-determine-macro-information 'wesnoth-macro-data)))
314 (wesnoth-determine-details file-or-dir
315 'wesnoth-extract-tag-information))
317 (provide 'wesnoth-update)
319 ;;; wesnoth-update.el ends here