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 ;; * Added support for several new tags.
36 ;; * Added menu entry for wesnoth-mode.
37 ;; * Significant speed increase to indentation.
38 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
39 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
41 ;; * Trailing whitespace is no longer created when creating a second consecutive
44 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
45 ;; `wesnoth-element', `wesnoth-element-opening',
46 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
47 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
48 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
49 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
50 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
51 ;; * Fixed a bug in indentation where content was needed between elements pairs
52 ;; for indentation to work.
53 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
54 ;; `wesnoth-auto-indent-flag'.
55 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
56 ;; * Added indentation styles: `wesnoth-indent-default',
57 ;; `wesnoth-indent-withtags' which implement a a similar indentation
58 ;; style to the existing styles, however all preprocessor statements are
59 ;; indented to the first column.
60 ;; * Added support for several new tags.
61 ;; * Modified `wesnoth-newline' to behave more consistently.
62 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
63 ;; now leaves point at the beginning (when moving backward) or end (when
64 ;; moving forward) of the match.
65 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
66 ;; will now work on preprocessor statements. Will now warn if jump
67 ;; destination may not be correct (due to errors in WML structure).
68 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
69 ;; * `wesnoth-check-structure' can now be applied over an active region and
70 ;; now checks preprocessor statements for correct nesting.
71 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
72 ;; perform indentation by providing a prefix argument.
73 ;; * Indentation styles now leave point at the first non-whitespace character of
75 ;; * `wesnoth-check-tag-names' now reports on success.
76 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
77 ;; * `outline-minor-mode' now works on macro definitions.
79 ;; * Base indent now defaults to 4.
80 ;; * Added support for #ifndef.
82 (defconst wesnoth-mode-version
"1.2.2+git"
83 "The current version of wesnoth-mode.")
85 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
89 (defcustom wesnoth-indent-preprocessor-bol t
90 "Style used to indent preprocessor statements.
91 If non-nil, preprocessor statements are indented to the first
92 column. Otherwise, they are indented as tags."
96 (defcustom wesnoth-indent-savefile t
97 "Style used to indent WML elements.
98 If non-nil, attributes will be indented further than their
99 containing element. Otherwise, attributes will be indented at
102 :group
'wesnoth-mode
)
104 (defcustom wesnoth-auto-indent-flag t
105 "Whether to attempt tag indentation when a newline is created.
106 If nil, no indentation will be attempted. Otherwise, attempt to
109 :group
'wesnoth-mode
)
111 (defcustom wesnoth-base-indent
4
112 "The number of columns to indent WML."
114 :group
'wesnoth-mode
)
116 (defconst wesnoth-preprocessor-regexp
117 "#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)"
118 "Regular expression to match all preprocessor statements.")
120 (defconst wesnoth-preprocessor-opening-regexp
121 "#\\(?:define \\|else\\|ifdef \\|ifndef \\)"
122 "Regular expression to match \"opening\" preprocessor statements.")
124 (defconst wesnoth-preprocessor-closing-regexp
125 "#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)"
126 "Regular expression to match \"closing\" preprocessor statements.")
128 (defvar wesnoth-mode-hook nil
)
130 (defvar wesnoth-mode-map
()
131 "Keymap used in wesnoth mode.")
132 (unless wesnoth-mode-map
133 (setq wesnoth-mode-map
(make-sparse-keymap))
134 (define-key wesnoth-mode-map
"\C-\M-a" 'wesnoth-backward-element
)
135 (define-key wesnoth-mode-map
"\C-\M-e" 'wesnoth-forward-element
)
136 (define-key wesnoth-mode-map
"\C-c\C-m" 'wesnoth-jump-to-matching
)
137 (define-key wesnoth-mode-map
"\C-cm" 'wesnoth-jump-to-matching
)
138 (define-key wesnoth-mode-map
"\C-m" 'wesnoth-newline
)
139 (define-key wesnoth-mode-map
"\C-j" 'wesnoth-newline-and-indent
)
140 (define-key wesnoth-mode-map
"\C-c\C-c" 'wesnoth-check-structure
)
141 (define-key wesnoth-mode-map
"\C-cc" 'wesnoth-check-structure
)
142 (define-key wesnoth-mode-map
"\C-c\C-n" 'wesnoth-check-tag-names
)
143 (define-key wesnoth-mode-map
"\C-cn" 'wesnoth-check-tag-names
)
144 (define-key wesnoth-mode-map
"\C-c\C-e" 'wesnoth-insert-tag
)
145 (define-key wesnoth-mode-map
"\C-ce" 'wesnoth-insert-tag
)
146 (define-key wesnoth-mode-map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
147 (define-key wesnoth-mode-map
(kbd "C-c /") 'wesnoth-insert-missing-closing
)
149 (define-key wesnoth-mode-map
[menu-bar wesnoth
]
150 (cons "WML" (make-sparse-keymap "WML")))
151 (define-key wesnoth-mode-map
[menu-bar wesnoth insert-tag
]
152 '("Insert Tag" . wesnoth-insert-tag
))
153 (define-key wesnoth-mode-map
[menu-bar wesnoth check-names
]
154 '("Check Tag Names" . wesnoth-check-tag-names
))
155 (define-key wesnoth-mode-map
[menu-bar wesnoth check-structure
]
156 '("Check Structure" . wesnoth-check-structure
))
157 (define-key wesnoth-mode-map
[menu-bar wesnoth jump-to-matching
]
158 '("Jump to Matching" . wesnoth-jump-to-matching
))
159 (define-key wesnoth-mode-map
[menu-bar wesnoth insert-missing-closing
]
160 '("Insert Missing Tag" . wesnoth-insert-missing-closing
)))
162 (defvar wesnoth-syntax-table
163 (let ((wesnoth-syntax-table (make-syntax-table)))
164 (modify-syntax-entry ?
= "." wesnoth-syntax-table
)
165 (modify-syntax-entry ?\_
"w" wesnoth-syntax-table
)
166 (modify-syntax-entry ?-
"_" wesnoth-syntax-table
)
167 (modify-syntax-entry ?.
"_" wesnoth-syntax-table
)
168 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
169 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
170 wesnoth-syntax-table
)
171 "Syntax table for wesnoth-mode.")
173 ;; Prevents automatic syntax-highlighting of elements which might be
174 ;; pre-processor statements.
175 (defvar wesnoth-syntactic-keywords
177 '("^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)" 1 "w")
178 '("\\(#[\t ]*.*$\\)" 1 "<"))
179 "Highlighting syntactic keywords within wesnoth-mode.")
181 (defvar wesnoth-font-lock-keywords
183 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)"
184 1 font-lock-preprocessor-face
)
185 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\w+\\)"
186 2 font-lock-function-name-face
)
187 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" . font-lock-preprocessor-face
)
188 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)"
189 (1 font-lock-function-name-face
))
190 '("\\({\\(\\w\\|:\\)+\\|{[~@]?\\)"
191 (1 font-lock-function-name-face
))
192 '("}" . font-lock-function-name-face
)
193 '("\\[[^]]+\\]" . font-lock-type-face
)
194 '("\\$\\w+" . font-lock-variable-name-face
)
195 '("\\(\\w+\\(\\,[\t ]*\\w+\\)*\\)="
196 1 font-lock-variable-name-face
))
197 "Syntax highlighting for wesnoth-mode.")
199 (defun wesnoth-element-closing (&optional all
)
200 "Return string to use for a closing element."
201 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
202 (concat "^[ \t]*\\(\\[/.+\\]\\|\\("
203 wesnoth-preprocessor-closing-regexp
"\\)\\)")
204 "^[ \t]*\\(\\[/.+\\]\\|#enddef\\)"))
206 (defun wesnoth-element-opening (&optional all
)
207 "Return string to use for an opening element."
208 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
209 (concat "^[ \t]*\\(\\[[^/]*?\\]\\|\\("
210 wesnoth-preprocessor-opening-regexp
"\\)\\)")
211 "^[ \t]*\\(\\[[^/]*?\\]\\|#define\\)"))
213 (defun wesnoth-element (&optional all
)
214 "Return string to use for an opening or closing element."
215 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
216 (concat "^[\t ]*\\(\\[\\|"
217 wesnoth-preprocessor-regexp
"\\)")
218 "^[\t ]*\\(\\[\\|#define\\|#enddef\\)"))
221 (defvar wesnoth-tags-list
223 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
224 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
225 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
226 "command" "deaths" "debug_message" "defend" "defends" "defense" "delay"
227 "destination" "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel"
228 "entry" "era" "event" "expenses" "filter" "filter_adjacent_location"
229 "filter_location" "filter_radius" "filter_second" "filter_vision" "format"
230 "frame" "game_config" "generator" "gold" "have_unit" "header" "hide_unit"
231 "if" "illuminated_time" "image" "img" "income" "italic" "item" "jump"
232 "kill" "killed" "label" "language" "leader_goal" "main_map" "menu"
233 "message" "mini_map" "missile_frame" "modifications" "modify_side"
234 "modify_turns" "move" "move_unit_fake" "movement_costs" "movetype"
235 "multiplayer" "multiplayer_side" "music" "not" "num_units" "object"
236 "objectives" "objective" "observers" "option" "or" "panel" "part"
237 "place_shroud" "position" "print" "protect_location" "protect_unit" "race"
238 "random" "recall" "recalls" "recruit" "recruits" "redraw" "ref"
239 "remove_shroud" "remove_unit_overlay" "removeitem" "replay" "replay_start"
240 "resistance" "resolution" "results" "role" "save" "scenario" "scroll"
241 "scroll_to" "scroll_to_unit" "section" "set_menu_item" "set_recruit"
242 "set_variable" "show_if" "side" "side_playing" "snapshot" "sound" "source"
243 "specials" "statistics" "status" "stone" "store_gold" "store_locations"
244 "store_starting_location" "store_side" "store_unit" "story" "target" "team"
245 "teleport" "teleport_anim" "terrain" "terrain_graphics" "test" "text_input"
246 "textdomain" "theme" "then" "tile" "time" "time_area" "time_of_day" "topic"
247 "toplevel" "trait" "turn" "tutorial" "unhide_unit" "unit" "unit_abilities"
248 "unit_alignment" "unit_description" "unit_hp" "unit_image" "unit_level"
249 "unit_moves" "unit_overlay" "unit_profile" "unit_status" "unit_traits"
250 "unit_type" "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit"
251 "upkeep" "variable" "variables" "village" "villages" "while" "wml_filter")
252 "A list containing all tags which are available in WML.")
254 (defvar wesnoth-completion-cache
'()
255 "List of tags which have been generated by `wesnoth-build-completion'.")
257 (defun wesnoth-build-completion (&optional rebuild
)
258 "Create a new list for tag completion if necessary.
259 If REBUILD is non-nil, regenerate `wesnoth-completion-cache'."
261 (if (> emacs-major-version
21)
263 (if (and wesnoth-completion-cache
(not rebuild
))
264 wesnoth-completion-cache
267 (dolist (tag wesnoth-tags-list
)
268 (setq iter
(1+ iter
))
269 (setq tags
(append tags
(list (cons tag iter
)))))
270 (setq wesnoth-completion-cache tags
)))))
272 (defun wesnoth-insert-tag (tagname &optional start end
)
273 "Inserts the specified opening tag and it's matching closing tag.
274 Both the opening and closing tags will be placed on their own
275 lines with point positioned between them. Completion of tags at
276 the prompt uses `wesnoth-tags-list'.
278 TAGNAME is the name of the tag to be inserted. If START and END
279 are given, the tags will be inserted around the specified region.
280 Enabling `transient-mark-mode' will cause `wesnoth-insert-tag' to
281 insert opening and closing tags around the specified region."
283 (list (completing-read "Tag: " (wesnoth-build-completion) nil nil
)))
284 (when (and (not (or start end
)) transient-mark-mode mark-active
)
285 (setq start
(region-beginning)
286 end
(copy-marker (region-end))))
290 (or (looking-at "^[\t ]*$")
292 (insert "[" tagname
"]")
295 (or (looking-at "^[\t ]*$")
300 (insert "[/" tagname
"]\n")
301 (setq end
(point)) ;; New target for indent-region
302 (indent-region start end
))
304 (or (looking-at "^[\t ]*$")
309 (wesnoth-insert-and-indent "[" tagname
"]")
310 (wesnoth-insert-and-indent "\n")
312 (wesnoth-insert-and-indent "\n[/" tagname
"]"))))
314 (defun wesnoth-insert-missing-closing (&optional start end
)
315 "Insert the next expected closing element at point.
317 START and END define the region to check for missing closing
318 elements. If `transient-mark-mode' is enabled, the region
319 specified will be used as START and END. Otherwise, START and
320 END will be the minimum and maximum positions of the buffer,
323 (if (and transient-mark-mode mark-active
)
324 (setq start
(region-beginning)
325 end
(copy-marker (region-end)))
326 (setq start
(point-min)
328 (let ((element (wesnoth-check-structure start end
)))
330 (if (string= element
"Unexpected end of file")
331 (message "%s" "Error: Expected end of file")
332 (when (not (looking-at "^[\t ]*$"))
338 (error "%s" "Unable to find element to insert"))))
340 (defun wesnoth-insert-and-indent (&rest args
)
341 "Concatenate and insert the given string(s) before indenting."
342 (insert (apply 'concat args
))
346 (defun wesnoth-newline (&optional indent
)
347 "Indent both the current line and the newline created.
348 If `wesnoth-auto-indent-flag' is nil, indentation will not be
349 performed. Indentation can be forced by setting INDENT to
353 (when (and (or wesnoth-auto-indent-flag indent
)
354 (not (looking-at "^[\t ]*$")))
359 (defun wesnoth-forward-element (repeat)
360 "Move point to the end of the next tag.
361 REPEAT is an optional numeric argument. If REPEAT is non-nil,
362 jump forward the specified number of tags."
364 (or repeat
(setq repeat
1))
366 (wesnoth-backward-element (abs repeat
))
367 (let ((iterations 0))
368 (while (< iterations repeat
)
370 (search-forward-regexp
371 (wesnoth-element-opening t
)
373 (setq iterations
(1+ iterations
))))
376 (defun wesnoth-backward-element (repeat)
377 "Move point to the beginning of the previous tag.
378 REPEAT is an optional numeric argument. If REPEAT is non-nil,
379 jump backward the specified number of tags."
381 (or repeat
(setq repeat
1))
383 (wesnoth-forward-element (abs repeat
))
384 (let ((iterations 0))
385 (while (< iterations repeat
)
387 (search-backward-regexp
388 (wesnoth-element-opening t
)
391 (search-forward-regexp "[^[:blank:]]")
393 (setq iterations
(1+ iterations
))))))
395 (defun wesnoth-jump-to-matching ()
396 "Jump point to the matching opening/closing tag.
397 A tag must be on the same line as point for jumping to occur.
398 Tag structure between the start and target positions must be
399 consistent for jumping to occur."
404 (search-backward nil
))
409 (when (looking-at (wesnoth-element-closing t
))
410 (setq search-backward t
))
411 (if (wesnoth-wml-start-pos)
412 (if (> (point) (wesnoth-wml-start-pos))
413 (search-backward-regexp
416 (goto-char (point-min))
417 (search-forward-regexp
420 (error "%s" "Unable to locate tag to jump from")))
425 (or (< open-tags
0) (not search-started
))
426 (search-backward-regexp
429 (setq search-started t
)
430 (if (looking-at (wesnoth-element-opening t
))
431 (setq open-tags
(1+ open-tags
))
432 (when (looking-at (wesnoth-element-closing t
))
433 (setq open-tags
(1- open-tags
)))))
435 (search-backward-regexp "\\[\\|#"))
437 (or (> open-tags
0) (not search-started
))
438 (search-forward-regexp
442 (setq search-started t
)
443 (if (looking-at (wesnoth-element-opening t
))
444 (setq open-tags
(1+ open-tags
))
445 (when (looking-at (wesnoth-element-closing t
))
446 (setq open-tags
(1- open-tags
))))
449 (setq tag-position
(point)))
452 (and (wesnoth-check-structure (min (point) tag-position
)
453 (max (point) tag-position
))
454 (message "%s" "Region concerning jump does not nest correctly; \
455 target may not be correct"))
457 (goto-char tag-position
)
460 (defun wesnoth-wml-start-pos ()
461 "Determine the position of `point' relative to where the actual WML begins.
462 Return the likely starting position of the WML if it is found.
463 Otherwise return nil."
465 (goto-char (point-min))
466 (when (search-forward-regexp (wesnoth-element t
) (buffer-size) t
)
470 (defun wesnoth-indent ()
471 "Indent the current line as WML."
473 (if (or (not (wesnoth-wml-start-pos))
474 (<= (point) (wesnoth-wml-start-pos))
475 (nth 3 (parse-partial-sexp (point-min) (point)))
476 (and wesnoth-indent-preprocessor-bol
477 (looking-at (concat "^[\t ]*" wesnoth-preprocessor-regexp
))))
483 ((looking-at (wesnoth-element-closing))
484 (wesnoth-ensure-define-separate)
485 ;; If closing preceded by closing, decrease indentation
486 (when (and (looking-at (wesnoth-element-closing)))
487 (setq cur-indent
(- (current-indentation) wesnoth-base-indent
))))
489 ((looking-at (wesnoth-element-opening))
490 ;; Goto start-pos if unable to find target
491 (wesnoth-ensure-define-separate)
492 ;; If opening preceded by opening, increase indentation
493 (when (or (looking-at (concat (wesnoth-element-opening)))
495 (wesnoth-check-structure (wesnoth-wml-start-pos) (point))))
496 (setq cur-indent
(+ (current-indentation) wesnoth-base-indent
))))
497 ;; Not opening, not closing
499 (wesnoth-ensure-define-separate)
500 ;; Decrease indent if preceded by closing
501 (if wesnoth-indent-savefile
502 (and (looking-at (wesnoth-element-opening))
504 (+ (current-indentation) wesnoth-base-indent
)))
505 (and (looking-at (wesnoth-element-closing))
506 (setq cur-indent
(- (current-indentation)
507 wesnoth-base-indent
))))))
508 ;; Indentation change unnecessary
510 (setq cur-indent
(current-indentation))))
511 (and (< cur-indent
0) (setq cur-indent
0))
512 (unless (and (not cur-indent
) (= (current-indentation) cur-indent
))
513 (indent-line-to cur-indent
))))
515 (search-forward-regexp "[\t ]*"))
517 (defun wesnoth-newline-and-indent (&optional indent
)
518 "Indent both the current line and the newline created.
519 If `wesnoth-auto-indent-flag' is nil, indentation will not be
523 (when (or wesnoth-auto-indent-flag indent
)
526 (defun wesnoth-within-define ()
532 (unless (looking-at "^[\t ]*#enddef")
534 (search-backward-regexp
535 "^[\t ]*\\(#define\\|#enddef\\)" (wesnoth-wml-start-pos) t
)
536 (if (looking-at "^[\t ]*#define")
537 (setq upper
(point)))
539 (search-forward-regexp
540 "^[\t ]*#enddef" (point-max) t
)
542 (if (looking-at "^[\t ]*#enddef")
543 (setq lower
(point))))
544 (if (and upper
(= upper
(wesnoth-wml-start-pos)))
548 (defun wesnoth-ensure-define-separate ()
549 "Ensure elements in #define blocks don't affect those outside."
550 (if (and (not (wesnoth-within-define)) wesnoth-indent-preprocessor-bol
)
552 (search-backward-regexp
554 (wesnoth-wml-start-pos) t
)
555 (while (and (wesnoth-within-define)
556 (not (= (point) (wesnoth-wml-start-pos))))
557 (search-backward-regexp
559 (wesnoth-wml-start-pos) 0)))
560 (search-backward-regexp
562 (wesnoth-wml-start-pos) 0)))
565 (defun wesnoth-check-tag-names ()
566 "Check the names of all tags in the buffer for correctness.
567 If a tag is found which is not present in the list an error will
568 be signalled and point will be moved to the corresponding
571 (let ((tag-position nil
)
572 (missing-tag-name nil
))
574 (goto-char (point-min))
576 (search-forward-regexp "^[\t ]*\\[" (point-max) t
)
579 (when (looking-at "^[\t ]*\\[[/]?\\(\\w+\\)\\]")
580 (unless (member (match-string-no-properties 1) wesnoth-tags-list
)
581 (setq tag-position
(point))
582 (setq missing-tag-name
(match-string-no-properties 1))))
586 (goto-char tag-position
)
588 (search-backward "[")
589 (message "'%s' is not known to exist"
591 (message "%s" "No unknown tag names found."))))
593 (defun wesnoth-check-structure (&optional start end
)
594 "Check the buffer for correct nesting of elements.
595 If a problem is found in the structure, point will be placed at
596 the location which an element was expected and the expected
597 element will be displayed in the mini-buffer.
599 START and END define the region to be checked. If
600 `transient-mark-mode' is enabled, the region specified will be
601 checked. Otherwise START and END will be the minimum and maximum
602 positions of the buffer, respectively."
604 (unless (or start end
)
605 (if (and transient-mark-mode mark-active
)
606 (setq start
(region-beginning)
607 end
(copy-marker (region-end)))
608 (setq start
(point-min)
610 (let ((unmatched-tag-list '())
615 (while (and (search-forward-regexp
616 (concat "^[\t ]*\\(\\[\\|"
617 wesnoth-preprocessor-regexp
"\\)")
619 (not error-position
))
621 (if (looking-at "^[\t ]*#\\(\\w+\\)")
622 (let ((preprocessor-name (match-string-no-properties 1)))
627 (string= preprocessor-name preproc
))
628 '("define" "ifdef" "ifndef")))
629 (setq unmatched-tag-list
630 (cons preprocessor-name unmatched-tag-list
)))
631 ((string= preprocessor-name
"else")
632 (unless (string-match "ifn?def" (car unmatched-tag-list
))
633 (setq error-position
(point))))
634 ((string= preprocessor-name
"endif")
635 (if (string-match "ifn?def" (car unmatched-tag-list
))
636 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
637 (setq error-position
(point))))
638 ((string= preprocessor-name
"enddef")
639 (if (string= (car unmatched-tag-list
) "define")
640 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
641 (setq error-position
(point))))))
642 (if (looking-at "^[\t ]*\\[\\+?\\(\\w+\\)\\]")
643 (setq unmatched-tag-list
644 (cons (match-string-no-properties 1)
646 (when (looking-at "^[\t ]*\\[/\\(\\w+\\)\\]")
647 (if (string= (match-string-no-properties 1)
648 (car unmatched-tag-list
))
649 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
650 (setq error-position
(point))))))
652 (when unmatched-tag-list
653 (cond ((string= (car unmatched-tag-list
) "define")
654 (setq expected
"#enddef"))
655 ((string-match "ifn?def" (car unmatched-tag-list
))
656 (setq expected
"#endif"))
657 ((not unmatched-tag-list
)
658 (setq expected
"Unexpected end of file"))))
660 (if (not (or unmatched-tag-list error-position
))
661 (message "%s" "Structure appears consistent.")
663 (goto-char error-position
))
664 (if (string= expected
"Unexpected end of file")
665 (message "Error %s" expected
)
666 (message "Error: Expecting %s"
669 (concat " [/" (car unmatched-tag-list
) "]")))))
670 (when (or expected unmatched-tag-list
)
671 (or expected
(concat "[/" (car unmatched-tag-list
) "]"))))))
674 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
675 "Major mode for editing WML."
676 (set-syntax-table wesnoth-syntax-table
)
677 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
678 (set (make-local-variable 'comment-start
) "#")
679 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
680 (set (make-local-variable 'font-lock-defaults
)
681 '(wesnoth-font-lock-keywords
683 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
684 (setq indent-tabs-mode nil
)
685 (setq mode-name
"WML")
686 (run-hooks 'wesnoth-mode-hook
))
688 (provide 'wesnoth-mode
)