* wesnoth-mode.el (wesnoth-tags-list): Added debug_message.
[wesnoth-mode.git] / wesnoth-mode.el
blob4a31457e6b22d3ec6e65965f9e3b140f376e5eb6
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" "debug_message" "defend" "defends" "defense" "delay"
178 "destination" "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel"
179 "entry" "era" "event" "expenses" "filter" "filter_adjacent_location"
180 "filter_radius" "filter_second" "filter_vision" "format" "frame"
181 "game_config" "generator" "gold" "have_unit" "header" "hide_unit" "if"
182 "illuminated_time" "image" "img" "income" "italic" "item" "jump" "kill"
183 "killed" "label" "language" "leader_goal" "main_map" "menu" "message"
184 "mini_map" "missile_frame" "modifications" "modify_side" "modify_turns"
185 "move" "move_unit_fake" "movement_costs" "movetype" "multiplayer"
186 "multiplayer_side" "music" "not" "num_units" "object" "objectives"
187 "objective" "observers" "option" "or" "panel" "part" "place_shroud"
188 "position" "print" "protect_location" "protect_unit" "race" "random"
189 "recall" "recalls" "recruit" "recruits" "redraw" "ref" "remove_shroud"
190 "remove_unit_overlay" "removeitem" "replay" "replay_start" "resistance"
191 "resolution" "results" "role" "save" "scenario" "scroll" "scroll_to"
192 "scroll_to_unit" "section" "set_recruit" "set_variable" "side"
193 "side_playing" "snapshot" "sound" "source" "specials" "statistics" "status"
194 "stone" "store_gold" "store_locations" "store_starting_location"
195 "store_unit" "story" "target" "team" "teleport" "teleport_anim" "terrain"
196 "terrain_graphics" "test" "text_input" "textdomain" "theme" "then" "tile"
197 "time" "time_area" "time_of_day" "topic" "toplevel" "trait" "turn"
198 "tutorial" "unhide_unit" "unit" "unit_abilities" "unit_alignment"
199 "unit_description" "unit_hp" "unit_image" "unit_level" "unit_moves"
200 "unit_overlay" "unit_profile" "unit_status" "unit_traits" "unit_type"
201 "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit" "upkeep"
202 "variable" "variables" "village" "villages" "while")
203 "A list containing all tags which are available for use in WML.")
205 (defun wesnoth-element-closing (&optional preproc-bol)
206 "Return string to use for closing indentation element.
208 If PREPROC-BOL is non-nil, return regexp for closing tags and
209 #enddef, otherwise return all closing elements."
210 (if preproc-bol
211 "^[ \t]*\\(\\[/.+\\]\\|#enddef\\)"
212 (concat "^[ \t]*\\(\\[/.+\\]\\|\\("
213 wesnoth-preprocessor-closing-regexp "\\)\\)")))
215 (defun wesnoth-element-opening (&optional preproc-bol)
216 "Return string to use for opening indentation element.
218 If PREPROC-BOL is non-nil, return regexp for opening tags and
219 #define, otherwise return all opening elements."
220 (if preproc-bol
221 "^[ \t]*\\(\\[[^/]*?\\]\\|#define\\)"
222 (concat "^[ \t]*\\(\\[[^/]*?\\]\\|\\("
223 wesnoth-preprocessor-opening-regexp "\\)\\)")))
225 (defun wesnoth-element (&optional preproc-bol)
226 "Return string to use for an opening or closing indentation element.
228 If PREPROC-BOL is non-nil, return regexp for tags, #enddef and
229 #define, otherwise return all opening and closing elements."
230 (if preproc-bol
231 "^[\t ]*\\(\\[\\|#define\\|#enddef\\)"
232 (concat "^[\t ]*\\(\\[\\|"
233 wesnoth-preprocessor-regexp "\\)")))
235 ;;; Insertion
236 (defun wesnoth-insert-tag (tagname &optional start end)
237 "Inserts the specified opening tag and it's matching closing tag.
238 Both the opening and closing tags will be placed on their own
239 lines with point positioned between them. Completion of tags at
240 the prompt uses `wesnoth-tags-list'.
242 TAGNAME is the name of the tag to be inserted. If START and END
243 are given, the tags will be inserted around the specified region.
244 Enabling `transient-mark-mode' will cause `wesnoth-insert-tag' to
245 insert opening and closing tags around the specified region."
246 (interactive
247 (list (completing-read "Tag: " wesnoth-tags-list nil nil)))
248 (when (and (not (or start end)) transient-mark-mode mark-active)
249 (setq start (region-beginning)
250 end (copy-marker (region-end))))
251 (if (and start end)
252 (progn
253 (goto-char start)
254 (or (looking-at "^[\t ]*$")
255 (open-line 1))
256 (insert "[" tagname "]")
257 (goto-char end)
258 (or (looking-back "^[\t ]*$")
259 (newline))
260 (insert "[/" tagname "]\n")
261 (setq end (point)) ;; New target for indent-region
262 (indent-region start end))
263 (or (looking-back "^[\t ]*$")
264 (newline))
265 (wesnoth-insert-and-indent "[" tagname "]")
266 (wesnoth-insert-and-indent "\n")
267 (save-excursion
268 (wesnoth-insert-and-indent "\n[/" tagname "]"))))
270 (defun wesnoth-insert-missing-closing (&optional start end)
271 "Insert the next expected closing element at point.
273 START and END define the region to check for missing closing
274 elements. If `transient-mark-mode' is enabled, the region
275 specified will be used as START and END. Otherwise, START and
276 END will be the minimum and maximum positions of the buffer,
277 respectively."
278 (interactive)
279 (if (and transient-mark-mode mark-active)
280 (setq start (region-beginning)
281 end (copy-marker (region-end)))
282 (setq start (point-min)
283 end (point-max)))
284 (let ((element (wesnoth-check-structure start end)))
285 (if element
286 (if (string= element "Unexpected end of file")
287 (message "%s" "Error: Expected end of file")
288 (when (not (looking-at "^[\t ]*$"))
289 (end-of-line)
290 (wesnoth-newline))
291 (insert element)
292 (wesnoth-indent-line)
293 (end-of-line))
294 (error "%s" "Unable to find element to insert"))))
296 (defun wesnoth-insert-and-indent (&rest args)
297 "Concatenate and insert the given string(s) before indenting."
298 (insert (apply 'concat args))
299 (wesnoth-indent-line)
300 (end-of-line))
302 (defun wesnoth-newline (&optional indent)
303 "Indent both the current line and the newline created.
304 If `wesnoth-auto-indent-flag' is nil, indentation will not be
305 performed."
306 (interactive)
307 (save-excursion
308 (when (or wesnoth-auto-indent-flag indent)
309 (wesnoth-indent-line)))
310 (newline))
312 ;;; Movement
313 (defun wesnoth-forward-tag (repeat)
314 "Move point to the end of the next tag.
315 REPEAT is an optional numeric argument. If REPEAT is non-nil,
316 jump forward the specified number of tags."
317 (interactive "p")
318 (or repeat (setq repeat 1))
319 (if (< repeat 0)
320 (wesnoth-backward-tag (abs repeat))
321 (let ((iterations 0))
322 (while (< iterations repeat)
323 (end-of-line)
324 (search-forward-regexp
325 (wesnoth-element-opening)
326 (buffer-size) t)
327 (setq iterations (1+ iterations))))
328 (end-of-line)))
330 (defun wesnoth-backward-tag (repeat)
331 "Move point to the beginning of the previous tag.
332 REPEAT is an optional numeric argument. If REPEAT is non-nil,
333 jump backward the specified number of tags."
334 (interactive "p")
335 (or repeat (setq repeat 1))
336 (if (< repeat 0)
337 (wesnoth-forward-tag (abs repeat))
338 (let ((iterations 0))
339 (while (< iterations repeat)
340 (beginning-of-line)
341 (search-backward-regexp
342 (wesnoth-element-opening)
343 0 t)
344 (unless (bobp)
345 (search-forward-regexp "[^[:blank:]]")
346 (backward-char))
347 (setq iterations (1+ iterations))))))
349 (defun wesnoth-jump-to-matching ()
350 "Jump point to the matching opening/closing tag.
351 A tag must be on the same line as point for jumping to occur.
352 Tag structure between the start and target positions must be
353 consistent for jumping to occur."
354 (interactive)
355 (let ((open-tags 0)
356 (search-started nil)
357 (tag-position nil)
358 (search-backward nil))
359 (save-excursion
360 (beginning-of-line)
361 (if (looking-at
362 (wesnoth-element))
363 (when (looking-at (wesnoth-element-closing))
364 (setq search-backward t))
365 (if (wesnoth-wml-start-pos)
366 (if (> (point) (wesnoth-wml-start-pos))
367 (search-backward-regexp
368 (wesnoth-element)
369 (point-min) t)
370 (goto-char (point-min))
371 (search-forward-regexp
372 (wesnoth-element))
373 (beginning-of-line))
374 (error "%s" "Unable to locate tag to jump from")))
375 (if search-backward
376 (progn
377 (end-of-line)
378 (while (and
379 (or (< open-tags 0) (not search-started))
380 (search-backward-regexp
381 (wesnoth-element)
382 (point-min) t))
383 (setq search-started t)
384 (if (looking-at (wesnoth-element-opening))
385 (setq open-tags (1+ open-tags))
386 (when (looking-at (wesnoth-element-closing))
387 (setq open-tags (1- open-tags)))))
388 (end-of-line)
389 (search-backward-regexp "\\[\\|#"))
390 (while (and
391 (or (> open-tags 0) (not search-started))
392 (search-forward-regexp
393 (wesnoth-element)
394 (point-max) t))
395 (beginning-of-line)
396 (setq search-started t)
397 (if (looking-at (wesnoth-element-opening))
398 (setq open-tags (1+ open-tags))
399 (when (looking-at (wesnoth-element-closing))
400 (setq open-tags (1- open-tags))))
401 (end-of-line))
402 (end-of-line))
403 (setq tag-position (point)))
404 (and search-backward
405 (end-of-line))
406 (and (wesnoth-check-structure (min (point) tag-position)
407 (max (point) tag-position))
408 (message "%s" "Region concerning jump does not nest correctly; \
409 target may not be correct"))
410 (if (interactive-p)
411 (goto-char tag-position)
412 tag-position)))
414 ;;; Indentation
415 (defun wesnoth-indent-line ()
416 "Determine and performs indentation on the current line.
417 The Indentation style can be customised by modifying
418 `wesnoth-indentation-function'."
419 (interactive)
420 (funcall wesnoth-indentation-function))
422 (defun wesnoth-wml-start-pos ()
423 "Determine the position of `point' relative to where the actual WML begins.
424 Return the likely starting position of the WML if it is found.
425 Otherwise return nil."
426 (save-excursion
427 (goto-char (point-min))
428 (when (search-forward-regexp (wesnoth-element nil)
429 (buffer-size) t)
430 (beginning-of-line)
431 (point))))
433 (defun wesnoth-indent (savefile preproc-bol)
434 "Indent the current line as WML.
435 If SAVEFILE is non-nil, use savefile-style indentation,
436 otherwise use default style indentation.
437 If PREPROC-BOL is non-nil, indent preprocessor statements
438 to the first column, otherwise indent them in-line with any
439 surrounding tags."
440 (beginning-of-line)
441 (if (or (not (wesnoth-wml-start-pos))
442 (<= (point) (wesnoth-wml-start-pos))
443 (nth 3 (syntax-ppss (point)))
444 (and preproc-bol (looking-at
445 (concat "^[\t ]*" wesnoth-preprocessor-regexp)))
446 (not (wesnoth-check-structure (wesnoth-wml-start-pos) (point))))
447 (indent-line-to 0)
448 (let ((cur-indent))
449 (save-excursion
450 (cond
451 ;; Closing regexp
452 ((looking-at (wesnoth-element-closing preproc-bol))
453 (wesnoth-ensure-define-separate preproc-bol)
454 ;; If closing preceded by closing, decrease indentation
455 (when (and (looking-at (wesnoth-element-closing preproc-bol)))
456 (setq cur-indent (- (current-indentation) wesnoth-base-indent))))
457 ;; Opening regexp
458 ((looking-at (wesnoth-element-opening preproc-bol))
459 ;; Goto start-pos if unable to find target
460 (wesnoth-ensure-define-separate preproc-bol)
461 ;; If opening preceded by opening, increase indentation
462 (when (or (looking-at (concat (wesnoth-element-opening preproc-bol)))
463 (not
464 (wesnoth-check-structure (wesnoth-wml-start-pos) (point))))
465 (setq cur-indent (+ (current-indentation) wesnoth-base-indent))))
466 ;; Not opening, not closing
468 (wesnoth-ensure-define-separate preproc-bol)
469 ;; Decrease indent if preceded by closing
470 (if savefile
471 (and (looking-at (wesnoth-element-opening preproc-bol))
472 (setq cur-indent
473 (+ (current-indentation) wesnoth-base-indent)))
474 (and (looking-at (wesnoth-element-closing preproc-bol))
475 (setq cur-indent (- (current-indentation)
476 wesnoth-base-indent))))))
477 ;; Indentation change unnecessary
478 (unless cur-indent
479 (setq cur-indent (current-indentation))))
480 (and (< cur-indent 0) (setq cur-indent 0))
481 (unless (and (not cur-indent) (= (current-indentation) cur-indent))
482 (indent-line-to cur-indent))))
483 (beginning-of-line)
484 (search-forward-regexp "[\t ]*"))
486 (defun wesnoth-indent-withtags-inline ()
487 "Indent the current line using withtags-inline-style indentation.
488 Preprocessor statements will be indented as tags."
489 (wesnoth-indent nil nil))
491 (defun wesnoth-indent-default-inline ()
492 "Indent the current line using default-inline-style indentation.
493 Preprocessor statements will be indented as tags."
494 (wesnoth-indent t nil))
496 (defun wesnoth-indent-withtags ()
497 "Indent the current line using withtags-style indentation.
498 Preprocessor statements will be indented to the first column."
499 (wesnoth-indent nil t))
501 (defun wesnoth-indent-default ()
502 "Indent the current line using default-style indentation.
503 Preprocessor statements will be indented to the first column."
504 (wesnoth-indent t t))
506 (defun wesnoth-newline-and-indent (&optional indent)
507 "Indent both the current line and the newline created.
508 If `wesnoth-auto-indent-flag' is nil, indentation will not be
509 performed."
510 (interactive)
511 (wesnoth-newline)
512 (when (or wesnoth-auto-indent-flag indent)
513 (wesnoth-indent-line)))
515 (defun wesnoth-within-define ()
516 (interactive)
517 (let ((upper)
518 (lower))
519 (save-excursion
520 (search-backward-regexp
521 "^[\t ]*\\(#define\\|#enddef\\)" (wesnoth-wml-start-pos) t)
522 (if (looking-at "^[\t ]*#define")
523 (setq upper (point)))
524 (end-of-line)
525 (search-forward-regexp
526 "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t)
527 (beginning-of-line)
528 (if (looking-at "^[\t ]*#enddef")
529 (setq lower (point))))
530 (and
531 (and upper (> upper 0))
532 (and lower (> lower 0)))))
534 (defun wesnoth-ensure-define-separate (&optional preproc-bol)
535 "Ensure elements in #define blocks don't affect those outside.
537 If PREPROC-BOL is non-nil, ensure elements outside of
538 #define... #enddef ignore the structure for determining
539 indentation."
540 (if (and (not (wesnoth-within-define)) preproc-bol)
541 (progn
542 (search-backward-regexp
543 (wesnoth-element preproc-bol)
544 (wesnoth-wml-start-pos) t)
545 (while (and (wesnoth-within-define)
546 (not (= (point) (wesnoth-wml-start-pos))))
547 (search-backward-regexp
548 (wesnoth-element preproc-bol)
549 (wesnoth-wml-start-pos) 'start-pos)))
550 (search-backward-regexp
551 (wesnoth-element preproc-bol)
552 (wesnoth-wml-start-pos) 0)))
554 ;;; WML checks
555 (defun wesnoth-check-tag-names ()
556 "Check the names of all tags in the buffer for correctness.
557 If a tag is found which is not present in the list an error will
558 be signalled and point will be moved to the corresponding
559 position."
560 (interactive)
561 (let ((tag-position nil)
562 (missing-tag-name nil))
563 (save-excursion
564 (goto-char (point-min))
565 (while (and
566 (search-forward-regexp "^[\t ]*\\[" (point-max) t)
567 (not tag-position))
568 (beginning-of-line)
569 (when (looking-at "^[\t ]*\\[[/]?\\(\\w+\\)\\]")
570 (unless (member (match-string-no-properties 1) wesnoth-tags-list)
571 (setq tag-position (point))
572 (setq missing-tag-name (match-string-no-properties 1))))
573 (end-of-line)))
574 (if tag-position
575 (progn
576 (goto-char tag-position)
577 (end-of-line)
578 (search-backward "[")
579 (message "'%s' is not known to exist"
580 missing-tag-name))
581 (message "%s" "No unknown tag names found."))))
583 (defun wesnoth-check-structure (&optional start end)
584 "Check the buffer for correct nesting of elements.
585 If a problem is found in the structure, point will be placed at
586 the location which an element was expected and the expected
587 element will be displayed in the mini-buffer.
589 START and END define the region to be checked. If
590 `transient-mark-mode' is enabled, the region specified will be
591 checked. Otherwise START and END will be the minimum and maximum
592 positions of the buffer, respectively."
593 (interactive)
594 (unless (or start end)
595 (if (and transient-mark-mode mark-active)
596 (setq start (region-beginning)
597 end (copy-marker (region-end)))
598 (setq start (point-min)
599 end (point-max))))
600 (let ((unmatched-tag-list '())
601 (error-position nil)
602 (expected nil))
603 (save-excursion
604 (goto-char start)
605 (while (and (search-forward-regexp (wesnoth-element) end t)
606 (not error-position))
607 (beginning-of-line)
608 (if (looking-at "^[\t ]*#\\(\\w+\\)")
609 (let ((preprocessor-name (match-string-no-properties 1)))
610 (cond
611 ((string= preprocessor-name "define")
612 (setq unmatched-tag-list
613 (cons preprocessor-name unmatched-tag-list)))
614 ((string= preprocessor-name "ifdef")
615 (setq unmatched-tag-list
616 (cons preprocessor-name unmatched-tag-list)))
617 ((string= preprocessor-name "ifndef")
618 (setq unmatched-tag-list
619 (cons preprocessor-name unmatched-tag-list)))
620 ((string= preprocessor-name "else")
621 (unless (string-match "ifn?def" (car unmatched-tag-list))
622 (setq error-position (point))))
623 ((string= preprocessor-name "endif")
624 (if (string-match "ifn?def" (car unmatched-tag-list))
625 (setq unmatched-tag-list (cdr unmatched-tag-list))
626 (setq error-position (point))))
627 ((string= preprocessor-name "enddef")
628 (if (string= (car unmatched-tag-list) "define")
629 (setq unmatched-tag-list (cdr unmatched-tag-list))
630 (setq error-position (point))))))
631 (if (looking-at "^[\t ]*\\[\\+?\\(\\w+\\)\\]")
632 (setq unmatched-tag-list
633 (cons (match-string-no-properties 1)
634 unmatched-tag-list))
635 (when (looking-at "^[\t ]*\\[/\\(\\w+\\)\\]")
636 (if (string= (match-string-no-properties 1)
637 (car unmatched-tag-list))
638 (setq unmatched-tag-list (cdr unmatched-tag-list))
639 (setq error-position (point))))))
640 (end-of-line)))
641 (when unmatched-tag-list
642 (cond ((string= (car unmatched-tag-list) "define")
643 (setq expected "#enddef"))
644 ((string-match "ifn?def" (car unmatched-tag-list))
645 (setq expected "#endif"))
646 ((not unmatched-tag-list)
647 (setq expected "Unexpected end of file"))))
648 (if (interactive-p)
649 (if (not (or unmatched-tag-list error-position))
650 (message "%s" "Structure appears consistent.")
651 (and error-position
652 (goto-char error-position))
653 (if (string= expected "Unexpected end of file")
654 (message "Error %s" expected)
655 (message "Error: Expecting %s"
657 expected
658 (concat " [/" (car unmatched-tag-list) "]")))))
659 (when (or expected unmatched-tag-list)
660 (or expected (concat "[/" (car unmatched-tag-list) "]"))))))
662 ;;; wesnoth-mode
663 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
664 "Major mode for editing WML."
665 (set-syntax-table wesnoth-syntax-table)
666 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
667 (set (make-local-variable 'comment-start) "#")
668 (set (make-local-variable 'indent-line-function) 'wesnoth-indent-line)
669 (set (make-local-variable 'font-lock-defaults)
670 '(wesnoth-font-lock-keywords
671 nil t nil nil
672 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
673 (setq indent-tabs-mode nil)
674 (setq mode-name "WML")
675 (run-hooks 'wesnoth-mode-hook))
677 (provide 'wesnoth-mode)