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