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