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