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,
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.
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)
30 ;; (add-to-list 'auto-mode-alist '("\\.cfg\\'" . wesnoth-mode))
31 ;; to automatically load wesnoth-mode for all files ending in '.cfg'.
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 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
45 ;; * Now compatible with GNU Emacs 21.4.
46 ;; * Added support for several new tags.
47 ;; * Added menu entry for wesnoth-mode.
48 ;; * Significant speed increase to indentation.
49 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
50 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
52 ;; * Trailing whitespace is no longer created when creating a second
53 ;; consecutive newline.
54 ;; * Spurious newlines are no longer created when inserting a tag elements
57 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
58 ;; `wesnoth-element', `wesnoth-element-opening',
59 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
60 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
61 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
62 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
63 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
64 ;; * Fixed a bug in indentation where content was needed between elements pairs
65 ;; for indentation to work.
66 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
67 ;; `wesnoth-auto-indent-flag'.
68 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
69 ;; * Added indentation styles: `wesnoth-indent-default',
70 ;; `wesnoth-indent-withtags' which implement a a similar indentation
71 ;; style to the existing styles, however all preprocessor statements are
72 ;; indented to the first column.
73 ;; * Added support for several new tags.
74 ;; * Modified `wesnoth-newline' to behave more consistently.
75 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
76 ;; now leaves point at the beginning (when moving backward) or end (when
77 ;; moving forward) of the match.
78 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
79 ;; will now work on preprocessor statements. Will now warn if jump
80 ;; destination may not be correct (due to errors in WML structure).
81 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
82 ;; * `wesnoth-check-structure' can now be applied over an active region and
83 ;; now checks preprocessor statements for correct nesting.
84 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
85 ;; perform indentation by providing a prefix argument.
86 ;; * Indentation styles now leave point at the first non-whitespace character
88 ;; * `wesnoth-check-tag-names' now reports on success.
89 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
90 ;; * `outline-minor-mode' now works on macro definitions.
92 ;; * Base indent now defaults to 4.
93 ;; * Added support for #ifndef.
96 (defconst wesnoth-mode-version
"1.2.4"
97 "The current version of `wesnoth-mode'.")
99 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
103 (defcustom wesnoth-indent-preprocessor-bol t
104 "Style used to indent preprocessor statements.
105 If non-nil, preprocessor statements are indented to the first
106 column. Otherwise, they are indented as tags."
108 :group
'wesnoth-mode
)
110 (defcustom wesnoth-indent-savefile t
111 "Style used to indent WML elements.
112 If non-nil, attributes will be indented further than their
113 containing element. Otherwise, attributes will be indented at
116 :group
'wesnoth-mode
)
118 (defcustom wesnoth-auto-indent-flag t
119 "Non-nil means indent the current line upon creating a newline."
121 :group
'wesnoth-mode
)
123 (defcustom wesnoth-base-indent
4
124 "The number of columns to indent WML."
126 :group
'wesnoth-mode
)
128 (defconst wesnoth-preprocessor-regexp
129 "[\t ]*#\\(?:enddef\\|define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def\\)"
130 "Regular expression to match all preprocessor statements.")
132 (defconst wesnoth-preprocessor-opening-regexp
133 "[\t ]*#\\(?:define \\|else\\|ifdef \\|ifndef \\)"
134 "Regular expression to match \"opening\" preprocessor statements.")
136 (defconst wesnoth-preprocessor-closing-regexp
137 "[\t ]*#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)"
138 "Regular expression to match \"closing\" preprocessor statements.")
140 (defvar wesnoth-define-blocks
'()
141 "Cache of all toplevel #define and #enddef pairs.")
143 (defvar wesnoth-mode-hook nil
)
145 (defvar wesnoth-mode-map
146 (let ((map (make-sparse-keymap)))
147 (define-key map
"\C-\M-a" 'wesnoth-backward-element
)
148 (define-key map
"\C-\M-e" 'wesnoth-forward-element
)
149 (define-key map
"\C-c\C-m" 'wesnoth-jump-to-matching
)
150 (define-key map
"\C-cm" 'wesnoth-jump-to-matching
)
151 (define-key map
"\C-m" 'wesnoth-newline
)
152 (define-key map
"\C-j" 'wesnoth-newline-and-indent
)
153 (define-key map
"\C-c\C-c" 'wesnoth-check-structure
)
154 (define-key map
"\C-cc" 'wesnoth-check-structure
)
155 (define-key map
"\C-c\C-n" 'wesnoth-check-tag-names
)
156 (define-key map
"\C-cn" 'wesnoth-check-tag-names
)
157 (define-key map
"\C-c\C-e" 'wesnoth-insert-tag
)
158 (define-key map
"\C-ce" 'wesnoth-insert-tag
)
159 (define-key map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
160 (define-key map
(kbd "C-c /") 'wesnoth-insert-missing-closing
)
162 (define-key map
[menu-bar wesnoth
]
163 (cons "WML" (make-sparse-keymap "WML")))
164 (define-key map
[menu-bar wesnoth insert-tag
]
165 '("Insert Tag" . wesnoth-insert-tag
))
166 (define-key map
[menu-bar wesnoth check-names
]
167 '("Check Tag Names" . wesnoth-check-tag-names
))
168 (define-key map
[menu-bar wesnoth check-structure
]
169 '("Check Structure" . wesnoth-check-structure
))
170 (define-key map
[menu-bar wesnoth jump-to-matching
]
171 '("Jump to Matching" . wesnoth-jump-to-matching
))
172 (define-key map
[menu-bar wesnoth insert-missing-closing
]
173 '("Insert Missing Tag" . wesnoth-insert-missing-closing
))
175 "Keymap used in wesnoth mode.")
177 (defvar wesnoth-syntax-table
178 (let ((wesnoth-syntax-table (make-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 ?.
"_" wesnoth-syntax-table
)
183 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
184 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
185 wesnoth-syntax-table
)
186 "Syntax table for `wesnoth-mode'.")
188 ;; Prevents automatic syntax-highlighting of elements which might be
189 ;; pre-processor statements.
190 (defvar wesnoth-syntactic-keywords
192 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\|#enddef\\)" 1 "w")
193 '("\\(#[\t ]*.*$\\)" 1 "<"))
194 "Highlighting syntactic keywords within `wesnoth-mode'.")
196 (defun wesnoth-preprocessor-best-face ()
197 "Use `font-lock-preprocessor-face' when available."
198 (if (boundp 'font-lock-preprocessor-face
)
199 (copy-face 'font-lock-preprocessor-face
'wesnoth-preprocessor-face
)
200 (copy-face 'font-lock-keyword-face
'wesnoth-preprocessor-face
)))
202 (defvar wesnoth-font-lock-keywords
204 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" .
'wesnoth-preprocessor-face
)
205 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
206 2 font-lock-function-name-face
)
207 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
208 'wesnoth-preprocessor-face
)
209 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)"
210 (1 font-lock-function-name-face
))
211 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)"
212 (1 font-lock-function-name-face
))
213 '("}" . font-lock-function-name-face
)
214 '("\\[[^]]+\\]" . font-lock-type-face
)
215 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face
)
216 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
217 1 font-lock-variable-name-face
))
218 "Syntax highlighting for `wesnoth-mode'.")
220 (defun wesnoth-element-closing (&optional all
)
221 "Return string to use for a closing element.
223 If the optional argument ALL is non-nil, include all closing
224 preprocessor statements."
225 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
226 (concat "^[ \t]*\\[/\\|"
227 wesnoth-preprocessor-closing-regexp
)
228 "^[ \t]*\\(\\[/\\|#enddef\\)"))
230 (defun wesnoth-element-opening (&optional all
)
231 "Return string to use for an opening element.
233 If the optional argument ALL is non-nil, include all closing
234 preprocessor statements."
235 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
236 (concat "^[ \t]*\\[[^/]\\|"
237 wesnoth-preprocessor-opening-regexp
)
238 "^[ \t]*\\(\\[[^/]\\|#define\\)"))
240 (defun wesnoth-element (&optional all
)
241 "Return string to use for an opening or closing element.
243 If the optional argument ALL is non-nil, include all preprocessor
245 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
246 (concat "^[\t ]*\\[/?\\|"
247 wesnoth-preprocessor-regexp
)
248 "^[\t ]*\\(\\[/?\\|#define\\|#enddef\\)"))
251 (defvar wesnoth-tags-list
253 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
254 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
255 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
256 "command" "deaths" "debug_message" "defend" "defends" "defense" "delay"
257 "destination" "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel"
258 "entry" "era" "event" "expenses" "filter" "filter_adjacent_location"
259 "filter_location" "filter_radius" "filter_second" "filter_vision" "format"
260 "frame" "game_config" "generator" "gold" "have_unit" "header" "hide_unit"
261 "if" "illuminated_time" "image" "img" "income" "italic" "item" "jump"
262 "kill" "killed" "label" "language" "leader_goal" "main_map" "menu"
263 "message" "mini_map" "missile_frame" "modifications" "modify_side"
264 "modify_turns" "move" "move_unit_fake" "movement_costs" "movetype"
265 "multiplayer" "multiplayer_side" "music" "not" "num_units" "object"
266 "objectives" "objective" "observers" "option" "or" "panel" "part"
267 "place_shroud" "position" "print" "protect_location" "protect_unit" "race"
268 "random" "recall" "recalls" "recruit" "recruits" "redraw" "ref"
269 "remove_shroud" "remove_unit_overlay" "removeitem" "replay" "replay_start"
270 "resistance" "resolution" "results" "role" "save" "scenario" "scroll"
271 "scroll_to" "scroll_to_unit" "section" "set_menu_item" "set_recruit"
272 "set_variable" "show_if" "side" "side_playing" "snapshot" "sound" "source"
273 "specials" "statistics" "status" "stone" "store_gold" "store_locations"
274 "store_starting_location" "store_side" "store_unit" "story" "target" "team"
275 "teleport" "teleport_anim" "terrain" "terrain_graphics" "test" "text_input"
276 "textdomain" "theme" "then" "tile" "time" "time_area" "time_of_day" "topic"
277 "toplevel" "trait" "turn" "tutorial" "unhide_unit" "unit" "unit_abilities"
278 "unit_alignment" "unit_description" "unit_hp" "unit_image" "unit_level"
279 "unit_moves" "unit_overlay" "unit_profile" "unit_status" "unit_traits"
280 "unit_type" "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit"
281 "upkeep" "variable" "variables" "village" "villages" "while" "wml_filter")
282 "A list containing all tags which are available in WML.")
284 (defvar wesnoth-completion-cache
'()
285 "List of tags which have been generated by `wesnoth-build-completion'.")
287 (defun wesnoth-build-completion (&optional rebuild
)
288 "Create a new list for tag completion if necessary.
289 If REBUILD is non-nil, regenerate `wesnoth-completion-cache'."
291 (if (> emacs-major-version
21)
293 (if (and wesnoth-completion-cache
(not rebuild
))
294 wesnoth-completion-cache
297 (dolist (tag wesnoth-tags-list
)
298 (setq iter
(1+ iter
))
299 (setq tags
(append tags
(list (cons tag iter
)))))
300 (setq wesnoth-completion-cache tags
)))))
302 (defun wesnoth-insert-tag (tagname &optional start end
)
303 "Insert the specified opening tag and it's matching closing tag.
304 Both the opening and closing tags will be placed on their own
305 lines with point positioned between them. Completion of tags at
306 the prompt uses `wesnoth-tags-list'.
308 TAGNAME is the name of the tag to be inserted. If START and END
309 are given, the tags will be inserted around the specified region.
310 Enabling function `transient-mark-mode' will cause `wesnoth-insert-tag' to
311 insert opening and closing tags around the specified region."
313 (list (completing-read "Tag: " (wesnoth-build-completion) nil nil
)))
314 (when (and (not (or start end
)) transient-mark-mode mark-active
)
315 (setq start
(region-beginning)
316 end
(copy-marker (region-end))))
320 (or (looking-at "^[\t ]*$")
322 (insert "[" tagname
"]")
325 (if (looking-at "^[\t ]*$")
329 (insert "[/" tagname
"]")
330 (setq end
(point)) ;; New target for indent-region
331 (indent-region start end nil
))
333 (or (looking-at "^[\t ]*$")
338 (wesnoth-insert-and-indent "[" tagname
"]")
339 (wesnoth-insert-and-indent "\n")
341 (wesnoth-insert-and-indent "\n[/" tagname
"]"))))
343 (defun wesnoth-insert-missing-closing (&optional start end
)
344 "Insert the next expected closing element at point.
346 START and END define the region to check for missing closing
347 elements. If function `transient-mark-mode' is enabled, the region
348 specified will be used as START and END. Otherwise, START and
349 END will be the minimum and maximum positions of the buffer,
352 (if (and transient-mark-mode mark-active
)
353 (setq start
(region-beginning)
354 end
(copy-marker (region-end)))
355 (setq start
(point-min)
357 (let ((element (wesnoth-check-structure start end
)))
359 (if (string= element
"Unexpected end of file")
360 (message "%s" "Error: Expected end of file")
361 (when (not (looking-at "^[\t ]*$"))
367 (error "%s" "Unable to find element to insert"))))
369 (defun wesnoth-insert-and-indent (&rest args
)
370 "Concatenate and insert the given string(s) before indenting.
372 ARGS is a list of strings to be inserted."
373 (insert (apply 'concat args
))
377 (defun wesnoth-newline (&optional indent
)
378 "Indent both the current line and the newline created.
379 If `wesnoth-auto-indent-flag' is nil, indentation will not be
380 performed. Indentation can be forced by setting INDENT to
384 (when (and (or wesnoth-auto-indent-flag indent
)
385 (not (looking-at "^[\t ]*$")))
390 (defun wesnoth-forward-element (repeat)
391 "Move point to the end of the next tag.
392 REPEAT is an optional numeric argument. If REPEAT is non-nil,
393 jump forward the specified number of tags."
395 (or repeat
(setq repeat
1))
397 (wesnoth-backward-element (abs repeat
))
398 (let ((iterations 0))
399 (while (< iterations repeat
)
401 (search-forward-regexp
402 (wesnoth-element-opening t
)
404 (setq iterations
(1+ iterations
))))
407 (defun wesnoth-backward-element (repeat)
408 "Move point to the beginning of the previous tag.
409 REPEAT is an optional numeric argument. If REPEAT is non-nil,
410 jump backward the specified number of tags."
412 (or repeat
(setq repeat
1))
414 (wesnoth-forward-element (abs repeat
))
415 (let ((iterations 0))
416 (while (< iterations repeat
)
418 (search-backward-regexp
419 (wesnoth-element-opening t
)
422 (search-forward-regexp "[^[:blank:]]")
424 (setq iterations
(1+ iterations
))))))
426 (defun wesnoth-jump-to-matching ()
427 "Jump point to the matching opening/closing tag.
428 A tag must be on the same line as point for jumping to occur.
429 Tag structure between the start and target positions must be
430 consistent for jumping to occur."
435 (search-backward nil
))
440 (when (looking-at (wesnoth-element-closing t
))
441 (setq search-backward t
))
442 (if (wesnoth-wml-start-pos)
443 (if (> (point) (wesnoth-wml-start-pos))
444 (search-backward-regexp
447 (goto-char (point-min))
448 (search-forward-regexp
451 (error "%s" "Unable to locate tag to jump from")))
456 (or (< open-tags
0) (not search-started
))
457 (search-backward-regexp
460 (setq search-started t
)
461 (if (looking-at (wesnoth-element-opening t
))
462 (setq open-tags
(1+ open-tags
))
463 (when (looking-at (wesnoth-element-closing t
))
464 (setq open-tags
(1- open-tags
)))))
466 (search-backward-regexp "\\[\\|#"))
468 (or (> open-tags
0) (not search-started
))
469 (search-forward-regexp
473 (setq search-started t
)
474 (if (looking-at (wesnoth-element-opening t
))
475 (setq open-tags
(1+ open-tags
))
476 (when (looking-at (wesnoth-element-closing t
))
477 (setq open-tags
(1- open-tags
))))
480 (setq tag-position
(point)))
483 (and (wesnoth-check-structure (min (point) tag-position
)
484 (max (point) tag-position
))
485 (message "%s" "Region concerning jump does not nest correctly; \
486 target may not be correct"))
488 (goto-char tag-position
)
491 (defun wesnoth-wml-start-pos ()
492 "Determine the position of `point' relative to where the actual WML begins.
493 Return the likely starting position of the WML if it is found.
494 Otherwise return nil."
496 (goto-char (point-min))
497 (when (search-forward-regexp (wesnoth-element t
) (buffer-size) t
)
501 (defun wesnoth-indent ()
502 "Indent the current line as WML."
504 (multiple-value-bind (context ref-indent
)
505 (wesnoth-determine-context (point))
506 (if (or (not (wesnoth-wml-start-pos))
507 (<= (point) (wesnoth-wml-start-pos))
508 (nth 3 (parse-partial-sexp (point-min) (point)))
509 (and wesnoth-indent-preprocessor-bol
510 (looking-at wesnoth-preprocessor-regexp
))
515 ((eq context
'opening
)
516 (if (or (looking-at "^[\t ]*\\[[^/]")
517 (looking-at (wesnoth-element-opening))
518 (and (not (looking-at (wesnoth-element-closing)))
519 wesnoth-indent-savefile
))
520 (setq cur-indent
(+ ref-indent wesnoth-base-indent
))
521 (setq cur-indent ref-indent
)))
522 ((eq context
'closing
)
523 (if (looking-at "^[\t ]*\\[/")
524 (setq cur-indent
(- ref-indent wesnoth-base-indent
))
525 (setq cur-indent ref-indent
))))
527 (indent-line-to (max cur-indent
0)))))))
529 (defun wesnoth-within-define (position)
530 "Determine whether point is currently inside a #define block.
531 POSITION is the initial cursor position."
533 (dolist (element (or wesnoth-define-blocks
534 (wesnoth-find-macro-definitions)))
535 (when (= (cadr (sort (append (mapcar 'marker-position
(cadr element
))
539 (setq depth
(max (car element
) depth
))))
542 (defun wesnoth-find-macro-definitions ()
543 "Return information regarding positioning of macro definitions."
545 (goto-char (point-min))
548 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t
)
549 (and (string= (match-string 1) "#define") (beginning-of-line))
551 (if (string= (match-string 1) "#define")
553 (add-to-list 'openings
(point-marker))
556 (list depth
(list (car openings
) (point-marker))))
557 (setq openings
(cdr openings
))
562 (defun wesnoth-indent-region (start end
)
563 "Indent the region from START to END.
565 Creates and destroys a cache of macro definition details as necessary."
570 (setq end
(point-marker))
572 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
573 (or (bolp) (forward-line 1))
574 (while (< (point) end
)
576 (if (looking-at "^[\t ]*$")
578 (funcall indent-line-function
))))
579 (setq wesnoth-define-blocks nil
)))
581 (defun wesnoth-determine-context (position)
582 "Determine the type of the last relevant element.
584 POSITION is the buffer position of the element for which to
585 determine the context."
587 (search-backward-regexp (wesnoth-element) (wesnoth-wml-start-pos) t
)
588 (let ((match (or (match-string 1) ""))
589 (depth (wesnoth-within-define position
)))
590 (while (and (> (wesnoth-within-define (point)) depth
)
591 (not (= (point) (wesnoth-wml-start-pos))))
592 (search-backward-regexp (wesnoth-element)
593 (wesnoth-wml-start-pos) t
)
594 (setq match
(match-string 1)))
595 (when (and (= (point) (wesnoth-wml-start-pos)) (= depth
0)
596 (string-match "#define" match
))
597 ;; Found nothing of use; reset match and assume top-level tag.
600 ((string-match "\\[/\\|#enddef" match
)
601 (values 'closing
(current-indentation)))
602 ((string-match "\\[[^/]?\\|#define" match
)
603 (values 'opening
(current-indentation)))))))
605 (defun wesnoth-newline-and-indent (&optional indent
)
606 "Indent both the current line and the newline created.
607 If `wesnoth-auto-indent-flag' is nil, indentation will not be
610 If the optional argument, INDENT is non-nil, force indentation to
614 (when (or wesnoth-auto-indent-flag indent
)
618 (defun wesnoth-check-tag-names ()
619 "Check the names of all tags in the buffer for correctness.
620 If a tag is found which is not present in the list an error will
621 be signalled and point will be moved to the corresponding
624 (let ((tag-position nil
)
625 (missing-tag-name nil
))
627 (goto-char (point-min))
629 (search-forward-regexp "^[\t ]*\\[" (point-max) t
)
632 (when (looking-at "^[\t ]*\\[[/]?\\(\\(\\(\\w\\|_\\)+\\|_\\)\\)\\]")
633 (unless (member (match-string-no-properties 1) wesnoth-tags-list
)
634 (setq tag-position
(point))
635 (setq missing-tag-name
(match-string-no-properties 1))))
639 (goto-char tag-position
)
641 (search-backward "[")
642 (message "'%s' is not known to exist"
644 (message "%s" "No unknown tag names found."))))
646 (defun wesnoth-check-structure (&optional start end
)
647 "Check the buffer for correct nesting of elements.
648 If a problem is found in the structure, point will be placed at
649 the location which an element was expected and the expected
650 element will be displayed in the mini-buffer.
652 START and END define the region to be checked. If
653 function `transient-mark-mode' is enabled, the region specified will be
654 checked. Otherwise START and END will be the minimum and maximum
655 positions of the buffer, respectively."
657 (unless (or start end
)
658 (if (and transient-mark-mode mark-active
)
659 (setq start
(region-beginning)
660 end
(copy-marker (region-end)))
661 (setq start
(point-min)
663 (let ((unmatched-tag-list '())
668 (while (and (search-forward-regexp
671 (not error-position
))
672 (search-backward-regexp
675 (if (looking-at wesnoth-preprocessor-regexp
)
676 (let ((preprocessor-name (match-string-no-properties 1)))
681 (string= preprocessor-name preproc
))
682 '("define" "ifdef" "ifndef")))
683 (setq unmatched-tag-list
684 (cons preprocessor-name unmatched-tag-list
)))
685 ((string= preprocessor-name
"else")
686 (unless (string-match "ifn?def" (car unmatched-tag-list
))
687 (setq error-position
(point))))
688 ((string= preprocessor-name
"endif")
689 (if (string-match "ifn?def" (car unmatched-tag-list
))
690 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
691 (setq error-position
(point))))
692 ((string= preprocessor-name
"enddef")
693 (if (string= (car unmatched-tag-list
) "define")
694 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
695 (setq error-position
(point))))))
696 (if (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
697 (setq unmatched-tag-list
698 (cons (match-string-no-properties 1)
700 (when (looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
701 (if (string= (match-string-no-properties 1)
702 (car unmatched-tag-list
))
703 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
704 (setq error-position
(point))))))
706 (when unmatched-tag-list
707 (cond ((string= (car unmatched-tag-list
) "define")
708 (setq expected
"#enddef"))
709 ((string-match "ifn?def" (car unmatched-tag-list
))
710 (setq expected
"#endif"))
711 ((not unmatched-tag-list
)
712 (setq expected
"Unexpected end of file"))))
714 (if (not (or unmatched-tag-list error-position
))
715 (message "%s" "Structure appears consistent.")
717 (goto-char error-position
))
718 (if (string= expected
"Unexpected end of file")
719 (message "Error %s" expected
)
720 (message "Error: Expecting %s"
723 (concat " [/" (car unmatched-tag-list
) "]")))))
724 (when (or expected unmatched-tag-list
)
725 (or expected
(concat "[/" (car unmatched-tag-list
) "]"))))))
728 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
729 "Major mode for editing WML."
730 (wesnoth-preprocessor-best-face)
731 (set-syntax-table wesnoth-syntax-table
)
732 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
733 (set (make-local-variable 'comment-start
) "#")
734 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
735 (set (make-local-variable 'indent-region-function
) 'wesnoth-indent-region
)
736 (set (make-local-variable 'font-lock-defaults
)
737 '(wesnoth-font-lock-keywords
739 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
740 (setq indent-tabs-mode nil
)
741 (setq mode-name
"WML")
742 (run-hooks 'wesnoth-mode-hook
))
744 (provide 'wesnoth-mode
)
746 ;;; wesnoth-mode.el ends here