1 ;;; wesnoth-update.el --- Update known WML data via existing valid WML.
2 ;; Copyright (C) 2008 Chris Mann
4 ;; This program is free software; you can redistribute it and/or
5 ;; modify it under the terms of the GNU General Public License as
6 ;; published by the Free Software Foundation; either version 2 of the
7 ;; License, or (at your option) any later version.
9 ;; This program is distributed in the hope that it will be useful, but
10 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;; General Public License for more details.
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with this program; see the file COPYING. If not, write to the
16 ;; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 ;; This file is part of wesnoth-mode.
22 ;; Update WML information using WML built-in to Wesnoth.
23 ;; The pathname of wesnoth must be set correctly for this to behave correctly,
25 ;; (setq wesnoth-root-directory "/usr/share/wesnoth/")
26 ;; Ensuring the pathname ends in a slash, then running `wesnoth-update'.
27 ;; `wesnoth-update' will produce 'wesnoth-wml-data.el' in `default-directory',
28 ;; ensure this file is in `load-path' to use the information retrieved in the
31 ;; Although much data is retreived, it is unlikely to be completely
32 ;; comprehensive. wesnoth-mode can be taught about additional tags,
33 ;; attributes and macros using the current project, or a single file, using
34 ;; `wesnoth-update-teach-wesnoth-mode'.
36 ;; To teach wesnoth-mode about elements it may have missed, you can create a
37 ;; formatted WML file, ensuring the nesting and all elements are correct
38 ;; (preferably by running wmllint) and evaluate:
39 ;; (wesnoth-update-teach-wesnoth-mode "/path/to/structured/wml.cfg")
41 ;; It may be preferable to perform this each time wesnoth-mode is used. For
42 ;; this, you can add the following to your .emacs:
43 ;; (add-hook 'wesnoth-mode-hook
45 ;; (wesnoth-update-teach-wesnoth-mode "/path/to/structured/wml.cfg")))
54 (defvar wesnoth-update-version
"0.1"
55 "Version of `wesnoth-update'.")
56 (defcustom wesnoth-root-directory nil
57 "Path to the root directory of wesnoth.
58 Path must end in trailing slash."
62 (defvar wesnoth-macro-directory
(concat wesnoth-root-directory
"data/core/macros")
63 "Directory which built-in macros are stored.
64 This is relative to `wesnoth-root-directory'.")
66 (defvar wesnoth-found-cfgs
'()
67 "Temporary list of all .cfg files found.")
69 (defvar wesnoth-tag-data
'()
70 "All information regarding the relation of tags and attributes.")
72 (defvar wesnoth-macro-data
'()
73 "Information regarding built-in macros.")
75 (defvar wesnoth-local-macro-data
'()
76 "All macro definitions available in the current project.")
78 (defun wesnoth-file-cfg-p (file)
79 "Return non-nil if FILE has a '.cfg' extension."
80 (not (and (not (file-directory-p file
)) (string-match "\\.cfg$" file
))))
82 (defun wesnoth-fetch-all-dirs (dir)
83 "Retrieve a list of subdirectories to scan.
84 DIR is the directory to check."
85 (let ((dirs-to-scan (wesnoth-files-in-dir dir
)))
87 (setq dirs-to-scan
(append (wesnoth-files-in-dir (pop dirs-to-scan
))
90 (defun wesnoth-files-in-dir (dir)
91 "Return a list of cfg files from DIR."
92 (let ((cfgs (remove-if-not 'wesnoth-file-cfg-p
93 (file-expand-wildcards
94 (concat dir
"/*") t
))))
96 (setq wesnoth-found-cfgs
(append cfgs wesnoth-found-cfgs
))))
99 (or (not (file-directory-p file
))
100 (string-match "^\\..+" (file-name-nondirectory file
))))
101 (file-expand-wildcards (concat dir
"/*") t
)))
103 (defun wesnoth-determine-details (dir-or-file function
)
104 "Process .cfg files in DIR-OR-FILE using FUNCTION."
105 (if (and (file-exists-p dir-or-file
)
106 (not (file-directory-p dir-or-file
)))
107 (wesnoth-handle-file function dir-or-file
)
108 (wesnoth-fetch-all-dirs dir-or-file
)
109 (while wesnoth-found-cfgs
110 (unless (string-match "^\\..+" (file-name-nondirectory
111 (car wesnoth-found-cfgs
)))
112 (wesnoth-handle-file function
(car wesnoth-found-cfgs
))
113 (setq wesnoth-found-cfgs
(cdr wesnoth-found-cfgs
))))))
115 (defun wesnoth-handle-file (function file
)
116 "Perform FUNCTION on FILE."
118 (insert-file-contents file
)
121 (defun wesnoth-extract-tag-information ()
122 "Retrieve relevant tag and attribute information."
123 (let ((unmatched-tag-list '()))
124 (goto-char (point-min))
125 (while (search-forward-regexp
126 "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|\\(\\w\\|_\\)+=\\)"
129 (cond ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
130 (wesnoth-append-tag-information (car unmatched-tag-list
)
131 (match-string-no-properties 1)
133 (setq unmatched-tag-list
(cons (match-string-no-properties 1)
134 unmatched-tag-list
)))
135 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=")
136 (wesnoth-append-tag-information (car unmatched-tag-list
)
137 nil
(match-string-no-properties 1)))
138 ((looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|")
139 (when (string= (match-string-no-properties 1)
140 (car unmatched-tag-list
))
141 (setq unmatched-tag-list
(cdr unmatched-tag-list
)))))
144 (defun wesnoth-append-tag-information (tag subtag attribute
)
145 "Add the information regarding TAG to the list.
146 SUBTAG and ATTRIBUTE are a children of TAG to be added."
147 (let ((match (find tag wesnoth-tag-data
:key
'car
:test
'string
=)))
149 (add-to-list 'wesnoth-tag-data
(list tag nil nil
))
151 (let ((tmp (nth 1 match
)))
152 (when (not (member subtag tmp
))
153 (add-to-list 'tmp subtag
)
154 (setq match
(list tag tmp
(car (last match
))))))
155 (when attribute
(let ((tmp (nth 2 match
)))
156 (when (not (member attribute tmp
))
157 (add-to-list 'tmp attribute
)
158 (setq match
(list tag
(nth 1 match
) tmp
))))))
159 (setq wesnoth-tag-data
160 (remove (find tag wesnoth-tag-data
:key
'car
:test
'string
=)
162 (add-to-list 'wesnoth-tag-data match
))))
164 (defmacro wesnoth-determine-macro-information
(macro-list)
165 "Process the buffer, retrieving macro definition information.
166 MACRO-LIST is the variable to append macro information."
168 (goto-char (point-min))
169 (while (search-forward-regexp
170 "#define \\(\\(\\w\\|_\\)+\\)\\([\t ]+\\(\\w\\|_\\)+\\)?"
173 (add-to-list ,macro-list
(list (match-string 1)
174 (when (match-string 3) t
)))
177 (defun wesnoth-determine-macro-builtins ()
178 "Retrieve built-in macro definition information."
179 (wesnoth-determine-macro-information 'wesnoth-macro-data
))
181 (defun wesnoth-update ()
182 "Update WML information."
184 (setq wesnoth-tag-data nil
185 wesnoth-macro-data nil
)
186 (unless wesnoth-root-directory
187 (error "`wesnoth-root-directory' not a suitable pathname"))
188 (wesnoth-determine-details wesnoth-root-directory
'wesnoth-extract-tag-information
)
189 (wesnoth-determine-details wesnoth-macro-directory
'wesnoth-determine-macro-builtins
)
191 (insert (format "(defvar wesnoth-tag-data '%S)\n\n" wesnoth-tag-data
))
192 (insert (format "(defvar wesnoth-macro-data '%S)\n\n" wesnoth-macro-data
))
193 (insert (format "(provide 'wesnoth-wml-data)\n"))
194 (write-file "wesnoth-wml-data.el")))
196 (defun wesnoth-update-project-information ()
197 "Update WML macro information for the current project."
199 (wesnoth-determine-details default-directory
201 (wesnoth-determine-macro-information 'wesnoth-local-macro-data
))))
203 (defun wesnoth-update-teach-wesnoth-mode (&optional file-or-dir
)
204 "Update WML tag and attribute information for the current project.
205 If FILE-OR-DIR is provided, perform the update using only that location."
207 (wesnoth-determine-details
210 (wesnoth-determine-macro-information 'wesnoth-macro-data
)))
211 (wesnoth-determine-details (or file-or-dir default-directory
)
212 'wesnoth-extract-tag-information
))
214 (provide 'wesnoth-update
)
216 ;;; wesnoth-update.el ends here