* wesnoth-update: Updated changelog.
[wesnoth-mode.git] / wesnoth-update.el
blob22a6a0936d391b7aabc3e027b58134c7c196ed43
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.1
63 ;; * Provide means for increased performance when referencing attributes and
64 ;; tags.
65 ;; * Gather project macro information for the local buffer only, instead of
66 ;; from files in the directory.
67 ;; 0.1
68 ;; * Initial version
70 ;;; Code:
71 (defvar wesnoth-update-version "0.1.1"
72 "Version of `wesnoth-update'.")
74 (defcustom wesnoth-root-directory nil
75 "Root directory of wesnoth."
76 :type 'directory
77 :group 'wesnoth-mode)
79 (defcustom wesnoth-addition-file nil
80 "Filename to the file containing additional WML information."
81 :type 'file
82 :group 'wesnoth-mode)
84 (defcustom wesnoth-update-output-directory nil
85 "Directory to write discovered WML syntax information.
86 Ensure this directory is in your `load-path'."
87 :type 'directory
88 :group 'wesnoth-mode)
90 (defconst wesnoth-macro-directory "data/core/macros"
91 "Directory which built-in macros are stored.
92 This is relative to the wesnoth directory in `wesnoth-root-directory.'.")
94 (defvar wesnoth-found-cfgs '()
95 "Temporary list of all .cfg files found.")
97 (defvar wesnoth-tag-data '()
98 "All information regarding the relation of tags and attributes.")
100 (defvar wesnoth-macro-data '()
101 "Information regarding built-in macros.")
103 (defvar wesnoth-local-macro-data '()
104 "All macro definitions available in the current project.")
106 (defvar wesnoth-tag-hash-table (make-hash-table :test 'equal
107 :size 350)
108 "Hash table of known WML tag data.")
110 (defun wesnoth-create-wml-hash-table ()
111 "Handle generation of `wesnoth-tag-hash-table'."
112 (clrhash wesnoth-tag-hash-table)
113 (dolist (tag wesnoth-tag-data)
114 (puthash (car tag) (cdr tag) wesnoth-tag-hash-table)))
116 (defun wesnoth-file-cfg-p (file)
117 "Return non-nil if FILE has a '.cfg' extension."
118 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
120 (defun wesnoth-fetch-all-dirs (dir)
121 "Retrieve a list of subdirectories to scan.
122 DIR is the directory to check."
123 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
124 (while dirs-to-scan
125 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
126 dirs-to-scan)))))
128 (defun wesnoth-files-in-dir (dir)
129 "Add cfgs to `wesnoth-files-in-dir'.
130 Returns a list of sub-directories in DIR."
131 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
132 (when cfgs
133 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
134 (let ((dirs '()))
135 (dolist (file (directory-files dir t))
136 (unless (string-match "^\\..*" (file-name-nondirectory file))
137 (cond ((file-directory-p file)
138 (add-to-list 'dirs file))
139 ((wesnoth-file-cfg-p file)
140 (add-to-list 'wesnoth-found-cfgs file)))))
141 dirs))
143 (defun wesnoth-cfg-files-in-dir (dir)
144 "Return all cfg files in DIR."
145 (let ((result '()))
146 (dolist (file (directory-files dir t))
147 (and (wesnoth-file-cfg-p file)
148 (add-to-list 'result file)))
149 result))
151 (defun wesnoth-determine-details (dir-or-file function)
152 "Process .cfg files in DIR-OR-FILE using FUNCTION.
153 DIR-OR-FILE can be a file, a directory, or a list of files."
154 (cond ((listp dir-or-file)
155 (dolist (file dir-or-file)
156 (wesnoth-handle-file function file)))
157 ((and (file-exists-p dir-or-file)
158 (not (file-directory-p dir-or-file)))
159 (wesnoth-handle-file function dir-or-file))
161 (wesnoth-fetch-all-dirs dir-or-file)
162 (while wesnoth-found-cfgs
163 (unless (string-match "^\\..+" (file-name-nondirectory
164 (car wesnoth-found-cfgs)))
165 (wesnoth-handle-file function (car wesnoth-found-cfgs))
166 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))))
168 (defun wesnoth-handle-file (function file)
169 "Perform FUNCTION on FILE."
170 (with-temp-buffer
171 (when (file-exists-p file)
172 (insert-file-contents file)
173 (funcall function))))
175 (defun wesnoth-extract-tag-information ()
176 "Retrieve relevant tag and attribute information."
177 (let ((unmatched-tag-list '()))
178 (goto-char (point-min))
179 (while (search-forward-regexp
180 "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|\\(\\w\\|_\\)+=\\)"
181 (point-max) t)
182 (beginning-of-line)
183 (cond
184 ((and (save-excursion
185 (search-backward-regexp
186 "^[\t ]*\\(\\[[^/]]?\\|#define \\|#enddef \\)"
187 (point-min) t))
188 (string-match "#define " (match-string 1))
189 (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]"))
190 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
191 (setq unmatched-tag-list (cons (match-string-no-properties 1)
192 unmatched-tag-list)))
193 ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
194 (wesnoth-append-tag-information (car unmatched-tag-list)
195 (match-string-no-properties 1)
196 nil)
197 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
198 (setq unmatched-tag-list (cons (match-string-no-properties 1)
199 unmatched-tag-list)))
200 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=")
201 (wesnoth-append-tag-information (car unmatched-tag-list)
202 nil (match-string-no-properties 1)))
203 ((looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|")
204 (when (string= (match-string-no-properties 1)
205 (car unmatched-tag-list))
206 (setq unmatched-tag-list (cdr unmatched-tag-list)))))
207 (end-of-line))))
209 (defun wesnoth-append-tag-information (tag subtag attribute)
210 "Add the information regarding TAG to the list.
211 SUBTAG and ATTRIBUTE are a children of TAG to be added."
212 (let ((match (assoc tag wesnoth-tag-data)))
213 (if (not match)
214 (add-to-list 'wesnoth-tag-data (list tag (and subtag (list subtag))
215 (and attribute (list attribute))))
216 (if subtag
217 (let ((tmp (nth 1 match)))
218 (when (not (member subtag tmp))
219 (add-to-list 'tmp subtag)
220 (setq match (list tag tmp (car (last match))))))
221 (when attribute (let ((tmp (nth 2 match)))
222 (when (not (member attribute tmp))
223 (add-to-list 'tmp attribute)
224 (setq match (list tag (nth 1 match) tmp))))))
225 (setq wesnoth-tag-data
226 (remove (assoc tag wesnoth-tag-data)
227 wesnoth-tag-data))
228 (add-to-list 'wesnoth-tag-data match))))
230 (defmacro wesnoth-determine-macro-information (macro-list)
231 "Process the buffer, retrieving macro definition information.
232 MACRO-LIST is the variable to append macro information."
233 `(save-excursion
234 (goto-char (point-min))
235 (while (search-forward-regexp
236 "#define \\(\\(\\w\\|_\\)+\\)\\([\t ]+\\(\\w\\|_\\)+\\)?"
237 (point-max) t)
238 (beginning-of-line)
239 (add-to-list ,macro-list (list (match-string-no-properties 1)
240 (not (null (match-string 3)))))
241 (end-of-line))))
243 (defun wesnoth-determine-macro-builtins ()
244 "Retrieve built-in macro definition information."
245 (wesnoth-determine-macro-information 'wesnoth-macro-data))
247 (defun wesnoth-output-path ()
248 "Determine the path to output wml information via `wesnoth-update'."
249 (or wesnoth-update-output-directory
250 (and (boundp 'user-emacs-directory)
251 user-emacs-directory)
252 "~/.emacs.d/"))
254 (defun wesnoth-update-wml-additions ()
255 "Update WML information contained in `wesnoth-addition-file'."
256 (wesnoth-determine-details wesnoth-addition-file
257 'wesnoth-extract-tag-information)
258 (wesnoth-determine-details wesnoth-addition-file
259 'wesnoth-determine-macro-builtins))
261 (defun wesnoth-update ()
262 "Update WML information.
263 Path to WML information included in wesnoth is set by
264 `wesnoth-root-directory.'."
265 (interactive)
266 (setq wesnoth-tag-data nil
267 wesnoth-macro-data nil
268 wesnoth-found-cfgs nil)
269 (unless (and (stringp wesnoth-root-directory)
270 (file-exists-p wesnoth-root-directory))
271 ;; Update failed; restore data.
272 (load "wesnoth-wml-data")
273 (error "%s: directory does not exist"
274 wesnoth-root-directory))
275 (message "Updating WML information...")
276 (wesnoth-determine-details wesnoth-root-directory
277 'wesnoth-extract-tag-information)
278 (wesnoth-update-wml-additions)
279 (wesnoth-determine-details (concat wesnoth-root-directory
280 wesnoth-macro-directory)
281 'wesnoth-determine-macro-builtins)
282 (with-temp-buffer
283 (insert (format "(setq wesnoth-tag-data '%S)\n\n" wesnoth-tag-data))
284 (insert (format "(setq wesnoth-macro-data '%S)\n\n" wesnoth-macro-data))
285 (insert "(provide 'wesnoth-wml-data)\n")
286 (write-file (expand-file-name (format "wesnoth-wml-data.el")
287 (wesnoth-output-path)))
288 (load "wesnoth-wml-data"))
289 (wesnoth-create-wml-hash-table)
290 (message "Updating WML information...done"))
292 (defun wesnoth-update-project-information ()
293 "Update WML macro information for the current project."
294 (interactive)
295 (wesnoth-determine-macro-information 'wesnoth-local-macro-data))
297 (defun wesnoth-update-teach-wesnoth-mode (file-or-dir)
298 "Update WML tag and attribute information for the current project.
299 If FILE-OR-DIR is provided, perform the update using only that location."
300 (interactive)
301 (wesnoth-determine-details
302 file-or-dir
303 (lambda ()
304 (wesnoth-determine-macro-information 'wesnoth-macro-data)))
305 (wesnoth-determine-details file-or-dir
306 'wesnoth-extract-tag-information))
308 (provide 'wesnoth-update)
310 ;;; wesnoth-update.el ends here