* wesnoth-mode.el (wesnoth-mode-version): Updated to 1.2.3+git.
[wesnoth-mode.git] / wesnoth-mode.el
blob7f602ff48c8034a2d609e52eb455d67c29d9d4d7
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.4
35 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
36 ;; 1.2.3
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'
43 ;; has been removed.
44 ;; * Trailing whitespace is no longer created when creating a second consecutive
45 ;; newline.
46 ;; * Spurious newlines are no longer created when inserting a tag elements
47 ;; around a region.
48 ;; 1.2.2
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
79 ;; the line.
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.
83 ;; 1.2.1
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"
91 :group 'languages
92 :prefix "wesnoth-")
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."
98 :type 'boolean
99 :group 'wesnoth-mode)
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
105 the same level."
106 :type 'boolean
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
112 indent the line."
113 :type 'boolean
114 :group 'wesnoth-mode)
116 (defcustom wesnoth-base-indent 4
117 "The number of columns to indent WML."
118 :type 'integer
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)
153 ;; Menu
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
181 (list
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
193 (list
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\\)"))
231 ;;; Insertion
232 (defvar wesnoth-tags-list
233 (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'."
271 (interactive "P")
272 (if (> emacs-major-version 21)
273 wesnoth-tags-list
274 (if (and wesnoth-completion-cache (not rebuild))
275 wesnoth-completion-cache
276 (let ((tags '())
277 (iter 0))
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."
293 (interactive
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))))
298 (if (and start end)
299 (progn
300 (goto-char start)
301 (or (looking-at "^[\t ]*$")
302 (open-line 1))
303 (insert "[" tagname "]")
304 (goto-char end)
305 (beginning-of-line)
306 (if (looking-at "^[\t ]*$")
307 (open-line 1)
308 (end-of-line)
309 (newline))
310 (insert "[/" tagname "]")
311 (setq end (point)) ;; New target for indent-region
312 (indent-region start end nil))
313 (beginning-of-line)
314 (or (looking-at "^[\t ]*$")
315 (progn
316 (end-of-line)
317 (newline)))
318 (end-of-line)
319 (wesnoth-insert-and-indent "[" tagname "]")
320 (wesnoth-insert-and-indent "\n")
321 (save-excursion
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,
331 respectively."
332 (interactive)
333 (if (and transient-mark-mode mark-active)
334 (setq start (region-beginning)
335 end (copy-marker (region-end)))
336 (setq start (point-min)
337 end (point-max)))
338 (let ((element (wesnoth-check-structure start end)))
339 (if element
340 (if (string= element "Unexpected end of file")
341 (message "%s" "Error: Expected end of file")
342 (when (not (looking-at "^[\t ]*$"))
343 (end-of-line)
344 (wesnoth-newline))
345 (insert element)
346 (wesnoth-indent)
347 (end-of-line))
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))
353 (wesnoth-indent)
354 (end-of-line))
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
360 non-nil."
361 (interactive)
362 (save-excursion
363 (when (and (or wesnoth-auto-indent-flag indent)
364 (not (looking-at "^[\t ]*$")))
365 (wesnoth-indent)))
366 (newline))
368 ;;; Movement
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."
373 (interactive "p")
374 (or repeat (setq repeat 1))
375 (if (< repeat 0)
376 (wesnoth-backward-element (abs repeat))
377 (let ((iterations 0))
378 (while (< iterations repeat)
379 (end-of-line)
380 (search-forward-regexp
381 (wesnoth-element-opening t)
382 (buffer-size) t)
383 (setq iterations (1+ iterations))))
384 (end-of-line)))
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."
390 (interactive "p")
391 (or repeat (setq repeat 1))
392 (if (< repeat 0)
393 (wesnoth-forward-element (abs repeat))
394 (let ((iterations 0))
395 (while (< iterations repeat)
396 (beginning-of-line)
397 (search-backward-regexp
398 (wesnoth-element-opening t)
399 0 t)
400 (unless (bobp)
401 (search-forward-regexp "[^[:blank:]]")
402 (backward-char))
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."
410 (interactive)
411 (let ((open-tags 0)
412 (search-started nil)
413 (tag-position nil)
414 (search-backward nil))
415 (save-excursion
416 (beginning-of-line)
417 (if (looking-at
418 (wesnoth-element t))
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
424 (wesnoth-element t)
425 (point-min) t)
426 (goto-char (point-min))
427 (search-forward-regexp
428 (wesnoth-element t))
429 (beginning-of-line))
430 (error "%s" "Unable to locate tag to jump from")))
431 (if search-backward
432 (progn
433 (end-of-line)
434 (while (and
435 (or (< open-tags 0) (not search-started))
436 (search-backward-regexp
437 (wesnoth-element t)
438 (point-min) t))
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)))))
444 (end-of-line)
445 (search-backward-regexp "\\[\\|#"))
446 (while (and
447 (or (> open-tags 0) (not search-started))
448 (search-forward-regexp
449 (wesnoth-element t)
450 (point-max) t))
451 (beginning-of-line)
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))))
457 (end-of-line))
458 (end-of-line))
459 (setq tag-position (point)))
460 (and search-backward
461 (end-of-line))
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"))
466 (if (interactive-p)
467 (goto-char tag-position)
468 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."
474 (save-excursion
475 (goto-char (point-min))
476 (when (search-forward-regexp (wesnoth-element t) (buffer-size) t)
477 (beginning-of-line)
478 (point))))
480 (defun wesnoth-indent ()
481 "Indent the current line as WML."
482 (beginning-of-line)
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)))
488 (indent-line-to 0)
489 (let ((cur-indent))
490 (save-excursion
491 (cond
492 ;; Closing 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))))
498 ;; Opening regexp
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)))
504 (not
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))
513 (setq cur-indent
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
519 (unless cur-indent
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))))
524 (beginning-of-line)
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
530 performed."
531 (interactive)
532 (wesnoth-newline)
533 (when (or wesnoth-auto-indent-flag indent)
534 (wesnoth-indent)))
536 (defun wesnoth-within-define ()
537 (interactive)
538 (let ((upper)
539 (lower))
540 (save-excursion
541 (beginning-of-line)
542 (unless (looking-at "#enddef")
543 (end-of-line))
544 (search-backward-regexp
545 "\\(^[\t ]*#define\\|#enddef\\)" (wesnoth-wml-start-pos) t)
546 (if (looking-at "^[\t ]*#define")
547 (setq upper (point)))
548 (end-of-line)
549 (search-forward-regexp
550 "#enddef" (point-max) t)
551 (beginning-of-line)
552 (if (looking-at "#enddef")
553 (setq lower (point))))
554 (if (and upper (= upper (wesnoth-wml-start-pos)))
556 (and upper lower))))
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)
561 (progn
562 (search-backward-regexp
563 (wesnoth-element)
564 (wesnoth-wml-start-pos) t)
565 (while (and (wesnoth-within-define)
566 (not (= (point) (wesnoth-wml-start-pos))))
567 (search-backward-regexp
568 (wesnoth-element)
569 (wesnoth-wml-start-pos) 0)))
570 (search-backward-regexp
571 (wesnoth-element)
572 (wesnoth-wml-start-pos) 0)))
574 ;;; WML checks
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
579 position."
580 (interactive)
581 (let ((tag-position nil)
582 (missing-tag-name nil))
583 (save-excursion
584 (goto-char (point-min))
585 (while (and
586 (search-forward-regexp "^[\t ]*\\[" (point-max) t)
587 (not tag-position))
588 (beginning-of-line)
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))))
593 (end-of-line)))
594 (if tag-position
595 (progn
596 (goto-char tag-position)
597 (end-of-line)
598 (search-backward "[")
599 (message "'%s' is not known to exist"
600 missing-tag-name))
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."
613 (interactive)
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)
619 end (point-max))))
620 (let ((unmatched-tag-list '())
621 (error-position nil)
622 (expected nil))
623 (save-excursion
624 (goto-char start)
625 (while (and (search-forward-regexp
626 (wesnoth-element t)
627 end t)
628 (not error-position))
629 (search-backward-regexp
630 (wesnoth-element t)
631 start t)
632 (if (looking-at wesnoth-preprocessor-regexp)
633 (let ((preprocessor-name (match-string-no-properties 1)))
634 (cond
635 ((member 't
636 (mapcar
637 '(lambda (preproc)
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)
656 unmatched-tag-list))
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))))))
662 (end-of-line)))
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"))))
670 (if (interactive-p)
671 (if (not (or unmatched-tag-list error-position))
672 (message "%s" "Structure appears consistent.")
673 (and error-position
674 (goto-char error-position))
675 (if (string= expected "Unexpected end of file")
676 (message "Error %s" expected)
677 (message "Error: Expecting %s"
679 expected
680 (concat " [/" (car unmatched-tag-list) "]")))))
681 (when (or expected unmatched-tag-list)
682 (or expected (concat "[/" (car unmatched-tag-list) "]"))))))
684 ;;; wesnoth-mode
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
694 nil t nil nil
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)