* wesnoth-mode.el (wesnoth-mode-map): Bound M-TAB to wesnoth-complete-tag.
[wesnoth-mode.git] / wesnoth-update.el
blobcc02396290194094b55175c02e7e35537b0b77c3
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 ;; Although WML data is provided along with wesnoth-mode, you can generate
25 ;; update-to-date, version-specific WML reference data for `wesnoth-mode'
26 ;; using `wesnoth-update'. This requires Wesnoth to be install and its
27 ;; pathname set for this to behave correctly. for example:
28 ;; (setq wesnoth-root-directory "/usr/share/wesnoth/")
29 ;; Ensuring the pathname ends in a slash.
31 ;; Then set the output directory for `wesnoth-update's results:
32 ;; (setq wesnoth-update-output-directory "/path/to/wesnoth-mode/")
33 ;; This should be in the same directory as `wesnoth-mode' and included in
34 ;; `load-path'.
36 ;; Once set, `wesnoth-update' will
37 ;; produce 'wesnoth-wml-data.el' in `wesnoth-update-output-directory' and the
38 ;; information will automatically be available in the future sessions.
40 ;; Although much data is retreived, it is unlikely to be completely
41 ;; comprehensive. wesnoth-mode can be taught about additional tags,
42 ;; attributes and macros using the current project, or a single file, using
43 ;; `wesnoth-update-wml-additions'.
45 ;; To teach wesnoth-mode about elements it may have missed, you can extend the
46 ;; sample additions included with wesnoth-mode; namely
47 ;; wesnoth-wml-additions.cfg (although any source of WML can be used). To
48 ;; enable this, do the following:
49 ;; Set `wesnoth-addition-file' appropriately, for example:
50 ;; (setq wesnoth-addition-file "/path/to/wesnoth-wml-additions.cfg")
51 ;; and run:
52 ;; M-x wesnoth-update-wml-additions.
53 ;; Once `wesnoth-addition-file' is set, `wesnoth-update' can be used to store
54 ;; the information retrieved, so that it may be used in future sessions.
56 ;;; History:
57 ;; 0.1
58 ;; * Initial version
60 ;;; Code:
61 (defvar wesnoth-update-version "0.1"
62 "Version of `wesnoth-update'.")
64 (defcustom wesnoth-root-directory nil
65 "Root directory of wesnoth."
66 :type 'directory
67 :group 'wesnoth-mode)
69 (defcustom wesnoth-addition-file nil
70 "Filename to the file containing additional WML information."
71 :type 'file
72 :group 'wesnoth-mode)
74 (defcustom wesnoth-update-output-directory nil
75 "Directory to write discovered WML syntax information.
76 Ensure this directory is in your `load-path'."
77 :type 'directory
78 :group 'wesnoth-mode)
80 (defconst wesnoth-macro-directory "data/core/macros"
81 "Directory which built-in macros are stored.
82 This is relative to the wesnoth directory in `wesnoth-root-directory.'.")
84 (defvar wesnoth-found-cfgs '()
85 "Temporary list of all .cfg files found.")
87 (defvar wesnoth-tag-data '()
88 "All information regarding the relation of tags and attributes.")
90 (defvar wesnoth-macro-data '()
91 "Information regarding built-in macros.")
93 (defvar wesnoth-local-macro-data '()
94 "All macro definitions available in the current project.")
96 (defun wesnoth-file-cfg-p (file)
97 "Return non-nil if FILE has a '.cfg' extension."
98 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
100 (defun wesnoth-fetch-all-dirs (dir)
101 "Retrieve a list of subdirectories to scan.
102 DIR is the directory to check."
103 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
104 (while dirs-to-scan
105 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
106 dirs-to-scan)))))
108 (defun wesnoth-files-in-dir (dir)
109 "Add cfgs to `wesnoth-files-in-dir'.
110 Returns a list of sub-directories in DIR."
111 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
112 (when cfgs
113 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
114 (let ((dirs '()))
115 (dolist (file (directory-files dir t))
116 (unless (string-match "^\\..*" (file-name-nondirectory file))
117 (cond ((file-directory-p file)
118 (add-to-list 'dirs file))
119 ((wesnoth-file-cfg-p file)
120 (add-to-list 'wesnoth-found-cfgs file)))))
121 dirs))
123 (defun wesnoth-cfg-files-in-dir (dir)
124 "Return all cfg files in DIR."
125 (let ((result '()))
126 (dolist (file (directory-files dir t))
127 (and (wesnoth-file-cfg-p file)
128 (add-to-list 'result file)))
129 result))
131 (defun wesnoth-determine-details (dir-or-file function)
132 "Process .cfg files in DIR-OR-FILE using FUNCTION.
133 DIR-OR-FILE can be a file, a directory, or a list of files."
134 (cond ((listp dir-or-file)
135 (dolist (file dir-or-file)
136 (wesnoth-handle-file function file)))
137 ((and (file-exists-p dir-or-file)
138 (not (file-directory-p dir-or-file)))
139 (wesnoth-handle-file function dir-or-file))
141 (wesnoth-fetch-all-dirs dir-or-file)
142 (while wesnoth-found-cfgs
143 (unless (string-match "^\\..+" (file-name-nondirectory
144 (car wesnoth-found-cfgs)))
145 (wesnoth-handle-file function (car wesnoth-found-cfgs))
146 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))))
148 (defun wesnoth-handle-file (function file)
149 "Perform FUNCTION on FILE."
150 (with-temp-buffer
151 (when (file-exists-p file)
152 (insert-file-contents file)
153 (funcall function))))
155 (defun wesnoth-extract-tag-information ()
156 "Retrieve relevant tag and attribute information."
157 (let ((unmatched-tag-list '()))
158 (goto-char (point-min))
159 (while (search-forward-regexp
160 "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|\\(\\w\\|_\\)+=\\)"
161 (point-max) t)
162 (beginning-of-line)
163 (cond
164 ((and (save-excursion
165 (search-backward-regexp
166 "^[\t ]*\\(\\[[^/]]?\\|#define \\|#enddef \\)"
167 (point-min) t))
168 (string-match "#define " (match-string 1))
169 (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]"))
170 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
171 (setq unmatched-tag-list (cons (match-string-no-properties 1)
172 unmatched-tag-list)))
173 ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
174 (wesnoth-append-tag-information (car unmatched-tag-list)
175 (match-string-no-properties 1)
176 nil)
177 (wesnoth-append-tag-information (match-string-no-properties 1) nil nil)
178 (setq unmatched-tag-list (cons (match-string-no-properties 1)
179 unmatched-tag-list)))
180 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=")
181 (wesnoth-append-tag-information (car unmatched-tag-list)
182 nil (match-string-no-properties 1)))
183 ((looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|")
184 (when (string= (match-string-no-properties 1)
185 (car unmatched-tag-list))
186 (setq unmatched-tag-list (cdr unmatched-tag-list)))))
187 (end-of-line))))
189 (defun wesnoth-append-tag-information (tag subtag attribute)
190 "Add the information regarding TAG to the list.
191 SUBTAG and ATTRIBUTE are a children of TAG to be added."
192 (let ((match (assoc tag wesnoth-tag-data)))
193 (if (not match)
194 (add-to-list 'wesnoth-tag-data (list tag (and subtag (list subtag))
195 (and attribute (list attribute))))
196 (if subtag
197 (let ((tmp (nth 1 match)))
198 (when (not (member subtag tmp))
199 (add-to-list 'tmp subtag)
200 (setq match (list tag tmp (car (last match))))))
201 (when attribute (let ((tmp (nth 2 match)))
202 (when (not (member attribute tmp))
203 (add-to-list 'tmp attribute)
204 (setq match (list tag (nth 1 match) tmp))))))
205 (setq wesnoth-tag-data
206 (remove (assoc tag wesnoth-tag-data)
207 wesnoth-tag-data))
208 (add-to-list 'wesnoth-tag-data match))))
210 (defmacro wesnoth-determine-macro-information (macro-list)
211 "Process the buffer, retrieving macro definition information.
212 MACRO-LIST is the variable to append macro information."
213 `(progn
214 (goto-char (point-min))
215 (while (search-forward-regexp
216 "#define \\(\\(\\w\\|_\\)+\\)\\([\t ]+\\(\\w\\|_\\)+\\)?"
217 (point-max) t)
218 (beginning-of-line)
219 (add-to-list ,macro-list (list (match-string 1)
220 (when (match-string 3) t)))
221 (end-of-line))))
223 (defun wesnoth-determine-macro-builtins ()
224 "Retrieve built-in macro definition information."
225 (wesnoth-determine-macro-information 'wesnoth-macro-data))
227 (defun wesnoth-output-path ()
228 "Determine the path to output wml information via `wesnoth-update'."
229 (or wesnoth-update-output-directory
230 (and (boundp 'user-emacs-directory)
231 user-emacs-directory)
232 "~/.emacs.d/"))
234 (defun wesnoth-update-wml-additions ()
235 "Update WML information contained in `wesnoth-addition-file'."
236 (wesnoth-determine-details wesnoth-addition-file
237 'wesnoth-extract-tag-information)
238 (wesnoth-determine-details wesnoth-addition-file
239 'wesnoth-determine-macro-builtins))
241 (defun wesnoth-update ()
242 "Update WML information.
243 Path to WML information included in wesnoth is set by
244 `wesnoth-root-directory.'."
245 (interactive)
246 (setq wesnoth-tag-data nil
247 wesnoth-macro-data nil
248 wesnoth-found-cfgs nil)
249 (unless (and (stringp wesnoth-root-directory)
250 (file-exists-p wesnoth-root-directory))
251 ;; Update failed; restore data.
252 (load "wesnoth-wml-data")
253 (error "%s: directory does not exist"
254 wesnoth-root-directory))
255 (message "Updating WML information...")
256 (wesnoth-determine-details wesnoth-root-directory
257 'wesnoth-extract-tag-information)
258 (wesnoth-update-wml-additions)
259 (wesnoth-determine-details (concat wesnoth-root-directory
260 wesnoth-macro-directory)
261 'wesnoth-determine-macro-builtins)
262 (with-temp-buffer
263 (insert (format "(setq wesnoth-tag-data '%S)\n\n" wesnoth-tag-data))
264 (insert (format "(setq wesnoth-macro-data '%S)\n\n" wesnoth-macro-data))
265 (insert "(provide 'wesnoth-wml-data)\n")
266 (write-file (expand-file-name (format "wesnoth-wml-data.el")
267 (wesnoth-output-path)))
268 (load "wesnoth-wml-data"))
269 (message "Updating WML information...done"))
271 (defun wesnoth-update-project-information ()
272 "Update WML macro information for the current project."
273 (interactive)
274 (wesnoth-determine-details (wesnoth-cfg-files-in-dir default-directory)
275 (lambda ()
276 (wesnoth-determine-macro-information
277 'wesnoth-local-macro-data))))
279 (defun wesnoth-update-teach-wesnoth-mode (file-or-dir)
280 "Update WML tag and attribute information for the current project.
281 If FILE-OR-DIR is provided, perform the update using only that location."
282 (interactive)
283 (wesnoth-determine-details
284 file-or-dir
285 (lambda ()
286 (wesnoth-determine-macro-information 'wesnoth-macro-data)))
287 (wesnoth-determine-details file-or-dir
288 'wesnoth-extract-tag-information))
290 (provide 'wesnoth-update)
292 ;;; wesnoth-update.el ends here