* wesnoth-update: Updated changelog.
[wesnoth-mode.git] / wesnoth-mode.el
blobc8effcb8e28cafd3cf4ecbe0d861c42bda8ac4a8
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 ;; * Added completion and checking for macro arguments.
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.0+git"
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 (save-excursion
414 (unless (= 0 (wesnoth-within-define (point)))
415 (save-match-data
416 (search-backward-regexp
417 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
418 (point-min) t)
419 (mapcar (lambda (macro) (list macro nil))
420 (split-string
421 (match-string-no-properties 1)))))))
423 (defun wesnoth-complete-macro (&optional completep)
424 "Complete and insert the macro at point.
425 If COMPLETEP is non-nil, attempt to complete the macro at point."
426 (interactive)
427 (wesnoth-update-project-information)
428 (let* ((macro-information (append (wesnoth-macro-arguments)
429 wesnoth-macro-data
430 wesnoth-local-macro-data))
431 (completions (wesnoth-emacs-completion-formats
432 (mapcar 'car macro-information)))
433 (partial (when completep
434 (save-excursion
435 (back-to-indentation)
436 (when (looking-at "{\\(\\(\\w\\|_\\)*\\)")
437 (match-string 1)))))
438 (macro (or (wesnoth-element-completion completions "Macro: " partial)
439 partial))
440 (args (cadr (assoc macro macro-information))))
441 (when macro
442 (if partial
443 (progn
444 (delete-region (progn (back-to-indentation) (point))
445 (progn (end-of-line) (point)))
446 (insert "{" macro (if args " }" "}")))
447 (wesnoth-insert-element-separately "{" macro (if args " }" "}")))
448 (save-excursion
449 (wesnoth-indent))
450 (when args
451 (forward-char -1)))))
453 (defun wesnoth-complete-attribute (&optional completep)
454 "Insert the attribute at point.
455 If COMPLETEP is non-nil, attempt to complete the attribute at point."
456 (interactive)
457 (let* ((completions (wesnoth-build-completion 2))
458 (partial (when completep
459 (save-excursion
460 (back-to-indentation)
461 (when (looking-at "\\(\\(\\w\\|_\\)+\\)")
462 (match-string 1)))))
463 (attribute (or (wesnoth-element-completion completions "Attribute: "
464 partial)
465 partial)))
466 (when attribute
467 (if (and partial completep)
468 (progn
469 (delete-region (progn (back-to-indentation) (point))
470 (progn (end-of-line) (point)))
471 (insert attribute "="))
472 (wesnoth-insert-element-separately attribute
473 (if (string-match "=" attribute)
475 "=")))
476 (save-excursion
477 (wesnoth-indent)))))
479 (defun wesnoth-complete-tag (&optional elements completep)
480 "Complete and insert the tag at point.
481 ELEMENTS is the number of elements to wrap around.
482 If COMPLETEP is non-nil, attempt to complete tag at point."
483 (interactive "P")
484 (or elements (setq elements 0))
485 (let* ((completions (wesnoth-build-completion 1))
486 (partial (when completep
487 (save-excursion
488 (back-to-indentation)
489 (when (looking-at "\\[\\(\\(\\w\\|_\\)+\\)")
490 (match-string 1)))))
491 (tag (or (wesnoth-element-completion completions "Tag: " partial)
492 partial))
493 (closedp
494 (save-excursion
495 (wesnoth-jump-to-matching)
496 (back-to-indentation)
497 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
498 (string= tag (match-string 1))))))
499 (when completep
500 (delete-region (progn (back-to-indentation) (point))
501 (progn (end-of-line) (point))))
502 (if (and closedp completep)
503 (progn
504 (wesnoth-insert-and-indent "[" tag "]")
505 (end-of-line))
506 (wesnoth-insert-tag elements tag))))
508 (defun wesnoth-build-completion (position)
509 "Create a new list for tag completion if necessary.
510 Rebuilding list is required for versions of GNU Emacs earlier
511 than 22. POSITION is the argument passed to `nth' for
512 `wesnoth-tag-data'."
513 (interactive "P")
514 (let* ((parent (wesnoth-parent-tag))
515 (candidates
516 (if (or (stringp parent) (null parent))
517 (nth (1- position) (gethash parent wesnoth-tag-hash-table))
518 (mapcar 'car wesnoth-tag-data))))
519 (wesnoth-emacs-completion-formats candidates)))
521 (defun wesnoth-emacs-completion-formats (candidates)
522 "Return the completions in the correct format for `emacs-major-version'.
523 CANDIDATES is a list of all possible completions."
524 (if (> emacs-major-version 21)
525 candidates
526 (let ((tags '())
527 (iter 0))
528 (dolist (tag candidates)
529 (setq iter (1+ iter))
530 (setq tags (append tags (list (cons tag iter)))))
531 tags)))
533 (defun wesnoth-insert-tag (&optional elements tagname)
534 "Insert the specified opening tag and it's matching closing tag.
535 Both the opening and closing tags will be placed on their own
536 lines with point positioned between them. Completion of tags at
537 the prompt uses `wesnoth-tags-list'.
539 ELEMENTS is specifies the number of following blocks which the
540 tag should wrap around.
542 TAGNAME is the name of the tag to be inserted."
543 (interactive "Ps")
544 (unless tagname
545 (setq tagname (completing-read "Tag: " (wesnoth-build-completion 1)
546 nil nil nil wesnoth-history-list)))
547 (when (or (not elements)
548 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
549 wesnoth-preprocessor-regexp "\\)")))
550 (setq elements 0))
551 (let ((depth 0)
552 (start (save-excursion (forward-line -1) (point)))
553 (end (unless (= elements 0)
554 (wesnoth-nth-pair-position elements))))
555 (if (string-match wesnoth-preprocessor-regexp tagname)
556 (wesnoth-insert-element-separately tagname)
557 (wesnoth-insert-element-separately "[" tagname "]"))
558 (save-excursion
559 (if end
560 (goto-char (marker-position end))
561 (newline (if (string-match wesnoth-preprocessor-regexp tagname) 1 2)))
562 (if (string-match wesnoth-preprocessor-opening-regexp tagname)
563 (wesnoth-insert-element-separately
564 (if (string= tagname "#define ")
565 "#enddef"
566 "#endif"))
567 (wesnoth-insert-element-separately "[/" tagname "]"))
568 (indent-region start (point) nil))
569 (unless end
570 (forward-line 1)))
571 (wesnoth-indent))
573 (defun wesnoth-insert-element-separately (&rest strings)
574 "Concatenate STRINGS and insert them on a line of their own."
575 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
576 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
577 (progn
578 (forward-line 1)
579 (end-of-line))
580 (end-of-line)
581 (newline))
582 (beginning-of-line)
583 (unless (looking-at "^[\t ]*$")
584 (open-line 1)))
585 (insert (apply 'concat strings)))
587 (defun wesnoth-insert-missing-closing (&optional start end)
588 "Insert the next expected closing element at point.
590 START and END define the region to check for missing closing
591 elements. If function `transient-mark-mode' is enabled, the region
592 specified will be used as START and END. Otherwise, START and
593 END will be the minimum and current positions of the buffer,
594 respectively."
595 (interactive)
596 (if (and (boundp 'transient-mark-mode)
597 transient-mark-mode mark-active)
598 (setq start (region-beginning)
599 end (copy-marker (region-end)))
600 (setq start (point-min)
601 end (point)))
602 (let ((element (wesnoth-check-structure start end)))
603 (if (not element)
604 (error "%s" "Unable to find element to insert")
605 (when (string= element "Unexpected end of file")
606 (error "%s" element))
607 (wesnoth-insert-element-separately element)))
608 (wesnoth-indent)
609 (end-of-line))
611 (defun wesnoth-insert-and-indent (&rest args)
612 "Concatenate and insert the given string(s) before indenting.
614 ARGS is a list of strings to be inserted."
615 (insert (apply 'concat args))
616 (wesnoth-indent))
618 (defun wesnoth-newline (&optional indent)
619 "Indent both the current line and the newline created.
620 If `wesnoth-auto-indent-flag' is nil, indentation will not be
621 performed. Indentation can be forced by setting INDENT to
622 non-nil."
623 (interactive)
624 (save-excursion
625 (when (and (or wesnoth-auto-indent-flag indent)
626 (not (looking-at "^[\t ]*$")))
627 (wesnoth-indent)))
628 (newline))
630 ;;; Movement
631 (defmacro wesnoth-navigate-element (repeat search-function bound)
632 "Move point to the tag in the given direction REPEAT times.
634 SEARCH-FUNCTION is the symbol of the function for searching in
635 the required direction, with BOUND marking the furthest point to
636 search."
637 `(progn
638 (or ,repeat (setq ,repeat 1))
639 (while (> ,repeat 0)
640 (and (eq ,search-function 'search-forward-regexp) (end-of-line))
641 (funcall ,search-function (wesnoth-element-opening) ,bound t)
642 (back-to-indentation)
643 (decf ,repeat))))
645 (defun wesnoth-nth-pair-position (count)
646 "Return `point' after COUNT number of matching element pairs.
647 COUNT is a positive number representing the number of balanced
648 pairs to move across.
649 `point' is returned as a marker object."
650 (save-excursion
651 (let ((start (point))
652 (failed nil))
653 (if (> (point) (save-excursion (back-to-indentation) (point)))
654 (end-of-line)
655 (beginning-of-line))
656 (while (> count 0)
657 ;; Currently looking-at target tag. Stop here to avoid
658 ;; incorrect nesting.
659 (unless (wesnoth-search-for-matching-tag
660 'search-forward-regexp (wesnoth-element-closing) 'point-max)
661 (setq count 0)
662 (unless (or (= (point) (point-max))
663 (progn (beginning-of-line)
664 (search-backward-regexp (wesnoth-element-closing)
665 start t)))
666 (setq failed t)))
667 (and (> (decf count) 0) (forward-line 1)))
668 (if failed
669 (beginning-of-line)
670 (end-of-line))
671 (point-marker))))
673 (defun wesnoth-forward-element (repeat)
674 "Move point to the end of the next tag.
675 REPEAT is an optional numeric argument. If REPEAT is non-nil,
676 jump forward the specified number of tags."
677 (interactive "p")
678 (if (< repeat 0)
679 (wesnoth-backward-element (abs repeat))
680 (wesnoth-navigate-element repeat 'search-forward-regexp (point-max))))
682 (defun wesnoth-backward-element (repeat)
683 "Move point to the beginning of the previous tag.
684 REPEAT is an optional numeric argument. If REPEAT is non-nil,
685 jump backward the specified number of tags."
686 (interactive "p")
687 (if (< repeat 0)
688 (wesnoth-forward-element (abs repeat))
689 (wesnoth-navigate-element repeat 'search-backward-regexp (point-min))))
691 (defmacro wesnoth-search-for-matching-tag (search-function
692 search-string bound &optional skip)
693 "Search for the matching tag for the current line.
695 SEARCH-FUNCTION is the name of the function used to perform the search.
696 SEARCH-STRING is a string representing the matching tag type.
697 BOUND is the bound to be passed to the search function.
698 If SKIP is non-nil, skip the first element and continue from there."
699 `(let ((depth 1))
700 (when (or (and ,skip (forward-line 1))
701 (funcall ,search-function (wesnoth-element) (funcall ,bound) t))
702 (when (or ,skip (not (string-match ,search-string (match-string 0))))
703 (while (and (> depth 0)
704 (funcall ,search-function (wesnoth-element)
705 (funcall ,bound) t))
706 (if (string-match ,search-string (match-string 0))
707 (decf depth)
708 (incf depth)))
709 (= depth 0)))))
711 (defun wesnoth-jump-to-matching (&optional opening)
712 "Jump point to the matching opening/closing tag.
713 OPENING is an opening preprocessor statement to attempt to find a match for."
714 (interactive)
715 (beginning-of-line)
716 (let ((target nil)
717 (first-element nil))
718 (save-excursion
719 (if (or (and (stringp opening)
720 (string-match (wesnoth-element-opening) opening))
721 (looking-at (wesnoth-element-opening)))
722 (progn
723 (setq first-element (match-string-no-properties 0 opening))
724 (when (wesnoth-search-for-matching-tag
725 'search-forward-regexp (wesnoth-element-closing) 'point-max
726 (stringp opening))
727 (beginning-of-line)
728 (if (and (string-match wesnoth-preprocessor-opening-regexp
729 first-element)
730 (looking-at (wesnoth-element-closing)))
731 (when (string= (match-string-no-properties 0)
732 (cdr (assoc first-element
733 '(("#define " . "#enddef")
734 ("#ifndef " . "#endif")
735 ("#ifdef " . "#endif")))))
736 (setq target (point)))
737 (setq target (point)))))
738 (when (looking-at (wesnoth-element-closing))
739 (setq first-element (match-string-no-properties 0))
740 (end-of-line)
741 (when (wesnoth-search-for-matching-tag
742 'search-backward-regexp (wesnoth-element-opening)
743 'wesnoth-wml-start-pos)
744 (if (and (string-match wesnoth-preprocessor-closing-regexp
745 first-element)
746 (looking-at (wesnoth-element-opening)))
747 (progn
748 (when (or (and (string= "#enddef" first-element)
749 (string= "#define "
750 (match-string-no-properties 0)))
751 (and (string= "#endif" first-element)
752 (string-match
753 "#ifn?def "
754 (match-string-no-properties 0))))
755 (setq target (point))))
756 (setq target (point)))))))
757 (if target
758 (goto-char target)
759 (when (interactive-p)
760 (error "Tag does not appear to be matched"))))
761 (back-to-indentation))
763 ;;; Indentation
764 (defun wesnoth-wml-start-pos ()
765 "Determine the position of `point' relative to where the actual WML begins.
766 Return the likely starting position of the WML if it is found.
767 Otherwise return nil."
768 (save-excursion
769 (goto-char (point-min))
770 (when (search-forward-regexp (wesnoth-element) (point-max) t)
771 (beginning-of-line)
772 (point))))
774 (defun first-column-indent-p (point)
775 "Return non-nil if the current line should not be indented.
777 POINT is the position in the buffer to check.
778 CONTEXT represents the type of element which precedes the current element."
779 (or (not (wesnoth-wml-start-pos))
780 (<= (point) (wesnoth-wml-start-pos))
781 (nth 3 (parse-partial-sexp (point-min) point))
782 (looking-at wesnoth-preprocessor-regexp)))
784 (defun wesnoth-indent ()
785 "Indent the current line as WML."
786 (beginning-of-line)
787 (let ((cur-indent 0))
788 (unless (first-column-indent-p (point))
789 (multiple-value-bind (context ref-indent)
790 (wesnoth-determine-context (point))
791 (cond
792 ((eq context 'opening)
793 (if (or (and wesnoth-indent-savefile
794 (not (looking-at (wesnoth-element-closing t))))
795 (looking-at (wesnoth-element-opening t)))
796 (setq cur-indent (+ ref-indent wesnoth-base-indent))
797 (setq cur-indent ref-indent)))
798 ((eq context 'closing)
799 (if (or (looking-at "^[\t ]*\\[/")
800 (and (not wesnoth-indent-savefile)
801 (not (looking-at (wesnoth-element-opening t)))))
802 (setq cur-indent (- ref-indent wesnoth-base-indent))
803 (setq cur-indent ref-indent))))))
804 (indent-line-to (max cur-indent 0))))
806 (defun wesnoth-within-define (position)
807 "Determine whether point is currently inside a #define block.
808 POSITION is the initial cursor position."
809 (save-match-data
810 (let ((depth 0))
811 (dolist (element (or wesnoth-define-blocks
812 (wesnoth-find-macro-definitions)))
813 (when (= (cadr (sort (append (mapcar 'marker-position (cadr element))
814 (list position)) '>)) position)
815 (setq depth (max (car element) depth))))
816 depth)))
818 (defun wesnoth-find-macro-definitions ()
819 "Return information regarding positioning of macro definitions."
820 (save-excursion
821 (goto-char (point-min))
822 (let ((depth 0)
823 openings cache)
824 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t)
825 (and (string= (match-string 1) "#define") (beginning-of-line))
826 (setq depth
827 (if (string= (match-string 1) "#define")
828 (progn
829 (add-to-list 'openings (point-marker))
830 (1+ depth))
831 (if openings
832 (progn
833 (add-to-list 'cache
834 (list depth (list (car openings)
835 (point-marker))))
836 (setq openings (cdr openings))
837 (1- depth))
838 depth)))
839 (end-of-line))
840 cache)))
842 (defun wesnoth-indent-region (start end)
843 "Indent the region from START to END.
845 Creates and destroys a cache of macro definition details as necessary."
846 (interactive "r")
847 (unwind-protect
848 (save-excursion
849 (goto-char end)
850 (setq end (point-marker))
851 (goto-char start)
852 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
853 (or (bolp) (forward-line 1))
854 (while (< (point) end)
855 (if (looking-at "^[\t ]*$")
856 (indent-line-to 0)
857 (funcall indent-line-function))
858 (forward-line 1)))
859 (setq wesnoth-define-blocks nil)))
861 (defun wesnoth-determine-context (position)
862 "Determine the type of the last relevant element.
864 POSITION is the buffer position of the element for which to
865 determine the context."
866 (save-excursion
867 (search-backward-regexp (wesnoth-element t)
868 (wesnoth-wml-start-pos) t)
869 (let ((match (or (match-string 1) ""))
870 (depth (wesnoth-within-define position)))
871 (while (and (> (wesnoth-within-define (point)) depth)
872 (not (= (point) (wesnoth-wml-start-pos))))
873 (search-backward-regexp (wesnoth-element t)
874 (wesnoth-wml-start-pos) t)
875 (setq match (match-string 1)))
876 (when (and (= (point) (wesnoth-wml-start-pos)) (= depth 0)
877 (string-match "#define" match))
878 ;; Found nothing of use; reset match and assume top-level tag.
879 (setq match ""))
880 (cond
881 ((string-match "\\[/\\|#enddef" match)
882 (values 'closing (current-indentation)))
883 ((string-match "\\[[^/]?\\|#define" match)
884 (values 'opening (current-indentation)))))))
886 (defun wesnoth-newline-and-indent (&optional indent)
887 "Indent both the current line and the newline created.
888 If `wesnoth-auto-indent-flag' is nil, indentation will not be
889 performed.
891 If the optional argument, INDENT is non-nil, force indentation to
892 be performed."
893 (interactive)
894 (wesnoth-newline)
895 (when (or wesnoth-auto-indent-flag indent)
896 (wesnoth-indent)))
898 ;;; WML checks
899 (defun wesnoth-check-element-type (position last-tag)
900 "Determine the context of the element.
901 POSITION is the position of the element in the list.
902 LAST-TAG is the parent element."
903 (if (or (not last-tag)
904 (save-match-data
905 (string-match "#\\(?:define\\|ifn?def\\)" last-tag)))
906 (member (match-string-no-properties 1)
907 (mapcar 'car wesnoth-tag-data))
908 (member (match-string-no-properties 1)
909 (nth position (gethash last-tag
910 wesnoth-tag-hash-table)))))
912 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
913 (defun wesnoth-line-number-at-pos (&optional pos)
914 "Return (narrowed) buffer line number at position POS.
915 If POS is nil, use current buffer location.
916 Counting starts at (point-min), so the value refers
917 to the contents of the accessible portion of the buffer."
918 (let ((opoint (or pos (point))) start)
919 (save-excursion
920 (goto-char (point-min))
921 (setq start (point))
922 (goto-char opoint)
923 (forward-line 0)
924 (1+ (count-lines start (point))))))
926 (defun wesnoth-check-output (buffer format-string &rest args)
927 "Output the string as passed to `format'.
928 BUFFER is the buffer to output the result.
929 FORMAT-STRING is the string as the first argument of `format'.
930 ARGS is any additional data required by `format' to handle FORMAT-STRING."
931 (save-excursion
932 (let ((lnap (wesnoth-line-number-at-pos)))
933 (set-buffer buffer)
934 (insert (apply 'format (concat "Line %d: " format-string "\n")
935 lnap args)))))
937 (defun wesnoth-extract-macro-details (macro-arguments)
938 "Return a list of all macros in MACRO-ARGUMENTS."
939 (when macro-arguments
940 (let ((results '()))
941 (save-match-data
942 (dolist (macro (split-string macro-arguments "[{}][\t ]*" t))
943 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
944 macro)
945 (add-to-list 'results (match-string-no-properties 1 macro)))))
946 results)))
948 (defun wesnoth-check-wml ()
949 "Perform context-sensitive analysis of WML-code."
950 (interactive)
951 (wesnoth-update-project-information)
952 (when (= 0 (hash-table-count wesnoth-tag-hash-table))
953 (error "WML data not available; can not generate report"))
954 (let ((unmatched '())
955 (outbuf (get-buffer-create "*WML*")))
956 (save-excursion
957 (let ((buffer (buffer-name)))
958 (set-buffer outbuf)
959 (erase-buffer)
960 (insert (format "Checking %s...\n" buffer))
961 (message (format "Checking %s..." buffer))))
962 (save-excursion
963 (goto-char (or (wesnoth-wml-start-pos) (point-min)))
964 (while (search-forward-regexp "[\t ]*{\\(\\(\\(\\w\\|_\\)+\\)[^=
965 ]*\\)}" (point-max) t)
966 (dolist (macro (wesnoth-extract-macro-details
967 (match-string-no-properties 1)))
968 (unless (assoc macro
969 (append (wesnoth-macro-arguments)
970 wesnoth-local-macro-data
971 wesnoth-macro-data))
972 (wesnoth-check-output outbuf "Unknown macro definition: '{%s}'"
973 macro))))
974 (goto-char (or (wesnoth-wml-start-pos) (point-min)))
975 (while (search-forward-regexp
976 ;; Match tags, preprocessor statements and attributes.
977 (concat "^[\t ]*\\(\\[[+/]?\\([a-z]\\(\\w\\|_\\)+\\)\\]\\|"
978 "\\(\\w\\|_\\)+=\\|"
979 wesnoth-preprocessor-regexp "\\)")
980 (point-max) t)
981 (save-excursion
982 (goto-char (match-beginning 1))
983 (cond ((nth 3 (parse-partial-sexp (point-min) (point)))
984 nil)
985 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
986 (unless (wesnoth-check-element-type 0 (car unmatched))
987 (wesnoth-check-output outbuf
988 "Tag not available in this context: '%s'"
989 (match-string-no-properties 1)))
990 (setq unmatched (cons (match-string-no-properties 1)
991 unmatched)))
992 ((looking-at
993 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
994 "\\( \\(\\w\\|_\\)+\\)*"))
995 (unless (match-string-no-properties 2)
996 (wesnoth-check-output
997 outbuf (concat "Preprocessor statement has no argument: "
998 (match-string-no-properties 1))))
999 (unless (string= (match-string-no-properties 1) "#undef")
1000 (setq unmatched (cons (match-string-no-properties 1)
1001 unmatched))))
1002 ((looking-at wesnoth-preprocessor-closing-regexp)
1003 (when (and unmatched
1004 (not (string-match
1005 (cdr (assoc (match-string-no-properties 1)
1006 '(("enddef" . "#define")
1007 ("endif" . "#ifn?def"))))
1008 (car unmatched))))
1009 (wesnoth-check-output
1010 outbuf
1011 "Preprocessor statement does not nest correctly"))
1012 (setq unmatched (cdr unmatched)))
1013 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)=\\(.+\\)?")
1014 (unless (wesnoth-check-element-type 1 (car unmatched))
1015 (wesnoth-check-output
1016 outbuf "Attribute not available in this context: '%s'"
1017 (match-string-no-properties 1)))
1018 (unless (match-string 3)
1019 (wesnoth-check-output
1020 outbuf "Attribute has no value")))
1021 ((looking-at "[\t ]*#else")
1022 (unless (string-match "ifn?def" (car unmatched))
1023 (if (string= (car unmatched) "#define")
1024 (wesnoth-check-output outbuf "Expecting: '%s'"
1025 (car unmatched))
1026 (wesnoth-check-output outbuf "Expecting: '[/%s]'"
1027 (car unmatched)))))
1028 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1029 (when (and unmatched
1030 (not (string= (match-string-no-properties 1)
1031 (car unmatched))))
1032 (wesnoth-check-output outbuf "Expecting: '[/%s]'"
1033 (car unmatched)))
1034 (setq unmatched (cdr unmatched))))))
1035 (if unmatched
1036 (dolist (tag unmatched)
1037 (wesnoth-check-output outbuf "Unmatched element: '%s'"
1038 (car unmatched)))))
1039 (save-excursion
1040 (let ((buffer (buffer-name)))
1041 (set-buffer outbuf)
1042 (display-buffer outbuf t)
1043 (let ((warnings (- (wesnoth-line-number-at-pos
1044 (save-excursion (goto-char (point-max)))) 2)))
1045 (insert (format (concat "\nCheck complete. %d warning"
1046 (if (= warnings 1) "." "s.")) warnings)))
1047 (message (format "Checking %s...done" buffer))))))
1049 (defmacro wesnoth-element-requires (element requirement &optional pop)
1050 "Process requirements for corresponding preprocessor elements.
1051 ELEMENT is the current element being tested.
1052 REQUIREMENT is the element required to exist for correct nesting.
1053 POP is an optional argument indicating the element should be
1054 removed from the list of unmatched elements."
1055 `(when (string= ,element (match-string-no-properties 1))
1057 (if (string-match ,requirement (car unmatched))
1058 (progn
1059 (and ,pop (setq unmatched (cdr unmatched)))
1061 (setq error-position (point)))))
1063 (defmacro wesnoth-structure-result (position element)
1064 "Process results of the structure check.
1065 POSITION is the error position or nil, if no error was found.
1066 ELEMENT is the last unmatched element, or nil if all opening
1067 elements have been matched."
1068 `(let ((expected nil))
1069 (when ,element
1070 (cond ((string= ,element "define ") (setq expected "#enddef"))
1071 ((string-match "ifn?def " ,element) (setq expected "#endif"))))
1072 (if (interactive-p)
1073 (if (or ,element ,position)
1074 (progn
1075 (and ,position (goto-char ,position))
1076 (message "Error: Expecting %s" (or expected
1077 (concat "[/" ,element "]"))))
1078 (message "%s" "Structure appears consistent."))
1079 (when (or expected ,element)
1080 (or expected (concat "[/" ,element "]"))))))
1082 (defun wesnoth-check-structure (&optional start end)
1083 "Check the buffer for correct nesting of elements.
1084 If a problem is found in the structure, point will be placed at
1085 the location which an element was expected and the expected
1086 element will be displayed in the mini-buffer.
1088 START and END define the region to be checked. If
1089 function `transient-mark-mode' is enabled, the region specified will be
1090 checked. Otherwise START and END will be the minimum and maximum
1091 positions of the buffer, respectively."
1092 (interactive)
1093 (unless (or start end)
1094 (if (and (boundp 'transient-mark-mode)
1095 transient-mark-mode mark-active)
1096 (setq start (region-beginning)
1097 end (copy-marker (region-end)))
1098 (setq start (point-min)
1099 end (point-max))))
1100 (let ((unmatched '())
1101 (error-position nil))
1102 (save-excursion
1103 (and start (goto-char start))
1104 (while (and (search-forward-regexp
1105 (concat "^\\([\t ]*\\[\\(/?\\(\\w\\|_\\)+\\)\\]\\|"
1106 wesnoth-preprocessor-regexp "\\)") end t)
1107 (not error-position))
1108 (beginning-of-line)
1109 (if (or (looking-at "^[\t ]*\\[\\(\\(\\w\\|_\\)+\\)\\]")
1110 (looking-at "[\t ]*#\\(define \\|ifdef \\|ifndef \\)"))
1111 (setq unmatched (cons (match-string-no-properties 1)
1112 unmatched))
1113 (cond
1114 ((wesnoth-element-requires "#else" "ifn?def "))
1115 ((wesnoth-element-requires "#endif" "ifn?def " t))
1116 ((wesnoth-element-requires "#enddef" "define " t))
1117 ((looking-at (concat "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|"
1118 wesnoth-preprocessor-closing-regexp))
1119 (if (string= (match-string-no-properties 1)
1120 (car unmatched))
1121 (setq unmatched (cdr unmatched))
1122 (setq error-position (point))))))
1123 (end-of-line)))
1124 (wesnoth-structure-result error-position (car unmatched))))
1126 ;;; wesnoth-mode
1127 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
1128 "Major mode for editing WML."
1129 (kill-all-local-variables)
1130 (use-local-map wesnoth-mode-map)
1131 (setq major-mode 'wesnoth-mode)
1132 (setq mode-name "WML")
1133 (set-syntax-table wesnoth-syntax-table)
1134 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
1135 (set (make-local-variable 'comment-start) "#")
1136 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
1137 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
1138 (set (make-local-variable 'font-lock-defaults)
1139 '(wesnoth-font-lock-keywords
1140 nil t nil nil
1141 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
1142 (setq indent-tabs-mode nil)
1143 (easy-menu-add wesnoth-menu wesnoth-mode-map)
1144 (wesnoth-create-wml-hash-table)
1145 (wesnoth-update-project-information)
1146 (run-hooks 'wesnoth-mode-hook))
1148 (provide 'wesnoth-mode)
1150 ;;; wesnoth-mode.el ends here