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 ;; * Now compatible with GNU Emacs 21.4.
36 ;; * Added support for several new tags.
37 ;; * Added menu entry for wesnoth-mode.
38 ;; * Significant speed increase to indentation.
39 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
40 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
42 ;; * Trailing whitespace is no longer created when creating a second consecutive
44 ;; * Spurious newlines are no longer created when inserting a tag elements
47 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
48 ;; `wesnoth-element', `wesnoth-element-opening',
49 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
50 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
51 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
52 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
53 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
54 ;; * Fixed a bug in indentation where content was needed between elements pairs
55 ;; for indentation to work.
56 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
57 ;; `wesnoth-auto-indent-flag'.
58 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
59 ;; * Added indentation styles: `wesnoth-indent-default',
60 ;; `wesnoth-indent-withtags' which implement a a similar indentation
61 ;; style to the existing styles, however all preprocessor statements are
62 ;; indented to the first column.
63 ;; * Added support for several new tags.
64 ;; * Modified `wesnoth-newline' to behave more consistently.
65 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
66 ;; now leaves point at the beginning (when moving backward) or end (when
67 ;; moving forward) of the match.
68 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
69 ;; will now work on preprocessor statements. Will now warn if jump
70 ;; destination may not be correct (due to errors in WML structure).
71 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
72 ;; * `wesnoth-check-structure' can now be applied over an active region and
73 ;; now checks preprocessor statements for correct nesting.
74 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
75 ;; perform indentation by providing a prefix argument.
76 ;; * Indentation styles now leave point at the first non-whitespace character of
78 ;; * `wesnoth-check-tag-names' now reports on success.
79 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
80 ;; * `outline-minor-mode' now works on macro definitions.
82 ;; * Base indent now defaults to 4.
83 ;; * Added support for #ifndef.
85 (defconst wesnoth-mode-version
"1.2.3b"
86 "The current version of wesnoth-mode.")
88 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
92 (defcustom wesnoth-indent-preprocessor-bol t
93 "Style used to indent preprocessor statements.
94 If non-nil, preprocessor statements are indented to the first
95 column. Otherwise, they are indented as tags."
99 (defcustom wesnoth-indent-savefile t
100 "Style used to indent WML elements.
101 If non-nil, attributes will be indented further than their
102 containing element. Otherwise, attributes will be indented at
105 :group
'wesnoth-mode
)
107 (defcustom wesnoth-auto-indent-flag t
108 "Whether to attempt tag indentation when a newline is created.
109 If nil, no indentation will be attempted. Otherwise, attempt to
112 :group
'wesnoth-mode
)
114 (defcustom wesnoth-base-indent
4
115 "The number of columns to indent WML."
117 :group
'wesnoth-mode
)
119 (defconst wesnoth-preprocessor-regexp
120 "#\\(?:enddef\\|define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def\\)"
121 "Regular expression to match all preprocessor statements.")
123 (defconst wesnoth-preprocessor-opening-regexp
124 "#\\(?:define \\|else\\|ifdef \\|ifndef \\)"
125 "Regular expression to match \"opening\" preprocessor statements.")
127 (defconst wesnoth-preprocessor-closing-regexp
128 "#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)"
129 "Regular expression to match \"closing\" preprocessor statements.")
131 (defvar wesnoth-mode-hook nil
)
133 (defvar wesnoth-mode-map
()
134 "Keymap used in wesnoth mode.")
135 (unless wesnoth-mode-map
136 (setq wesnoth-mode-map
(make-sparse-keymap))
137 (define-key wesnoth-mode-map
"\C-\M-a" 'wesnoth-backward-element
)
138 (define-key wesnoth-mode-map
"\C-\M-e" 'wesnoth-forward-element
)
139 (define-key wesnoth-mode-map
"\C-c\C-m" 'wesnoth-jump-to-matching
)
140 (define-key wesnoth-mode-map
"\C-cm" 'wesnoth-jump-to-matching
)
141 (define-key wesnoth-mode-map
"\C-m" 'wesnoth-newline
)
142 (define-key wesnoth-mode-map
"\C-j" 'wesnoth-newline-and-indent
)
143 (define-key wesnoth-mode-map
"\C-c\C-c" 'wesnoth-check-structure
)
144 (define-key wesnoth-mode-map
"\C-cc" 'wesnoth-check-structure
)
145 (define-key wesnoth-mode-map
"\C-c\C-n" 'wesnoth-check-tag-names
)
146 (define-key wesnoth-mode-map
"\C-cn" 'wesnoth-check-tag-names
)
147 (define-key wesnoth-mode-map
"\C-c\C-e" 'wesnoth-insert-tag
)
148 (define-key wesnoth-mode-map
"\C-ce" 'wesnoth-insert-tag
)
149 (define-key wesnoth-mode-map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
150 (define-key wesnoth-mode-map
(kbd "C-c /") 'wesnoth-insert-missing-closing
)
152 (define-key wesnoth-mode-map
[menu-bar wesnoth
]
153 (cons "WML" (make-sparse-keymap "WML")))
154 (define-key wesnoth-mode-map
[menu-bar wesnoth insert-tag
]
155 '("Insert Tag" . wesnoth-insert-tag
))
156 (define-key wesnoth-mode-map
[menu-bar wesnoth check-names
]
157 '("Check Tag Names" . wesnoth-check-tag-names
))
158 (define-key wesnoth-mode-map
[menu-bar wesnoth check-structure
]
159 '("Check Structure" . wesnoth-check-structure
))
160 (define-key wesnoth-mode-map
[menu-bar wesnoth jump-to-matching
]
161 '("Jump to Matching" . wesnoth-jump-to-matching
))
162 (define-key wesnoth-mode-map
[menu-bar wesnoth insert-missing-closing
]
163 '("Insert Missing Tag" . wesnoth-insert-missing-closing
)))
165 (defvar wesnoth-syntax-table
166 (let ((wesnoth-syntax-table (make-syntax-table)))
167 (modify-syntax-entry ?
= "." wesnoth-syntax-table
)
168 (modify-syntax-entry ?\_
"w" wesnoth-syntax-table
)
169 (modify-syntax-entry ?-
"_" wesnoth-syntax-table
)
170 (modify-syntax-entry ?.
"_" wesnoth-syntax-table
)
171 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
172 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
173 wesnoth-syntax-table
)
174 "Syntax table for wesnoth-mode.")
176 ;; Prevents automatic syntax-highlighting of elements which might be
177 ;; pre-processor statements.
178 (defvar wesnoth-syntactic-keywords
180 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\|#enddef\\)" 1 "w")
181 '("\\(#[\t ]*.*$\\)" 1 "<"))
182 "Highlighting syntactic keywords within wesnoth-mode.")
184 (defun wesnoth-preprocessor-best-face ()
185 "Use `font-lock-preprocessor-face' when available."
186 (if (boundp 'font-lock-preprocessor-face
)
187 (copy-face 'font-lock-preprocessor-face
'wesnoth-preprocessor-face
)
188 (copy-face 'font-lock-keyword-face
'wesnoth-preprocessor-face
)))
190 (defvar wesnoth-font-lock-keywords
192 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)"
193 1 'wesnoth-preprocessor-face
)
194 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\w+\\)"
195 2 font-lock-function-name-face
)
196 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
'wesnoth-preprocessor-face
)
197 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)"
198 (1 font-lock-function-name-face
))
199 '("\\({\\(\\w\\|:\\)+\\|{[~@]?\\)"
200 (1 font-lock-function-name-face
))
201 '("}" . font-lock-function-name-face
)
202 '("\\[[^]]+\\]" . font-lock-type-face
)
203 '("\\$\\w+" . font-lock-variable-name-face
)
204 '("\\(\\w+\\(\\,[\t ]*\\w+\\)*\\)="
205 1 font-lock-variable-name-face
))
206 "Syntax highlighting for wesnoth-mode.")
208 (defun wesnoth-element-closing (&optional all
)
209 "Return string to use for a closing element."
210 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
211 (concat "^[ \t]*\\[/.+\\]\\|"
212 wesnoth-preprocessor-closing-regexp
)
213 "^[ \t]*\\(\\[/.+\\]\\)\\|#enddef"))
215 (defun wesnoth-element-opening (&optional all
)
216 "Return string to use for an opening element."
217 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
218 (concat "^[ \t]*\\[[^/]*?\\]\\|"
219 wesnoth-preprocessor-opening-regexp
)
220 "^[ \t]*\\(\\[[^/]*?\\]\\)\\|#define"))
222 (defun wesnoth-element (&optional all
)
223 "Return string to use for an opening or closing element."
224 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
225 (concat "^[\t ]*\\[\\|"
226 wesnoth-preprocessor-regexp
)
227 "^[\t ]*\\(\\[\\|#define\\)\\|#enddef"))
230 (defvar wesnoth-tags-list
232 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
233 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
234 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
235 "command" "deaths" "debug_message" "defend" "defends" "defense" "delay"
236 "destination" "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel"
237 "entry" "era" "event" "expenses" "filter" "filter_adjacent_location"
238 "filter_location" "filter_radius" "filter_second" "filter_vision" "format"
239 "frame" "game_config" "generator" "gold" "have_unit" "header" "hide_unit"
240 "if" "illuminated_time" "image" "img" "income" "italic" "item" "jump"
241 "kill" "killed" "label" "language" "leader_goal" "main_map" "menu"
242 "message" "mini_map" "missile_frame" "modifications" "modify_side"
243 "modify_turns" "move" "move_unit_fake" "movement_costs" "movetype"
244 "multiplayer" "multiplayer_side" "music" "not" "num_units" "object"
245 "objectives" "objective" "observers" "option" "or" "panel" "part"
246 "place_shroud" "position" "print" "protect_location" "protect_unit" "race"
247 "random" "recall" "recalls" "recruit" "recruits" "redraw" "ref"
248 "remove_shroud" "remove_unit_overlay" "removeitem" "replay" "replay_start"
249 "resistance" "resolution" "results" "role" "save" "scenario" "scroll"
250 "scroll_to" "scroll_to_unit" "section" "set_menu_item" "set_recruit"
251 "set_variable" "show_if" "side" "side_playing" "snapshot" "sound" "source"
252 "specials" "statistics" "status" "stone" "store_gold" "store_locations"
253 "store_starting_location" "store_side" "store_unit" "story" "target" "team"
254 "teleport" "teleport_anim" "terrain" "terrain_graphics" "test" "text_input"
255 "textdomain" "theme" "then" "tile" "time" "time_area" "time_of_day" "topic"
256 "toplevel" "trait" "turn" "tutorial" "unhide_unit" "unit" "unit_abilities"
257 "unit_alignment" "unit_description" "unit_hp" "unit_image" "unit_level"
258 "unit_moves" "unit_overlay" "unit_profile" "unit_status" "unit_traits"
259 "unit_type" "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit"
260 "upkeep" "variable" "variables" "village" "villages" "while" "wml_filter")
261 "A list containing all tags which are available in WML.")
263 (defvar wesnoth-completion-cache
'()
264 "List of tags which have been generated by `wesnoth-build-completion'.")
266 (defun wesnoth-build-completion (&optional rebuild
)
267 "Create a new list for tag completion if necessary.
268 If REBUILD is non-nil, regenerate `wesnoth-completion-cache'."
270 (if (> emacs-major-version
21)
272 (if (and wesnoth-completion-cache
(not rebuild
))
273 wesnoth-completion-cache
276 (dolist (tag wesnoth-tags-list
)
277 (setq iter
(1+ iter
))
278 (setq tags
(append tags
(list (cons tag iter
)))))
279 (setq wesnoth-completion-cache tags
)))))
281 (defun wesnoth-insert-tag (tagname &optional start end
)
282 "Inserts the specified opening tag and it's matching closing tag.
283 Both the opening and closing tags will be placed on their own
284 lines with point positioned between them. Completion of tags at
285 the prompt uses `wesnoth-tags-list'.
287 TAGNAME is the name of the tag to be inserted. If START and END
288 are given, the tags will be inserted around the specified region.
289 Enabling `transient-mark-mode' will cause `wesnoth-insert-tag' to
290 insert opening and closing tags around the specified region."
292 (list (completing-read "Tag: " (wesnoth-build-completion) nil nil
)))
293 (when (and (not (or start end
)) transient-mark-mode mark-active
)
294 (setq start
(region-beginning)
295 end
(copy-marker (region-end))))
299 (or (looking-at "^[\t ]*$")
301 (insert "[" tagname
"]")
304 (if (looking-at "^[\t ]*$")
308 (insert "[/" tagname
"]")
309 (setq end
(point)) ;; New target for indent-region
310 (indent-region start end nil
))
312 (or (looking-at "^[\t ]*$")
317 (wesnoth-insert-and-indent "[" tagname
"]")
318 (wesnoth-insert-and-indent "\n")
320 (wesnoth-insert-and-indent "\n[/" tagname
"]"))))
322 (defun wesnoth-insert-missing-closing (&optional start end
)
323 "Insert the next expected closing element at point.
325 START and END define the region to check for missing closing
326 elements. If `transient-mark-mode' is enabled, the region
327 specified will be used as START and END. Otherwise, START and
328 END will be the minimum and maximum positions of the buffer,
331 (if (and transient-mark-mode mark-active
)
332 (setq start
(region-beginning)
333 end
(copy-marker (region-end)))
334 (setq start
(point-min)
336 (let ((element (wesnoth-check-structure start end
)))
338 (if (string= element
"Unexpected end of file")
339 (message "%s" "Error: Expected end of file")
340 (when (not (looking-at "^[\t ]*$"))
346 (error "%s" "Unable to find element to insert"))))
348 (defun wesnoth-insert-and-indent (&rest args
)
349 "Concatenate and insert the given string(s) before indenting."
350 (insert (apply 'concat args
))
354 (defun wesnoth-newline (&optional indent
)
355 "Indent both the current line and the newline created.
356 If `wesnoth-auto-indent-flag' is nil, indentation will not be
357 performed. Indentation can be forced by setting INDENT to
361 (when (and (or wesnoth-auto-indent-flag indent
)
362 (not (looking-at "^[\t ]*$")))
367 (defun wesnoth-forward-element (repeat)
368 "Move point to the end of the next tag.
369 REPEAT is an optional numeric argument. If REPEAT is non-nil,
370 jump forward the specified number of tags."
372 (or repeat
(setq repeat
1))
374 (wesnoth-backward-element (abs repeat
))
375 (let ((iterations 0))
376 (while (< iterations repeat
)
378 (search-forward-regexp
379 (wesnoth-element-opening t
)
381 (setq iterations
(1+ iterations
))))
384 (defun wesnoth-backward-element (repeat)
385 "Move point to the beginning of the previous tag.
386 REPEAT is an optional numeric argument. If REPEAT is non-nil,
387 jump backward the specified number of tags."
389 (or repeat
(setq repeat
1))
391 (wesnoth-forward-element (abs repeat
))
392 (let ((iterations 0))
393 (while (< iterations repeat
)
395 (search-backward-regexp
396 (wesnoth-element-opening t
)
399 (search-forward-regexp "[^[:blank:]]")
401 (setq iterations
(1+ iterations
))))))
403 (defun wesnoth-jump-to-matching ()
404 "Jump point to the matching opening/closing tag.
405 A tag must be on the same line as point for jumping to occur.
406 Tag structure between the start and target positions must be
407 consistent for jumping to occur."
412 (search-backward nil
))
417 (when (looking-at (wesnoth-element-closing t
))
418 (setq search-backward t
))
419 (if (wesnoth-wml-start-pos)
420 (if (> (point) (wesnoth-wml-start-pos))
421 (search-backward-regexp
424 (goto-char (point-min))
425 (search-forward-regexp
428 (error "%s" "Unable to locate tag to jump from")))
433 (or (< open-tags
0) (not search-started
))
434 (search-backward-regexp
437 (setq search-started t
)
438 (if (looking-at (wesnoth-element-opening t
))
439 (setq open-tags
(1+ open-tags
))
440 (when (looking-at (wesnoth-element-closing t
))
441 (setq open-tags
(1- open-tags
)))))
443 (search-backward-regexp "\\[\\|#"))
445 (or (> open-tags
0) (not search-started
))
446 (search-forward-regexp
450 (setq search-started t
)
451 (if (looking-at (wesnoth-element-opening t
))
452 (setq open-tags
(1+ open-tags
))
453 (when (looking-at (wesnoth-element-closing t
))
454 (setq open-tags
(1- open-tags
))))
457 (setq tag-position
(point)))
460 (and (wesnoth-check-structure (min (point) tag-position
)
461 (max (point) tag-position
))
462 (message "%s" "Region concerning jump does not nest correctly; \
463 target may not be correct"))
465 (goto-char tag-position
)
468 (defun wesnoth-wml-start-pos ()
469 "Determine the position of `point' relative to where the actual WML begins.
470 Return the likely starting position of the WML if it is found.
471 Otherwise return nil."
473 (goto-char (point-min))
474 (when (search-forward-regexp (wesnoth-element t
) (buffer-size) t
)
478 (defun wesnoth-indent ()
479 "Indent the current line as WML."
481 (if (or (not (wesnoth-wml-start-pos))
482 (<= (point) (wesnoth-wml-start-pos))
483 (nth 3 (parse-partial-sexp (point-min) (point)))
484 (and wesnoth-indent-preprocessor-bol
485 (looking-at wesnoth-preprocessor-regexp
)))
491 ((looking-at (wesnoth-element-closing))
492 (wesnoth-ensure-define-separate)
493 ;; If closing preceded by closing, decrease indentation
494 (when (and (looking-at (wesnoth-element-closing)))
495 (setq cur-indent
(- (current-indentation) wesnoth-base-indent
))))
497 ((looking-at (wesnoth-element-opening))
498 ;; Goto start-pos if unable to find target
499 (wesnoth-ensure-define-separate)
500 ;; If opening preceded by opening, increase indentation
501 (when (or (looking-at (concat (wesnoth-element-opening)))
503 (wesnoth-check-structure (wesnoth-wml-start-pos) (point))))
504 (setq cur-indent
(+ (current-indentation) wesnoth-base-indent
))))
505 ;; Not opening, not closing
507 (wesnoth-ensure-define-separate)
508 ;; Decrease indent if preceded by closing
509 (if wesnoth-indent-savefile
510 (and (looking-at (wesnoth-element-opening))
512 (+ (current-indentation) wesnoth-base-indent
)))
513 (and (looking-at (wesnoth-element-closing))
514 (setq cur-indent
(- (current-indentation)
515 wesnoth-base-indent
))))))
516 ;; Indentation change unnecessary
518 (setq cur-indent
(current-indentation))))
519 (and (< cur-indent
0) (setq cur-indent
0))
520 (unless (and (not cur-indent
) (= (current-indentation) cur-indent
))
521 (indent-line-to cur-indent
))))
523 (search-forward-regexp "[\t ]*"))
525 (defun wesnoth-newline-and-indent (&optional indent
)
526 "Indent both the current line and the newline created.
527 If `wesnoth-auto-indent-flag' is nil, indentation will not be
531 (when (or wesnoth-auto-indent-flag indent
)
534 (defun wesnoth-within-define ()
540 (unless (looking-at "#enddef")
542 (search-backward-regexp
543 "\\(^[\t ]*#define\\|#enddef\\)" (wesnoth-wml-start-pos) t
)
544 (if (looking-at "^[\t ]*#define")
545 (setq upper
(point)))
547 (search-forward-regexp
548 "#enddef" (point-max) t
)
550 (if (looking-at "#enddef")
551 (setq lower
(point))))
552 (if (and upper
(= upper
(wesnoth-wml-start-pos)))
556 (defun wesnoth-ensure-define-separate ()
557 "Ensure elements in #define blocks don't affect those outside."
558 (if (and (not (wesnoth-within-define)) wesnoth-indent-preprocessor-bol
)
560 (search-backward-regexp
562 (wesnoth-wml-start-pos) t
)
563 (while (and (wesnoth-within-define)
564 (not (= (point) (wesnoth-wml-start-pos))))
565 (search-backward-regexp
567 (wesnoth-wml-start-pos) 0)))
568 (search-backward-regexp
570 (wesnoth-wml-start-pos) 0)))
573 (defun wesnoth-check-tag-names ()
574 "Check the names of all tags in the buffer for correctness.
575 If a tag is found which is not present in the list an error will
576 be signalled and point will be moved to the corresponding
579 (let ((tag-position nil
)
580 (missing-tag-name nil
))
582 (goto-char (point-min))
584 (search-forward-regexp "^[\t ]*\\[" (point-max) t
)
587 (when (looking-at "^[\t ]*\\[[/]?\\(\\w+\\)\\]")
588 (unless (member (match-string-no-properties 1) wesnoth-tags-list
)
589 (setq tag-position
(point))
590 (setq missing-tag-name
(match-string-no-properties 1))))
594 (goto-char tag-position
)
596 (search-backward "[")
597 (message "'%s' is not known to exist"
599 (message "%s" "No unknown tag names found."))))
601 (defun wesnoth-check-structure (&optional start end
)
602 "Check the buffer for correct nesting of elements.
603 If a problem is found in the structure, point will be placed at
604 the location which an element was expected and the expected
605 element will be displayed in the mini-buffer.
607 START and END define the region to be checked. If
608 `transient-mark-mode' is enabled, the region specified will be
609 checked. Otherwise START and END will be the minimum and maximum
610 positions of the buffer, respectively."
612 (unless (or start end
)
613 (if (and transient-mark-mode mark-active
)
614 (setq start
(region-beginning)
615 end
(copy-marker (region-end)))
616 (setq start
(point-min)
618 (let ((unmatched-tag-list '())
623 (while (and (search-forward-regexp
626 (not error-position
))
627 (search-backward-regexp
630 (if (looking-at wesnoth-preprocessor-regexp
)
631 (let ((preprocessor-name (match-string-no-properties 1)))
636 (string= preprocessor-name preproc
))
637 '("define" "ifdef" "ifndef")))
638 (setq unmatched-tag-list
639 (cons preprocessor-name unmatched-tag-list
)))
640 ((string= preprocessor-name
"else")
641 (unless (string-match "ifn?def" (car unmatched-tag-list
))
642 (setq error-position
(point))))
643 ((string= preprocessor-name
"endif")
644 (if (string-match "ifn?def" (car unmatched-tag-list
))
645 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
646 (setq error-position
(point))))
647 ((string= preprocessor-name
"enddef")
648 (if (string= (car unmatched-tag-list
) "define")
649 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
650 (setq error-position
(point))))))
651 (if (looking-at "^[\t ]*\\[\\+?\\(\\w+\\)\\]")
652 (setq unmatched-tag-list
653 (cons (match-string-no-properties 1)
655 (when (looking-at "^[\t ]*\\[/\\(\\w+\\)\\]")
656 (if (string= (match-string-no-properties 1)
657 (car unmatched-tag-list
))
658 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
659 (setq error-position
(point))))))
661 (when unmatched-tag-list
662 (cond ((string= (car unmatched-tag-list
) "define")
663 (setq expected
"#enddef"))
664 ((string-match "ifn?def" (car unmatched-tag-list
))
665 (setq expected
"#endif"))
666 ((not unmatched-tag-list
)
667 (setq expected
"Unexpected end of file"))))
669 (if (not (or unmatched-tag-list error-position
))
670 (message "%s" "Structure appears consistent.")
672 (goto-char error-position
))
673 (if (string= expected
"Unexpected end of file")
674 (message "Error %s" expected
)
675 (message "Error: Expecting %s"
678 (concat " [/" (car unmatched-tag-list
) "]")))))
679 (when (or expected unmatched-tag-list
)
680 (or expected
(concat "[/" (car unmatched-tag-list
) "]"))))))
683 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
684 "Major mode for editing WML."
685 (wesnoth-preprocessor-best-face)
686 (set-syntax-table wesnoth-syntax-table
)
687 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
688 (set (make-local-variable 'comment-start
) "#")
689 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
690 (set (make-local-variable 'font-lock-defaults
)
691 '(wesnoth-font-lock-keywords
693 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
694 (setq indent-tabs-mode nil
)
695 (setq mode-name
"WML")
696 (run-hooks 'wesnoth-mode-hook
))
698 (provide 'wesnoth-mode
)