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.
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'
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
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
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.
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"
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."
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
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
120 :group
'wesnoth-mode
)
122 (defcustom wesnoth-base-indent
4
123 "The number of columns to indent WML."
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
)
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
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
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\\)"))
241 (defvar wesnoth-tags-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'."
281 (if (> emacs-major-version
21)
283 (if (and wesnoth-completion-cache
(not rebuild
))
284 wesnoth-completion-cache
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."
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))))
310 (or (looking-at "^[\t ]*$")
312 (insert "[" tagname
"]")
315 (if (looking-at "^[\t ]*$")
319 (insert "[/" tagname
"]")
320 (setq end
(point)) ;; New target for indent-region
321 (indent-region start end nil
))
323 (or (looking-at "^[\t ]*$")
328 (wesnoth-insert-and-indent "[" tagname
"]")
329 (wesnoth-insert-and-indent "\n")
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,
342 (if (and transient-mark-mode mark-active
)
343 (setq start
(region-beginning)
344 end
(copy-marker (region-end)))
345 (setq start
(point-min)
347 (let ((element (wesnoth-check-structure start end
)))
349 (if (string= element
"Unexpected end of file")
350 (message "%s" "Error: Expected end of file")
351 (when (not (looking-at "^[\t ]*$"))
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
))
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
372 (when (and (or wesnoth-auto-indent-flag indent
)
373 (not (looking-at "^[\t ]*$")))
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."
383 (or repeat
(setq repeat
1))
385 (wesnoth-backward-element (abs repeat
))
386 (let ((iterations 0))
387 (while (< iterations repeat
)
389 (search-forward-regexp
390 (wesnoth-element-opening t
)
392 (setq iterations
(1+ iterations
))))
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."
400 (or repeat
(setq repeat
1))
402 (wesnoth-forward-element (abs repeat
))
403 (let ((iterations 0))
404 (while (< iterations repeat
)
406 (search-backward-regexp
407 (wesnoth-element-opening t
)
410 (search-forward-regexp "[^[:blank:]]")
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."
423 (search-backward nil
))
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
435 (goto-char (point-min))
436 (search-forward-regexp
439 (error "%s" "Unable to locate tag to jump from")))
444 (or (< open-tags
0) (not search-started
))
445 (search-backward-regexp
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
)))))
454 (search-backward-regexp "\\[\\|#"))
456 (or (> open-tags
0) (not search-started
))
457 (search-forward-regexp
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
))))
468 (setq tag-position
(point)))
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"))
476 (goto-char 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."
484 (goto-char (point-min))
485 (when (search-forward-regexp (wesnoth-element t
) (buffer-size) t
)
489 (defun wesnoth-indent ()
490 "Indent the current line as WML."
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
))
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
))))
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."
521 (dolist (element (or wesnoth-define-blocks
522 (wesnoth-find-macro-definitions)))
523 (when (= (cadr (sort (append (mapcar 'marker-position
(cadr element
))
527 (setq depth
(max (car element
) depth
))))
530 (defun wesnoth-find-macro-definitions ()
531 "Return information regarding positioning of macro definitions."
533 (goto-char (point-min))
536 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t
)
537 (and (string= (match-string 1) "#define") (beginning-of-line))
539 (if (string= (match-string 1) "#define")
541 (add-to-list 'openings
(point-marker))
544 (list depth
(list (car openings
) (point-marker))))
545 (setq openings
(cdr openings
))
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."
558 (setq end
(point-marker))
560 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
561 (or (bolp) (forward-line 1))
562 (while (< (point) end
)
564 (if (looking-at "^[\t ]*$")
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."
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.
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
596 (when (or wesnoth-auto-indent-flag indent
)
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
606 (let ((tag-position nil
)
607 (missing-tag-name nil
))
609 (goto-char (point-min))
611 (search-forward-regexp "^[\t ]*\\[" (point-max) t
)
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))))
621 (goto-char tag-position
)
623 (search-backward "[")
624 (message "'%s' is not known to exist"
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."
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)
645 (let ((unmatched-tag-list '())
650 (while (and (search-forward-regexp
653 (not error-position
))
654 (search-backward-regexp
657 (if (looking-at wesnoth-preprocessor-regexp
)
658 (let ((preprocessor-name (match-string-no-properties 1)))
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)
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))))))
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"))))
696 (if (not (or unmatched-tag-list error-position
))
697 (message "%s" "Structure appears consistent.")
699 (goto-char error-position
))
700 (if (string= expected
"Unexpected end of file")
701 (message "Error %s" expected
)
702 (message "Error: Expecting %s"
705 (concat " [/" (car unmatched-tag-list
) "]")))))
706 (when (or expected unmatched-tag-list
)
707 (or expected
(concat "[/" (car unmatched-tag-list
) "]"))))))
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
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