* wesnoth-mode.el (wesnoth-mode-version): Update version number.
[wesnoth-mode.git] / wesnoth-mode.el
blobe0df6cb383fd4708b41827d7759981758be5b835
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.3
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'
41 ;; has been removed.
42 ;; * Trailing whitespace is no longer created when creating a second consecutive
43 ;; newline.
44 ;; * Spurious newlines are no longer created when inserting a tag elements
45 ;; around a region.
46 ;; 1.2.2
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
77 ;; the line.
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.
81 ;; 1.2.1
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"
89 :group 'languages
90 :prefix "wesnoth-")
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."
96 :type 'boolean
97 :group 'wesnoth-mode)
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
103 the same level."
104 :type 'boolean
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
110 indent the line."
111 :type 'boolean
112 :group 'wesnoth-mode)
114 (defcustom wesnoth-base-indent 4
115 "The number of columns to indent WML."
116 :type 'integer
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)
151 ;; Menu
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
179 (list
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
191 (list
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"))
229 ;;; Insertion
230 (defvar wesnoth-tags-list
231 (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'."
269 (interactive "P")
270 (if (> emacs-major-version 21)
271 wesnoth-tags-list
272 (if (and wesnoth-completion-cache (not rebuild))
273 wesnoth-completion-cache
274 (let ((tags '())
275 (iter 0))
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."
291 (interactive
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))))
296 (if (and start end)
297 (progn
298 (goto-char start)
299 (or (looking-at "^[\t ]*$")
300 (open-line 1))
301 (insert "[" tagname "]")
302 (goto-char end)
303 (beginning-of-line)
304 (if (looking-at "^[\t ]*$")
305 (open-line 1)
306 (end-of-line)
307 (newline))
308 (insert "[/" tagname "]")
309 (setq end (point)) ;; New target for indent-region
310 (indent-region start end nil))
311 (beginning-of-line)
312 (or (looking-at "^[\t ]*$")
313 (progn
314 (end-of-line)
315 (newline)))
316 (end-of-line)
317 (wesnoth-insert-and-indent "[" tagname "]")
318 (wesnoth-insert-and-indent "\n")
319 (save-excursion
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,
329 respectively."
330 (interactive)
331 (if (and transient-mark-mode mark-active)
332 (setq start (region-beginning)
333 end (copy-marker (region-end)))
334 (setq start (point-min)
335 end (point-max)))
336 (let ((element (wesnoth-check-structure start end)))
337 (if element
338 (if (string= element "Unexpected end of file")
339 (message "%s" "Error: Expected end of file")
340 (when (not (looking-at "^[\t ]*$"))
341 (end-of-line)
342 (wesnoth-newline))
343 (insert element)
344 (wesnoth-indent)
345 (end-of-line))
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))
351 (wesnoth-indent)
352 (end-of-line))
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
358 non-nil."
359 (interactive)
360 (save-excursion
361 (when (and (or wesnoth-auto-indent-flag indent)
362 (not (looking-at "^[\t ]*$")))
363 (wesnoth-indent)))
364 (newline))
366 ;;; Movement
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."
371 (interactive "p")
372 (or repeat (setq repeat 1))
373 (if (< repeat 0)
374 (wesnoth-backward-element (abs repeat))
375 (let ((iterations 0))
376 (while (< iterations repeat)
377 (end-of-line)
378 (search-forward-regexp
379 (wesnoth-element-opening t)
380 (buffer-size) t)
381 (setq iterations (1+ iterations))))
382 (end-of-line)))
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."
388 (interactive "p")
389 (or repeat (setq repeat 1))
390 (if (< repeat 0)
391 (wesnoth-forward-element (abs repeat))
392 (let ((iterations 0))
393 (while (< iterations repeat)
394 (beginning-of-line)
395 (search-backward-regexp
396 (wesnoth-element-opening t)
397 0 t)
398 (unless (bobp)
399 (search-forward-regexp "[^[:blank:]]")
400 (backward-char))
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."
408 (interactive)
409 (let ((open-tags 0)
410 (search-started nil)
411 (tag-position nil)
412 (search-backward nil))
413 (save-excursion
414 (beginning-of-line)
415 (if (looking-at
416 (wesnoth-element t))
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
422 (wesnoth-element t)
423 (point-min) t)
424 (goto-char (point-min))
425 (search-forward-regexp
426 (wesnoth-element t))
427 (beginning-of-line))
428 (error "%s" "Unable to locate tag to jump from")))
429 (if search-backward
430 (progn
431 (end-of-line)
432 (while (and
433 (or (< open-tags 0) (not search-started))
434 (search-backward-regexp
435 (wesnoth-element t)
436 (point-min) t))
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)))))
442 (end-of-line)
443 (search-backward-regexp "\\[\\|#"))
444 (while (and
445 (or (> open-tags 0) (not search-started))
446 (search-forward-regexp
447 (wesnoth-element t)
448 (point-max) t))
449 (beginning-of-line)
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))))
455 (end-of-line))
456 (end-of-line))
457 (setq tag-position (point)))
458 (and search-backward
459 (end-of-line))
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"))
464 (if (interactive-p)
465 (goto-char tag-position)
466 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."
472 (save-excursion
473 (goto-char (point-min))
474 (when (search-forward-regexp (wesnoth-element t) (buffer-size) t)
475 (beginning-of-line)
476 (point))))
478 (defun wesnoth-indent ()
479 "Indent the current line as WML."
480 (beginning-of-line)
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)))
486 (indent-line-to 0)
487 (let ((cur-indent))
488 (save-excursion
489 (cond
490 ;; Closing 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))))
496 ;; Opening regexp
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)))
502 (not
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))
511 (setq cur-indent
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
517 (unless cur-indent
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))))
522 (beginning-of-line)
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
528 performed."
529 (interactive)
530 (wesnoth-newline)
531 (when (or wesnoth-auto-indent-flag indent)
532 (wesnoth-indent)))
534 (defun wesnoth-within-define ()
535 (interactive)
536 (let ((upper)
537 (lower))
538 (save-excursion
539 (beginning-of-line)
540 (unless (looking-at "#enddef")
541 (end-of-line))
542 (search-backward-regexp
543 "\\(^[\t ]*#define\\|#enddef\\)" (wesnoth-wml-start-pos) t)
544 (if (looking-at "^[\t ]*#define")
545 (setq upper (point)))
546 (end-of-line)
547 (search-forward-regexp
548 "#enddef" (point-max) t)
549 (beginning-of-line)
550 (if (looking-at "#enddef")
551 (setq lower (point))))
552 (if (and upper (= upper (wesnoth-wml-start-pos)))
554 (and upper lower))))
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)
559 (progn
560 (search-backward-regexp
561 (wesnoth-element)
562 (wesnoth-wml-start-pos) t)
563 (while (and (wesnoth-within-define)
564 (not (= (point) (wesnoth-wml-start-pos))))
565 (search-backward-regexp
566 (wesnoth-element)
567 (wesnoth-wml-start-pos) 0)))
568 (search-backward-regexp
569 (wesnoth-element)
570 (wesnoth-wml-start-pos) 0)))
572 ;;; WML checks
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
577 position."
578 (interactive)
579 (let ((tag-position nil)
580 (missing-tag-name nil))
581 (save-excursion
582 (goto-char (point-min))
583 (while (and
584 (search-forward-regexp "^[\t ]*\\[" (point-max) t)
585 (not tag-position))
586 (beginning-of-line)
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))))
591 (end-of-line)))
592 (if tag-position
593 (progn
594 (goto-char tag-position)
595 (end-of-line)
596 (search-backward "[")
597 (message "'%s' is not known to exist"
598 missing-tag-name))
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."
611 (interactive)
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)
617 end (point-max))))
618 (let ((unmatched-tag-list '())
619 (error-position nil)
620 (expected nil))
621 (save-excursion
622 (goto-char start)
623 (while (and (search-forward-regexp
624 (wesnoth-element t)
625 end t)
626 (not error-position))
627 (search-backward-regexp
628 (wesnoth-element t)
629 start t)
630 (if (looking-at wesnoth-preprocessor-regexp)
631 (let ((preprocessor-name (match-string-no-properties 1)))
632 (cond
633 ((member 't
634 (mapcar
635 '(lambda (preproc)
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)
654 unmatched-tag-list))
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))))))
660 (end-of-line)))
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"))))
668 (if (interactive-p)
669 (if (not (or unmatched-tag-list error-position))
670 (message "%s" "Structure appears consistent.")
671 (and error-position
672 (goto-char error-position))
673 (if (string= expected "Unexpected end of file")
674 (message "Error %s" expected)
675 (message "Error: Expecting %s"
677 expected
678 (concat " [/" (car unmatched-tag-list) "]")))))
679 (when (or expected unmatched-tag-list)
680 (or expected (concat "[/" (car unmatched-tag-list) "]"))))))
682 ;;; wesnoth-mode
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
692 nil t nil nil
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)