* wesnoth-mode.el: Updated history for latest changes.
[wesnoth-mode.git] / wesnoth-mode.el
blob43ec1038aa6fabe467081ae5e3284d2c0c5ed1ef
1 ;;; wesnoth-mode.el --- A major mode for editing WML.
2 ;; Copyright (C) 2006, 2007, 2008 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
23 ;; of Wesnoth Markup Language (WML) files. Currently, this major-mode
24 ;; features syntax highlighting support, automatic indentation,
25 ;; context-sensitive 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.2
37 ;; * Fixed a bug where attributes immediately within #ifn?def were always
38 ;; reported to be illegal.
39 ;; * Fixed a bug where tags immediately within #ifn?def were always legal.
40 ;; * Major performance improvements to indentation and WML checking.
41 ;; * Fixed a bug where when inserting missing tags, scanning would only be
42 ;; performed up to point.
43 ;; * Fixed a bug when jumping between preprocessor statements.
44 ;; 1.3.1
45 ;; * Completion history available is now specific to wesnoth-mode.
46 ;; * Added binding to explicitly update macro information from the current
47 ;; buffer (C-c C-u).
48 ;; * Significantly improved performance of completion and WML checking.
49 ;; * Improved performance for inserting missing tags.
50 ;; * Fixed a bug where #ifdef was never matched when checking WML.
51 ;; * Added completion for preprocessor statements.
52 ;; * Improved macro completion and checking.
53 ;; 1.3.0
54 ;; * Added support for Xemacs.
55 ;; * WML checking is now context sensitive; checks attributes and macros.
56 ;; * WML checks are now always performed on the entire buffer, with results
57 ;; displayed in a temporary buffer.
58 ;; * Context-sensitive completion for attributes and tags.
59 ;; * Completion for built-in and project-specific macros.
60 ;; * Changed the following bindings:
61 ;; `wesnoth-insert-tag' - C-c e -> C-c t
62 ;; `wesnoth-jump-to-matching' - C-c m -> C-c o
63 ;; `wesnoth-check-structure' -> `wesnoth-check-wml' - C-c c
64 ;; * Added the following bindings:
65 ;; `wesnoth-complete-attribute' - C-c a
66 ;; `wesnoth-complete-macro' - C-c m
67 ;; `wesnoth-complete-tag' - C-c t
68 ;; * Removed the following bindings:
69 ;; `wesnoth-check-tag-names' - C-c n
70 ;; * Removed `wesnoth-check-tag-names'. Replaced by `wesnoth-check-wml'.
71 ;; * Completion for an incomplete attribute, tag, or macro at point is
72 ;; attempted via TAB.
73 ;; 1.2.5
74 ;; * Fixed support for GNU Emacs 21.
75 ;; * Added several new tags to `wesnoth-tags-list'.
76 ;; * Added M-TAB binding for `wesnoth-insert-tag'.
77 ;; * `wesnoth-insert-tag' now takes an optional numeric argument indicating
78 ;; how many blocks to wrap across instead of a region.
79 ;; * Support for `wesnoth-indent-preprocessor-bol' removed.
80 ;; * Fixed a bug in `wesnoth-insert-tag' and `wesnoth-insert-missing-closing'
81 ;; causing tags not to be inserted in the correct position.
82 ;; * Fixed highlighting of array indexes as tags.
83 ;; 1.2.4
84 ;; * Improved syntax-highlighting for macro calls.
85 ;; * Underscore is now treated as whitespace.
86 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
87 ;; * Point is now placed at the first non-whitespace character of the line,
88 ;; instead of the last.
89 ;; * Corrected minor indentation bugs.
90 ;; * Indenting across large regions is now much more efficient.
91 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
92 ;; applied.
93 ;; 1.2.3
94 ;; * Now compatible with GNU Emacs 21.4.
95 ;; * Added support for several new tags.
96 ;; * Added menu entry for wesnoth-mode.
97 ;; * Significant speed increase to indentation.
98 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
99 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
100 ;; has been removed.
101 ;; * Trailing whitespace is no longer created when creating a second
102 ;; consecutive newline.
103 ;; * Spurious newlines are no longer created when inserting a tag elements
104 ;; around a region.
105 ;; 1.2.2
106 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
107 ;; `wesnoth-element', `wesnoth-element-opening',
108 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
109 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
110 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
111 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
112 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
113 ;; * Fixed a bug in indentation where content was needed between elements pairs
114 ;; for indentation to work.
115 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
116 ;; `wesnoth-auto-indent-flag'.
117 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
118 ;; * Added indentation styles: `wesnoth-indent-default',
119 ;; `wesnoth-indent-withtags' which implement a a similar indentation
120 ;; style to the existing styles, however all preprocessor statements are
121 ;; indented to the first column.
122 ;; * Added support for several new tags.
123 ;; * Modified `wesnoth-newline' to behave more consistently.
124 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
125 ;; now leaves point at the beginning (when moving backward) or end (when
126 ;; moving forward) of the match.
127 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
128 ;; will now work on preprocessor statements. Will now warn if jump
129 ;; destination may not be correct (due to errors in WML structure).
130 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
131 ;; * `wesnoth-check-structure' can now be applied over an active region and
132 ;; now checks preprocessor statements for correct nesting.
133 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
134 ;; perform indentation by providing a prefix argument.
135 ;; * Indentation styles now leave point at the first non-whitespace character
136 ;; of the line.
137 ;; * `wesnoth-check-tag-names' now reports on success.
138 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
139 ;; * `outline-minor-mode' now works on macro definitions.
140 ;; 1.2.1
141 ;; * Base indent now defaults to 4.
142 ;; * Added support for #ifndef.
144 ;;; Code:
145 (require 'easymenu)
146 (require 'wesnoth-update)
147 (require 'wesnoth-wml-data)
149 (defconst wesnoth-mode-version "1.3.2"
150 "The current version of `wesnoth-mode'.")
152 (defgroup wesnoth-mode nil "Wesnoth-mode access"
153 :group 'languages
154 :prefix "wesnoth-")
156 (defcustom wesnoth-auto-indent-flag t
157 "Non-nil means indent the current line upon creating a newline."
158 :type 'boolean
159 :group 'wesnoth-mode)
161 (defcustom wesnoth-indent-savefile t
162 "Non-nil means to use the current indentation conventions.
163 If nil, use the old convention for indentation.
164 The current convention is all attributes are indented a level deeper
165 than their parent; in the past attributes were indented to the same
166 level as their parent.")
168 (defcustom wesnoth-base-indent 4
169 "The number of columns to indent WML."
170 :type 'integer
171 :group 'wesnoth-mode)
173 (defconst wesnoth-preprocessor-regexp
174 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
175 "Regular expression to match all preprocessor statements.")
177 (defconst wesnoth-preprocessor-opening-regexp
178 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
179 "Regular expression to match \"opening\" preprocessor statements.")
181 (defconst wesnoth-preprocessor-closing-regexp
182 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
183 "Regular expression to match \"closing\" preprocessor statements.")
185 (defvar wesnoth-define-blocks '()
186 "Cache of all toplevel #define and #enddef pairs.")
188 (defvar wesnoth-history-list '()
189 "History of inserted WML elements.")
191 (defvar wesnoth-mode-hook nil)
193 (defvar wesnoth-mode-map
194 (let ((map (make-sparse-keymap)))
195 (define-key map (kbd "C-M-a") 'wesnoth-backward-element)
196 (define-key map (kbd "C-M-e") 'wesnoth-forward-element)
197 (define-key map (kbd "C-m") 'wesnoth-newline)
198 (define-key map (kbd "C-j") 'wesnoth-newline-and-indent)
199 (define-key map (kbd "C-c C-c") 'wesnoth-check-wml)
200 (define-key map (kbd "C-c C-a") 'wesnoth-complete-attribute)
201 (define-key map (kbd "C-c C-t") 'wesnoth-complete-tag)
202 (define-key map (kbd "C-c C-p") 'wesnoth-complete-preprocessor)
203 (define-key map (kbd "C-c C-u") 'wesnoth-update-project-information)
204 (define-key map (kbd "M-TAB") 'wesnoth-complete-tag)
205 (define-key map (kbd "C-c C-m") 'wesnoth-complete-macro)
206 (define-key map (kbd "C-c C-o") 'wesnoth-jump-to-matching)
207 (define-key map (kbd "C-c C-/") 'wesnoth-insert-missing-closing)
208 (define-key map (kbd "TAB") 'wesnoth-indent-or-complete)
209 map)
210 "Keymap used in `wesnoth-mode'.")
212 (easy-menu-define wesnoth-menu wesnoth-mode-map "Menu for wesnoth-mode"
213 '("WML"
214 ["Check WML" wesnoth-check-wml t]
215 ["Indent or Complete" wesnoth-indent-or-complete t]
216 ["Indent buffer" (lambda ()
217 (interactive)
218 (wesnoth-indent-region (point-min) (point-max))) t]
219 ["Insert Tag" wesnoth-complete-tag t]
220 ["Insert Attribute" wesnoth-complete-attribute t]
221 ["Insert Macro" wesnoth-complete-macro t]
222 ["Insert Preprocessor" wesnoth-complete-preprocessor t]
223 ["Insert Missing Tag" wesnoth-insert-missing-closing t]
224 ["Jump to Matching" wesnoth-jump-to-matching t]
225 ["Update Macros" wesnoth-update-project-information t]))
227 (defvar wesnoth-syntax-table
228 (let ((wesnoth-syntax-table (make-syntax-table)))
229 (modify-syntax-entry ?= "." wesnoth-syntax-table)
230 (modify-syntax-entry ?_ "_" wesnoth-syntax-table)
231 (modify-syntax-entry ?- "_" wesnoth-syntax-table)
232 (modify-syntax-entry ?. "_" wesnoth-syntax-table)
233 (modify-syntax-entry ?\n ">" wesnoth-syntax-table)
234 (modify-syntax-entry ?\r ">" wesnoth-syntax-table)
235 wesnoth-syntax-table)
236 "Syntax table for `wesnoth-mode'.")
238 ;; Prevents automatic syntax-highlighting of elements which might be
239 ;; pre-processor statements.
240 (defvar wesnoth-syntactic-keywords
241 (list
242 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\)" 1 "w")
243 '("\\(#[\t ]*.*$\\)" 1 "<"))
244 "Highlighting syntactic keywords within `wesnoth-mode'.")
246 (defvar wesnoth-font-lock-keywords
247 (list
248 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face)
249 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
250 font-lock-keyword-face)
251 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
252 2 font-lock-function-name-face)
253 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face))
254 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face))
255 '("}" . font-lock-function-name-face)
256 '("^[\t ]*\\(\\[[^]]+\\]\\)" 1 font-lock-type-face)
257 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face)
258 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
259 1 font-lock-variable-name-face))
260 "Syntax highlighting for `wesnoth-mode'.")
262 (defun wesnoth-element-closing (&optional limited)
263 "Return the regexp to match a closing element.
264 If LIMITED is non-nil, return a regexp which matches only the
265 #enddef preprocessor."
266 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\|"
267 (if limited
268 "#enddef"
269 "#end\\(?:def\\|if\\)")
270 "\\)"))
272 (defun wesnoth-element-opening (&optional limited)
273 "Return the regexp to match a closing element.
274 If LIMITED is non-nil, return a regexp which matches only the
275 #define preprocessor."
276 (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define "
277 (if limited
279 "\\|#ifn?def ")
280 "\\)"))
282 (defun wesnoth-element (&optional limited)
283 "Return the regexp to match a closing element.
284 If LIMITED is non-nil, return a regexp which matches only the
285 #define and #enddef preprocessors."
286 (concat "^[\t ]*\\(\\[[/+]?\\(\\w\\|_\\)+\\]?\\|"
287 (if limited
288 "#define \\|#enddef"
289 (substring wesnoth-preprocessor-regexp 5))
290 "\\)"))
293 ;;; Insertion and completion
294 (defmacro wesnoth-element-completion (completions prompt partial)
295 "Process completion of COMPLETIONS, displaying PROMPT.
296 PARTIAL is the partial string on which to attempt completion."
297 `(let* ((element (when ,partial (try-completion ,partial ,completions))))
298 (cond ((eq element t)
299 (setq element nil))
300 ((null element)
301 (setq element
302 (completing-read ,prompt ,completions nil nil nil
303 'wesnoth-history-list)))
304 ((not (if (listp (car ,completions))
305 (assoc element ,completions)
306 (member element ,completions)))
307 (setq element
308 (completing-read ,prompt ,completions
309 nil nil ,partial
310 'wesnoth-history-list))))
311 element))
313 (defun wesnoth-parent-tag ()
314 "Return the name of the parent tag.
315 If the parent is a preprocessor statement, return non-nil.
316 If the element does not have a parent, return nil.
317 Otherwise, return a string containing the name of the parent tag."
318 (save-excursion
319 (let ((start-point (point))
320 (depth 1))
321 (when (save-excursion (> (point) (progn (back-to-indentation)
322 (point))))
323 (end-of-line))
324 (while (and (> depth 0)
325 (search-backward-regexp (wesnoth-element t)
326 (point-min) t))
327 (if (string-match "[\t ]*\\[/" (match-string 0))
328 (setq depth (1+ depth))
329 (setq depth (1- depth))))
330 (beginning-of-line)
331 (if (> depth 0)
333 (when (looking-at (wesnoth-element-opening))
334 (let ((parent (match-string-no-properties 1)))
335 (if (string-match wesnoth-preprocessor-opening-regexp parent)
337 (substring parent 1 (1- (length parent))))))))))
339 (defun wesnoth-indent-or-complete (&optional elements)
340 "Indent or complete the line at point, depending on context.
341 ELEMENTS is the number of elements to wrap around if inserting
342 matching tags."
343 (interactive "P")
344 (or elements (setq elements 0))
345 (let ((target nil))
346 (save-excursion
347 (back-to-indentation)
348 (cond ((looking-at "\\(\\(\\w\\|_\\)+\\)[\t ]*$")
349 (wesnoth-complete-attribute t))
350 ((looking-at "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$")
351 (wesnoth-complete-tag elements t))
352 ((looking-at "{\\(\\(\\w\\|_\\)*\\)[\t ]*$")
353 (wesnoth-complete-macro t))
354 ((looking-at "^#\\w+$")
355 (wesnoth-complete-preprocessor elements t))
356 ((looking-at "\\[/\\(\\(\\w\\|_\\)*\\)[\t ]*$")
357 (delete-region (point) (progn (end-of-line) (point)))
358 (wesnoth-insert-missing-closing)
359 (end-of-line))
361 (wesnoth-indent)))
362 (setq target (point)))
363 (goto-char target)))
365 (defun wesnoth-preprocessor-closed-p (preprocessor)
366 "Determine whether PREPROCESSOR has been closed.
367 PREPROCESSOR is a string matching the preprocessor statement to
368 be inserted."
369 (save-excursion
370 (back-to-indentation)
371 (wesnoth-jump-to-matching preprocessor)
372 (looking-at
373 (if (string= preprocessor "#define ")
374 "#enddef"
375 "#endif"))))
377 (defun wesnoth-complete-preprocessor (&optional elements completep)
378 "Complete and insert the preprocessor at point.
379 ELEMENTS is the number of elements to wrap around.
380 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
381 (interactive "P")
382 (or elements (setq elements 0))
383 (let* ((completions (wesnoth-emacs-completion-formats
384 '("define" "else" "ifdef" "ifndef"
385 "enddef" "endif" "undef")))
386 (partial (when completep
387 (save-excursion
388 (back-to-indentation)
389 (when (looking-at "#\\(\\w+\\)$")
390 (match-string-no-properties 1)))))
391 (preprocessor (or (wesnoth-element-completion
392 completions "Preprocessor: " partial)
393 partial))
394 (closedp
395 (save-excursion
396 (when preprocessor
397 (unless (string= "#" (substring preprocessor 0 1))
398 (setq preprocessor (concat "#" preprocessor)))
399 (when (string-match "#\\(define\\|ifn?def\\|undef\\)" preprocessor)
400 (setq preprocessor (concat preprocessor " ")))
401 (when partial
402 (delete-region (progn (back-to-indentation) (point))
403 (progn (end-of-line) (point))))
404 (wesnoth-preprocessor-closed-p preprocessor)))))
405 (when preprocessor
406 (when partial
407 (delete-region (progn (back-to-indentation) (point))
408 (progn (end-of-line) (point))))
409 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor)
410 (not closedp))
411 (progn
412 (wesnoth-insert-tag elements preprocessor)
413 (forward-line -1)
414 (end-of-line))
415 (wesnoth-insert-element-separately preprocessor)))))
417 (defun wesnoth-macro-arguments ()
418 "Find any current macro arguments."
419 (let ((results '())
420 (depth (wesnoth-within-define (point))))
421 (save-excursion
422 (while (> depth 0)
423 (save-match-data
424 (search-backward-regexp
425 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
426 (point-min) t)
427 (when (<= (wesnoth-within-define (point)) depth)
428 (and (> depth 0)
429 (setq results
430 (append (mapcar (lambda (macro)
431 (list macro nil))
432 (split-string
433 (match-string-no-properties 1)))
434 results)))
435 (setq depth (1- depth)))))
436 results)))
438 (defun wesnoth-complete-macro (&optional completep)
439 "Complete and insert the macro at point.
440 If COMPLETEP is non-nil, attempt to complete the macro at point."
441 (interactive)
442 (wesnoth-update-project-information)
443 (let* ((macro-information (append (wesnoth-macro-arguments)
444 wesnoth-macro-data
445 wesnoth-local-macro-data))
446 (completions (wesnoth-emacs-completion-formats
447 (mapcar 'car macro-information)))
448 (partial (when completep
449 (save-excursion
450 (back-to-indentation)
451 (when (looking-at "{\\(\\(\\w\\|_\\)*\\)")
452 (match-string-no-properties 1)))))
453 (macro (or (wesnoth-element-completion completions "Macro: " partial)
454 partial))
455 (args (cadr (assoc macro macro-information))))
456 (when macro
457 (if partial
458 (progn
459 (delete-region (progn (back-to-indentation) (point))
460 (progn (end-of-line) (point)))
461 (insert "{" macro (if args " }" "}")))
462 (wesnoth-insert-element-separately "{" macro (if args " }" "}")))
463 (save-excursion
464 (wesnoth-indent))
465 (when args
466 (forward-char -1)))))
468 (defun wesnoth-complete-attribute (&optional completep)
469 "Insert the attribute at point.
470 If COMPLETEP is non-nil, attempt to complete the attribute at point."
471 (interactive)
472 (let* ((completions (wesnoth-build-completion 1))
473 (partial (when completep
474 (save-excursion
475 (back-to-indentation)
476 (when (looking-at "\\(\\(\\w\\|_\\)+\\)")
477 (match-string-no-properties 1)))))
478 (attribute (or (wesnoth-element-completion completions "Attribute: "
479 partial)
480 partial)))
481 (when attribute
482 (if (and partial completep)
483 (progn
484 (delete-region (progn (back-to-indentation) (point))
485 (progn (end-of-line) (point)))
486 (insert attribute "="))
487 (wesnoth-insert-element-separately attribute
488 (if (string-match "=" attribute)
490 "=")))
491 (save-excursion
492 (wesnoth-indent)))))
494 (defun wesnoth-complete-tag (&optional elements completep)
495 "Complete and insert the tag at point.
496 ELEMENTS is the number of elements to wrap around.
497 If COMPLETEP is non-nil, attempt to complete tag at point."
498 (interactive "P")
499 (or elements (setq elements 0))
500 (let* ((completions (wesnoth-build-completion 0))
501 (partial (when completep
502 (save-excursion
503 (back-to-indentation)
504 (when (looking-at "\\[\\(\\(\\w\\|_\\)+\\)")
505 (match-string-no-properties 1)))))
506 (tag (or (wesnoth-element-completion completions "Tag: " partial)
507 partial))
508 (closedp
509 (save-excursion
510 (wesnoth-jump-to-matching (concat "[" tag "]"))
511 (back-to-indentation)
512 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
513 (string= tag (match-string 1))))))
514 (when completep
515 (delete-region (progn (back-to-indentation) (point))
516 (progn (end-of-line) (point))))
517 (if (and closedp completep)
518 (progn
519 (wesnoth-insert-and-indent "[" tag "]")
520 (end-of-line))
521 (wesnoth-insert-tag elements tag))))
523 (defun wesnoth-build-completion (position)
524 "Create a new list for tag completion if necessary.
525 Rebuilding list is required for versions of GNU Emacs earlier
526 than 22. POSITION is the argument passed to `nth' for
527 `wesnoth-tag-data'."
528 (interactive "P")
529 (let ((parent (wesnoth-parent-tag)))
530 (wesnoth-emacs-completion-formats
531 (if (or (stringp parent) (null parent))
532 (nth position (gethash parent wesnoth-tag-hash-table))
533 (mapcar 'car wesnoth-tag-data)))))
535 (defun wesnoth-emacs-completion-formats (candidates)
536 "Return the completions in the correct format for `emacs-major-version'.
537 CANDIDATES is a list of all possible completions."
538 (if (> emacs-major-version 21)
539 candidates
540 (let ((tags '())
541 (iter 0))
542 (dolist (tag candidates)
543 (setq iter (1+ iter))
544 (setq tags (append tags (list (cons tag iter)))))
545 tags)))
547 (defun wesnoth-insert-tag (&optional elements tagname)
548 "Insert the specified opening tag and it's matching closing tag.
549 Both the opening and closing tags will be placed on their own
550 lines with point positioned between them. Completion of tags at
551 the prompt uses `wesnoth-tags-list'.
553 ELEMENTS is specifies the number of following blocks which the
554 tag should wrap around.
556 TAGNAME is the name of the tag to be inserted."
557 (interactive "Ps")
558 (unless tagname
559 (setq tagname (completing-read "Tag: " (wesnoth-build-completion 1)
560 nil nil nil wesnoth-history-list)))
561 (when (or (not elements)
562 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
563 wesnoth-preprocessor-regexp "\\)")))
564 (setq elements 0))
565 (let ((depth 0)
566 (start (save-excursion (forward-line -1) (point)))
567 (end (unless (= elements 0)
568 (wesnoth-nth-pair-position elements))))
569 (if (string-match wesnoth-preprocessor-regexp tagname)
570 (wesnoth-insert-element-separately tagname)
571 (wesnoth-insert-element-separately "[" tagname "]"))
572 (save-excursion
573 (if end
574 (goto-char (marker-position end))
575 (newline (if (string-match wesnoth-preprocessor-regexp tagname) 1 2)))
576 (if (string-match wesnoth-preprocessor-opening-regexp tagname)
577 (wesnoth-insert-element-separately
578 (if (string= tagname "#define ")
579 "#enddef"
580 "#endif"))
581 (wesnoth-insert-element-separately "[/" tagname "]"))
582 (indent-region start (point) nil))
583 (unless end
584 (forward-line 1)))
585 (wesnoth-indent))
587 (defun wesnoth-insert-element-separately (&rest strings)
588 "Concatenate STRINGS and insert them on a line of their own."
589 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
590 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
591 (progn
592 (forward-line 1)
593 (end-of-line))
594 (end-of-line)
595 (newline))
596 (beginning-of-line)
597 (if (looking-at "^[\t ]*$")
598 (end-of-line)
599 (open-line 1)))
600 (insert (apply 'concat strings)))
602 (defun wesnoth-insert-missing-closing (&optional start end)
603 "Insert the next expected closing element at point.
605 START and END define the region to check for missing closing
606 elements. If function `transient-mark-mode' is enabled, the region
607 specified will be used as START and END. Otherwise, START and
608 END will be the minimum and current positions of the buffer,
609 respectively."
610 (interactive)
611 (if (and (boundp 'transient-mark-mode)
612 transient-mark-mode mark-active)
613 (setq start (region-beginning)
614 end (copy-marker (region-end)))
615 (setq start (point-min)
616 end (point-max)))
617 (let ((element (wesnoth-check-structure start end)))
618 (cond ((not element)
619 (message "%s" "Unable to find element to insert"))
620 ((string= element "Unexpected end of file")
621 (message "%s" element))
623 (wesnoth-insert-element-separately element)
624 (wesnoth-indent)
625 (end-of-line)))))
627 (defun wesnoth-insert-and-indent (&rest args)
628 "Concatenate and insert the given string(s) before indenting.
630 ARGS is a list of strings to be inserted."
631 (insert (apply 'concat args))
632 (wesnoth-indent))
634 (defun wesnoth-newline (&optional indent)
635 "Indent both the current line and the newline created.
636 If `wesnoth-auto-indent-flag' is nil, indentation will not be
637 performed. Indentation can be forced by setting INDENT to
638 non-nil."
639 (interactive)
640 (save-excursion
641 (when (and (or wesnoth-auto-indent-flag indent)
642 (not (looking-at "^[\t ]*$")))
643 (wesnoth-indent)))
644 (newline))
646 ;;; Movement
647 (defmacro wesnoth-navigate-element (repeat search-function bound)
648 "Move point to the tag in the given direction REPEAT times.
650 SEARCH-FUNCTION is the symbol of the function for searching in
651 the required direction, with BOUND marking the furthest point to
652 search."
653 `(progn
654 (or ,repeat (setq ,repeat 1))
655 (while (> ,repeat 0)
656 (and (eq ,search-function 'search-forward-regexp) (end-of-line))
657 (funcall ,search-function (wesnoth-element-opening) ,bound t)
658 (back-to-indentation)
659 (setq repeat (1- ,repeat)))))
661 (defun wesnoth-nth-pair-position (count)
662 "Return `point' after COUNT number of matching element pairs.
663 COUNT is a positive number representing the number of balanced
664 pairs to move across.
665 `point' is returned as a marker object."
666 (save-excursion
667 (let ((start (point))
668 (failed nil))
669 (if (> (point) (save-excursion (back-to-indentation) (point)))
670 (end-of-line)
671 (beginning-of-line))
672 (while (> count 0)
673 ;; Currently looking-at target tag. Stop here to avoid
674 ;; incorrect nesting.
675 (unless (wesnoth-search-for-matching-tag
676 'search-forward-regexp (wesnoth-element-closing) 'point-max)
677 (setq count 0)
678 (unless (or (= (point) (point-max))
679 (progn (beginning-of-line)
680 (search-backward-regexp (wesnoth-element-closing)
681 start t)))
682 (setq failed t)))
683 (and (> (setq count (1- count)) 0) (forward-line 1)))
684 (if failed
685 (beginning-of-line)
686 (end-of-line))
687 (point-marker))))
689 (defun wesnoth-forward-element (repeat)
690 "Move point to the end of the next tag.
691 REPEAT is an optional numeric argument. If REPEAT is non-nil,
692 jump forward the specified number of tags."
693 (interactive "p")
694 (if (< repeat 0)
695 (wesnoth-backward-element (abs repeat))
696 (wesnoth-navigate-element repeat 'search-forward-regexp (point-max))))
698 (defun wesnoth-backward-element (repeat)
699 "Move point to the beginning of the previous tag.
700 REPEAT is an optional numeric argument. If REPEAT is non-nil,
701 jump backward the specified number of tags."
702 (interactive "p")
703 (if (< repeat 0)
704 (wesnoth-forward-element (abs repeat))
705 (wesnoth-navigate-element repeat 'search-backward-regexp (point-min))))
707 (defmacro wesnoth-search-for-matching-tag (search-function
708 search-string bound &optional skip)
709 "Search for the matching tag for the current line.
711 SEARCH-FUNCTION is the name of the function used to perform the search.
712 SEARCH-STRING is a string representing the matching tag type.
713 BOUND is the bound to be passed to the search function.
714 If SKIP is non-nil, skip the first element and continue from there."
715 `(let ((depth 1))
716 (when (and (or (and ,skip (forward-line 1))
717 (funcall ,search-function (wesnoth-element) (funcall ,bound) t))
718 (or ,skip (not (string-match ,search-string (match-string 0)))))
719 (while (and (> depth 0)
720 (funcall ,search-function (wesnoth-element)
721 (funcall ,bound) t))
722 (if (string-match ,search-string (match-string 0))
723 (setq depth (1- depth))
724 (setq depth (1+ depth))))
725 (= depth 0))))
727 (defun wesnoth-jump-to-matching (&optional opening)
728 "Jump point to the matching opening/closing tag.
729 OPENING is an opening preprocessor statement to attempt to find a match for."
730 (interactive)
731 (beginning-of-line)
732 (let ((target nil)
733 (first-element nil))
734 (save-excursion
735 (cond
736 ((or (and (stringp opening)
737 (string-match (wesnoth-element-opening) opening))
738 (looking-at (wesnoth-element-opening)))
739 (progn
740 (setq first-element (match-string-no-properties 0 opening))
741 (when (wesnoth-search-for-matching-tag
742 'search-forward-regexp (wesnoth-element-closing) 'point-max
743 (stringp opening))
744 (beginning-of-line)
745 (if (and (string-match wesnoth-preprocessor-opening-regexp
746 first-element)
747 (looking-at (wesnoth-element-closing)))
748 (when (string= (match-string-no-properties 0)
749 (cdr (assoc first-element
750 '(("#define " . "#enddef")
751 ("#ifndef " . "#endif")
752 ("#ifdef " . "#endif")))))
753 (setq target (point)))
754 (setq target (point))))))
755 ((looking-at (wesnoth-element-closing))
756 (end-of-line)
757 (setq first-element (match-string-no-properties 0))
758 (when (wesnoth-search-for-matching-tag
759 'search-backward-regexp (wesnoth-element-opening)
760 'wesnoth-wml-start-pos)
761 (if (and (string-match wesnoth-preprocessor-closing-regexp
762 first-element)
763 (looking-at (wesnoth-element-opening)))
764 (progn
765 (when (or (and (string= "#enddef" first-element)
766 (string= "#define "
767 (match-string-no-properties
768 0)))
769 (and (string= "#endif" first-element)
770 (string-match
771 "#ifn?def "
772 (match-string-no-properties
773 0))))
774 (setq target (point))))
775 (setq target (point)))))
777 (search-backward-regexp (wesnoth-element-opening) (point-min) t)
778 (setq target (point)))))
779 (if target
780 (goto-char target)
781 (when (interactive-p)
782 (error "Tag does not appear to be matched"))))
783 (back-to-indentation))
785 ;;; Indentation
786 (defun wesnoth-wml-start-pos ()
787 "Determine the position of `point' relative to where the actual WML begins.
788 Return the likely starting position of the WML if it is found.
789 Otherwise return nil."
790 (save-excursion
791 (goto-char (point-min))
792 (when (search-forward-regexp (wesnoth-element) (point-max) t)
793 (beginning-of-line)
794 (point))))
796 (defun wesnoth-first-column-indent-p (point)
797 "Return non-nil if the current line should not be indented.
799 POINT is the position in the buffer to check.
800 CONTEXT represents the type of element which precedes the current element."
801 (or (not (wesnoth-wml-start-pos))
802 (<= (point) (wesnoth-wml-start-pos))
803 (nth 3 (parse-partial-sexp
804 (save-excursion (search-backward-regexp
805 (wesnoth-element t) (point-min) t)
806 (point))
807 point))
808 (looking-at wesnoth-preprocessor-regexp)))
810 (defun wesnoth-indent ()
811 "Indent the current line as WML."
812 (beginning-of-line)
813 (let* ((cur-indent 0)
814 (context-data (wesnoth-determine-context (point)))
815 (context (car context-data))
816 (ref-indent (cdr context-data)))
817 (unless (wesnoth-first-column-indent-p (point))
818 (cond
819 ((eq context 'opening)
820 (if (or (and wesnoth-indent-savefile
821 (not (looking-at (wesnoth-element-closing t))))
822 (looking-at (wesnoth-element-opening t)))
823 (setq cur-indent (+ ref-indent wesnoth-base-indent))
824 (setq cur-indent ref-indent)))
825 ((eq context 'closing)
826 (if (or (looking-at "^[\t ]*\\[/")
827 (and (not wesnoth-indent-savefile)
828 (not (looking-at (wesnoth-element-opening t)))))
829 (setq cur-indent (- ref-indent wesnoth-base-indent))
830 (setq cur-indent ref-indent)))))
831 (indent-line-to (max cur-indent 0))))
833 (defun wesnoth-within-define (position)
834 "Determine whether point is currently inside a #define block.
835 POSITION is the initial cursor position."
836 (save-match-data
837 (let ((depth 0)
838 (defblocks (or wesnoth-define-blocks
839 (wesnoth-find-macro-definitions))))
840 (unless (equal (car defblocks) 'none)
841 (dolist (element defblocks)
842 (when (= (cadr (sort (append (mapcar 'marker-position (cadr element))
843 (list position)) '>)) position)
844 (setq depth (max (car element) depth)))))
845 depth)))
847 (defun wesnoth-find-macro-definitions ()
848 "Return information regarding positioning of macro definitions."
849 (save-excursion
850 (goto-char (point-min))
851 (let ((depth 0)
852 openings cache)
853 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t)
854 (and (string= (match-string 1) "#define") (beginning-of-line))
855 (setq depth
856 (if (string= (match-string 1) "#define")
857 (progn
858 (add-to-list 'openings (point-marker))
859 (1+ depth))
860 (if openings
861 (progn
862 (add-to-list 'cache
863 (list depth (list (car openings)
864 (point-marker))))
865 (setq openings (cdr openings))
866 (1- depth))
867 depth)))
868 (end-of-line))
869 (or cache (list 'none)))))
871 (defun wesnoth-indent-region (start end)
872 "Indent the region from START to END.
874 Creates and destroys a cache of macro definition details as necessary."
875 (interactive "r")
876 (unwind-protect
877 (save-excursion
878 (goto-char end)
879 (setq end (point-marker))
880 (goto-char start)
881 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
882 (or (bolp) (forward-line 1))
883 (while (< (point) end)
884 (if (looking-at "^[\t ]*$")
885 (indent-line-to 0)
886 (funcall indent-line-function))
887 (forward-line 1)))
888 (setq wesnoth-define-blocks nil)))
890 (defun wesnoth-determine-context (position)
891 "Determine the type of the last relevant element.
893 POSITION is the buffer position of the element for which to
894 determine the context."
895 (save-excursion
896 (search-backward-regexp (wesnoth-element t)
897 (point-min) t)
898 (let ((match (or (match-string 1) ""))
899 (depth (wesnoth-within-define position)))
900 (while (and (> (wesnoth-within-define (point)) depth)
901 (not (= (point) (wesnoth-wml-start-pos))))
902 (search-backward-regexp (wesnoth-element t)
903 (wesnoth-wml-start-pos) t)
904 (setq match (match-string 1)))
905 (when (and (= (point) (wesnoth-wml-start-pos)) (= depth 0)
906 (string-match "#define" match))
907 ;; Found nothing of use; reset match and assume top-level tag.
908 (setq match ""))
909 (cond
910 ((string-match "\\[/\\|#enddef" match)
911 (cons 'closing (current-indentation)))
912 ((string-match "\\[[^/]?\\|#define" match)
913 (cons 'opening (current-indentation)))))))
915 (defun wesnoth-newline-and-indent (&optional indent)
916 "Indent both the current line and the newline created.
917 If `wesnoth-auto-indent-flag' is nil, indentation will not be
918 performed.
920 If the optional argument, INDENT is non-nil, force indentation to
921 be performed."
922 (interactive)
923 (wesnoth-newline)
924 (when (or wesnoth-auto-indent-flag indent)
925 (wesnoth-indent)))
927 ;;; WML checks
928 (defun wesnoth-check-element-type (position)
929 "Determine the context of the element.
930 POSITION is the position of the element in the list."
931 (let ((parent (save-match-data (wesnoth-parent-tag))))
932 (if (or (stringp parent) (null parent))
933 (member (match-string-no-properties 1)
934 (nth position (gethash parent wesnoth-tag-hash-table)))
935 (member (match-string-no-properties 1)
936 (mapcar 'car wesnoth-tag-data)))))
938 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
939 (defun wesnoth-line-number-at-pos (&optional pos)
940 "Return (narrowed) buffer line number at position POS.
941 If POS is nil, use current buffer location.
942 Counting starts at (point-min), so the value refers
943 to the contents of the accessible portion of the buffer."
944 (let ((opoint (or pos (point))) start)
945 (save-excursion
946 (goto-char (point-min))
947 (setq start (point))
948 (goto-char opoint)
949 (forward-line 0)
950 (1+ (count-lines start (point))))))
952 (defun wesnoth-check-output (buffer format-string &rest args)
953 "Output the string as passed to `format'.
954 BUFFER is the buffer to output the result.
955 FORMAT-STRING is the string as the first argument of `format'.
956 ARGS is any additional data required by `format' to handle FORMAT-STRING."
957 (save-excursion
958 (let ((lnap (wesnoth-line-number-at-pos)))
959 (set-buffer buffer)
960 (insert (apply 'format (concat "Line %d: " format-string "\n")
961 lnap args)))))
963 (defun wesnoth-extract-macro-details (macro-arguments)
964 "Return a list of all macros in MACRO-ARGUMENTS."
965 (when macro-arguments
966 (let ((results '()))
967 (save-match-data
968 (dolist (macro (split-string macro-arguments "[{}][\t ]*" t))
969 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
970 macro)
971 (add-to-list 'results (match-string-no-properties 1 macro)))))
972 results)))
974 (defun wesnoth-check-wml ()
975 "Perform context-sensitive analysis of WML-code."
976 (interactive)
977 (wesnoth-update-project-information)
978 (when (= 0 (hash-table-count wesnoth-tag-hash-table))
979 (error "WML data not available; can not generate report"))
980 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
981 (let ((unmatched '())
982 (outbuf (get-buffer-create "*WML*"))
983 (last-match-pos 1))
984 (save-excursion
985 (let ((buffer (buffer-name)))
986 (set-buffer outbuf)
987 (erase-buffer)
988 (insert (format "Checking %s...\n" buffer))
989 (message (format "Checking %s..." buffer))))
990 (save-excursion
991 (goto-char (or (wesnoth-wml-start-pos) (point-min)))
992 (while (search-forward-regexp "[\t ]*{\\(\\(\\(\\w\\|_\\)+\\)[^=
993 ]*\\)}" (point-max) t)
994 (dolist (macro (wesnoth-extract-macro-details
995 (match-string-no-properties 1)))
996 (unless (assoc macro
997 (append (wesnoth-macro-arguments)
998 wesnoth-local-macro-data
999 wesnoth-macro-data))
1000 (wesnoth-check-output outbuf "Unknown macro definition: '%s'"
1001 macro))))
1002 (goto-char (or (wesnoth-wml-start-pos) (point-min)))
1003 (while (search-forward-regexp
1004 ;; Match tags, preprocessor statements and attributes.
1005 (concat "^[\t ]*\\(\\[[+/]?\\([a-z]\\(\\w\\|_\\)+\\)\\]\\|"
1006 "\\(\\w\\|_\\)+=\\|"
1007 wesnoth-preprocessor-regexp "\\)")
1008 (point-max) t)
1009 (save-excursion
1010 (goto-char (match-beginning 1))
1011 (cond ((nth 3 (parse-partial-sexp last-match-pos (point)))
1012 nil)
1013 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
1014 (unless (wesnoth-check-element-type 0)
1015 (wesnoth-check-output outbuf
1016 "Tag not available in this context: '%s'"
1017 (match-string-no-properties 1)))
1018 (setq unmatched (cons (match-string-no-properties 1)
1019 unmatched)))
1020 ((looking-at
1021 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1022 "\\( \\(\\w\\|_\\)+\\)*"))
1023 (unless (match-string-no-properties 2)
1024 (wesnoth-check-output
1025 outbuf (concat "Preprocessor statement has no argument: "
1026 (match-string-no-properties 1))))
1027 (unless (string= (match-string-no-properties 1) "#undef")
1028 (setq unmatched (cons (match-string-no-properties 1)
1029 unmatched))))
1030 ((looking-at wesnoth-preprocessor-closing-regexp)
1031 (when (and unmatched
1032 (not (string-match
1033 (cdr (assoc (match-string-no-properties 1)
1034 '(("enddef" . "#define")
1035 ("endif" . "#ifn?def"))))
1036 (car unmatched))))
1037 (wesnoth-check-output
1038 outbuf
1039 "Preprocessor statement does not nest correctly"))
1040 (setq unmatched (cdr unmatched)))
1041 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)=\\(.+\\)?")
1042 (unless (wesnoth-check-element-type 1)
1043 (wesnoth-check-output
1044 outbuf "Attribute not available in this context: '%s'"
1045 (match-string-no-properties 1)))
1046 (unless (match-string 3)
1047 (wesnoth-check-output
1048 outbuf "Attribute has no value")))
1049 ((looking-at "[\t ]*#else")
1050 (unless (string-match "ifn?def" (car unmatched))
1051 (if (string= (car unmatched) "#define")
1052 (wesnoth-check-output outbuf "Expecting: '%s'"
1053 (car unmatched))
1054 (wesnoth-check-output outbuf "Expecting: '[/%s]'"
1055 (car unmatched)))))
1056 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1057 (when (and unmatched
1058 (not (string= (match-string-no-properties 1)
1059 (car unmatched))))
1060 (wesnoth-check-output outbuf "Expecting: '[/%s]'"
1061 (car unmatched)))
1062 (setq unmatched (cdr unmatched)
1063 last-match-pos (point))))))
1064 (if unmatched
1065 (dolist (tag unmatched)
1066 (wesnoth-check-output outbuf "Unmatched element: '%s'"
1067 (car unmatched)))))
1068 (save-excursion
1069 (setq wesnoth-define-blocks nil)
1070 (let ((buffer (buffer-name)))
1071 (set-buffer outbuf)
1072 (display-buffer outbuf t)
1073 (let ((warnings (- (wesnoth-line-number-at-pos
1074 (save-excursion (goto-char (point-max)))) 2)))
1075 (insert (format (concat "\nCheck complete. %d warning"
1076 (if (= warnings 1) "." "s.")) warnings)))
1077 (message (format "Checking %s...done" buffer))))))
1079 (defmacro wesnoth-element-requires (element requirement &optional pop)
1080 "Process requirements for corresponding preprocessor elements.
1081 ELEMENT is the current element being tested.
1082 REQUIREMENT is the element required to exist for correct nesting.
1083 POP is an optional argument indicating the element should be
1084 removed from the list of unmatched elements."
1085 `(when (string= ,element (match-string-no-properties 1))
1087 (if (string-match ,requirement (car unmatched))
1088 (progn
1089 (and ,pop (setq unmatched (cdr unmatched)))
1091 (setq error-position (point)))))
1093 (defmacro wesnoth-structure-result (position element)
1094 "Process results of the structure check.
1095 POSITION is the error position or nil, if no error was found.
1096 ELEMENT is the last unmatched element, or nil if all opening
1097 elements have been matched."
1098 `(let ((expected nil))
1099 (when ,element
1100 (cond ((string= ,element "define ") (setq expected "#enddef"))
1101 ((string-match "ifn?def " ,element) (setq expected "#endif"))))
1102 (if (interactive-p)
1103 (if (or ,element ,position)
1104 (progn
1105 (and ,position (goto-char ,position))
1106 (message "Error: Expecting %s" (or expected
1107 (concat "[/" ,element "]"))))
1108 (message "%s" "Structure appears consistent."))
1109 (when (or expected ,element)
1110 (or expected (concat "[/" ,element "]"))))))
1112 (defun wesnoth-check-structure (&optional start end)
1113 "Check the buffer for correct nesting of elements.
1114 If a problem is found in the structure, point will be placed at
1115 the location which an element was expected and the expected
1116 element will be displayed in the mini-buffer.
1118 START and END define the region to be checked. If
1119 function `transient-mark-mode' is enabled, the region specified will be
1120 checked. Otherwise START and END will be the minimum and maximum
1121 positions of the buffer, respectively."
1122 (interactive)
1123 (unless (or start end)
1124 (if (and (boundp 'transient-mark-mode)
1125 transient-mark-mode mark-active)
1126 (setq start (region-beginning)
1127 end (copy-marker (region-end)))
1128 (setq start (point-min)
1129 end (point-max))))
1130 (let ((unmatched '())
1131 (error-position nil))
1132 (save-excursion
1133 (and start (goto-char start))
1134 (while (and (search-forward-regexp
1135 (concat "^\\([\t ]*\\[\\(/?\\(\\w\\|_\\)+\\)\\]\\|"
1136 wesnoth-preprocessor-regexp "\\)") end t)
1137 (not error-position))
1138 (beginning-of-line)
1139 (if (or (looking-at "^[\t ]*\\[\\(\\(\\w\\|_\\)+\\)\\]")
1140 (looking-at "[\t ]*#\\(define \\|ifdef \\|ifndef \\)"))
1141 (setq unmatched (cons (match-string-no-properties 1)
1142 unmatched))
1143 (cond
1144 ((wesnoth-element-requires "#else" "ifn?def "))
1145 ((wesnoth-element-requires "#endif" "ifn?def " t))
1146 ((wesnoth-element-requires "#enddef" "define " t))
1147 ((looking-at (concat "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|"
1148 wesnoth-preprocessor-closing-regexp))
1149 (if (string= (match-string-no-properties 1)
1150 (car unmatched))
1151 (setq unmatched (cdr unmatched))
1152 (setq error-position (point))))))
1153 (end-of-line)))
1154 (wesnoth-structure-result error-position (car unmatched))))
1156 ;;; wesnoth-mode
1157 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
1158 "Major mode for editing WML."
1159 (kill-all-local-variables)
1160 (use-local-map wesnoth-mode-map)
1161 (setq major-mode 'wesnoth-mode)
1162 (setq mode-name "WML")
1163 (set-syntax-table wesnoth-syntax-table)
1164 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
1165 (set (make-local-variable 'comment-start) "#")
1166 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
1167 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
1168 (set (make-local-variable 'font-lock-defaults)
1169 '(wesnoth-font-lock-keywords
1170 nil t nil nil
1171 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
1172 (setq indent-tabs-mode nil)
1173 (easy-menu-add wesnoth-menu wesnoth-mode-map)
1174 (wesnoth-create-wml-hash-table)
1175 (wesnoth-update-project-information)
1176 (run-hooks 'wesnoth-mode-hook))
1178 (provide 'wesnoth-mode)
1180 ;;; wesnoth-mode.el ends here