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