* wesnoth-mode.el (wesnoth-complete-macro, wesnoth-complete-preprocessor,
[wesnoth-mode.git] / wesnoth-mode.el
blob859902cc9e4701f9e9cd635aeb17c36242784115
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-no-properties 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-no-properties 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-no-properties 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-no-properties 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 (if (looking-at "^[\t ]*$")
594 (end-of-line)
595 (open-line 1)))
596 (insert (apply 'concat strings)))
598 (defun wesnoth-insert-missing-closing (&optional start end)
599 "Insert the next expected closing element at point.
601 START and END define the region to check for missing closing
602 elements. If function `transient-mark-mode' is enabled, the region
603 specified will be used as START and END. Otherwise, START and
604 END will be the minimum and current positions of the buffer,
605 respectively."
606 (interactive)
607 (if (and (boundp 'transient-mark-mode)
608 transient-mark-mode mark-active)
609 (setq start (region-beginning)
610 end (copy-marker (region-end)))
611 (setq start (point-min)
612 end (point)))
613 (let ((element (wesnoth-check-structure start end)))
614 (if (not element)
615 (error "%s" "Unable to find element to insert")
616 (when (string= element "Unexpected end of file")
617 (error "%s" element))
618 (wesnoth-insert-element-separately element)))
619 (wesnoth-indent)
620 (end-of-line))
622 (defun wesnoth-insert-and-indent (&rest args)
623 "Concatenate and insert the given string(s) before indenting.
625 ARGS is a list of strings to be inserted."
626 (insert (apply 'concat args))
627 (wesnoth-indent))
629 (defun wesnoth-newline (&optional indent)
630 "Indent both the current line and the newline created.
631 If `wesnoth-auto-indent-flag' is nil, indentation will not be
632 performed. Indentation can be forced by setting INDENT to
633 non-nil."
634 (interactive)
635 (save-excursion
636 (when (and (or wesnoth-auto-indent-flag indent)
637 (not (looking-at "^[\t ]*$")))
638 (wesnoth-indent)))
639 (newline))
641 ;;; Movement
642 (defmacro wesnoth-navigate-element (repeat search-function bound)
643 "Move point to the tag in the given direction REPEAT times.
645 SEARCH-FUNCTION is the symbol of the function for searching in
646 the required direction, with BOUND marking the furthest point to
647 search."
648 `(progn
649 (or ,repeat (setq ,repeat 1))
650 (while (> ,repeat 0)
651 (and (eq ,search-function 'search-forward-regexp) (end-of-line))
652 (funcall ,search-function (wesnoth-element-opening) ,bound t)
653 (back-to-indentation)
654 (decf ,repeat))))
656 (defun wesnoth-nth-pair-position (count)
657 "Return `point' after COUNT number of matching element pairs.
658 COUNT is a positive number representing the number of balanced
659 pairs to move across.
660 `point' is returned as a marker object."
661 (save-excursion
662 (let ((start (point))
663 (failed nil))
664 (if (> (point) (save-excursion (back-to-indentation) (point)))
665 (end-of-line)
666 (beginning-of-line))
667 (while (> count 0)
668 ;; Currently looking-at target tag. Stop here to avoid
669 ;; incorrect nesting.
670 (unless (wesnoth-search-for-matching-tag
671 'search-forward-regexp (wesnoth-element-closing) 'point-max)
672 (setq count 0)
673 (unless (or (= (point) (point-max))
674 (progn (beginning-of-line)
675 (search-backward-regexp (wesnoth-element-closing)
676 start t)))
677 (setq failed t)))
678 (and (> (decf count) 0) (forward-line 1)))
679 (if failed
680 (beginning-of-line)
681 (end-of-line))
682 (point-marker))))
684 (defun wesnoth-forward-element (repeat)
685 "Move point to the end of the next tag.
686 REPEAT is an optional numeric argument. If REPEAT is non-nil,
687 jump forward the specified number of tags."
688 (interactive "p")
689 (if (< repeat 0)
690 (wesnoth-backward-element (abs repeat))
691 (wesnoth-navigate-element repeat 'search-forward-regexp (point-max))))
693 (defun wesnoth-backward-element (repeat)
694 "Move point to the beginning of the previous tag.
695 REPEAT is an optional numeric argument. If REPEAT is non-nil,
696 jump backward the specified number of tags."
697 (interactive "p")
698 (if (< repeat 0)
699 (wesnoth-forward-element (abs repeat))
700 (wesnoth-navigate-element repeat 'search-backward-regexp (point-min))))
702 (defmacro wesnoth-search-for-matching-tag (search-function
703 search-string bound &optional skip)
704 "Search for the matching tag for the current line.
706 SEARCH-FUNCTION is the name of the function used to perform the search.
707 SEARCH-STRING is a string representing the matching tag type.
708 BOUND is the bound to be passed to the search function.
709 If SKIP is non-nil, skip the first element and continue from there."
710 `(let ((depth 1))
711 (when (or (and ,skip (forward-line 1))
712 (funcall ,search-function (wesnoth-element) (funcall ,bound) t))
713 (when (or ,skip (not (string-match ,search-string (match-string 0))))
714 (while (and (> depth 0)
715 (funcall ,search-function (wesnoth-element)
716 (funcall ,bound) t))
717 (if (string-match ,search-string (match-string 0))
718 (decf depth)
719 (incf depth)))
720 (= depth 0)))))
722 (defun wesnoth-jump-to-matching (&optional opening)
723 "Jump point to the matching opening/closing tag.
724 OPENING is an opening preprocessor statement to attempt to find a match for."
725 (interactive)
726 (beginning-of-line)
727 (let ((target nil)
728 (first-element nil))
729 (save-excursion
730 (if (or (and (stringp opening)
731 (string-match (wesnoth-element-opening) opening))
732 (looking-at (wesnoth-element-opening)))
733 (progn
734 (setq first-element (match-string-no-properties 0 opening))
735 (when (wesnoth-search-for-matching-tag
736 'search-forward-regexp (wesnoth-element-closing) 'point-max
737 (stringp opening))
738 (beginning-of-line)
739 (if (and (string-match wesnoth-preprocessor-opening-regexp
740 first-element)
741 (looking-at (wesnoth-element-closing)))
742 (when (string= (match-string-no-properties 0)
743 (cdr (assoc first-element
744 '(("#define " . "#enddef")
745 ("#ifndef " . "#endif")
746 ("#ifdef " . "#endif")))))
747 (setq target (point)))
748 (setq target (point)))))
749 (when (looking-at (wesnoth-element-closing))
750 (setq first-element (match-string-no-properties 0))
751 (end-of-line)
752 (when (wesnoth-search-for-matching-tag
753 'search-backward-regexp (wesnoth-element-opening)
754 'wesnoth-wml-start-pos)
755 (if (and (string-match wesnoth-preprocessor-closing-regexp
756 first-element)
757 (looking-at (wesnoth-element-opening)))
758 (progn
759 (when (or (and (string= "#enddef" first-element)
760 (string= "#define "
761 (match-string-no-properties 0)))
762 (and (string= "#endif" first-element)
763 (string-match
764 "#ifn?def "
765 (match-string-no-properties 0))))
766 (setq target (point))))
767 (setq target (point)))))))
768 (if target
769 (goto-char target)
770 (when (interactive-p)
771 (error "Tag does not appear to be matched"))))
772 (back-to-indentation))
774 ;;; Indentation
775 (defun wesnoth-wml-start-pos ()
776 "Determine the position of `point' relative to where the actual WML begins.
777 Return the likely starting position of the WML if it is found.
778 Otherwise return nil."
779 (save-excursion
780 (goto-char (point-min))
781 (when (search-forward-regexp (wesnoth-element) (point-max) t)
782 (beginning-of-line)
783 (point))))
785 (defun first-column-indent-p (point)
786 "Return non-nil if the current line should not be indented.
788 POINT is the position in the buffer to check.
789 CONTEXT represents the type of element which precedes the current element."
790 (or (not (wesnoth-wml-start-pos))
791 (<= (point) (wesnoth-wml-start-pos))
792 (nth 3 (parse-partial-sexp (point-min) point))
793 (looking-at wesnoth-preprocessor-regexp)))
795 (defun wesnoth-indent ()
796 "Indent the current line as WML."
797 (beginning-of-line)
798 (let ((cur-indent 0))
799 (unless (first-column-indent-p (point))
800 (multiple-value-bind (context ref-indent)
801 (wesnoth-determine-context (point))
802 (cond
803 ((eq context 'opening)
804 (if (or (and wesnoth-indent-savefile
805 (not (looking-at (wesnoth-element-closing t))))
806 (looking-at (wesnoth-element-opening t)))
807 (setq cur-indent (+ ref-indent wesnoth-base-indent))
808 (setq cur-indent ref-indent)))
809 ((eq context 'closing)
810 (if (or (looking-at "^[\t ]*\\[/")
811 (and (not wesnoth-indent-savefile)
812 (not (looking-at (wesnoth-element-opening t)))))
813 (setq cur-indent (- ref-indent wesnoth-base-indent))
814 (setq cur-indent ref-indent))))))
815 (indent-line-to (max cur-indent 0))))
817 (defun wesnoth-within-define (position)
818 "Determine whether point is currently inside a #define block.
819 POSITION is the initial cursor position."
820 (save-match-data
821 (let ((depth 0))
822 (dolist (element (or wesnoth-define-blocks
823 (wesnoth-find-macro-definitions)))
824 (when (= (cadr (sort (append (mapcar 'marker-position (cadr element))
825 (list position)) '>)) position)
826 (setq depth (max (car element) depth))))
827 depth)))
829 (defun wesnoth-find-macro-definitions ()
830 "Return information regarding positioning of macro definitions."
831 (save-excursion
832 (goto-char (point-min))
833 (let ((depth 0)
834 openings cache)
835 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t)
836 (and (string= (match-string 1) "#define") (beginning-of-line))
837 (setq depth
838 (if (string= (match-string 1) "#define")
839 (progn
840 (add-to-list 'openings (point-marker))
841 (1+ depth))
842 (if openings
843 (progn
844 (add-to-list 'cache
845 (list depth (list (car openings)
846 (point-marker))))
847 (setq openings (cdr openings))
848 (1- depth))
849 depth)))
850 (end-of-line))
851 cache)))
853 (defun wesnoth-indent-region (start end)
854 "Indent the region from START to END.
856 Creates and destroys a cache of macro definition details as necessary."
857 (interactive "r")
858 (unwind-protect
859 (save-excursion
860 (goto-char end)
861 (setq end (point-marker))
862 (goto-char start)
863 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
864 (or (bolp) (forward-line 1))
865 (while (< (point) end)
866 (if (looking-at "^[\t ]*$")
867 (indent-line-to 0)
868 (funcall indent-line-function))
869 (forward-line 1)))
870 (setq wesnoth-define-blocks nil)))
872 (defun wesnoth-determine-context (position)
873 "Determine the type of the last relevant element.
875 POSITION is the buffer position of the element for which to
876 determine the context."
877 (save-excursion
878 (search-backward-regexp (wesnoth-element t)
879 (wesnoth-wml-start-pos) t)
880 (let ((match (or (match-string 1) ""))
881 (depth (wesnoth-within-define position)))
882 (while (and (> (wesnoth-within-define (point)) depth)
883 (not (= (point) (wesnoth-wml-start-pos))))
884 (search-backward-regexp (wesnoth-element t)
885 (wesnoth-wml-start-pos) t)
886 (setq match (match-string 1)))
887 (when (and (= (point) (wesnoth-wml-start-pos)) (= depth 0)
888 (string-match "#define" match))
889 ;; Found nothing of use; reset match and assume top-level tag.
890 (setq match ""))
891 (cond
892 ((string-match "\\[/\\|#enddef" match)
893 (values 'closing (current-indentation)))
894 ((string-match "\\[[^/]?\\|#define" match)
895 (values 'opening (current-indentation)))))))
897 (defun wesnoth-newline-and-indent (&optional indent)
898 "Indent both the current line and the newline created.
899 If `wesnoth-auto-indent-flag' is nil, indentation will not be
900 performed.
902 If the optional argument, INDENT is non-nil, force indentation to
903 be performed."
904 (interactive)
905 (wesnoth-newline)
906 (when (or wesnoth-auto-indent-flag indent)
907 (wesnoth-indent)))
909 ;;; WML checks
910 (defun wesnoth-check-element-type (position last-tag)
911 "Determine the context of the element.
912 POSITION is the position of the element in the list.
913 LAST-TAG is the parent element."
914 (if (or (not last-tag)
915 (save-match-data
916 (string-match "#\\(?:define\\|ifn?def\\)" last-tag)))
917 (member (match-string-no-properties 1)
918 (mapcar 'car wesnoth-tag-data))
919 (member (match-string-no-properties 1)
920 (nth position (gethash last-tag
921 wesnoth-tag-hash-table)))))
923 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
924 (defun wesnoth-line-number-at-pos (&optional pos)
925 "Return (narrowed) buffer line number at position POS.
926 If POS is nil, use current buffer location.
927 Counting starts at (point-min), so the value refers
928 to the contents of the accessible portion of the buffer."
929 (let ((opoint (or pos (point))) start)
930 (save-excursion
931 (goto-char (point-min))
932 (setq start (point))
933 (goto-char opoint)
934 (forward-line 0)
935 (1+ (count-lines start (point))))))
937 (defun wesnoth-check-output (buffer format-string &rest args)
938 "Output the string as passed to `format'.
939 BUFFER is the buffer to output the result.
940 FORMAT-STRING is the string as the first argument of `format'.
941 ARGS is any additional data required by `format' to handle FORMAT-STRING."
942 (save-excursion
943 (let ((lnap (wesnoth-line-number-at-pos)))
944 (set-buffer buffer)
945 (insert (apply 'format (concat "Line %d: " format-string "\n")
946 lnap args)))))
948 (defun wesnoth-extract-macro-details (macro-arguments)
949 "Return a list of all macros in MACRO-ARGUMENTS."
950 (when macro-arguments
951 (let ((results '()))
952 (save-match-data
953 (dolist (macro (split-string macro-arguments "[{}][\t ]*" t))
954 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
955 macro)
956 (add-to-list 'results (match-string-no-properties 1 macro)))))
957 results)))
959 (defun wesnoth-check-wml ()
960 "Perform context-sensitive analysis of WML-code."
961 (interactive)
962 (wesnoth-update-project-information)
963 (when (= 0 (hash-table-count wesnoth-tag-hash-table))
964 (error "WML data not available; can not generate report"))
965 (let ((unmatched '())
966 (outbuf (get-buffer-create "*WML*")))
967 (save-excursion
968 (let ((buffer (buffer-name)))
969 (set-buffer outbuf)
970 (erase-buffer)
971 (insert (format "Checking %s...\n" buffer))
972 (message (format "Checking %s..." buffer))))
973 (save-excursion
974 (goto-char (or (wesnoth-wml-start-pos) (point-min)))
975 (while (search-forward-regexp "[\t ]*{\\(\\(\\(\\w\\|_\\)+\\)[^=
976 ]*\\)}" (point-max) t)
977 (dolist (macro (wesnoth-extract-macro-details
978 (match-string-no-properties 1)))
979 (unless (assoc macro
980 (append (wesnoth-macro-arguments)
981 wesnoth-local-macro-data
982 wesnoth-macro-data))
983 (wesnoth-check-output outbuf "Unknown macro definition: '{%s}'"
984 macro))))
985 (goto-char (or (wesnoth-wml-start-pos) (point-min)))
986 (while (search-forward-regexp
987 ;; Match tags, preprocessor statements and attributes.
988 (concat "^[\t ]*\\(\\[[+/]?\\([a-z]\\(\\w\\|_\\)+\\)\\]\\|"
989 "\\(\\w\\|_\\)+=\\|"
990 wesnoth-preprocessor-regexp "\\)")
991 (point-max) t)
992 (save-excursion
993 (goto-char (match-beginning 1))
994 (cond ((nth 3 (parse-partial-sexp (point-min) (point)))
995 nil)
996 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
997 (unless (wesnoth-check-element-type 0 (car unmatched))
998 (wesnoth-check-output outbuf
999 "Tag not available in this context: '%s'"
1000 (match-string-no-properties 1)))
1001 (setq unmatched (cons (match-string-no-properties 1)
1002 unmatched)))
1003 ((looking-at
1004 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1005 "\\( \\(\\w\\|_\\)+\\)*"))
1006 (unless (match-string-no-properties 2)
1007 (wesnoth-check-output
1008 outbuf (concat "Preprocessor statement has no argument: "
1009 (match-string-no-properties 1))))
1010 (unless (string= (match-string-no-properties 1) "#undef")
1011 (setq unmatched (cons (match-string-no-properties 1)
1012 unmatched))))
1013 ((looking-at wesnoth-preprocessor-closing-regexp)
1014 (when (and unmatched
1015 (not (string-match
1016 (cdr (assoc (match-string-no-properties 1)
1017 '(("enddef" . "#define")
1018 ("endif" . "#ifn?def"))))
1019 (car unmatched))))
1020 (wesnoth-check-output
1021 outbuf
1022 "Preprocessor statement does not nest correctly"))
1023 (setq unmatched (cdr unmatched)))
1024 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)=\\(.+\\)?")
1025 (unless (wesnoth-check-element-type 1 (car unmatched))
1026 (wesnoth-check-output
1027 outbuf "Attribute not available in this context: '%s'"
1028 (match-string-no-properties 1)))
1029 (unless (match-string 3)
1030 (wesnoth-check-output
1031 outbuf "Attribute has no value")))
1032 ((looking-at "[\t ]*#else")
1033 (unless (string-match "ifn?def" (car unmatched))
1034 (if (string= (car unmatched) "#define")
1035 (wesnoth-check-output outbuf "Expecting: '%s'"
1036 (car unmatched))
1037 (wesnoth-check-output outbuf "Expecting: '[/%s]'"
1038 (car unmatched)))))
1039 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1040 (when (and unmatched
1041 (not (string= (match-string-no-properties 1)
1042 (car unmatched))))
1043 (wesnoth-check-output outbuf "Expecting: '[/%s]'"
1044 (car unmatched)))
1045 (setq unmatched (cdr unmatched))))))
1046 (if unmatched
1047 (dolist (tag unmatched)
1048 (wesnoth-check-output outbuf "Unmatched element: '%s'"
1049 (car unmatched)))))
1050 (save-excursion
1051 (let ((buffer (buffer-name)))
1052 (set-buffer outbuf)
1053 (display-buffer outbuf t)
1054 (let ((warnings (- (wesnoth-line-number-at-pos
1055 (save-excursion (goto-char (point-max)))) 2)))
1056 (insert (format (concat "\nCheck complete. %d warning"
1057 (if (= warnings 1) "." "s.")) warnings)))
1058 (message (format "Checking %s...done" buffer))))))
1060 (defmacro wesnoth-element-requires (element requirement &optional pop)
1061 "Process requirements for corresponding preprocessor elements.
1062 ELEMENT is the current element being tested.
1063 REQUIREMENT is the element required to exist for correct nesting.
1064 POP is an optional argument indicating the element should be
1065 removed from the list of unmatched elements."
1066 `(when (string= ,element (match-string-no-properties 1))
1068 (if (string-match ,requirement (car unmatched))
1069 (progn
1070 (and ,pop (setq unmatched (cdr unmatched)))
1072 (setq error-position (point)))))
1074 (defmacro wesnoth-structure-result (position element)
1075 "Process results of the structure check.
1076 POSITION is the error position or nil, if no error was found.
1077 ELEMENT is the last unmatched element, or nil if all opening
1078 elements have been matched."
1079 `(let ((expected nil))
1080 (when ,element
1081 (cond ((string= ,element "define ") (setq expected "#enddef"))
1082 ((string-match "ifn?def " ,element) (setq expected "#endif"))))
1083 (if (interactive-p)
1084 (if (or ,element ,position)
1085 (progn
1086 (and ,position (goto-char ,position))
1087 (message "Error: Expecting %s" (or expected
1088 (concat "[/" ,element "]"))))
1089 (message "%s" "Structure appears consistent."))
1090 (when (or expected ,element)
1091 (or expected (concat "[/" ,element "]"))))))
1093 (defun wesnoth-check-structure (&optional start end)
1094 "Check the buffer for correct nesting of elements.
1095 If a problem is found in the structure, point will be placed at
1096 the location which an element was expected and the expected
1097 element will be displayed in the mini-buffer.
1099 START and END define the region to be checked. If
1100 function `transient-mark-mode' is enabled, the region specified will be
1101 checked. Otherwise START and END will be the minimum and maximum
1102 positions of the buffer, respectively."
1103 (interactive)
1104 (unless (or start end)
1105 (if (and (boundp 'transient-mark-mode)
1106 transient-mark-mode mark-active)
1107 (setq start (region-beginning)
1108 end (copy-marker (region-end)))
1109 (setq start (point-min)
1110 end (point-max))))
1111 (let ((unmatched '())
1112 (error-position nil))
1113 (save-excursion
1114 (and start (goto-char start))
1115 (while (and (search-forward-regexp
1116 (concat "^\\([\t ]*\\[\\(/?\\(\\w\\|_\\)+\\)\\]\\|"
1117 wesnoth-preprocessor-regexp "\\)") end t)
1118 (not error-position))
1119 (beginning-of-line)
1120 (if (or (looking-at "^[\t ]*\\[\\(\\(\\w\\|_\\)+\\)\\]")
1121 (looking-at "[\t ]*#\\(define \\|ifdef \\|ifndef \\)"))
1122 (setq unmatched (cons (match-string-no-properties 1)
1123 unmatched))
1124 (cond
1125 ((wesnoth-element-requires "#else" "ifn?def "))
1126 ((wesnoth-element-requires "#endif" "ifn?def " t))
1127 ((wesnoth-element-requires "#enddef" "define " t))
1128 ((looking-at (concat "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|"
1129 wesnoth-preprocessor-closing-regexp))
1130 (if (string= (match-string-no-properties 1)
1131 (car unmatched))
1132 (setq unmatched (cdr unmatched))
1133 (setq error-position (point))))))
1134 (end-of-line)))
1135 (wesnoth-structure-result error-position (car unmatched))))
1137 ;;; wesnoth-mode
1138 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
1139 "Major mode for editing WML."
1140 (kill-all-local-variables)
1141 (use-local-map wesnoth-mode-map)
1142 (setq major-mode 'wesnoth-mode)
1143 (setq mode-name "WML")
1144 (set-syntax-table wesnoth-syntax-table)
1145 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
1146 (set (make-local-variable 'comment-start) "#")
1147 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
1148 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
1149 (set (make-local-variable 'font-lock-defaults)
1150 '(wesnoth-font-lock-keywords
1151 nil t nil nil
1152 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
1153 (setq indent-tabs-mode nil)
1154 (easy-menu-add wesnoth-menu wesnoth-mode-map)
1155 (wesnoth-create-wml-hash-table)
1156 (wesnoth-update-project-information)
1157 (run-hooks 'wesnoth-mode-hook))
1159 (provide 'wesnoth-mode)
1161 ;;; wesnoth-mode.el ends here