* wesnoth-update.el: (require 'cl)
[wesnoth-mode.git] / wesnoth-update.el
blob99035450978085105d83636edd6dc0c6c50c4d23
1 ;;; wesnoth-update.el --- Update WML data via a wesnoth installation.
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,
17 ;; MA 02139, USA.
19 ;; This file is part of wesnoth-mode.
21 ;;; Commentary:
22 ;; Update WML information using WML built-in to Wesnoth.
23 ;; The pathname of wesnoth must be set correctly for this to behave correctly,
24 ;; for example:
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
29 ;; future sessions.
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
44 ;; '(lambda ()
45 ;; (wesnoth-update-teach-wesnoth-mode "/path/to/structured/wml.cfg")))
47 ;;; Code:
48 (require 'cl)
50 (defcustom wesnoth-root-directory nil
51 "Path to the root directory of wesnoth.
52 Path must end in trailing slash."
53 :type 'directory
54 :group 'wesnoth-mode)
55 (defvar wesnoth-macro-directory (concat wesnoth-root-directory "data/core/macros"))
56 (defvar wesnoth-found-cfgs '())
57 (defvar wesnoth-tag-data '())
58 (defvar wesnoth-macro-data '())
60 (defun wesnoth-file-cfg-p (file)
61 "Return non-nil if FILE has a '.cfg' extension."
62 (not (and (not (file-directory-p file)) (string-match "\\.cfg$" file))))
64 (defun wesnoth-fetch-all-dirs (dir)
65 "Retrieve a list of subdirectories to scan.
66 DIR is the directory to check."
67 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
68 (while dirs-to-scan
69 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
70 dirs-to-scan)))))
72 (defun wesnoth-files-in-dir (dir)
73 "Return a list of cfg files from DIR."
74 (let ((cfgs (remove-if-not 'wesnoth-file-cfg-p
75 (file-expand-wildcards
76 (concat dir "/*") t))))
77 (when cfgs
78 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
79 (remove-if
80 (lambda (file)
81 (or (not (file-directory-p file))
82 (string-match "^\\..+" (file-name-nondirectory file))))
83 (file-expand-wildcards (concat dir "/*") t)))
85 (defun wesnoth-determine-details (dir-or-file function)
86 "Process .cfg files in DIR-OR-FILE using FUNCTION."
87 (if (and (file-exists-p dir-or-file)
88 (not (file-directory-p dir-or-file)))
89 (wesnoth-handle-file function dir-or-file)
90 (wesnoth-fetch-all-dirs dir-or-file)
91 (while wesnoth-found-cfgs
92 (unless (string-match "^\\..+" (file-name-nondirectory
93 (car wesnoth-found-cfgs)))
94 (wesnoth-handle-file function (car wesnoth-found-cfgs))
95 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs))))))
97 (defun wesnoth-handle-file (function file)
98 "Perform FUNCTION on FILE."
99 (with-temp-buffer
100 (insert-file-contents file)
101 (funcall function)))
103 (defun wesnoth-extract-tag-information ()
104 "Retrieve relevant tag and attribute information."
105 (let ((unmatched-tag-list '()))
106 (goto-char (point-min))
107 (while (search-forward-regexp
108 "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|\\(\\w\\|_\\)+=\\)"
109 (point-max) t)
110 (beginning-of-line)
111 (cond ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
112 (wesnoth-append-tag-information (car unmatched-tag-list)
113 (match-string-no-properties 1)
114 nil)
115 (setq unmatched-tag-list (cons (match-string-no-properties 1)
116 unmatched-tag-list)))
117 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=")
118 (wesnoth-append-tag-information (car unmatched-tag-list)
119 nil (match-string-no-properties 1)))
120 ((looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|")
121 (when (string= (match-string-no-properties 1)
122 (car unmatched-tag-list))
123 (setq unmatched-tag-list (cdr unmatched-tag-list)))))
124 (end-of-line))))
126 (defun wesnoth-append-tag-information (tag subtag attribute)
127 "Add the information regarding TAG to the list.
128 SUBTAG and ATTRIBUTE are a children of TAG to be added."
129 (let ((match (find tag wesnoth-tag-data :key 'car :test 'string=)))
130 (if (not match)
131 (add-to-list 'wesnoth-tag-data (list tag nil nil))
132 (if subtag
133 (let ((tmp (nth 1 match)))
134 (when (not (member subtag tmp))
135 (add-to-list 'tmp subtag)
136 (setq match (list tag tmp (car (last match))))))
137 (when attribute (let ((tmp (nth 2 match)))
138 (when (not (member attribute tmp))
139 (add-to-list 'tmp attribute)
140 (setq match (list tag (nth 1 match) tmp))))))
141 (setq wesnoth-tag-data
142 (remove (find tag wesnoth-tag-data :key 'car :test 'string=)
143 wesnoth-tag-data))
144 (add-to-list 'wesnoth-tag-data match))))
146 (defmacro wesnoth-determine-macro-information (macro-list)
147 "Process the buffer, retrieving macro definition information.
148 MACRO-LIST is the variable to append macro information."
149 `(progn
150 (goto-char (point-min))
151 (while (search-forward-regexp
152 "#define \\(\\(\\w\\|_\\)+\\)\\([\t ]+\\(\\w\\|_\\)+\\)?"
153 (point-max) t)
154 (beginning-of-line)
155 (add-to-list ,macro-list (list (match-string 1)
156 (when (match-string 3) t)))
157 (end-of-line))))
159 (defun wesnoth-determine-macro-builtins ()
160 "Retrieve built-in macro definition information."
161 (wesnoth-determine-macro-information 'wesnoth-macro-data))
163 (defun wesnoth-update ()
164 "Update WML information."
165 (interactive)
166 (setq wesnoth-tag-data nil
167 wesnoth-macro-data nil)
168 (unless wesnoth-root-directory
169 (error "`wesnoth-root-directory' not a suitable pathname"))
170 (wesnoth-determine-details wesnoth-root-directory 'wesnoth-extract-tag-information)
171 (wesnoth-determine-details wesnoth-macro-directory 'wesnoth-determine-macro-builtins)
172 (with-temp-buffer
173 (insert (format "(defvar wesnoth-tag-data '%S)\n\n" wesnoth-tag-data))
174 (insert (format "(defvar wesnoth-macro-data '%S)\n\n" wesnoth-macro-data))
175 (insert (format "(provide 'wesnoth-wml-data)\n"))
176 (write-file "wesnoth-wml-data.el")))
178 (defun wesnoth-update-project-information ()
179 "Update WML macro information for the current project."
180 (interactive)
181 (wesnoth-determine-details default-directory
182 (lambda ()
183 (wesnoth-determine-macro-information 'wesnoth-local-macro-data))))
185 (defun wesnoth-update-teach-wesnoth-mode (&optional file-or-dir)
186 "Update WML tag and attribute information for the current project.
187 If FILE-OR-DIR is provided, perform the update using only that location."
188 (interactive)
189 (wesnoth-determine-details
190 file-or-dir
191 (lambda ()
192 (wesnoth-determine-macro-information 'wesnoth-macro-data)))
193 (wesnoth-determine-details (or file-or-dir default-directory)
194 'wesnoth-extract-tag-information))
196 (provide 'wesnoth-update)
198 ;;; wesnoth-update.el ends here