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