* wesnoth-update.el (wesnoth-macro-additions): Fix superfluous paren.
[wesnoth-mode.git] / wesnoth-update.el
blob16b7375704cdee68b9411481ddf3b627f838065a
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.4
63 ;; * WML data from the addition file can now read when as it is required.
64 ;; 0.1.3
65 ;; * Any arguments are now stored for each macro.
66 ;; 0.1.2
67 ;; * Allow forced updating of the hash table.
68 ;; * Allow clearing of local macro data via a prefix argument.
69 ;; 0.1.1
70 ;; * Provide means for increased performance when referencing attributes and
71 ;; tags.
72 ;; * Gather project macro information for the local buffer only, instead of
73 ;; from files in the directory.
74 ;; 0.1
75 ;; * Initial version
77 ;;; Code:
78 (defvar wesnoth-update-version "0.1.3"
79 "Version of `wesnoth-update'.")
81 (defcustom wesnoth-root-directory nil
82 "Root directory of wesnoth."
83 :type 'directory
84 :group 'wesnoth-mode)
86 (defcustom wesnoth-addition-file nil
87 "Filename to the file containing additional WML information."
88 :type 'file
89 :group 'wesnoth-mode)
91 (defcustom wesnoth-update-output-directory nil
92 "Directory to write discovered WML syntax information.
93 Ensure this directory is in your `load-path'."
94 :type 'directory
95 :group 'wesnoth-mode)
97 (defconst wesnoth-macro-directory "data/core/macros"
98 "Directory which built-in macros are stored.
99 This is relative to the wesnoth directory in `wesnoth-root-directory.'.")
101 (defvar wesnoth-found-cfgs '()
102 "Temporary list of all .cfg files found.")
104 (defvar wesnoth-tmp-tag-data '()
105 "Temporary list of tag data.")
107 (defvar wesnoth-tmp-macro-data '()
108 "Temporary list of macro data.")
110 (defvar wesnoth-tag-data '()
111 "All information regarding the relation of tags and attributes.")
113 (defvar wesnoth-macro-data '()
114 "Information regarding built-in macros.")
116 (defvar wesnoth-local-macro-data '()
117 "All macro definitions available in the current project.")
119 (defvar wesnoth-tag-hash-table (make-hash-table :test 'equal
120 :size 350)
121 "Hash table of known WML tag data.")
123 (defun wesnoth-create-wml-hash-table (tag-data &optional force)
124 "Handle generation of `wesnoth-tag-hash-table'."
125 (when (or (= (hash-table-count wesnoth-tag-hash-table) 0)
126 force)
127 (clrhash wesnoth-tag-hash-table)
128 (dolist (tag tag-data)
129 (puthash (car tag) (cdr tag) wesnoth-tag-hash-table))))
131 (defun wesnoth-file-cfg-p (file)
132 "Return non-nil if FILE has a '.cfg' extension."
133 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
135 (defun wesnoth-fetch-all-dirs (dir)
136 "Retrieve a list of subdirectories to scan.
137 DIR is the directory to check."
138 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
139 (while dirs-to-scan
140 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
141 dirs-to-scan)))))
143 (defun wesnoth-files-in-dir (dir)
144 "Add cfgs to `wesnoth-files-in-dir'.
145 Returns a list of sub-directories in DIR."
146 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
147 (when cfgs
148 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
149 (let ((dirs '()))
150 (dolist (file (directory-files dir t))
151 (unless (string-match "^\\..*" (file-name-nondirectory file))
152 (cond ((file-directory-p file)
153 (add-to-list 'dirs file))
154 ((wesnoth-file-cfg-p file)
155 (add-to-list 'wesnoth-found-cfgs file)))))
156 dirs))
158 (defun wesnoth-cfg-files-in-dir (dir)
159 "Return all cfg files in DIR."
160 (let ((result '()))
161 (dolist (file (directory-files dir t))
162 (and (wesnoth-file-cfg-p file)
163 (add-to-list 'result file)))
164 result))
166 (defun wesnoth-determine-details (dir-or-file function)
167 "Process .cfg files in DIR-OR-FILE using FUNCTION.
168 DIR-OR-FILE can be a file, a directory, or a list of files."
169 (cond ((listp dir-or-file)
170 (dolist (file dir-or-file)
171 (wesnoth-handle-file function file)))
172 ((and (file-exists-p dir-or-file)
173 (not (file-directory-p dir-or-file)))
174 (wesnoth-handle-file function dir-or-file))
176 (wesnoth-fetch-all-dirs dir-or-file)
177 (while wesnoth-found-cfgs
178 (unless (string-match "^\\..+" (file-name-nondirectory
179 (car wesnoth-found-cfgs)))
180 (wesnoth-handle-file function (car wesnoth-found-cfgs))
181 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))))
183 (defun wesnoth-handle-file (function file)
184 "Perform FUNCTION on FILE."
185 (with-temp-buffer
186 (when (file-exists-p file)
187 (insert-file-contents file)
188 (funcall function))))
190 (defun wesnoth-extract-tag-information ()
191 "Retrieve relevant tag and attribute information."
192 (let ((unmatched-tag-list '()))
193 (goto-char (point-min))
194 (while (search-forward-regexp
195 "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|\\(\\w\\|_\\)+=\\)"
196 (point-max) t)
197 (beginning-of-line)
198 (cond
199 ((and (save-excursion
200 (search-backward-regexp
201 "^[\t ]*\\(\\[[^/]]?\\|#define \\|#enddef \\)"
202 (point-min) t))
203 (string-match "#define " (match-string 1))
204 (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]"))
205 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
206 (setq unmatched-tag-list (cons (match-string-no-properties 1)
207 unmatched-tag-list)))
208 ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
209 (wesnoth-append-tag-information (car unmatched-tag-list)
210 (match-string-no-properties 1)
211 nil)
212 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
213 (setq unmatched-tag-list (cons (match-string-no-properties 1)
214 unmatched-tag-list)))
215 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=")
216 (wesnoth-append-tag-information (car unmatched-tag-list)
217 nil (match-string-no-properties 1)))
218 ((looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|")
219 (when (string= (match-string-no-properties 1)
220 (car unmatched-tag-list))
221 (setq unmatched-tag-list (cdr unmatched-tag-list)))))
222 (end-of-line))))
224 (defun wesnoth-append-tag-information (tag subtag attribute)
225 "Add the information regarding TAG to the list.
226 SUBTAG and ATTRIBUTE are a children of TAG to be added."
227 (let ((match (assoc tag wesnoth-tmp-tag-data)))
228 (if (not match)
229 (add-to-list 'wesnoth-tmp-tag-data (list tag (and subtag (list subtag))
230 (and attribute (list attribute))))
231 (if subtag
232 (let ((tmp (nth 1 match)))
233 (when (not (member subtag tmp))
234 (add-to-list 'tmp subtag)
235 (setq match (list tag tmp (car (last match))))))
236 (when attribute (let ((tmp (nth 2 match)))
237 (when (not (member attribute tmp))
238 (add-to-list 'tmp attribute)
239 (setq match (list tag (nth 1 match) tmp))))))
240 (setq wesnoth-tmp-tag-data
241 (remove (assoc tag wesnoth-tmp-tag-data)
242 wesnoth-tmp-tag-data))
243 (add-to-list 'wesnoth-tmp-tag-data match))))
245 (defmacro wesnoth-determine-macro-information (macro-list)
246 "Process the buffer, retrieving macro definition information.
247 MACRO-LIST is the variable to append macro information."
248 `(save-excursion
249 (goto-char (point-min))
250 (while (search-forward-regexp
251 "#define \\(\\(?:\\w\\|_\\)+\\)\\(\\([\t ]+\\(\\w\\|_\\)+\\)*\\)"
252 (point-max) t)
253 (beginning-of-line)
254 (add-to-list ,macro-list (list (match-string-no-properties 1)
255 (and (match-string 2)
256 (split-string
257 (match-string-no-properties 2)))))
258 (end-of-line))))
260 (defun wesnoth-determine-macro-builtins ()
261 "Retrieve built-in macro definition information."
262 (wesnoth-determine-macro-information 'wesnoth-macro-data))
264 (defun wesnoth-output-path ()
265 "Determine the path to output wml information via `wesnoth-update'."
266 (or wesnoth-update-output-directory
267 (if (boundp 'user-emacs-directory)
268 (symbol-value 'user-emacs-directory)
269 "~/.emacs.d/")))
271 (defun wesnoth-read-tmp-tag-data ()
272 "Read `wesnoth-tmp-tag-data' and reset its value."
273 (let ((results wesnoth-tmp-tag-data))
274 (setq wesnoth-tmp-tag-data nil)
275 results))
277 (defun wesnoth-tag-additions ()
278 "Update WML tag information contained in `wesnoth-addition-file'."
279 (setq wesnoth-tmp-tag-data nil)
280 (wesnoth-determine-details wesnoth-addition-file
281 'wesnoth-extract-tag-information)
282 (wesnoth-read-tmp-tag-data))
284 (defun wesnoth-macro-additions ()
285 "Update WML macro information contained in `wesnoth-addition-file.'"
286 (setq wesnoth-tmp-macro-data nil)
287 (wesnoth-determine-details
288 wesnoth-addition-file
289 (lambda ()
290 (wesnoth-determine-macro-information 'wesnoth-tmp-macro-data)))
291 (let ((results wesnoth-tmp-macro-data))
292 (setq wesnoth-tmp-macro-data nil)
293 results))
295 (defun wesnoth-update ()
296 "Update WML information.
297 Path to WML information included in wesnoth is set by
298 `wesnoth-root-directory.'."
299 (interactive)
300 (setq wesnoth-tag-data nil
301 wesnoth-macro-data nil
302 wesnoth-found-cfgs nil)
303 (unless (and (stringp wesnoth-root-directory)
304 (file-exists-p wesnoth-root-directory))
305 ;; Update failed; restore data.
306 (load "wesnoth-wml-data")
307 (error "%s: directory does not exist"
308 wesnoth-root-directory))
309 (message "Updating WML information...")
310 (wesnoth-determine-details wesnoth-root-directory
311 'wesnoth-extract-tag-information)
312 (wesnoth-determine-details (concat wesnoth-root-directory
313 wesnoth-macro-directory)
314 'wesnoth-determine-macro-builtins)
315 (setq wesnoth-tag-data wesnoth-tmp-tag-data
316 wesnoth-tmp-tag-data nil)
317 (with-temp-buffer
318 (insert (format "(setq wesnoth-tag-data '%S)\n\n" wesnoth-tag-data))
319 (insert (format "(setq wesnoth-macro-data '%S)\n\n" wesnoth-macro-data))
320 (insert "(provide 'wesnoth-wml-data)\n")
321 (write-file (expand-file-name (format "wesnoth-wml-data.el")
322 (wesnoth-output-path)))
323 (load "wesnoth-wml-data"))
324 (message "Updating WML information...done"))
326 (defun wesnoth-merge-macro-data (&rest macro-data)
327 "Merge WML macro information sets and return the result."
328 (let ((set-data '())
329 (macro-base-data (car macro-data)))
330 (while (setq macro-data (cdr macro-data))
331 (setq set-data (car macro-data))
332 (while set-data
333 (setq macro-base-data
334 (append (list (car set-data))
335 (remove (assoc (car (car set-data)) macro-base-data)
336 macro-base-data))
337 set-data (cdr set-data))))
338 macro-base-data))
340 (defun wesnoth-merge-tag-data (&rest tag-data)
341 "Merge WML tag information sets and return the result."
342 (setq wesnoth-tmp-tag-data (car tag-data))
343 (let ((set-data '()))
344 (while (setq tag-data (cdr tag-data))
345 (setq set-data (car tag-data))
346 (while set-data
347 (let ((subtags (nth 1 (car set-data))))
348 (while subtags
349 (wesnoth-append-tag-information (caar set-data) (car subtags)
350 nil)
351 (setq subtags (cdr subtags))))
352 (let ((attributes (nth 2 (car set-data))))
353 (while attributes
354 (wesnoth-append-tag-information (caar set-data) nil
355 (car attributes))
356 (setq attributes (cdr attributes))))
357 (setq set-data (cdr set-data))))
358 (wesnoth-read-tmp-tag-data)))
360 (defun wesnoth-update-project-information (&optional clear)
361 "Update WML macro information for the current project."
362 (interactive "P")
363 (if clear
364 (setq wesnoth-local-macro-data nil)
365 (wesnoth-determine-macro-information 'wesnoth-local-macro-data)))
367 (defun wesnoth-refresh-wml-data ()
368 (save-match-data
369 (let ((result (wesnoth-merge-tag-data
370 wesnoth-tag-data (wesnoth-tag-additions))))
371 (wesnoth-create-wml-hash-table result t)
372 result)))
374 (defun wesnoth-update-teach-wesnoth-mode (file-or-dir)
375 "Update WML tag and attribute information for the current project.
376 If FILE-OR-DIR is provided, perform the update using only that location."
377 (interactive)
378 (wesnoth-determine-details
379 file-or-dir
380 (lambda ()
381 (wesnoth-determine-macro-information 'wesnoth-macro-data)))
382 (wesnoth-determine-details file-or-dir
383 'wesnoth-extract-tag-information))
385 (provide 'wesnoth-update)
387 ;;; wesnoth-update.el ends here