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