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 major-mode
24 ;; features syntax highlighting support, automatic indentation,
25 ;; context-sensitive completion and WML 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 ;; * All warnings found when checking WML are now underlined in the buffer.
38 ;; * Next and previous warning can be jumped to using C-c C-f (or C-x `) and
39 ;; C-c C-b, respectively.
40 ;; * Any macro arguments are now prompted for and inserted when performing
41 ;; completing, as suggested by Fabi.
42 ;; * Improved handling of completion; no longer prompt when no completion
43 ;; found, as suggested by uzytkownik.
44 ;; * Several bugs and inconsistencies corrected.
46 ;; * Major performance improvements to indentation and WML checking.
47 ;; * Fixed a bug where nesting could break when inserting multiple elements
48 ;; near the last element in a buffer.
49 ;; * Fixed a bug where attributes immediately within #ifn?def were always
50 ;; reported to be illegal.
51 ;; * Fixed a bug where tags immediately within #ifn?def were always legal.
52 ;; * Fixed a bug where when inserting missing tags, scanning would only be
53 ;; performed up to point.
54 ;; * Fixed a bug when jumping between preprocessor statements.
56 ;; * Completion history available is now specific to wesnoth-mode.
57 ;; * Added binding to explicitly update macro information from the current
59 ;; * Significantly improved performance of completion and WML checking.
60 ;; * Improved performance for inserting missing tags.
61 ;; * Fixed a bug where #ifdef was never matched when checking WML.
62 ;; * Added completion for preprocessor statements.
63 ;; * Improved macro completion and checking.
65 ;; * Added support for Xemacs.
66 ;; * WML checking is now context sensitive; checks attributes and macros.
67 ;; * WML checks are now always performed on the entire buffer, with results
68 ;; displayed in a temporary buffer.
69 ;; * Context-sensitive completion for attributes and tags.
70 ;; * Completion for built-in and project-specific macros.
71 ;; * Changed the following bindings:
72 ;; `wesnoth-insert-tag' - C-c e -> C-c t
73 ;; `wesnoth-jump-to-matching' - C-c m -> C-c o
74 ;; `wesnoth-check-structure' -> `wesnoth-check-wml' - C-c c
75 ;; * Added the following bindings:
76 ;; `wesnoth-complete-attribute' - C-c a
77 ;; `wesnoth-complete-macro' - C-c m
78 ;; `wesnoth-complete-tag' - C-c t
79 ;; * Removed the following bindings:
80 ;; `wesnoth-check-tag-names' - C-c n
81 ;; * Removed `wesnoth-check-tag-names'. Replaced by `wesnoth-check-wml'.
82 ;; * Completion for an incomplete attribute, tag, or macro at point is
85 ;; * Fixed support for GNU Emacs 21.
86 ;; * Added several new tags to `wesnoth-tags-list'.
87 ;; * Added M-TAB binding for `wesnoth-insert-tag'.
88 ;; * `wesnoth-insert-tag' now takes an optional numeric argument indicating
89 ;; how many blocks to wrap across instead of a region.
90 ;; * Support for `wesnoth-indent-preprocessor-bol' removed.
91 ;; * Fixed a bug in `wesnoth-insert-tag' and `wesnoth-insert-missing-closing'
92 ;; causing tags not to be inserted in the correct position.
93 ;; * Fixed highlighting of array indexes as tags.
95 ;; * Improved syntax-highlighting for macro calls.
96 ;; * Underscore is now treated as whitespace.
97 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
98 ;; * Point is now placed at the first non-whitespace character of the line,
99 ;; instead of the last.
100 ;; * Corrected minor indentation bugs.
101 ;; * Indenting across large regions is now much more efficient.
102 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
105 ;; * Now compatible with GNU Emacs 21.4.
106 ;; * Added support for several new tags.
107 ;; * Added menu entry for wesnoth-mode.
108 ;; * Significant speed increase to indentation.
109 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
110 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
112 ;; * Trailing whitespace is no longer created when creating a second
113 ;; consecutive newline.
114 ;; * Spurious newlines are no longer created when inserting a tag elements
117 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
118 ;; `wesnoth-element', `wesnoth-element-opening',
119 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
120 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
121 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
122 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
123 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
124 ;; * Fixed a bug in indentation where content was needed between elements pairs
125 ;; for indentation to work.
126 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
127 ;; `wesnoth-auto-indent-flag'.
128 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
129 ;; * Added indentation styles: `wesnoth-indent-default',
130 ;; `wesnoth-indent-withtags' which implement a a similar indentation
131 ;; style to the existing styles, however all preprocessor statements are
132 ;; indented to the first column.
133 ;; * Added support for several new tags.
134 ;; * Modified `wesnoth-newline' to behave more consistently.
135 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
136 ;; now leaves point at the beginning (when moving backward) or end (when
137 ;; moving forward) of the match.
138 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
139 ;; will now work on preprocessor statements. Will now warn if jump
140 ;; destination may not be correct (due to errors in WML structure).
141 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
142 ;; * `wesnoth-check-structure' can now be applied over an active region and
143 ;; now checks preprocessor statements for correct nesting.
144 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
145 ;; perform indentation by providing a prefix argument.
146 ;; * Indentation styles now leave point at the first non-whitespace character
148 ;; * `wesnoth-check-tag-names' now reports on success.
149 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
150 ;; * `outline-minor-mode' now works on macro definitions.
152 ;; * Base indent now defaults to 4.
153 ;; * Added support for #ifndef.
157 (require 'wesnoth-update
)
158 (require 'wesnoth-wml-data
)
160 (defconst wesnoth-mode-version
"1.3.2"
161 "The current version of `wesnoth-mode'.")
163 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
167 (defcustom wesnoth-auto-indent-flag t
168 "Non-nil means indent the current line upon creating a newline."
170 :group
'wesnoth-mode
)
172 (defcustom wesnoth-indent-savefile t
173 "Non-nil means to use the current indentation conventions.
174 If nil, use the old convention for indentation.
175 The current convention is all attributes are indented a level deeper
176 than their parent; in the past attributes were indented to the same
177 level as their parent.")
179 (defcustom wesnoth-base-indent
4
180 "The number of columns to indent WML."
182 :group
'wesnoth-mode
)
184 (defconst wesnoth-preprocessor-regexp
185 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
186 "Regular expression to match all preprocessor statements.")
188 (defconst wesnoth-preprocessor-opening-regexp
189 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
190 "Regular expression to match \"opening\" preprocessor statements.")
192 (defconst wesnoth-preprocessor-closing-regexp
193 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
194 "Regular expression to match \"closing\" preprocessor statements.")
196 (defvar wesnoth-define-blocks
'()
197 "Cache of all toplevel #define and #enddef pairs.")
199 (defvar wesnoth-history-list
'()
200 "History of inserted WML elements.")
202 (defvar wesnoth-mode-hook nil
)
204 (defface wesnoth-warning-face
205 '((t (:underline
"tomato1")))
206 "Face to use for warnings in wesnoth-mode"
207 :group
'wesnoth-mode
)
209 (defvar wesnoth-mode-map
210 (let ((map (make-sparse-keymap)))
211 (define-key map
(kbd "C-M-a") 'wesnoth-backward-element
)
212 (define-key map
(kbd "C-M-e") 'wesnoth-forward-element
)
213 (define-key map
(kbd "C-m") 'wesnoth-newline
)
214 (define-key map
(kbd "C-j") 'wesnoth-newline-and-indent
)
215 (define-key map
(kbd "C-c C-c") 'wesnoth-check-wml
)
216 (define-key map
(kbd "C-c C-a") 'wesnoth-complete-attribute
)
217 (define-key map
(kbd "C-c C-t") 'wesnoth-complete-tag
)
218 (define-key map
(kbd "C-c C-p") 'wesnoth-complete-preprocessor
)
219 (define-key map
(kbd "C-c C-u") 'wesnoth-update-project-information
)
220 (define-key map
(kbd "M-TAB") 'wesnoth-complete-tag
)
221 (define-key map
(kbd "C-c C-m") 'wesnoth-complete-macro
)
222 (define-key map
(kbd "C-c C-o") 'wesnoth-jump-to-matching
)
223 (define-key map
(kbd "C-c C-f") 'wesnoth-forward-warning
)
224 (define-key map
(kbd "C-x `") 'wesnoth-forward-warning
)
225 (define-key map
(kbd "C-c C-b") 'wesnoth-backward-warning
)
226 (define-key map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
227 (define-key map
(kbd "TAB") 'wesnoth-indent-or-complete
)
229 "Keymap used in `wesnoth-mode'.")
231 (easy-menu-define wesnoth-menu wesnoth-mode-map
"Menu for wesnoth-mode"
233 ["Check WML" wesnoth-check-wml t
]
234 ["Indent or Complete" wesnoth-indent-or-complete t
]
235 ["Indent buffer" (lambda ()
237 (wesnoth-indent-region (point-min) (point-max))) t
]
238 ["Insert Tag" wesnoth-complete-tag t
]
239 ["Insert Attribute" wesnoth-complete-attribute t
]
240 ["Insert Macro" wesnoth-complete-macro t
]
241 ["Insert Preprocessor" wesnoth-complete-preprocessor t
]
242 ["Insert Missing Tag" wesnoth-insert-missing-closing t
]
243 ["Jump to Matching" wesnoth-jump-to-matching t
]
244 ["Update Macros" wesnoth-update-project-information t
]))
246 (defvar wesnoth-syntax-table
247 (let ((wesnoth-syntax-table (make-syntax-table)))
248 (modify-syntax-entry ?
= "." wesnoth-syntax-table
)
249 (modify-syntax-entry ?_
"_" wesnoth-syntax-table
)
250 (modify-syntax-entry ?-
"_" wesnoth-syntax-table
)
251 (modify-syntax-entry ?.
"_" wesnoth-syntax-table
)
252 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
253 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
254 wesnoth-syntax-table
)
255 "Syntax table for `wesnoth-mode'.")
257 ;; Prevents automatic syntax-highlighting of elements which might be
258 ;; pre-processor statements.
259 (defvar wesnoth-syntactic-keywords
261 '("\\([\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\)" 1 "w")
262 '("\\(#[\t ]*.*$\\)" 1 "<"))
263 "Highlighting syntactic keywords within `wesnoth-mode'.")
265 (defvar wesnoth-font-lock-keywords
267 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face
)
268 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
269 font-lock-keyword-face
)
270 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
271 2 font-lock-function-name-face
)
272 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face
))
273 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face
))
274 '("}" . font-lock-function-name-face
)
275 '("[\t ]*\\(\\[/?[^$]\\(\\w\\|_\\)+\\]\\)" 1 font-lock-type-face
)
276 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face
)
277 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
278 1 font-lock-variable-name-face
))
279 "Syntax highlighting for `wesnoth-mode'.")
281 (defun wesnoth-element-closing (&optional limited
)
282 "Return the regexp to match a closing element.
283 If LIMITED is non-nil, return a regexp which matches only the
284 #enddef preprocessor."
285 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\|"
287 "#enddef\\|{NEXT .+}"
288 "#end\\(?:def\\|if\\)")
291 (defun wesnoth-element-opening (&optional limited
)
292 "Return the regexp to match a closing element.
293 If LIMITED is non-nil, return a regexp which matches only the
294 #define preprocessor."
295 (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define "
301 (defun wesnoth-find-next (type)
302 "Find the next element of TYPE.
303 TYPE is a symbol representing an element type, or a list of
304 element types to find."
305 (let ((element (wesnoth-next-element)))
306 (while (and element
(if (listp type
)
307 (not (member (car element
) type
))
308 (not (eql (car element
) type
))))
309 (setq element
(wesnoth-next-element)))
310 (when (if (listp type
)
311 (member (car element
) type
)
312 (eql (car element
) type
))
315 (defun wesnoth-element (&optional limited
)
316 "Return the regexp to match a closing element.
317 If LIMITED is non-nil, return a regexp which matches only the
318 #define and #enddef preprocessors."
319 (concat "^[\t ]*\\(\\[[/+]?\\(\\w\\|_\\)+\\]?\\|"
321 "#define \\|#enddef\\|{FOREACH .+}\\|{NEXT .+}"
322 (substring wesnoth-preprocessor-regexp
5))
325 (defun wesnoth-next-element ()
326 "Move to the next element in the buffer.
327 Return non-nil when an element is found. Otherwise, return nil."
330 (and (or (eolp) (looking-at "[}\t ]"))
331 (search-forward-regexp "[^}
334 (let ((details (wesnoth-element-type (point))))
336 (when (nth 2 details
)
337 (goto-char (nth 2 details
))
338 (while (nth 3 (parse-partial-sexp
339 (save-excursion (search-backward-regexp
340 (wesnoth-element t
) (point-min) t
)
343 (search-forward "\"" (point-max) t
))))
346 (defun wesnoth-element-type (point)
347 "Return details regarding the element at `point'.
348 A list is returned, the elements of the list represent the
349 following, respectively: A symbol representing the type of
350 element; the position of the start of the element and the
351 position of the end of the element. `point' must be at the start
353 (let ((element-matches
354 '(("{\\(.*?[/\]\\)+}" . nil
)
355 ("{\\(\\w\\|_\\)+" . macro
)
356 ("\\[\\+?[^/]+?\\]" . tag-opening
)
357 ("\\[/.+?\\]" . tag-closing
)
358 ("\\(\\w\\|_\\)+[\t ]*=" . attribute
)
359 ("#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)" . preprocessor
)
361 ("[^\t\n |]+") . nil
)))
362 (dolist (pair element-matches
)
363 (when (looking-at (car pair
))
364 (return (list (cdr pair
)
368 (defun wesnoth-get-element-type (point)
369 "Return details for the the element near `point'.
370 Locate the start of the element before calling `wesnoth-element-type'."
372 (let ((details (wesnoth-element-type (point))))
373 (if (and (>= point
(nth 1 details
))
374 (<= point
(nth 2 details
)))
376 (goto-char (nth 2 details
))
377 (when (search-forward-regexp "[^\t\n .|()]+"
379 (goto-char (match-beginning 0)))
380 (when (nth 3 (parse-partial-sexp (save-excursion (beginning-of-line)
383 (search-forward "\"" (point-max) t
))
384 (when (looking-at "[\t\n .|()]")
385 (search-forward-regexp "[\t\n .|()]+"))
386 (wesnoth-get-element-type point
)))))
388 (defun wesnoth-element-at-point (&optional point
)
389 "Return details of the element under `point'."
392 (let ((target point
))
393 (back-to-indentation)
394 (wesnoth-get-element-type target
))))
397 ;;; Insertion and completion
398 (defmacro wesnoth-element-completion
(completions prompt partial
400 "Process completion of COMPLETIONS, displaying PROMPT.
401 PARTIAL is the partial string on which to attempt completion."
402 `(let* ((element (when ,partial
(try-completion ,partial
,completions
))))
403 (cond ((eq element t
)
405 ((and completep
(null element
))
407 ((and element
(eq (try-completion element
,completions
) t
))
409 ((> (length (all-completions (or element
"") ,completions
)) 1)
410 (completing-read ,prompt
,completions
412 'wesnoth-history-list
))
416 (defun wesnoth-parent-tag ()
417 "Return the name of the parent tag.
418 If the parent is a preprocessor statement, return non-nil.
419 If the element does not have a parent, return nil.
420 Otherwise, return a string containing the name of the parent tag."
422 (let ((start-point (point))
424 (when (save-excursion (> (point) (progn (back-to-indentation)
427 (while (and (> depth
0)
428 (search-backward-regexp (wesnoth-element t
)
430 (if (string-match "[\t ]*\\[/" (match-string 0))
431 (setq depth
(1+ depth
))
432 (setq depth
(1- depth
))))
436 (when (looking-at (wesnoth-element-opening))
437 (let ((parent (match-string-no-properties 1)))
438 (if (or (string-match wesnoth-preprocessor-opening-regexp parent
)
439 ;; Check if we're immediately within a macro
440 (and (goto-char start-point
)
441 (search-backward-regexp "[}{]" (point-min) t
)
442 (string= (match-string 0) "{")
443 (goto-char start-point
)
444 (not (and (search-backward parent
(point-min) t
)
445 (search-backward-regexp "[}{]" (point-min) t
)
446 (string= (match-string 0) "{")))))
448 (substring parent
1 (1- (length parent
))))))))))
450 (defun wesnoth-partial-macro-p ()
451 "Return non-nil if point is in a partial macro."
455 (search-backward-regexp "{" (point-min) t
)
456 (while (search-forward-regexp "[{}]" (point-max) t
)
457 (if (string= (match-string 0) "{")
458 (setq opened
(1+ opened
))
459 (setq opened
(1- opened
))))
462 (defun wesnoth-indent-or-complete (&optional elements
)
463 "Indent or complete the line at point, depending on context.
464 ELEMENTS is the number of elements to wrap around if inserting
467 (or elements
(setq elements
0))
469 ((looking-back "\\[\\(\\(\\w\\|_\\)*\\)")
470 (wesnoth-complete-tag elements t
))
471 ((and (looking-back "{\\(\\w\\|_\\)*") (wesnoth-partial-macro-p))
472 (wesnoth-complete-macro t
))
473 ((looking-back "^#\\w+$")
474 (wesnoth-complete-preprocessor elements t
))
475 ((looking-back "\\[/\\(\\(\\w\\|_\\)*\\)[\t ]*$")
478 (and (search-backward
480 (save-excursion (back-to-indentation)
485 (wesnoth-insert-missing-closing)
487 ((looking-back "\\(\\(\\w\\|_\\)+\\)[\t ]*$")
488 (wesnoth-complete-attribute t
))
492 (defun wesnoth-preprocessor-closed-p (preprocessor)
493 "Determine whether PREPROCESSOR has been closed.
494 PREPROCESSOR is a string matching the preprocessor statement to
497 (back-to-indentation)
498 (wesnoth-jump-to-matching preprocessor
)
500 (if (string= preprocessor
"#define ")
504 (defun wesnoth-complete-preprocessor (&optional elements completep
)
505 "Complete and insert the preprocessor at point.
506 ELEMENTS is the number of elements to wrap around.
507 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
509 (or elements
(setq elements
0))
510 (let* ((completions (wesnoth-emacs-completion-formats
511 '("define" "else" "ifdef" "ifndef"
512 "enddef" "endif" "undef")))
513 (partial (when completep
515 (back-to-indentation)
516 (when (looking-at "#\\(\\w+\\)$")
517 (match-string-no-properties 1)))))
518 (preprocessor (wesnoth-element-completion
519 completions
"Preprocessor: " partial completep
))
523 (unless (string= "#" (substring preprocessor
0 1))
524 (setq preprocessor
(concat "#" preprocessor
)))
525 (when (string-match "#\\(define\\|ifn?def\\|undef\\)" preprocessor
)
526 (setq preprocessor
(concat preprocessor
" ")))
530 (and (search-backward
532 (save-excursion (back-to-indentation)
537 (wesnoth-preprocessor-closed-p preprocessor
)))))
542 (and (search-backward
543 "#" (save-excursion (back-to-indentation)
548 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor
)
551 (wesnoth-insert-tag elements preprocessor
)
554 (wesnoth-insert-element-separately preprocessor
)))))
556 (defun wesnoth-macro-arguments ()
557 "Find any current macro arguments."
559 (depth (wesnoth-within-define (point))))
563 (search-backward-regexp
564 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
566 (when (<= (wesnoth-within-define (point)) depth
)
569 (append (mapcar (lambda (macro)
572 (match-string-no-properties 1)))
574 (setq depth
(1- depth
)))))
577 (defun wesnoth-complete-macro (&optional completep
)
578 "Complete and insert the macro at point.
579 If COMPLETEP is non-nil, attempt to complete the macro at point."
581 (wesnoth-update-project-information)
582 (let* ((macro-information (append (wesnoth-macro-arguments)
584 wesnoth-local-macro-data
))
585 (completions (wesnoth-emacs-completion-formats
586 (mapcar 'car macro-information
)))
587 (partial (when completep
589 (when (looking-back "{\\(\\(\\w\\|_\\)*\\)")
590 (match-string-no-properties 1)))))
591 (macro (wesnoth-element-completion completions
"Macro: " partial
593 (args (cadr (assoc macro macro-information
))))
597 ;; Delete the region corresponding to the current macro.
600 (and (search-backward
601 "{" (save-excursion (back-to-indentation) (point))
605 (if (search-forward-regexp
607 (save-excursion (end-of-line) (point))
610 (save-excursion (end-of-line) (point)))))
611 (insert "{" macro
(if args
" }" "}")))
612 (wesnoth-insert-element-separately "{" macro
(if args
" }" "}")))
617 (let ((input (read-string (concat (car args
) ": "))))
618 (insert input
(if (string= input
"") "" " "))
619 (while (and (setq args
(cdr args
)) (not (string= input
"")))
620 (insert (setq input
(read-string (concat (car args
) ": ")))
621 (if (and (not (string= input
""))
622 (cdr args
)) " " "")))))
623 (when (null args
) (forward-char 1)))))
625 (defun wesnoth-complete-attribute (&optional completep
)
626 "Insert the attribute at point.
627 If COMPLETEP is non-nil, attempt to complete the attribute at point."
629 (let* ((completions (wesnoth-build-completion 1))
630 (partial (when completep
632 (when (looking-back "^[\t ]*\\(\\(\\w\\|_\\)+\\)")
633 (match-string-no-properties 1)))))
634 (attribute (wesnoth-element-completion completions
"Attribute: "
641 (or (and (search-backward-regexp
642 "[\t ]" (save-excursion
643 (back-to-indentation)
646 (back-to-indentation (point))))
648 (insert attribute
"="))
649 (wesnoth-insert-element-separately attribute
650 (if (string-match "=" attribute
)
656 (defun wesnoth-complete-tag (&optional elements completep
)
657 "Complete and insert the tag at point.
658 ELEMENTS is the number of elements to wrap around.
659 If COMPLETEP is non-nil, attempt to complete tag at point."
661 (or elements
(setq elements
0))
662 (let* ((completions (wesnoth-build-completion 0))
663 (partial (when (and completep
664 (looking-back "\\[\\(\\(\\w\\|_\\)+\\)[\t ]*$"))
665 (match-string-no-properties 1)))
666 (tag (wesnoth-element-completion completions
"Tag: " partial
670 (wesnoth-jump-to-matching (concat "[" tag
"]"))
671 (back-to-indentation)
672 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
673 (string= tag
(match-string 1))))))
678 (delete-region (save-excursion
681 (back-to-indentation)
688 (wesnoth-insert-and-indent "[" tag
"]")
690 (wesnoth-insert-tag elements tag
)))
691 (wesnoth-insert-tag elements tag
)))
692 (or completep
(wesnoth-insert-tag elements
)))))
694 (defun wesnoth-build-completion (position)
695 "Create a new list for tag completion if necessary.
696 Rebuilding list is required for versions of GNU Emacs earlier
697 than 22. POSITION is the argument passed to `nth' for
700 (let ((parent (wesnoth-parent-tag)))
701 (wesnoth-emacs-completion-formats
702 (if (or (stringp parent
) (null parent
))
703 (nth position
(gethash parent wesnoth-tag-hash-table
))
704 (mapcar 'car wesnoth-tag-data
)))))
706 (defun wesnoth-emacs-completion-formats (candidates)
707 "Return the completions in the correct format for `emacs-major-version'.
708 CANDIDATES is a list of all possible completions."
709 (if (> emacs-major-version
21)
713 (dolist (tag candidates
)
714 (setq iter
(1+ iter
))
715 (setq tags
(append tags
(list (cons tag iter
)))))
718 (defun wesnoth-insert-tag (&optional elements tagname
)
719 "Insert the specified opening tag and it's matching closing tag.
720 Both the opening and closing tags will be placed on their own
721 lines with point positioned between them. Completion of tags at
722 the prompt uses `wesnoth-tags-list'.
724 ELEMENTS is specifies the number of following blocks which the
725 tag should wrap around.
727 TAGNAME is the name of the tag to be inserted."
730 (setq tagname
(completing-read "Tag: " (wesnoth-build-completion 1)
731 nil nil nil wesnoth-history-list
)))
732 (when (or (not elements
)
733 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
734 wesnoth-preprocessor-regexp
"\\)")))
737 (start (save-excursion (forward-line -
1) (point)))
738 (end (unless (= elements
0)
739 ;; Work around some strange behaviour when the target is at the
740 ;; end of the buffer.
742 (goto-char (point-max))
744 (unless (looking-at "^[\t ]*$")
747 (wesnoth-nth-pair-position elements
))))
748 (if (string-match wesnoth-preprocessor-regexp tagname
)
749 (wesnoth-insert-element-separately tagname
)
750 (wesnoth-insert-element-separately "[" tagname
"]"))
753 (goto-char (marker-position end
))
754 (newline (if (string-match wesnoth-preprocessor-regexp tagname
) 1 2)))
755 (if (string-match wesnoth-preprocessor-opening-regexp tagname
)
756 (wesnoth-insert-element-separately
757 (if (string= tagname
"#define ")
760 (wesnoth-insert-element-separately "[/" tagname
"]"))
761 (indent-region start
(point) nil
))
766 (defun wesnoth-insert-element-separately (&rest strings
)
767 "Concatenate STRINGS and insert them on a line of their own."
768 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
769 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
776 (if (looking-at "^[\t ]*$")
779 (insert (apply 'concat strings
)))
781 (defun wesnoth-insert-missing-closing (&optional start end
)
782 "Insert the next expected closing element at point.
784 START and END define the region to check for missing closing
785 elements. If function `transient-mark-mode' is enabled, the region
786 specified will be used as START and END. Otherwise, START and
787 END will be the minimum and current positions of the buffer,
790 (if (and (boundp 'transient-mark-mode
)
791 transient-mark-mode mark-active
)
792 (setq start
(region-beginning)
793 end
(copy-marker (region-end)))
794 (setq start
(point-min)
796 (let ((element (wesnoth-check-structure start end
)))
798 (message "%s" "Unable to find element to insert"))
799 ((string= element
"Unexpected end of file")
800 (message "%s" element
))
802 (wesnoth-insert-element-separately element
)
806 (defun wesnoth-insert-and-indent (&rest args
)
807 "Concatenate and insert the given string(s) before indenting.
809 ARGS is a list of strings to be inserted."
810 (insert (apply 'concat args
))
813 (defun wesnoth-newline (&optional indent
)
814 "Indent both the current line and the newline created.
815 If `wesnoth-auto-indent-flag' is nil, indentation will not be
816 performed. Indentation can be forced by setting INDENT to
822 (when (and (or wesnoth-auto-indent-flag indent
)
823 (not (looking-at "^[\t ]*$")))
827 (defmacro wesnoth-navigate-element
(repeat search-function bound
)
828 "Move point to the tag in the given direction REPEAT times.
830 SEARCH-FUNCTION is the symbol of the function for searching in
831 the required direction, with BOUND marking the furthest point to
834 (or ,repeat
(setq ,repeat
1))
836 (and (eq ,search-function
'search-forward-regexp
) (end-of-line))
837 (funcall ,search-function
(wesnoth-element-opening) ,bound t
)
838 (back-to-indentation)
839 (setq repeat
(1- ,repeat
)))))
841 (defun wesnoth-nth-pair-position (count)
842 "Return `point' after COUNT number of matching element pairs.
843 COUNT is a positive number representing the number of balanced
844 pairs to move across.
845 `point' is returned as a marker object."
847 (let ((start (point))
849 (if (> (point) (save-excursion (back-to-indentation) (point)))
853 ;; Currently looking-at target tag. Stop here to avoid
854 ;; incorrect nesting.
855 (unless (wesnoth-search-for-matching-tag
856 'search-forward-regexp
(wesnoth-element-closing) 'point-max
)
859 (and (> (setq count
(1- count
)) 0) (forward-line 1)))
865 (defun wesnoth-forward-element (repeat)
866 "Move point to the end of the next tag.
867 REPEAT is an optional numeric argument. If REPEAT is non-nil,
868 jump forward the specified number of tags."
871 (wesnoth-backward-element (abs repeat
))
872 (wesnoth-navigate-element repeat
'search-forward-regexp
(point-max))))
874 (defun wesnoth-backward-element (repeat)
875 "Move point to the beginning of the previous tag.
876 REPEAT is an optional numeric argument. If REPEAT is non-nil,
877 jump backward the specified number of tags."
880 (wesnoth-forward-element (abs repeat
))
881 (wesnoth-navigate-element repeat
'search-backward-regexp
(point-min))))
883 (defmacro wesnoth-search-for-matching-tag
(search-function
884 search-string bound
&optional skip
)
885 "Search for the matching tag for the current line.
886 SEARCH-FUNCTION is the name of the function used to perform the search.
887 SEARCH-STRING is a string representing the matching tag type.
888 BOUND is the bound to be passed to the search function.
889 If SKIP is non-nil, skip the first element and continue from there."
891 (when (and (or (and ,skip
(forward-line 1))
892 (funcall ,search-function
(wesnoth-element) (funcall ,bound
) t
))
893 (or ,skip
(not (string-match ,search-string
(match-string 0)))))
894 (while (and (> depth
0)
895 (funcall ,search-function
(wesnoth-element)
897 (if (string-match ,search-string
(match-string 0))
898 (setq depth
(1- depth
))
899 (setq depth
(1+ depth
))))
902 (defun wesnoth-jump-to-matching (&optional opening
)
903 "Jump point to the matching opening/closing tag.
904 OPENING is an opening preprocessor statement to attempt to find a match for."
911 ((or (and (stringp opening
)
912 (string-match (wesnoth-element-opening) opening
))
913 (looking-at (wesnoth-element-opening)))
915 (setq first-element
(match-string-no-properties 0 opening
))
916 (when (wesnoth-search-for-matching-tag
917 'search-forward-regexp
(wesnoth-element-closing) 'point-max
920 (if (and (string-match wesnoth-preprocessor-opening-regexp
922 (looking-at (wesnoth-element-closing)))
923 (when (string= (match-string-no-properties 0)
924 (cdr (assoc first-element
925 '(("#define " .
"#enddef")
926 ("#ifndef " .
"#endif")
927 ("#ifdef " .
"#endif")))))
928 (setq target
(point)))
929 (setq target
(point))))))
930 ((looking-at (wesnoth-element-closing))
932 (setq first-element
(match-string-no-properties 0))
933 (when (wesnoth-search-for-matching-tag
934 'search-backward-regexp
(wesnoth-element-opening)
935 'wesnoth-wml-start-pos
)
936 (if (and (string-match wesnoth-preprocessor-closing-regexp
938 (looking-at (wesnoth-element-opening)))
940 (when (or (and (string= "#enddef" first-element
)
942 (match-string-no-properties
944 (and (string= "#endif" first-element
)
947 (match-string-no-properties
949 (setq target
(point))))
950 (setq target
(point)))))
952 (search-backward-regexp (wesnoth-element-opening) (point-min) t
)
953 (setq target
(point)))))
957 (back-to-indentation))
958 (when (interactive-p)
959 (message "%s" "Tag does not appear to be matched")))))
962 (defun wesnoth-wml-start-pos ()
963 "Determine the position of `point' relative to where the actual WML begins.
964 Return the likely starting position of the WML if it is found.
965 Otherwise return nil."
967 (goto-char (point-min))
968 (when (search-forward-regexp (wesnoth-element) (point-max) t
)
972 (defun wesnoth-first-column-indent-p (point)
973 "Return non-nil if the current line should not be indented.
975 POINT is the position in the buffer to check.
976 CONTEXT represents the type of element which precedes the current element."
977 (or (not (wesnoth-wml-start-pos))
978 (<= (point) (wesnoth-wml-start-pos))
979 (nth 3 (parse-partial-sexp
980 (save-excursion (search-backward-regexp
981 (wesnoth-element t
) (point-min) t
)
984 (looking-at wesnoth-preprocessor-regexp
)))
986 (defun wesnoth-indent ()
987 "Indent the current line as WML."
990 (let* ((cur-indent 0)
991 (context-data (wesnoth-determine-context (point)))
992 (context (car context-data
))
993 (ref-indent (cdr context-data
)))
994 (unless (wesnoth-first-column-indent-p (point))
996 ((eq context
'opening
)
997 (if (or (and wesnoth-indent-savefile
998 (not (looking-at (wesnoth-element-closing t
))))
999 (looking-at (wesnoth-element-opening t
)))
1000 (setq cur-indent
(+ ref-indent wesnoth-base-indent
))
1001 (setq cur-indent ref-indent
)))
1002 ((eq context
'closing
)
1003 (if (or (looking-at "^[\t ]*\\(\\[/\\|{NEXT\\)")
1004 (and (not wesnoth-indent-savefile
)
1005 (not (looking-at (wesnoth-element-opening t
)))))
1006 (setq cur-indent
(- ref-indent wesnoth-base-indent
))
1007 (setq cur-indent ref-indent
)))))
1008 (indent-line-to (max cur-indent
0))))
1009 (when (> (save-excursion (back-to-indentation) (point))
1011 (back-to-indentation)))
1013 (defun wesnoth-within-define (position)
1014 "Determine whether point is currently inside a #define block.
1015 POSITION is the initial cursor position."
1018 (defblocks (or wesnoth-define-blocks
1019 (wesnoth-find-macro-definitions))))
1020 (unless (equal (car defblocks
) 'none
)
1021 (dolist (element defblocks
)
1022 (when (= (cadr (sort (append (mapcar 'marker-position
(cadr element
))
1023 (list position
)) '>)) position
)
1024 (setq depth
(max (car element
) depth
)))))
1027 (defun wesnoth-find-macro-definitions ()
1028 "Return information regarding positioning of macro definitions."
1030 (goto-char (point-min))
1033 (while (search-forward-regexp "#define\\|#enddef" (point-max) t
)
1034 (and (string= (match-string 0) "#define") (beginning-of-line))
1036 (if (string= (match-string 0) "#define")
1038 (add-to-list 'openings
(point-marker))
1043 (list depth
(list (car openings
)
1045 (setq openings
(cdr openings
))
1049 (or cache
(list 'none
)))))
1051 (defun wesnoth-indent-region (start end
)
1052 "Indent the region from START to END.
1053 Creates and destroys a cache of macro definition details as necessary."
1058 (setq end
(point-marker))
1060 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
1061 (or (bolp) (forward-line 1))
1062 (while (< (point) end
)
1063 (if (looking-at "^[\t ]*$")
1065 (funcall indent-line-function
))
1067 (setq wesnoth-define-blocks nil
)))
1069 (defun wesnoth-determine-context (position)
1070 "Determine the type of the last relevant element.
1071 POSITION is the buffer position of the element for which to
1072 determine the context."
1075 (and (search-backward-regexp
1076 (wesnoth-element t
) (point-min) t
)
1078 (while (save-match-data
1079 (looking-at "^[\t ]*\\[[^/].+\\]\\[/.+\\]"))
1080 (search-backward-regexp (wesnoth-element t
)
1085 (depth (wesnoth-within-define position
)))
1086 (while (and (wesnoth-wml-start-pos)
1087 (> (wesnoth-within-define (point)) depth
)
1088 (not (= (point) (wesnoth-wml-start-pos))))
1089 (search-backward-regexp (wesnoth-element t
)
1090 (wesnoth-wml-start-pos) t
)
1091 (setq match
(match-string 1)))
1092 (when (and (wesnoth-wml-start-pos)
1093 (= (point) (wesnoth-wml-start-pos))
1095 (string-match "#define" match
))
1096 ;; Found nothing of use; reset match and assume top-level tag.
1099 ((string-match "\\[/\\|#enddef\\|{NEXT " match
)
1100 (cons 'closing
(current-indentation)))
1101 ((string-match "\\[[^/]?\\|#define\\|{FOREACH " match
)
1102 (cons 'opening
(current-indentation)))))))
1104 (defun wesnoth-newline-and-indent (&optional indent
)
1105 "Indent both the current line and the newline created.
1106 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1109 If the optional argument, INDENT is non-nil, force indentation to
1113 (when (or wesnoth-auto-indent-flag indent
)
1117 (defun wesnoth-check-element-type (position)
1118 "Determine the context of the element.
1119 POSITION is the position of the element in the list."
1120 (let ((parent (save-match-data (wesnoth-parent-tag))))
1121 (if (or (stringp parent
) (null parent
))
1122 (member (match-string-no-properties 1)
1123 (nth position
(gethash parent wesnoth-tag-hash-table
)))
1124 (member (match-string-no-properties 1)
1125 (mapcar 'car wesnoth-tag-data
)))))
1127 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
1128 (defun wesnoth-line-number-at-pos (&optional pos
)
1129 "Return (narrowed) buffer line number at position POS.
1130 If POS is nil, use current buffer location.
1131 Counting starts at (point-min), so the value refers
1132 to the contents of the accessible portion of the buffer."
1133 (let ((opoint (or pos
(point))) start
)
1135 (goto-char (point-min))
1136 (setq start
(point))
1139 (1+ (count-lines start
(point))))))
1141 (defun wesnoth-check-output (buffer format-string
&rest args
)
1142 "Output the string as passed to `format'.
1143 BUFFER is the buffer to output the result.
1144 FORMAT-STRING is the string as the first argument of `format'.
1145 ARGS is any additional data required by `format' to handle FORMAT-STRING."
1147 (let ((lnap (wesnoth-line-number-at-pos)))
1149 (let ((buffer-read-only nil
))
1150 (insert (apply 'format
(concat "Line %d: " format-string
"\n")
1153 (defun wesnoth-extract-macro-details (macro-arguments)
1154 "Return a list of all macros in MACRO-ARGUMENTS."
1155 (when macro-arguments
1156 (let ((results '()))
1157 (dolist (macro (split-string macro-arguments
"[{}][\t ]*" t
))
1158 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
1160 (add-to-list 'results
(match-string-no-properties 1 macro
))))
1163 (defmacro wesnoth-check-process
(format-string &rest args
)
1164 "Output to buffer where requested and position overlays as required.
1165 FORMAT-STRING is the string to pass as the first argument to
1166 `format' for the error. ARGS is a list of arguments required by
1169 (wesnoth-check-output outbuf
,format-string
,@args
)
1170 (wesnoth-place-overlay (match-beginning 0) (match-end 0))))
1172 (defmacro wesnoth-overlay-at-pos-p
(position)
1173 "Return non-nil when there is an overlay at POSITION."
1174 `(and (overlays-at (goto-char ,position
))
1175 (overlay-get (car (overlays-at (point))) 'wesnoth-error
)
1176 (overlay-start (car (overlays-at (point))))))
1178 (defmacro wesnoth-locate-warning
(search start end
)
1179 "Search for and move to the warning in the given direction.
1180 Searching starts from `point' and will wrap from START if no
1181 match was found. SEARCH is a form to locate the warning in the
1182 required direction. START is the start of the region searched.
1183 END is the end of the region searched."
1184 `(let ((target nil
))
1187 ((setq target
(wesnoth-overlay-at-pos-p ,search
)))
1188 ((setq target
(wesnoth-overlay-at-pos-p ,start
)))
1189 ((and (not (= (goto-char ,search
) ,end
))
1190 (setq target
(wesnoth-overlay-at-pos-p (point)))))
1192 (message "%s" "No warnings found")
1195 (defun wesnoth-forward-warning ()
1196 "Move to the next warning."
1201 (wesnoth-locate-warning (next-overlay-change (point))
1204 (and target
(goto-char target
))))
1206 (defun wesnoth-backward-warning ()
1207 "Move to the previous warning."
1212 (wesnoth-locate-warning (1- (previous-overlay-change (point)))
1215 (and target
(goto-char target
))))
1217 (defun wesnoth-place-overlay (start end
)
1218 "Place overlay in the region and apply necessary properties.
1219 START is the start of the region to place the overlay. END is
1220 the end of the region to place the overlay."
1221 (let ((overlay (make-overlay start end
)))
1222 (overlay-put overlay
'wesnoth-error t
)
1223 (overlay-put overlay
'face
'wesnoth-warning-face
)))
1225 (defun wesnoth-check-wml ()
1226 "Perform context-sensitive analysis of WML-code."
1228 (wesnoth-update-project-information)
1229 (remove-overlays (point-min) (point-max))
1230 (when (= 0 (hash-table-count wesnoth-tag-hash-table
))
1231 (error "WML data not available; unable to generate report"))
1232 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
1233 (let ((unmatched '())
1234 (outbuf (and (interactive-p) (get-buffer-create "*WML*")))
1239 (let ((buffer (buffer-name))
1240 (buffer-read-only nil
))
1242 (insert (format "Checking %s...\n" buffer
))
1243 (message (format "Checking %s..." buffer
))))
1245 (goto-char (point-min))
1246 (while (setq details
(wesnoth-find-next
1247 '(tag-opening tag-closing preprocessor attribute
1250 (goto-char (match-beginning 0))
1251 (cond ((nth 3 (parse-partial-sexp last-match-pos
(point)))
1253 ((eql (car details
) 'macro
)
1254 (dolist (macro (save-match-data (wesnoth-extract-macro-details
1255 (match-string-no-properties 0))))
1256 (unless (assoc macro
1257 (append (wesnoth-macro-arguments)
1258 wesnoth-local-macro-data
1259 wesnoth-macro-data
))
1260 (wesnoth-check-process "Unknown macro: '%s'"
1262 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
1263 (unless (wesnoth-check-element-type 0)
1264 (wesnoth-check-process
1265 "Tag not available in this context: '%s'"
1266 (match-string-no-properties 1)))
1267 (setq unmatched
(cons (match-string-no-properties 1)
1270 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1271 "\\( \\(\\w\\|_\\)+\\)*"))
1272 (unless (match-string-no-properties 2)
1273 (wesnoth-check-process
1274 (concat "Preprocessor statement has no argument: "
1275 (match-string-no-properties 1))))
1276 (unless (string= (match-string-no-properties 1) "#undef")
1277 (setq unmatched
(cons (match-string-no-properties 1)
1279 ((looking-at wesnoth-preprocessor-closing-regexp
)
1280 (when (and unmatched
1282 (cdr (assoc (match-string-no-properties 1)
1283 '(("enddef" .
"#define")
1284 ("endif" .
"#ifn?def"))))
1286 (wesnoth-check-process
1287 "Preprocessor statement does not nest correctly"))
1288 (setq unmatched
(cdr unmatched
)))
1289 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)=\\(.+\\)?")
1290 (unless (wesnoth-check-element-type 1)
1291 (wesnoth-check-process
1292 "Attribute not available in this context: '%s'"
1293 (match-string-no-properties 1)))
1294 (unless (match-string 3)
1295 (wesnoth-check-process
1296 "Attribute has no value")))
1297 ((looking-at "[\t ]*#else")
1298 (unless (string-match "ifn?def" (car unmatched
))
1299 (if (string= (car unmatched
) "#define")
1300 (wesnoth-check-process "Expecting: '%s'"
1302 (wesnoth-check-process "Expecting: '[/%s]'"
1304 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1305 (when (and unmatched
1306 (not (string= (match-string-no-properties 1)
1308 (wesnoth-check-process
1310 (or (cdr (assoc (car unmatched
)
1311 '(("#define" .
"#enddef")
1312 ("#ifdef" .
"#endif")
1313 ("#ifndef" .
"#endif"))))
1314 (concat "[/" (car unmatched
) "]"))))
1315 (setq unmatched
(cdr unmatched
)
1316 last-match-pos
(point))))))
1318 (dolist (tag unmatched
)
1319 (wesnoth-check-process "Unmatched element: '%s'"
1322 (setq wesnoth-define-blocks nil
)
1324 (let ((buffer (buffer-name))
1325 (buffer-read-only nil
))
1326 (display-buffer outbuf t
)
1327 (let ((warnings (- (wesnoth-line-number-at-pos
1328 (save-excursion (goto-char (point-max)))) 2)))
1329 (insert (format (concat "\nCheck complete. %d warning"
1330 (if (= warnings
1) "." "s.")) warnings
))
1331 (toggle-read-only t
))
1332 (message (format "Checking %s...done" buffer
))))))
1334 (defmacro wesnoth-element-requires
(element requirement
&optional pop
)
1335 "Process requirements for corresponding preprocessor elements.
1336 ELEMENT is the current element being tested.
1337 REQUIREMENT is the element required to exist for correct nesting.
1338 POP is an optional argument indicating the element should be
1339 removed from the list of unmatched elements."
1340 `(when (string= ,element
(match-string-no-properties 1))
1341 (if (string-match ,requirement
(car unmatched
))
1343 (and ,pop
(setq unmatched
(cdr unmatched
)))
1345 (setq position
(point)))))
1347 (defmacro wesnoth-structure-result
(position element
)
1348 "Process results of the structure check.
1349 POSITION is the position of the error or nil, if no error was
1350 found. ELEMENT is the last unmatched element, or nil if all
1351 opening elements have been matched."
1352 `(let ((expected nil
))
1354 (cond ((string= ,element
"define ") (setq expected
"#enddef"))
1355 ((string-match "ifn?def " ,element
) (setq expected
"#endif"))))
1357 (if (or ,element
,position
)
1359 (and ,position
(goto-char ,position
))
1360 (message "Expecting %s" (or expected
1361 (concat "[/" ,element
"]"))))
1362 (message "%s" "Structure appears consistent."))
1363 (when (or expected
,element
)
1364 (or expected
(concat "[/" ,element
"]"))))))
1366 (defun wesnoth-check-structure (&optional start end
)
1367 "Check the buffer for correct nesting of elements.
1368 If a problem is found in the structure, point will be placed at
1369 the location which an element was expected and the expected
1370 element will be displayed in the mini-buffer.
1372 START and END define the region to be checked. If
1373 function `transient-mark-mode' is enabled, the region specified will be
1374 checked. Otherwise START and END will be the minimum and maximum
1375 positions of the buffer, respectively."
1377 (unless (or start end
)
1378 (if (and (boundp 'transient-mark-mode
)
1379 transient-mark-mode mark-active
)
1380 (setq start
(region-beginning)
1381 end
(copy-marker (region-end)))
1382 (setq start
(point-min)
1384 (let ((unmatched '())
1387 (and start
(goto-char start
))
1388 (while (and (search-forward-regexp
1389 (concat "^\\([\t ]*\\[\\(/?\\(\\w\\|_\\)+\\)\\]\\|"
1390 wesnoth-preprocessor-regexp
"\\)") end t
)
1393 (if (or (looking-at "^[\t ]*\\[\\(\\(\\w\\|_\\)+\\)\\]")
1394 (looking-at "[\t ]*#\\(define \\|ifdef \\|ifndef \\)"))
1395 (setq unmatched
(cons (match-string-no-properties 1)
1398 ((wesnoth-element-requires "#else" "ifn?def "))
1399 ((wesnoth-element-requires "#endif" "ifn?def " t
))
1400 ((wesnoth-element-requires "#enddef" "define " t
))
1401 ((looking-at (concat "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|"
1402 wesnoth-preprocessor-closing-regexp
))
1403 (if (string= (match-string-no-properties 1)
1405 (setq unmatched
(cdr unmatched
))
1406 (setq position
(point))))))
1408 (wesnoth-structure-result position
(car unmatched
))))
1411 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
1412 "Major mode for editing WML."
1413 (kill-all-local-variables)
1414 (use-local-map wesnoth-mode-map
)
1415 (setq major-mode
'wesnoth-mode
)
1416 (setq mode-name
"WML")
1417 (set-syntax-table wesnoth-syntax-table
)
1418 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
1419 (set (make-local-variable 'comment-start
) "#")
1420 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
1421 (set (make-local-variable 'indent-region-function
) 'wesnoth-indent-region
)
1422 (set (make-local-variable 'font-lock-defaults
)
1423 '(wesnoth-font-lock-keywords
1425 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
1426 (setq indent-tabs-mode nil
)
1427 (easy-menu-add wesnoth-menu wesnoth-mode-map
)
1428 (wesnoth-create-wml-hash-table)
1429 (wesnoth-update-project-information)
1430 (run-hooks 'wesnoth-mode-hook
))
1432 (provide 'wesnoth-mode
)
1434 ;;; wesnoth-mode.el ends here