1 ;; wesnoth-mode.el - A major mode for editing WML.
2 ;; Copyright (C) 2006, 2007, 2008 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,
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.
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)
30 ;; (add-to-list 'auto-mode-alist '("\\.cfg\\'" . wesnoth-mode))
31 ;; to automatically load wesnoth-mode for all files ending in '.cfg'.
35 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
36 ;; `wesnoth-element', `wesnoth-element-opening',
37 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
38 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
39 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
40 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
41 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
42 ;; * Fixed a bug in indentation where content was needed between elements pairs
43 ;; for indentation to work.
44 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
45 ;; `wesnoth-auto-indent-flag'.
46 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
47 ;; * Added indentation styles: `wesnoth-indent-default',
48 ;; `wesnoth-indent-withtags' which implement a a similar indentation
49 ;; style to the existing styles, however all preprocessor statements are
50 ;; indented to the first column.
51 ;; * Added support for several new tags.
52 ;; * Modified `wesnoth-newline' to behave more consistently.
53 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
54 ;; now leaves point at the beginning (when moving backward) or end (when
55 ;; moving forward) of the match.
56 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
57 ;; will now work on preprocessor statements. Will now warn if jump
58 ;; destination may not be correct (due to errors in WML structure).
59 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
60 ;; * `wesnoth-check-structure' can now be applied over an active region and
61 ;; now checks preprocessor statements for correct nesting.
62 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
63 ;; perform indentation by providing a prefix argument.
64 ;; * Indentation styles now leave point at the first non-whitespace character of
66 ;; * `wesnoth-check-tag-names' now reports on success.
67 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
68 ;; * `outline-minor-mode' now works on macro definitions.
70 ;; * Base indent now defaults to 4.
71 ;; * Added support for #ifndef.
73 (defconst wesnoth-mode-version
"1.2.2"
74 "The current version of wesnoth-mode.")
76 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
80 (defcustom wesnoth-indentation-function
'wesnoth-indent-default
81 "Use the specified function when indenting WML.
82 You can specify `wesnoth-indent-default',
83 `wesnoth-indent-savefile', `wesnoth-indent-default-preproc' or
84 `wesnoth-indent-savefile-preproc' or a custom function as the
89 (defcustom wesnoth-auto-indent-flag t
90 "Whether to attempt tag indentation when a newline is created.
91 If nil, no indentation will be attempted. Otherwise, attempt to
96 (defcustom wesnoth-base-indent
4
97 "The number of columns to indent WML."
101 (defconst wesnoth-preprocessor-regexp
102 "#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)"
103 "Regular expression to match all preprocessor statements.")
105 (defconst wesnoth-preprocessor-opening-regexp
106 "#\\(?:define \\|else\\|ifdef \\|ifndef \\)"
107 "Regular expression to match \"opening\" preprocessor statements.")
109 (defconst wesnoth-preprocessor-closing-regexp
110 "#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)"
111 "Regular expression to match \"closing\" preprocessor statements.")
113 (defvar wesnoth-mode-hook nil
)
115 (defvar wesnoth-mode-map
()
116 "Keymap used in wesnoth mode.")
117 (unless wesnoth-mode-map
118 (setq wesnoth-mode-map
(make-sparse-keymap))
119 (define-key wesnoth-mode-map
"\C-\M-a" 'wesnoth-backward-tag
)
120 (define-key wesnoth-mode-map
"\C-\M-e" 'wesnoth-forward-tag
)
121 (define-key wesnoth-mode-map
"\C-c\C-m" 'wesnoth-jump-to-matching
)
122 (define-key wesnoth-mode-map
"\C-cm" 'wesnoth-jump-to-matching
)
123 (define-key wesnoth-mode-map
"\C-m" 'wesnoth-newline
)
124 (define-key wesnoth-mode-map
"\C-j" 'wesnoth-newline-and-indent
)
125 (define-key wesnoth-mode-map
"\C-c\C-c" 'wesnoth-check-structure
)
126 (define-key wesnoth-mode-map
"\C-cc" 'wesnoth-check-structure
)
127 (define-key wesnoth-mode-map
"\C-c\C-n" 'wesnoth-check-tag-names
)
128 (define-key wesnoth-mode-map
"\C-cn" 'wesnoth-check-tag-names
)
129 (define-key wesnoth-mode-map
"\C-c\C-e" 'wesnoth-insert-tag
)
130 (define-key wesnoth-mode-map
"\C-ce" 'wesnoth-insert-tag
)
131 (define-key wesnoth-mode-map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
132 (define-key wesnoth-mode-map
(kbd "C-c /") 'wesnoth-insert-missing-closing
))
134 (defvar wesnoth-syntax-table
135 (let ((wesnoth-syntax-table (make-syntax-table)))
136 (modify-syntax-entry ?
= "." wesnoth-syntax-table
)
137 (modify-syntax-entry ?\_
"w" wesnoth-syntax-table
)
138 (modify-syntax-entry ?-
"_" wesnoth-syntax-table
)
139 (modify-syntax-entry ?.
"_" wesnoth-syntax-table
)
140 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
141 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
142 wesnoth-syntax-table
)
143 "Syntax table for wesnoth-mode.")
145 ;; Prevents automatic syntax-highlighting of elements which might be
146 ;; pre-processor statements.
147 (defvar wesnoth-syntactic-keywords
149 '("^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)" 1 "w")
150 '("\\(#[\t ]*.*$\\)" 1 "<"))
151 "Highlighting syntactic keywords within wesnoth-mode.")
153 (defvar wesnoth-font-lock-keywords
155 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)"
156 1 font-lock-preprocessor-face
)
157 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\w+\\)"
158 2 font-lock-function-name-face
)
159 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" . font-lock-preprocessor-face
)
160 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)"
161 (1 font-lock-function-name-face
))
162 '("\\({\\w+\\|{[~@]?\\)"
163 (1 font-lock-function-name-face
))
164 ;; Separated from above to support multi-line macro calls.
165 '("}" . font-lock-function-name-face
)
166 '("\\[[^]]+\\]" . font-lock-type-face
)
167 '("\\$\\w+" . font-lock-variable-name-face
)
168 '("\\(\\w+\\(\\,[\t ]*\\w+\\)*\\)="
169 1 font-lock-variable-name-face
))
170 "Syntax highlighting for wesnoth-mode.")
172 (defvar wesnoth-tags-list
174 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
175 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
176 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
177 "command" "deaths" "defend" "defends" "defense" "delay" "destination"
178 "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel" "entry" "era"
179 "event" "expenses" "filter" "filter_adjacent_location" "filter_radius"
180 "filter_second" "format" "frame" "game_config" "generator" "gold"
181 "have_unit" "header" "hide_unit" "if" "illuminated_time" "image" "img"
182 "income" "italic" "item" "jump" "kill" "killed" "label" "language"
183 "leader_goal" "main_map" "menu" "message" "mini_map" "missile_frame"
184 "modifications" "modify_side" "modify_turns" "move" "move_unit_fake"
185 "movement_costs" "movetype" "multiplayer" "multiplayer_side" "music" "not"
186 "num_units" "object" "objectives" "objective" "observers" "option" "or"
187 "panel" "part" "place_shroud" "position" "print" "protect_location"
188 "protect_unit" "race" "random" "recall" "recalls" "recruit" "recruits"
189 "redraw" "ref" "remove_shroud" "remove_unit_overlay" "removeitem" "replay"
190 "replay_start" "resistance" "resolution" "results" "role" "save" "scenario"
191 "scroll" "scroll_to" "scroll_to_unit" "section" "set_recruit"
192 "set_variable" "side" "side_playing" "snapshot" "sound" "source" "specials"
193 "statistics" "status" "stone" "store_gold" "store_locations"
194 "store_starting_location" "store_unit" "story" "target" "team" "teleport"
195 "teleport_anim" "terrain" "terrain_graphics" "test" "textdomain" "theme"
196 "then" "tile" "time" "time_area" "time_of_day" "topic" "toplevel" "trait"
197 "turn" "tutorial" "unhide_unit" "unit" "unit_abilities" "unit_alignment"
198 "unit_description" "unit_hp" "unit_image" "unit_level" "unit_moves"
199 "unit_overlay" "unit_profile" "unit_status" "unit_traits" "unit_type"
200 "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit" "upkeep"
201 "variable" "variables" "village" "villages" "while")
202 "A list containing all tags which are available for use in WML.")
204 (defun wesnoth-element-closing (&optional preproc-bol
)
205 "Return string to use for closing indentation element.
207 If PREPROC-BOL is non-nil, return regexp for closing tags and
208 #enddef, otherwise return all closing elements."
210 "^[ \t]*\\(\\[/.+\\]\\|#enddef\\)"
211 (concat "^[ \t]*\\(\\[/.+\\]\\|\\("
212 wesnoth-preprocessor-closing-regexp
"\\)\\)")))
214 (defun wesnoth-element-opening (&optional preproc-bol
)
215 "Return string to use for opening indentation element.
217 If PREPROC-BOL is non-nil, return regexp for opening tags and
218 #define, otherwise return all opening elements."
220 "^[ \t]*\\(\\[[^/]*?\\]\\|#define\\)"
221 (concat "^[ \t]*\\(\\[[^/]*?\\]\\|\\("
222 wesnoth-preprocessor-opening-regexp
"\\)\\)")))
224 (defun wesnoth-element (&optional preproc-bol
)
225 "Return string to use for an opening or closing indentation element.
227 If PREPROC-BOL is non-nil, return regexp for tags, #enddef and
228 #define, otherwise return all opening and closing elements."
230 "^[\t ]*\\(\\[\\|#define\\|#enddef\\)"
231 (concat "^[\t ]*\\(\\[\\|"
232 wesnoth-preprocessor-regexp
"\\)")))
235 (defun wesnoth-insert-tag (tagname &optional start end
)
236 "Inserts the specified opening tag and it's matching closing tag.
237 Both the opening and closing tags will be placed on their own
238 lines with point positioned between them. Completion of tags at
239 the prompt uses `wesnoth-tags-list'.
241 TAGNAME is the name of the tag to be inserted. If START and END
242 are given, the tags will be inserted around the specified region.
243 Enabling `transient-mark-mode' will cause `wesnoth-insert-tag' to
244 insert opening and closing tags around the specified region."
246 (list (completing-read "Tag: " wesnoth-tags-list nil nil
)))
247 (when (and (not (or start end
)) transient-mark-mode mark-active
)
248 (setq start
(region-beginning)
249 end
(copy-marker (region-end))))
253 (or (looking-at "^[\t ]*$")
255 (insert "[" tagname
"]")
257 (or (looking-back "^[\t ]*$")
259 (insert "[/" tagname
"]\n")
260 (setq end
(point)) ;; New target for indent-region
261 (indent-region start end
))
262 (or (looking-back "^[\t ]*$")
264 (wesnoth-insert-and-indent "[" tagname
"]")
265 (wesnoth-insert-and-indent "\n")
267 (wesnoth-insert-and-indent "\n[/" tagname
"]"))))
269 (defun wesnoth-insert-missing-closing (&optional start end
)
270 "Insert the next expected closing element at point.
272 START and END define the region to check for missing closing
273 elements. If `transient-mark-mode' is enabled, the region
274 specified will be used as START and END. Otherwise, START and
275 END will be the minimum and maximum positions of the buffer,
278 (if (and transient-mark-mode mark-active
)
279 (setq start
(region-beginning)
280 end
(copy-marker (region-end)))
281 (setq start
(point-min)
283 (let ((element (wesnoth-check-structure start end
)))
285 (if (string= element
"Unexpected end of file")
286 (message "%s" "Error: Expected end of file")
287 (when (not (looking-at "^[\t ]*$"))
291 (wesnoth-indent-line)
293 (error "%s" "Unable to find element to insert"))))
295 (defun wesnoth-insert-and-indent (&rest args
)
296 "Concatenate and insert the given string(s) before indenting."
297 (insert (apply 'concat args
))
298 (wesnoth-indent-line)
301 (defun wesnoth-newline (&optional indent
)
302 "Indent both the current line and the newline created.
303 If `wesnoth-auto-indent-flag' is nil, indentation will not be
307 (when (or wesnoth-auto-indent-flag indent
)
308 (wesnoth-indent-line)))
312 (defun wesnoth-forward-tag (repeat)
313 "Move point to the end of the next tag.
314 REPEAT is an optional numeric argument. If REPEAT is non-nil,
315 jump forward the specified number of tags."
317 (or repeat
(setq repeat
1))
319 (wesnoth-backward-tag (abs repeat
))
320 (let ((iterations 0))
321 (while (< iterations repeat
)
323 (search-forward-regexp
324 (wesnoth-element-opening)
326 (setq iterations
(1+ iterations
))))
329 (defun wesnoth-backward-tag (repeat)
330 "Move point to the beginning of the previous tag.
331 REPEAT is an optional numeric argument. If REPEAT is non-nil,
332 jump backward the specified number of tags."
334 (or repeat
(setq repeat
1))
336 (wesnoth-forward-tag (abs repeat
))
337 (let ((iterations 0))
338 (while (< iterations repeat
)
340 (search-backward-regexp
341 (wesnoth-element-opening)
344 (search-forward-regexp "[^[:blank:]]")
346 (setq iterations
(1+ iterations
))))))
348 (defun wesnoth-jump-to-matching ()
349 "Jump point to the matching opening/closing tag.
350 A tag must be on the same line as point for jumping to occur.
351 Tag structure between the start and target positions must be
352 consistent for jumping to occur."
357 (search-backward nil
))
362 (when (looking-at (wesnoth-element-closing))
363 (setq search-backward t
))
364 (if (wesnoth-wml-start-pos)
365 (if (> (point) (wesnoth-wml-start-pos))
366 (search-backward-regexp
369 (goto-char (point-min))
370 (search-forward-regexp
373 (error "%s" "Unable to locate tag to jump from")))
378 (or (< open-tags
0) (not search-started
))
379 (search-backward-regexp
382 (setq search-started t
)
383 (if (looking-at (wesnoth-element-opening))
384 (setq open-tags
(1+ open-tags
))
385 (when (looking-at (wesnoth-element-closing))
386 (setq open-tags
(1- open-tags
)))))
388 (search-backward-regexp "\\[\\|#"))
390 (or (> open-tags
0) (not search-started
))
391 (search-forward-regexp
395 (setq search-started t
)
396 (if (looking-at (wesnoth-element-opening))
397 (setq open-tags
(1+ open-tags
))
398 (when (looking-at (wesnoth-element-closing))
399 (setq open-tags
(1- open-tags
))))
402 (setq tag-position
(point)))
405 (and (wesnoth-check-structure (min (point) tag-position
)
406 (max (point) tag-position
))
407 (message "%s" "Region concerning jump does not nest correctly; \
408 target may not be correct"))
410 (goto-char tag-position
)
414 (defun wesnoth-indent-line ()
415 "Determine and performs indentation on the current line.
416 The Indentation style can be customised by modifying
417 `wesnoth-indentation-function'."
419 (funcall wesnoth-indentation-function
))
421 (defun wesnoth-wml-start-pos ()
422 "Determine the position of `point' relative to where the actual WML begins.
423 Return the likely starting position of the WML if it is found.
424 Otherwise return nil."
426 (goto-char (point-min))
427 (when (search-forward-regexp (wesnoth-element nil
)
432 (defun wesnoth-indent (savefile preproc-bol
)
433 "Indent the current line as WML.
434 If SAVEFILE is non-nil, use savefile-style indentation,
435 otherwise use default style indentation.
436 If PREPROC-BOL is non-nil, indent preprocessor statements
437 to the first column, otherwise indent them in-line with any
440 (if (or (not (wesnoth-wml-start-pos))
441 (<= (point) (wesnoth-wml-start-pos))
442 (nth 3 (syntax-ppss (point)))
443 (and preproc-bol
(looking-at
444 (concat "^[\t ]*" wesnoth-preprocessor-regexp
)))
445 (not (wesnoth-check-structure (wesnoth-wml-start-pos) (point))))
451 ((looking-at (wesnoth-element-closing preproc-bol
))
452 (wesnoth-ensure-define-separate preproc-bol
)
453 ;; If closing preceded by closing, decrease indentation
454 (when (and (looking-at (wesnoth-element-closing preproc-bol
)))
455 (setq cur-indent
(- (current-indentation) wesnoth-base-indent
))))
457 ((looking-at (wesnoth-element-opening preproc-bol
))
458 ;; Goto start-pos if unable to find target
459 (wesnoth-ensure-define-separate preproc-bol
)
460 ;; If opening preceded by opening, increase indentation
461 (when (or (looking-at (concat (wesnoth-element-opening preproc-bol
)))
463 (wesnoth-check-structure (wesnoth-wml-start-pos) (point))))
464 (setq cur-indent
(+ (current-indentation) wesnoth-base-indent
))))
465 ;; Not opening, not closing
467 (wesnoth-ensure-define-separate preproc-bol
)
468 ;; Decrease indent if preceded by closing
470 (and (looking-at (wesnoth-element-opening preproc-bol
))
472 (+ (current-indentation) wesnoth-base-indent
)))
473 (and (looking-at (wesnoth-element-closing preproc-bol
))
474 (setq cur-indent
(- (current-indentation)
475 wesnoth-base-indent
))))))
476 ;; Indentation change unnecessary
478 (setq cur-indent
(current-indentation))))
479 (and (< cur-indent
0) (setq cur-indent
0))
480 (unless (and (not cur-indent
) (= (current-indentation) cur-indent
))
481 (indent-line-to cur-indent
))))
483 (search-forward-regexp "[\t ]*"))
485 (defun wesnoth-indent-withtags-inline ()
486 "Indent the current line using withtags-inline-style indentation.
487 Preprocessor statements will be indented as tags."
488 (wesnoth-indent nil nil
))
490 (defun wesnoth-indent-default-inline ()
491 "Indent the current line using default-inline-style indentation.
492 Preprocessor statements will be indented as tags."
493 (wesnoth-indent t nil
))
495 (defun wesnoth-indent-withtags ()
496 "Indent the current line using withtags-style indentation.
497 Preprocessor statements will be indented to the first column."
498 (wesnoth-indent nil t
))
500 (defun wesnoth-indent-default ()
501 "Indent the current line using default-style indentation.
502 Preprocessor statements will be indented to the first column."
503 (wesnoth-indent t t
))
505 (defun wesnoth-newline-and-indent (&optional indent
)
506 "Indent both the current line and the newline created.
507 If `wesnoth-auto-indent-flag' is nil, indentation will not be
511 (when (or wesnoth-auto-indent-flag indent
)
512 (wesnoth-indent-line)))
514 (defun wesnoth-within-define ()
519 (search-backward-regexp
520 "^[\t ]*\\(#define\\|#enddef\\)" (wesnoth-wml-start-pos) t
)
521 (if (looking-at "^[\t ]*#define")
522 (setq upper
(point)))
524 (search-forward-regexp
525 "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t
)
527 (if (looking-at "^[\t ]*#enddef")
528 (setq lower
(point))))
530 (and upper
(> upper
0))
531 (and lower
(> lower
0)))))
533 (defun wesnoth-ensure-define-separate (&optional preproc-bol
)
534 "Ensure elements in #define blocks don't affect those outside.
536 If PREPROC-BOL is non-nil, ensure elements outside of
537 #define... #enddef ignore the structure for determining
539 (if (and (not (wesnoth-within-define)) preproc-bol
)
541 (search-backward-regexp
542 (wesnoth-element preproc-bol
)
543 (wesnoth-wml-start-pos) t
)
544 (while (and (wesnoth-within-define)
545 (not (= (point) (wesnoth-wml-start-pos))))
546 (search-backward-regexp
547 (wesnoth-element preproc-bol
)
548 (wesnoth-wml-start-pos) 'start-pos
)))
549 (search-backward-regexp
550 (wesnoth-element preproc-bol
)
551 (wesnoth-wml-start-pos) 0)))
554 (defun wesnoth-check-tag-names ()
555 "Check the names of all tags in the buffer for correctness.
556 If a tag is found which is not present in the list an error will
557 be signalled and point will be moved to the corresponding
560 (let ((tag-position nil
)
561 (missing-tag-name nil
))
563 (goto-char (point-min))
565 (search-forward-regexp "^[\t ]*\\[" (point-max) t
)
568 (when (looking-at "^[\t ]*\\[[/]?\\(\\w+\\)\\]")
569 (unless (member (match-string-no-properties 1) wesnoth-tags-list
)
570 (setq tag-position
(point))
571 (setq missing-tag-name
(match-string-no-properties 1))))
575 (goto-char tag-position
)
577 (search-backward "[")
578 (message "'%s' is not known to exist"
580 (message "%s" "No unknown tag names found."))))
582 (defun wesnoth-check-structure (&optional start end
)
583 "Check the buffer for correct nesting of elements.
584 If a problem is found in the structure, point will be placed at
585 the location which an element was expected and the expected
586 element will be displayed in the mini-buffer.
588 START and END define the region to be checked. If
589 `transient-mark-mode' is enabled, the region specified will be
590 checked. Otherwise START and END will be the minimum and maximum
591 positions of the buffer, respectively."
593 (unless (or start end
)
594 (if (and transient-mark-mode mark-active
)
595 (setq start
(region-beginning)
596 end
(copy-marker (region-end)))
597 (setq start
(point-min)
599 (let ((unmatched-tag-list '())
604 (while (and (search-forward-regexp (wesnoth-element) end t
)
605 (not error-position
))
607 (if (looking-at "^[\t ]*#\\(\\w+\\)")
608 (let ((preprocessor-name (match-string-no-properties 1)))
610 ((string= preprocessor-name
"define")
611 (setq unmatched-tag-list
612 (cons preprocessor-name unmatched-tag-list
)))
613 ((string= preprocessor-name
"ifdef")
614 (setq unmatched-tag-list
615 (cons preprocessor-name unmatched-tag-list
)))
616 ((string= preprocessor-name
"ifndef")
617 (setq unmatched-tag-list
618 (cons preprocessor-name unmatched-tag-list
)))
619 ((string= preprocessor-name
"else")
620 (unless (string-match "ifn?def" (car unmatched-tag-list
))
621 (setq error-position
(point))))
622 ((string= preprocessor-name
"endif")
623 (if (string-match "ifn?def" (car unmatched-tag-list
))
624 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
625 (setq error-position
(point))))
626 ((string= preprocessor-name
"enddef")
627 (if (string= (car unmatched-tag-list
) "define")
628 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
629 (setq error-position
(point))))))
630 (if (looking-at "^[\t ]*\\[\\+?\\(\\w+\\)\\]")
631 (setq unmatched-tag-list
632 (cons (match-string-no-properties 1)
634 (when (looking-at "^[\t ]*\\[/\\(\\w+\\)\\]")
635 (if (string= (match-string-no-properties 1)
636 (car unmatched-tag-list
))
637 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
638 (setq error-position
(point))))))
640 (when unmatched-tag-list
641 (cond ((string= (car unmatched-tag-list
) "define")
642 (setq expected
"#enddef"))
643 ((string-match "ifn?def" (car unmatched-tag-list
))
644 (setq expected
"#endif"))
645 ((not unmatched-tag-list
)
646 (setq expected
"Unexpected end of file"))))
648 (if (not (or unmatched-tag-list error-position
))
649 (message "%s" "Structure appears consistent.")
651 (goto-char error-position
))
652 (if (string= expected
"Unexpected end of file")
653 (message "Error %s" expected
)
654 (message "Error: Expecting %s"
657 (concat " [/" (car unmatched-tag-list
) "]")))))
658 (when (or expected unmatched-tag-list
)
659 (or expected
(concat "[/" (car unmatched-tag-list
) "]"))))))
662 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
663 "Major mode for editing WML."
664 (set-syntax-table wesnoth-syntax-table
)
665 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
666 (set (make-local-variable 'comment-start
) "#")
667 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent-line
)
668 (set (make-local-variable 'font-lock-defaults
)
669 '(wesnoth-font-lock-keywords
671 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
672 (setq indent-tabs-mode nil
)
673 (setq mode-name
"WML")
674 (run-hooks 'wesnoth-mode-hook
))
676 (provide 'wesnoth-mode
)