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 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
37 ;; * Now compatible with GNU Emacs 21.4.
38 ;; * Added support for several new tags.
39 ;; * Added menu entry for wesnoth-mode.
40 ;; * Significant speed increase to indentation.
41 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
42 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
44 ;; * Trailing whitespace is no longer created when creating a second consecutive
46 ;; * Spurious newlines are no longer created when inserting a tag elements
49 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
50 ;; `wesnoth-element', `wesnoth-element-opening',
51 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
52 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
53 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
54 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
55 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
56 ;; * Fixed a bug in indentation where content was needed between elements pairs
57 ;; for indentation to work.
58 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
59 ;; `wesnoth-auto-indent-flag'.
60 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
61 ;; * Added indentation styles: `wesnoth-indent-default',
62 ;; `wesnoth-indent-withtags' which implement a a similar indentation
63 ;; style to the existing styles, however all preprocessor statements are
64 ;; indented to the first column.
65 ;; * Added support for several new tags.
66 ;; * Modified `wesnoth-newline' to behave more consistently.
67 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
68 ;; now leaves point at the beginning (when moving backward) or end (when
69 ;; moving forward) of the match.
70 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
71 ;; will now work on preprocessor statements. Will now warn if jump
72 ;; destination may not be correct (due to errors in WML structure).
73 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
74 ;; * `wesnoth-check-structure' can now be applied over an active region and
75 ;; now checks preprocessor statements for correct nesting.
76 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
77 ;; perform indentation by providing a prefix argument.
78 ;; * Indentation styles now leave point at the first non-whitespace character of
80 ;; * `wesnoth-check-tag-names' now reports on success.
81 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
82 ;; * `outline-minor-mode' now works on macro definitions.
84 ;; * Base indent now defaults to 4.
85 ;; * Added support for #ifndef.
87 (defconst wesnoth-mode-version
"1.2.3+git"
88 "The current version of wesnoth-mode.")
90 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
94 (defcustom wesnoth-indent-preprocessor-bol t
95 "Style used to indent preprocessor statements.
96 If non-nil, preprocessor statements are indented to the first
97 column. Otherwise, they are indented as tags."
101 (defcustom wesnoth-indent-savefile t
102 "Style used to indent WML elements.
103 If non-nil, attributes will be indented further than their
104 containing element. Otherwise, attributes will be indented at
107 :group
'wesnoth-mode
)
109 (defcustom wesnoth-auto-indent-flag t
110 "Whether to attempt tag indentation when a newline is created.
111 If nil, no indentation will be attempted. Otherwise, attempt to
114 :group
'wesnoth-mode
)
116 (defcustom wesnoth-base-indent
4
117 "The number of columns to indent WML."
119 :group
'wesnoth-mode
)
121 (defconst wesnoth-preprocessor-regexp
122 "[\t ]*#\\(?:enddef\\|define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def\\)"
123 "Regular expression to match all preprocessor statements.")
125 (defconst wesnoth-preprocessor-opening-regexp
126 "[\t ]*#\\(?:define \\|else\\|ifdef \\|ifndef \\)"
127 "Regular expression to match \"opening\" preprocessor statements.")
129 (defconst wesnoth-preprocessor-closing-regexp
130 "[\t ]*#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)"
131 "Regular expression to match \"closing\" preprocessor statements.")
133 (defvar wesnoth-mode-hook nil
)
135 (defvar wesnoth-mode-map
()
136 "Keymap used in wesnoth mode.")
137 (unless wesnoth-mode-map
138 (setq wesnoth-mode-map
(make-sparse-keymap))
139 (define-key wesnoth-mode-map
"\C-\M-a" 'wesnoth-backward-element
)
140 (define-key wesnoth-mode-map
"\C-\M-e" 'wesnoth-forward-element
)
141 (define-key wesnoth-mode-map
"\C-c\C-m" 'wesnoth-jump-to-matching
)
142 (define-key wesnoth-mode-map
"\C-cm" 'wesnoth-jump-to-matching
)
143 (define-key wesnoth-mode-map
"\C-m" 'wesnoth-newline
)
144 (define-key wesnoth-mode-map
"\C-j" 'wesnoth-newline-and-indent
)
145 (define-key wesnoth-mode-map
"\C-c\C-c" 'wesnoth-check-structure
)
146 (define-key wesnoth-mode-map
"\C-cc" 'wesnoth-check-structure
)
147 (define-key wesnoth-mode-map
"\C-c\C-n" 'wesnoth-check-tag-names
)
148 (define-key wesnoth-mode-map
"\C-cn" 'wesnoth-check-tag-names
)
149 (define-key wesnoth-mode-map
"\C-c\C-e" 'wesnoth-insert-tag
)
150 (define-key wesnoth-mode-map
"\C-ce" 'wesnoth-insert-tag
)
151 (define-key wesnoth-mode-map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
152 (define-key wesnoth-mode-map
(kbd "C-c /") 'wesnoth-insert-missing-closing
)
154 (define-key wesnoth-mode-map
[menu-bar wesnoth
]
155 (cons "WML" (make-sparse-keymap "WML")))
156 (define-key wesnoth-mode-map
[menu-bar wesnoth insert-tag
]
157 '("Insert Tag" . wesnoth-insert-tag
))
158 (define-key wesnoth-mode-map
[menu-bar wesnoth check-names
]
159 '("Check Tag Names" . wesnoth-check-tag-names
))
160 (define-key wesnoth-mode-map
[menu-bar wesnoth check-structure
]
161 '("Check Structure" . wesnoth-check-structure
))
162 (define-key wesnoth-mode-map
[menu-bar wesnoth jump-to-matching
]
163 '("Jump to Matching" . wesnoth-jump-to-matching
))
164 (define-key wesnoth-mode-map
[menu-bar wesnoth insert-missing-closing
]
165 '("Insert Missing Tag" . wesnoth-insert-missing-closing
)))
167 (defvar wesnoth-syntax-table
168 (let ((wesnoth-syntax-table (make-syntax-table)))
169 (modify-syntax-entry ?
= "." wesnoth-syntax-table
)
170 (modify-syntax-entry ?_
"_" wesnoth-syntax-table
)
171 (modify-syntax-entry ?-
"_" wesnoth-syntax-table
)
172 (modify-syntax-entry ?.
"_" wesnoth-syntax-table
)
173 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
174 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
175 wesnoth-syntax-table
)
176 "Syntax table for wesnoth-mode.")
178 ;; Prevents automatic syntax-highlighting of elements which might be
179 ;; pre-processor statements.
180 (defvar wesnoth-syntactic-keywords
182 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\|#enddef\\)" 1 "w")
183 '("\\(#[\t ]*.*$\\)" 1 "<"))
184 "Highlighting syntactic keywords within wesnoth-mode.")
186 (defun wesnoth-preprocessor-best-face ()
187 "Use `font-lock-preprocessor-face' when available."
188 (if (boundp 'font-lock-preprocessor-face
)
189 (copy-face 'font-lock-preprocessor-face
'wesnoth-preprocessor-face
)
190 (copy-face 'font-lock-keyword-face
'wesnoth-preprocessor-face
)))
192 (defvar wesnoth-font-lock-keywords
194 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)"
195 1 'wesnoth-preprocessor-face
)
196 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
197 2 font-lock-function-name-face
)
198 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
'wesnoth-preprocessor-face
)
199 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)"
200 (1 font-lock-function-name-face
))
201 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)"
202 (1 font-lock-function-name-face
))
203 '("}" . font-lock-function-name-face
)
204 '("\\[[^]]+\\]" . font-lock-type-face
)
205 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face
)
206 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
207 1 font-lock-variable-name-face
))
208 "Syntax highlighting for wesnoth-mode.")
210 (defun wesnoth-element-closing (&optional all
)
211 "Return string to use for a closing element."
212 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
213 (concat "^[ \t]*\\[/.+\\]\\|"
214 wesnoth-preprocessor-closing-regexp
)
215 "^[ \t]*\\(\\(\\[/.+\\]\\)\\|#enddef\\)"))
217 (defun wesnoth-element-opening (&optional all
)
218 "Return string to use for an opening element."
219 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
220 (concat "^[ \t]*\\[[^/]*?\\]\\|"
221 wesnoth-preprocessor-opening-regexp
)
222 "^[ \t]*\\(\\[[^/]*?\\]\\|#define\\)"))
224 (defun wesnoth-element (&optional all
)
225 "Return string to use for an opening or closing element."
226 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
227 (concat "^[\t ]*\\[\\|"
228 wesnoth-preprocessor-regexp
)
229 "^[\t ]*\\(\\[\\|#define\\|#enddef\\)"))
232 (defvar wesnoth-tags-list
234 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
235 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
236 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
237 "command" "deaths" "debug_message" "defend" "defends" "defense" "delay"
238 "destination" "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel"
239 "entry" "era" "event" "expenses" "filter" "filter_adjacent_location"
240 "filter_location" "filter_radius" "filter_second" "filter_vision" "format"
241 "frame" "game_config" "generator" "gold" "have_unit" "header" "hide_unit"
242 "if" "illuminated_time" "image" "img" "income" "italic" "item" "jump"
243 "kill" "killed" "label" "language" "leader_goal" "main_map" "menu"
244 "message" "mini_map" "missile_frame" "modifications" "modify_side"
245 "modify_turns" "move" "move_unit_fake" "movement_costs" "movetype"
246 "multiplayer" "multiplayer_side" "music" "not" "num_units" "object"
247 "objectives" "objective" "observers" "option" "or" "panel" "part"
248 "place_shroud" "position" "print" "protect_location" "protect_unit" "race"
249 "random" "recall" "recalls" "recruit" "recruits" "redraw" "ref"
250 "remove_shroud" "remove_unit_overlay" "removeitem" "replay" "replay_start"
251 "resistance" "resolution" "results" "role" "save" "scenario" "scroll"
252 "scroll_to" "scroll_to_unit" "section" "set_menu_item" "set_recruit"
253 "set_variable" "show_if" "side" "side_playing" "snapshot" "sound" "source"
254 "specials" "statistics" "status" "stone" "store_gold" "store_locations"
255 "store_starting_location" "store_side" "store_unit" "story" "target" "team"
256 "teleport" "teleport_anim" "terrain" "terrain_graphics" "test" "text_input"
257 "textdomain" "theme" "then" "tile" "time" "time_area" "time_of_day" "topic"
258 "toplevel" "trait" "turn" "tutorial" "unhide_unit" "unit" "unit_abilities"
259 "unit_alignment" "unit_description" "unit_hp" "unit_image" "unit_level"
260 "unit_moves" "unit_overlay" "unit_profile" "unit_status" "unit_traits"
261 "unit_type" "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit"
262 "upkeep" "variable" "variables" "village" "villages" "while" "wml_filter")
263 "A list containing all tags which are available in WML.")
265 (defvar wesnoth-completion-cache
'()
266 "List of tags which have been generated by `wesnoth-build-completion'.")
268 (defun wesnoth-build-completion (&optional rebuild
)
269 "Create a new list for tag completion if necessary.
270 If REBUILD is non-nil, regenerate `wesnoth-completion-cache'."
272 (if (> emacs-major-version
21)
274 (if (and wesnoth-completion-cache
(not rebuild
))
275 wesnoth-completion-cache
278 (dolist (tag wesnoth-tags-list
)
279 (setq iter
(1+ iter
))
280 (setq tags
(append tags
(list (cons tag iter
)))))
281 (setq wesnoth-completion-cache tags
)))))
283 (defun wesnoth-insert-tag (tagname &optional start end
)
284 "Inserts the specified opening tag and it's matching closing tag.
285 Both the opening and closing tags will be placed on their own
286 lines with point positioned between them. Completion of tags at
287 the prompt uses `wesnoth-tags-list'.
289 TAGNAME is the name of the tag to be inserted. If START and END
290 are given, the tags will be inserted around the specified region.
291 Enabling `transient-mark-mode' will cause `wesnoth-insert-tag' to
292 insert opening and closing tags around the specified region."
294 (list (completing-read "Tag: " (wesnoth-build-completion) nil nil
)))
295 (when (and (not (or start end
)) transient-mark-mode mark-active
)
296 (setq start
(region-beginning)
297 end
(copy-marker (region-end))))
301 (or (looking-at "^[\t ]*$")
303 (insert "[" tagname
"]")
306 (if (looking-at "^[\t ]*$")
310 (insert "[/" tagname
"]")
311 (setq end
(point)) ;; New target for indent-region
312 (indent-region start end nil
))
314 (or (looking-at "^[\t ]*$")
319 (wesnoth-insert-and-indent "[" tagname
"]")
320 (wesnoth-insert-and-indent "\n")
322 (wesnoth-insert-and-indent "\n[/" tagname
"]"))))
324 (defun wesnoth-insert-missing-closing (&optional start end
)
325 "Insert the next expected closing element at point.
327 START and END define the region to check for missing closing
328 elements. If `transient-mark-mode' is enabled, the region
329 specified will be used as START and END. Otherwise, START and
330 END will be the minimum and maximum positions of the buffer,
333 (if (and transient-mark-mode mark-active
)
334 (setq start
(region-beginning)
335 end
(copy-marker (region-end)))
336 (setq start
(point-min)
338 (let ((element (wesnoth-check-structure start end
)))
340 (if (string= element
"Unexpected end of file")
341 (message "%s" "Error: Expected end of file")
342 (when (not (looking-at "^[\t ]*$"))
348 (error "%s" "Unable to find element to insert"))))
350 (defun wesnoth-insert-and-indent (&rest args
)
351 "Concatenate and insert the given string(s) before indenting."
352 (insert (apply 'concat args
))
356 (defun wesnoth-newline (&optional indent
)
357 "Indent both the current line and the newline created.
358 If `wesnoth-auto-indent-flag' is nil, indentation will not be
359 performed. Indentation can be forced by setting INDENT to
363 (when (and (or wesnoth-auto-indent-flag indent
)
364 (not (looking-at "^[\t ]*$")))
369 (defun wesnoth-forward-element (repeat)
370 "Move point to the end of the next tag.
371 REPEAT is an optional numeric argument. If REPEAT is non-nil,
372 jump forward the specified number of tags."
374 (or repeat
(setq repeat
1))
376 (wesnoth-backward-element (abs repeat
))
377 (let ((iterations 0))
378 (while (< iterations repeat
)
380 (search-forward-regexp
381 (wesnoth-element-opening t
)
383 (setq iterations
(1+ iterations
))))
386 (defun wesnoth-backward-element (repeat)
387 "Move point to the beginning of the previous tag.
388 REPEAT is an optional numeric argument. If REPEAT is non-nil,
389 jump backward the specified number of tags."
391 (or repeat
(setq repeat
1))
393 (wesnoth-forward-element (abs repeat
))
394 (let ((iterations 0))
395 (while (< iterations repeat
)
397 (search-backward-regexp
398 (wesnoth-element-opening t
)
401 (search-forward-regexp "[^[:blank:]]")
403 (setq iterations
(1+ iterations
))))))
405 (defun wesnoth-jump-to-matching ()
406 "Jump point to the matching opening/closing tag.
407 A tag must be on the same line as point for jumping to occur.
408 Tag structure between the start and target positions must be
409 consistent for jumping to occur."
414 (search-backward nil
))
419 (when (looking-at (wesnoth-element-closing t
))
420 (setq search-backward t
))
421 (if (wesnoth-wml-start-pos)
422 (if (> (point) (wesnoth-wml-start-pos))
423 (search-backward-regexp
426 (goto-char (point-min))
427 (search-forward-regexp
430 (error "%s" "Unable to locate tag to jump from")))
435 (or (< open-tags
0) (not search-started
))
436 (search-backward-regexp
439 (setq search-started t
)
440 (if (looking-at (wesnoth-element-opening t
))
441 (setq open-tags
(1+ open-tags
))
442 (when (looking-at (wesnoth-element-closing t
))
443 (setq open-tags
(1- open-tags
)))))
445 (search-backward-regexp "\\[\\|#"))
447 (or (> open-tags
0) (not search-started
))
448 (search-forward-regexp
452 (setq search-started t
)
453 (if (looking-at (wesnoth-element-opening t
))
454 (setq open-tags
(1+ open-tags
))
455 (when (looking-at (wesnoth-element-closing t
))
456 (setq open-tags
(1- open-tags
))))
459 (setq tag-position
(point)))
462 (and (wesnoth-check-structure (min (point) tag-position
)
463 (max (point) tag-position
))
464 (message "%s" "Region concerning jump does not nest correctly; \
465 target may not be correct"))
467 (goto-char tag-position
)
470 (defun wesnoth-wml-start-pos ()
471 "Determine the position of `point' relative to where the actual WML begins.
472 Return the likely starting position of the WML if it is found.
473 Otherwise return nil."
475 (goto-char (point-min))
476 (when (search-forward-regexp (wesnoth-element t
) (buffer-size) t
)
480 (defun wesnoth-indent ()
481 "Indent the current line as WML."
483 (if (or (not (wesnoth-wml-start-pos))
484 (<= (point) (wesnoth-wml-start-pos))
485 (nth 3 (parse-partial-sexp (point-min) (point)))
486 (and wesnoth-indent-preprocessor-bol
487 (looking-at wesnoth-preprocessor-regexp
)))
493 ((looking-at (wesnoth-element-closing))
494 (wesnoth-ensure-define-separate)
495 ;; If closing preceded by closing, decrease indentation
496 (when (and (looking-at (wesnoth-element-closing)))
497 (setq cur-indent
(- (current-indentation) wesnoth-base-indent
))))
499 ((looking-at (wesnoth-element-opening))
500 ;; Goto start-pos if unable to find target
501 (wesnoth-ensure-define-separate)
502 ;; If opening preceded by opening, increase indentation
503 (when (or (looking-at (concat (wesnoth-element-opening)))
505 (wesnoth-check-structure (wesnoth-wml-start-pos) (point))))
506 (setq cur-indent
(+ (current-indentation) wesnoth-base-indent
))))
507 ;; Not opening, not closing
509 (wesnoth-ensure-define-separate)
510 ;; Decrease indent if preceded by closing
511 (if wesnoth-indent-savefile
512 (and (looking-at (wesnoth-element-opening))
514 (+ (current-indentation) wesnoth-base-indent
)))
515 (and (looking-at (wesnoth-element-closing))
516 (setq cur-indent
(- (current-indentation)
517 wesnoth-base-indent
))))))
518 ;; Indentation change unnecessary
520 (setq cur-indent
(current-indentation))))
521 (and (< cur-indent
0) (setq cur-indent
0))
522 (unless (and (not cur-indent
) (= (current-indentation) cur-indent
))
523 (indent-line-to cur-indent
))))
525 (search-forward-regexp "[\t ]*"))
527 (defun wesnoth-newline-and-indent (&optional indent
)
528 "Indent both the current line and the newline created.
529 If `wesnoth-auto-indent-flag' is nil, indentation will not be
533 (when (or wesnoth-auto-indent-flag indent
)
536 (defun wesnoth-within-define ()
542 (unless (looking-at "#enddef")
544 (search-backward-regexp
545 "\\(^[\t ]*#define\\|#enddef\\)" (wesnoth-wml-start-pos) t
)
546 (if (looking-at "^[\t ]*#define")
547 (setq upper
(point)))
549 (search-forward-regexp
550 "#enddef" (point-max) t
)
552 (if (looking-at "#enddef")
553 (setq lower
(point))))
554 (if (and upper
(= upper
(wesnoth-wml-start-pos)))
558 (defun wesnoth-ensure-define-separate ()
559 "Ensure elements in #define blocks don't affect those outside."
560 (if (and (not (wesnoth-within-define)) wesnoth-indent-preprocessor-bol
)
562 (search-backward-regexp
564 (wesnoth-wml-start-pos) t
)
565 (while (and (wesnoth-within-define)
566 (not (= (point) (wesnoth-wml-start-pos))))
567 (search-backward-regexp
569 (wesnoth-wml-start-pos) 0)))
570 (search-backward-regexp
572 (wesnoth-wml-start-pos) 0)))
575 (defun wesnoth-check-tag-names ()
576 "Check the names of all tags in the buffer for correctness.
577 If a tag is found which is not present in the list an error will
578 be signalled and point will be moved to the corresponding
581 (let ((tag-position nil
)
582 (missing-tag-name nil
))
584 (goto-char (point-min))
586 (search-forward-regexp "^[\t ]*\\[" (point-max) t
)
589 (when (looking-at "^[\t ]*\\[[/]?\\(\\(\\(\\w\\|_\\)+\\|_\\)\\)\\]")
590 (unless (member (match-string-no-properties 1) wesnoth-tags-list
)
591 (setq tag-position
(point))
592 (setq missing-tag-name
(match-string-no-properties 1))))
596 (goto-char tag-position
)
598 (search-backward "[")
599 (message "'%s' is not known to exist"
601 (message "%s" "No unknown tag names found."))))
603 (defun wesnoth-check-structure (&optional start end
)
604 "Check the buffer for correct nesting of elements.
605 If a problem is found in the structure, point will be placed at
606 the location which an element was expected and the expected
607 element will be displayed in the mini-buffer.
609 START and END define the region to be checked. If
610 `transient-mark-mode' is enabled, the region specified will be
611 checked. Otherwise START and END will be the minimum and maximum
612 positions of the buffer, respectively."
614 (unless (or start end
)
615 (if (and transient-mark-mode mark-active
)
616 (setq start
(region-beginning)
617 end
(copy-marker (region-end)))
618 (setq start
(point-min)
620 (let ((unmatched-tag-list '())
625 (while (and (search-forward-regexp
628 (not error-position
))
629 (search-backward-regexp
632 (if (looking-at wesnoth-preprocessor-regexp
)
633 (let ((preprocessor-name (match-string-no-properties 1)))
638 (string= preprocessor-name preproc
))
639 '("define" "ifdef" "ifndef")))
640 (setq unmatched-tag-list
641 (cons preprocessor-name unmatched-tag-list
)))
642 ((string= preprocessor-name
"else")
643 (unless (string-match "ifn?def" (car unmatched-tag-list
))
644 (setq error-position
(point))))
645 ((string= preprocessor-name
"endif")
646 (if (string-match "ifn?def" (car unmatched-tag-list
))
647 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
648 (setq error-position
(point))))
649 ((string= preprocessor-name
"enddef")
650 (if (string= (car unmatched-tag-list
) "define")
651 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
652 (setq error-position
(point))))))
653 (if (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
654 (setq unmatched-tag-list
655 (cons (match-string-no-properties 1)
657 (when (looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
658 (if (string= (match-string-no-properties 1)
659 (car unmatched-tag-list
))
660 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
661 (setq error-position
(point))))))
663 (when unmatched-tag-list
664 (cond ((string= (car unmatched-tag-list
) "define")
665 (setq expected
"#enddef"))
666 ((string-match "ifn?def" (car unmatched-tag-list
))
667 (setq expected
"#endif"))
668 ((not unmatched-tag-list
)
669 (setq expected
"Unexpected end of file"))))
671 (if (not (or unmatched-tag-list error-position
))
672 (message "%s" "Structure appears consistent.")
674 (goto-char error-position
))
675 (if (string= expected
"Unexpected end of file")
676 (message "Error %s" expected
)
677 (message "Error: Expecting %s"
680 (concat " [/" (car unmatched-tag-list
) "]")))))
681 (when (or expected unmatched-tag-list
)
682 (or expected
(concat "[/" (car unmatched-tag-list
) "]"))))))
685 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
686 "Major mode for editing WML."
687 (wesnoth-preprocessor-best-face)
688 (set-syntax-table wesnoth-syntax-table
)
689 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
690 (set (make-local-variable 'comment-start
) "#")
691 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
692 (set (make-local-variable 'font-lock-defaults
)
693 '(wesnoth-font-lock-keywords
695 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
696 (setq indent-tabs-mode nil
)
697 (setq mode-name
"WML")
698 (run-hooks 'wesnoth-mode-hook
))
700 (provide 'wesnoth-mode
)