Add support for alternate completion functions. Ido now supported.
[wesnoth-mode.git] / wesnoth-mode.el
blob9e63f2893f9f040be315675c6d25eb73a1e741f8
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-use-alternate-completion 'guess
217 "Use the specified completion method if it is available.
218 Possible values are `guess', `ido' and nil. `guess'
219 checks for packages which provide completion functions, and uses
220 when available; `ido' will try to use `ido-completing-read'; and
221 nil will use the built-in `completing-read' function.
222 Currently ido is the only alternate completion method available.
223 Patches for other completion functions are welcome."
224 :type '(choice (const :tag "ido completion" guess)
225 (const :tag "ido completion" ido)
226 (const :tag "standard completion" nil))
227 :group 'wesnoth-mode)
229 (defcustom wesnoth-indent-savefile t
230 "Non-nil means to use the current indentation conventions.
231 If nil, use the old convention for indentation.
232 The current convention is all attributes are indented a level deeper
233 than their parent; in the past attributes were indented to the same
234 level as their parent.")
236 (defcustom wesnoth-base-indent 4
237 "The number of columns to indent WML."
238 :type 'integer
239 :group 'wesnoth-mode)
241 (defcustom wesnoth-allow-accurate-validation nil
242 "Whether to allow a more accurate method of WML validation.
243 If non-nil, allow a more accurate method (which is able to better
244 determine the current element's parent) to be used. Note that
245 this may cause WML validation to slow significantly when checking
246 error-prone or messy WML. If clean WML is generally used, it is
247 recommended to set this to non-nil."
248 :type 'boolean
249 :group 'wesnoth-mode)
251 (defconst wesnoth-preprocessor-regexp
252 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\
253 \\|\\(ifn?\\|un\\)def \\)"
254 "Regular expression to match all preprocessor statements.")
256 (defconst wesnoth-preprocessor-opening-regexp
257 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
258 "Regular expression to match \"opening\" preprocessor statements.")
260 (defconst wesnoth-preprocessor-closing-regexp
261 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
262 "Regular expression to match \"closing\" preprocessor statements.")
264 (defvar wesnoth-define-blocks '()
265 "Cache of all toplevel #define and #enddef pairs.")
267 (defvar wesnoth-history-list '()
268 "History of inserted WML elements.")
270 (defvar wesnoth-warning-markers '()
271 "Markers for warnings in the buffer.")
273 (defvar wesnoth-found-cfgs '()
274 "Temporary list of all .cfg files found.")
276 (defvar wesnoth-mode-hook nil)
278 (defface wesnoth-warning-face
279 '((t (:underline "tomato1")))
280 "Face to use for warnings in wesnoth-mode"
281 :group 'wesnoth-mode)
283 (defvar wesnoth-mode-map
284 (let ((map (make-sparse-keymap)))
285 (define-key map (kbd "C-M-a") 'wesnoth-backward-element)
286 (define-key map (kbd "C-M-e") 'wesnoth-forward-element)
287 (define-key map (kbd "C-M-n") 'wesnoth-forward-list)
288 (define-key map (kbd "C-M-p") 'wesnoth-backward-list)
289 (define-key map (kbd "C-M-d") 'wesnoth-down-list)
290 (define-key map (kbd "C-M-u") 'wesnoth-backward-up-list)
291 (define-key map (kbd "C-M-k") 'wesnoth-kill-block)
292 (define-key map (kbd "C-M-SPC") 'wesnoth-mark-block)
293 (define-key map (kbd "C-m") 'wesnoth-newline)
294 (define-key map (kbd "C-j") 'wesnoth-newline-and-indent)
295 (define-key map (kbd "C-c C-c") 'wesnoth-check-wml)
296 (define-key map (kbd "C-c C-a") 'wesnoth-complete-attribute)
297 (define-key map (kbd "C-c C-t") 'wesnoth-complete-tag)
298 (define-key map (kbd "C-c C-p") 'wesnoth-complete-preprocessor)
299 (define-key map (kbd "C-c C-u") 'wesnoth-update-project-information)
300 (define-key map (kbd "M-TAB") 'wesnoth-complete-tag)
301 (define-key map (kbd "C-c C-m") 'wesnoth-complete-macro)
302 (define-key map (kbd "C-c C-o") 'wesnoth-jump-to-matching)
303 (define-key map (kbd "C-c C-f") 'wesnoth-forward-warning)
304 (define-key map (kbd "C-x `") 'wesnoth-forward-warning)
305 (define-key map (kbd "C-c C-b") 'wesnoth-backward-warning)
306 (define-key map (kbd "C-c C-/") 'wesnoth-insert-missing-closing)
307 (define-key map (kbd "TAB") 'wesnoth-indent-or-complete)
308 map)
309 "Keymap used in `wesnoth-mode'.")
311 (easy-menu-define wesnoth-menu wesnoth-mode-map "Menu for wesnoth-mode"
312 '("WML"
313 ["Check WML" wesnoth-check-wml t]
314 ["Indent or Complete" wesnoth-indent-or-complete t]
315 ["Indent buffer" (lambda ()
316 (interactive)
317 (wesnoth-indent-region (point-min) (point-max))) t]
318 ["Insert Tag" wesnoth-complete-tag t]
319 ["Insert Attribute" wesnoth-complete-attribute t]
320 ["Insert Macro" wesnoth-complete-macro t]
321 ["Insert Preprocessor" wesnoth-complete-preprocessor t]
322 ["Insert Missing Tag" wesnoth-insert-missing-closing t]
323 ["Jump to Matching" wesnoth-jump-to-matching t]
324 ["Update Macros" wesnoth-update-project-information t]))
326 (defvar wesnoth-syntax-table
327 (let ((wesnoth-syntax-table (make-syntax-table)))
328 (modify-syntax-entry ?# "<" wesnoth-syntax-table)
329 (modify-syntax-entry ?\" "\"" wesnoth-syntax-table)
330 (modify-syntax-entry ?= "." wesnoth-syntax-table)
331 (modify-syntax-entry ?| "w" wesnoth-syntax-table)
332 (modify-syntax-entry ?_ "_" wesnoth-syntax-table)
333 (modify-syntax-entry ?- "_" wesnoth-syntax-table)
334 (modify-syntax-entry ?. "_" wesnoth-syntax-table)
335 (modify-syntax-entry ?\n ">" wesnoth-syntax-table)
336 (modify-syntax-entry ?\r ">" wesnoth-syntax-table)
337 wesnoth-syntax-table)
338 "Syntax table for `wesnoth-mode'.")
340 ;; Prevents automatic syntax-highlighting of elements which might be
341 ;; pre-processor statements.
342 (defvar wesnoth-syntactic-keywords
343 (list
344 '("\\([\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\
345 \\(?:ifn?\\|un\\)def \\)\\)\\)" 1 "w"))
346 "Syntactic keywords for preprocessor statements within `wesnoth-mode'.")
348 (defvar wesnoth-font-lock-keywords
349 (list
350 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face)
351 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
352 font-lock-keyword-face)
353 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
354 2 font-lock-function-name-face)
355 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face))
356 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face))
357 '("}" . font-lock-function-name-face)
358 '("[\t ]*\\(\\[/?[^$]\\(\\w\\|_\\)+\\]\\)" 1 font-lock-type-face)
359 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face)
360 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)[\t ]*="
361 1 font-lock-variable-name-face))
362 "Syntax highlighting for `wesnoth-mode'.")
364 (defun wesnoth-element-closing (&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 #enddef preprocessor."
368 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\]\\|"
369 (if limited
370 "#enddef"
371 "#end\\(?:def\\|if\\)")
372 (if (and (not wesnoth-indent-preprocessor-bol) limited)
373 "\\|#endif"
375 "\\)"))
377 (defun wesnoth-element-opening (&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 preprocessor."
381 (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define "
382 (if limited
383 "\\|{FOREACH .+}"
384 "\\|#ifn?def ")
385 (if (and (not wesnoth-indent-preprocessor-bol) limited)
386 "\\|#ifn?def \\|#else"
388 "\\)"))
390 (defun wesnoth-element (&optional limited)
391 "Return the regexp to match a closing element.
392 If LIMITED is non-nil, return a regexp which matches only the
393 #define and #enddef preprocessors."
394 (concat "^[\t ]*\\(\\[[+/]?\\(\\w\\|_\\)+\\]?\\|"
395 (if limited
396 "#define \\|#enddef"
397 (substring wesnoth-preprocessor-regexp 5))
398 (if (and (not wesnoth-indent-preprocessor-bol) limited)
399 "\\|#\\(ifn?def \\|endif\\|else\\)"
401 "\\)"))
403 (defun wesnoth-find-next (type)
404 "Find the next element of TYPE.
405 TYPE is a symbol representing an element type, or a list of
406 element types to find."
407 (let ((element (wesnoth-next-element)))
408 (while (and element (if (listp type)
409 (not (member (car element) type))
410 (not (eq (car element) type))))
411 (setq element (wesnoth-next-element)))
412 (when (if (listp type)
413 (member (car element) type)
414 (eq (car element) type))
415 element)))
417 (defun wesnoth-next-element ()
418 "Move to the next element in the buffer.
419 Return non-nil when an element is found. Otherwise, return nil."
420 (interactive)
421 (save-match-data
422 (and (or (eolp) (looking-at "[}\t ]"))
423 (search-forward-regexp "[^}
424 \t ]" (point-max) t)
425 (forward-char -1)))
426 (let ((details (wesnoth-element-type (point))))
427 (save-match-data
428 (when (nth 2 details)
429 (goto-char (nth 2 details))
430 (while (nth 3 (parse-partial-sexp
431 (save-excursion (search-backward-regexp
432 (wesnoth-element t) (point-min) t)
433 (point))
434 (point)))
435 (search-forward "\"" (point-max) t))))
436 details))
438 (defun wesnoth-find-previous (type)
439 "Find the previous element of TYPE.
440 TYPE is a symbol representing an element type, or a list of
441 element types to find."
442 (let ((element (wesnoth-previous-element)))
443 (while (and element (if (listp type)
444 (not (member (car element) type))
445 (not (eq (car element) type))))
446 (setq element (wesnoth-previous-element)))
447 (when (if (listp type)
448 (member (car element) type)
449 (eq (car element) type))
450 element)))
452 ;; TODO: This is currently quite inefficient, and therefore is only used where
453 ;; necessary. (i.e., when the parent element is initially may not be
454 ;; correctly detected.)
455 (defun wesnoth-previous-element ()
456 "Move to the previous element in the buffer.
457 Return non-nil when an element is found. Otherwise, return nil."
458 (interactive)
459 (if (= (point) (wesnoth-wml-start-pos))
461 (let ((init-element (save-excursion (wesnoth-next-element)))
462 last-element cur-element)
463 (save-match-data
464 (search-backward-regexp "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|#ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)" (point-min) t))
465 (while (< (cadr (setq cur-element (wesnoth-next-element)))
466 (cadr init-element))
467 (when (car cur-element)
468 (setq last-element cur-element)))
469 (goto-char (or (cadr last-element) (cadr init-element) (point-min))))
470 ;; Determine whether currently within a string, and move to its beginning
471 ;; where necessary.
472 (save-excursion
473 (let ((details (wesnoth-element-type (point))))
474 (save-match-data
475 (when (cadr details)
476 (goto-char (cadr details))
477 (while (nth 3 (parse-partial-sexp
478 (save-excursion (search-backward-regexp
479 (wesnoth-element t) (point-min) t)
480 (point))
481 (point)))
482 (search-backward "\"" (point-min) t))))
483 details))))
485 (defun wesnoth-element-type (point)
486 "Return details regarding the element at POINT.
487 A list is returned, the elements of the list represent the
488 following, respectively: A symbol representing the type of
489 element; the position of the start of the element and the
490 position of the end of the element. POINT must be at the start
491 of the element."
492 (let ((element-matches
493 '(("{\\(.*?[/\]\\)+}" . nil) ;; pathnames
494 ("{\\(\\w\\|_\\)+" . macro)
495 ("\\[\\+?[^/]+?\\]" . tag-opening)
496 ("\\[/.+?\\]" . tag-closing)
497 ("\\(\\w\\|_\\)+[\t ]*=" . attribute)
498 ("#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\
499 \\(ifn?\\|un\\)def \\)"
500 . preprocessor)
501 ("#.*$" . comment)
502 ("[^\t ]+") . nil)))
503 (catch 'result
504 (dolist (pair element-matches)
505 (when (looking-at (car pair))
506 (throw 'result (list (cdr pair)
507 (match-beginning 0)
508 (min (save-excursion (forward-line 1) (point))
509 (match-end 0)))))))))
511 (defun wesnoth-estimate-element-type (point)
512 "Return match data for a partial element at POINT."
513 (save-excursion
514 (goto-char point)
515 (let ((element-matches
516 '(("{\\(.*?[/\]\\)+$" . nil) ; pathnames
517 ("{\\(\\w\\|_\\)*$" . macro)
518 ("\\[/\\(\\w\\|_\\)*$" . tag-closing)
519 ("\\[\\+?\\(\\w\\|_\\)*$" . tag-opening)
520 ("^[\t ]*\\(\\w\\|_\\)+$" . attribute)
521 ("[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\
522 \\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
523 . nil) ; not a partial match
524 ("[\t ]*#\\w*$" . preprocessor))))
525 (catch 'result
526 (dolist (pair element-matches)
527 (when (looking-at (car pair))
528 (throw 'result (list (cdr pair)
529 (match-beginning 0)
530 (match-end 0)))))))))
532 (defun wesnoth-guess-element-type (point)
533 "Return details for the the element near POINT.
534 Locate the start of the element before determining details.
535 BOUND is the limit to search backwards."
536 (let ((details (wesnoth-estimate-element-type point))
537 (bound (save-excursion
538 (goto-char point)
539 (beginning-of-line)
540 (point))))
541 (while (and (not (car details)) (> point bound))
542 (setq point (1- point)
543 details (wesnoth-estimate-element-type point)))
544 (and (nth 1 details)
545 (>= (point) (nth 1 details))
546 (nth 2 details)
547 (<= (point) (nth 2 details))
548 details)))
551 ;;; Insertion and completion
552 (defmacro wesnoth-completion-method (prompt completions)
553 "Determine a suitable completion method for use.
554 See `wesnoth-use-alternate-completion' for more information."
555 (cond
556 ((and (featurep 'ido)
557 (or (eq wesnoth-use-alternate-completion 'ido)
558 (eq wesnoth-use-alternate-completion 'guess)))
559 `(ido-completing-read ,prompt ,completions
560 nil nil element
561 'wesnoth-history-list))
563 `(completing-read ,prompt ,completions
564 nil nil element
565 'wesnoth-history-list))))
567 (defmacro wesnoth-element-completion (completions prompt partial
568 &optional completep)
569 "Process completion of COMPLETIONS, displaying PROMPT.
570 PARTIAL is the partial string on which to attempt completion.
571 If COMPLETEP is non-nil, do not prompt if no completion is found."
572 `(let* ((element (when ,partial (try-completion ,partial ,completions))))
573 (cond ((eq element t)
574 ,partial)
575 ((and ,completep (null element))
576 nil)
577 ((and element (eq (try-completion element ,completions) t))
578 element)
579 ((> (length (all-completions (or element "") ,completions)) 1)
580 (wesnoth-completion-method ,prompt ,completions))
581 ((= (length ,completions) 1)
582 (car ,completions))
584 element))))
586 (defun wesnoth-active-parent-tag ()
587 "Return the name of the active parent tag.
588 Finds the relevant parent tag, ignoring any conditional tags."
589 (save-excursion
590 (let ((parent (wesnoth-parent-tag)))
591 (while (and (stringp (car parent))
592 (string-match "else\\|then"
593 (car parent)))
594 (goto-char (cdr parent))
595 (setq parent (wesnoth-parent-tag))
596 (when (string= (car parent) "if")
597 (goto-char (cdr parent))
598 (setq parent (wesnoth-parent-tag))))
599 (car parent))))
601 (defun wesnoth-parent-tag (&optional exact)
602 "Return the name of the parent tag.
603 If the parent is a preprocessor statement, return non-nil.
604 If the element does not have a parent, return nil.
605 Otherwise, return a string containing the name of the parent tag.
606 If EXACT is non-nil a more accurate, yet slower method is used."
607 (save-excursion
608 (let ((start-point (point))
609 (depth 1))
610 (when (save-excursion (> (point) (progn (back-to-indentation)
611 (point))))
612 (end-of-line))
613 (while (and (> depth 0)
614 (if exact
615 (wesnoth-find-previous
616 '(tag-opening tag-closing preprocessor))
617 (search-backward-regexp (wesnoth-element t) (point-min) t)))
618 (if (string-match "[\t ]*\\[/\\|#enddef" (match-string 0))
619 (setq depth (1+ depth))
620 (setq depth (1- depth))))
621 (beginning-of-line)
622 (if (> depth 0)
623 (cons nil nil)
624 (when (looking-at (wesnoth-element-opening))
625 (let ((parent (match-string-no-properties 1))
626 (position (point)))
627 (if (or (string-match wesnoth-preprocessor-opening-regexp parent)
628 ;; Check if we're immediately within a macro
629 (and (goto-char start-point)
630 (search-backward-regexp "[}{]" (point-min) t)
631 (string= (match-string 0) "{")
632 (goto-char start-point)
633 (not (and (search-backward parent (point-min) t)
634 (search-backward-regexp "[}{]" (point-min)
636 (string= (match-string 0) "{")))))
637 (cons t position)
638 (cons (substring parent (if (string-match "[\\+.+]" parent)
641 (1- (length parent))) position))))))))
643 (defun wesnoth-partial-macro-p ()
644 "Return non-nil if point is in a partial macro."
645 (save-excursion
646 (let ((opened 0))
647 (search-backward-regexp "{" (point-min) t)
648 (while (search-forward-regexp "[{}]" (point-max) t)
649 (if (string= (match-string 0) "{")
650 (setq opened (1+ opened))
651 (setq opened (1- opened))))
652 (> opened 0))))
654 (defun wesnoth-indent-or-complete (&optional elements)
655 "Indent or complete the line at point, depending on context.
656 ELEMENTS is the number of elements to wrap around if inserting
657 matching tags."
658 (interactive "P")
659 (or elements (setq elements 0))
660 (let ((details (wesnoth-guess-element-type (point))))
661 (cond
662 ((eq (car details) 'tag-opening)
663 (wesnoth-complete-tag elements t))
664 ((and (eq (car details) 'macro)
665 (wesnoth-partial-macro-p))
666 (wesnoth-complete-macro t))
667 ((eq (car details) 'preprocessor)
668 (wesnoth-complete-preprocessor elements t))
669 ((eq (car details) 'tag-closing)
670 ;; FIXME: Solve incorrect behaviour when partial closing is "[/"
671 (insert "a")
672 (and (wesnoth-insert-missing-closing t)
673 (delete-region (nth 1 details)
674 (save-excursion (beginning-of-line) (point))))
675 (end-of-line)
676 (wesnoth-indent))
677 ((eq (car details) 'attribute)
678 (wesnoth-complete-attribute t))
680 (wesnoth-indent)))))
682 (defun wesnoth-preprocessor-closed-p (preprocessor)
683 "Determine whether PREPROCESSOR has been closed.
684 PREPROCESSOR is a string matching the preprocessor statement to
685 be inserted."
686 (save-excursion
687 (back-to-indentation)
688 (wesnoth-jump-to-matching preprocessor)
689 (looking-at
690 (if (string= preprocessor "#define ")
691 "#enddef"
692 "#endif"))))
694 (defun wesnoth-complete-preprocessor (&optional elements completep)
695 "Complete and insert the preprocessor at point.
696 ELEMENTS is the number of elements to wrap around.
697 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
698 (interactive "P")
699 (or elements (setq elements 0))
700 (let* ((completions (wesnoth-emacs-completion-formats
701 '("define" "else" "ifdef" "ifndef"
702 "enddef" "endif" "undef")))
703 (partial (when completep
704 (save-excursion
705 (back-to-indentation)
706 (when (looking-at "#\\(\\w*\\)$")
707 (match-string-no-properties 1)))))
708 (preprocessor (wesnoth-element-completion
709 completions "Preprocessor: " partial completep))
710 (details (wesnoth-guess-element-type (point)))
711 (closedp
712 (save-excursion
713 (when preprocessor
714 (unless (string= "#" (substring preprocessor 0 1))
715 (setq preprocessor (concat "#" preprocessor)))
716 (when (string-match "#\\(define\\|ifn?def\\|undef\\)"
717 preprocessor)
718 (setq preprocessor (concat preprocessor " ")))
719 (when partial
720 (delete-region (nth 1 details) (nth 2 details)))
721 (wesnoth-preprocessor-closed-p preprocessor)))))
722 (when preprocessor
723 (when partial
724 (delete-region
725 (save-excursion
726 (progn (search-backward
727 "#" (save-excursion (back-to-indentation)
728 (point))
730 (point)))
731 (point)))
732 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor)
733 (not closedp))
734 (progn
735 (wesnoth-insert-tag elements preprocessor)
736 (forward-line -1)
737 (end-of-line))
738 (wesnoth-insert-element-separately preprocessor)))))
740 (defun wesnoth-macro-arguments ()
741 "Find any current macro arguments."
742 (let ((results '())
743 (depth (wesnoth-within-define (point))))
744 (save-excursion
745 (while (> depth 0)
746 (save-match-data
747 (search-backward-regexp
748 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
749 (point-min) t)
750 (when (<= (wesnoth-within-define (point)) depth)
751 (and (> depth 0)
752 (setq results
753 (append (mapcar (lambda (macro)
754 (list macro nil))
755 (split-string
756 (match-string-no-properties 1)))
757 results)))
758 (setq depth (1- depth)))))
759 results)))
761 (defun wesnoth-complete-macro (&optional completep)
762 "Complete and insert the macro at point.
763 If COMPLETEP is non-nil, attempt to complete the macro at point."
764 (interactive)
765 (wesnoth-update-project-information)
766 (let* ((macro-information (wesnoth-merge-macro-data
767 wesnoth-macro-data
768 (wesnoth-macro-additions)
769 wesnoth-local-macro-data
770 (wesnoth-macro-arguments)))
771 (completions (wesnoth-emacs-completion-formats
772 (mapcar 'car macro-information)))
773 (details (wesnoth-guess-element-type (point)))
774 (partial (when
775 (save-excursion
776 (and completep
777 (eq (car details) 'macro)
778 (goto-char (cadr details))
779 (looking-at "{\\(\\(\\w\\|_\\)*\\)")))
780 (match-string-no-properties 1)))
781 (macro (wesnoth-element-completion completions "Macro: " partial
782 completep))
783 (args (cadr (assoc macro macro-information))))
784 (when macro
785 (if partial
786 (progn
787 ;; Delete the region corresponding to the current macro.
788 (delete-region (nth 1 details) (nth 2 details))
789 (insert "{" macro (if args " }" "}"))
790 (save-excursion
791 (wesnoth-indent)))
792 (wesnoth-insert-and-indent "{" macro (if args " }" "}")))
793 (forward-char -1)
794 (when args
795 (let ((input (read-string (concat (car args) ": "))))
796 (insert input (if (and (cdr args)
797 (not (string= input "")))
798 " " ""))
799 (while (and (setq args (cdr args)) (not (string= input "")))
800 (insert (setq input (read-string (concat (car args) ": ")))
801 (if (and (not (string= input ""))
802 (cdr args))
803 " " "")))))
804 (when (null args) (forward-char 1)))))
806 (defun wesnoth-complete-attribute (&optional completep)
807 "Insert the attribute at point.
808 If COMPLETEP is non-nil, attempt to complete the attribute at point."
809 (interactive)
810 (wesnoth-refresh-wml-data)
811 (let* ((details (save-excursion
812 (back-to-indentation)
813 (wesnoth-guess-element-type (point))))
814 (completions (save-excursion (when (nth 1 details)
815 (goto-char (nth 1 details)))
816 (wesnoth-build-completion 1)))
817 (partial (when completep
818 (when (save-excursion
819 (back-to-indentation)
820 (looking-at "\\(\\(\\w\\|_\\)+\\)"))
821 (match-string-no-properties 1))))
822 (attribute (wesnoth-element-completion completions "Attribute: "
823 partial completep)))
824 (when attribute
825 (if partial
826 (progn
827 (delete-region (nth 1 details) (nth 2 details))
828 (insert attribute "="))
829 (wesnoth-insert-element-separately attribute
830 (if (string-match "=" attribute)
832 "=")))
833 (save-excursion
834 (wesnoth-indent)))))
836 (defun wesnoth-complete-tag (&optional elements completep)
837 "Complete and insert the tag at point.
838 ELEMENTS is the number of elements to wrap around.
839 If COMPLETEP is non-nil, attempt to complete tag at point."
840 (interactive "P")
841 (or elements (setq elements 0))
842 (let* ((details (wesnoth-guess-element-type (point)))
843 (completions (save-excursion (and (nth 1 details)
844 (goto-char (nth 1 details)))
845 (wesnoth-build-completion 0)))
846 (partial (save-excursion
847 (when (and completep
848 (eq (car details) 'tag-opening)
849 (goto-char (cadr details))
850 (looking-at "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$"))
851 (match-string-no-properties 1))))
852 (tag (wesnoth-element-completion completions "Tag: " partial
853 completep))
854 (closedp
855 (save-excursion
856 (wesnoth-jump-to-matching (concat "[" tag "]"))
857 (back-to-indentation)
858 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
859 (string= tag (match-string 1))))))
860 (if tag
861 (progn
862 (if completep
863 (progn
864 (delete-region (nth 1 details) (nth 2 details))
865 (if closedp
866 (progn
867 (wesnoth-insert-and-indent "[" tag "]")
868 (end-of-line))
869 (wesnoth-insert-tag elements tag)))
870 (wesnoth-insert-tag elements tag)))
871 (or completep (wesnoth-insert-tag elements)))))
873 (defun wesnoth-build-completion (position)
874 "Create a new list for tag completion if necessary.
875 Rebuilding list is required for versions of GNU Emacs earlier
876 than 22. POSITION is the argument passed to `nth' for
877 `wesnoth-tag-data'."
878 (interactive "P")
879 (let ((parent (wesnoth-active-parent-tag))
880 (tag-data (wesnoth-refresh-wml-data)))
881 (wesnoth-emacs-completion-formats
882 (if (or (stringp parent) (null parent))
883 (nth position (gethash parent wesnoth-tag-hash-table))
884 (mapcar 'car tag-data)))))
886 (defun wesnoth-emacs-completion-formats (candidates)
887 "Return the completions in the correct format for `emacs-major-version'.
888 CANDIDATES is a list of all possible completions."
889 (if (> emacs-major-version 21)
890 candidates
891 (let ((tags '())
892 (iter 0))
893 (dolist (tag candidates)
894 (setq iter (1+ iter))
895 (setq tags (append tags (list (cons tag iter)))))
896 tags)))
898 (defun wesnoth-insert-tag (&optional elements tagname)
899 "Insert the specified opening tag and it's matching closing tag.
900 Both the opening and closing tags will be placed on their own
901 lines with point positioned between them. Completion of tags at
902 the prompt uses `wesnoth-tags-list'.
904 ELEMENTS is specifies the number of following blocks which the
905 tag should wrap around.
907 TAGNAME is the name of the tag to be inserted."
908 (interactive "Ps")
909 (unless tagname
910 (setq tagname (wesnoth-completion-method "Tag: "
911 (wesnoth-build-completion 0))))
912 (when (or (not elements)
913 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
914 wesnoth-preprocessor-regexp "\\)")))
915 (setq elements 0))
916 (let ((start (save-excursion (forward-line -1) (point)))
917 (end (unless (= elements 0)
918 ;; Work around some strange behaviour when the target is at the
919 ;; end of the buffer.
920 (save-excursion
921 (goto-char (point-max))
922 (beginning-of-line)
923 (unless (looking-at "^[\t ]*$")
924 (end-of-line)
925 (newline)))
926 (wesnoth-nth-pair-position elements))))
927 (if (string-match wesnoth-preprocessor-regexp tagname)
928 (wesnoth-insert-element-separately tagname)
929 (wesnoth-insert-element-separately "[" tagname "]"))
930 (save-excursion
931 (if end
932 (goto-char (marker-position end))
933 (newline (if (string-match wesnoth-preprocessor-regexp tagname) 1 2)))
934 (if (string-match wesnoth-preprocessor-opening-regexp tagname)
935 (wesnoth-insert-element-separately
936 (if (string= tagname "#define ")
937 "#enddef"
938 "#endif"))
939 (wesnoth-insert-element-separately "[/" (if (string-match "^+"
940 tagname)
941 (substring tagname 1)
942 tagname)
943 "]"))
944 (indent-region start (point) nil))
945 (unless end
946 (forward-line 1)))
947 (wesnoth-indent))
949 (defun wesnoth-insert-element-separately (&rest strings)
950 "Concatenate STRINGS and insert them on a line of their own."
951 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
952 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
953 (progn
954 (forward-line 1)
955 (end-of-line))
956 (end-of-line)
957 (newline))
958 (beginning-of-line)
959 (if (looking-at "^[\t ]*$")
960 (end-of-line)
961 (open-line 1)))
962 (insert (apply 'concat strings)))
964 (defun wesnoth-insert-missing-closing (&optional completep)
965 "Insert the next expected closing element at point.
966 If COMPLETEP is non-nil, do not move forward a line when scanning
967 for the matching tag."
968 (interactive)
969 (let ((match nil)
970 (skip t))
971 (save-excursion
972 (when (and (null completep)
973 (<= (point) (save-excursion (back-to-indentation) (point))))
974 (if (save-excursion (beginning-of-line)
975 (looking-at (wesnoth-element-opening)))
976 (forward-line -1)
977 (when
978 (save-excursion (beginning-of-line)
979 (looking-at (wesnoth-element-closing)))
980 (setq skip nil))))
981 (when (wesnoth-search-for-matching-tag
982 'search-backward-regexp (wesnoth-element-opening) 'point-min
983 (and skip (if completep nil 1)))
984 (setq match (and (looking-at (wesnoth-element-opening))
985 (match-string-no-properties 1)))))
986 (when match
987 (if (string= (substring match 0 1) "[")
988 (wesnoth-insert-element-separately
989 "[/"
990 (let ((tagname (substring match 1 (1- (length match)))))
991 (if (string-match "^+" tagname)
992 (substring tagname 1)
993 tagname))
994 "]")
995 (wesnoth-insert-element-separately
996 (cdr (assoc match '(("#define " . "#enddef")
997 ("#ifndef " . "#endif")
998 ("#ifdef " . "#endif")))))))
999 (wesnoth-indent)
1000 (end-of-line)
1001 match))
1003 (defun wesnoth-insert-and-indent (&rest args)
1004 "Concatenate and insert the given string(s) before indenting.
1006 ARGS is a list of strings to be inserted."
1007 (insert (apply 'concat args))
1008 (wesnoth-indent))
1010 (defun wesnoth-newline (&optional indent)
1011 "Indent the current line and create a newline.
1012 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1013 performed. Indentation can be forced by setting INDENT to
1014 non-nil."
1015 (interactive "P")
1016 (newline)
1017 (save-excursion
1018 (forward-line -1)
1019 (when (and (or wesnoth-auto-indent-flag indent)
1020 (not (looking-at "^[\t ]*$")))
1021 (wesnoth-indent))))
1023 ;;; Movement
1024 (defun wesnoth-navigate-element (repeat search-function bound)
1025 "Move point to the tag in the given direction REPEAT times.
1027 SEARCH-FUNCTION is the symbol of the function for searching in
1028 the required direction, with BOUND marking the furthest point to
1029 search."
1030 (or repeat (setq repeat 1))
1031 (while (> repeat 0)
1032 (and (eq search-function 'search-forward-regexp) (end-of-line))
1033 (funcall search-function (wesnoth-element-opening) bound t)
1034 (back-to-indentation)
1035 (setq repeat (1- repeat))))
1037 (defun wesnoth-nth-pair-position (count)
1038 "Return `point' after COUNT number of matching element pairs.
1039 COUNT is a positive number representing the number of balanced
1040 pairs to move across.
1041 `point' is returned as a marker object."
1042 (save-excursion
1043 (let ((failed nil))
1044 (if (> (point) (save-excursion (back-to-indentation) (point)))
1045 (end-of-line)
1046 (beginning-of-line))
1047 (while (> count 0)
1048 ;; Currently looking-at target tag. Stop here to avoid
1049 ;; incorrect nesting.
1050 (unless (wesnoth-search-for-matching-tag
1051 'search-forward-regexp (wesnoth-element-closing) 'point-max)
1052 (setq count 0)
1053 (setq failed t))
1054 (and (> (setq count (1- count)) 0) (forward-line 1)))
1055 (if failed
1056 (beginning-of-line)
1057 (end-of-line))
1058 (point-marker))))
1060 (defun wesnoth-forward-element (repeat)
1061 "Move point to the end of the next tag.
1062 REPEAT is an optional numeric argument. If REPEAT is non-nil,
1063 jump forward the specified number of tags."
1064 (interactive "p")
1065 (if (< repeat 0)
1066 (wesnoth-backward-element (abs repeat))
1067 (wesnoth-navigate-element repeat 'search-forward-regexp (point-max))))
1069 (defun wesnoth-backward-element (repeat)
1070 "Move point to the beginning of the previous tag.
1071 REPEAT is an optional numeric argument. If REPEAT is non-nil,
1072 jump backward the specified number of tags."
1073 (interactive "p")
1074 (if (< repeat 0)
1075 (wesnoth-forward-element (abs repeat))
1076 (wesnoth-navigate-element repeat 'search-backward-regexp (point-min))))
1078 (defun wesnoth-search-for-matching-tag (search-function
1079 search-string bound &optional skip)
1080 "Search for the matching tag for the current line.
1081 SEARCH-FUNCTION is the name of the function used to perform the search.
1082 SEARCH-STRING is a string representing the matching tag type.
1083 BOUND is the bound to be passed to the search function.
1084 If SKIP is non-nil, skip the first element and continue from there."
1085 (let ((depth 1))
1086 (when (and (or (and (numberp skip) (forward-line skip))
1087 (funcall search-function (wesnoth-element)
1088 (funcall bound) t))
1089 (or skip (not (string-match search-string (match-string 0)))))
1090 (while (and (> depth 0)
1091 (funcall search-function (wesnoth-element)
1092 (funcall bound) t))
1093 (if (string-match search-string (match-string 0))
1094 (setq depth (1- depth))
1095 (setq depth (1+ depth))))
1096 (= depth 0))))
1098 (defun wesnoth-jump-to-matching (&optional element)
1099 "Jump point to the matching opening/closing tag.
1100 ELEMENT is an element to find a match for."
1101 (interactive)
1102 (beginning-of-line)
1103 (let ((target nil)
1104 (first-element nil))
1105 (save-excursion
1106 (cond
1107 ((or (and (stringp element)
1108 (string-match (wesnoth-element-opening) element))
1109 (looking-at (wesnoth-element-opening)))
1110 (setq first-element (match-string-no-properties 0 element))
1111 (when (wesnoth-search-for-matching-tag
1112 'search-forward-regexp (wesnoth-element-closing) 'point-max
1113 (and (stringp element) 1))
1114 (beginning-of-line)
1115 (if (and (string-match wesnoth-preprocessor-opening-regexp
1116 first-element)
1117 (looking-at (wesnoth-element-closing)))
1118 (when (string= (match-string-no-properties 0)
1119 (cdr (assoc first-element
1120 '(("#define " . "#enddef")
1121 ("#ifndef " . "#endif")
1122 ("#ifdef " . "#endif")))))
1123 (setq target (point)))
1124 (setq target (point)))))
1125 ((or (and (stringp element)
1126 (string-match (wesnoth-element-closing) element))
1127 (looking-at (wesnoth-element-closing)))
1128 (end-of-line)
1129 (setq first-element (match-string-no-properties 0 element))
1130 (when (wesnoth-search-for-matching-tag
1131 'search-backward-regexp (wesnoth-element-opening)
1132 'wesnoth-wml-start-pos (and (stringp element) -1))
1133 (if (and (string-match wesnoth-preprocessor-closing-regexp
1134 first-element)
1135 (looking-at (wesnoth-element-opening)))
1136 (progn
1137 (when (or (and (string= "#enddef" first-element)
1138 (string= "#define "
1139 (match-string-no-properties
1140 0)))
1141 (and (string= "#endif" first-element)
1142 (string-match
1143 "#ifn?def "
1144 (match-string-no-properties
1145 0))))
1146 (setq target (point))))
1147 (setq target (point)))))
1149 (search-backward-regexp (wesnoth-element-opening) (point-min) t)
1150 (setq target (point)))))
1151 (if target
1152 (progn
1153 (goto-char target)
1154 (back-to-indentation))
1155 (when (interactive-p)
1156 (message "%s" "Tag does not appear to be matched")))))
1158 ;;; Indentation
1159 (defun wesnoth-wml-start-pos ()
1160 "Determine the position of `point' relative to where the actual WML begins.
1161 Return the likely starting position of the WML if it is found.
1162 Otherwise return nil."
1163 (save-excursion
1164 (goto-char (point-min))
1165 (when (search-forward-regexp (wesnoth-element) (point-max) t)
1166 (beginning-of-line)
1167 (point))))
1169 (defun wesnoth-first-column-indent-p (point)
1170 "Return non-nil if the current line should not be indented.
1172 POINT is the position in the buffer to check.
1173 CONTEXT represents the type of element which precedes the current element."
1174 (or (not (wesnoth-wml-start-pos))
1175 (<= (point) (wesnoth-wml-start-pos))
1176 (nth 3 (parse-partial-sexp
1177 (save-excursion (search-backward-regexp
1178 (wesnoth-element t) (point-min) t)
1179 (point))
1180 point))
1181 (and (looking-at wesnoth-preprocessor-regexp)
1182 wesnoth-indent-preprocessor-bol)))
1184 (defun wesnoth-indent ()
1185 "Indent the current line as WML."
1186 (save-excursion
1187 (beginning-of-line)
1188 (let* ((cur-indent 0)
1189 (context-data (wesnoth-determine-context (point)))
1190 (context (car context-data))
1191 (ref-indent (cdr context-data)))
1192 (unless (wesnoth-first-column-indent-p (point))
1193 (cond
1194 ((eq context 'opening)
1195 (if (and (looking-at "^[\t ]*#else")
1196 (not wesnoth-indent-preprocessor-bol))
1197 (setq cur-indent ref-indent)
1198 (if (or (and wesnoth-indent-savefile
1199 (or (looking-at "[\t ]*{NEXT ")
1200 (and (not
1201 (looking-at (wesnoth-element-closing t)))
1202 (not (looking-at "[\t ]*{NEXT ")))))
1203 (looking-at (wesnoth-element-opening t))
1204 (looking-at "[\t ]*{FOREACH "))
1205 (setq cur-indent (+ ref-indent wesnoth-base-indent))
1206 (setq cur-indent ref-indent))))
1207 ((eq context 'closing)
1208 (if (and (looking-at "^[\t ]*#else")
1209 (not wesnoth-indent-preprocessor-bol))
1210 (setq cur-indent (- ref-indent wesnoth-base-indent))
1211 (if (or (looking-at
1212 (concat "^[\t ]*\\(\\[/\\|\\#enddef"
1213 (if (not wesnoth-indent-preprocessor-bol)
1214 "\\|#endif"
1216 "\\)"))
1217 (and (not wesnoth-indent-savefile)
1218 (not (looking-at (wesnoth-element-opening t)))
1219 (not (looking-at "[\t ]*{FOREACH "))))
1220 (setq cur-indent (- ref-indent wesnoth-base-indent))
1221 (setq cur-indent ref-indent))))))
1222 (indent-line-to (max cur-indent 0))))
1223 (when (> (save-excursion (back-to-indentation) (point))
1224 (point))
1225 (back-to-indentation)))
1227 (defun wesnoth-within-define (position)
1228 "Determine whether point is currently inside a #define block.
1229 POSITION is the initial cursor position."
1230 (save-match-data
1231 (let ((depth 0)
1232 (defblocks (or wesnoth-define-blocks
1233 (wesnoth-find-macro-definitions))))
1234 (unless (equal (car defblocks) 'none)
1235 (dolist (element defblocks)
1236 (when (= (cadr (sort (append (mapcar 'marker-position
1237 (cadr element))
1238 (list position)) '>)) position)
1239 (setq depth (max (car element) depth)))))
1240 depth)))
1242 (defun wesnoth-find-macro-definitions ()
1243 "Return information regarding positioning of macro definitions."
1244 (save-excursion
1245 (goto-char (point-min))
1246 (let ((depth 0)
1247 openings cache)
1248 (while (search-forward-regexp "#define\\|#enddef" (point-max) t)
1249 (and (string= (match-string 0) "#define") (beginning-of-line))
1250 (setq depth
1251 (if (string= (match-string 0) "#define")
1252 (progn
1253 (add-to-list 'openings (point-marker))
1254 (1+ depth))
1255 (if openings
1256 (progn
1257 (add-to-list 'cache
1258 (list depth (list (car openings)
1259 (point-marker))))
1260 (setq openings (cdr openings))
1261 (1- depth))
1262 depth)))
1263 (end-of-line))
1264 (or cache (list 'none)))))
1266 (defun wesnoth-indent-region (start end)
1267 "Indent the region from START to END.
1268 Creates and destroys a cache of macro definition details as necessary."
1269 (interactive "r")
1270 (unwind-protect
1271 (save-excursion
1272 (goto-char end)
1273 (setq end (point-marker))
1274 (goto-char start)
1275 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1276 (or (bolp) (forward-line 1))
1277 (while (< (point) end)
1278 (if (looking-at "^[\t ]*$")
1279 (indent-line-to 0)
1280 (funcall indent-line-function))
1281 (forward-line 1)))
1282 (setq wesnoth-define-blocks nil)))
1284 (defun wesnoth-determine-context (position)
1285 "Determine the type of the last relevant element.
1286 POSITION is the buffer position of the element for which to
1287 determine the context."
1288 (save-excursion
1289 (let* ((elements (concat (substring (wesnoth-element t)
1290 0 (- (length (wesnoth-element t)) 2))
1291 "\\|{FOREACH .+}\\|{NEXT .+}\\)"))
1292 (match (or
1293 (and (search-backward-regexp
1294 elements (point-min) t)
1295 (progn
1296 (while
1297 (save-match-data
1298 (looking-at "^[\t ]*\\[[^/].+\\]\\[/.+\\]"))
1299 (search-backward-regexp elements
1300 (point-min) t))
1302 (match-string 1))
1303 ""))
1304 (depth (wesnoth-within-define position)))
1305 (while (and (wesnoth-wml-start-pos)
1306 (> (wesnoth-within-define (point)) depth)
1307 (not (= (point) (wesnoth-wml-start-pos))))
1308 (search-backward-regexp elements
1309 (wesnoth-wml-start-pos) t)
1310 (setq match (match-string 1)))
1311 (when (and (wesnoth-wml-start-pos)
1312 (= (point) (wesnoth-wml-start-pos))
1313 (= depth 0)
1314 (string-match "#define" match))
1315 ;; Found nothing of use; reset match and assume top-level tag.
1316 (setq match ""))
1317 (cond
1318 ((string-match (concat "\\[/\\|#enddef"
1319 (if (not wesnoth-indent-preprocessor-bol)
1320 "\\|#endif"
1321 ""))
1322 match)
1323 (cons 'closing (current-indentation)))
1324 ((string-match "{NEXT " match)
1325 (cons 'closing (if wesnoth-indent-savefile
1326 (- (current-indentation) wesnoth-base-indent)
1327 (current-indentation))))
1328 ((string-match (concat "\\[[^/]?\\|#define\\|{FOREACH "
1329 (if (not wesnoth-indent-preprocessor-bol)
1330 "\\|#ifn?def \\|#else"
1331 "")) match)
1332 (cons 'opening (current-indentation)))))))
1334 (defun wesnoth-newline-and-indent (&optional indent)
1335 "Indent both the current line and the newline created.
1336 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1337 performed.
1339 If the optional argument, INDENT is non-nil, force indentation to
1340 be performed."
1341 (interactive)
1342 (wesnoth-newline)
1343 (when (or wesnoth-auto-indent-flag indent)
1344 (wesnoth-indent)))
1346 ;;; WML checks
1347 (defun wesnoth-check-element-type (position &optional exact)
1348 "Determine the context of the element.
1349 POSITION is the position of the element in the list. If EXACT is
1350 non-nil, a more accurate, yet slower method is used. This is
1351 enabled when the attempt to match initially fails."
1352 (let ((parent (save-match-data (car (wesnoth-parent-tag exact))))
1353 (result '()))
1354 (if (or (stringp parent) (null parent))
1355 (setq result (member (match-string-no-properties 1)
1356 (nth position (gethash parent
1357 wesnoth-tag-hash-table))))
1358 (member (match-string-no-properties 1)
1359 (mapc
1360 '(lambda (x)
1361 (let ((value (nth position (cdr x))))
1362 (and value (mapc '(lambda (y)
1363 (setq result (cons y result)))
1364 value))))
1365 (or wesnoth-tmp-tag-data (wesnoth-refresh-wml-data)))))
1366 ;; If unsuccessful and more accurate checking is available, use it.
1367 ;; Otherwise, return the result found.
1368 (if (and wesnoth-allow-accurate-validation (not exact) (not result))
1369 (wesnoth-check-element-type position t)
1370 result)))
1372 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
1373 (defun wesnoth-line-number-at-pos (&optional pos)
1374 "Return (narrowed) buffer line number at position POS.
1375 If POS is nil, use current buffer location.
1376 Counting starts at (point-min), so the value refers
1377 to the contents of the accessible portion of the buffer."
1378 (let ((opoint (or pos (point))) start)
1379 (save-excursion
1380 (goto-char (point-min))
1381 (setq start (point))
1382 (goto-char opoint)
1383 (forward-line 0)
1384 (1+ (count-lines start (point))))))
1386 (defun wesnoth-check-output (buffer format-string &rest args)
1387 "Output the string as passed to `format'.
1388 BUFFER is the buffer to output the result.
1389 FORMAT-STRING is the string as the first argument of `format'.
1390 ARGS is any additional data required by `format' to handle FORMAT-STRING."
1391 (save-excursion
1392 (let ((lnap (wesnoth-line-number-at-pos))
1393 (source-buffer (buffer-name)))
1394 (set-buffer buffer)
1395 (let ((buffer-read-only nil))
1396 (insert (apply 'format (concat "%s: %d: " format-string "\n")
1397 source-buffer lnap args))))))
1399 (defun wesnoth-extract-macro-details (macro-arguments)
1400 "Return a list of all macros in MACRO-ARGUMENTS."
1401 (when macro-arguments
1402 (let ((results '()))
1403 (dolist (macro (split-string macro-arguments "[{}][\t ]*"))
1404 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
1405 macro)
1406 (add-to-list 'results (match-string-no-properties 1 macro))))
1407 results)))
1409 (defmacro wesnoth-check-process (format-string &rest args)
1410 "Output to buffer where requested and position overlays as required.
1411 FORMAT-STRING is the string to pass as the first argument to
1412 `format' for the error. ARGS is a list of arguments required by
1413 FORMAT-STRING."
1414 `(progn
1415 (wesnoth-check-output outbuf ,format-string ,@args)
1416 (wesnoth-place-overlay (match-beginning 0) (match-end 0))))
1418 (defmacro wesnoth-overlay-at-pos-p (position)
1419 "Return non-nil when there is an overlay at POSITION."
1420 `(and (overlays-at (goto-char ,position))
1421 (overlay-get (car (overlays-at (point))) 'wesnoth-error)
1422 (overlay-start (car (overlays-at (point))))))
1424 (defmacro wesnoth-locate-warning (string start end)
1425 "Search for STRING and move to the warning in the given direction.
1426 Searching starts from `point' and will wrap from START if no
1427 match was found. STRING is a form to locate the warning in the
1428 required direction. START is the start of the region searched.
1429 END is the end of the region searched."
1430 `(let ((target nil))
1431 (save-excursion
1432 (cond
1433 ((setq target (wesnoth-overlay-at-pos-p ,string)))
1434 ((setq target (wesnoth-overlay-at-pos-p ,start)))
1435 ((and (not (= (goto-char ,string) ,end))
1436 (setq target (wesnoth-overlay-at-pos-p (point)))))))
1437 target))
1439 (defun wesnoth-forward-warning ()
1440 "Move to the next warning."
1441 (interactive)
1442 (let ((target
1443 (if (fboundp 'next-overlay-change)
1444 (save-excursion
1445 (end-of-line)
1446 (wesnoth-locate-warning (next-overlay-change (point))
1447 (point-min)
1448 (point-max)))
1449 (wesnoth-target-position '< '>))))
1450 (if target
1451 (goto-char target)
1452 (message "%s" "No warnings found"))))
1454 (defun wesnoth-backward-warning ()
1455 "Move to the previous warning."
1456 (interactive)
1457 (let ((target
1458 (if (fboundp 'previous-overlay-change)
1459 (save-excursion
1460 (beginning-of-line)
1461 (wesnoth-locate-warning (1- (previous-overlay-change (point)))
1462 (point-max)
1463 (point-min)))
1464 (wesnoth-target-position '> '< t))))
1465 (if target
1466 (goto-char target)
1467 (message "%s" "No warnings found"))))
1469 (defun wesnoth-target-position (predicate search &optional lastp)
1470 "Return the target marker position.
1471 PREDICATE is the function to use to sort
1472 `wesnoth-warning-markers'. SEARCH must be a function which
1473 returns non-nil when the match is correct. If LASTP is non-nil,
1474 swap the order of the sorted positions when attempting to
1475 fallback."
1476 (let ((positions (sort (mapcar 'marker-position wesnoth-warning-markers)
1477 predicate)))
1478 (or (catch 'pos
1479 (dolist (position positions)
1480 (when (funcall search position (point))
1481 (throw 'pos position))))
1482 (car positions))))
1484 (defun wesnoth-place-overlay (start end)
1485 "Place overlay in the region and apply necessary properties.
1486 START is the start of the region to place the overlay. END is
1487 the end of the region to place the overlay."
1488 (if (fboundp 'overlay-put)
1489 (let ((overlay (make-overlay start end)))
1490 (overlay-put overlay 'wesnoth-error t)
1491 (overlay-put overlay 'face 'wesnoth-warning-face))
1492 (add-to-list 'wesnoth-warning-markers (save-excursion
1493 (goto-char start)
1494 (point-marker)))))
1496 (defun wesnoth-kill-block (arg)
1497 "Kill ARG blocks at point."
1498 (interactive "p")
1499 (save-excursion
1500 (while (> arg 0)
1501 (let ((kill-whole-line t))
1502 (while (and (looking-at "[\t ]*$") (= (forward-line 1) 0)))
1503 (if (looking-at "[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1504 #ifn?def\\)")
1505 (kill-region (point)
1506 (save-excursion
1507 (wesnoth-jump-to-matching)
1508 (forward-line 1)
1509 (point)))
1510 (kill-line))
1511 (setq arg (1- arg)))
1512 (wesnoth-indent))))
1514 (defun wesnoth-mark-block ()
1515 "Mark the block at point."
1516 (interactive)
1517 (save-excursion
1518 (while (and (save-excursion (beginning-of-line)
1519 (looking-at "[\t ]*$"))
1520 (= (forward-line 1) 0)))
1521 (when (looking-at "[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1522 #ifn?def\\)")
1523 (push-mark (save-excursion
1524 (wesnoth-jump-to-matching)
1525 (end-of-line)
1526 (point))))))
1528 (defun wesnoth-down-list (&optional arg)
1529 "Move forward down ARG levels of elements.
1530 If ARG is not specified, move forward down one level."
1531 (interactive "p")
1532 (unless arg
1533 (setq arg 1))
1534 (when (looking-at "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1535 #ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)")
1536 (forward-char 1))
1537 (let ((target nil))
1538 (save-excursion
1539 (when (looking-at "\\(\\w\\|_\\)+[\t ]*=")
1540 (if (and (search-forward-regexp (wesnoth-element) (point-max) t)
1541 (progn (beginning-of-line)
1542 (looking-at (wesnoth-element-opening t))))
1543 (setq target (point))
1544 (message "%s" "Innermost level"))))
1545 (when target (goto-char target)))
1546 (while (> arg 0)
1547 (when (> (save-excursion (wesnoth-jump-to-matching) (point)) (point))
1548 (search-forward-regexp
1549 "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|#ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)"
1550 (save-excursion
1551 (wesnoth-jump-to-matching)
1552 (point))
1554 (setq arg (1- arg)))
1555 (back-to-indentation))
1557 (defun wesnoth-backward-up-list (&optional arg)
1558 "Move backward up ARG levels of elements.
1559 If ARG is not specified, move backward up one level."
1560 (interactive "p")
1561 (unless arg
1562 (setq arg 1))
1563 (let ((parent nil))
1564 (while (> arg 0)
1565 (setq parent (cdr (wesnoth-parent-tag)))
1566 (if (numberp parent)
1567 (progn
1568 (goto-char parent)
1569 (setq arg (1- arg)))
1570 (message "%s" "Outermost level")
1571 (setq arg 0))
1572 (when (numberp parent)
1573 (back-to-indentation)))))
1575 (defun wesnoth-forward-list (arg)
1576 "Move to forward ARG elements at the current depth.
1577 If ARG is not specifed, move forward one element."
1578 (interactive "p")
1579 (while (and (save-excursion (beginning-of-line)
1580 (looking-at "[\t ]*$"))
1581 (= (forward-line 1) 0)))
1582 (when (save-excursion (beginning-of-line)
1583 (looking-at (wesnoth-element-opening)))
1584 (wesnoth-jump-to-matching)
1585 (setq arg (1- arg)))
1586 (let ((revert-target (point)))
1587 (while (> arg 0)
1588 (beginning-of-line)
1589 (unless (looking-at (wesnoth-element-closing))
1590 (wesnoth-jump-to-matching))
1591 (end-of-line)
1592 (if (and (search-forward-regexp (wesnoth-element) (point-max) t)
1593 (save-excursion (beginning-of-line)
1594 (looking-at (wesnoth-element-opening))))
1595 (setq revert-target (point))
1596 (goto-char revert-target)
1597 (message "%s" "End of block"))
1598 (setq arg (1- arg))))
1599 (when (save-excursion (beginning-of-line)
1600 (looking-at (wesnoth-element-opening)))
1601 (wesnoth-jump-to-matching))
1602 (end-of-line))
1604 (defun wesnoth-backward-list (arg)
1605 "Move to backward ARG elements at the current depth.
1606 If ARG is not specifed, move backward one element."
1607 (interactive "p")
1608 (while (and (save-excursion (beginning-of-line) (looking-at "[\t ]*$"))
1609 (= (forward-line -1) 0)))
1610 (when (save-excursion (beginning-of-line)
1611 (looking-at (wesnoth-element-closing)))
1612 (wesnoth-jump-to-matching)
1613 (setq arg (1- arg)))
1614 (let ((revert-target (point)))
1615 (while (> arg 0)
1616 (beginning-of-line)
1617 (unless (looking-at (wesnoth-element-opening))
1618 (wesnoth-jump-to-matching))
1619 (if (and (search-backward-regexp (wesnoth-element) (point-min) t)
1620 (save-excursion (beginning-of-line)
1621 (looking-at (wesnoth-element-closing))))
1622 (setq revert-target (point))
1623 (goto-char revert-target)
1624 (message "%s" "Beginning of block"))
1625 (setq arg (1- arg))))
1626 (when (looking-at (wesnoth-element-closing))
1627 (wesnoth-jump-to-matching)))
1629 (defun wesnoth-check-wml (&optional preserve-buffer)
1630 "Perform context-sensitive analysis of WML-code.
1631 If PRESERVE-BUFFER is non-nil, the contents of *WML* will be
1632 maintained through successive calls."
1633 (interactive)
1634 ;; Temporarily cache all tag-data.
1635 (setq wesnoth-tmp-tag-data (wesnoth-refresh-wml-data))
1636 (wesnoth-update-project-information)
1637 (if (fboundp 'delete-overlay)
1638 (dolist (overlay (overlays-in (point-min) (point-max)))
1639 (if (eq 'wesnoth-warning-face (overlay-get overlay 'face))
1640 (delete-overlay overlay)))
1641 (setq wesnoth-warning-markers nil))
1642 (when (= 0 (hash-table-count wesnoth-tag-hash-table))
1643 (error "WML data not available; unable to generate report"))
1644 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1645 (let ((unmatched '())
1646 (source-buffer (buffer-name))
1647 (outbuf (get-buffer-create "*WML*"))
1648 (last-match-pos 1)
1649 (details nil)
1650 (foreach '())
1651 (map (make-sparse-keymap)))
1652 (define-key map (kbd "q") 'bury-buffer)
1653 (save-excursion
1654 (set-buffer outbuf)
1655 (use-local-map map)
1656 (let ((buffer (buffer-name))
1657 (buffer-read-only nil))
1658 (if preserve-buffer
1659 (goto-char (point-max))
1660 (erase-buffer))
1661 (message (format "Checking %s..." source-buffer))))
1662 (save-excursion
1663 (goto-char (point-min))
1664 (while (setq details (wesnoth-find-next
1665 '(tag-opening tag-closing preprocessor attribute
1666 macro)))
1667 (save-excursion
1668 (goto-char (match-beginning 0))
1669 (cond ((nth 3 (parse-partial-sexp last-match-pos (point)))
1670 nil)
1671 ((eq (car details) 'macro)
1672 (dolist (macro (save-match-data
1673 (wesnoth-extract-macro-details
1674 (match-string-no-properties 0))))
1675 (unless (assoc macro
1676 (wesnoth-merge-macro-data
1677 wesnoth-macro-data
1678 (wesnoth-macro-additions)
1679 wesnoth-local-macro-data
1680 (wesnoth-macro-arguments)))
1681 (wesnoth-check-process "Unknown macro: '%s'"
1682 macro)))
1683 (save-match-data
1684 (when
1685 (looking-at
1686 "{\\(FOREACH\\|NEXT\\).*[\t ]+\
1687 \\(\\(?:\\w\\|_\\)+\\)}")
1688 (if (string= (match-string-no-properties 1) "FOREACH")
1689 (setq foreach
1690 (cons (match-string-no-properties 2) foreach))
1691 (if (string= (match-string-no-properties 1) "NEXT")
1692 (progn
1693 (unless (string= (car foreach)
1694 (match-string-no-properties 2))
1695 (wesnoth-check-process
1696 (concat "NEXT does not match corresponding "
1697 "FOREACH: '%s' found; '%s' expected.")
1698 (match-string-no-properties 2)
1699 (car foreach)))
1700 (setq foreach (cdr foreach))))))))
1701 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
1702 (unless (wesnoth-check-element-type 0)
1703 (wesnoth-check-process
1704 "Tag not available in this context: '%s'"
1705 (match-string-no-properties 1)))
1706 (setq unmatched (cons (match-string-no-properties 1)
1707 unmatched)))
1708 ((looking-at
1709 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1710 "\\( \\(\\w\\|_\\)+\\)*"))
1711 (unless (match-string-no-properties 2)
1712 (wesnoth-check-process
1713 (concat "Preprocessor statement has no argument: "
1714 (match-string-no-properties 1))))
1715 (unless (string= (match-string-no-properties 1) "#undef")
1716 (setq unmatched (cons (match-string-no-properties 1)
1717 unmatched))))
1718 ((looking-at wesnoth-preprocessor-closing-regexp)
1719 (when (and unmatched
1720 (not (string-match
1721 (cdr (assoc (match-string-no-properties 1)
1722 '(("enddef" . "#define")
1723 ("endif" . "#ifn?def"))))
1724 (car unmatched))))
1725 (wesnoth-check-process
1726 "Preprocessor statement does not nest correctly"))
1727 (setq unmatched (cdr unmatched)))
1728 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)[\t ]*=\\(.+\\)?")
1729 (unless (wesnoth-check-element-type 1)
1730 (wesnoth-check-process
1731 "Key not available in this context: '%s'"
1732 (match-string-no-properties 1)))
1733 (unless (match-string 3)
1734 (wesnoth-check-process
1735 "Attribute has no value")))
1736 ((looking-at "[\t ]*#else")
1737 (unless (string-match "ifn?def" (car unmatched))
1738 (if (string= (car unmatched) "#define")
1739 (wesnoth-check-process "Expecting: '%s'"
1740 (car unmatched))
1741 (wesnoth-check-process "Expecting: '[/%s]'"
1742 (car unmatched)))))
1743 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1744 (when (and unmatched
1745 (not (string= (match-string-no-properties 1)
1746 (car unmatched))))
1747 (wesnoth-check-process
1748 "Expecting '%s'"
1749 (or (cdr (assoc (car unmatched)
1750 '(("#define" . "#enddef")
1751 ("#ifdef" . "#endif")
1752 ("#ifndef" . "#endif"))))
1753 (concat "[/" (car unmatched) "]"))))
1754 (setq unmatched (cdr unmatched)
1755 last-match-pos (point))))))
1756 (when foreach
1757 (dolist (var foreach)
1758 (wesnoth-check-process "Unmatched FOREACH: '%s'" var)))
1759 (when unmatched
1760 (dolist (element unmatched)
1761 (wesnoth-check-process "Unmatched element: '%s'" element))))
1762 (when (or (not preserve-buffer)
1763 (and preserve-buffer (not (cdr wesnoth-found-cfgs))))
1764 (save-excursion
1765 (setq wesnoth-define-blocks nil
1766 wesnoth-tmp-tag-data nil)
1767 (set-buffer outbuf)
1768 (toggle-read-only t)
1769 (let ((buffer (buffer-name))
1770 (buffer-read-only nil))
1771 (display-buffer outbuf t)
1772 (let ((warnings (- (wesnoth-line-number-at-pos
1773 (goto-char (point-max))) 1)))
1774 (insert (format (concat "\nCheck complete. %d warning"
1775 (if (= warnings 1) "." "s.")) warnings)))
1776 (message (format "Checking %s...done" source-buffer)))))))
1778 ;; TODO: This will not load dependant files in the right order. Need to
1779 ;; process from _main.cfg and similar.
1780 (defun wesnoth-check-directory (dir)
1781 "Check all WML in DIR.
1782 Where DIR is a directory containing WML files."
1783 (interactive "DDirectory: ")
1784 (setq wesnoth-found-cfgs nil)
1785 (when (member (mapcar 'buffer-name (buffer-list)) "*WML*")
1786 (kill-buffer "*WML*"))
1787 (wesnoth-fetch-all-dirs dir)
1788 (let ((tmp-cfgs wesnoth-found-cfgs))
1789 (while tmp-cfgs
1790 (find-file (car wesnoth-found-cfgs))
1791 (kill-buffer)
1792 (setq tmp-cfgs (cdr tmp-cfgs))))
1793 (save-excursion
1794 (while wesnoth-found-cfgs
1795 (find-file (car wesnoth-found-cfgs))
1796 (wesnoth-check-wml t)
1797 (kill-buffer)
1798 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))
1800 (defun wesnoth-file-cfg-p (file)
1801 "Return non-nil if FILE has a '.cfg' extension."
1802 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
1804 (defun wesnoth-fetch-all-dirs (dir)
1805 "Retrieve a list of subdirectories to scan.
1806 DIR is the directory to check."
1807 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
1808 (while dirs-to-scan
1809 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
1810 dirs-to-scan)))))
1812 (defun wesnoth-files-in-dir (dir)
1813 "Add cfgs to `wesnoth-files-in-dir'.
1814 Returns a list of sub-directories in DIR."
1815 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
1816 (when cfgs
1817 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
1818 (let ((dirs '()))
1819 (dolist (file (directory-files dir t))
1820 (unless (string-match "^\\..*" (file-name-nondirectory file))
1821 (cond ((file-directory-p file)
1822 (add-to-list 'dirs file))
1823 ((wesnoth-file-cfg-p file)
1824 (add-to-list 'wesnoth-found-cfgs file)))))
1825 dirs))
1827 (defun wesnoth-cfg-files-in-dir (dir)
1828 "Return all cfg files in DIR."
1829 (let ((result '()))
1830 (dolist (file (directory-files dir t))
1831 (and (wesnoth-file-cfg-p file)
1832 (add-to-list 'result file)))
1833 result))
1836 ;;; wesnoth-mode
1837 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
1838 "Major mode for editing WML."
1839 (kill-all-local-variables)
1840 (use-local-map wesnoth-mode-map)
1841 (setq major-mode 'wesnoth-mode)
1842 (setq mode-name "WML")
1843 (set-syntax-table wesnoth-syntax-table)
1844 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
1845 (set (make-local-variable 'comment-start) "#")
1846 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
1847 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
1848 (set (make-local-variable 'wesnoth-warning-markers) '())
1849 (set (make-local-variable 'font-lock-defaults)
1850 '(wesnoth-font-lock-keywords
1851 nil t nil nil
1852 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
1853 (setq indent-tabs-mode nil)
1854 (easy-menu-add wesnoth-menu wesnoth-mode-map)
1855 (wesnoth-refresh-wml-data)
1856 (wesnoth-update-project-information)
1857 (run-hooks 'wesnoth-mode-hook))
1859 (provide 'wesnoth-mode)
1861 ;;; wesnoth-mode.el ends here