1 ;;; wesnoth-mode.el --- A major mode for editing WML.
2 ;; Copyright (C) 2006, 2007, 2008 Chris Mann
4 ;; This file is part of wesnoth-mode.
6 ;; This program is free software; you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation; either version 2 of the
9 ;; License, or (at your option) any later version.
11 ;; This program is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with this program; see the file COPYING. If not, write to the
18 ;; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
22 ;; wesnoth-mode is a major mode for Emacs which assists in the editing
23 ;; of Wesnoth Markup Language (WML) files. Currently, this mode
24 ;; features syntax highlighting support, automatic indentation,
25 ;; tag-completion and preliminary support for syntax checking.
28 ;; Add the following to your .emacs:
29 ;; (add-to-list 'load-path "path/to/wesnoth-mode")
30 ;; (autoload 'wesnoth-mode "wesnoth-mode" "Major mode for editing WML." t)
32 ;; (add-to-list 'auto-mode-alist '("\\.cfg\\'" . wesnoth-mode))
33 ;; to automatically load wesnoth-mode for all files ending in '.cfg'.
37 ;; * WML checking is now context sensitive; checks attributes and macros.
38 ;; * WML checks are now always performed on the entire buffer, with results
39 ;; displayed in a temporary buffer.
40 ;; * Context-sensitive completion for attributes and tags.
41 ;; * Completion for built-in and project-specific macros.
42 ;; * Changed the following bindings:
43 ;; `wesnoth-insert-tag' - C-c e -> C-c t
44 ;; `wesnoth-jump-to-matching' - C-c m -> C-c o
45 ;; `wesnoth-check-structure' -> `wesnoth-check-wml' - C-c c
46 ;; * Added the following bindings:
47 ;; `wesnoth-complete-attribute' - C-c a
48 ;; `wesnoth-complete-macro' - C-c m
49 ;; `wesnoth-complete-tag' - C-c t
50 ;; * Removed the following bindings:
51 ;; `wesnoth-check-tag-names' - C-c n
52 ;; * Removed `wesnoth-check-tag-names'. Replaced by `wesnoth-check-wml'.
53 ;; * Completion for an incomplete attribute, tag, or macro at point is
56 ;; * Fixed support for GNU Emacs 21.
57 ;; * Added several new tags to `wesnoth-tags-list'.
58 ;; * Added M-TAB binding for `wesnoth-insert-tag'.
59 ;; * `wesnoth-insert-tag' now takes an optional numeric argument indicating
60 ;; how many blocks to wrap across instead of a region.
61 ;; * Support for `wesnoth-indent-preprocessor-bol' removed.
62 ;; * Fixed a bug in `wesnoth-insert-tag' and `wesnoth-insert-missing-closing'
63 ;; causing tags not to be inserted in the correct position.
64 ;; * Fixed highlighting of array indexes as tags.
66 ;; * Improved syntax-highlighting for macro calls.
67 ;; * Underscore is now treated as whitespace.
68 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
69 ;; * Point is now placed at the first non-whitespace character of the line,
70 ;; instead of the last.
71 ;; * Corrected minor indentation bugs.
72 ;; * Indenting across large regions is now much more efficient.
73 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
76 ;; * Now compatible with GNU Emacs 21.4.
77 ;; * Added support for several new tags.
78 ;; * Added menu entry for wesnoth-mode.
79 ;; * Significant speed increase to indentation.
80 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
81 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
83 ;; * Trailing whitespace is no longer created when creating a second
84 ;; consecutive newline.
85 ;; * Spurious newlines are no longer created when inserting a tag elements
88 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
89 ;; `wesnoth-element', `wesnoth-element-opening',
90 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
91 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
92 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
93 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
94 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
95 ;; * Fixed a bug in indentation where content was needed between elements pairs
96 ;; for indentation to work.
97 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
98 ;; `wesnoth-auto-indent-flag'.
99 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
100 ;; * Added indentation styles: `wesnoth-indent-default',
101 ;; `wesnoth-indent-withtags' which implement a a similar indentation
102 ;; style to the existing styles, however all preprocessor statements are
103 ;; indented to the first column.
104 ;; * Added support for several new tags.
105 ;; * Modified `wesnoth-newline' to behave more consistently.
106 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
107 ;; now leaves point at the beginning (when moving backward) or end (when
108 ;; moving forward) of the match.
109 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
110 ;; will now work on preprocessor statements. Will now warn if jump
111 ;; destination may not be correct (due to errors in WML structure).
112 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
113 ;; * `wesnoth-check-structure' can now be applied over an active region and
114 ;; now checks preprocessor statements for correct nesting.
115 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
116 ;; perform indentation by providing a prefix argument.
117 ;; * Indentation styles now leave point at the first non-whitespace character
119 ;; * `wesnoth-check-tag-names' now reports on success.
120 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
121 ;; * `outline-minor-mode' now works on macro definitions.
123 ;; * Base indent now defaults to 4.
124 ;; * Added support for #ifndef.
128 (require 'wesnoth-wml-data
)
129 (require 'wesnoth-update
)
131 (defconst wesnoth-mode-version
"1.3.0-git"
132 "The current version of `wesnoth-mode'.")
134 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
138 (defcustom wesnoth-auto-indent-flag t
139 "Non-nil means indent the current line upon creating a newline."
141 :group
'wesnoth-mode
)
143 (defcustom wesnoth-indent-savefile t
144 "Non-nil means to use the current indentation conventions.
145 If nil, use the old convention for indentation.
146 The current convention is all attributes are indented a level deeper
147 than their parent; in the past attributes were indented to the same
148 level as their parent.")
150 (defcustom wesnoth-base-indent
4
151 "The number of columns to indent WML."
153 :group
'wesnoth-mode
)
155 (defconst wesnoth-preprocessor-regexp
156 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def\\)"
157 "Regular expression to match all preprocessor statements.")
159 (defconst wesnoth-preprocessor-opening-regexp
160 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
161 "Regular expression to match \"opening\" preprocessor statements.")
163 (defconst wesnoth-preprocessor-closing-regexp
164 "[\t ]*#\\(e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\)"
165 "Regular expression to match \"closing\" preprocessor statements.")
167 (defvar wesnoth-define-blocks
'()
168 "Cache of all toplevel #define and #enddef pairs.")
170 (defvar wesnoth-mode-hook nil
)
172 (defvar wesnoth-mode-map
173 (let ((map (make-sparse-keymap)))
174 (define-key map
(kbd "C-M-a") 'wesnoth-backward-element
)
175 (define-key map
(kbd "C-M-e") 'wesnoth-forward-element
)
176 (define-key map
(kbd "C-m") 'wesnoth-newline
)
177 (define-key map
(kbd "C-j") 'wesnoth-newline-and-indent
)
178 (define-key map
(kbd "C-c c") 'wesnoth-check-wml
)
179 (define-key map
(kbd "C-c C-c") 'wesnoth-check-wml
)
180 (define-key map
(kbd "C-c a") 'wesnoth-complete-attribute
)
181 (define-key map
(kbd "C-c C-a") 'wesnoth-complete-attribute
)
182 (define-key map
(kbd "C-c t") 'wesnoth-complete-tag
)
183 (define-key map
(kbd "C-c C-t") 'wesnoth-complete-tag
)
184 (define-key map
(kbd "M-TAB") 'wesnoth-insert-tag
)
185 (define-key map
(kbd "C-c m") 'wesnoth-complete-macro
)
186 (define-key map
(kbd "C-c C-m") 'wesnoth-complete-macro
)
187 (define-key map
(kbd "C-c o") 'wesnoth-jump-to-matching
)
188 (define-key map
(kbd "C-c C-o") 'wesnoth-jump-to-matching
)
189 (define-key map
(kbd "C-c /") 'wesnoth-insert-missing-closing
)
190 (define-key map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
191 (define-key map
(kbd "TAB") 'wesnoth-indent-or-complete
)
192 (define-key map
[menu-bar wesnoth
]
193 (cons "WML" (make-sparse-keymap "WML")))
194 (define-key map
[menu-bar wesnoth check-structure
]
195 '("Check WML" . wesnoth-check-wml
))
196 (define-key map
[menu-bar wesnoth insert-tag
]
197 '("Insert Tag" . wesnoth-insert-tag
))
198 (define-key map
[menu-bar wesnoth complete-attribute
]
199 '("Insert Attribute" . wesnoth-complete-attribute
))
200 (define-key map
[menu-bar wesnoth complete-macro
]
201 '("Insert Macro" . wesnoth-complete-macro
))
202 (define-key map
[menu-bar wesnoth jump-to-matching
]
203 '("Jump to Matching" . wesnoth-jump-to-matching
))
204 (define-key map
[menu-bar wesnoth insert-missing-closing
]
205 '("Insert Missing Tag" . wesnoth-insert-missing-closing
))
207 "Keymap used in wesnoth mode.")
209 (defvar wesnoth-syntax-table
210 (let ((wesnoth-syntax-table (make-syntax-table)))
211 (modify-syntax-entry ?
= "." wesnoth-syntax-table
)
212 (modify-syntax-entry ?_
"_" wesnoth-syntax-table
)
213 (modify-syntax-entry ?-
"_" wesnoth-syntax-table
)
214 (modify-syntax-entry ?.
"_" wesnoth-syntax-table
)
215 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
216 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
217 wesnoth-syntax-table
)
218 "Syntax table for `wesnoth-mode'.")
220 ;; Prevents automatic syntax-highlighting of elements which might be
221 ;; pre-processor statements.
222 (defvar wesnoth-syntactic-keywords
224 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\|#enddef\\)" 1 "w")
225 '("\\(#[\t ]*.*$\\)" 1 "<"))
226 "Highlighting syntactic keywords within `wesnoth-mode'.")
228 (defun wesnoth-preprocessor-best-face ()
229 "Use `font-lock-preprocessor-face' when available."
230 (when global-font-lock-mode
231 (if (boundp 'font-lock-preprocessor-face
)
232 (copy-face 'font-lock-preprocessor-face
'wesnoth-preprocessor-face
)
233 (copy-face 'font-lock-keyword-face
'wesnoth-preprocessor-face
))))
235 (defvar wesnoth-font-lock-keywords
237 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" .
'wesnoth-preprocessor-face
)
238 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
239 2 font-lock-function-name-face
)
240 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
241 'wesnoth-preprocessor-face
)
242 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)"
243 (1 font-lock-function-name-face
))
244 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)"
245 (1 font-lock-function-name-face
))
246 '("}" . font-lock-function-name-face
)
247 '("^[\t ]*\\(\\[[^]]+\\]\\)" 1 font-lock-type-face
)
248 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face
)
249 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
250 1 font-lock-variable-name-face
))
251 "Syntax highlighting for `wesnoth-mode'.")
253 (defconst wesnoth-element-closing
"^[\t ]*\\(\\[/\\|#enddef\\)"
254 "String to use for a closing element.")
256 (defconst wesnoth-element-opening
"^[\t ]*\\(\\[[^/]\\|#define\\)"
257 "String to use for an opening element.")
259 (defconst wesnoth-element
"^[\t ]*\\(\\[[^]]?\\|#define\\|#enddef\\)"
260 "String to use for an opening or closing element.")
262 ;;; Insertion and completion
263 (defun wesnoth-parent-tag ()
264 "Return the name of the parent tag, nil otherwise."
266 (let ((parent (when (and (wesnoth-wml-start-pos)
267 (> (point) (wesnoth-wml-start-pos)))
268 (wesnoth-check-structure (wesnoth-wml-start-pos)
271 (if (string-match wesnoth-preprocessor-closing-regexp parent
)
273 (subseq parent
2 (1- (length parent
))))))))
275 (defun wesnoth-indent-or-complete ()
276 "Indent or complete the line at point, depending on context."
280 (back-to-indentation)
281 (cond ((looking-at "\\(\\(\\w\\|_\\)+\\)[\t ]*$")
282 (wesnoth-complete-attribute))
283 ((looking-at "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$")
284 (wesnoth-complete-tag))
285 ((looking-at "{\\(\\(\\w\\|_\\)*\\)[\t ]*$")
286 (wesnoth-complete-macro))
287 ((looking-at "\\[/\\(\\(\\w\\|_\\)*\\)[\t ]*$")
288 (delete-region (point) (progn (end-of-line) (point)))
289 (wesnoth-insert-missing-closing)
293 (setq target
(point)))
296 (defun wesnoth-complete-macro ()
297 "Complete and insert the macro at point."
299 (wesnoth-update-project-information)
300 (let* ((macro-information (append wesnoth-macro-data
301 wesnoth-local-macro-data
))
302 (completions (wesnoth-emacs-completion-formats
303 (mapcar 'car macro-information
)))
304 (partial (save-excursion
305 (back-to-indentation)
306 (looking-at "{\\(\\(\\w\\|_\\)*\\)")
308 (macro (or (wesnoth-element-completion completions
"Macro: ")
310 (args (second (find macro macro-information
311 :key
'car
:test
'string
=))))
313 (delete-region (point) (progn (end-of-line) (point)))
314 (insert (concat "{" macro
(if args
" }" "}")))
316 (forward-char -
1)))))
318 (defun wesnoth-complete-attribute ()
319 "Complete and insert the attribute at point."
321 (let* ((completions (wesnoth-build-completion 2))
322 (partial (save-excursion
323 (back-to-indentation)
324 (looking-at "\\(\\(\\w\\|_\\)+\\)")
326 (attribute (or (wesnoth-element-completion completions
"Attribute: ")
329 (delete-region (point) (progn (end-of-line) (point)))
330 (insert (concat attribute
"=")))))
332 (defun wesnoth-complete-tag ()
333 "Complete and insert the tag at point."
335 (let* ((completions (wesnoth-build-completion 1))
336 (partial (save-excursion
337 (back-to-indentation)
338 (looking-at "\\[\\(\\(\\w\\|_\\)+\\)")
340 (tag (or (wesnoth-element-completion completions
"Tag: ")
342 (let ((closed-p nil
))
344 (wesnoth-jump-to-matching)
345 (back-to-indentation)
346 (when (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
347 (string= tag
(match-string 1)))
349 (delete-region (point) (progn (end-of-line) (point)))
352 (wesnoth-insert-and-indent "[" tag
"]")
354 (wesnoth-insert-tag nil tag
)))))
356 (defmacro wesnoth-element-completion
(completions prompt
)
357 "Process completion of COMPLETIONS, displaying PROMPT."
358 (let ((partial (gensym))
360 `(let* ((,partial
(match-string-no-properties 1))
361 (,element
(when ,partial
(try-completion ,partial
,completions
))))
362 (cond ((eq ,element t
)
366 (completing-read ,prompt
,completions
)))
367 ((not (member ,element
,completions
))
369 (completing-read ,prompt
,completions
373 (defun wesnoth-build-completion (position)
374 "Create a new list for tag completion if necessary.
375 Rebuilding list is required for versions of GNU Emacs earlier
376 than 22. POSITION is the argument passed to `nth' for
379 (let* ((parent (wesnoth-parent-tag))
381 (if (or (stringp parent
) (null parent
))
382 (nth position
(find (wesnoth-parent-tag) wesnoth-tag-data
383 :key
'car
:test
'string
=))
384 (mapcar 'car wesnoth-tag-data
))))
385 (wesnoth-emacs-completion-formats candidates
)))
387 (defun wesnoth-emacs-completion-formats (candidates)
388 "Return the completions in the correct format for `emacs-major-version'.
389 CANDIDATES is a list of all possible completions."
390 (if (> emacs-major-version
21)
394 (dolist (tag candidates
)
395 (setq iter
(1+ iter
))
396 (setq tags
(append tags
(list (cons tag iter
)))))
399 (defun wesnoth-insert-tag (&optional elements tagname
)
400 "Insert the specified opening tag and it's matching closing tag.
401 Both the opening and closing tags will be placed on their own
402 lines with point positioned between them. Completion of tags at
403 the prompt uses `wesnoth-tags-list'.
405 ELEMENTS is specifies the number of following blocks which the
406 tag should wrap around.
408 TAGNAME is the name of the tag to be inserted."
411 (setq tagname
(completing-read "Tag: " (wesnoth-build-completion 1))))
412 (or elements
(setq elements
0))
414 (start (save-excursion (forward-line -
1) (point)))
415 (end (unless (= elements
0)
416 (wesnoth-nth-pair-position elements
))))
417 (wesnoth-insert-element-separately "[" tagname
"]")
420 (goto-char (marker-position end
))
422 (wesnoth-insert-element-separately "[/" tagname
"]")
423 (indent-region start
(point) nil
))
428 (defun wesnoth-nth-pair-position (count)
429 "Return `point' after COUNT number of matching element pairs.
430 COUNT is a positive number representing the number of balanced
431 pairs to move across.
432 `point' is returned as a marker object."
435 ;; Currently looking-at target tag. Stop here to avoid
436 ;; incorrect nesting.
437 (unless (wesnoth-search-for-matching-tag
438 'search-forward-regexp wesnoth-element-closing
'point-max
)
440 (search-backward-regexp wesnoth-element-closing
(point-min) t
))
441 (and (> (decf count
) 0) (forward-line 1)))
445 (defun wesnoth-insert-element-separately (&rest strings
)
446 "Concatenate STRINGS and insert them on a line of their own."
447 (let ((create-newline (save-excursion
449 (if (looking-at "^[\t ]*$") nil t
))))
451 (if (> (point) (save-excursion (back-to-indentation) (point)))
457 (insert (apply 'concat strings
))))
459 (defun wesnoth-insert-missing-closing (&optional start end
)
460 "Insert the next expected closing element at point.
462 START and END define the region to check for missing closing
463 elements. If function `transient-mark-mode' is enabled, the region
464 specified will be used as START and END. Otherwise, START and
465 END will be the minimum and maximum positions of the buffer,
468 (if (and transient-mark-mode mark-active
)
469 (setq start
(region-beginning)
470 end
(copy-marker (region-end)))
471 (setq start
(point-min)
473 (let ((element (wesnoth-check-structure start end
)))
475 (error "%s" "Unable to find element to insert")
476 (when (string= element
"Unexpected end of file")
477 (error "%s" element
))
478 (wesnoth-insert-element-separately element
)))
482 (defun wesnoth-insert-and-indent (&rest args
)
483 "Concatenate and insert the given string(s) before indenting.
485 ARGS is a list of strings to be inserted."
486 (insert (apply 'concat args
))
489 (defun wesnoth-newline (&optional indent
)
490 "Indent both the current line and the newline created.
491 If `wesnoth-auto-indent-flag' is nil, indentation will not be
492 performed. Indentation can be forced by setting INDENT to
496 (when (and (or wesnoth-auto-indent-flag indent
)
497 (not (looking-at "^[\t ]*$")))
502 (defmacro wesnoth-navigate-element
(repeat search-function bound
)
503 "Move point to the tag in the given direction REPEAT times.
505 SEARCH-FUNCTION is the symbol of the function for searching in
506 the required direction, with BOUND marking the furthest point to
509 (or ,repeat
(setq ,repeat
1))
511 (and (eq ,search-function
'search-forward-regexp
) (end-of-line))
512 (funcall ,search-function wesnoth-element-opening
,bound t
)
513 (back-to-indentation)
516 (defun wesnoth-forward-element (repeat)
517 "Move point to the end of the next tag.
518 REPEAT is an optional numeric argument. If REPEAT is non-nil,
519 jump forward the specified number of tags."
522 (wesnoth-backward-element (abs repeat
))
523 (wesnoth-navigate-element repeat
'search-forward-regexp
(point-max))))
525 (defun wesnoth-backward-element (repeat)
526 "Move point to the beginning of the previous tag.
527 REPEAT is an optional numeric argument. If REPEAT is non-nil,
528 jump backward the specified number of tags."
531 (wesnoth-forward-element (abs repeat
))
532 (wesnoth-navigate-element repeat
'search-backward-regexp
(point-min))))
534 (defmacro wesnoth-search-for-matching-tag
(search-function search-string bound
)
535 "Search for the matching tag for the current line.
537 SEARCH-FUNCTION is the name of the function used to perform the search.
538 SEARCH-STRING is a string representing the matching tag type.
539 BOUND is the bound to be passed to the search function."
541 (unless (looking-at ,search-string
)
542 (unless (> (point) (funcall ,bound
)) (end-of-line))
543 (while (and (> depth
0)
544 (funcall ,search-function wesnoth-element
546 (if (string-match ,search-string
(match-string 0))
551 (defun wesnoth-jump-to-matching ()
552 "Jump point to the matching opening/closing tag."
555 (if (looking-at wesnoth-element-opening
)
556 (wesnoth-search-for-matching-tag
557 'search-forward-regexp wesnoth-element-closing
'point-max
)
558 (wesnoth-search-for-matching-tag
559 'search-backward-regexp wesnoth-element-opening
'wesnoth-wml-start-pos
))
560 (back-to-indentation))
562 (defun wesnoth-wml-start-pos ()
563 "Determine the position of `point' relative to where the actual WML begins.
564 Return the likely starting position of the WML if it is found.
565 Otherwise return nil."
567 (goto-char (point-min))
568 (when (search-forward-regexp wesnoth-element
(point-max) t
)
572 (defun first-column-indent-p (point)
573 "Return non-nil if the current line should not be indented.
575 POINT is the position in the buffer to check.
576 CONTEXT represents the type of element which precedes the current element."
577 (or (not (wesnoth-wml-start-pos))
578 (<= (point) (wesnoth-wml-start-pos))
579 (nth 3 (parse-partial-sexp (point-min) point
))
580 (looking-at wesnoth-preprocessor-regexp
)))
582 (defun wesnoth-indent ()
583 "Indent the current line as WML."
585 (let ((cur-indent 0))
586 (unless (first-column-indent-p (point))
587 (multiple-value-bind (context ref-indent
)
588 (wesnoth-determine-context (point))
590 ((eq context
'opening
)
591 (if (or (and wesnoth-indent-savefile
592 (not (looking-at wesnoth-element-closing
)))
593 (looking-at wesnoth-element-opening
))
594 (setq cur-indent
(+ ref-indent wesnoth-base-indent
))
595 (setq cur-indent ref-indent
)))
596 ((eq context
'closing
)
597 (if (or (looking-at "^[\t ]*\\[/")
598 (and (not wesnoth-indent-savefile
)
599 (not (looking-at wesnoth-element-opening
))))
600 (setq cur-indent
(- ref-indent wesnoth-base-indent
))
601 (setq cur-indent ref-indent
))))))
602 (indent-line-to (max cur-indent
0))))
604 (defun wesnoth-within-define (position)
605 "Determine whether point is currently inside a #define block.
606 POSITION is the initial cursor position."
608 (dolist (element (or wesnoth-define-blocks
609 (wesnoth-find-macro-definitions)))
610 (when (= (cadr (sort (append (mapcar 'marker-position
(cadr element
))
611 (list position
)) '>)) position
)
612 (setq depth
(max (car element
) depth
))))
615 (defun wesnoth-find-macro-definitions ()
616 "Return information regarding positioning of macro definitions."
618 (goto-char (point-min))
621 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t
)
622 (and (string= (match-string 1) "#define") (beginning-of-line))
624 (if (string= (match-string 1) "#define")
626 (add-to-list 'openings
(point-marker))
629 (list depth
(list (car openings
) (point-marker))))
630 (setq openings
(cdr openings
))
635 (defun wesnoth-indent-region (start end
)
636 "Indent the region from START to END.
638 Creates and destroys a cache of macro definition details as necessary."
643 (setq end
(point-marker))
645 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
646 (or (bolp) (forward-line 1))
647 (while (< (point) end
)
648 (if (looking-at "^[\t ]*$")
650 (funcall indent-line-function
))
652 (setq wesnoth-define-blocks nil
)))
654 (defun wesnoth-determine-context (position)
655 "Determine the type of the last relevant element.
657 POSITION is the buffer position of the element for which to
658 determine the context."
660 (search-backward-regexp wesnoth-element
(wesnoth-wml-start-pos) t
)
661 (let ((match (or (match-string 1) ""))
662 (depth (wesnoth-within-define position
)))
663 (while (and (> (wesnoth-within-define (point)) depth
)
664 (not (= (point) (wesnoth-wml-start-pos))))
665 (search-backward-regexp wesnoth-element
666 (wesnoth-wml-start-pos) t
)
667 (setq match
(match-string 1)))
668 (when (and (= (point) (wesnoth-wml-start-pos)) (= depth
0)
669 (string-match "#define" match
))
670 ;; Found nothing of use; reset match and assume top-level tag.
673 ((string-match "\\[/\\|#enddef" match
)
674 (values 'closing
(current-indentation)))
675 ((string-match "\\[[^/]?\\|#define" match
)
676 (values 'opening
(current-indentation)))))))
678 (defun wesnoth-newline-and-indent (&optional indent
)
679 "Indent both the current line and the newline created.
680 If `wesnoth-auto-indent-flag' is nil, indentation will not be
683 If the optional argument, INDENT is non-nil, force indentation to
687 (when (or wesnoth-auto-indent-flag indent
)
691 (defun wesnoth-check-element-type (position last-tag
)
692 "Determine the context of the element.
693 POSITION is the position of the element in the list.
694 LAST-TAG is the parent element."
695 (if (or (string= last-tag
"#define")
696 (string= last-tag
"#ifndef")
697 (string= last-tag
"#ifdef"))
698 (member (match-string-no-properties 1)
699 (mapcar 'car wesnoth-tag-data
))
704 (member (match-string-no-properties 1)
706 wesnoth-tag-data
:key position
)))))
708 (defun wesnoth-check-output (buffer format-string
&rest args
)
709 "Output the string as passed to `format'.
710 BUFFER is the buffer to output the result.
711 FORMAT-STRING is the string as the first argument of `format'.
712 ARGS is any additional data required by `format' to handle FORMAT-STRING."
714 (let ((lnap (line-number-at-pos)))
716 (insert (apply 'format
(concat "%d: " format-string
"\n")
719 (defun wesnoth-check-wml ()
720 "Perform context-sensitive analysis of WML-code."
722 (wesnoth-update-project-information)
723 (let ((unmatched-tag-list '())
724 (outbuf (get-buffer-create "*WML*")))
726 (let ((buffer (buffer-name)))
729 (insert (format "Checking %s...\n" buffer
))))
731 (goto-char (or (wesnoth-wml-start-pos) (point-min)))
732 (while (search-forward-regexp
733 (concat "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|"
734 "\\(\\w\\|_\\)+=\\|{\\(\\(\\w\\|_\\)+\\).*}\\|"
735 wesnoth-preprocessor-regexp
"\\)")
738 (cond ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
739 (unless (wesnoth-check-element-type 'second
740 (car unmatched-tag-list
))
741 (wesnoth-check-output outbuf
742 "Tag not available in this context: %s"
743 (match-string-no-properties 1)))
744 (setq unmatched-tag-list
(cons
745 (match-string-no-properties 1)
746 unmatched-tag-list
)))
747 ((looking-at "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\) ")
748 (setq unmatched-tag-list
(cons (match-string-no-properties 1)
749 unmatched-tag-list
)))
750 ((looking-at wesnoth-preprocessor-closing-regexp
)
751 (unless (string= (car unmatched-tag-list
)
752 (second (find (match-string-no-properties 1)
753 '(("enddef" "#define")
756 :key
'car
:test
'string
=)))
757 (wesnoth-check-output outbuf
758 "Preprocessor statement does not nest correctly"))
759 (setq unmatched-tag-list
(cdr unmatched-tag-list
)))
760 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=")
761 (unless (wesnoth-check-element-type 'third
762 (car unmatched-tag-list
))
763 (wesnoth-check-output
764 outbuf
"Attribute not available in this context: %s"
765 (match-string-no-properties 1))))
766 ((looking-at "^[\t ]*#else")
767 (unless (string-match "ifn?def" (car unmatched-tag-list
))
768 (if (string= (car unmatched-tag-list
) "#define")
769 (wesnoth-check-output outbuf
"Expecting: %s"
770 (car unmatched-tag-list
))
771 (wesnoth-check-output outbuf
"Expecting: [/%s]"
772 (car unmatched-tag-list
)))))
773 ((looking-at "^[\t ]*{\\(\\(\\w\\|_\\)+\\).*}")
774 (unless (find (match-string-no-properties 1)
775 (append wesnoth-local-macro-data
777 :test
'string
= :key
'car
)
778 (wesnoth-check-output outbuf
"Unknown macro definition: {%s}"
779 (match-string-no-properties 1))))
780 ((or (looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]"))
781 (unless (string= (match-string-no-properties 1)
782 (car unmatched-tag-list
))
783 (if (string= "#" (subseq (car unmatched-tag-list
) 0 1))
784 (wesnoth-check-output outbuf
"Expecting: #%s"
786 (find (car unmatched-tag-list
)
787 '(("enddef" "#define")
790 :key
'second
:test
'string
=)))
791 (wesnoth-check-output outbuf
"Expecting: [/%s]"
792 (car unmatched-tag-list
))))
793 (setq unmatched-tag-list
(cdr unmatched-tag-list
))))
795 (if unmatched-tag-list
796 (dolist (tag unmatched-tag-list
)
797 (wesnoth-check-output outbuf
"Unmatched tag: %s"
798 (car unmatched-tag-list
)))))
800 (display-buffer outbuf t
)
802 (insert (format "\nChecking complete.\n")))))
804 (defmacro wesnoth-element-requires
(element requirement
&optional pop
)
805 "Process requirements for corresponding preprocessor elements.
806 ELEMENT is the current element being tested.
807 REQUIREMENT is the element required to exist for correct nesting.
808 POP is an optional argument indicating the element should be
809 removed from the unmatched-tag-list."
810 `(when (string= ,element
(match-string-no-properties 1))
812 (if (string-match ,requirement
(car unmatched-tag-list
))
814 (and ,pop
(setq unmatched-tag-list
(cdr unmatched-tag-list
)))
816 (setq error-position
(point)))))
818 (defmacro wesnoth-structure-result
(position element
)
819 "Process results of the structure check.
820 POSITION is the error position or nil, if no error was found.
821 ELEMENT is the last unmatched element, or nil if all opening
822 elements have been matched."
823 `(let ((expected nil
))
825 (cond ((string= ,element
"define ") (setq expected
"#enddef"))
826 ((string-match "ifn?def " ,element
) (setq expected
"#endif"))))
828 (if (or ,element
,position
)
830 (and ,position
(goto-char ,position
))
831 (message "Error: Expecting %s" (or expected
832 (concat "[/" ,element
"]"))))
833 (message "%s" "Structure appears consistent."))
834 (when (or expected
,element
)
835 (or expected
(concat "[/" ,element
"]"))))))
837 (defun wesnoth-check-structure (&optional start end
)
838 "Check the buffer for correct nesting of elements.
839 If a problem is found in the structure, point will be placed at
840 the location which an element was expected and the expected
841 element will be displayed in the mini-buffer.
843 START and END define the region to be checked. If
844 function `transient-mark-mode' is enabled, the region specified will be
845 checked. Otherwise START and END will be the minimum and maximum
846 positions of the buffer, respectively."
848 (unless (or start end
)
849 (if (and transient-mark-mode mark-active
)
850 (setq start
(region-beginning)
851 end
(copy-marker (region-end)))
852 (setq start
(point-min)
854 (let ((unmatched-tag-list '())
855 (error-position nil
))
857 (and start
(goto-char start
))
858 (while (and (search-forward-regexp
859 (concat "^\\([\t ]*\\[\\(/?\\(\\w\\|_\\)+\\)\\]\\|"
860 wesnoth-preprocessor-regexp
"\\)") end t
)
861 (not error-position
))
863 (if (or (looking-at "^[\t ]*\\[\\(\\(\\w\\|_\\)+\\)\\]")
864 (looking-at "[\t ]*#\\(define \\|ifdef \\|ifndef \\)"))
865 (setq unmatched-tag-list
(cons (match-string-no-properties 1)
868 ((wesnoth-element-requires "#else" "ifn?def "))
869 ((wesnoth-element-requires "#endif" "ifn?def " t
))
870 ((wesnoth-element-requires "#enddef" "define " t
))
871 ((looking-at (concat "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|"
872 wesnoth-preprocessor-closing-regexp
))
873 (if (string= (match-string-no-properties 1)
874 (car unmatched-tag-list
))
875 (setq unmatched-tag-list
(cdr unmatched-tag-list
))
876 (setq error-position
(point))))))
878 (wesnoth-structure-result error-position
(car unmatched-tag-list
))))
881 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
882 "Major mode for editing WML."
883 (wesnoth-preprocessor-best-face)
884 (set-syntax-table wesnoth-syntax-table
)
885 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
886 (set (make-local-variable 'comment-start
) "#")
887 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
888 (set (make-local-variable 'indent-region-function
) 'wesnoth-indent-region
)
889 (set (make-local-variable 'font-lock-defaults
)
890 '(wesnoth-font-lock-keywords
892 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
893 (setq indent-tabs-mode nil
)
894 (setq mode-name
"WML")
895 (run-hooks 'wesnoth-mode-hook
))
897 (provide 'wesnoth-mode
)
899 ;;; wesnoth-mode.el ends here