* (wesnoth-indent-line-default, wesnoth-indent-line-savefile): renamed.
[wesnoth-mode.git] / wesnoth-mode.el
blobac48f3d8b9dec7c3b399b3f2a86b95359cac55d1
1 ;; wesnoth-mode.el - A major mode for editing WML.
2 ;; Copyright (C) 2006, 2007 Chris Mann
4 ;; This program is free software; you can redistribute it and/or
5 ;; modify it under the terms of the GNU General Public License as
6 ;; published by the Free Software Foundation; either version 2 of the
7 ;; License, or (at your option) any later version.
9 ;; This program is distributed in the hope that it will be useful, but
10 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;; General Public License for more details.
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with this program; see the file COPYING. If not, write to the
16 ;; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17 ;; MA 02139, USA.
19 ;;; Description:
20 ;; wesnoth-mode is a major mode for Emacs which assists in the editing
21 ;; of Wesnoth Markup Language (WML) files. Currently, this mode
22 ;; features syntax highlighting support, automatic indentation,
23 ;; tag-completion and preliminary support for syntax checking.
25 ;;; Usage:
26 ;; Add the following to your .emacs:
27 ;; (add-to-list 'load-path "path/to/wesnoth-mode")
28 ;; (autoload 'wesnoth-mode "wesnoth-mode" "Major mode for editing WML." t)
29 ;; Optionally adding:
30 ;; (add-to-list 'auto-mode-alist '("\\.cfg\\'" . wesnoth-mode))
31 ;; to automatically load wesnoth-mode for all files ending in '.cfg'.
33 ;;; Changes:
34 ;; 1.2.2
35 ;; * Fixed #endif not highlighting correctly.
36 ;; * Improvements to jump-to-matching.
37 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
38 ;; * Performing indentation now leaves point at the first non-whitespace
39 ;; character on the line.
40 ;; * Preprocessor statements are now checked for correct nesting when
41 ;; performing `wesnoth-check-structure'.
42 ;; * `wesnoth-check-structure' can now be applied over an active region.
43 ;; * `wesnoth-insert-missing-closing' new function to insert the next
44 ;; expected closing element. (Bound to C-c C-/.)
45 ;; * Changed `wesnoth-newline' to behave more consistantly.
46 ;; * Fixed automatic indentation ignoring the state of
47 ;; `wesnoth-auto-indent-flag' in `wesnoth-newline-and-indent'.
48 ;; * Renamed `wesnoth-indent-line-default' and `wesnoth-indent-line-savefile'
49 ;; to `wesnoth-indent-default' and `wesnoth-indent-savefile', respectively.
50 ;; Set default value for `wesnoth-indentation-function' accordingly.
51 ;; 1.2.1
52 ;; * Base indent now defaults to 4.
53 ;; * Added support for #ifndef.
55 (defconst wesnoth-mode-version "1.2.2"
56 "The current version of wesnoth-mode.")
58 (defgroup wesnoth-mode nil "Wesnoth-mode access"
59 :group 'languages
60 :prefix "wesnoth-")
62 (defcustom wesnoth-indentation-function 'wesnoth-indent-default
63 "Use the specified function when indenting WML.
64 You can specify either `wesnoth-indent-default' or
65 `wesnoth-indent-savefile' as the indentation style or a
66 customised function for indentation."
67 :type 'function
68 :group 'wesnoth-mode)
70 (defcustom wesnoth-auto-indent-flag t
71 "Whether to attempt tag indentation when a newline is created.
72 If nil, no indentation will be attempted. Otherwise, attempt to
73 indent the line."
74 :type 'boolean
75 :group 'wesnoth-mode)
77 (defcustom wesnoth-base-indent 4
78 "The number of columns to indent WML."
79 :type 'integer
80 :group 'wesnoth-mode)
82 (defconst wesnoth-preprocessor-regexp
83 "#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)"
84 "Regular expression to match all preprocessor statements.")
86 (defconst wesnoth-preprocessor-opening-regexp
87 "#\\(?:define \\|else\\|ifdef \\|ifndef \\)"
88 "Regular expression to match \"opening\" preprocessor statements.")
90 (defconst wesnoth-preprocessor-closing-regexp
91 "#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)"
92 "Regular expression to match \"closing\" preprocessor statements.")
94 (defvar wesnoth-mode-hook nil)
96 (defvar wesnoth-mode-map ()
97 "Keymap used in wesnoth mode.")
98 (unless wesnoth-mode-map
99 (setq wesnoth-mode-map (make-sparse-keymap))
100 (define-key wesnoth-mode-map "\C-\M-a" 'wesnoth-jump-backward)
101 (define-key wesnoth-mode-map "\C-\M-e" 'wesnoth-jump-forward)
102 (define-key wesnoth-mode-map "\C-c\C-m" 'wesnoth-jump-to-matching)
103 (define-key wesnoth-mode-map "\C-cm" 'wesnoth-jump-to-matching)
104 (define-key wesnoth-mode-map "\C-m" 'wesnoth-newline)
105 (define-key wesnoth-mode-map "\C-j" 'wesnoth-newline-and-indent)
106 (define-key wesnoth-mode-map "\C-c\C-c" 'wesnoth-check-structure)
107 (define-key wesnoth-mode-map "\C-cc" 'wesnoth-check-structure)
108 (define-key wesnoth-mode-map "\C-c\C-n" 'wesnoth-check-tag-names)
109 (define-key wesnoth-mode-map "\C-cn" 'wesnoth-check-tag-names)
110 (define-key wesnoth-mode-map "\C-c\C-e" 'wesnoth-insert-tag)
111 (define-key wesnoth-mode-map "\C-ce" 'wesnoth-insert-tag)
112 (define-key wesnoth-mode-map (kbd "C-c C-/") 'wesnoth-insert-missing-closing)
113 (define-key wesnoth-mode-map (kbd "C-c /") 'wesnoth-insert-missing-closing))
115 (defvar wesnoth-syntax-table
116 (let ((wesnoth-syntax-table (make-syntax-table)))
117 (modify-syntax-entry ?= "." wesnoth-syntax-table)
118 (modify-syntax-entry ?\_ "w" wesnoth-syntax-table)
119 (modify-syntax-entry ?- "_" wesnoth-syntax-table)
120 (modify-syntax-entry ?. "_" wesnoth-syntax-table)
121 (modify-syntax-entry ?\n ">" wesnoth-syntax-table)
122 (modify-syntax-entry ?\r ">" wesnoth-syntax-table)
123 wesnoth-syntax-table)
124 "Syntax table for wesnoth-mode.")
126 ;; Prevents automatic syntax-highlighting of elements which might be
127 ;; pre-processor statements.
128 (defvar wesnoth-syntactic-keywords
129 (list
130 '("^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)" 1 "w")
131 '("\\(#[\t ]*.*$\\)" 1 "<"))
132 "Highlighting syntactic keywords within wesnoth-mode.")
134 (defvar wesnoth-font-lock-keywords
135 (list
136 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)"
137 1 font-lock-preprocessor-face)
138 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\w+\\)"
139 2 font-lock-function-name-face)
140 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" . font-lock-preprocessor-face)
141 '("[\t ]*\\({[[:word:]/\.]+\\|{[@~][[:word:]/\.]+\\).*\\(}\\)"
142 (1 font-lock-function-name-face)
143 (2 font-lock-function-name-face))
144 '("\\[[^]]+\\]" . font-lock-type-face)
145 '("\\$\\w+" . font-lock-variable-name-face)
146 '("\\(\\w+\\(\\,[\t ]*\\w+\\)*\\)="
147 1 font-lock-variable-name-face))
148 "Syntax highlighting for wesnoth-mode.")
150 (defvar wesnoth-tags-list
151 (list
152 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
153 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
154 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
155 "command" "deaths" "defend" "defends" "defense" "delay" "destination"
156 "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel" "era" "event"
157 "expenses" "filter" "filter_radius" "filter_second" "format" "frame"
158 "game_config" "generator" "gold" "have_unit" "header" "hide_unit" "if"
159 "illuminated_time" "image" "img" "income" "italic" "item" "jump" "kill"
160 "killed" "label" "language" "leader_goal" "main_map" "menu" "message"
161 "mini_map" "missile_frame" "modifications" "modify_side" "modify_turns"
162 "move" "move_unit_fake" "movement_costs" "movetype" "multiplayer"
163 "multiplayer_side" "music" "not" "num_units" "object" "objectives"
164 "objective" "observers" "option" "or" "panel" "part" "place_shroud"
165 "position" "print" "protect_location" "protect_unit" "race" "random"
166 "recall" "recalls" "recruit" "recruits" "redraw" "ref" "remove_shroud"
167 "remove_unit_overlay" "removeitem" "replay" "replay_start" "resistance"
168 "resolution" "results" "role" "save" "scenario" "scroll" "scroll_to"
169 "scroll_to_unit" "section" "set_recruit" "set_variable" "side"
170 "side_playing" "snapshot" "sound" "source" "specials" "statistics" "status"
171 "stone" "store_gold" "store_locations" "store_starting_location"
172 "store_unit" "story" "target" "team" "teleport" "teleport_anim" "terrain"
173 "terrain_graphics" "test" "textdomain" "theme" "then" "tile" "time"
174 "time_area" "time_of_day" "topic" "toplevel" "trait" "turn" "tutorial"
175 "unhide_unit" "unit" "unit_abilities" "unit_alignment" "unit_description"
176 "unit_hp" "unit_image" "unit_level" "unit_moves" "unit_overlay"
177 "unit_profile" "unit_status" "unit_traits" "unit_type" "unit_weapons"
178 "unit_xp" "units" "unstone" "unstore_unit" "upkeep" "variable" "variables"
179 "village" "villages" "while")
180 "A list containing all tags which are available for use in WML.")
182 (defun wesnoth-insert-tag ()
183 "Inserts the specified opening tag and it's matching closing tag.
184 Both the opening and closing tags will be placed on their own
185 lines with point positioned between them. Completion of tags at
186 the prompt uses `wesnoth-tags-list'."
187 (interactive)
188 (let ((tagname
189 (completing-read
190 "Tag: "
191 wesnoth-tags-list
192 nil nil)))
193 (progn
194 (beginning-of-line)
195 (unless (looking-at "^[\t ]*$")
196 (end-of-line)
197 (insert "\n"))
198 (end-of-line)
199 (insert "[" tagname "]")
200 (wesnoth-indent-line)
201 (insert "\n")
202 (wesnoth-indent-line)
203 (save-excursion
204 (insert "\n[/" tagname "]")
205 (wesnoth-indent-line)
206 (forward-line -1)))))
208 (defun wesnoth-jump-forward (repeat)
209 "Move point to the end of the next tag.
210 REPEAT is an optional numeric argument. If REPEAT is non-nil,
211 jump forward the specified number of tags."
212 (interactive "p")
213 (or repeat (setq repeat 1))
214 (and (< repeat 0) (wesnoth-jump-backward (abs repeat)))
215 (let ((iterations 0))
216 (while (< iterations repeat)
217 (end-of-line)
218 (search-forward-regexp
219 (concat "^[\t ]*\\(\\[\\w+\\]\\|"
220 wesnoth-preprocessor-opening-regexp "\\)")
221 (buffer-size) t)
222 (setq iterations (1+ iterations)))))
224 (defun wesnoth-jump-backward (repeat)
225 "Move point to the beginning of the previous tag.
226 REPEAT is an optional numeric argument. If REPEAT is non-nil,
227 jump backward the specified number of tags."
228 (interactive "p")
229 (or repeat (setq repeat 1))
230 (and (< repeat 0) (wesnoth-jump-forward (abs repeat)))
231 (let ((iterations 0))
232 (while (< iterations repeat)
233 (beginning-of-line)
234 (search-backward-regexp
235 (concat "^[\t ]*\\(\\[\\w+\\]\\|"
236 wesnoth-preprocessor-opening-regexp "\\)")
237 0 t)
238 (unless (bobp)
239 (search-forward-regexp "[^[:blank:]]")
240 (backward-char))
241 (setq iterations (1+ iterations)))))
243 (defun wesnoth-jump-to-matching ()
244 "Jump point to the matching opening/closing tag.
245 A tag must be on the same line as point for jumping to occur. If
246 the tag structure is not correct this may have unexpected
247 results."
248 (interactive)
249 (let ((open-tags 0)
250 (search-started nil)
251 (tag-position nil)
252 (search-backward nil))
253 (save-excursion
254 (beginning-of-line)
255 (if (looking-at
256 (concat "^[\t ]*\\(\\[\\|"
257 wesnoth-preprocessor-regexp "\\)"))
258 (when (looking-at
259 (concat "^[\t ]*\\(\\[/\\|#\\(?:endif\\|enddef\\)\\)"))
260 (setq search-backward t))
261 (if (wesnoth-wml-start-pos)
262 (if (> (point) (wesnoth-wml-start-pos))
263 (search-backward-regexp
264 (concat "^[\t ]*\\(\\[\\|"
265 wesnoth-preprocessor-regexp "\\)")
266 (point-min) t)
267 (goto-char (point-min))
268 (search-forward-regexp
269 (concat "^[\t ]*\\(\\[\\|"
270 wesnoth-preprocessor-regexp "\\)"))
271 (beginning-of-line))
272 (error "%s" "Unable to locate tag to jump from")))
273 (if search-backward
274 (progn
275 (end-of-line)
276 (while (and
277 (or (< open-tags 0) (not search-started))
278 (search-backward-regexp
279 (concat "^[\t ]*\\(\\[\\|"
280 wesnoth-preprocessor-regexp "\\)")
281 (point-min) t))
282 (setq search-started t)
283 (if (looking-at
284 "^[\t ]*\\(\\[\\+?\\w+\\]\\|#\\(?:define\\|ifn?def\\) \\)")
285 (setq open-tags (1+ open-tags))
286 (when (looking-at
287 (concat "^[\t ]*\\(\\[/\\w+\\]\\|#\\(?:endif\\|enddef\\)\\)"))
288 (setq open-tags (1- open-tags))))))
289 (while (and
290 (or (> open-tags 0) (not search-started))
291 (search-forward-regexp
292 (concat "^[\t ]*\\(\\[\\|"
293 wesnoth-preprocessor-regexp "\\)")
294 (point-max) t))
295 (beginning-of-line)
296 (setq search-started t)
297 (if (looking-at
298 "^[\t ]*\\(\\[\\+?\\w+\\]\\|#\\(?:define\\|ifn?def\\) \\)")
299 (setq open-tags (1+ open-tags))
300 (when (looking-at
301 (concat "^[\t ]*\\(\\[\\+?/\\w+\\]\\|#\\(?:endif\\|enddef\\)\\)"))
302 (setq open-tags (1- open-tags))))
303 (end-of-line)))
304 (setq tag-position (point)))
305 (if (interactive-p)
306 (goto-char tag-position)
307 tag-position))
308 (end-of-line)
309 (search-backward-regexp "\\[\\|#"))
311 (defun wesnoth-wml-start-pos ()
312 "Determine the position of `point' relative to where the actual WML begins.
313 Return the likely starting position of the WML if it is found.
314 Otherwise return nil."
315 (save-excursion
316 (goto-char (point-min))
317 (when (search-forward-regexp
318 (concat "^[\t ]*\\(\\[\\)\\|\\("
319 wesnoth-preprocessor-opening-regexp
320 "\\)")
321 (buffer-size) t)
322 (beginning-of-line)
323 (point))))
325 (defun wesnoth-indent-default ()
326 "Indent the current line as WML using normal-style indentation."
327 (beginning-of-line)
328 (if (or (not (wesnoth-wml-start-pos))
329 (<= (point) (wesnoth-wml-start-pos))
330 (nth 3 (syntax-ppss (point))))
331 (indent-line-to 0)
332 (let ((not-indented t) cur-indent)
333 (if (looking-at
334 (concat "^[ \t]*\\(\\[\\/[^]]*?\\|\\("
335 wesnoth-preprocessor-closing-regexp "\\)\\)"))
336 (progn
337 (save-excursion
338 (search-backward-regexp
339 (concat "^[\t ]*\\[\\|\\(" wesnoth-preprocessor-regexp "\\)"))
340 (setq cur-indent (current-indentation))
341 (beginning-of-line)
342 (when (looking-at
343 (concat "^[\t ]*\\(\\[/.+\\]\\|\\("
344 wesnoth-preprocessor-closing-regexp
345 "\\)\\)"))
346 (setq cur-indent (- (current-indentation) wesnoth-base-indent))))
347 (if (< cur-indent 0)
348 (setq cur-indent 0)))
349 (if (not (looking-at
350 (concat "^[ \t]*\\(\\[[^/]*?\\]\\|\\("
351 wesnoth-preprocessor-closing-regexp "\\)\\)")))
352 (save-excursion
353 (search-backward-regexp
354 (concat "^[\t ]*\\(\\[\\|\\("
355 wesnoth-preprocessor-regexp "\\)\\)"))
356 (if (looking-at
357 (concat "^[\t ]*\\(\\[/.+\\]\\|\\("
358 wesnoth-preprocessor-closing-regexp
359 "\\)\\)"))
360 (progn
361 (setq cur-indent (- (current-indentation) wesnoth-base-indent))
362 (if (< cur-indent 0)
363 (setq cur-indent 0))
364 (setq not-indented nil))
365 (setq cur-indent (current-indentation))
366 (setq not-indented nil)))
367 (save-excursion
368 (unless cur-indent
369 (search-backward-regexp
370 (concat "^[\t ]*\\([[}]\\|\\("
371 wesnoth-preprocessor-regexp
372 "\\)\\)"))
373 (if (looking-at
374 (concat "^[ \t]*\\(\\[[^/]*?\\]\\|\\("
375 wesnoth-preprocessor-opening-regexp "\\)\\)"))
376 (setq cur-indent (+ (current-indentation) wesnoth-base-indent))
377 (setq cur-indent (current-indentation)))))))
378 (unless (and (not cur-indent) (= (current-indentation) cur-indent))
379 (indent-line-to cur-indent))))
380 (beginning-of-line)
381 (search-forward-regexp "[\t ]*"))
383 (defun wesnoth-indent-savefile ()
384 "Indent the current line as WML code using savefile-style indentation."
385 (beginning-of-line)
386 (if (or (not (wesnoth-wml-start-pos))
387 (<= (point) (wesnoth-wml-start-pos))
388 (nth 3 (syntax-ppss (point))))
389 (indent-line-to 0)
390 (let ((cur-indent))
391 (if (looking-at
392 (concat "^[ \t]*\\(\\[\\/[^]]*?\\|\\("
393 wesnoth-preprocessor-closing-regexp "\\)\\)"))
394 (progn
395 (save-excursion
396 (search-backward-regexp "^[ \t]+.\\|^[{[#]")
397 (setq cur-indent (- (current-indentation) wesnoth-base-indent))
398 (when (looking-at
399 (concat "^[ \t]*\\(\\[[^/].+\\]\\|\\("
400 wesnoth-preprocessor-opening-regexp "\\)\\)"))
401 (setq cur-indent (current-indentation))))
402 (if (< cur-indent 0)
403 (setq cur-indent 0)))
404 (save-excursion
405 (unless cur-indent
406 (search-backward-regexp "^[\t ]*\\([[#}]\\)")
407 (if (looking-at
408 (concat "^[ \t]*\\(\\[[^/]+?\\]\\|\\("
409 wesnoth-preprocessor-opening-regexp "\\)\\)"))
410 (setq cur-indent (+ (current-indentation) wesnoth-base-indent))
411 (setq cur-indent (current-indentation))))))
412 (unless (and (not cur-indent)
413 (= (current-indentation) cur-indent))
414 (indent-line-to cur-indent))))
415 (beginning-of-line)
416 (search-forward-regexp "[\t ]*"))
418 (defun wesnoth-newline ()
419 "Indent both the current line and the newline created.
420 If `wesnoth-auto-indent-flag' is nil, indentation will not be
421 performed."
422 (interactive)
423 (save-excursion
424 (when wesnoth-auto-indent-flag
425 (wesnoth-indent-line)))
426 (newline))
428 (defun wesnoth-newline-and-indent ()
429 "Indent both the current line and the newline created.
430 If `wesnoth-auto-indent-flag' is nil, indentation will not be
431 performed."
432 (interactive)
433 (wesnoth-newline)
434 (when wesnoth-auto-indent-flag
435 (wesnoth-indent-line)))
437 (defun wesnoth-check-tag-names ()
438 "Check the names of all tags in the buffer for correctness.
439 If a tag is found which is not present in the list an error will
440 be signalled and point will be moved to the corresponding
441 position."
442 (interactive)
443 (let ((tag-position nil)
444 (missing-tag-name nil))
445 (save-excursion
446 (goto-char (point-min))
447 (while (and
448 (search-forward-regexp "^[\t ]*\\[" (point-max) t)
449 (not tag-position))
450 (beginning-of-line)
451 (when (looking-at "^[\t ]*\\[[/]?\\(\\w+\\)\\]")
452 (unless (member (match-string-no-properties 1) wesnoth-tags-list)
453 (setq tag-position (point))
454 (setq missing-tag-name (match-string-no-properties 1))))
455 (end-of-line)))
456 (when tag-position
457 (goto-char tag-position)
458 (message "'%s' is not known to exist"
459 missing-tag-name))))
461 (defun wesnoth-check-structure (&optional start end)
462 "Check the buffer for correct nesting of elements.
463 If a problem is found in the structure, point will be placed at
464 the location which an element was expected and the expected
465 element will be displayed in the minibuffer."
466 (interactive)
467 (if (and transient-mark-mode mark-active)
468 (setq start (region-beginning)
469 end (copy-marker (region-end)))
470 (setq start (point-min)
471 end (point-max)))
472 (let ((unmatched-tag-list '())
473 (error-position nil)
474 (expected nil))
475 (save-excursion
476 (goto-char start)
477 (while (and
478 (search-forward-regexp
479 (concat "^[\t ]*\\[\\|\\(" wesnoth-preprocessor-regexp "\\)")
480 end t)
481 (not error-position))
482 (beginning-of-line)
483 (if (looking-at "^[\t ]*#\\(\\w+\\)")
484 (let ((preprocessor-name (match-string-no-properties 1)))
485 (cond
486 ((string= preprocessor-name "define")
487 (setq unmatched-tag-list
488 (cons preprocessor-name unmatched-tag-list)))
489 ((string= preprocessor-name "ifdef")
490 (setq unmatched-tag-list
491 (cons preprocessor-name unmatched-tag-list)))
492 ((string= preprocessor-name "ifndef")
493 (setq unmatched-tag-list
494 (cons preprocessor-name unmatched-tag-list))
495 (message "added a thing to the list...?"))
496 ((string= preprocessor-name "else")
497 (unless (= (string-match "ifn?def" (car unmatched-tag-list)) 0)
498 (setq error-position (point))))
499 ((string= preprocessor-name "endif")
500 (if (= (string-match "ifn?def" (car unmatched-tag-list)) 0)
501 (setq unmatched-tag-list (cdr unmatched-tag-list))
502 (setq error-position (point))))
503 ((string= preprocessor-name "enddef")
504 (if (string= (car unmatched-tag-list) "define")
505 (setq unmatched-tag-list (cdr unmatched-tag-list))
506 (setq error-position (point))))))
507 (if (looking-at "^[\t ]*\\[\\+?\\(\\w+\\)\\]")
508 (setq unmatched-tag-list
509 (cons (match-string-no-properties 1)
510 unmatched-tag-list))
511 (when (looking-at "^[\t ]*\\[/\\(\\w+\\)\\]")
512 (if (string= (match-string-no-properties 1)
513 (car unmatched-tag-list))
514 (setq unmatched-tag-list (cdr unmatched-tag-list))
515 (setq error-position (point))))))
516 (end-of-line)))
517 (when unmatched-tag-list
518 (cond ((string= (car unmatched-tag-list) "define")
519 (setq expected "#enddef"))
520 ((string-match "ifn?def" (car unmatched-tag-list))
521 (setq expected "#endif"))
522 ((not unmatched-tag-list)
523 (setq expected "Unexpected end of file"))))
524 (if (interactive-p)
525 (if (not (or unmatched-tag-list error-position))
526 (message "%s" "Structure appears consistent.")
527 (and error-position
528 (goto-char error-position))
529 (if (string= expected "Unexpected end of file")
530 (message "Error %s" expected)
531 (message "Error: Expecting %s"
533 expected
534 (concat " [/" (car unmatched-tag-list) "]")))))
535 (when (or expected unmatched-tag-list)
536 (or expected (concat "[/" (car unmatched-tag-list) "]"))))))
538 (defun wesnoth-insert-missing-closing ()
539 "Insert the next exected closing element at point."
540 (interactive "r")
541 (let ((element (wesnoth-check-structure start end)))
542 (if element
543 (if (string= element "Unexpected end of file")
544 (message "%s" "Error: Expected end of file")
545 (when (not (looking-at "^[\t ]*$"))
546 (end-of-line)
547 (wesnoth-newline))
548 (insert element)
549 (wesnoth-indent-line)
550 (end-of-line))
551 (error "%s" "Unable to find element to insert"))))
553 (defun wesnoth-indent-line ()
554 "Determine and performs indentation on the current line.
555 The Indentation style can be customised by modifying
556 `wesnoth-indentation-function'."
557 (interactive)
558 (funcall wesnoth-indentation-function))
560 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
561 "Major mode for editing WML."
562 (set-syntax-table wesnoth-syntax-table)
563 (set (make-local-variable 'outline-regexp) "^[\t ]*\\[\\w+")
564 (set (make-local-variable 'comment-start) "#")
565 (set (make-local-variable 'indent-line-function) 'wesnoth-indent-line)
566 (set (make-local-variable 'font-lock-defaults)
567 '(wesnoth-font-lock-keywords
568 nil t nil nil
569 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
570 (setq indent-tabs-mode nil)
571 (setq mode-name "WML")
572 (run-hooks 'wesnoth-mode-hook))
574 (provide 'wesnoth-mode)