bc07b0ce02a21f08895cbd636f359bdc545efca1
[wesnoth-mode.git] / wesnoth-mode.el
blobbc07b0ce02a21f08895cbd636f359bdc545efca1
1 ;;; wesnoth-mode.el --- A major mode for editing WML.
2 ;; Copyright (C) 2006, 2007, 2008, 2009 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 ;;; Description:
22 ;; wesnoth-mode is a major mode for Emacs which assists in the editing of
23 ;; Wesnoth Markup Language (WML) files. Features of wesnoth-mode include
24 ;; syntax highlighting support, automatic indentation, context-sensitive
25 ;; completion and WML checking.
27 ;;; Commentary:
28 ;; Add the following to your .emacs:
29 ;; (add-to-list 'load-path "path/to/wesnoth-mode")
30 ;; (autoload 'wesnoth-mode "wesnoth-mode" "Major mode for editing WML." t)
31 ;; Optionally adding:
32 ;; (add-to-list 'auto-mode-alist '("\\.cfg\\'" . wesnoth-mode))
33 ;; to automatically load wesnoth-mode for all files ending in '.cfg'.
35 ;;; History:
36 ;; 1.3.5+git
37 ;; * Add support for WML validation on a directory.
38 ;; * Add support for more accurate WML validation. (See
39 ;; `wesnoth-allow-accurate-validation'.)
40 ;; * Improve and add support for prefix arg to `wesnoth-kill-block' to kill
41 ;; multiple blocks.
42 ;; * Add support for attributes where whitespace is between the key and `='.
43 ;; 1.3.5
44 ;; * Added navigation commands: C-M-n and C-M-p can be used to move to the
45 ;; next and previous children of the current parent element, respectively.
46 ;; C-M-u and C-M-d can be used to move to the parent element and the next
47 ;; child element, respectively.
48 ;; * Added commands for structured editing: C-M-k kills the block at point.
49 ;; C-M-SPC sets the mark to the position of the end of the block at point.
50 ;; * Fixed a bug where the parent tag would be unknown if a macro was defined
51 ;; between point and the position of the parent element.
52 ;; * Fixed a bug where an incorrectly formatted closing tag could be inserted
53 ;; in some circumstances.
54 ;; 1.3.4a
55 ;; * #ifdef, #ifndef preprocessor statements now indent their contents
56 ;; relative to themselves when `wesnoth-indent-preprocessor-bol' is nil.
57 ;; * Fixed a bug which could prevent element completion immediately within a
58 ;; preprocessor statement.
59 ;; 1.3.4
60 ;; * Fixed some errors produced when wesnoth-mode.el is byte-compiled.
61 ;; * Improve detection and position of inserted closing tags in some
62 ;; circustances.
63 ;; * Improved context detection for completion in some circumstances.
64 ;; * Read `wesnoth-addition-file' as needed; M-x wesnoth-update no longer
65 ;; required.
66 ;; * `wesnoth-indent-preprocessor-bol' has been re-introduced to control
67 ;; whether preprocessor statements are indented to the beginning of the line
68 ;; or as tags.
69 ;; * Many minor bug fixes.
70 ;; 1.3.3
71 ;; * Improve performance when inserting missing elements. Support for
72 ;; searching for missing elements over a region has been removed;
73 ;; `narrow-to-region' can be used to provide the functionality when
74 ;; required.
75 ;; * All warnings found when checking WML are now underlined in the buffer.
76 ;; * Next and previous warning can be jumped to using C-c C-f (or C-x `) and
77 ;; C-c C-b, respectively.
78 ;; * Any macro arguments are now prompted for and inserted when performing
79 ;; completing, as suggested by fabi.
80 ;; * Improved handling of completion; no longer prompt when no completion
81 ;; found, as suggested by uzytkownik.
82 ;; * Added indentation for FOREACH, as suggested by fabi.
83 ;; * Several bugs and inconsistencies corrected.
84 ;; 1.3.2
85 ;; * Major performance improvements to indentation and WML checking.
86 ;; * Fixed a bug where nesting could break when inserting multiple elements
87 ;; near the last element in a buffer.
88 ;; * Fixed a bug where attributes immediately within #ifn?def were always
89 ;; reported to be illegal.
90 ;; * Fixed a bug where tags immediately within #ifn?def were always legal.
91 ;; * Fixed a bug where when inserting missing tags, scanning would only be
92 ;; performed up to point.
93 ;; * Fixed a bug when jumping between preprocessor statements.
94 ;; 1.3.1
95 ;; * Completion history available is now specific to wesnoth-mode.
96 ;; * Added binding to explicitly update macro information from the current
97 ;; buffer (C-c C-u).
98 ;; * Significantly improved performance of completion and WML checking.
99 ;; * Improved performance for inserting missing tags.
100 ;; * Fixed a bug where #ifdef was never matched when checking WML.
101 ;; * Added completion for preprocessor statements.
102 ;; * Improved macro completion and checking.
103 ;; 1.3.0
104 ;; * Added support for Xemacs.
105 ;; * WML checking is now context sensitive; checks attributes and macros.
106 ;; * WML checks are now always performed on the entire buffer, with results
107 ;; displayed in a temporary buffer.
108 ;; * Context-sensitive completion for attributes and tags.
109 ;; * Completion for built-in and project-specific macros.
110 ;; * Changed the following bindings:
111 ;; `wesnoth-insert-tag' - C-c e -> C-c t
112 ;; `wesnoth-jump-to-matching' - C-c m -> C-c o
113 ;; `wesnoth-check-structure' -> `wesnoth-check-wml' - C-c c
114 ;; * Added the following bindings:
115 ;; `wesnoth-complete-attribute' - C-c a
116 ;; `wesnoth-complete-macro' - C-c m
117 ;; `wesnoth-complete-tag' - C-c t
118 ;; * Removed the following bindings:
119 ;; `wesnoth-check-tag-names' - C-c n
120 ;; * Removed `wesnoth-check-tag-names'. Replaced by `wesnoth-check-wml'.
121 ;; * Completion for an incomplete attribute, tag, or macro at point is
122 ;; attempted via TAB.
123 ;; 1.2.5
124 ;; * Fixed support for GNU Emacs 21.
125 ;; * Added several new tags to `wesnoth-tags-list'.
126 ;; * Added M-TAB binding for `wesnoth-insert-tag'.
127 ;; * `wesnoth-insert-tag' now takes an optional numeric argument indicating
128 ;; how many blocks to wrap across instead of a region.
129 ;; * Support for `wesnoth-indent-preprocessor-bol' removed.
130 ;; * Fixed a bug in `wesnoth-insert-tag' and `wesnoth-insert-missing-closing'
131 ;; causing tags not to be inserted in the correct position.
132 ;; * Fixed highlighting of array indexes as tags.
133 ;; 1.2.4
134 ;; * Improved syntax-highlighting for macro calls.
135 ;; * Underscore is now treated as whitespace.
136 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
137 ;; * Point is now placed at the first non-whitespace character of the line,
138 ;; instead of the last.
139 ;; * Corrected minor indentation bugs.
140 ;; * Indenting across large regions is now much more efficient.
141 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
142 ;; applied.
143 ;; 1.2.3
144 ;; * Now compatible with GNU Emacs 21.4.
145 ;; * Added support for several new tags.
146 ;; * Added menu entry for wesnoth-mode.
147 ;; * Significant speed increase to indentation.
148 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
149 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
150 ;; has been removed.
151 ;; * Trailing whitespace is no longer created when creating a second
152 ;; consecutive newline.
153 ;; * Spurious newlines are no longer created when inserting a tag elements
154 ;; around a region.
155 ;; 1.2.2
156 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
157 ;; `wesnoth-element', `wesnoth-element-opening',
158 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
159 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
160 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
161 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
162 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
163 ;; * Fixed a bug in indentation where content was needed between elements
164 ;; pairs for indentation to work.
165 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
166 ;; `wesnoth-auto-indent-flag'.
167 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
168 ;; * Added indentation styles: `wesnoth-indent-default',
169 ;; `wesnoth-indent-withtags' which implement a a similar indentation
170 ;; style to the existing styles, however all preprocessor statements are
171 ;; indented to the first column.
172 ;; * Added support for several new tags.
173 ;; * Modified `wesnoth-newline' to behave more consistently.
174 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
175 ;; now leaves point at the beginning (when moving backward) or end (when
176 ;; moving forward) of the match.
177 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
178 ;; will now work on preprocessor statements. Will now warn if jump
179 ;; destination may not be correct (due to errors in WML structure).
180 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
181 ;; * `wesnoth-check-structure' can now be applied over an active region and
182 ;; now checks preprocessor statements for correct nesting.
183 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
184 ;; perform indentation by providing a prefix argument.
185 ;; * Indentation styles now leave point at the first non-whitespace character
186 ;; of the line.
187 ;; * `wesnoth-check-tag-names' now reports on success.
188 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
189 ;; * `outline-minor-mode' now works on macro definitions.
190 ;; 1.2.1
191 ;; * Base indent now defaults to 4.
192 ;; * Added support for #ifndef.
194 ;;; Code:
195 (require 'easymenu)
196 (require 'wesnoth-update)
197 (require 'wesnoth-wml-data)
199 (defconst wesnoth-mode-version "1.3.5+git"
200 "The current version of `wesnoth-mode'.")
202 (defgroup wesnoth-mode nil "Wesnoth-mode access"
203 :group 'languages
204 :prefix "wesnoth-")
206 (defcustom wesnoth-auto-indent-flag t
207 "Non-nil means indent the current line upon creating a newline."
208 :type 'boolean
209 :group 'wesnoth-mode)
211 (defcustom wesnoth-indent-preprocessor-bol t
212 "Whether to indent Preprocessor statements to the beginning of the line."
213 :type 'boolean
214 :group 'wesnoth-mode)
216 (defcustom wesnoth-indent-savefile t
217 "Non-nil means to use the current indentation conventions.
218 If nil, use the old convention for indentation.
219 The current convention is all attributes are indented a level deeper
220 than their parent; in the past attributes were indented to the same
221 level as their parent.")
223 (defcustom wesnoth-base-indent 4
224 "The number of columns to indent WML."
225 :type 'integer
226 :group 'wesnoth-mode)
228 (defcustom wesnoth-allow-accurate-validation nil
229 "Whether to allow a more accurate method of WML validation.
230 If non-nil, allow a more accurate method (which is able to better
231 determine the current element's parent) to be used. Note that
232 this may cause WML validation to slow significantly when checking
233 error-prone or messy WML. If clean WML is generally used, it is
234 recommended to set this to non-nil."
235 :type 'boolean
236 :group 'wesnoth-mode)
238 (defconst wesnoth-preprocessor-regexp
239 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\
240 \\|\\(ifn?\\|un\\)def \\)"
241 "Regular expression to match all preprocessor statements.")
243 (defconst wesnoth-preprocessor-opening-regexp
244 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
245 "Regular expression to match \"opening\" preprocessor statements.")
247 (defconst wesnoth-preprocessor-closing-regexp
248 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
249 "Regular expression to match \"closing\" preprocessor statements.")
251 (defvar wesnoth-define-blocks '()
252 "Cache of all toplevel #define and #enddef pairs.")
254 (defvar wesnoth-history-list '()
255 "History of inserted WML elements.")
257 (defvar wesnoth-warning-markers '()
258 "Markers for warnings in the buffer.")
260 (defvar wesnoth-found-cfgs '()
261 "Temporary list of all .cfg files found.")
263 (defvar wesnoth-mode-hook nil)
265 (defface wesnoth-warning-face
266 '((t (:underline "tomato1")))
267 "Face to use for warnings in wesnoth-mode"
268 :group 'wesnoth-mode)
270 (defvar wesnoth-mode-map
271 (let ((map (make-sparse-keymap)))
272 (define-key map (kbd "C-M-a") 'wesnoth-backward-element)
273 (define-key map (kbd "C-M-e") 'wesnoth-forward-element)
274 (define-key map (kbd "C-M-n") 'wesnoth-forward-list)
275 (define-key map (kbd "C-M-p") 'wesnoth-backward-list)
276 (define-key map (kbd "C-M-d") 'wesnoth-down-list)
277 (define-key map (kbd "C-M-u") 'wesnoth-backward-up-list)
278 (define-key map (kbd "C-M-k") 'wesnoth-kill-block)
279 (define-key map (kbd "C-M-SPC") 'wesnoth-mark-block)
280 (define-key map (kbd "C-m") 'wesnoth-newline)
281 (define-key map (kbd "C-j") 'wesnoth-newline-and-indent)
282 (define-key map (kbd "C-c C-c") 'wesnoth-check-wml)
283 (define-key map (kbd "C-c C-a") 'wesnoth-complete-attribute)
284 (define-key map (kbd "C-c C-t") 'wesnoth-complete-tag)
285 (define-key map (kbd "C-c C-p") 'wesnoth-complete-preprocessor)
286 (define-key map (kbd "C-c C-u") 'wesnoth-update-project-information)
287 (define-key map (kbd "M-TAB") 'wesnoth-complete-tag)
288 (define-key map (kbd "C-c C-m") 'wesnoth-complete-macro)
289 (define-key map (kbd "C-c C-o") 'wesnoth-jump-to-matching)
290 (define-key map (kbd "C-c C-f") 'wesnoth-forward-warning)
291 (define-key map (kbd "C-x `") 'wesnoth-forward-warning)
292 (define-key map (kbd "C-c C-b") 'wesnoth-backward-warning)
293 (define-key map (kbd "C-c C-/") 'wesnoth-insert-missing-closing)
294 (define-key map (kbd "TAB") 'wesnoth-indent-or-complete)
295 map)
296 "Keymap used in `wesnoth-mode'.")
298 (easy-menu-define wesnoth-menu wesnoth-mode-map "Menu for wesnoth-mode"
299 '("WML"
300 ["Check WML" wesnoth-check-wml t]
301 ["Indent or Complete" wesnoth-indent-or-complete t]
302 ["Indent buffer" (lambda ()
303 (interactive)
304 (wesnoth-indent-region (point-min) (point-max))) t]
305 ["Insert Tag" wesnoth-complete-tag t]
306 ["Insert Attribute" wesnoth-complete-attribute t]
307 ["Insert Macro" wesnoth-complete-macro t]
308 ["Insert Preprocessor" wesnoth-complete-preprocessor t]
309 ["Insert Missing Tag" wesnoth-insert-missing-closing t]
310 ["Jump to Matching" wesnoth-jump-to-matching t]
311 ["Update Macros" wesnoth-update-project-information t]))
313 (defvar wesnoth-syntax-table
314 (let ((wesnoth-syntax-table (make-syntax-table)))
315 (modify-syntax-entry ?# "<" wesnoth-syntax-table)
316 (modify-syntax-entry ?\" "\"" wesnoth-syntax-table)
317 (modify-syntax-entry ?= "." wesnoth-syntax-table)
318 (modify-syntax-entry ?| "w" wesnoth-syntax-table)
319 (modify-syntax-entry ?_ "_" wesnoth-syntax-table)
320 (modify-syntax-entry ?- "_" wesnoth-syntax-table)
321 (modify-syntax-entry ?. "_" wesnoth-syntax-table)
322 (modify-syntax-entry ?\n ">" wesnoth-syntax-table)
323 (modify-syntax-entry ?\r ">" wesnoth-syntax-table)
324 wesnoth-syntax-table)
325 "Syntax table for `wesnoth-mode'.")
327 ;; Prevents automatic syntax-highlighting of elements which might be
328 ;; pre-processor statements.
329 (defvar wesnoth-syntactic-keywords
330 (list
331 '("\\([\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\
332 \\(?:ifn?\\|un\\)def \\)\\)\\)" 1 "w"))
333 "Syntactic keywords for preprocessor statements within `wesnoth-mode'.")
335 (defvar wesnoth-font-lock-keywords
336 (list
337 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face)
338 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
339 font-lock-keyword-face)
340 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
341 2 font-lock-function-name-face)
342 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face))
343 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face))
344 '("}" . font-lock-function-name-face)
345 '("[\t ]*\\(\\[/?[^$]\\(\\w\\|_\\)+\\]\\)" 1 font-lock-type-face)
346 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face)
347 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)[\t ]*="
348 1 font-lock-variable-name-face))
349 "Syntax highlighting for `wesnoth-mode'.")
351 (defun wesnoth-element-closing (&optional limited)
352 "Return the regexp to match a closing element.
353 If LIMITED is non-nil, return a regexp which matches only the
354 #enddef preprocessor."
355 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\]\\|"
356 (if limited
357 "#enddef"
358 "#end\\(?:def\\|if\\)")
359 (if (and (not wesnoth-indent-preprocessor-bol) limited)
360 "\\|#endif"
362 "\\)"))
364 (defun wesnoth-element-opening (&optional limited)
365 "Return the regexp to match a closing element.
366 If LIMITED is non-nil, return a regexp which matches only the
367 #define preprocessor."
368 (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define "
369 (if limited
370 "\\|{FOREACH .+}"
371 "\\|#ifn?def ")
372 (if (and (not wesnoth-indent-preprocessor-bol) limited)
373 "\\|#ifn?def \\|#else"
375 "\\)"))
377 (defun wesnoth-element (&optional limited)
378 "Return the regexp to match a closing element.
379 If LIMITED is non-nil, return a regexp which matches only the
380 #define and #enddef preprocessors."
381 (concat "^[\t ]*\\(\\[[+/]?\\(\\w\\|_\\)+\\]?\\|"
382 (if limited
383 "#define \\|#enddef"
384 (substring wesnoth-preprocessor-regexp 5))
385 (if (and (not wesnoth-indent-preprocessor-bol) limited)
386 "\\|#\\(ifn?def \\|endif\\|else\\)"
388 "\\)"))
390 (defun wesnoth-find-next (type)
391 "Find the next element of TYPE.
392 TYPE is a symbol representing an element type, or a list of
393 element types to find."
394 (let ((element (wesnoth-next-element)))
395 (while (and element (if (listp type)
396 (not (member (car element) type))
397 (not (eq (car element) type))))
398 (setq element (wesnoth-next-element)))
399 (when (if (listp type)
400 (member (car element) type)
401 (eq (car element) type))
402 element)))
404 (defun wesnoth-next-element ()
405 "Move to the next element in the buffer.
406 Return non-nil when an element is found. Otherwise, return nil."
407 (interactive)
408 (save-match-data
409 (and (or (eolp) (looking-at "[}\t ]"))
410 (search-forward-regexp "[^}
411 \t ]" (point-max) t)
412 (forward-char -1)))
413 (let ((details (wesnoth-element-type (point))))
414 (save-match-data
415 (when (nth 2 details)
416 (goto-char (nth 2 details))
417 (while (nth 3 (parse-partial-sexp
418 (save-excursion (search-backward-regexp
419 (wesnoth-element t) (point-min) t)
420 (point))
421 (point)))
422 (search-forward "\"" (point-max) t))))
423 details))
425 (defun wesnoth-find-previous (type)
426 "Find the previous element of TYPE.
427 TYPE is a symbol representing an element type, or a list of
428 element types to find."
429 (let ((element (wesnoth-previous-element)))
430 (while (and element (if (listp type)
431 (not (member (car element) type))
432 (not (eq (car element) type))))
433 (setq element (wesnoth-previous-element)))
434 (when (if (listp type)
435 (member (car element) type)
436 (eq (car element) type))
437 element)))
439 ;; TODO: This is currently quite inefficient, and therefore is only used where
440 ;; necessary. (i.e., when the parent element is initially may not be
441 ;; correctly detected.)
442 (defun wesnoth-previous-element ()
443 "Move to the previous element in the buffer.
444 Return non-nil when an element is found. Otherwise, return nil."
445 (interactive)
446 (if (= (point) (wesnoth-wml-start-pos))
448 (let ((init-element (save-excursion (wesnoth-next-element)))
449 last-element cur-element)
450 (save-match-data
451 (search-backward-regexp "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|#ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)" (point-min) t))
452 (while (< (cadr (setq cur-element (wesnoth-next-element)))
453 (cadr init-element))
454 (when (car cur-element)
455 (setq last-element cur-element)))
456 (goto-char (or (cadr last-element) (cadr init-element) (point-min))))
457 ;; Determine whether currently within a string, and move to its beginning
458 ;; where necessary.
459 (save-excursion
460 (let ((details (wesnoth-element-type (point))))
461 (save-match-data
462 (when (cadr details)
463 (goto-char (cadr details))
464 (while (nth 3 (parse-partial-sexp
465 (save-excursion (search-backward-regexp
466 (wesnoth-element t) (point-min) t)
467 (point))
468 (point)))
469 (search-backward "\"" (point-min) t))))
470 details))))
472 (defun wesnoth-element-type (point)
473 "Return details regarding the element at POINT.
474 A list is returned, the elements of the list represent the
475 following, respectively: A symbol representing the type of
476 element; the position of the start of the element and the
477 position of the end of the element. POINT must be at the start
478 of the element."
479 (let ((element-matches
480 '(("{\\(.*?[/\]\\)+}" . nil) ;; pathnames
481 ("{\\(\\w\\|_\\)+" . macro)
482 ("\\[\\+?[^/]+?\\]" . tag-opening)
483 ("\\[/.+?\\]" . tag-closing)
484 ("\\(\\w\\|_\\)+[\t ]*=" . attribute)
485 ("#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\
486 \\(ifn?\\|un\\)def \\)"
487 . preprocessor)
488 ("#.*$" . comment)
489 ("[^\t ]+") . nil)))
490 (catch 'result
491 (dolist (pair element-matches)
492 (when (looking-at (car pair))
493 (throw 'result (list (cdr pair)
494 (match-beginning 0)
495 (min (save-excursion (forward-line 1) (point))
496 (match-end 0)))))))))
498 (defun wesnoth-estimate-element-type (point)
499 "Return match data for a partial element at POINT."
500 (save-excursion
501 (goto-char point)
502 (let ((element-matches
503 '(("{\\(.*?[/\]\\)+$" . nil) ; pathnames
504 ("{\\(\\w\\|_\\)*$" . macro)
505 ("\\[/\\(\\w\\|_\\)*$" . tag-closing)
506 ("\\[\\+?\\(\\w\\|_\\)*$" . tag-opening)
507 ("^[\t ]*\\(\\w\\|_\\)+$" . attribute)
508 ("[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\
509 \\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
510 . nil) ; not a partial match
511 ("[\t ]*#\\w*$" . preprocessor))))
512 (catch 'result
513 (dolist (pair element-matches)
514 (when (looking-at (car pair))
515 (throw 'result (list (cdr pair)
516 (match-beginning 0)
517 (match-end 0)))))))))
519 (defun wesnoth-guess-element-type (point)
520 "Return details for the the element near POINT.
521 Locate the start of the element before determining details.
522 BOUND is the limit to search backwards."
523 (let ((details (wesnoth-estimate-element-type point))
524 (bound (save-excursion
525 (goto-char point)
526 (beginning-of-line)
527 (point))))
528 (while (and (not (car details)) (> point bound))
529 (setq point (1- point)
530 details (wesnoth-estimate-element-type point)))
531 (and (nth 1 details)
532 (>= (point) (nth 1 details))
533 (nth 2 details)
534 (<= (point) (nth 2 details))
535 details)))
538 ;;; Insertion and completion
539 (defmacro wesnoth-element-completion (completions prompt partial
540 &optional completep)
541 "Process completion of COMPLETIONS, displaying PROMPT.
542 PARTIAL is the partial string on which to attempt completion.
543 If COMPLETEP is non-nil, do not prompt if no completion is found."
544 `(let* ((element (when ,partial (try-completion ,partial ,completions))))
545 (cond ((eq element t)
546 ,partial)
547 ((and ,completep (null element))
548 nil)
549 ((and element (eq (try-completion element ,completions) t))
550 element)
551 ((> (length (all-completions (or element "") ,completions)) 1)
552 (completing-read ,prompt ,completions
553 nil nil element
554 'wesnoth-history-list))
555 ((= (length ,completions) 1)
556 (car ,completions))
558 element))))
560 (defun wesnoth-active-parent-tag ()
561 "Return the name of the active parent tag.
562 Finds the relevant parent tag, ignoring any conditional tags."
563 (save-excursion
564 (let ((parent (wesnoth-parent-tag)))
565 (while (and (stringp (car parent))
566 (string-match "else\\|then"
567 (car parent)))
568 (goto-char (cdr parent))
569 (setq parent (wesnoth-parent-tag))
570 (when (string= (car parent) "if")
571 (goto-char (cdr parent))
572 (setq parent (wesnoth-parent-tag))))
573 (car parent))))
575 (defun wesnoth-parent-tag (&optional exact)
576 "Return the name of the parent tag.
577 If the parent is a preprocessor statement, return non-nil.
578 If the element does not have a parent, return nil.
579 Otherwise, return a string containing the name of the parent tag.
580 If EXACT is non-nil a more accurate, yet slower method is used."
581 (save-excursion
582 (let ((start-point (point))
583 (depth 1))
584 (when (save-excursion (> (point) (progn (back-to-indentation)
585 (point))))
586 (end-of-line))
587 (while (and (> depth 0)
588 (if exact
589 (wesnoth-find-previous
590 '(tag-opening tag-closing preprocessor))
591 (search-backward-regexp (wesnoth-element t) (point-min) t)))
592 (if (string-match "[\t ]*\\[/\\|#enddef" (match-string 0))
593 (setq depth (1+ depth))
594 (setq depth (1- depth))))
595 (beginning-of-line)
596 (if (> depth 0)
597 (cons nil nil)
598 (when (looking-at (wesnoth-element-opening))
599 (let ((parent (match-string-no-properties 1))
600 (position (point)))
601 (if (or (string-match wesnoth-preprocessor-opening-regexp parent)
602 ;; Check if we're immediately within a macro
603 (and (goto-char start-point)
604 (search-backward-regexp "[}{]" (point-min) t)
605 (string= (match-string 0) "{")
606 (goto-char start-point)
607 (not (and (search-backward parent (point-min) t)
608 (search-backward-regexp "[}{]" (point-min)
610 (string= (match-string 0) "{")))))
611 (cons t position)
612 (cons (substring parent (if (string-match "[\\+.+]" parent)
615 (1- (length parent))) position))))))))
617 (defun wesnoth-partial-macro-p ()
618 "Return non-nil if point is in a partial macro."
619 (save-excursion
620 (let ((opened 0))
621 (search-backward-regexp "{" (point-min) t)
622 (while (search-forward-regexp "[{}]" (point-max) t)
623 (if (string= (match-string 0) "{")
624 (setq opened (1+ opened))
625 (setq opened (1- opened))))
626 (> opened 0))))
628 (defun wesnoth-indent-or-complete (&optional elements)
629 "Indent or complete the line at point, depending on context.
630 ELEMENTS is the number of elements to wrap around if inserting
631 matching tags."
632 (interactive "P")
633 (or elements (setq elements 0))
634 (let ((details (wesnoth-guess-element-type (point))))
635 (cond
636 ((eq (car details) 'tag-opening)
637 (wesnoth-complete-tag elements t))
638 ((and (eq (car details) 'macro)
639 (wesnoth-partial-macro-p))
640 (wesnoth-complete-macro t))
641 ((eq (car details) 'preprocessor)
642 (wesnoth-complete-preprocessor elements t))
643 ((eq (car details) 'tag-closing)
644 ;; FIXME: Solve incorrect behaviour when partial closing is "[/"
645 (insert "a")
646 (and (wesnoth-insert-missing-closing t)
647 (delete-region (nth 1 details)
648 (save-excursion (beginning-of-line) (point))))
649 (end-of-line)
650 (wesnoth-indent))
651 ((eq (car details) 'attribute)
652 (wesnoth-complete-attribute t))
654 (wesnoth-indent)))))
656 (defun wesnoth-preprocessor-closed-p (preprocessor)
657 "Determine whether PREPROCESSOR has been closed.
658 PREPROCESSOR is a string matching the preprocessor statement to
659 be inserted."
660 (save-excursion
661 (back-to-indentation)
662 (wesnoth-jump-to-matching preprocessor)
663 (looking-at
664 (if (string= preprocessor "#define ")
665 "#enddef"
666 "#endif"))))
668 (defun wesnoth-complete-preprocessor (&optional elements completep)
669 "Complete and insert the preprocessor at point.
670 ELEMENTS is the number of elements to wrap around.
671 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
672 (interactive "P")
673 (or elements (setq elements 0))
674 (let* ((completions (wesnoth-emacs-completion-formats
675 '("define" "else" "ifdef" "ifndef"
676 "enddef" "endif" "undef")))
677 (partial (when completep
678 (save-excursion
679 (back-to-indentation)
680 (when (looking-at "#\\(\\w*\\)$")
681 (match-string-no-properties 1)))))
682 (preprocessor (wesnoth-element-completion
683 completions "Preprocessor: " partial completep))
684 (details (wesnoth-guess-element-type (point)))
685 (closedp
686 (save-excursion
687 (when preprocessor
688 (unless (string= "#" (substring preprocessor 0 1))
689 (setq preprocessor (concat "#" preprocessor)))
690 (when (string-match "#\\(define\\|ifn?def\\|undef\\)"
691 preprocessor)
692 (setq preprocessor (concat preprocessor " ")))
693 (when partial
694 (delete-region (nth 1 details) (nth 2 details)))
695 (wesnoth-preprocessor-closed-p preprocessor)))))
696 (when preprocessor
697 (when partial
698 (delete-region
699 (save-excursion
700 (progn (search-backward
701 "#" (save-excursion (back-to-indentation)
702 (point))
704 (point)))
705 (point)))
706 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor)
707 (not closedp))
708 (progn
709 (wesnoth-insert-tag elements preprocessor)
710 (forward-line -1)
711 (end-of-line))
712 (wesnoth-insert-element-separately preprocessor)))))
714 (defun wesnoth-macro-arguments ()
715 "Find any current macro arguments."
716 (let ((results '())
717 (depth (wesnoth-within-define (point))))
718 (save-excursion
719 (while (> depth 0)
720 (save-match-data
721 (search-backward-regexp
722 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
723 (point-min) t)
724 (when (<= (wesnoth-within-define (point)) depth)
725 (and (> depth 0)
726 (setq results
727 (append (mapcar (lambda (macro)
728 (list macro nil))
729 (split-string
730 (match-string-no-properties 1)))
731 results)))
732 (setq depth (1- depth)))))
733 results)))
735 (defun wesnoth-complete-macro (&optional completep)
736 "Complete and insert the macro at point.
737 If COMPLETEP is non-nil, attempt to complete the macro at point."
738 (interactive)
739 (wesnoth-update-project-information)
740 (let* ((macro-information (wesnoth-merge-macro-data
741 wesnoth-macro-data
742 (wesnoth-macro-additions)
743 wesnoth-local-macro-data
744 (wesnoth-macro-arguments)))
745 (completions (wesnoth-emacs-completion-formats
746 (mapcar 'car macro-information)))
747 (details (wesnoth-guess-element-type (point)))
748 (partial (when
749 (save-excursion
750 (and completep
751 (eq (car details) 'macro)
752 (goto-char (cadr details))
753 (looking-at "{\\(\\(\\w\\|_\\)*\\)")))
754 (match-string-no-properties 1)))
755 (macro (wesnoth-element-completion completions "Macro: " partial
756 completep))
757 (args (cadr (assoc macro macro-information))))
758 (when macro
759 (if partial
760 (progn
761 ;; Delete the region corresponding to the current macro.
762 (delete-region (nth 1 details) (nth 2 details))
763 (insert "{" macro (if args " }" "}"))
764 (save-excursion
765 (wesnoth-indent)))
766 (wesnoth-insert-and-indent "{" macro (if args " }" "}")))
767 (forward-char -1)
768 (when args
769 (let ((input (read-string (concat (car args) ": "))))
770 (insert input (if (and (cdr args)
771 (not (string= input "")))
772 " " ""))
773 (while (and (setq args (cdr args)) (not (string= input "")))
774 (insert (setq input (read-string (concat (car args) ": ")))
775 (if (and (not (string= input ""))
776 (cdr args))
777 " " "")))))
778 (when (null args) (forward-char 1)))))
780 (defun wesnoth-complete-attribute (&optional completep)
781 "Insert the attribute at point.
782 If COMPLETEP is non-nil, attempt to complete the attribute at point."
783 (interactive)
784 (wesnoth-refresh-wml-data)
785 (let* ((details (save-excursion
786 (back-to-indentation)
787 (wesnoth-guess-element-type (point))))
788 (completions (save-excursion (when (nth 1 details)
789 (goto-char (nth 1 details)))
790 (wesnoth-build-completion 1)))
791 (partial (when completep
792 (when (save-excursion
793 (back-to-indentation)
794 (looking-at "\\(\\(\\w\\|_\\)+\\)"))
795 (match-string-no-properties 1))))
796 (attribute (wesnoth-element-completion completions "Attribute: "
797 partial completep)))
798 (when attribute
799 (if partial
800 (progn
801 (delete-region (nth 1 details) (nth 2 details))
802 (insert attribute "="))
803 (wesnoth-insert-element-separately attribute
804 (if (string-match "=" attribute)
806 "=")))
807 (save-excursion
808 (wesnoth-indent)))))
810 (defun wesnoth-complete-tag (&optional elements completep)
811 "Complete and insert the tag at point.
812 ELEMENTS is the number of elements to wrap around.
813 If COMPLETEP is non-nil, attempt to complete tag at point."
814 (interactive "P")
815 (or elements (setq elements 0))
816 (let* ((details (wesnoth-guess-element-type (point)))
817 (completions (save-excursion (and (nth 1 details)
818 (goto-char (nth 1 details)))
819 (wesnoth-build-completion 0)))
820 (partial (save-excursion
821 (when (and completep
822 (eq (car details) 'tag-opening)
823 (goto-char (cadr details))
824 (looking-at "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$"))
825 (match-string-no-properties 1))))
826 (tag (wesnoth-element-completion completions "Tag: " partial
827 completep))
828 (closedp
829 (save-excursion
830 (wesnoth-jump-to-matching (concat "[" tag "]"))
831 (back-to-indentation)
832 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
833 (string= tag (match-string 1))))))
834 (if tag
835 (progn
836 (if completep
837 (progn
838 (delete-region (nth 1 details) (nth 2 details))
839 (if closedp
840 (progn
841 (wesnoth-insert-and-indent "[" tag "]")
842 (end-of-line))
843 (wesnoth-insert-tag elements tag)))
844 (wesnoth-insert-tag elements tag)))
845 (or completep (wesnoth-insert-tag elements)))))
847 (defun wesnoth-build-completion (position)
848 "Create a new list for tag completion if necessary.
849 Rebuilding list is required for versions of GNU Emacs earlier
850 than 22. POSITION is the argument passed to `nth' for
851 `wesnoth-tag-data'."
852 (interactive "P")
853 (let ((parent (wesnoth-active-parent-tag))
854 (tag-data (wesnoth-refresh-wml-data)))
855 (wesnoth-emacs-completion-formats
856 (if (or (stringp parent) (null parent))
857 (nth position (gethash parent wesnoth-tag-hash-table))
858 (mapcar 'car tag-data)))))
860 (defun wesnoth-emacs-completion-formats (candidates)
861 "Return the completions in the correct format for `emacs-major-version'.
862 CANDIDATES is a list of all possible completions."
863 (if (> emacs-major-version 21)
864 candidates
865 (let ((tags '())
866 (iter 0))
867 (dolist (tag candidates)
868 (setq iter (1+ iter))
869 (setq tags (append tags (list (cons tag iter)))))
870 tags)))
872 (defun wesnoth-insert-tag (&optional elements tagname)
873 "Insert the specified opening tag and it's matching closing tag.
874 Both the opening and closing tags will be placed on their own
875 lines with point positioned between them. Completion of tags at
876 the prompt uses `wesnoth-tags-list'.
878 ELEMENTS is specifies the number of following blocks which the
879 tag should wrap around.
881 TAGNAME is the name of the tag to be inserted."
882 (interactive "Ps")
883 (unless tagname
884 (setq tagname (completing-read "Tag: " (wesnoth-build-completion 0)
885 nil nil nil 'wesnoth-history-list)))
886 (when (or (not elements)
887 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
888 wesnoth-preprocessor-regexp "\\)")))
889 (setq elements 0))
890 (let ((start (save-excursion (forward-line -1) (point)))
891 (end (unless (= elements 0)
892 ;; Work around some strange behaviour when the target is at the
893 ;; end of the buffer.
894 (save-excursion
895 (goto-char (point-max))
896 (beginning-of-line)
897 (unless (looking-at "^[\t ]*$")
898 (end-of-line)
899 (newline)))
900 (wesnoth-nth-pair-position elements))))
901 (if (string-match wesnoth-preprocessor-regexp tagname)
902 (wesnoth-insert-element-separately tagname)
903 (wesnoth-insert-element-separately "[" tagname "]"))
904 (save-excursion
905 (if end
906 (goto-char (marker-position end))
907 (newline (if (string-match wesnoth-preprocessor-regexp tagname) 1 2)))
908 (if (string-match wesnoth-preprocessor-opening-regexp tagname)
909 (wesnoth-insert-element-separately
910 (if (string= tagname "#define ")
911 "#enddef"
912 "#endif"))
913 (wesnoth-insert-element-separately "[/" (if (string-match "^+"
914 tagname)
915 (substring tagname 1)
916 tagname)
917 "]"))
918 (indent-region start (point) nil))
919 (unless end
920 (forward-line 1)))
921 (wesnoth-indent))
923 (defun wesnoth-insert-element-separately (&rest strings)
924 "Concatenate STRINGS and insert them on a line of their own."
925 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
926 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
927 (progn
928 (forward-line 1)
929 (end-of-line))
930 (end-of-line)
931 (newline))
932 (beginning-of-line)
933 (if (looking-at "^[\t ]*$")
934 (end-of-line)
935 (open-line 1)))
936 (insert (apply 'concat strings)))
938 (defun wesnoth-insert-missing-closing (&optional completep)
939 "Insert the next expected closing element at point.
940 If COMPLETEP is non-nil, do not move forward a line when scanning
941 for the matching tag."
942 (interactive)
943 (let ((match nil)
944 (skip t))
945 (save-excursion
946 (when (and (null completep)
947 (<= (point) (save-excursion (back-to-indentation) (point))))
948 (if (save-excursion (beginning-of-line)
949 (looking-at (wesnoth-element-opening)))
950 (forward-line -1)
951 (when
952 (save-excursion (beginning-of-line)
953 (looking-at (wesnoth-element-closing)))
954 (setq skip nil))))
955 (when (wesnoth-search-for-matching-tag
956 'search-backward-regexp (wesnoth-element-opening) 'point-min
957 (and skip (if completep nil 1)))
958 (setq match (and (looking-at (wesnoth-element-opening))
959 (match-string-no-properties 1)))))
960 (when match
961 (if (string= (substring match 0 1) "[")
962 (wesnoth-insert-element-separately
963 "[/"
964 (let ((tagname (substring match 1 (1- (length match)))))
965 (if (string-match "^+" tagname)
966 (substring tagname 1)
967 tagname))
968 "]")
969 (wesnoth-insert-element-separately
970 (cdr (assoc match '(("#define " . "#enddef")
971 ("#ifndef " . "#endif")
972 ("#ifdef " . "#endif")))))))
973 (wesnoth-indent)
974 (end-of-line)
975 match))
977 (defun wesnoth-insert-and-indent (&rest args)
978 "Concatenate and insert the given string(s) before indenting.
980 ARGS is a list of strings to be inserted."
981 (insert (apply 'concat args))
982 (wesnoth-indent))
984 (defun wesnoth-newline (&optional indent)
985 "Indent the current line and create a newline.
986 If `wesnoth-auto-indent-flag' is nil, indentation will not be
987 performed. Indentation can be forced by setting INDENT to
988 non-nil."
989 (interactive "P")
990 (newline)
991 (save-excursion
992 (forward-line -1)
993 (when (and (or wesnoth-auto-indent-flag indent)
994 (not (looking-at "^[\t ]*$")))
995 (wesnoth-indent))))
997 ;;; Movement
998 (defun wesnoth-navigate-element (repeat search-function bound)
999 "Move point to the tag in the given direction REPEAT times.
1001 SEARCH-FUNCTION is the symbol of the function for searching in
1002 the required direction, with BOUND marking the furthest point to
1003 search."
1004 (or repeat (setq repeat 1))
1005 (while (> repeat 0)
1006 (and (eq search-function 'search-forward-regexp) (end-of-line))
1007 (funcall search-function (wesnoth-element-opening) bound t)
1008 (back-to-indentation)
1009 (setq repeat (1- repeat))))
1011 (defun wesnoth-nth-pair-position (count)
1012 "Return `point' after COUNT number of matching element pairs.
1013 COUNT is a positive number representing the number of balanced
1014 pairs to move across.
1015 `point' is returned as a marker object."
1016 (save-excursion
1017 (let ((failed nil))
1018 (if (> (point) (save-excursion (back-to-indentation) (point)))
1019 (end-of-line)
1020 (beginning-of-line))
1021 (while (> count 0)
1022 ;; Currently looking-at target tag. Stop here to avoid
1023 ;; incorrect nesting.
1024 (unless (wesnoth-search-for-matching-tag
1025 'search-forward-regexp (wesnoth-element-closing) 'point-max)
1026 (setq count 0)
1027 (setq failed t))
1028 (and (> (setq count (1- count)) 0) (forward-line 1)))
1029 (if failed
1030 (beginning-of-line)
1031 (end-of-line))
1032 (point-marker))))
1034 (defun wesnoth-forward-element (repeat)
1035 "Move point to the end of the next tag.
1036 REPEAT is an optional numeric argument. If REPEAT is non-nil,
1037 jump forward the specified number of tags."
1038 (interactive "p")
1039 (if (< repeat 0)
1040 (wesnoth-backward-element (abs repeat))
1041 (wesnoth-navigate-element repeat 'search-forward-regexp (point-max))))
1043 (defun wesnoth-backward-element (repeat)
1044 "Move point to the beginning of the previous tag.
1045 REPEAT is an optional numeric argument. If REPEAT is non-nil,
1046 jump backward the specified number of tags."
1047 (interactive "p")
1048 (if (< repeat 0)
1049 (wesnoth-forward-element (abs repeat))
1050 (wesnoth-navigate-element repeat 'search-backward-regexp (point-min))))
1052 (defun wesnoth-search-for-matching-tag (search-function
1053 search-string bound &optional skip)
1054 "Search for the matching tag for the current line.
1055 SEARCH-FUNCTION is the name of the function used to perform the search.
1056 SEARCH-STRING is a string representing the matching tag type.
1057 BOUND is the bound to be passed to the search function.
1058 If SKIP is non-nil, skip the first element and continue from there."
1059 (let ((depth 1))
1060 (when (and (or (and (numberp skip) (forward-line skip))
1061 (funcall search-function (wesnoth-element)
1062 (funcall bound) t))
1063 (or skip (not (string-match search-string (match-string 0)))))
1064 (while (and (> depth 0)
1065 (funcall search-function (wesnoth-element)
1066 (funcall bound) t))
1067 (if (string-match search-string (match-string 0))
1068 (setq depth (1- depth))
1069 (setq depth (1+ depth))))
1070 (= depth 0))))
1072 (defun wesnoth-jump-to-matching (&optional element)
1073 "Jump point to the matching opening/closing tag.
1074 ELEMENT is an element to find a match for."
1075 (interactive)
1076 (beginning-of-line)
1077 (let ((target nil)
1078 (first-element nil))
1079 (save-excursion
1080 (cond
1081 ((or (and (stringp element)
1082 (string-match (wesnoth-element-opening) element))
1083 (looking-at (wesnoth-element-opening)))
1084 (setq first-element (match-string-no-properties 0 element))
1085 (when (wesnoth-search-for-matching-tag
1086 'search-forward-regexp (wesnoth-element-closing) 'point-max
1087 (and (stringp element) 1))
1088 (beginning-of-line)
1089 (if (and (string-match wesnoth-preprocessor-opening-regexp
1090 first-element)
1091 (looking-at (wesnoth-element-closing)))
1092 (when (string= (match-string-no-properties 0)
1093 (cdr (assoc first-element
1094 '(("#define " . "#enddef")
1095 ("#ifndef " . "#endif")
1096 ("#ifdef " . "#endif")))))
1097 (setq target (point)))
1098 (setq target (point)))))
1099 ((or (and (stringp element)
1100 (string-match (wesnoth-element-closing) element))
1101 (looking-at (wesnoth-element-closing)))
1102 (end-of-line)
1103 (setq first-element (match-string-no-properties 0 element))
1104 (when (wesnoth-search-for-matching-tag
1105 'search-backward-regexp (wesnoth-element-opening)
1106 'wesnoth-wml-start-pos (and (stringp element) -1))
1107 (if (and (string-match wesnoth-preprocessor-closing-regexp
1108 first-element)
1109 (looking-at (wesnoth-element-opening)))
1110 (progn
1111 (when (or (and (string= "#enddef" first-element)
1112 (string= "#define "
1113 (match-string-no-properties
1114 0)))
1115 (and (string= "#endif" first-element)
1116 (string-match
1117 "#ifn?def "
1118 (match-string-no-properties
1119 0))))
1120 (setq target (point))))
1121 (setq target (point)))))
1123 (search-backward-regexp (wesnoth-element-opening) (point-min) t)
1124 (setq target (point)))))
1125 (if target
1126 (progn
1127 (goto-char target)
1128 (back-to-indentation))
1129 (when (interactive-p)
1130 (message "%s" "Tag does not appear to be matched")))))
1132 ;;; Indentation
1133 (defun wesnoth-wml-start-pos ()
1134 "Determine the position of `point' relative to where the actual WML begins.
1135 Return the likely starting position of the WML if it is found.
1136 Otherwise return nil."
1137 (save-excursion
1138 (goto-char (point-min))
1139 (when (search-forward-regexp (wesnoth-element) (point-max) t)
1140 (beginning-of-line)
1141 (point))))
1143 (defun wesnoth-first-column-indent-p (point)
1144 "Return non-nil if the current line should not be indented.
1146 POINT is the position in the buffer to check.
1147 CONTEXT represents the type of element which precedes the current element."
1148 (or (not (wesnoth-wml-start-pos))
1149 (<= (point) (wesnoth-wml-start-pos))
1150 (nth 3 (parse-partial-sexp
1151 (save-excursion (search-backward-regexp
1152 (wesnoth-element t) (point-min) t)
1153 (point))
1154 point))
1155 (and (looking-at wesnoth-preprocessor-regexp)
1156 wesnoth-indent-preprocessor-bol)))
1158 (defun wesnoth-indent ()
1159 "Indent the current line as WML."
1160 (save-excursion
1161 (beginning-of-line)
1162 (let* ((cur-indent 0)
1163 (context-data (wesnoth-determine-context (point)))
1164 (context (car context-data))
1165 (ref-indent (cdr context-data)))
1166 (unless (wesnoth-first-column-indent-p (point))
1167 (cond
1168 ((eq context 'opening)
1169 (if (and (looking-at "^[\t ]*#else")
1170 (not wesnoth-indent-preprocessor-bol))
1171 (setq cur-indent ref-indent)
1172 (if (or (and wesnoth-indent-savefile
1173 (or (looking-at "[\t ]*{NEXT ")
1174 (and (not
1175 (looking-at (wesnoth-element-closing t)))
1176 (not (looking-at "[\t ]*{NEXT ")))))
1177 (looking-at (wesnoth-element-opening t))
1178 (looking-at "[\t ]*{FOREACH "))
1179 (setq cur-indent (+ ref-indent wesnoth-base-indent))
1180 (setq cur-indent ref-indent))))
1181 ((eq context 'closing)
1182 (if (and (looking-at "^[\t ]*#else")
1183 (not wesnoth-indent-preprocessor-bol))
1184 (setq cur-indent (- ref-indent wesnoth-base-indent))
1185 (if (or (looking-at
1186 (concat "^[\t ]*\\(\\[/\\|\\#enddef"
1187 (if (not wesnoth-indent-preprocessor-bol)
1188 "\\|#endif"
1190 "\\)"))
1191 (and (not wesnoth-indent-savefile)
1192 (not (looking-at (wesnoth-element-opening t)))
1193 (not (looking-at "[\t ]*{FOREACH "))))
1194 (setq cur-indent (- ref-indent wesnoth-base-indent))
1195 (setq cur-indent ref-indent))))))
1196 (indent-line-to (max cur-indent 0))))
1197 (when (> (save-excursion (back-to-indentation) (point))
1198 (point))
1199 (back-to-indentation)))
1201 (defun wesnoth-within-define (position)
1202 "Determine whether point is currently inside a #define block.
1203 POSITION is the initial cursor position."
1204 (save-match-data
1205 (let ((depth 0)
1206 (defblocks (or wesnoth-define-blocks
1207 (wesnoth-find-macro-definitions))))
1208 (unless (equal (car defblocks) 'none)
1209 (dolist (element defblocks)
1210 (when (= (cadr (sort (append (mapcar 'marker-position
1211 (cadr element))
1212 (list position)) '>)) position)
1213 (setq depth (max (car element) depth)))))
1214 depth)))
1216 (defun wesnoth-find-macro-definitions ()
1217 "Return information regarding positioning of macro definitions."
1218 (save-excursion
1219 (goto-char (point-min))
1220 (let ((depth 0)
1221 openings cache)
1222 (while (search-forward-regexp "#define\\|#enddef" (point-max) t)
1223 (and (string= (match-string 0) "#define") (beginning-of-line))
1224 (setq depth
1225 (if (string= (match-string 0) "#define")
1226 (progn
1227 (add-to-list 'openings (point-marker))
1228 (1+ depth))
1229 (if openings
1230 (progn
1231 (add-to-list 'cache
1232 (list depth (list (car openings)
1233 (point-marker))))
1234 (setq openings (cdr openings))
1235 (1- depth))
1236 depth)))
1237 (end-of-line))
1238 (or cache (list 'none)))))
1240 (defun wesnoth-indent-region (start end)
1241 "Indent the region from START to END.
1242 Creates and destroys a cache of macro definition details as necessary."
1243 (interactive "r")
1244 (unwind-protect
1245 (save-excursion
1246 (goto-char end)
1247 (setq end (point-marker))
1248 (goto-char start)
1249 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1250 (or (bolp) (forward-line 1))
1251 (while (< (point) end)
1252 (if (looking-at "^[\t ]*$")
1253 (indent-line-to 0)
1254 (funcall indent-line-function))
1255 (forward-line 1)))
1256 (setq wesnoth-define-blocks nil)))
1258 (defun wesnoth-determine-context (position)
1259 "Determine the type of the last relevant element.
1260 POSITION is the buffer position of the element for which to
1261 determine the context."
1262 (save-excursion
1263 (let* ((elements (concat (substring (wesnoth-element t)
1264 0 (- (length (wesnoth-element t)) 2))
1265 "\\|{FOREACH .+}\\|{NEXT .+}\\)"))
1266 (match (or
1267 (and (search-backward-regexp
1268 elements (point-min) t)
1269 (progn
1270 (while
1271 (save-match-data
1272 (looking-at "^[\t ]*\\[[^/].+\\]\\[/.+\\]"))
1273 (search-backward-regexp elements
1274 (point-min) t))
1276 (match-string 1))
1277 ""))
1278 (depth (wesnoth-within-define position)))
1279 (while (and (wesnoth-wml-start-pos)
1280 (> (wesnoth-within-define (point)) depth)
1281 (not (= (point) (wesnoth-wml-start-pos))))
1282 (search-backward-regexp elements
1283 (wesnoth-wml-start-pos) t)
1284 (setq match (match-string 1)))
1285 (when (and (wesnoth-wml-start-pos)
1286 (= (point) (wesnoth-wml-start-pos))
1287 (= depth 0)
1288 (string-match "#define" match))
1289 ;; Found nothing of use; reset match and assume top-level tag.
1290 (setq match ""))
1291 (cond
1292 ((string-match (concat "\\[/\\|#enddef"
1293 (if (not wesnoth-indent-preprocessor-bol)
1294 "\\|#endif"
1295 ""))
1296 match)
1297 (cons 'closing (current-indentation)))
1298 ((string-match "{NEXT " match)
1299 (cons 'closing (if wesnoth-indent-savefile
1300 (- (current-indentation) wesnoth-base-indent)
1301 (current-indentation))))
1302 ((string-match (concat "\\[[^/]?\\|#define\\|{FOREACH "
1303 (if (not wesnoth-indent-preprocessor-bol)
1304 "\\|#ifn?def \\|#else"
1305 "")) match)
1306 (cons 'opening (current-indentation)))))))
1308 (defun wesnoth-newline-and-indent (&optional indent)
1309 "Indent both the current line and the newline created.
1310 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1311 performed.
1313 If the optional argument, INDENT is non-nil, force indentation to
1314 be performed."
1315 (interactive)
1316 (wesnoth-newline)
1317 (when (or wesnoth-auto-indent-flag indent)
1318 (wesnoth-indent)))
1320 ;;; WML checks
1321 (defun wesnoth-check-element-type (position &optional exact)
1322 "Determine the context of the element.
1323 POSITION is the position of the element in the list. If EXACT is
1324 non-nil, a more accurate, yet slower method is used. This is
1325 enabled when the attempt to match initially fails."
1326 (let ((parent (save-match-data (car (wesnoth-parent-tag exact))))
1327 (result '()))
1328 (if (or (stringp parent) (null parent))
1329 (setq result (member (match-string-no-properties 1)
1330 (nth position (gethash parent
1331 wesnoth-tag-hash-table))))
1332 (member (match-string-no-properties 1)
1333 (mapc
1334 '(lambda (x)
1335 (let ((value (nth position (cdr x))))
1336 (and value (mapc '(lambda (y)
1337 (setq result (cons y result)))
1338 value))))
1339 (or wesnoth-tmp-tag-data (wesnoth-refresh-wml-data)))))
1340 ;; If unsuccessful and more accurate checking is available, use it.
1341 ;; Otherwise, return the result found.
1342 (if (and wesnoth-allow-accurate-validation (not exact) (not result))
1343 (wesnoth-check-element-type position t)
1344 result)))
1346 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
1347 (defun wesnoth-line-number-at-pos (&optional pos)
1348 "Return (narrowed) buffer line number at position POS.
1349 If POS is nil, use current buffer location.
1350 Counting starts at (point-min), so the value refers
1351 to the contents of the accessible portion of the buffer."
1352 (let ((opoint (or pos (point))) start)
1353 (save-excursion
1354 (goto-char (point-min))
1355 (setq start (point))
1356 (goto-char opoint)
1357 (forward-line 0)
1358 (1+ (count-lines start (point))))))
1360 (defun wesnoth-check-output (buffer format-string &rest args)
1361 "Output the string as passed to `format'.
1362 BUFFER is the buffer to output the result.
1363 FORMAT-STRING is the string as the first argument of `format'.
1364 ARGS is any additional data required by `format' to handle FORMAT-STRING."
1365 (save-excursion
1366 (let ((lnap (wesnoth-line-number-at-pos))
1367 (source-buffer (buffer-name)))
1368 (set-buffer buffer)
1369 (let ((buffer-read-only nil))
1370 (insert (apply 'format (concat "%s: %d: " format-string "\n")
1371 source-buffer lnap args))))))
1373 (defun wesnoth-extract-macro-details (macro-arguments)
1374 "Return a list of all macros in MACRO-ARGUMENTS."
1375 (when macro-arguments
1376 (let ((results '()))
1377 (dolist (macro (split-string macro-arguments "[{}][\t ]*"))
1378 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
1379 macro)
1380 (add-to-list 'results (match-string-no-properties 1 macro))))
1381 results)))
1383 (defmacro wesnoth-check-process (format-string &rest args)
1384 "Output to buffer where requested and position overlays as required.
1385 FORMAT-STRING is the string to pass as the first argument to
1386 `format' for the error. ARGS is a list of arguments required by
1387 FORMAT-STRING."
1388 `(progn
1389 (wesnoth-check-output outbuf ,format-string ,@args)
1390 (wesnoth-place-overlay (match-beginning 0) (match-end 0))))
1392 (defmacro wesnoth-overlay-at-pos-p (position)
1393 "Return non-nil when there is an overlay at POSITION."
1394 `(and (overlays-at (goto-char ,position))
1395 (overlay-get (car (overlays-at (point))) 'wesnoth-error)
1396 (overlay-start (car (overlays-at (point))))))
1398 (defmacro wesnoth-locate-warning (string start end)
1399 "Search for STRING and move to the warning in the given direction.
1400 Searching starts from `point' and will wrap from START if no
1401 match was found. STRING is a form to locate the warning in the
1402 required direction. START is the start of the region searched.
1403 END is the end of the region searched."
1404 `(let ((target nil))
1405 (save-excursion
1406 (cond
1407 ((setq target (wesnoth-overlay-at-pos-p ,string)))
1408 ((setq target (wesnoth-overlay-at-pos-p ,start)))
1409 ((and (not (= (goto-char ,string) ,end))
1410 (setq target (wesnoth-overlay-at-pos-p (point)))))))
1411 target))
1413 (defun wesnoth-forward-warning ()
1414 "Move to the next warning."
1415 (interactive)
1416 (let ((target
1417 (if (fboundp 'next-overlay-change)
1418 (save-excursion
1419 (end-of-line)
1420 (wesnoth-locate-warning (next-overlay-change (point))
1421 (point-min)
1422 (point-max)))
1423 (wesnoth-target-position '< '>))))
1424 (if target
1425 (goto-char target)
1426 (message "%s" "No warnings found"))))
1428 (defun wesnoth-backward-warning ()
1429 "Move to the previous warning."
1430 (interactive)
1431 (let ((target
1432 (if (fboundp 'previous-overlay-change)
1433 (save-excursion
1434 (beginning-of-line)
1435 (wesnoth-locate-warning (1- (previous-overlay-change (point)))
1436 (point-max)
1437 (point-min)))
1438 (wesnoth-target-position '> '< t))))
1439 (if target
1440 (goto-char target)
1441 (message "%s" "No warnings found"))))
1443 (defun wesnoth-target-position (predicate search &optional lastp)
1444 "Return the target marker position.
1445 PREDICATE is the function to use to sort
1446 `wesnoth-warning-markers'. SEARCH must be a function which
1447 returns non-nil when the match is correct. If LASTP is non-nil,
1448 swap the order of the sorted positions when attempting to
1449 fallback."
1450 (let ((positions (sort (mapcar 'marker-position wesnoth-warning-markers)
1451 predicate)))
1452 (or (catch 'pos
1453 (dolist (position positions)
1454 (when (funcall search position (point))
1455 (throw 'pos position))))
1456 (car positions))))
1458 (defun wesnoth-place-overlay (start end)
1459 "Place overlay in the region and apply necessary properties.
1460 START is the start of the region to place the overlay. END is
1461 the end of the region to place the overlay."
1462 (if (fboundp 'overlay-put)
1463 (let ((overlay (make-overlay start end)))
1464 (overlay-put overlay 'wesnoth-error t)
1465 (overlay-put overlay 'face 'wesnoth-warning-face))
1466 (add-to-list 'wesnoth-warning-markers (save-excursion
1467 (goto-char start)
1468 (point-marker)))))
1470 (defun wesnoth-kill-block (arg)
1471 "Kill ARG blocks at point."
1472 (interactive "p")
1473 (save-excursion
1474 (while (> arg 0)
1475 (let ((kill-whole-line t))
1476 (while (and (looking-at "[\t ]*$") (= (forward-line 1) 0)))
1477 (if (looking-at "[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1478 #ifn?def\\)")
1479 (kill-region (point)
1480 (save-excursion
1481 (wesnoth-jump-to-matching)
1482 (forward-line 1)
1483 (point)))
1484 (kill-line))
1485 (setq arg (1- arg)))
1486 (wesnoth-indent))))
1488 (defun wesnoth-mark-block ()
1489 "Mark the block at point."
1490 (interactive)
1491 (save-excursion
1492 (while (and (save-excursion (beginning-of-line)
1493 (looking-at "[\t ]*$"))
1494 (= (forward-line 1) 0)))
1495 (when (looking-at "[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1496 #ifn?def\\)")
1497 (push-mark (save-excursion
1498 (wesnoth-jump-to-matching)
1499 (end-of-line)
1500 (point))))))
1502 (defun wesnoth-down-list (&optional arg)
1503 "Move forward down ARG levels of elements.
1504 If ARG is not specified, move forward down one level."
1505 (interactive "p")
1506 (unless arg
1507 (setq arg 1))
1508 (when (looking-at "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1509 #ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)")
1510 (forward-char 1))
1511 (let ((target nil))
1512 (save-excursion
1513 (when (looking-at "\\(\\w\\|_\\)+[\t ]*=")
1514 (if (and (search-forward-regexp (wesnoth-element) (point-max) t)
1515 (progn (beginning-of-line)
1516 (looking-at (wesnoth-element-opening t))))
1517 (setq target (point))
1518 (message "%s" "Innermost level"))))
1519 (when target (goto-char target)))
1520 (while (> arg 0)
1521 (when (> (save-excursion (wesnoth-jump-to-matching) (point)) (point))
1522 (search-forward-regexp
1523 "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|#ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)"
1524 (save-excursion
1525 (wesnoth-jump-to-matching)
1526 (point))
1528 (setq arg (1- arg)))
1529 (back-to-indentation))
1531 (defun wesnoth-backward-up-list (&optional arg)
1532 "Move backward up ARG levels of elements.
1533 If ARG is not specified, move backward up one level."
1534 (interactive "p")
1535 (unless arg
1536 (setq arg 1))
1537 (let ((parent nil))
1538 (while (> arg 0)
1539 (setq parent (cdr (wesnoth-parent-tag)))
1540 (if (numberp parent)
1541 (progn
1542 (goto-char parent)
1543 (setq arg (1- arg)))
1544 (message "%s" "Outermost level")
1545 (setq arg 0))
1546 (when (numberp parent)
1547 (back-to-indentation)))))
1549 (defun wesnoth-forward-list (arg)
1550 "Move to forward ARG elements at the current depth.
1551 If ARG is not specifed, move forward one element."
1552 (interactive "p")
1553 (while (and (save-excursion (beginning-of-line)
1554 (looking-at "[\t ]*$"))
1555 (= (forward-line 1) 0)))
1556 (when (save-excursion (beginning-of-line)
1557 (looking-at (wesnoth-element-opening)))
1558 (wesnoth-jump-to-matching)
1559 (setq arg (1- arg)))
1560 (let ((revert-target (point)))
1561 (while (> arg 0)
1562 (beginning-of-line)
1563 (unless (looking-at (wesnoth-element-closing))
1564 (wesnoth-jump-to-matching))
1565 (end-of-line)
1566 (if (and (search-forward-regexp (wesnoth-element) (point-max) t)
1567 (save-excursion (beginning-of-line)
1568 (looking-at (wesnoth-element-opening))))
1569 (setq revert-target (point))
1570 (goto-char revert-target)
1571 (message "%s" "End of block"))
1572 (setq arg (1- arg))))
1573 (when (save-excursion (beginning-of-line)
1574 (looking-at (wesnoth-element-opening)))
1575 (wesnoth-jump-to-matching))
1576 (end-of-line))
1578 (defun wesnoth-backward-list (arg)
1579 "Move to backward ARG elements at the current depth.
1580 If ARG is not specifed, move backward one element."
1581 (interactive "p")
1582 (while (and (save-excursion (beginning-of-line) (looking-at "[\t ]*$"))
1583 (= (forward-line -1) 0)))
1584 (when (save-excursion (beginning-of-line)
1585 (looking-at (wesnoth-element-closing)))
1586 (wesnoth-jump-to-matching)
1587 (setq arg (1- arg)))
1588 (let ((revert-target (point)))
1589 (while (> arg 0)
1590 (beginning-of-line)
1591 (unless (looking-at (wesnoth-element-opening))
1592 (wesnoth-jump-to-matching))
1593 (if (and (search-backward-regexp (wesnoth-element) (point-min) t)
1594 (save-excursion (beginning-of-line)
1595 (looking-at (wesnoth-element-closing))))
1596 (setq revert-target (point))
1597 (goto-char revert-target)
1598 (message "%s" "Beginning of block"))
1599 (setq arg (1- arg))))
1600 (when (looking-at (wesnoth-element-closing))
1601 (wesnoth-jump-to-matching)))
1603 (defun wesnoth-check-wml (&optional preserve-buffer)
1604 "Perform context-sensitive analysis of WML-code.
1605 If PRESERVE-BUFFER is non-nil, the contents of *WML* will be
1606 maintained through successive calls."
1607 (interactive)
1608 ;; Temporarily cache all tag-data.
1609 (setq wesnoth-tmp-tag-data (wesnoth-refresh-wml-data))
1610 (wesnoth-update-project-information)
1611 (if (fboundp 'delete-overlay)
1612 (dolist (overlay (overlays-in (point-min) (point-max)))
1613 (if (eq 'wesnoth-warning-face (overlay-get overlay 'face))
1614 (delete-overlay overlay)))
1615 (setq wesnoth-warning-markers nil))
1616 (when (= 0 (hash-table-count wesnoth-tag-hash-table))
1617 (error "WML data not available; unable to generate report"))
1618 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1619 (let ((unmatched '())
1620 (source-buffer (buffer-name))
1621 (outbuf (get-buffer-create "*WML*"))
1622 (last-match-pos 1)
1623 (details nil)
1624 (foreach '()))
1625 (save-excursion
1626 (set-buffer outbuf)
1627 (let ((buffer (buffer-name))
1628 (buffer-read-only nil))
1629 (if preserve-buffer
1630 (goto-char (point-max))
1631 (erase-buffer))
1632 (message (format "Checking %s..." source-buffer))))
1633 (save-excursion
1634 (goto-char (point-min))
1635 (while (setq details (wesnoth-find-next
1636 '(tag-opening tag-closing preprocessor attribute
1637 macro)))
1638 (save-excursion
1639 (goto-char (match-beginning 0))
1640 (cond ((nth 3 (parse-partial-sexp last-match-pos (point)))
1641 nil)
1642 ((eq (car details) 'macro)
1643 (dolist (macro (save-match-data
1644 (wesnoth-extract-macro-details
1645 (match-string-no-properties 0))))
1646 (unless (assoc macro
1647 (wesnoth-merge-macro-data
1648 wesnoth-macro-data
1649 (wesnoth-macro-additions)
1650 wesnoth-local-macro-data
1651 (wesnoth-macro-arguments)))
1652 (wesnoth-check-process "Unknown macro: '%s'"
1653 macro)))
1654 (save-match-data
1655 (when
1656 (looking-at
1657 "{\\(FOREACH\\|NEXT\\).*[\t ]+\
1658 \\(\\(?:\\w\\|_\\)+\\)}")
1659 (if (string= (match-string-no-properties 1) "FOREACH")
1660 (setq foreach
1661 (cons (match-string-no-properties 2) foreach))
1662 (if (string= (match-string-no-properties 1) "NEXT")
1663 (progn
1664 (unless (string= (car foreach)
1665 (match-string-no-properties 2))
1666 (wesnoth-check-process
1667 (concat "NEXT does not match corresponding "
1668 "FOREACH: '%s' found; '%s' expected.")
1669 (match-string-no-properties 2)
1670 (car foreach)))
1671 (setq foreach (cdr foreach))))))))
1672 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
1673 (unless (wesnoth-check-element-type 0)
1674 (wesnoth-check-process
1675 "Tag not available in this context: '%s'"
1676 (match-string-no-properties 1)))
1677 (setq unmatched (cons (match-string-no-properties 1)
1678 unmatched)))
1679 ((looking-at
1680 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1681 "\\( \\(\\w\\|_\\)+\\)*"))
1682 (unless (match-string-no-properties 2)
1683 (wesnoth-check-process
1684 (concat "Preprocessor statement has no argument: "
1685 (match-string-no-properties 1))))
1686 (unless (string= (match-string-no-properties 1) "#undef")
1687 (setq unmatched (cons (match-string-no-properties 1)
1688 unmatched))))
1689 ((looking-at wesnoth-preprocessor-closing-regexp)
1690 (when (and unmatched
1691 (not (string-match
1692 (cdr (assoc (match-string-no-properties 1)
1693 '(("enddef" . "#define")
1694 ("endif" . "#ifn?def"))))
1695 (car unmatched))))
1696 (wesnoth-check-process
1697 "Preprocessor statement does not nest correctly"))
1698 (setq unmatched (cdr unmatched)))
1699 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)[\t ]*=\\(.+\\)?")
1700 (unless (wesnoth-check-element-type 1)
1701 (wesnoth-check-process
1702 "Key not available in this context: '%s'"
1703 (match-string-no-properties 1)))
1704 (unless (match-string 3)
1705 (wesnoth-check-process
1706 "Attribute has no value")))
1707 ((looking-at "[\t ]*#else")
1708 (unless (string-match "ifn?def" (car unmatched))
1709 (if (string= (car unmatched) "#define")
1710 (wesnoth-check-process "Expecting: '%s'"
1711 (car unmatched))
1712 (wesnoth-check-process "Expecting: '[/%s]'"
1713 (car unmatched)))))
1714 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1715 (when (and unmatched
1716 (not (string= (match-string-no-properties 1)
1717 (car unmatched))))
1718 (wesnoth-check-process
1719 "Expecting '%s'"
1720 (or (cdr (assoc (car unmatched)
1721 '(("#define" . "#enddef")
1722 ("#ifdef" . "#endif")
1723 ("#ifndef" . "#endif"))))
1724 (concat "[/" (car unmatched) "]"))))
1725 (setq unmatched (cdr unmatched)
1726 last-match-pos (point))))))
1727 (when foreach
1728 (dolist (var foreach)
1729 (wesnoth-check-process "Unmatched FOREACH: '%s'" var)))
1730 (when unmatched
1731 (dolist (element unmatched)
1732 (wesnoth-check-process "Unmatched element: '%s'" element))))
1733 (when (or (not preserve-buffer)
1734 (and preserve-buffer (not (cdr wesnoth-found-cfgs))))
1735 (save-excursion
1736 (setq wesnoth-define-blocks nil
1737 wesnoth-tmp-tag-data nil)
1738 (set-buffer outbuf)
1739 (toggle-read-only t)
1740 (let ((buffer (buffer-name))
1741 (buffer-read-only nil))
1742 (display-buffer outbuf t)
1743 (let ((warnings (- (wesnoth-line-number-at-pos
1744 (goto-char (point-max))) 1)))
1745 (insert (format (concat "\nCheck complete. %d warning"
1746 (if (= warnings 1) "." "s.")) warnings)))
1747 (message (format "Checking %s...done" source-buffer)))))))
1749 (defun wesnoth-check-directory (dir)
1750 "Check all WML in DIR.
1751 Where DIR is a directory containing WML files."
1752 (interactive "DDirectory: ")
1753 (setq wesnoth-found-cfgs nil)
1754 (when (member (mapcar 'buffer-name (buffer-list)) "*WML*")
1755 (kill-buffer "*WML*"))
1756 (wesnoth-fetch-all-dirs dir)
1757 (let ((tmp-cfgs wesnoth-found-cfgs))
1758 (while tmp-cfgs
1759 (find-file (car wesnoth-found-cfgs))
1760 (kill-buffer)
1761 (setq tmp-cfgs (cdr tmp-cfgs))))
1762 (save-excursion
1763 (while wesnoth-found-cfgs
1764 (find-file (car wesnoth-found-cfgs))
1765 (wesnoth-check-wml t)
1766 (kill-buffer)
1767 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))
1769 (defun wesnoth-file-cfg-p (file)
1770 "Return non-nil if FILE has a '.cfg' extension."
1771 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
1773 (defun wesnoth-fetch-all-dirs (dir)
1774 "Retrieve a list of subdirectories to scan.
1775 DIR is the directory to check."
1776 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
1777 (while dirs-to-scan
1778 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
1779 dirs-to-scan)))))
1781 (defun wesnoth-files-in-dir (dir)
1782 "Add cfgs to `wesnoth-files-in-dir'.
1783 Returns a list of sub-directories in DIR."
1784 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
1785 (when cfgs
1786 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
1787 (let ((dirs '()))
1788 (dolist (file (directory-files dir t))
1789 (unless (string-match "^\\..*" (file-name-nondirectory file))
1790 (cond ((file-directory-p file)
1791 (add-to-list 'dirs file))
1792 ((wesnoth-file-cfg-p file)
1793 (add-to-list 'wesnoth-found-cfgs file)))))
1794 dirs))
1796 (defun wesnoth-cfg-files-in-dir (dir)
1797 "Return all cfg files in DIR."
1798 (let ((result '()))
1799 (dolist (file (directory-files dir t))
1800 (and (wesnoth-file-cfg-p file)
1801 (add-to-list 'result file)))
1802 result))
1805 ;;; wesnoth-mode
1806 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
1807 "Major mode for editing WML."
1808 (kill-all-local-variables)
1809 (use-local-map wesnoth-mode-map)
1810 (setq major-mode 'wesnoth-mode)
1811 (setq mode-name "WML")
1812 (set-syntax-table wesnoth-syntax-table)
1813 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
1814 (set (make-local-variable 'comment-start) "#")
1815 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
1816 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
1817 (set (make-local-variable 'wesnoth-warning-markers) '())
1818 (set (make-local-variable 'font-lock-defaults)
1819 '(wesnoth-font-lock-keywords
1820 nil t nil nil
1821 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
1822 (setq indent-tabs-mode nil)
1823 (easy-menu-add wesnoth-menu wesnoth-mode-map)
1824 (wesnoth-refresh-wml-data)
1825 (wesnoth-update-project-information)
1826 (run-hooks 'wesnoth-mode-hook))
1828 (provide 'wesnoth-mode)
1830 ;;; wesnoth-mode.el ends here