* wesnoth-mode.el: Update version to 1.2.4; update changelog.
[wesnoth-mode.git] / wesnoth-mode.el
blobc15d8ab096cb45f9ef556fdd14f3f86473addc98
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 ;; * Improved syntax-highlighting for macro calls.
36 ;; * Underscore is now treated as whitespace.
37 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
38 ;; * Point is now placed at the first non-whitespace character of the line,
39 ;; instead of the last.
40 ;; * Corrected minor indentation bugs.
41 ;; * Indenting across large regions is now much more efficient.
42 ;; 1.2.3
43 ;; * Now compatible with GNU Emacs 21.4.
44 ;; * Added support for several new tags.
45 ;; * Added menu entry for wesnoth-mode.
46 ;; * Significant speed increase to indentation.
47 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
48 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
49 ;; has been removed.
50 ;; * Trailing whitespace is no longer created when creating a second
51 ;; consecutive newline.
52 ;; * Spurious newlines are no longer created when inserting a tag elements
53 ;; around a region.
54 ;; 1.2.2
55 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
56 ;; `wesnoth-element', `wesnoth-element-opening',
57 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
58 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
59 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
60 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
61 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
62 ;; * Fixed a bug in indentation where content was needed between elements pairs
63 ;; for indentation to work.
64 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
65 ;; `wesnoth-auto-indent-flag'.
66 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
67 ;; * Added indentation styles: `wesnoth-indent-default',
68 ;; `wesnoth-indent-withtags' which implement a a similar indentation
69 ;; style to the existing styles, however all preprocessor statements are
70 ;; indented to the first column.
71 ;; * Added support for several new tags.
72 ;; * Modified `wesnoth-newline' to behave more consistently.
73 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
74 ;; now leaves point at the beginning (when moving backward) or end (when
75 ;; moving forward) of the match.
76 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
77 ;; will now work on preprocessor statements. Will now warn if jump
78 ;; destination may not be correct (due to errors in WML structure).
79 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
80 ;; * `wesnoth-check-structure' can now be applied over an active region and
81 ;; now checks preprocessor statements for correct nesting.
82 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
83 ;; perform indentation by providing a prefix argument.
84 ;; * Indentation styles now leave point at the first non-whitespace character
85 ;; of the line.
86 ;; * `wesnoth-check-tag-names' now reports on success.
87 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
88 ;; * `outline-minor-mode' now works on macro definitions.
89 ;; 1.2.1
90 ;; * Base indent now defaults to 4.
91 ;; * Added support for #ifndef.
93 (defconst wesnoth-mode-version "1.2.4"
94 "The current version of wesnoth-mode.")
96 (defgroup wesnoth-mode nil "Wesnoth-mode access"
97 :group 'languages
98 :prefix "wesnoth-")
100 (defcustom wesnoth-indent-preprocessor-bol t
101 "Style used to indent preprocessor statements.
102 If non-nil, preprocessor statements are indented to the first
103 column. Otherwise, they are indented as tags."
104 :type 'boolean
105 :group 'wesnoth-mode)
107 (defcustom wesnoth-indent-savefile t
108 "Style used to indent WML elements.
109 If non-nil, attributes will be indented further than their
110 containing element. Otherwise, attributes will be indented at
111 the same level."
112 :type 'boolean
113 :group 'wesnoth-mode)
115 (defcustom wesnoth-auto-indent-flag t
116 "Whether to attempt tag indentation when a newline is created.
117 If nil, no indentation will be attempted. Otherwise, attempt to
118 indent the line."
119 :type 'boolean
120 :group 'wesnoth-mode)
122 (defcustom wesnoth-base-indent 4
123 "The number of columns to indent WML."
124 :type 'integer
125 :group 'wesnoth-mode)
127 (defconst wesnoth-preprocessor-regexp
128 "[\t ]*#\\(?:enddef\\|define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def\\)"
129 "Regular expression to match all preprocessor statements.")
131 (defconst wesnoth-preprocessor-opening-regexp
132 "[\t ]*#\\(?:define \\|else\\|ifdef \\|ifndef \\)"
133 "Regular expression to match \"opening\" preprocessor statements.")
135 (defconst wesnoth-preprocessor-closing-regexp
136 "[\t ]*#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)"
137 "Regular expression to match \"closing\" preprocessor statements.")
139 (defvar wesnoth-define-blocks '()
140 "Cache of all toplevel #define and #enddef pairs.")
142 (defvar wesnoth-mode-hook nil)
144 (defvar wesnoth-mode-map ()
145 "Keymap used in wesnoth mode.")
146 (unless wesnoth-mode-map
147 (setq wesnoth-mode-map (make-sparse-keymap))
148 (define-key wesnoth-mode-map "\C-\M-a" 'wesnoth-backward-element)
149 (define-key wesnoth-mode-map "\C-\M-e" 'wesnoth-forward-element)
150 (define-key wesnoth-mode-map "\C-c\C-m" 'wesnoth-jump-to-matching)
151 (define-key wesnoth-mode-map "\C-cm" 'wesnoth-jump-to-matching)
152 (define-key wesnoth-mode-map "\C-m" 'wesnoth-newline)
153 (define-key wesnoth-mode-map "\C-j" 'wesnoth-newline-and-indent)
154 (define-key wesnoth-mode-map "\C-c\C-c" 'wesnoth-check-structure)
155 (define-key wesnoth-mode-map "\C-cc" 'wesnoth-check-structure)
156 (define-key wesnoth-mode-map "\C-c\C-n" 'wesnoth-check-tag-names)
157 (define-key wesnoth-mode-map "\C-cn" 'wesnoth-check-tag-names)
158 (define-key wesnoth-mode-map "\C-c\C-e" 'wesnoth-insert-tag)
159 (define-key wesnoth-mode-map "\C-ce" 'wesnoth-insert-tag)
160 (define-key wesnoth-mode-map (kbd "C-c C-/") 'wesnoth-insert-missing-closing)
161 (define-key wesnoth-mode-map (kbd "C-c /") 'wesnoth-insert-missing-closing)
162 ;; Menu
163 (define-key wesnoth-mode-map [menu-bar wesnoth]
164 (cons "WML" (make-sparse-keymap "WML")))
165 (define-key wesnoth-mode-map [menu-bar wesnoth insert-tag]
166 '("Insert Tag" . wesnoth-insert-tag))
167 (define-key wesnoth-mode-map [menu-bar wesnoth check-names]
168 '("Check Tag Names" . wesnoth-check-tag-names))
169 (define-key wesnoth-mode-map [menu-bar wesnoth check-structure]
170 '("Check Structure" . wesnoth-check-structure))
171 (define-key wesnoth-mode-map [menu-bar wesnoth jump-to-matching]
172 '("Jump to Matching" . wesnoth-jump-to-matching))
173 (define-key wesnoth-mode-map [menu-bar wesnoth insert-missing-closing]
174 '("Insert Missing Tag" . wesnoth-insert-missing-closing)))
176 (defvar wesnoth-syntax-table
177 (let ((wesnoth-syntax-table (make-syntax-table)))
178 (modify-syntax-entry ?= "." wesnoth-syntax-table)
179 (modify-syntax-entry ?_ "_" wesnoth-syntax-table)
180 (modify-syntax-entry ?- "_" wesnoth-syntax-table)
181 (modify-syntax-entry ?. "_" wesnoth-syntax-table)
182 (modify-syntax-entry ?\n ">" wesnoth-syntax-table)
183 (modify-syntax-entry ?\r ">" wesnoth-syntax-table)
184 wesnoth-syntax-table)
185 "Syntax table for wesnoth-mode.")
187 ;; Prevents automatic syntax-highlighting of elements which might be
188 ;; pre-processor statements.
189 (defvar wesnoth-syntactic-keywords
190 (list
191 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\|#enddef\\)" 1 "w")
192 '("\\(#[\t ]*.*$\\)" 1 "<"))
193 "Highlighting syntactic keywords within wesnoth-mode.")
195 (defun wesnoth-preprocessor-best-face ()
196 "Use `font-lock-preprocessor-face' when available."
197 (if (boundp 'font-lock-preprocessor-face)
198 (copy-face 'font-lock-preprocessor-face 'wesnoth-preprocessor-face)
199 (copy-face 'font-lock-keyword-face 'wesnoth-preprocessor-face)))
201 (defvar wesnoth-font-lock-keywords
202 (list
203 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . 'wesnoth-preprocessor-face)
204 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
205 2 font-lock-function-name-face)
206 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
207 'wesnoth-preprocessor-face)
208 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)"
209 (1 font-lock-function-name-face))
210 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)"
211 (1 font-lock-function-name-face))
212 '("}" . font-lock-function-name-face)
213 '("\\[[^]]+\\]" . font-lock-type-face)
214 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face)
215 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
216 1 font-lock-variable-name-face))
217 "Syntax highlighting for wesnoth-mode.")
219 (defun wesnoth-element-closing (&optional all)
220 "Return string to use for a closing element."
221 (if (or (not wesnoth-indent-preprocessor-bol) all)
222 (concat "^[ \t]*\\[/\\|"
223 wesnoth-preprocessor-closing-regexp)
224 "^[ \t]*\\(\\[/\\|#enddef\\)"))
226 (defun wesnoth-element-opening (&optional all)
227 "Return string to use for an opening element."
228 (if (or (not wesnoth-indent-preprocessor-bol) all)
229 (concat "^[ \t]*\\[[^/]\\|"
230 wesnoth-preprocessor-opening-regexp)
231 "^[ \t]*\\(\\[[^/]\\|#define\\)"))
233 (defun wesnoth-element (&optional all)
234 "Return string to use for an opening or closing element."
235 (if (or (not wesnoth-indent-preprocessor-bol) all)
236 (concat "^[\t ]*\\[/?\\|"
237 wesnoth-preprocessor-regexp)
238 "^[\t ]*\\(\\[/?\\|#define\\|#enddef\\)"))
240 ;;; Insertion
241 (defvar wesnoth-tags-list
242 (list
243 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
244 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
245 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
246 "command" "deaths" "debug_message" "defend" "defends" "defense" "delay"
247 "destination" "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel"
248 "entry" "era" "event" "expenses" "filter" "filter_adjacent_location"
249 "filter_location" "filter_radius" "filter_second" "filter_vision" "format"
250 "frame" "game_config" "generator" "gold" "have_unit" "header" "hide_unit"
251 "if" "illuminated_time" "image" "img" "income" "italic" "item" "jump"
252 "kill" "killed" "label" "language" "leader_goal" "main_map" "menu"
253 "message" "mini_map" "missile_frame" "modifications" "modify_side"
254 "modify_turns" "move" "move_unit_fake" "movement_costs" "movetype"
255 "multiplayer" "multiplayer_side" "music" "not" "num_units" "object"
256 "objectives" "objective" "observers" "option" "or" "panel" "part"
257 "place_shroud" "position" "print" "protect_location" "protect_unit" "race"
258 "random" "recall" "recalls" "recruit" "recruits" "redraw" "ref"
259 "remove_shroud" "remove_unit_overlay" "removeitem" "replay" "replay_start"
260 "resistance" "resolution" "results" "role" "save" "scenario" "scroll"
261 "scroll_to" "scroll_to_unit" "section" "set_menu_item" "set_recruit"
262 "set_variable" "show_if" "side" "side_playing" "snapshot" "sound" "source"
263 "specials" "statistics" "status" "stone" "store_gold" "store_locations"
264 "store_starting_location" "store_side" "store_unit" "story" "target" "team"
265 "teleport" "teleport_anim" "terrain" "terrain_graphics" "test" "text_input"
266 "textdomain" "theme" "then" "tile" "time" "time_area" "time_of_day" "topic"
267 "toplevel" "trait" "turn" "tutorial" "unhide_unit" "unit" "unit_abilities"
268 "unit_alignment" "unit_description" "unit_hp" "unit_image" "unit_level"
269 "unit_moves" "unit_overlay" "unit_profile" "unit_status" "unit_traits"
270 "unit_type" "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit"
271 "upkeep" "variable" "variables" "village" "villages" "while" "wml_filter")
272 "A list containing all tags which are available in WML.")
274 (defvar wesnoth-completion-cache '()
275 "List of tags which have been generated by `wesnoth-build-completion'.")
277 (defun wesnoth-build-completion (&optional rebuild)
278 "Create a new list for tag completion if necessary.
279 If REBUILD is non-nil, regenerate `wesnoth-completion-cache'."
280 (interactive "P")
281 (if (> emacs-major-version 21)
282 wesnoth-tags-list
283 (if (and wesnoth-completion-cache (not rebuild))
284 wesnoth-completion-cache
285 (let ((tags '())
286 (iter 0))
287 (dolist (tag wesnoth-tags-list)
288 (setq iter (1+ iter))
289 (setq tags (append tags (list (cons tag iter)))))
290 (setq wesnoth-completion-cache tags)))))
292 (defun wesnoth-insert-tag (tagname &optional start end)
293 "Inserts the specified opening tag and it's matching closing tag.
294 Both the opening and closing tags will be placed on their own
295 lines with point positioned between them. Completion of tags at
296 the prompt uses `wesnoth-tags-list'.
298 TAGNAME is the name of the tag to be inserted. If START and END
299 are given, the tags will be inserted around the specified region.
300 Enabling `transient-mark-mode' will cause `wesnoth-insert-tag' to
301 insert opening and closing tags around the specified region."
302 (interactive
303 (list (completing-read "Tag: " (wesnoth-build-completion) nil nil)))
304 (when (and (not (or start end)) transient-mark-mode mark-active)
305 (setq start (region-beginning)
306 end (copy-marker (region-end))))
307 (if (and start end)
308 (progn
309 (goto-char start)
310 (or (looking-at "^[\t ]*$")
311 (open-line 1))
312 (insert "[" tagname "]")
313 (goto-char end)
314 (beginning-of-line)
315 (if (looking-at "^[\t ]*$")
316 (open-line 1)
317 (end-of-line)
318 (newline))
319 (insert "[/" tagname "]")
320 (setq end (point)) ;; New target for indent-region
321 (indent-region start end nil))
322 (beginning-of-line)
323 (or (looking-at "^[\t ]*$")
324 (progn
325 (end-of-line)
326 (newline)))
327 (end-of-line)
328 (wesnoth-insert-and-indent "[" tagname "]")
329 (wesnoth-insert-and-indent "\n")
330 (save-excursion
331 (wesnoth-insert-and-indent "\n[/" tagname "]"))))
333 (defun wesnoth-insert-missing-closing (&optional start end)
334 "Insert the next expected closing element at point.
336 START and END define the region to check for missing closing
337 elements. If `transient-mark-mode' is enabled, the region
338 specified will be used as START and END. Otherwise, START and
339 END will be the minimum and maximum positions of the buffer,
340 respectively."
341 (interactive)
342 (if (and transient-mark-mode mark-active)
343 (setq start (region-beginning)
344 end (copy-marker (region-end)))
345 (setq start (point-min)
346 end (point-max)))
347 (let ((element (wesnoth-check-structure start end)))
348 (if element
349 (if (string= element "Unexpected end of file")
350 (message "%s" "Error: Expected end of file")
351 (when (not (looking-at "^[\t ]*$"))
352 (end-of-line)
353 (wesnoth-newline))
354 (insert element)
355 (wesnoth-indent)
356 (end-of-line))
357 (error "%s" "Unable to find element to insert"))))
359 (defun wesnoth-insert-and-indent (&rest args)
360 "Concatenate and insert the given string(s) before indenting."
361 (insert (apply 'concat args))
362 (wesnoth-indent)
363 (end-of-line))
365 (defun wesnoth-newline (&optional indent)
366 "Indent both the current line and the newline created.
367 If `wesnoth-auto-indent-flag' is nil, indentation will not be
368 performed. Indentation can be forced by setting INDENT to
369 non-nil."
370 (interactive)
371 (save-excursion
372 (when (and (or wesnoth-auto-indent-flag indent)
373 (not (looking-at "^[\t ]*$")))
374 (wesnoth-indent)))
375 (newline))
377 ;;; Movement
378 (defun wesnoth-forward-element (repeat)
379 "Move point to the end of the next tag.
380 REPEAT is an optional numeric argument. If REPEAT is non-nil,
381 jump forward the specified number of tags."
382 (interactive "p")
383 (or repeat (setq repeat 1))
384 (if (< repeat 0)
385 (wesnoth-backward-element (abs repeat))
386 (let ((iterations 0))
387 (while (< iterations repeat)
388 (end-of-line)
389 (search-forward-regexp
390 (wesnoth-element-opening t)
391 (buffer-size) t)
392 (setq iterations (1+ iterations))))
393 (end-of-line)))
395 (defun wesnoth-backward-element (repeat)
396 "Move point to the beginning of the previous tag.
397 REPEAT is an optional numeric argument. If REPEAT is non-nil,
398 jump backward the specified number of tags."
399 (interactive "p")
400 (or repeat (setq repeat 1))
401 (if (< repeat 0)
402 (wesnoth-forward-element (abs repeat))
403 (let ((iterations 0))
404 (while (< iterations repeat)
405 (beginning-of-line)
406 (search-backward-regexp
407 (wesnoth-element-opening t)
408 0 t)
409 (unless (bobp)
410 (search-forward-regexp "[^[:blank:]]")
411 (backward-char))
412 (setq iterations (1+ iterations))))))
414 (defun wesnoth-jump-to-matching ()
415 "Jump point to the matching opening/closing tag.
416 A tag must be on the same line as point for jumping to occur.
417 Tag structure between the start and target positions must be
418 consistent for jumping to occur."
419 (interactive)
420 (let ((open-tags 0)
421 (search-started nil)
422 (tag-position nil)
423 (search-backward nil))
424 (save-excursion
425 (beginning-of-line)
426 (if (looking-at
427 (wesnoth-element t))
428 (when (looking-at (wesnoth-element-closing t))
429 (setq search-backward t))
430 (if (wesnoth-wml-start-pos)
431 (if (> (point) (wesnoth-wml-start-pos))
432 (search-backward-regexp
433 (wesnoth-element t)
434 (point-min) t)
435 (goto-char (point-min))
436 (search-forward-regexp
437 (wesnoth-element t))
438 (beginning-of-line))
439 (error "%s" "Unable to locate tag to jump from")))
440 (if search-backward
441 (progn
442 (end-of-line)
443 (while (and
444 (or (< open-tags 0) (not search-started))
445 (search-backward-regexp
446 (wesnoth-element t)
447 (point-min) t))
448 (setq search-started t)
449 (if (looking-at (wesnoth-element-opening t))
450 (setq open-tags (1+ open-tags))
451 (when (looking-at (wesnoth-element-closing t))
452 (setq open-tags (1- open-tags)))))
453 (end-of-line)
454 (search-backward-regexp "\\[\\|#"))
455 (while (and
456 (or (> open-tags 0) (not search-started))
457 (search-forward-regexp
458 (wesnoth-element t)
459 (point-max) t))
460 (beginning-of-line)
461 (setq search-started t)
462 (if (looking-at (wesnoth-element-opening t))
463 (setq open-tags (1+ open-tags))
464 (when (looking-at (wesnoth-element-closing t))
465 (setq open-tags (1- open-tags))))
466 (end-of-line))
467 (end-of-line))
468 (setq tag-position (point)))
469 (and search-backward
470 (end-of-line))
471 (and (wesnoth-check-structure (min (point) tag-position)
472 (max (point) tag-position))
473 (message "%s" "Region concerning jump does not nest correctly; \
474 target may not be correct"))
475 (if (interactive-p)
476 (goto-char tag-position)
477 tag-position)))
479 (defun wesnoth-wml-start-pos ()
480 "Determine the position of `point' relative to where the actual WML begins.
481 Return the likely starting position of the WML if it is found.
482 Otherwise return nil."
483 (save-excursion
484 (goto-char (point-min))
485 (when (search-forward-regexp (wesnoth-element t) (buffer-size) t)
486 (beginning-of-line)
487 (point))))
489 (defun wesnoth-indent ()
490 "Indent the current line as WML."
491 (beginning-of-line)
492 (multiple-value-bind (context ref-indent)
493 (wesnoth-determine-context (point))
494 (if (or (not (wesnoth-wml-start-pos))
495 (<= (point) (wesnoth-wml-start-pos))
496 (nth 3 (parse-partial-sexp (point-min) (point)))
497 (and wesnoth-indent-preprocessor-bol
498 (looking-at wesnoth-preprocessor-regexp))
499 (not context))
500 (indent-line-to 0)
501 (let ((cur-indent))
502 (cond
503 ((eq context 'opening)
504 (if (or (looking-at "^[\t ]*\\[[^/]")
505 (looking-at (wesnoth-element-opening))
506 (and (not (looking-at (wesnoth-element-closing)))
507 wesnoth-indent-savefile))
508 (setq cur-indent (+ ref-indent wesnoth-base-indent))
509 (setq cur-indent ref-indent)))
510 ((eq context 'closing)
511 (if (looking-at "^[\t ]*\\[/")
512 (setq cur-indent (- ref-indent wesnoth-base-indent))
513 (setq cur-indent ref-indent))))
514 (when cur-indent
515 (indent-line-to (max cur-indent 0)))))))
517 (defun wesnoth-within-define (position)
518 "Determine whether point is currently inside a #define block.
519 POSITION is the initial cursor position."
520 (let ((depth 0))
521 (dolist (element (or wesnoth-define-blocks
522 (wesnoth-find-macro-definitions)))
523 (when (= (cadr (sort (append (mapcar 'marker-position (cadr element))
524 (list position))
525 '>))
526 position)
527 (setq depth (max (car element) depth))))
528 depth))
530 (defun wesnoth-find-macro-definitions ()
531 "Return information regarding positioning of macro definitions."
532 (save-excursion
533 (goto-char (point-min))
534 (let ((depth 0)
535 openings cache)
536 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t)
537 (and (string= (match-string 1) "#define") (beginning-of-line))
538 (setq depth
539 (if (string= (match-string 1) "#define")
540 (progn
541 (add-to-list 'openings (point-marker))
542 (1+ depth))
543 (add-to-list 'cache
544 (list depth (list (car openings) (point-marker))))
545 (setq openings (cdr openings))
546 (1- depth)))
547 (end-of-line))
548 cache)))
550 (defun wesnoth-indent-region (start end)
551 "Indent the region from START to END.
553 Creates and destroys a cache of macro definition details as necessary."
554 (interactive "r")
555 (unwind-protect
556 (save-excursion
557 (goto-char end)
558 (setq end (point-marker))
559 (goto-char start)
560 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
561 (or (bolp) (forward-line 1))
562 (while (< (point) end)
563 (forward-line 1)
564 (if (looking-at "^[\t ]*$")
565 (indent-line-to 0)
566 (funcall indent-line-function))))
567 (setq wesnoth-define-blocks nil)))
569 (defun wesnoth-determine-context (position)
570 "Determine the type of the last relevant element."
571 (save-excursion
572 (search-backward-regexp (wesnoth-element) (wesnoth-wml-start-pos) t)
573 (let ((match (or (match-string 1) ""))
574 (depth (wesnoth-within-define position)))
575 (while (and (> (wesnoth-within-define (point)) depth)
576 (not (= (point) (wesnoth-wml-start-pos))))
577 (search-backward-regexp (wesnoth-element)
578 (wesnoth-wml-start-pos) t)
579 (setq match (match-string 1)))
580 (when (and (= (point) (wesnoth-wml-start-pos)) (= depth 0)
581 (string-match "#define" match))
582 ;; Found nothing of use; reset match and assume top-level tag.
583 (setq match ""))
584 (cond
585 ((string-match "\\[/\\|#enddef" match)
586 (values 'closing (current-indentation)))
587 ((string-match "\\[[^/]?\\|#define" match)
588 (values 'opening (current-indentation)))))))
590 (defun wesnoth-newline-and-indent (&optional indent)
591 "Indent both the current line and the newline created.
592 If `wesnoth-auto-indent-flag' is nil, indentation will not be
593 performed."
594 (interactive)
595 (wesnoth-newline)
596 (when (or wesnoth-auto-indent-flag indent)
597 (wesnoth-indent)))
599 ;;; WML checks
600 (defun wesnoth-check-tag-names ()
601 "Check the names of all tags in the buffer for correctness.
602 If a tag is found which is not present in the list an error will
603 be signalled and point will be moved to the corresponding
604 position."
605 (interactive)
606 (let ((tag-position nil)
607 (missing-tag-name nil))
608 (save-excursion
609 (goto-char (point-min))
610 (while (and
611 (search-forward-regexp "^[\t ]*\\[" (point-max) t)
612 (not tag-position))
613 (beginning-of-line)
614 (when (looking-at "^[\t ]*\\[[/]?\\(\\(\\(\\w\\|_\\)+\\|_\\)\\)\\]")
615 (unless (member (match-string-no-properties 1) wesnoth-tags-list)
616 (setq tag-position (point))
617 (setq missing-tag-name (match-string-no-properties 1))))
618 (end-of-line)))
619 (if tag-position
620 (progn
621 (goto-char tag-position)
622 (end-of-line)
623 (search-backward "[")
624 (message "'%s' is not known to exist"
625 missing-tag-name))
626 (message "%s" "No unknown tag names found."))))
628 (defun wesnoth-check-structure (&optional start end)
629 "Check the buffer for correct nesting of elements.
630 If a problem is found in the structure, point will be placed at
631 the location which an element was expected and the expected
632 element will be displayed in the mini-buffer.
634 START and END define the region to be checked. If
635 `transient-mark-mode' is enabled, the region specified will be
636 checked. Otherwise START and END will be the minimum and maximum
637 positions of the buffer, respectively."
638 (interactive)
639 (unless (or start end)
640 (if (and transient-mark-mode mark-active)
641 (setq start (region-beginning)
642 end (copy-marker (region-end)))
643 (setq start (point-min)
644 end (point-max))))
645 (let ((unmatched-tag-list '())
646 (error-position nil)
647 (expected nil))
648 (save-excursion
649 (goto-char start)
650 (while (and (search-forward-regexp
651 (wesnoth-element t)
652 end t)
653 (not error-position))
654 (search-backward-regexp
655 (wesnoth-element t)
656 start t)
657 (if (looking-at wesnoth-preprocessor-regexp)
658 (let ((preprocessor-name (match-string-no-properties 1)))
659 (cond
660 ((member 't
661 (mapcar
662 '(lambda (preproc)
663 (string= preprocessor-name preproc))
664 '("define" "ifdef" "ifndef")))
665 (setq unmatched-tag-list
666 (cons preprocessor-name unmatched-tag-list)))
667 ((string= preprocessor-name "else")
668 (unless (string-match "ifn?def" (car unmatched-tag-list))
669 (setq error-position (point))))
670 ((string= preprocessor-name "endif")
671 (if (string-match "ifn?def" (car unmatched-tag-list))
672 (setq unmatched-tag-list (cdr unmatched-tag-list))
673 (setq error-position (point))))
674 ((string= preprocessor-name "enddef")
675 (if (string= (car unmatched-tag-list) "define")
676 (setq unmatched-tag-list (cdr unmatched-tag-list))
677 (setq error-position (point))))))
678 (if (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
679 (setq unmatched-tag-list
680 (cons (match-string-no-properties 1)
681 unmatched-tag-list))
682 (when (looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
683 (if (string= (match-string-no-properties 1)
684 (car unmatched-tag-list))
685 (setq unmatched-tag-list (cdr unmatched-tag-list))
686 (setq error-position (point))))))
687 (end-of-line)))
688 (when unmatched-tag-list
689 (cond ((string= (car unmatched-tag-list) "define")
690 (setq expected "#enddef"))
691 ((string-match "ifn?def" (car unmatched-tag-list))
692 (setq expected "#endif"))
693 ((not unmatched-tag-list)
694 (setq expected "Unexpected end of file"))))
695 (if (interactive-p)
696 (if (not (or unmatched-tag-list error-position))
697 (message "%s" "Structure appears consistent.")
698 (and error-position
699 (goto-char error-position))
700 (if (string= expected "Unexpected end of file")
701 (message "Error %s" expected)
702 (message "Error: Expecting %s"
704 expected
705 (concat " [/" (car unmatched-tag-list) "]")))))
706 (when (or expected unmatched-tag-list)
707 (or expected (concat "[/" (car unmatched-tag-list) "]"))))))
709 ;;; wesnoth-mode
710 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
711 "Major mode for editing WML."
712 (wesnoth-preprocessor-best-face)
713 (set-syntax-table wesnoth-syntax-table)
714 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
715 (set (make-local-variable 'comment-start) "#")
716 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
717 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
718 (set (make-local-variable 'font-lock-defaults)
719 '(wesnoth-font-lock-keywords
720 nil t nil nil
721 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
722 (setq indent-tabs-mode nil)
723 (setq mode-name "WML")
724 (run-hooks 'wesnoth-mode-hook))
726 (provide 'wesnoth-mode)
728 ;;; wesnoth-mode.el ends here