* wesnoth-mode.el (wesnoth-complete-tag): No longer prompt when no tag
[wesnoth-mode.git] / wesnoth-mode.el
blobf75a59a17fb776994699fe2a7f288dc45f96794b
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,
19 ;; MA 02139, USA.
21 ;;; Description:
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.
27 ;;; Commentary:
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)
31 ;; Optionally adding:
32 ;; (add-to-list 'auto-mode-alist '("\\.cfg\\'" . wesnoth-mode))
33 ;; to automatically load wesnoth-mode for all files ending in '.cfg'.
35 ;;; History:
36 ;; 1.3.2
37 ;; * Major performance improvements to indentation and WML checking.
38 ;; * Fixed a bug where nesting could break when inserting multiple elements
39 ;; near the last element in a buffer.
40 ;; * Fixed a bug where attributes immediately within #ifn?def were always
41 ;; reported to be illegal.
42 ;; * Fixed a bug where tags immediately within #ifn?def were always legal.
43 ;; * Fixed a bug where when inserting missing tags, scanning would only be
44 ;; performed up to point.
45 ;; * Fixed a bug when jumping between preprocessor statements.
46 ;; 1.3.1
47 ;; * Completion history available is now specific to wesnoth-mode.
48 ;; * Added binding to explicitly update macro information from the current
49 ;; buffer (C-c C-u).
50 ;; * Significantly improved performance of completion and WML checking.
51 ;; * Improved performance for inserting missing tags.
52 ;; * Fixed a bug where #ifdef was never matched when checking WML.
53 ;; * Added completion for preprocessor statements.
54 ;; * Improved macro completion and checking.
55 ;; 1.3.0
56 ;; * Added support for Xemacs.
57 ;; * WML checking is now context sensitive; checks attributes and macros.
58 ;; * WML checks are now always performed on the entire buffer, with results
59 ;; displayed in a temporary buffer.
60 ;; * Context-sensitive completion for attributes and tags.
61 ;; * Completion for built-in and project-specific macros.
62 ;; * Changed the following bindings:
63 ;; `wesnoth-insert-tag' - C-c e -> C-c t
64 ;; `wesnoth-jump-to-matching' - C-c m -> C-c o
65 ;; `wesnoth-check-structure' -> `wesnoth-check-wml' - C-c c
66 ;; * Added the following bindings:
67 ;; `wesnoth-complete-attribute' - C-c a
68 ;; `wesnoth-complete-macro' - C-c m
69 ;; `wesnoth-complete-tag' - C-c t
70 ;; * Removed the following bindings:
71 ;; `wesnoth-check-tag-names' - C-c n
72 ;; * Removed `wesnoth-check-tag-names'. Replaced by `wesnoth-check-wml'.
73 ;; * Completion for an incomplete attribute, tag, or macro at point is
74 ;; attempted via TAB.
75 ;; 1.2.5
76 ;; * Fixed support for GNU Emacs 21.
77 ;; * Added several new tags to `wesnoth-tags-list'.
78 ;; * Added M-TAB binding for `wesnoth-insert-tag'.
79 ;; * `wesnoth-insert-tag' now takes an optional numeric argument indicating
80 ;; how many blocks to wrap across instead of a region.
81 ;; * Support for `wesnoth-indent-preprocessor-bol' removed.
82 ;; * Fixed a bug in `wesnoth-insert-tag' and `wesnoth-insert-missing-closing'
83 ;; causing tags not to be inserted in the correct position.
84 ;; * Fixed highlighting of array indexes as tags.
85 ;; 1.2.4
86 ;; * Improved syntax-highlighting for macro calls.
87 ;; * Underscore is now treated as whitespace.
88 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
89 ;; * Point is now placed at the first non-whitespace character of the line,
90 ;; instead of the last.
91 ;; * Corrected minor indentation bugs.
92 ;; * Indenting across large regions is now much more efficient.
93 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
94 ;; applied.
95 ;; 1.2.3
96 ;; * Now compatible with GNU Emacs 21.4.
97 ;; * Added support for several new tags.
98 ;; * Added menu entry for wesnoth-mode.
99 ;; * Significant speed increase to indentation.
100 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
101 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
102 ;; has been removed.
103 ;; * Trailing whitespace is no longer created when creating a second
104 ;; consecutive newline.
105 ;; * Spurious newlines are no longer created when inserting a tag elements
106 ;; around a region.
107 ;; 1.2.2
108 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
109 ;; `wesnoth-element', `wesnoth-element-opening',
110 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
111 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
112 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
113 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
114 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
115 ;; * Fixed a bug in indentation where content was needed between elements pairs
116 ;; for indentation to work.
117 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
118 ;; `wesnoth-auto-indent-flag'.
119 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
120 ;; * Added indentation styles: `wesnoth-indent-default',
121 ;; `wesnoth-indent-withtags' which implement a a similar indentation
122 ;; style to the existing styles, however all preprocessor statements are
123 ;; indented to the first column.
124 ;; * Added support for several new tags.
125 ;; * Modified `wesnoth-newline' to behave more consistently.
126 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
127 ;; now leaves point at the beginning (when moving backward) or end (when
128 ;; moving forward) of the match.
129 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
130 ;; will now work on preprocessor statements. Will now warn if jump
131 ;; destination may not be correct (due to errors in WML structure).
132 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
133 ;; * `wesnoth-check-structure' can now be applied over an active region and
134 ;; now checks preprocessor statements for correct nesting.
135 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
136 ;; perform indentation by providing a prefix argument.
137 ;; * Indentation styles now leave point at the first non-whitespace character
138 ;; of the line.
139 ;; * `wesnoth-check-tag-names' now reports on success.
140 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
141 ;; * `outline-minor-mode' now works on macro definitions.
142 ;; 1.2.1
143 ;; * Base indent now defaults to 4.
144 ;; * Added support for #ifndef.
146 ;;; Code:
147 (require 'easymenu)
148 (require 'wesnoth-update)
149 (require 'wesnoth-wml-data)
151 (defconst wesnoth-mode-version "1.3.2"
152 "The current version of `wesnoth-mode'.")
154 (defgroup wesnoth-mode nil "Wesnoth-mode access"
155 :group 'languages
156 :prefix "wesnoth-")
158 (defcustom wesnoth-auto-indent-flag t
159 "Non-nil means indent the current line upon creating a newline."
160 :type 'boolean
161 :group 'wesnoth-mode)
163 (defcustom wesnoth-indent-savefile t
164 "Non-nil means to use the current indentation conventions.
165 If nil, use the old convention for indentation.
166 The current convention is all attributes are indented a level deeper
167 than their parent; in the past attributes were indented to the same
168 level as their parent.")
170 (defcustom wesnoth-base-indent 4
171 "The number of columns to indent WML."
172 :type 'integer
173 :group 'wesnoth-mode)
175 (defcustom wesnoth-check-display-type nil
176 "Determine the reporting behaviour of `wesnoth-check-wml'.
177 If t, display results using overlays in the current buffer and
178 in the summary buffer.
179 Otherwise, if non-nil, show results in a separate buffer only.
180 If nil, use only overlays in the current buffer to show results."
181 :type '(choice (const :tag "Overlays only" nil)
182 (const :tag "Summary buffer only" summary)
183 (const :tag "Both" t))
184 :group 'wesnoth-mode)
186 (defcustom wesnoth-idle-warning-timer 0.2
187 "Whether whether to display warnings in the echo-area while idle.
188 The value should be a floating point value after which to check
189 the element at point for errors, or nil to disable the feature.
190 Errors are located by running `wesnoth-check-wml' with a prefix
191 argument or by setting `wesnoth-check-display-type' to 'nil' or
192 't'."
193 :type 'float
194 :group 'wesnoth-mode)
196 (defconst wesnoth-preprocessor-regexp
197 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
198 "Regular expression to match all preprocessor statements.")
200 (defconst wesnoth-preprocessor-opening-regexp
201 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
202 "Regular expression to match \"opening\" preprocessor statements.")
204 (defconst wesnoth-preprocessor-closing-regexp
205 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
206 "Regular expression to match \"closing\" preprocessor statements.")
208 (defvar wesnoth-define-blocks '()
209 "Cache of all toplevel #define and #enddef pairs.")
211 (defvar wesnoth-history-list '()
212 "History of inserted WML elements.")
214 (defvar wesnoth-mode-hook nil)
216 (defface wesnoth-warning-face
217 '((t (:underline "tomato1")))
218 "Face to use for warnings in wesnoth-mode"
219 :group 'wesnoth-mode)
221 (defvar wesnoth-mode-map
222 (let ((map (make-sparse-keymap)))
223 (define-key map (kbd "C-M-a") 'wesnoth-backward-element)
224 (define-key map (kbd "C-M-e") 'wesnoth-forward-element)
225 (define-key map (kbd "C-m") 'wesnoth-newline)
226 (define-key map (kbd "C-j") 'wesnoth-newline-and-indent)
227 (define-key map (kbd "C-c C-c") 'wesnoth-check-wml)
228 (define-key map (kbd "C-c C-a") 'wesnoth-complete-attribute)
229 (define-key map (kbd "C-c C-t") 'wesnoth-complete-tag)
230 (define-key map (kbd "C-c C-p") 'wesnoth-complete-preprocessor)
231 (define-key map (kbd "C-c C-u") 'wesnoth-update-project-information)
232 (define-key map (kbd "M-TAB") 'wesnoth-complete-tag)
233 (define-key map (kbd "C-c C-m") 'wesnoth-complete-macro)
234 (define-key map (kbd "C-c C-o") 'wesnoth-jump-to-matching)
235 (define-key map (kbd "C-c C-f") 'wesnoth-forward-warning)
236 (define-key map (kbd "C-x `") 'wesnoth-forward-warning)
237 (define-key map (kbd "C-c C-b") 'wesnoth-backward-warning)
238 (define-key map (kbd "C-c C-/") 'wesnoth-insert-missing-closing)
239 (define-key map (kbd "TAB") 'wesnoth-indent-or-complete)
240 map)
241 "Keymap used in `wesnoth-mode'.")
243 (easy-menu-define wesnoth-menu wesnoth-mode-map "Menu for wesnoth-mode"
244 '("WML"
245 ["Check WML" wesnoth-check-wml t]
246 ["Indent or Complete" wesnoth-indent-or-complete t]
247 ["Indent buffer" (lambda ()
248 (interactive)
249 (wesnoth-indent-region (point-min) (point-max))) t]
250 ["Insert Tag" wesnoth-complete-tag t]
251 ["Insert Attribute" wesnoth-complete-attribute t]
252 ["Insert Macro" wesnoth-complete-macro t]
253 ["Insert Preprocessor" wesnoth-complete-preprocessor t]
254 ["Insert Missing Tag" wesnoth-insert-missing-closing t]
255 ["Jump to Matching" wesnoth-jump-to-matching t]
256 ["Update Macros" wesnoth-update-project-information t]))
258 (defvar wesnoth-syntax-table
259 (let ((wesnoth-syntax-table (make-syntax-table)))
260 (modify-syntax-entry ?= "." wesnoth-syntax-table)
261 (modify-syntax-entry ?_ "_" wesnoth-syntax-table)
262 (modify-syntax-entry ?- "_" wesnoth-syntax-table)
263 (modify-syntax-entry ?. "_" wesnoth-syntax-table)
264 (modify-syntax-entry ?\n ">" wesnoth-syntax-table)
265 (modify-syntax-entry ?\r ">" wesnoth-syntax-table)
266 wesnoth-syntax-table)
267 "Syntax table for `wesnoth-mode'.")
269 (defvar wesnoth-auto-check-timer nil
270 "Currently running auto-check timer.")
272 ;; Prevents automatic syntax-highlighting of elements which might be
273 ;; pre-processor statements.
274 (defvar wesnoth-syntactic-keywords
275 (list
276 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\)" 1 "w")
277 '("\\(#[\t ]*.*$\\)" 1 "<"))
278 "Highlighting syntactic keywords within `wesnoth-mode'.")
280 (defvar wesnoth-font-lock-keywords
281 (list
282 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face)
283 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
284 font-lock-keyword-face)
285 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
286 2 font-lock-function-name-face)
287 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face))
288 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face))
289 '("}" . font-lock-function-name-face)
290 '("^[\t ]*\\(\\[[^]]+\\]\\)" 1 font-lock-type-face)
291 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face)
292 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
293 1 font-lock-variable-name-face))
294 "Syntax highlighting for `wesnoth-mode'.")
296 (defun wesnoth-element-closing (&optional limited)
297 "Return the regexp to match a closing element.
298 If LIMITED is non-nil, return a regexp which matches only the
299 #enddef preprocessor."
300 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\|"
301 (if limited
302 "#enddef"
303 "#end\\(?:def\\|if\\)")
304 "\\)"))
306 (defun wesnoth-element-opening (&optional limited)
307 "Return the regexp to match a closing element.
308 If LIMITED is non-nil, return a regexp which matches only the
309 #define preprocessor."
310 (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define "
311 (if limited
313 "\\|#ifn?def ")
314 "\\)"))
316 (defun wesnoth-element (&optional limited)
317 "Return the regexp to match a closing element.
318 If LIMITED is non-nil, return a regexp which matches only the
319 #define and #enddef preprocessors."
320 (concat "^[\t ]*\\(\\[[/+]?\\(\\w\\|_\\)+\\]?\\|"
321 (if limited
322 "#define \\|#enddef"
323 (substring wesnoth-preprocessor-regexp 5))
324 "\\)"))
327 ;;; Insertion and completion
328 (defmacro wesnoth-element-completion (completions prompt partial
329 &optional completep)
330 "Process completion of COMPLETIONS, displaying PROMPT.
331 PARTIAL is the partial string on which to attempt completion."
332 `(let* ((element (when ,partial (try-completion ,partial ,completions))))
333 (cond ((eq element t)
334 ,partial)
335 ((and completep (null element))
336 nil)
337 ((and element (eq (try-completion element ,completions) t))
338 element)
339 ((not (if (listp (car ,completions))
340 (assoc element ,completions)
341 (member element ,completions)))
342 (completing-read ,prompt ,completions
343 nil nil element
344 'wesnoth-history-list)))))
346 (defun wesnoth-parent-tag ()
347 "Return the name of the parent tag.
348 If the parent is a preprocessor statement, return non-nil.
349 If the element does not have a parent, return nil.
350 Otherwise, return a string containing the name of the parent tag."
351 (save-excursion
352 (let ((start-point (point))
353 (depth 1))
354 (when (save-excursion (> (point) (progn (back-to-indentation)
355 (point))))
356 (end-of-line))
357 (while (and (> depth 0)
358 (search-backward-regexp (wesnoth-element t)
359 (point-min) t))
360 (if (string-match "[\t ]*\\[/" (match-string 0))
361 (setq depth (1+ depth))
362 (setq depth (1- depth))))
363 (beginning-of-line)
364 (if (> depth 0)
366 (when (looking-at (wesnoth-element-opening))
367 (let ((parent (match-string-no-properties 1)))
368 (if (string-match wesnoth-preprocessor-opening-regexp parent)
370 (substring parent 1 (1- (length parent))))))))))
372 (defun wesnoth-partial-macro-p ()
373 "Return non-nil if point is in a partial macro."
374 (save-excursion
375 (back-to-indentation)
376 (let ((end (save-excursion (end-of-line) (point)))
377 (opened 0))
378 (while (search-forward-regexp "[{}]" end t)
379 (if (string= (match-string 0) "{")
380 (setq opened (1+ opened))
381 (setq opened (1- opened))))
382 (> opened 0))))
384 (defun wesnoth-indent-or-complete (&optional elements)
385 "Indent or complete the line at point, depending on context.
386 ELEMENTS is the number of elements to wrap around if inserting
387 matching tags."
388 (interactive "P")
389 (or elements (setq elements 0))
390 (cond
391 ((looking-back "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$")
392 (wesnoth-complete-tag elements t))
393 ((wesnoth-partial-macro-p)
394 (wesnoth-complete-macro t))
395 ((looking-back "^#\\w+$")
396 (wesnoth-complete-preprocessor elements t))
397 ((looking-back "\\[/\\(\\(\\w\\|_\\)*\\)[\t ]*$")
398 (delete-region (save-excursion (back-to-indentation) (point))
399 (progn (end-of-line) (point)))
400 (wesnoth-insert-missing-closing)
401 (end-of-line))
402 ((looking-back "\\(\\(\\w\\|_\\)+\\)[\t ]*$")
403 (wesnoth-complete-attribute t))
405 (if (looking-back "^[^#]*=.*")
406 (save-excursion
407 (wesnoth-indent))
408 (wesnoth-indent)))))
410 (defun wesnoth-preprocessor-closed-p (preprocessor)
411 "Determine whether PREPROCESSOR has been closed.
412 PREPROCESSOR is a string matching the preprocessor statement to
413 be inserted."
414 (save-excursion
415 (back-to-indentation)
416 (wesnoth-jump-to-matching preprocessor)
417 (looking-at
418 (if (string= preprocessor "#define ")
419 "#enddef"
420 "#endif"))))
422 (defun wesnoth-complete-preprocessor (&optional elements completep)
423 "Complete and insert the preprocessor at point.
424 ELEMENTS is the number of elements to wrap around.
425 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
426 (interactive "P")
427 (or elements (setq elements 0))
428 (let* ((completions (wesnoth-emacs-completion-formats
429 '("define" "else" "ifdef" "ifndef"
430 "enddef" "endif" "undef")))
431 (partial (when completep
432 (save-excursion
433 (back-to-indentation)
434 (when (looking-at "#\\(\\w+\\)$")
435 (match-string-no-properties 1)))))
436 (preprocessor (wesnoth-element-completion
437 completions "Preprocessor: " partial completep))
438 (closedp
439 (save-excursion
440 (when preprocessor
441 (unless (string= "#" (substring preprocessor 0 1))
442 (setq preprocessor (concat "#" preprocessor)))
443 (when (string-match "#\\(define\\|ifn?def\\|undef\\)" preprocessor)
444 (setq preprocessor (concat preprocessor " ")))
445 (when partial
446 (delete-region (progn (back-to-indentation) (point))
447 (progn (end-of-line) (point))))
448 (wesnoth-preprocessor-closed-p preprocessor)))))
449 (when preprocessor
450 (when partial
451 (delete-region (progn (back-to-indentation) (point))
452 (progn (end-of-line) (point))))
453 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor)
454 (not closedp))
455 (progn
456 (wesnoth-insert-tag elements preprocessor)
457 (forward-line -1)
458 (end-of-line))
459 (wesnoth-insert-element-separately preprocessor)))))
461 (defun wesnoth-macro-arguments ()
462 "Find any current macro arguments."
463 (let ((results '())
464 (depth (wesnoth-within-define (point))))
465 (save-excursion
466 (while (> depth 0)
467 (save-match-data
468 (search-backward-regexp
469 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
470 (point-min) t)
471 (when (<= (wesnoth-within-define (point)) depth)
472 (and (> depth 0)
473 (setq results
474 (append (mapcar (lambda (macro)
475 (list macro nil))
476 (split-string
477 (match-string-no-properties 1)))
478 results)))
479 (setq depth (1- depth)))))
480 results)))
482 (defun wesnoth-complete-macro (&optional completep)
483 "Complete and insert the macro at point.
484 If COMPLETEP is non-nil, attempt to complete the macro at point."
485 (interactive)
486 (wesnoth-update-project-information)
487 (let* ((macro-information (append (wesnoth-macro-arguments)
488 wesnoth-macro-data
489 wesnoth-local-macro-data))
490 (completions (wesnoth-emacs-completion-formats
491 (mapcar 'car macro-information)))
492 (partial (when completep
493 (save-excursion
494 (when (looking-back "{\\(\\(\\w\\|_\\)*\\)")
495 (match-string-no-properties 1)))))
496 (macro (wesnoth-element-completion completions "Macro: " partial
497 completep))
498 (args (cadr (assoc macro macro-information))))
499 (when macro
500 (if partial
501 (progn
502 ;; Delete the region corresponding to the current macro.
503 (delete-region
504 (save-excursion
505 (search-backward "{"
506 (save-excursion (back-to-indentation) (point))
508 (point))
509 (save-excursion
510 (if (search-forward-regexp
511 "[ }\t]"
512 (save-excursion (end-of-line) (point))
514 (1- (point))
515 (save-excursion (end-of-line) (point)))))
516 (insert "{" macro (if args " }" "}")))
517 (wesnoth-insert-element-separately "{" macro (if args " }" "}")))
518 (save-excursion
519 (wesnoth-indent))
520 (when args
521 (forward-char -1)))))
523 (defun wesnoth-complete-attribute (&optional completep)
524 "Insert the attribute at point.
525 If COMPLETEP is non-nil, attempt to complete the attribute at point."
526 (interactive)
527 (let* ((completions (wesnoth-build-completion 1))
528 (partial (when completep
529 (save-excursion
530 (when (looking-back "^[\t ]*\\(\\(\\w\\|_\\)+\\)")
531 (match-string-no-properties 1)))))
532 (attribute (wesnoth-element-completion completions "Attribute: "
533 partial completep)))
534 (when attribute
535 (if (and partial completep)
536 (progn
537 (delete-region (progn (back-to-indentation) (point))
538 (progn (end-of-line) (point)))
539 (insert attribute "="))
540 (wesnoth-insert-element-separately attribute
541 (if (string-match "=" attribute)
543 "=")))
544 (save-excursion
545 (wesnoth-indent)))))
547 (defun wesnoth-complete-tag (&optional elements completep)
548 "Complete and insert the tag at point.
549 ELEMENTS is the number of elements to wrap around.
550 If COMPLETEP is non-nil, attempt to complete tag at point."
551 (interactive "P")
552 (or elements (setq elements 0))
553 (let* ((completions (wesnoth-build-completion 0))
554 (partial (when (and completep
555 (looking-back "\\[\\(\\(\\w\\|_\\)+\\)"))
556 (match-string-no-properties 1)))
557 (tag (wesnoth-element-completion completions "Tag: " partial
558 completep))
559 (closedp
560 (save-excursion
561 (wesnoth-jump-to-matching (concat "[" tag "]"))
562 (back-to-indentation)
563 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
564 (string= tag (match-string 1))))))
565 (when tag
566 (when completep
567 (delete-region (progn (back-to-indentation) (point))
568 (progn (end-of-line) (point))))
569 (if (and closedp completep)
570 (progn
571 (wesnoth-insert-and-indent "[" tag "]")
572 (end-of-line))
573 (wesnoth-insert-tag elements tag)))))
575 (defun wesnoth-build-completion (position)
576 "Create a new list for tag completion if necessary.
577 Rebuilding list is required for versions of GNU Emacs earlier
578 than 22. POSITION is the argument passed to `nth' for
579 `wesnoth-tag-data'."
580 (interactive "P")
581 (let ((parent (wesnoth-parent-tag)))
582 (wesnoth-emacs-completion-formats
583 (if (or (stringp parent) (null parent))
584 (nth position (gethash parent wesnoth-tag-hash-table))
585 (mapcar 'car wesnoth-tag-data)))))
587 (defun wesnoth-emacs-completion-formats (candidates)
588 "Return the completions in the correct format for `emacs-major-version'.
589 CANDIDATES is a list of all possible completions."
590 (if (> emacs-major-version 21)
591 candidates
592 (let ((tags '())
593 (iter 0))
594 (dolist (tag candidates)
595 (setq iter (1+ iter))
596 (setq tags (append tags (list (cons tag iter)))))
597 tags)))
599 (defun wesnoth-insert-tag (&optional elements tagname)
600 "Insert the specified opening tag and it's matching closing tag.
601 Both the opening and closing tags will be placed on their own
602 lines with point positioned between them. Completion of tags at
603 the prompt uses `wesnoth-tags-list'.
605 ELEMENTS is specifies the number of following blocks which the
606 tag should wrap around.
608 TAGNAME is the name of the tag to be inserted."
609 (interactive "Ps")
610 (unless tagname
611 (setq tagname (completing-read "Tag: " (wesnoth-build-completion 1)
612 nil nil nil wesnoth-history-list)))
613 (when (or (not elements)
614 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
615 wesnoth-preprocessor-regexp "\\)")))
616 (setq elements 0))
617 (let ((depth 0)
618 (start (save-excursion (forward-line -1) (point)))
619 (end (unless (= elements 0)
620 ;; Work around some strange behaviour when the target is at the
621 ;; end of the buffer.
622 (save-excursion
623 (goto-char (point-max))
624 (beginning-of-line)
625 (unless (looking-at "^[\t ]*$")
626 (end-of-line)
627 (newline)))
628 (wesnoth-nth-pair-position elements))))
629 (if (string-match wesnoth-preprocessor-regexp tagname)
630 (wesnoth-insert-element-separately tagname)
631 (wesnoth-insert-element-separately "[" tagname "]"))
632 (save-excursion
633 (if end
634 (goto-char (marker-position end))
635 (newline (if (string-match wesnoth-preprocessor-regexp tagname) 1 2)))
636 (if (string-match wesnoth-preprocessor-opening-regexp tagname)
637 (wesnoth-insert-element-separately
638 (if (string= tagname "#define ")
639 "#enddef"
640 "#endif"))
641 (wesnoth-insert-element-separately "[/" tagname "]"))
642 (indent-region start (point) nil))
643 (unless end
644 (forward-line 1)))
645 (wesnoth-indent))
647 (defun wesnoth-insert-element-separately (&rest strings)
648 "Concatenate STRINGS and insert them on a line of their own."
649 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
650 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
651 (progn
652 (forward-line 1)
653 (end-of-line))
654 (end-of-line)
655 (newline))
656 (beginning-of-line)
657 (if (looking-at "^[\t ]*$")
658 (end-of-line)
659 (open-line 1)))
660 (insert (apply 'concat strings)))
662 (defun wesnoth-insert-missing-closing (&optional start end)
663 "Insert the next expected closing element at point.
665 START and END define the region to check for missing closing
666 elements. If function `transient-mark-mode' is enabled, the region
667 specified will be used as START and END. Otherwise, START and
668 END will be the minimum and current positions of the buffer,
669 respectively."
670 (interactive)
671 (if (and (boundp 'transient-mark-mode)
672 transient-mark-mode mark-active)
673 (setq start (region-beginning)
674 end (copy-marker (region-end)))
675 (setq start (point-min)
676 end (point-max)))
677 (let ((element (wesnoth-check-structure start end)))
678 (cond ((not element)
679 (message "%s" "Unable to find element to insert"))
680 ((string= element "Unexpected end of file")
681 (message "%s" element))
683 (wesnoth-insert-element-separately element)
684 (wesnoth-indent)
685 (end-of-line)))))
687 (defun wesnoth-insert-and-indent (&rest args)
688 "Concatenate and insert the given string(s) before indenting.
690 ARGS is a list of strings to be inserted."
691 (insert (apply 'concat args))
692 (wesnoth-indent))
694 (defun wesnoth-newline (&optional indent)
695 "Indent both the current line and the newline created.
696 If `wesnoth-auto-indent-flag' is nil, indentation will not be
697 performed. Indentation can be forced by setting INDENT to
698 non-nil."
699 (interactive "P")
700 (save-excursion
701 (when (and (or wesnoth-auto-indent-flag indent)
702 (not (looking-at "^[\t ]*$")))
703 (wesnoth-indent)))
704 (newline))
706 ;;; Movement
707 (defmacro wesnoth-navigate-element (repeat search-function bound)
708 "Move point to the tag in the given direction REPEAT times.
710 SEARCH-FUNCTION is the symbol of the function for searching in
711 the required direction, with BOUND marking the furthest point to
712 search."
713 `(progn
714 (or ,repeat (setq ,repeat 1))
715 (while (> ,repeat 0)
716 (and (eq ,search-function 'search-forward-regexp) (end-of-line))
717 (funcall ,search-function (wesnoth-element-opening) ,bound t)
718 (back-to-indentation)
719 (setq repeat (1- ,repeat)))))
721 (defun wesnoth-nth-pair-position (count)
722 "Return `point' after COUNT number of matching element pairs.
723 COUNT is a positive number representing the number of balanced
724 pairs to move across.
725 `point' is returned as a marker object."
726 (save-excursion
727 (let ((start (point))
728 (failed nil))
729 (if (> (point) (save-excursion (back-to-indentation) (point)))
730 (end-of-line)
731 (beginning-of-line))
732 (while (> count 0)
733 ;; Currently looking-at target tag. Stop here to avoid
734 ;; incorrect nesting.
735 (unless (wesnoth-search-for-matching-tag
736 'search-forward-regexp (wesnoth-element-closing) 'point-max)
737 (setq count 0)
738 (setq failed t))
739 (and (> (setq count (1- count)) 0) (forward-line 1)))
740 (if failed
741 (beginning-of-line)
742 (end-of-line))
743 (point-marker))))
745 (defun wesnoth-forward-element (repeat)
746 "Move point to the end of the next tag.
747 REPEAT is an optional numeric argument. If REPEAT is non-nil,
748 jump forward the specified number of tags."
749 (interactive "p")
750 (if (< repeat 0)
751 (wesnoth-backward-element (abs repeat))
752 (wesnoth-navigate-element repeat 'search-forward-regexp (point-max))))
754 (defun wesnoth-backward-element (repeat)
755 "Move point to the beginning of the previous tag.
756 REPEAT is an optional numeric argument. If REPEAT is non-nil,
757 jump backward the specified number of tags."
758 (interactive "p")
759 (if (< repeat 0)
760 (wesnoth-forward-element (abs repeat))
761 (wesnoth-navigate-element repeat 'search-backward-regexp (point-min))))
763 (defmacro wesnoth-search-for-matching-tag (search-function
764 search-string bound &optional skip)
765 "Search for the matching tag for the current line.
766 SEARCH-FUNCTION is the name of the function used to perform the search.
767 SEARCH-STRING is a string representing the matching tag type.
768 BOUND is the bound to be passed to the search function.
769 If SKIP is non-nil, skip the first element and continue from there."
770 `(let ((depth 1))
771 (when (and (or (and ,skip (forward-line 1))
772 (funcall ,search-function (wesnoth-element) (funcall ,bound) t))
773 (or ,skip (not (string-match ,search-string (match-string 0)))))
774 (while (and (> depth 0)
775 (funcall ,search-function (wesnoth-element)
776 (funcall ,bound) t))
777 (if (string-match ,search-string (match-string 0))
778 (setq depth (1- depth))
779 (setq depth (1+ depth))))
780 (= depth 0))))
782 (defun wesnoth-jump-to-matching (&optional opening)
783 "Jump point to the matching opening/closing tag.
784 OPENING is an opening preprocessor statement to attempt to find a match for."
785 (interactive)
786 (beginning-of-line)
787 (let ((target nil)
788 (first-element nil))
789 (save-excursion
790 (cond
791 ((or (and (stringp opening)
792 (string-match (wesnoth-element-opening) opening))
793 (looking-at (wesnoth-element-opening)))
794 (progn
795 (setq first-element (match-string-no-properties 0 opening))
796 (when (wesnoth-search-for-matching-tag
797 'search-forward-regexp (wesnoth-element-closing) 'point-max
798 (stringp opening))
799 (beginning-of-line)
800 (if (and (string-match wesnoth-preprocessor-opening-regexp
801 first-element)
802 (looking-at (wesnoth-element-closing)))
803 (when (string= (match-string-no-properties 0)
804 (cdr (assoc first-element
805 '(("#define " . "#enddef")
806 ("#ifndef " . "#endif")
807 ("#ifdef " . "#endif")))))
808 (setq target (point)))
809 (setq target (point))))))
810 ((looking-at (wesnoth-element-closing))
811 (end-of-line)
812 (setq first-element (match-string-no-properties 0))
813 (when (wesnoth-search-for-matching-tag
814 'search-backward-regexp (wesnoth-element-opening)
815 'wesnoth-wml-start-pos)
816 (if (and (string-match wesnoth-preprocessor-closing-regexp
817 first-element)
818 (looking-at (wesnoth-element-opening)))
819 (progn
820 (when (or (and (string= "#enddef" first-element)
821 (string= "#define "
822 (match-string-no-properties
823 0)))
824 (and (string= "#endif" first-element)
825 (string-match
826 "#ifn?def "
827 (match-string-no-properties
828 0))))
829 (setq target (point))))
830 (setq target (point)))))
832 (search-backward-regexp (wesnoth-element-opening) (point-min) t)
833 (setq target (point)))))
834 (if target
835 (progn
836 (goto-char target)
837 (back-to-indentation))
838 (when (interactive-p)
839 (message "%s" "Tag does not appear to be matched")))))
841 ;;; Indentation
842 (defun wesnoth-wml-start-pos ()
843 "Determine the position of `point' relative to where the actual WML begins.
844 Return the likely starting position of the WML if it is found.
845 Otherwise return nil."
846 (save-excursion
847 (goto-char (point-min))
848 (when (search-forward-regexp (wesnoth-element) (point-max) t)
849 (beginning-of-line)
850 (point))))
852 (defun wesnoth-first-column-indent-p (point)
853 "Return non-nil if the current line should not be indented.
855 POINT is the position in the buffer to check.
856 CONTEXT represents the type of element which precedes the current element."
857 (or (not (wesnoth-wml-start-pos))
858 (<= (point) (wesnoth-wml-start-pos))
859 (nth 3 (parse-partial-sexp
860 (save-excursion (search-backward-regexp
861 (wesnoth-element t) (point-min) t)
862 (point))
863 point))
864 (looking-at wesnoth-preprocessor-regexp)))
866 (defun wesnoth-indent ()
867 "Indent the current line as WML."
868 (beginning-of-line)
869 (let* ((cur-indent 0)
870 (context-data (wesnoth-determine-context (point)))
871 (context (car context-data))
872 (ref-indent (cdr context-data)))
873 (unless (wesnoth-first-column-indent-p (point))
874 (cond
875 ((eq context 'opening)
876 (if (or (and wesnoth-indent-savefile
877 (not (looking-at (wesnoth-element-closing t))))
878 (looking-at (wesnoth-element-opening t)))
879 (setq cur-indent (+ ref-indent wesnoth-base-indent))
880 (setq cur-indent ref-indent)))
881 ((eq context 'closing)
882 (if (or (looking-at "^[\t ]*\\[/")
883 (and (not wesnoth-indent-savefile)
884 (not (looking-at (wesnoth-element-opening t)))))
885 (setq cur-indent (- ref-indent wesnoth-base-indent))
886 (setq cur-indent ref-indent)))))
887 (indent-line-to (max cur-indent 0))))
889 (defun wesnoth-within-define (position)
890 "Determine whether point is currently inside a #define block.
891 POSITION is the initial cursor position."
892 (save-match-data
893 (let ((depth 0)
894 (defblocks (or wesnoth-define-blocks
895 (wesnoth-find-macro-definitions))))
896 (unless (equal (car defblocks) 'none)
897 (dolist (element defblocks)
898 (when (= (cadr (sort (append (mapcar 'marker-position (cadr element))
899 (list position)) '>)) position)
900 (setq depth (max (car element) depth)))))
901 depth)))
903 (defun wesnoth-find-macro-definitions ()
904 "Return information regarding positioning of macro definitions."
905 (save-excursion
906 (goto-char (point-min))
907 (let ((depth 0)
908 openings cache)
909 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t)
910 (and (string= (match-string 1) "#define") (beginning-of-line))
911 (setq depth
912 (if (string= (match-string 1) "#define")
913 (progn
914 (add-to-list 'openings (point-marker))
915 (1+ depth))
916 (if openings
917 (progn
918 (add-to-list 'cache
919 (list depth (list (car openings)
920 (point-marker))))
921 (setq openings (cdr openings))
922 (1- depth))
923 depth)))
924 (end-of-line))
925 (or cache (list 'none)))))
927 (defun wesnoth-indent-region (start end)
928 "Indent the region from START to END.
929 Creates and destroys a cache of macro definition details as necessary."
930 (interactive "r")
931 (unwind-protect
932 (save-excursion
933 (goto-char end)
934 (setq end (point-marker))
935 (goto-char start)
936 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
937 (or (bolp) (forward-line 1))
938 (while (< (point) end)
939 (if (looking-at "^[\t ]*$")
940 (indent-line-to 0)
941 (funcall indent-line-function))
942 (forward-line 1)))
943 (setq wesnoth-define-blocks nil)))
945 (defun wesnoth-determine-context (position)
946 "Determine the type of the last relevant element.
947 POSITION is the buffer position of the element for which to
948 determine the context."
949 (save-excursion
950 (let ((match (or
951 (and (search-backward-regexp
952 (wesnoth-element t) (point-min) t)
953 (match-string 1))
954 ""))
955 (depth (wesnoth-within-define position)))
956 (while (and (wesnoth-wml-start-pos)
957 (> (wesnoth-within-define (point)) depth)
958 (not (= (point) (wesnoth-wml-start-pos))))
959 (search-backward-regexp (wesnoth-element t)
960 (wesnoth-wml-start-pos) t)
961 (setq match (match-string 1)))
962 (when (and (wesnoth-wml-start-pos)
963 (= (point) (wesnoth-wml-start-pos))
964 (= depth 0)
965 (string-match "#define" match))
966 ;; Found nothing of use; reset match and assume top-level tag.
967 (setq match ""))
968 (cond
969 ((string-match "\\[/\\|#enddef" match)
970 (cons 'closing (current-indentation)))
971 ((string-match "\\[[^/]?\\|#define" match)
972 (cons 'opening (current-indentation)))))))
974 (defun wesnoth-newline-and-indent (&optional indent)
975 "Indent both the current line and the newline created.
976 If `wesnoth-auto-indent-flag' is nil, indentation will not be
977 performed.
979 If the optional argument, INDENT is non-nil, force indentation to
980 be performed."
981 (interactive)
982 (wesnoth-newline)
983 (when (or wesnoth-auto-indent-flag indent)
984 (wesnoth-indent)))
986 ;;; WML checks
987 (defun wesnoth-check-element-type (position)
988 "Determine the context of the element.
989 POSITION is the position of the element in the list."
990 (let ((parent (save-match-data (wesnoth-parent-tag))))
991 (if (or (stringp parent) (null parent))
992 (member (match-string-no-properties 1)
993 (nth position (gethash parent wesnoth-tag-hash-table)))
994 (member (match-string-no-properties 1)
995 (mapcar 'car wesnoth-tag-data)))))
997 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
998 (defun wesnoth-line-number-at-pos (&optional pos)
999 "Return (narrowed) buffer line number at position POS.
1000 If POS is nil, use current buffer location.
1001 Counting starts at (point-min), so the value refers
1002 to the contents of the accessible portion of the buffer."
1003 (let ((opoint (or pos (point))) start)
1004 (save-excursion
1005 (goto-char (point-min))
1006 (setq start (point))
1007 (goto-char opoint)
1008 (forward-line 0)
1009 (1+ (count-lines start (point))))))
1011 (defun wesnoth-check-output (buffer format-string &rest args)
1012 "Output the string as passed to `format'.
1013 BUFFER is the buffer to output the result.
1014 FORMAT-STRING is the string as the first argument of `format'.
1015 ARGS is any additional data required by `format' to handle FORMAT-STRING."
1016 (save-excursion
1017 (let ((lnap (wesnoth-line-number-at-pos)))
1018 (set-buffer buffer)
1019 (insert (apply 'format (concat "Line %d: " format-string "\n")
1020 lnap args)))))
1022 (defun wesnoth-extract-macro-details (macro-arguments)
1023 "Return a list of all macros in MACRO-ARGUMENTS."
1024 (when macro-arguments
1025 (let ((results '()))
1026 (dolist (macro (split-string macro-arguments "[{}][\t ]*" t))
1027 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
1028 macro)
1029 (add-to-list 'results (match-string-no-properties 1 macro))))
1030 results)))
1032 (defmacro wesnoth-check-process (format-string &rest args)
1033 "Output to buffer where requested and position overlays as required.
1034 FORMAT-STRING is the string to pass as the first argument to
1035 `format' for the error. ARGS is a list of arguments required by
1036 FORMAT-STRING."
1037 `(progn
1038 (and (or wesnoth-check-display-type detailedp)
1039 (wesnoth-check-output outbuf ,format-string ,@args))
1040 (when (or (eql wesnoth-check-display-type nil)
1041 (eql wesnoth-check-display-type t)
1042 detailedp)
1043 (wesnoth-place-overlay (match-beginning 0) (match-end 0)
1044 ,format-string ,@args))))
1046 (defmacro wesnoth-locate-warning (search start end)
1047 "Search for and move to the warning in the given direction.
1048 SEARCH is a form to locate the warning in the required direction.
1049 START is the start of the region searched.
1050 END is the end of the region searched."
1051 `(progn
1052 (when (and (overlays-at (point))
1053 (overlay-get (car (overlays-at (point))) 'error))
1054 (goto-char ,search))
1055 (when (= (point) ,start)
1056 (goto-char ,end))
1057 (let* ((position nil)
1058 (target
1059 (while (and (setq position (goto-char ,search))
1060 (not
1061 (or (= ,start position)
1062 (and (overlays-at (point))
1063 (overlay-get (car (overlays-at (point)))
1064 'error))))))))
1065 (when (= position ,start)
1066 t))))
1068 (defun wesnoth-forward-warning ()
1069 "Move to the next warning."
1070 (interactive)
1071 (forward-line 1)
1072 (while (wesnoth-locate-warning (next-overlay-change (point)) (point-max)
1073 (point-min))))
1075 (defun wesnoth-backward-warning ()
1076 "Move to the previous warning."
1077 (interactive)
1078 (forward-line -1)
1079 (while (wesnoth-locate-warning (previous-overlay-change (point)) (point-min)
1080 (point-max))))
1082 (defun wesnoth-place-overlay (start end format-string &rest args)
1083 "Place overlay in the region and apply necessary properties.
1084 START is the start of the region to place the overlay. END is
1085 the end of the region to place the overlay. FORMAT-STRING is the
1086 string to pass as the first argument to `format' to use for the
1087 error. ARGS is a list of arguments for FORMAT-STRING."
1088 (let ((overlay (make-overlay start end)))
1089 (overlay-put overlay
1090 'error (apply 'format format-string args))
1091 (overlay-put overlay 'face 'wesnoth-warning-face)))
1093 (defun wesnoth-check-wml (&optional detailedp)
1094 "Perform context-sensitive analysis of WML-code.
1095 If DETAILEDP or `wesnoth-check-display-type' are non-nil, report
1096 results in a separate buffer and as overlays in the current buffer."
1097 (interactive "P")
1098 (wesnoth-update-project-information)
1099 (remove-overlays (point-min) (point-max))
1100 (when (= 0 (hash-table-count wesnoth-tag-hash-table))
1101 (error "WML data not available; unable to generate report"))
1102 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1103 (let ((unmatched '())
1104 (outbuf (and (interactive-p) (get-buffer-create "*WML*")))
1105 (last-match-pos 1))
1106 (when (or wesnoth-check-display-type detailedp)
1107 (save-excursion
1108 (let ((buffer (buffer-name)))
1109 (set-buffer outbuf)
1110 (erase-buffer)
1111 (insert (format "Checking %s...\n" buffer))
1112 (message (format "Checking %s..." buffer)))))
1113 (save-excursion
1114 (goto-char (point-min))
1115 (while (search-forward-regexp "{\\(\\(\\(\\w\\|_\\)+\\)[^=
1116 ]*\\)}" (point-max) t)
1117 (dolist (macro (save-match-data (wesnoth-extract-macro-details
1118 (match-string-no-properties 1))))
1119 (unless (assoc macro
1120 (append (wesnoth-macro-arguments)
1121 wesnoth-local-macro-data
1122 wesnoth-macro-data))
1123 (wesnoth-check-process "Unknown macro definition: '%s'"
1124 macro))))
1125 (goto-char (point-min))
1126 (while (search-forward-regexp
1127 ;; Match tags, preprocessor statements and attributes.
1128 (concat "^[\t ]*\\(\\[[+/]?\\([a-z]\\(\\w\\|_\\)+\\)\\]\\|"
1129 "\\(\\w\\|_\\)+=\\|"
1130 wesnoth-preprocessor-regexp "\\)")
1131 (point-max) t)
1132 (save-excursion
1133 (goto-char (match-beginning 1))
1134 (cond ((nth 3 (parse-partial-sexp last-match-pos (point)))
1135 nil)
1136 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
1137 (unless (wesnoth-check-element-type 0)
1138 (wesnoth-check-process
1139 "Tag not available in this context: '%s'"
1140 (match-string-no-properties 1)))
1141 (setq unmatched (cons (match-string-no-properties 1)
1142 unmatched)))
1143 ((looking-at
1144 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1145 "\\( \\(\\w\\|_\\)+\\)*"))
1146 (unless (match-string-no-properties 2)
1147 (wesnoth-check-process
1148 (concat "Preprocessor statement has no argument: "
1149 (match-string-no-properties 1))))
1150 (unless (string= (match-string-no-properties 1) "#undef")
1151 (setq unmatched (cons (match-string-no-properties 1)
1152 unmatched))))
1153 ((looking-at wesnoth-preprocessor-closing-regexp)
1154 (when (and unmatched
1155 (not (string-match
1156 (cdr (assoc (match-string-no-properties 1)
1157 '(("enddef" . "#define")
1158 ("endif" . "#ifn?def"))))
1159 (car unmatched))))
1160 (wesnoth-check-process
1161 "Preprocessor statement does not nest correctly"))
1162 (setq unmatched (cdr unmatched)))
1163 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)=\\(.+\\)?")
1164 (unless (wesnoth-check-element-type 1)
1165 (wesnoth-check-process
1166 "Attribute not available in this context: '%s'"
1167 (match-string-no-properties 1)))
1168 (unless (match-string 3)
1169 (wesnoth-check-process
1170 "Attribute has no value")))
1171 ((looking-at "[\t ]*#else")
1172 (unless (string-match "ifn?def" (car unmatched))
1173 (if (string= (car unmatched) "#define")
1174 (wesnoth-check-process "Expecting: '%s'"
1175 (car unmatched))
1176 (wesnoth-check-process "Expecting: '[/%s]'"
1177 (car unmatched)))))
1178 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1179 (when (and unmatched
1180 (not (string= (match-string-no-properties 1)
1181 (car unmatched))))
1182 (wesnoth-check-process "Expecting: '[/%s]'"
1183 (car unmatched)))
1184 (setq unmatched (cdr unmatched)
1185 last-match-pos (point))))))
1186 (if unmatched
1187 (dolist (tag unmatched)
1188 (wesnoth-check-process "Unmatched element: '%s'"
1189 (car unmatched)))))
1190 (save-excursion
1191 (setq wesnoth-define-blocks nil)
1192 (when (or wesnoth-check-display-type detailedp)
1193 (let ((buffer (buffer-name)))
1194 (set-buffer outbuf)
1195 (display-buffer outbuf t)
1196 (let ((warnings (- (wesnoth-line-number-at-pos
1197 (save-excursion (goto-char (point-max)))) 2)))
1198 (insert (format (concat "\nCheck complete. %d warning"
1199 (if (= warnings 1) "." "s.")) warnings)))
1200 (message (format "Checking %s...done" buffer)))))))
1202 (defmacro wesnoth-element-requires (element requirement &optional pop)
1203 "Process requirements for corresponding preprocessor elements.
1204 ELEMENT is the current element being tested.
1205 REQUIREMENT is the element required to exist for correct nesting.
1206 POP is an optional argument indicating the element should be
1207 removed from the list of unmatched elements."
1208 `(when (string= ,element (match-string-no-properties 1))
1209 (if (string-match ,requirement (car unmatched))
1210 (progn
1211 (and ,pop (setq unmatched (cdr unmatched)))
1213 (setq position (point)))))
1215 (defmacro wesnoth-structure-result (position element)
1216 "Process results of the structure check.
1217 POSITION is the position of the error or nil, if no error was
1218 found. ELEMENT is the last unmatched element, or nil if all
1219 opening elements have been matched."
1220 `(let ((expected nil))
1221 (when ,element
1222 (cond ((string= ,element "define ") (setq expected "#enddef"))
1223 ((string-match "ifn?def " ,element) (setq expected "#endif"))))
1224 (if (interactive-p)
1225 (if (or ,element ,position)
1226 (progn
1227 (and ,position (goto-char ,position))
1228 (message "Expecting %s" (or expected
1229 (concat "[/" ,element "]"))))
1230 (message "%s" "Structure appears consistent."))
1231 (when (or expected ,element)
1232 (or expected (concat "[/" ,element "]"))))))
1234 (defun wesnoth-idle-warning-check ()
1235 "Control timer for checking warnings at `point'.
1236 If a timer is not running and `wesnoth-idle-warning-timer' is
1237 non-nil, run an idle timer for at intervals specified by
1238 `wesnoth-idle-warning-timer'."
1239 (interactive)
1240 (cond
1241 ((and wesnoth-auto-check-timer (not wesnoth-idle-warning-timer))
1242 (cancel-timer wesnoth-auto-check-timer))
1243 ((and (not wesnoth-auto-check-timer) wesnoth-idle-warning-timer)
1244 (setq wesnoth-auto-check-timer
1245 (run-with-idle-timer wesnoth-idle-warning-timer
1246 wesnoth-idle-warning-timer
1247 'wesnoth-warnings-at-point)))
1248 ((and wesnoth-auto-check-timer wesnoth-idle-warning-timer)
1249 (cancel-timer wesnoth-auto-check-timer)
1250 (setq wesnoth-auto-check-timer
1251 (run-with-idle-timer wesnoth-idle-warning-timer
1252 wesnoth-idle-warning-timer
1253 'wesnoth-warnings-at-point)))))
1255 (defun wesnoth-warnings-at-point ()
1256 "Display any warnings under `point'."
1257 (interactive)
1258 (save-excursion
1259 (forward-char))
1260 (let ((warning (overlay-get (car (overlays-at (point))) 'error)))
1261 (and warning (message "%s" warning))))
1263 (defun wesnoth-check-structure (&optional start end)
1264 "Check the buffer for correct nesting of elements.
1265 If a problem is found in the structure, point will be placed at
1266 the location which an element was expected and the expected
1267 element will be displayed in the mini-buffer.
1269 START and END define the region to be checked. If
1270 function `transient-mark-mode' is enabled, the region specified will be
1271 checked. Otherwise START and END will be the minimum and maximum
1272 positions of the buffer, respectively."
1273 (interactive)
1274 (unless (or start end)
1275 (if (and (boundp 'transient-mark-mode)
1276 transient-mark-mode mark-active)
1277 (setq start (region-beginning)
1278 end (copy-marker (region-end)))
1279 (setq start (point-min)
1280 end (point-max))))
1281 (let ((unmatched '())
1282 (position nil))
1283 (save-excursion
1284 (and start (goto-char start))
1285 (while (and (search-forward-regexp
1286 (concat "^\\([\t ]*\\[\\(/?\\(\\w\\|_\\)+\\)\\]\\|"
1287 wesnoth-preprocessor-regexp "\\)") end t)
1288 (not position))
1289 (beginning-of-line)
1290 (if (or (looking-at "^[\t ]*\\[\\(\\(\\w\\|_\\)+\\)\\]")
1291 (looking-at "[\t ]*#\\(define \\|ifdef \\|ifndef \\)"))
1292 (setq unmatched (cons (match-string-no-properties 1)
1293 unmatched))
1294 (cond
1295 ((wesnoth-element-requires "#else" "ifn?def "))
1296 ((wesnoth-element-requires "#endif" "ifn?def " t))
1297 ((wesnoth-element-requires "#enddef" "define " t))
1298 ((looking-at (concat "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|"
1299 wesnoth-preprocessor-closing-regexp))
1300 (if (string= (match-string-no-properties 1)
1301 (car unmatched))
1302 (setq unmatched (cdr unmatched))
1303 (setq position (point))))))
1304 (end-of-line)))
1305 (wesnoth-structure-result position (car unmatched))))
1307 ;;; wesnoth-mode
1308 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
1309 "Major mode for editing WML."
1310 (kill-all-local-variables)
1311 (use-local-map wesnoth-mode-map)
1312 (setq major-mode 'wesnoth-mode)
1313 (setq mode-name "WML")
1314 (set-syntax-table wesnoth-syntax-table)
1315 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
1316 (set (make-local-variable 'comment-start) "#")
1317 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
1318 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
1319 (set (make-local-variable 'font-lock-defaults)
1320 '(wesnoth-font-lock-keywords
1321 nil t nil nil
1322 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
1323 (setq indent-tabs-mode nil)
1324 (easy-menu-add wesnoth-menu wesnoth-mode-map)
1325 (wesnoth-create-wml-hash-table)
1326 (wesnoth-update-project-information)
1327 (wesnoth-idle-warning-check)
1328 (run-hooks 'wesnoth-mode-hook))
1330 (provide 'wesnoth-mode)
1332 ;;; wesnoth-mode.el ends here