* wesnoth-mode.el (wesnoth-first-column-indent-p): Fix a bug which slowed
[wesnoth-mode.git] / wesnoth-update.el
blob902bb74c0399201a44d8524a1b91797018bdda06
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 (&optional force)
111 "Handle generation of `wesnoth-tag-hash-table'."
112 (when (or (= (hash-table-count wesnoth-tag-hash-table) 0)
113 force)
114 (clrhash wesnoth-tag-hash-table)
115 (dolist (tag wesnoth-tag-data)
116 (puthash (car tag) (cdr tag) wesnoth-tag-hash-table))))
118 (defun wesnoth-file-cfg-p (file)
119 "Return non-nil if FILE has a '.cfg' extension."
120 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
122 (defun wesnoth-fetch-all-dirs (dir)
123 "Retrieve a list of subdirectories to scan.
124 DIR is the directory to check."
125 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
126 (while dirs-to-scan
127 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
128 dirs-to-scan)))))
130 (defun wesnoth-files-in-dir (dir)
131 "Add cfgs to `wesnoth-files-in-dir'.
132 Returns a list of sub-directories in DIR."
133 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
134 (when cfgs
135 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
136 (let ((dirs '()))
137 (dolist (file (directory-files dir t))
138 (unless (string-match "^\\..*" (file-name-nondirectory file))
139 (cond ((file-directory-p file)
140 (add-to-list 'dirs file))
141 ((wesnoth-file-cfg-p file)
142 (add-to-list 'wesnoth-found-cfgs file)))))
143 dirs))
145 (defun wesnoth-cfg-files-in-dir (dir)
146 "Return all cfg files in DIR."
147 (let ((result '()))
148 (dolist (file (directory-files dir t))
149 (and (wesnoth-file-cfg-p file)
150 (add-to-list 'result file)))
151 result))
153 (defun wesnoth-determine-details (dir-or-file function)
154 "Process .cfg files in DIR-OR-FILE using FUNCTION.
155 DIR-OR-FILE can be a file, a directory, or a list of files."
156 (cond ((listp dir-or-file)
157 (dolist (file dir-or-file)
158 (wesnoth-handle-file function file)))
159 ((and (file-exists-p dir-or-file)
160 (not (file-directory-p dir-or-file)))
161 (wesnoth-handle-file function dir-or-file))
163 (wesnoth-fetch-all-dirs dir-or-file)
164 (while wesnoth-found-cfgs
165 (unless (string-match "^\\..+" (file-name-nondirectory
166 (car wesnoth-found-cfgs)))
167 (wesnoth-handle-file function (car wesnoth-found-cfgs))
168 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))))
170 (defun wesnoth-handle-file (function file)
171 "Perform FUNCTION on FILE."
172 (with-temp-buffer
173 (when (file-exists-p file)
174 (insert-file-contents file)
175 (funcall function))))
177 (defun wesnoth-extract-tag-information ()
178 "Retrieve relevant tag and attribute information."
179 (let ((unmatched-tag-list '()))
180 (goto-char (point-min))
181 (while (search-forward-regexp
182 "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|\\(\\w\\|_\\)+=\\)"
183 (point-max) t)
184 (beginning-of-line)
185 (cond
186 ((and (save-excursion
187 (search-backward-regexp
188 "^[\t ]*\\(\\[[^/]]?\\|#define \\|#enddef \\)"
189 (point-min) t))
190 (string-match "#define " (match-string 1))
191 (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]"))
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 (match-string-no-properties 1)
198 nil)
199 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
200 (setq unmatched-tag-list (cons (match-string-no-properties 1)
201 unmatched-tag-list)))
202 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=")
203 (wesnoth-append-tag-information (car unmatched-tag-list)
204 nil (match-string-no-properties 1)))
205 ((looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|")
206 (when (string= (match-string-no-properties 1)
207 (car unmatched-tag-list))
208 (setq unmatched-tag-list (cdr unmatched-tag-list)))))
209 (end-of-line))))
211 (defun wesnoth-append-tag-information (tag subtag attribute)
212 "Add the information regarding TAG to the list.
213 SUBTAG and ATTRIBUTE are a children of TAG to be added."
214 (let ((match (assoc tag wesnoth-tag-data)))
215 (if (not match)
216 (add-to-list 'wesnoth-tag-data (list tag (and subtag (list subtag))
217 (and attribute (list attribute))))
218 (if subtag
219 (let ((tmp (nth 1 match)))
220 (when (not (member subtag tmp))
221 (add-to-list 'tmp subtag)
222 (setq match (list tag tmp (car (last match))))))
223 (when attribute (let ((tmp (nth 2 match)))
224 (when (not (member attribute tmp))
225 (add-to-list 'tmp attribute)
226 (setq match (list tag (nth 1 match) tmp))))))
227 (setq wesnoth-tag-data
228 (remove (assoc tag wesnoth-tag-data)
229 wesnoth-tag-data))
230 (add-to-list 'wesnoth-tag-data match))))
232 (defmacro wesnoth-determine-macro-information (macro-list)
233 "Process the buffer, retrieving macro definition information.
234 MACRO-LIST is the variable to append macro information."
235 `(save-excursion
236 (goto-char (point-min))
237 (while (search-forward-regexp
238 "#define \\(\\(\\w\\|_\\)+\\)\\([\t ]+\\(\\w\\|_\\)+\\)?"
239 (point-max) t)
240 (beginning-of-line)
241 (add-to-list ,macro-list (list (match-string-no-properties 1)
242 (not (null (match-string 3)))))
243 (end-of-line))))
245 (defun wesnoth-determine-macro-builtins ()
246 "Retrieve built-in macro definition information."
247 (wesnoth-determine-macro-information 'wesnoth-macro-data))
249 (defun wesnoth-output-path ()
250 "Determine the path to output wml information via `wesnoth-update'."
251 (or wesnoth-update-output-directory
252 (and (boundp 'user-emacs-directory)
253 user-emacs-directory)
254 "~/.emacs.d/"))
256 (defun wesnoth-update-wml-additions ()
257 "Update WML information contained in `wesnoth-addition-file'."
258 (wesnoth-determine-details wesnoth-addition-file
259 'wesnoth-extract-tag-information)
260 (wesnoth-determine-details wesnoth-addition-file
261 'wesnoth-determine-macro-builtins))
263 (defun wesnoth-update ()
264 "Update WML information.
265 Path to WML information included in wesnoth is set by
266 `wesnoth-root-directory.'."
267 (interactive)
268 (setq wesnoth-tag-data nil
269 wesnoth-macro-data nil
270 wesnoth-found-cfgs nil)
271 (unless (and (stringp wesnoth-root-directory)
272 (file-exists-p wesnoth-root-directory))
273 ;; Update failed; restore data.
274 (load "wesnoth-wml-data")
275 (error "%s: directory does not exist"
276 wesnoth-root-directory))
277 (message "Updating WML information...")
278 (wesnoth-determine-details wesnoth-root-directory
279 'wesnoth-extract-tag-information)
280 (wesnoth-update-wml-additions)
281 (wesnoth-determine-details (concat wesnoth-root-directory
282 wesnoth-macro-directory)
283 'wesnoth-determine-macro-builtins)
284 (with-temp-buffer
285 (insert (format "(setq wesnoth-tag-data '%S)\n\n" wesnoth-tag-data))
286 (insert (format "(setq wesnoth-macro-data '%S)\n\n" wesnoth-macro-data))
287 (insert "(provide 'wesnoth-wml-data)\n")
288 (write-file (expand-file-name (format "wesnoth-wml-data.el")
289 (wesnoth-output-path)))
290 (load "wesnoth-wml-data"))
291 (wesnoth-create-wml-hash-table t)
292 (message "Updating WML information...done"))
294 (defun wesnoth-update-project-information ()
295 "Update WML macro information for the current project."
296 (interactive)
297 (wesnoth-determine-macro-information 'wesnoth-local-macro-data))
299 (defun wesnoth-update-teach-wesnoth-mode (file-or-dir)
300 "Update WML tag and attribute information for the current project.
301 If FILE-OR-DIR is provided, perform the update using only that location."
302 (interactive)
303 (wesnoth-determine-details
304 file-or-dir
305 (lambda ()
306 (wesnoth-determine-macro-information 'wesnoth-macro-data)))
307 (wesnoth-determine-details file-or-dir
308 'wesnoth-extract-tag-information))
310 (provide 'wesnoth-update)
312 ;;; wesnoth-update.el ends here