* wesnoth-mode.el (wesnoth-indent-preprocessor-bol, wesnoth-indent-savefile):
[wesnoth-mode.git] / wesnoth-mode.el
blob767cbaf00eb79e8e9090722404f460acd5f3daf8
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 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
42 ;; consectutive 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-tag)
135 (define-key wesnoth-mode-map "\C-\M-e" 'wesnoth-forward-tag)
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 ;; Separated from above to support multi-line macro calls.
193 '("}" . font-lock-function-name-face)
194 '("\\[[^]]+\\]" . font-lock-type-face)
195 '("\\$\\w+" . font-lock-variable-name-face)
196 '("\\(\\w+\\(\\,[\t ]*\\w+\\)*\\)="
197 1 font-lock-variable-name-face))
198 "Syntax highlighting for wesnoth-mode.")
200 (defvar wesnoth-tags-list
201 (list
202 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
203 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
204 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
205 "command" "deaths" "debug_message" "defend" "defends" "defense" "delay"
206 "destination" "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel"
207 "entry" "era" "event" "expenses" "filter" "filter_adjacent_location"
208 "filter_radius" "filter_second" "filter_vision" "format" "frame"
209 "game_config" "generator" "gold" "have_unit" "header" "hide_unit" "if"
210 "illuminated_time" "image" "img" "income" "italic" "item" "jump" "kill"
211 "killed" "label" "language" "leader_goal" "main_map" "menu" "message"
212 "mini_map" "missile_frame" "modifications" "modify_side" "modify_turns"
213 "move" "move_unit_fake" "movement_costs" "movetype" "multiplayer"
214 "multiplayer_side" "music" "not" "num_units" "object" "objectives"
215 "objective" "observers" "option" "or" "panel" "part" "place_shroud"
216 "position" "print" "protect_location" "protect_unit" "race" "random"
217 "recall" "recalls" "recruit" "recruits" "redraw" "ref" "remove_shroud"
218 "remove_unit_overlay" "removeitem" "replay" "replay_start" "resistance"
219 "resolution" "results" "role" "save" "scenario" "scroll" "scroll_to"
220 "scroll_to_unit" "section" "set_recruit" "set_variable" "side"
221 "side_playing" "snapshot" "sound" "source" "specials" "statistics" "status"
222 "stone" "store_gold" "store_locations" "store_starting_location"
223 "store_unit" "story" "target" "team" "teleport" "teleport_anim" "terrain"
224 "terrain_graphics" "test" "text_input" "textdomain" "theme" "then" "tile"
225 "time" "time_area" "time_of_day" "topic" "toplevel" "trait" "turn"
226 "tutorial" "unhide_unit" "unit" "unit_abilities" "unit_alignment"
227 "unit_description" "unit_hp" "unit_image" "unit_level" "unit_moves"
228 "unit_overlay" "unit_profile" "unit_status" "unit_traits" "unit_type"
229 "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit" "upkeep"
230 "variable" "variables" "village" "villages" "while")
231 "A list containing all tags which are available for use in WML.")
233 (defun wesnoth-element-closing ()
234 "Return string to use for a closing element."
235 (if wesnoth-indent-preprocessor-bol
236 "^[ \t]*\\(\\[/.+\\]\\|#enddef\\)"
237 (concat "^[ \t]*\\(\\[/.+\\]\\|\\("
238 wesnoth-preprocessor-closing-regexp "\\)\\)")))
240 (defun wesnoth-element-opening ()
241 "Return string to use for an opening element."
242 (if wesnoth-indent-preprocessor-bol
243 "^[ \t]*\\(\\[[^/]*?\\]\\|#define\\)"
244 (concat "^[ \t]*\\(\\[[^/]*?\\]\\|\\("
245 wesnoth-preprocessor-opening-regexp "\\)\\)")))
247 (defun wesnoth-element ()
248 "Return string to use for an opening or closing element."
249 (if wesnoth-indent-preprocessor-bol
250 "^[\t ]*\\(\\[\\|#define\\|#enddef\\)"
251 (concat "^[\t ]*\\(\\[\\|"
252 wesnoth-preprocessor-regexp "\\)")))
254 ;;; Insertion
255 (defun wesnoth-insert-tag (tagname &optional start end)
256 "Inserts the specified opening tag and it's matching closing tag.
257 Both the opening and closing tags will be placed on their own
258 lines with point positioned between them. Completion of tags at
259 the prompt uses `wesnoth-tags-list'.
261 TAGNAME is the name of the tag to be inserted. If START and END
262 are given, the tags will be inserted around the specified region.
263 Enabling `transient-mark-mode' will cause `wesnoth-insert-tag' to
264 insert opening and closing tags around the specified region."
265 (interactive
266 (list (completing-read "Tag: " wesnoth-tags-list nil nil)))
267 (when (and (not (or start end)) transient-mark-mode mark-active)
268 (setq start (region-beginning)
269 end (copy-marker (region-end))))
270 (if (and start end)
271 (progn
272 (goto-char start)
273 (or (looking-at "^[\t ]*$")
274 (open-line 1))
275 (insert "[" tagname "]")
276 (goto-char end)
277 (or (looking-back "^[\t ]*$")
278 (newline))
279 (insert "[/" tagname "]\n")
280 (setq end (point)) ;; New target for indent-region
281 (indent-region start end))
282 (or (looking-back "^[\t ]*$")
283 (newline))
284 (wesnoth-insert-and-indent "[" tagname "]")
285 (wesnoth-insert-and-indent "\n")
286 (save-excursion
287 (wesnoth-insert-and-indent "\n[/" tagname "]"))))
289 (defun wesnoth-insert-missing-closing (&optional start end)
290 "Insert the next expected closing element at point.
292 START and END define the region to check for missing closing
293 elements. If `transient-mark-mode' is enabled, the region
294 specified will be used as START and END. Otherwise, START and
295 END will be the minimum and maximum positions of the buffer,
296 respectively."
297 (interactive)
298 (if (and transient-mark-mode mark-active)
299 (setq start (region-beginning)
300 end (copy-marker (region-end)))
301 (setq start (point-min)
302 end (point-max)))
303 (let ((element (wesnoth-check-structure start end)))
304 (if element
305 (if (string= element "Unexpected end of file")
306 (message "%s" "Error: Expected end of file")
307 (when (not (looking-at "^[\t ]*$"))
308 (end-of-line)
309 (wesnoth-newline))
310 (insert element)
311 (wesnoth-indent)
312 (end-of-line))
313 (error "%s" "Unable to find element to insert"))))
315 (defun wesnoth-insert-and-indent (&rest args)
316 "Concatenate and insert the given string(s) before indenting."
317 (insert (apply 'concat args))
318 (wesnoth-indent)
319 (end-of-line))
321 (defun wesnoth-newline (&optional indent)
322 "Indent both the current line and the newline created.
323 If `wesnoth-auto-indent-flag' is nil, indentation will not be
324 performed."
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-tag (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-tag (abs repeat))
341 (let ((iterations 0))
342 (while (< iterations repeat)
343 (end-of-line)
344 (search-forward-regexp
345 (wesnoth-element-opening)
346 (buffer-size) t)
347 (setq iterations (1+ iterations))))
348 (end-of-line)))
350 (defun wesnoth-backward-tag (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-tag (abs repeat))
358 (let ((iterations 0))
359 (while (< iterations repeat)
360 (beginning-of-line)
361 (search-backward-regexp
362 (wesnoth-element-opening)
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))
383 (when (looking-at (wesnoth-element-closing))
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)
389 (point-min) t)
390 (goto-char (point-min))
391 (search-forward-regexp
392 (wesnoth-element))
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)
402 (point-min) t))
403 (setq search-started t)
404 (if (looking-at (wesnoth-element-opening))
405 (setq open-tags (1+ open-tags))
406 (when (looking-at (wesnoth-element-closing))
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)
414 (point-max) t))
415 (beginning-of-line)
416 (setq search-started t)
417 (if (looking-at (wesnoth-element-opening))
418 (setq open-tags (1+ open-tags))
419 (when (looking-at (wesnoth-element-closing))
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
441 (concat "^[\t ]*\\(\\[\\|" wesnoth-preprocessor-regexp "\\)")
442 (buffer-size) t)
443 (beginning-of-line)
444 (point))))
446 (defun wesnoth-indent ()
447 "Indent the current line as WML."
448 (beginning-of-line)
449 (if (or (not (wesnoth-wml-start-pos))
450 (<= (point) (wesnoth-wml-start-pos))
451 (nth 3 (syntax-ppss (point)))
452 (and wesnoth-indent-preprocessor-bol
453 (looking-at (concat "^[\t ]*" wesnoth-preprocessor-regexp))))
454 (indent-line-to 0)
455 (let ((cur-indent))
456 (save-excursion
457 (cond
458 ;; Closing regexp
459 ((looking-at (wesnoth-element-closing))
460 (wesnoth-ensure-define-separate)
461 ;; If closing preceded by closing, decrease indentation
462 (when (and (looking-at (wesnoth-element-closing)))
463 (setq cur-indent (- (current-indentation) wesnoth-base-indent))))
464 ;; Opening regexp
465 ((looking-at (wesnoth-element-opening))
466 ;; Goto start-pos if unable to find target
467 (wesnoth-ensure-define-separate)
468 ;; If opening preceded by opening, increase indentation
469 (when (or (looking-at (concat (wesnoth-element-opening)))
470 (not
471 (wesnoth-check-structure (wesnoth-wml-start-pos) (point))))
472 (setq cur-indent (+ (current-indentation) wesnoth-base-indent))))
473 ;; Not opening, not closing
475 (wesnoth-ensure-define-separate)
476 ;; Decrease indent if preceded by closing
477 (if wesnoth-indent-savefile
478 (and (looking-at (wesnoth-element-opening))
479 (setq cur-indent
480 (+ (current-indentation) wesnoth-base-indent)))
481 (and (looking-at (wesnoth-element-closing))
482 (setq cur-indent (- (current-indentation)
483 wesnoth-base-indent))))))
484 ;; Indentation change unnecessary
485 (unless cur-indent
486 (setq cur-indent (current-indentation))))
487 (and (< cur-indent 0) (setq cur-indent 0))
488 (unless (and (not cur-indent) (= (current-indentation) cur-indent))
489 (indent-line-to cur-indent))))
490 (beginning-of-line)
491 (search-forward-regexp "[\t ]*"))
493 (defun wesnoth-newline-and-indent (&optional indent)
494 "Indent both the current line and the newline created.
495 If `wesnoth-auto-indent-flag' is nil, indentation will not be
496 performed."
497 (interactive)
498 (wesnoth-newline)
499 (when (or wesnoth-auto-indent-flag indent)
500 (wesnoth-indent)))
502 (defun wesnoth-within-define ()
503 (interactive)
504 (let ((upper)
505 (lower))
506 (save-excursion
507 (end-of-line)
508 (when (looking-back "^[\t ]*#enddef")
509 (beginning-of-line))
510 (search-backward-regexp
511 "^[\t ]*\\(#define\\|#enddef\\)" (wesnoth-wml-start-pos) t)
512 (if (looking-at "^[\t ]*#define")
513 (setq upper (point)))
514 (end-of-line)
515 (search-forward-regexp
516 "^[\t ]*#enddef" (point-max) t)
517 (beginning-of-line)
518 (if (looking-at "^[\t ]*#enddef")
519 (setq lower (point))))
520 (if (and upper (= upper (wesnoth-wml-start-pos)))
522 (and upper lower))))
524 (defun wesnoth-ensure-define-separate ()
525 "Ensure elements in #define blocks don't affect those outside."
526 (if (and (not (wesnoth-within-define)) wesnoth-indent-preprocessor-bol)
527 (progn
528 (search-backward-regexp
529 (wesnoth-element)
530 (wesnoth-wml-start-pos) t)
531 (while (and (wesnoth-within-define)
532 (not (= (point) (wesnoth-wml-start-pos))))
533 (search-backward-regexp
534 (wesnoth-element)
535 (wesnoth-wml-start-pos) 'start-pos)))
536 (search-backward-regexp
537 (wesnoth-element)
538 (wesnoth-wml-start-pos) 0)))
540 ;;; WML checks
541 (defun wesnoth-check-tag-names ()
542 "Check the names of all tags in the buffer for correctness.
543 If a tag is found which is not present in the list an error will
544 be signalled and point will be moved to the corresponding
545 position."
546 (interactive)
547 (let ((tag-position nil)
548 (missing-tag-name nil))
549 (save-excursion
550 (goto-char (point-min))
551 (while (and
552 (search-forward-regexp "^[\t ]*\\[" (point-max) t)
553 (not tag-position))
554 (beginning-of-line)
555 (when (looking-at "^[\t ]*\\[[/]?\\(\\w+\\)\\]")
556 (unless (member (match-string-no-properties 1) wesnoth-tags-list)
557 (setq tag-position (point))
558 (setq missing-tag-name (match-string-no-properties 1))))
559 (end-of-line)))
560 (if tag-position
561 (progn
562 (goto-char tag-position)
563 (end-of-line)
564 (search-backward "[")
565 (message "'%s' is not known to exist"
566 missing-tag-name))
567 (message "%s" "No unknown tag names found."))))
569 (defun wesnoth-check-structure (&optional start end)
570 "Check the buffer for correct nesting of elements.
571 If a problem is found in the structure, point will be placed at
572 the location which an element was expected and the expected
573 element will be displayed in the mini-buffer.
575 START and END define the region to be checked. If
576 `transient-mark-mode' is enabled, the region specified will be
577 checked. Otherwise START and END will be the minimum and maximum
578 positions of the buffer, respectively."
579 (interactive)
580 (unless (or start end)
581 (if (and transient-mark-mode mark-active)
582 (setq start (region-beginning)
583 end (copy-marker (region-end)))
584 (setq start (point-min)
585 end (point-max))))
586 (let ((unmatched-tag-list '())
587 (error-position nil)
588 (expected nil))
589 (save-excursion
590 (goto-char start)
591 (while (and (search-forward-regexp (wesnoth-element) end t)
592 (not error-position))
593 (beginning-of-line)
594 (if (looking-at "^[\t ]*#\\(\\w+\\)")
595 (let ((preprocessor-name (match-string-no-properties 1)))
596 (cond
597 ((member 't
598 (mapcar
599 '(lambda (preproc)
600 (string= preprocessor-name preproc))
601 '("define" "ifdef" "ifndef")))
602 (setq unmatched-tag-list
603 (cons preprocessor-name unmatched-tag-list)))
604 ((string= preprocessor-name "else")
605 (unless (string-match "ifn?def" (car unmatched-tag-list))
606 (setq error-position (point))))
607 ((string= preprocessor-name "endif")
608 (if (string-match "ifn?def" (car unmatched-tag-list))
609 (setq unmatched-tag-list (cdr unmatched-tag-list))
610 (setq error-position (point))))
611 ((string= preprocessor-name "enddef")
612 (if (string= (car unmatched-tag-list) "define")
613 (setq unmatched-tag-list (cdr unmatched-tag-list))
614 (setq error-position (point))))))
615 (if (looking-at "^[\t ]*\\[\\+?\\(\\w+\\)\\]")
616 (setq unmatched-tag-list
617 (cons (match-string-no-properties 1)
618 unmatched-tag-list))
619 (when (looking-at "^[\t ]*\\[/\\(\\w+\\)\\]")
620 (if (string= (match-string-no-properties 1)
621 (car unmatched-tag-list))
622 (setq unmatched-tag-list (cdr unmatched-tag-list))
623 (setq error-position (point))))))
624 (end-of-line)))
625 (when unmatched-tag-list
626 (cond ((string= (car unmatched-tag-list) "define")
627 (setq expected "#enddef"))
628 ((string-match "ifn?def" (car unmatched-tag-list))
629 (setq expected "#endif"))
630 ((not unmatched-tag-list)
631 (setq expected "Unexpected end of file"))))
632 (if (interactive-p)
633 (if (not (or unmatched-tag-list error-position))
634 (message "%s" "Structure appears consistent.")
635 (and error-position
636 (goto-char error-position))
637 (if (string= expected "Unexpected end of file")
638 (message "Error %s" expected)
639 (message "Error: Expecting %s"
641 expected
642 (concat " [/" (car unmatched-tag-list) "]")))))
643 (when (or expected unmatched-tag-list)
644 (or expected (concat "[/" (car unmatched-tag-list) "]"))))))
646 ;;; wesnoth-mode
647 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
648 "Major mode for editing WML."
649 (set-syntax-table wesnoth-syntax-table)
650 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
651 (set (make-local-variable 'comment-start) "#")
652 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
653 (set (make-local-variable 'font-lock-defaults)
654 '(wesnoth-font-lock-keywords
655 nil t nil nil
656 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
657 (setq indent-tabs-mode nil)
658 (setq mode-name "WML")
659 (run-hooks 'wesnoth-mode-hook))
661 (provide 'wesnoth-mode)