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