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.
95 (defconst wesnoth-mode-version
"1.2.4"
96 "The current version of wesnoth-mode.")
98 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
102 (defcustom wesnoth-indent-preprocessor-bol t
103 "Style used to indent preprocessor statements.
104 If non-nil, preprocessor statements are indented to the first
105 column. Otherwise, they are indented as tags."
107 :group
'wesnoth-mode
)
109 (defcustom wesnoth-indent-savefile t
110 "Style used to indent WML elements.
111 If non-nil, attributes will be indented further than their
112 containing element. Otherwise, attributes will be indented at
115 :group
'wesnoth-mode
)
117 (defcustom wesnoth-auto-indent-flag t
118 "Whether to attempt tag indentation when a newline is created.
119 If nil, no indentation will be attempted. Otherwise, attempt to
122 :group
'wesnoth-mode
)
124 (defcustom wesnoth-base-indent
4
125 "The number of columns to indent WML."
127 :group
'wesnoth-mode
)
129 (defconst wesnoth-preprocessor-regexp
130 "[\t ]*#\\(?:enddef\\|define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def\\)"
131 "Regular expression to match all preprocessor statements.")
133 (defconst wesnoth-preprocessor-opening-regexp
134 "[\t ]*#\\(?:define \\|else\\|ifdef \\|ifndef \\)"
135 "Regular expression to match \"opening\" preprocessor statements.")
137 (defconst wesnoth-preprocessor-closing-regexp
138 "[\t ]*#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)"
139 "Regular expression to match \"closing\" preprocessor statements.")
141 (defvar wesnoth-define-blocks
'()
142 "Cache of all toplevel #define and #enddef pairs.")
144 (defvar wesnoth-mode-hook nil
)
146 (defvar wesnoth-mode-map
147 (let ((map (make-sparse-keymap)))
148 (define-key map
"\C-\M-a" 'wesnoth-backward-element
)
149 (define-key map
"\C-\M-e" 'wesnoth-forward-element
)
150 (define-key map
"\C-c\C-m" 'wesnoth-jump-to-matching
)
151 (define-key map
"\C-cm" 'wesnoth-jump-to-matching
)
152 (define-key map
"\C-m" 'wesnoth-newline
)
153 (define-key map
"\C-j" 'wesnoth-newline-and-indent
)
154 (define-key map
"\C-c\C-c" 'wesnoth-check-structure
)
155 (define-key map
"\C-cc" 'wesnoth-check-structure
)
156 (define-key map
"\C-c\C-n" 'wesnoth-check-tag-names
)
157 (define-key map
"\C-cn" 'wesnoth-check-tag-names
)
158 (define-key map
"\C-c\C-e" 'wesnoth-insert-tag
)
159 (define-key map
"\C-ce" 'wesnoth-insert-tag
)
160 (define-key map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
161 (define-key map
(kbd "C-c /") 'wesnoth-insert-missing-closing
)
163 (define-key map
[menu-bar wesnoth
]
164 (cons "WML" (make-sparse-keymap "WML")))
165 (define-key map
[menu-bar wesnoth insert-tag
]
166 '("Insert Tag" . wesnoth-insert-tag
))
167 (define-key map
[menu-bar wesnoth check-names
]
168 '("Check Tag Names" . wesnoth-check-tag-names
))
169 (define-key map
[menu-bar wesnoth check-structure
]
170 '("Check Structure" . wesnoth-check-structure
))
171 (define-key map
[menu-bar wesnoth jump-to-matching
]
172 '("Jump to Matching" . wesnoth-jump-to-matching
))
173 (define-key map
[menu-bar wesnoth insert-missing-closing
]
174 '("Insert Missing Tag" . wesnoth-insert-missing-closing
))
176 "Keymap used in wesnoth mode.")
178 (defvar wesnoth-syntax-table
179 (let ((wesnoth-syntax-table (make-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 ?.
"_" wesnoth-syntax-table
)
184 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
185 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
186 wesnoth-syntax-table
)
187 "Syntax table for wesnoth-mode.")
189 ;; Prevents automatic syntax-highlighting of elements which might be
190 ;; pre-processor statements.
191 (defvar wesnoth-syntactic-keywords
193 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\|#enddef\\)" 1 "w")
194 '("\\(#[\t ]*.*$\\)" 1 "<"))
195 "Highlighting syntactic keywords within wesnoth-mode.")
197 (defun wesnoth-preprocessor-best-face ()
198 "Use `font-lock-preprocessor-face' when available."
199 (if (boundp 'font-lock-preprocessor-face
)
200 (copy-face 'font-lock-preprocessor-face
'wesnoth-preprocessor-face
)
201 (copy-face 'font-lock-keyword-face
'wesnoth-preprocessor-face
)))
203 (defvar wesnoth-font-lock-keywords
205 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" .
'wesnoth-preprocessor-face
)
206 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
207 2 font-lock-function-name-face
)
208 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
209 'wesnoth-preprocessor-face
)
210 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)"
211 (1 font-lock-function-name-face
))
212 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)"
213 (1 font-lock-function-name-face
))
214 '("}" . font-lock-function-name-face
)
215 '("\\[[^]]+\\]" . font-lock-type-face
)
216 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face
)
217 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
218 1 font-lock-variable-name-face
))
219 "Syntax highlighting for wesnoth-mode.")
221 (defun wesnoth-element-closing (&optional all
)
222 "Return string to use for a closing element."
223 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
224 (concat "^[ \t]*\\[/\\|"
225 wesnoth-preprocessor-closing-regexp
)
226 "^[ \t]*\\(\\[/\\|#enddef\\)"))
228 (defun wesnoth-element-opening (&optional all
)
229 "Return string to use for an opening element."
230 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
231 (concat "^[ \t]*\\[[^/]\\|"
232 wesnoth-preprocessor-opening-regexp
)
233 "^[ \t]*\\(\\[[^/]\\|#define\\)"))
235 (defun wesnoth-element (&optional all
)
236 "Return string to use for an opening or closing element."
237 (if (or (not wesnoth-indent-preprocessor-bol
) all
)
238 (concat "^[\t ]*\\[/?\\|"
239 wesnoth-preprocessor-regexp
)
240 "^[\t ]*\\(\\[/?\\|#define\\|#enddef\\)"))
243 (defvar wesnoth-tags-list
245 "abilities" "about" "advances" "advancefrom" "ai" "allow_recruit" "and"
246 "animation" "array" "attack" "attacks" "avoid" "binary_path" "bold"
247 "campaign" "capture_village" "choose""clear_variable" "colour_adjust"
248 "command" "deaths" "debug_message" "defend" "defends" "defense" "delay"
249 "destination" "disallow_recruit" "do" "effect" "else" "end_turn" "endlevel"
250 "entry" "era" "event" "expenses" "filter" "filter_adjacent_location"
251 "filter_location" "filter_radius" "filter_second" "filter_vision" "format"
252 "frame" "game_config" "generator" "gold" "have_unit" "header" "hide_unit"
253 "if" "illuminated_time" "image" "img" "income" "italic" "item" "jump"
254 "kill" "killed" "label" "language" "leader_goal" "main_map" "menu"
255 "message" "mini_map" "missile_frame" "modifications" "modify_side"
256 "modify_turns" "move" "move_unit_fake" "movement_costs" "movetype"
257 "multiplayer" "multiplayer_side" "music" "not" "num_units" "object"
258 "objectives" "objective" "observers" "option" "or" "panel" "part"
259 "place_shroud" "position" "print" "protect_location" "protect_unit" "race"
260 "random" "recall" "recalls" "recruit" "recruits" "redraw" "ref"
261 "remove_shroud" "remove_unit_overlay" "removeitem" "replay" "replay_start"
262 "resistance" "resolution" "results" "role" "save" "scenario" "scroll"
263 "scroll_to" "scroll_to_unit" "section" "set_menu_item" "set_recruit"
264 "set_variable" "show_if" "side" "side_playing" "snapshot" "sound" "source"
265 "specials" "statistics" "status" "stone" "store_gold" "store_locations"
266 "store_starting_location" "store_side" "store_unit" "story" "target" "team"
267 "teleport" "teleport_anim" "terrain" "terrain_graphics" "test" "text_input"
268 "textdomain" "theme" "then" "tile" "time" "time_area" "time_of_day" "topic"
269 "toplevel" "trait" "turn" "tutorial" "unhide_unit" "unit" "unit_abilities"
270 "unit_alignment" "unit_description" "unit_hp" "unit_image" "unit_level"
271 "unit_moves" "unit_overlay" "unit_profile" "unit_status" "unit_traits"
272 "unit_type" "unit_weapons" "unit_xp" "units" "unstone" "unstore_unit"
273 "upkeep" "variable" "variables" "village" "villages" "while" "wml_filter")
274 "A list containing all tags which are available in WML.")
276 (defvar wesnoth-completion-cache
'()
277 "List of tags which have been generated by `wesnoth-build-completion'.")
279 (defun wesnoth-build-completion (&optional rebuild
)
280 "Create a new list for tag completion if necessary.
281 If REBUILD is non-nil, regenerate `wesnoth-completion-cache'."
283 (if (> emacs-major-version
21)
285 (if (and wesnoth-completion-cache
(not rebuild
))
286 wesnoth-completion-cache
289 (dolist (tag wesnoth-tags-list
)
290 (setq iter
(1+ iter
))
291 (setq tags
(append tags
(list (cons tag iter
)))))
292 (setq wesnoth-completion-cache tags
)))))
294 (defun wesnoth-insert-tag (tagname &optional start end
)
295 "Inserts the specified opening tag and it's matching closing tag.
296 Both the opening and closing tags will be placed on their own
297 lines with point positioned between them. Completion of tags at
298 the prompt uses `wesnoth-tags-list'.
300 TAGNAME is the name of the tag to be inserted. If START and END
301 are given, the tags will be inserted around the specified region.
302 Enabling `transient-mark-mode' will cause `wesnoth-insert-tag' to
303 insert opening and closing tags around the specified region."
305 (list (completing-read "Tag: " (wesnoth-build-completion) nil nil
)))
306 (when (and (not (or start end
)) transient-mark-mode mark-active
)
307 (setq start
(region-beginning)
308 end
(copy-marker (region-end))))
312 (or (looking-at "^[\t ]*$")
314 (insert "[" tagname
"]")
317 (if (looking-at "^[\t ]*$")
321 (insert "[/" tagname
"]")
322 (setq end
(point)) ;; New target for indent-region
323 (indent-region start end nil
))
325 (or (looking-at "^[\t ]*$")
330 (wesnoth-insert-and-indent "[" tagname
"]")
331 (wesnoth-insert-and-indent "\n")
333 (wesnoth-insert-and-indent "\n[/" tagname
"]"))))
335 (defun wesnoth-insert-missing-closing (&optional start end
)
336 "Insert the next expected closing element at point.
338 START and END define the region to check for missing closing
339 elements. If `transient-mark-mode' is enabled, the region
340 specified will be used as START and END. Otherwise, START and
341 END will be the minimum and maximum positions of the buffer,
344 (if (and transient-mark-mode mark-active
)
345 (setq start
(region-beginning)
346 end
(copy-marker (region-end)))
347 (setq start
(point-min)
349 (let ((element (wesnoth-check-structure start end
)))
351 (if (string= element
"Unexpected end of file")
352 (message "%s" "Error: Expected end of file")
353 (when (not (looking-at "^[\t ]*$"))
359 (error "%s" "Unable to find element to insert"))))
361 (defun wesnoth-insert-and-indent (&rest args
)
362 "Concatenate and insert the given string(s) before indenting."
363 (insert (apply 'concat args
))
367 (defun wesnoth-newline (&optional indent
)
368 "Indent both the current line and the newline created.
369 If `wesnoth-auto-indent-flag' is nil, indentation will not be
370 performed. Indentation can be forced by setting INDENT to
374 (when (and (or wesnoth-auto-indent-flag indent
)
375 (not (looking-at "^[\t ]*$")))
380 (defun wesnoth-forward-element (repeat)
381 "Move point to the end of the next tag.
382 REPEAT is an optional numeric argument. If REPEAT is non-nil,
383 jump forward the specified number of tags."
385 (or repeat
(setq repeat
1))
387 (wesnoth-backward-element (abs repeat
))
388 (let ((iterations 0))
389 (while (< iterations repeat
)
391 (search-forward-regexp
392 (wesnoth-element-opening t
)
394 (setq iterations
(1+ iterations
))))
397 (defun wesnoth-backward-element (repeat)
398 "Move point to the beginning of the previous tag.
399 REPEAT is an optional numeric argument. If REPEAT is non-nil,
400 jump backward the specified number of tags."
402 (or repeat
(setq repeat
1))
404 (wesnoth-forward-element (abs repeat
))
405 (let ((iterations 0))
406 (while (< iterations repeat
)
408 (search-backward-regexp
409 (wesnoth-element-opening t
)
412 (search-forward-regexp "[^[:blank:]]")
414 (setq iterations
(1+ iterations
))))))
416 (defun wesnoth-jump-to-matching ()
417 "Jump point to the matching opening/closing tag.
418 A tag must be on the same line as point for jumping to occur.
419 Tag structure between the start and target positions must be
420 consistent for jumping to occur."
425 (search-backward nil
))
430 (when (looking-at (wesnoth-element-closing t
))
431 (setq search-backward t
))
432 (if (wesnoth-wml-start-pos)
433 (if (> (point) (wesnoth-wml-start-pos))
434 (search-backward-regexp
437 (goto-char (point-min))
438 (search-forward-regexp
441 (error "%s" "Unable to locate tag to jump from")))
446 (or (< open-tags
0) (not search-started
))
447 (search-backward-regexp
450 (setq search-started t
)
451 (if (looking-at (wesnoth-element-opening t
))
452 (setq open-tags
(1+ open-tags
))
453 (when (looking-at (wesnoth-element-closing t
))
454 (setq open-tags
(1- open-tags
)))))
456 (search-backward-regexp "\\[\\|#"))
458 (or (> open-tags
0) (not search-started
))
459 (search-forward-regexp
463 (setq search-started t
)
464 (if (looking-at (wesnoth-element-opening t
))
465 (setq open-tags
(1+ open-tags
))
466 (when (looking-at (wesnoth-element-closing t
))
467 (setq open-tags
(1- open-tags
))))
470 (setq tag-position
(point)))
473 (and (wesnoth-check-structure (min (point) tag-position
)
474 (max (point) tag-position
))
475 (message "%s" "Region concerning jump does not nest correctly; \
476 target may not be correct"))
478 (goto-char tag-position
)
481 (defun wesnoth-wml-start-pos ()
482 "Determine the position of `point' relative to where the actual WML begins.
483 Return the likely starting position of the WML if it is found.
484 Otherwise return nil."
486 (goto-char (point-min))
487 (when (search-forward-regexp (wesnoth-element t
) (buffer-size) t
)
491 (defun wesnoth-indent ()
492 "Indent the current line as WML."
494 (multiple-value-bind (context ref-indent
)
495 (wesnoth-determine-context (point))
496 (if (or (not (wesnoth-wml-start-pos))
497 (<= (point) (wesnoth-wml-start-pos))
498 (nth 3 (parse-partial-sexp (point-min) (point)))
499 (and wesnoth-indent-preprocessor-bol
500 (looking-at wesnoth-preprocessor-regexp
))
505 ((eq context
'opening
)
506 (if (or (looking-at "^[\t ]*\\[[^/]")
507 (looking-at (wesnoth-element-opening))
508 (and (not (looking-at (wesnoth-element-closing)))
509 wesnoth-indent-savefile
))
510 (setq cur-indent
(+ ref-indent wesnoth-base-indent
))
511 (setq cur-indent ref-indent
)))
512 ((eq context
'closing
)
513 (if (looking-at "^[\t ]*\\[/")
514 (setq cur-indent
(- ref-indent wesnoth-base-indent
))
515 (setq cur-indent ref-indent
))))
517 (indent-line-to (max cur-indent
0)))))))
519 (defun wesnoth-within-define (position)
520 "Determine whether point is currently inside a #define block.
521 POSITION is the initial cursor position."
523 (dolist (element (or wesnoth-define-blocks
524 (wesnoth-find-macro-definitions)))
525 (when (= (cadr (sort (append (mapcar 'marker-position
(cadr element
))
529 (setq depth
(max (car element
) depth
))))
532 (defun wesnoth-find-macro-definitions ()
533 "Return information regarding positioning of macro definitions."
535 (goto-char (point-min))
538 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t
)
539 (and (string= (match-string 1) "#define") (beginning-of-line))
541 (if (string= (match-string 1) "#define")
543 (add-to-list 'openings
(point-marker))
546 (list depth
(list (car openings
) (point-marker))))
547 (setq openings
(cdr openings
))
552 (defun wesnoth-indent-region (start end
)
553 "Indent the region from START to END.
555 Creates and destroys a cache of macro definition details as necessary."
560 (setq end
(point-marker))
562 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
563 (or (bolp) (forward-line 1))
564 (while (< (point) end
)
566 (if (looking-at "^[\t ]*$")
568 (funcall indent-line-function
))))
569 (setq wesnoth-define-blocks nil
)))
571 (defun wesnoth-determine-context (position)
572 "Determine the type of the last relevant element."
574 (search-backward-regexp (wesnoth-element) (wesnoth-wml-start-pos) t
)
575 (let ((match (or (match-string 1) ""))
576 (depth (wesnoth-within-define position
)))
577 (while (and (> (wesnoth-within-define (point)) depth
)
578 (not (= (point) (wesnoth-wml-start-pos))))
579 (search-backward-regexp (wesnoth-element)
580 (wesnoth-wml-start-pos) t
)
581 (setq match
(match-string 1)))
582 (when (and (= (point) (wesnoth-wml-start-pos)) (= depth
0)
583 (string-match "#define" match
))
584 ;; Found nothing of use; reset match and assume top-level tag.
587 ((string-match "\\[/\\|#enddef" match
)
588 (values 'closing
(current-indentation)))
589 ((string-match "\\[[^/]?\\|#define" match
)
590 (values 'opening
(current-indentation)))))))
592 (defun wesnoth-newline-and-indent (&optional indent
)
593 "Indent both the current line and the newline created.
594 If `wesnoth-auto-indent-flag' is nil, indentation will not be
598 (when (or wesnoth-auto-indent-flag indent
)
602 (defun wesnoth-check-tag-names ()
603 "Check the names of all tags in the buffer for correctness.
604 If a tag is found which is not present in the list an error will
605 be signalled and point will be moved to the corresponding
608 (let ((tag-position nil
)
609 (missing-tag-name nil
))
611 (goto-char (point-min))
613 (search-forward-regexp "^[\t ]*\\[" (point-max) t
)
616 (when (looking-at "^[\t ]*\\[[/]?\\(\\(\\(\\w\\|_\\)+\\|_\\)\\)\\]")
617 (unless (member (match-string-no-properties 1) wesnoth-tags-list
)
618 (setq tag-position
(point))
619 (setq missing-tag-name
(match-string-no-properties 1))))
623 (goto-char tag-position
)
625 (search-backward "[")
626 (message "'%s' is not known to exist"
628 (message "%s" "No unknown tag names found."))))
630 (defun wesnoth-check-structure (&optional start end
)
631 "Check the buffer for correct nesting of elements.
632 If a problem is found in the structure, point will be placed at
633 the location which an element was expected and the expected
634 element will be displayed in the mini-buffer.
636 START and END define the region to be checked. If
637 `transient-mark-mode' is enabled, the region specified will be
638 checked. Otherwise START and END will be the minimum and maximum
639 positions of the buffer, respectively."
641 (unless (or start end
)
642 (if (and transient-mark-mode mark-active
)
643 (setq start
(region-beginning)
644 end
(copy-marker (region-end)))
645 (setq start
(point-min)
647 (let ((unmatched-tag-list '())
652 (while (and (search-forward-regexp
655 (not error-position
))
656 (search-backward-regexp
659 (if (looking-at wesnoth-preprocessor-regexp
)
660 (let ((preprocessor-name (match-string-no-properties 1)))
665 (string= preprocessor-name preproc
))
666 '("define" "ifdef" "ifndef")))
667 (setq unmatched-tag-list
668 (cons preprocessor-name unmatched-tag-list
)))
669 ((string= preprocessor-name
"else")
670 (unless (string-match "ifn?def" (car unmatched-tag-list
))
671 (setq error-position
(point))))
672 ((string= preprocessor-name
"endif")
673 (if (string-match "ifn?def" (car unmatched-tag-list
))
674 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
675 (setq error-position
(point))))
676 ((string= preprocessor-name
"enddef")
677 (if (string= (car unmatched-tag-list
) "define")
678 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
679 (setq error-position
(point))))))
680 (if (looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
681 (setq unmatched-tag-list
682 (cons (match-string-no-properties 1)
684 (when (looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
685 (if (string= (match-string-no-properties 1)
686 (car unmatched-tag-list
))
687 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
688 (setq error-position
(point))))))
690 (when unmatched-tag-list
691 (cond ((string= (car unmatched-tag-list
) "define")
692 (setq expected
"#enddef"))
693 ((string-match "ifn?def" (car unmatched-tag-list
))
694 (setq expected
"#endif"))
695 ((not unmatched-tag-list
)
696 (setq expected
"Unexpected end of file"))))
698 (if (not (or unmatched-tag-list error-position
))
699 (message "%s" "Structure appears consistent.")
701 (goto-char error-position
))
702 (if (string= expected
"Unexpected end of file")
703 (message "Error %s" expected
)
704 (message "Error: Expecting %s"
707 (concat " [/" (car unmatched-tag-list
) "]")))))
708 (when (or expected unmatched-tag-list
)
709 (or expected
(concat "[/" (car unmatched-tag-list
) "]"))))))
712 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
713 "Major mode for editing WML."
714 (wesnoth-preprocessor-best-face)
715 (set-syntax-table wesnoth-syntax-table
)
716 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
717 (set (make-local-variable 'comment-start
) "#")
718 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
719 (set (make-local-variable 'indent-region-function
) 'wesnoth-indent-region
)
720 (set (make-local-variable 'font-lock-defaults
)
721 '(wesnoth-font-lock-keywords
723 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
724 (setq indent-tabs-mode nil
)
725 (setq mode-name
"WML")
726 (run-hooks 'wesnoth-mode-hook
))
728 (provide 'wesnoth-mode
)
730 ;;; wesnoth-mode.el ends here