Add 2010 to copyright period
[wesnoth-mode.git] / wesnoth-mode.el
blob187a9ad63789768eb670c867911ba4edddbed2a4
1 ;;; wesnoth-mode.el --- A major mode for editing WML.
2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 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 of
23 ;; Wesnoth Markup Language (WML) files. Features of wesnoth-mode include
24 ;; syntax highlighting support, automatic indentation, context-sensitive
25 ;; 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.5+git
37 ;; * Add support for ido completion (see `wesnoth-use-alternate-completion')
38 ;; * Add support for WML validation on a directory.
39 ;; * Add support for more accurate WML validation. (See
40 ;; `wesnoth-allow-accurate-validation'.)
41 ;; * Improve and add support for prefix arg to `wesnoth-kill-block' to kill
42 ;; multiple blocks.
43 ;; * Add support for attributes where whitespace is between the key and `='.
44 ;; 1.3.5
45 ;; * Added navigation commands: C-M-n and C-M-p can be used to move to the
46 ;; next and previous children of the current parent element, respectively.
47 ;; C-M-u and C-M-d can be used to move to the parent element and the next
48 ;; child element, respectively.
49 ;; * Added commands for structured editing: C-M-k kills the block at point.
50 ;; C-M-SPC sets the mark to the position of the end of the block at point.
51 ;; * Fixed a bug where the parent tag would be unknown if a macro was defined
52 ;; between point and the position of the parent element.
53 ;; * Fixed a bug where an incorrectly formatted closing tag could be inserted
54 ;; in some circumstances.
55 ;; 1.3.4a
56 ;; * #ifdef, #ifndef preprocessor statements now indent their contents
57 ;; relative to themselves when `wesnoth-indent-preprocessor-bol' is nil.
58 ;; * Fixed a bug which could prevent element completion immediately within a
59 ;; preprocessor statement.
60 ;; 1.3.4
61 ;; * Fixed some errors produced when wesnoth-mode.el is byte-compiled.
62 ;; * Improve detection and position of inserted closing tags in some
63 ;; circustances.
64 ;; * Improved context detection for completion in some circumstances.
65 ;; * Read `wesnoth-addition-file' as needed; M-x wesnoth-update no longer
66 ;; required.
67 ;; * `wesnoth-indent-preprocessor-bol' has been re-introduced to control
68 ;; whether preprocessor statements are indented to the beginning of the line
69 ;; or as tags.
70 ;; * Many minor bug fixes.
71 ;; 1.3.3
72 ;; * Improve performance when inserting missing elements. Support for
73 ;; searching for missing elements over a region has been removed;
74 ;; `narrow-to-region' can be used to provide the functionality when
75 ;; required.
76 ;; * All warnings found when checking WML are now underlined in the buffer.
77 ;; * Next and previous warning can be jumped to using C-c C-f (or C-x `) and
78 ;; C-c C-b, respectively.
79 ;; * Any macro arguments are now prompted for and inserted when performing
80 ;; completing, as suggested by fabi.
81 ;; * Improved handling of completion; no longer prompt when no completion
82 ;; found, as suggested by uzytkownik.
83 ;; * Added indentation for FOREACH, as suggested by fabi.
84 ;; * Several bugs and inconsistencies corrected.
85 ;; 1.3.2
86 ;; * Major performance improvements to indentation and WML checking.
87 ;; * Fixed a bug where nesting could break when inserting multiple elements
88 ;; near the last element in a buffer.
89 ;; * Fixed a bug where attributes immediately within #ifn?def were always
90 ;; reported to be illegal.
91 ;; * Fixed a bug where tags immediately within #ifn?def were always legal.
92 ;; * Fixed a bug where when inserting missing tags, scanning would only be
93 ;; performed up to point.
94 ;; * Fixed a bug when jumping between preprocessor statements.
95 ;; 1.3.1
96 ;; * Completion history available is now specific to wesnoth-mode.
97 ;; * Added binding to explicitly update macro information from the current
98 ;; buffer (C-c C-u).
99 ;; * Significantly improved performance of completion and WML checking.
100 ;; * Improved performance for inserting missing tags.
101 ;; * Fixed a bug where #ifdef was never matched when checking WML.
102 ;; * Added completion for preprocessor statements.
103 ;; * Improved macro completion and checking.
104 ;; 1.3.0
105 ;; * Added support for Xemacs.
106 ;; * WML checking is now context sensitive; checks attributes and macros.
107 ;; * WML checks are now always performed on the entire buffer, with results
108 ;; displayed in a temporary buffer.
109 ;; * Context-sensitive completion for attributes and tags.
110 ;; * Completion for built-in and project-specific macros.
111 ;; * Changed the following bindings:
112 ;; `wesnoth-insert-tag' - C-c e -> C-c t
113 ;; `wesnoth-jump-to-matching' - C-c m -> C-c o
114 ;; `wesnoth-check-structure' -> `wesnoth-check-wml' - C-c c
115 ;; * Added the following bindings:
116 ;; `wesnoth-complete-attribute' - C-c a
117 ;; `wesnoth-complete-macro' - C-c m
118 ;; `wesnoth-complete-tag' - C-c t
119 ;; * Removed the following bindings:
120 ;; `wesnoth-check-tag-names' - C-c n
121 ;; * Removed `wesnoth-check-tag-names'. Replaced by `wesnoth-check-wml'.
122 ;; * Completion for an incomplete attribute, tag, or macro at point is
123 ;; attempted via TAB.
124 ;; 1.2.5
125 ;; * Fixed support for GNU Emacs 21.
126 ;; * Added several new tags to `wesnoth-tags-list'.
127 ;; * Added M-TAB binding for `wesnoth-insert-tag'.
128 ;; * `wesnoth-insert-tag' now takes an optional numeric argument indicating
129 ;; how many blocks to wrap across instead of a region.
130 ;; * Support for `wesnoth-indent-preprocessor-bol' removed.
131 ;; * Fixed a bug in `wesnoth-insert-tag' and `wesnoth-insert-missing-closing'
132 ;; causing tags not to be inserted in the correct position.
133 ;; * Fixed highlighting of array indexes as tags.
134 ;; 1.2.4
135 ;; * Improved syntax-highlighting for macro calls.
136 ;; * Underscore is now treated as whitespace.
137 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
138 ;; * Point is now placed at the first non-whitespace character of the line,
139 ;; instead of the last.
140 ;; * Corrected minor indentation bugs.
141 ;; * Indenting across large regions is now much more efficient.
142 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
143 ;; applied.
144 ;; 1.2.3
145 ;; * Now compatible with GNU Emacs 21.4.
146 ;; * Added support for several new tags.
147 ;; * Added menu entry for wesnoth-mode.
148 ;; * Significant speed increase to indentation.
149 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
150 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
151 ;; has been removed.
152 ;; * Trailing whitespace is no longer created when creating a second
153 ;; consecutive newline.
154 ;; * Spurious newlines are no longer created when inserting a tag elements
155 ;; around a region.
156 ;; 1.2.2
157 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
158 ;; `wesnoth-element', `wesnoth-element-opening',
159 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
160 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
161 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
162 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
163 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
164 ;; * Fixed a bug in indentation where content was needed between elements
165 ;; pairs for indentation to work.
166 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
167 ;; `wesnoth-auto-indent-flag'.
168 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
169 ;; * Added indentation styles: `wesnoth-indent-default',
170 ;; `wesnoth-indent-withtags' which implement a a similar indentation
171 ;; style to the existing styles, however all preprocessor statements are
172 ;; indented to the first column.
173 ;; * Added support for several new tags.
174 ;; * Modified `wesnoth-newline' to behave more consistently.
175 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
176 ;; now leaves point at the beginning (when moving backward) or end (when
177 ;; moving forward) of the match.
178 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
179 ;; will now work on preprocessor statements. Will now warn if jump
180 ;; destination may not be correct (due to errors in WML structure).
181 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
182 ;; * `wesnoth-check-structure' can now be applied over an active region and
183 ;; now checks preprocessor statements for correct nesting.
184 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
185 ;; perform indentation by providing a prefix argument.
186 ;; * Indentation styles now leave point at the first non-whitespace character
187 ;; of the line.
188 ;; * `wesnoth-check-tag-names' now reports on success.
189 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
190 ;; * `outline-minor-mode' now works on macro definitions.
191 ;; 1.2.1
192 ;; * Base indent now defaults to 4.
193 ;; * Added support for #ifndef.
195 ;;; Code:
196 (require 'easymenu)
197 (require 'wesnoth-update)
198 (require 'wesnoth-wml-data)
200 (defconst wesnoth-mode-version "1.3.5+git"
201 "The current version of `wesnoth-mode'.")
203 (defgroup wesnoth-mode nil "Wesnoth-mode access"
204 :group 'languages
205 :prefix "wesnoth-")
207 (defcustom wesnoth-auto-indent-flag t
208 "Non-nil means indent the current line upon creating a newline."
209 :type 'boolean
210 :group 'wesnoth-mode)
212 (defcustom wesnoth-indent-preprocessor-bol t
213 "Whether to indent Preprocessor statements to the beginning of the line."
214 :type 'boolean
215 :group 'wesnoth-mode)
217 (defcustom wesnoth-use-alternate-completion 'guess
218 "Use the specified completion method if it is available.
219 Possible values are `guess', `ido' and nil. `guess'
220 checks for packages which provide completion functions, and uses
221 when available; `ido' will try to use `ido-completing-read'; and
222 nil will use the built-in `completing-read' function.
223 Currently ido is the only alternate completion method available.
224 Patches for other completion functions are welcome."
225 :type '(choice (const :tag "ido completion" guess)
226 (const :tag "ido completion" ido)
227 (const :tag "standard completion" nil))
228 :group 'wesnoth-mode)
230 (defcustom wesnoth-indent-savefile t
231 "Non-nil means to use the current indentation conventions.
232 If nil, use the old convention for indentation.
233 The current convention is all attributes are indented a level deeper
234 than their parent; in the past attributes were indented to the same
235 level as their parent."
236 :type 'boolean
237 :group 'wesnoth-mode)
239 (defcustom wesnoth-base-indent 4
240 "The number of columns to indent WML."
241 :type 'integer
242 :group 'wesnoth-mode)
244 (defcustom wesnoth-allow-accurate-validation nil
245 "Whether to allow a more accurate method of WML validation.
246 If non-nil, allow a more accurate method (which is able to better
247 determine the current element's parent) to be used. Note that
248 this may cause WML validation to slow significantly when checking
249 error-prone or messy WML. If WML checked is rarely incorrect it
250 is recommended to set this to non-nil."
251 :type 'boolean
252 :group 'wesnoth-mode)
254 (defconst wesnoth-preprocessor-regexp
255 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\
256 \\|\\(ifn?\\|un\\)def \\)"
257 "Regular expression to match all preprocessor statements.")
259 (defconst wesnoth-preprocessor-opening-regexp
260 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
261 "Regular expression to match \"opening\" preprocessor statements.")
263 (defconst wesnoth-preprocessor-closing-regexp
264 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
265 "Regular expression to match \"closing\" preprocessor statements.")
267 (defvar wesnoth-define-blocks '()
268 "Cache of all toplevel #define and #enddef pairs.")
270 (defvar wesnoth-history-list '()
271 "History of inserted WML elements.")
273 (defvar wesnoth-warning-markers '()
274 "Markers for warnings in the buffer.")
276 (defvar wesnoth-found-cfgs '()
277 "Temporary list of all .cfg files found.")
279 (defvar wesnoth-mode-hook nil)
281 (defface wesnoth-warning-face
282 '((t (:underline "tomato1")))
283 "Face to use for warnings in wesnoth-mode"
284 :group 'wesnoth-mode)
286 (defvar wesnoth-mode-map
287 (let ((map (make-sparse-keymap)))
288 (define-key map (kbd "C-M-a") 'wesnoth-backward-element)
289 (define-key map (kbd "C-M-e") 'wesnoth-forward-element)
290 (define-key map (kbd "C-M-n") 'wesnoth-forward-list)
291 (define-key map (kbd "C-M-p") 'wesnoth-backward-list)
292 (define-key map (kbd "C-M-d") 'wesnoth-down-list)
293 (define-key map (kbd "C-M-u") 'wesnoth-backward-up-list)
294 (define-key map (kbd "C-M-k") 'wesnoth-kill-block)
295 (define-key map (kbd "C-M-SPC") 'wesnoth-mark-block)
296 (define-key map (kbd "C-m") 'wesnoth-newline)
297 (define-key map (kbd "C-j") 'wesnoth-newline-and-indent)
298 (define-key map (kbd "C-c C-c") 'wesnoth-check-wml)
299 (define-key map (kbd "C-c C-a") 'wesnoth-complete-attribute)
300 (define-key map (kbd "C-c C-t") 'wesnoth-complete-tag)
301 (define-key map (kbd "C-c C-p") 'wesnoth-complete-preprocessor)
302 (define-key map (kbd "C-c C-u") 'wesnoth-update-project-information)
303 (define-key map (kbd "M-TAB") 'wesnoth-complete-tag)
304 (define-key map (kbd "C-c C-m") 'wesnoth-complete-macro)
305 (define-key map (kbd "C-c C-o") 'wesnoth-jump-to-matching)
306 (define-key map (kbd "C-c C-f") 'wesnoth-forward-warning)
307 (define-key map (kbd "C-x `") 'wesnoth-forward-warning)
308 (define-key map (kbd "C-c C-b") 'wesnoth-backward-warning)
309 (define-key map (kbd "C-c C-/") 'wesnoth-insert-missing-closing)
310 (define-key map (kbd "TAB") 'wesnoth-indent-or-complete)
311 map)
312 "Keymap used in `wesnoth-mode'.")
314 (easy-menu-define wesnoth-menu wesnoth-mode-map "Menu for wesnoth-mode"
315 '("WML"
316 ["Check WML" wesnoth-check-wml t]
317 ["Indent or Complete" wesnoth-indent-or-complete t]
318 ["Indent buffer" (lambda ()
319 (interactive)
320 (wesnoth-indent-region (point-min) (point-max))) t]
321 ["Insert Tag" wesnoth-complete-tag t]
322 ["Insert Attribute" wesnoth-complete-attribute t]
323 ["Insert Macro" wesnoth-complete-macro t]
324 ["Insert Preprocessor" wesnoth-complete-preprocessor t]
325 ["Insert Missing Tag" wesnoth-insert-missing-closing t]
326 ["Jump to Matching" wesnoth-jump-to-matching t]
327 ["Update Macros" wesnoth-update-project-information t]))
329 (defvar wesnoth-syntax-table
330 (let ((wesnoth-syntax-table (make-syntax-table)))
331 (modify-syntax-entry ?\# "<" wesnoth-syntax-table)
332 (modify-syntax-entry ?\" "\"" wesnoth-syntax-table)
333 (modify-syntax-entry ?\= "." wesnoth-syntax-table)
334 (modify-syntax-entry ?\| "w" wesnoth-syntax-table)
335 (modify-syntax-entry ?\_ "_" wesnoth-syntax-table)
336 (modify-syntax-entry ?\[ "(" wesnoth-syntax-table)
337 (modify-syntax-entry ?\] ")" wesnoth-syntax-table)
338 (modify-syntax-entry ?\/ "." wesnoth-syntax-table)
339 (modify-syntax-entry ?\- "." wesnoth-syntax-table)
340 (modify-syntax-entry ?\. "_" wesnoth-syntax-table)
341 (modify-syntax-entry ?\n ">" wesnoth-syntax-table)
342 (modify-syntax-entry ?\r ">" wesnoth-syntax-table)
343 wesnoth-syntax-table)
344 "Syntax table for `wesnoth-mode'.")
346 ;; Prevent automatic font-locking of elements which might be pre-processor
347 ;; statements.
348 (defvar wesnoth-syntactic-keywords
349 '(("#\\(?:define\\|if\\(?:n?def\\)\\)" 0 "(")
350 ("#end\\(?:\\(?:de\\|i\\)f\\)" 0 ")")
351 ("#\\(?:else\\|undef\\)" 0 "w"))
352 "Syntactic keywords for preprocessor statements within `wesnoth-mode'.")
354 (defvar wesnoth-font-lock-keywords
355 (list
356 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face)
357 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
358 font-lock-keyword-face)
359 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
360 2 font-lock-function-name-face)
361 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face))
362 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face))
363 '("}" . font-lock-function-name-face)
364 '("[\t ]*\\(\\[/?[^$]\\(\\w\\|_\\)+\\]\\)" 1 font-lock-type-face)
365 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face)
366 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)[\t ]*="
367 1 font-lock-variable-name-face))
368 "Syntax highlighting for `wesnoth-mode'.")
370 (defun wesnoth-element-closing (&optional limited)
371 "Return the regexp to match a closing element.
372 If LIMITED is non-nil, return a regexp which matches only the
373 #enddef preprocessor."
374 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\]\\|"
375 (if limited
376 "#enddef"
377 "#end\\(?:def\\|if\\)")
378 (if (and (not wesnoth-indent-preprocessor-bol) limited)
379 "\\|#endif"
381 "\\)"))
383 (defun wesnoth-element-opening (&optional limited)
384 "Return the regexp to match an opening element.
385 If LIMITED is non-nil, return a regexp which matches only the
386 #define preprocessor."
387 (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define "
388 (if limited
389 "\\|{FOREACH .+}"
390 "\\|#ifn?def ")
391 (if (and (not wesnoth-indent-preprocessor-bol) limited)
392 "\\|#ifn?def \\|#else"
394 "\\)"))
396 (defun wesnoth-element (&optional limited)
397 "Return the regexp to match any element.
398 If LIMITED is non-nil, return a regexp which matches only the
399 #define and #enddef preprocessors."
400 (concat "^[\t ]*\\(\\[[+/]?\\(\\w\\|_\\)+\\]?\\|"
401 (if limited
402 "#define \\|#enddef"
403 (substring wesnoth-preprocessor-regexp 5))
404 (if (and (not wesnoth-indent-preprocessor-bol) limited)
405 "\\|#\\(ifn?def \\|endif\\|else\\)"
407 "\\)"))
409 (defun wesnoth-find-next (type)
410 "Find the next element of TYPE.
411 TYPE is a symbol representing an element type, or a list of
412 element types to find."
413 (let ((element (wesnoth-next-element)))
414 (while (and element (if (listp type)
415 (not (member (car element) type))
416 (not (eq (car element) type))))
417 (setq element (wesnoth-next-element)))
418 (when (if (listp type)
419 (member (car element) type)
420 (eq (car element) type))
421 element)))
423 (defun wesnoth-next-element ()
424 "Move to the next element in the buffer.
425 Return non-nil when an element is found. Otherwise, return nil."
426 (interactive)
427 (save-match-data
428 (and (or (eolp) (looking-at "[}\t ]"))
429 (search-forward-regexp "[^}
430 \t ]" (point-max) t)
431 (forward-char -1)))
432 (let ((details (wesnoth-element-type (point))))
433 (save-match-data
434 (when (nth 2 details)
435 (goto-char (nth 2 details))
436 (while (nth 3 (parse-partial-sexp
437 (save-excursion (search-backward-regexp
438 (wesnoth-element t) (point-min) t)
439 (point))
440 (point)))
441 (search-forward "\"" (point-max) t))))
442 details))
444 (defun wesnoth-find-previous (type)
445 "Find the previous element of TYPE.
446 TYPE is a symbol representing an element type, or a list of
447 element types to find."
448 (let ((element (wesnoth-previous-element)))
449 (while (and element (if (listp type)
450 (not (member (car element) type))
451 (not (eq (car element) type))))
452 (setq element (wesnoth-previous-element)))
453 (when (if (listp type)
454 (member (car element) type)
455 (eq (car element) type))
456 element)))
458 ;; TODO: This is currently quite inefficient, and therefore is only used where
459 ;; necessary. (i.e., when the parent element is initially may not be
460 ;; correctly detected.)
461 (defun wesnoth-previous-element ()
462 "Move to the previous element in the buffer.
463 Return non-nil when an element is found. Otherwise, return nil."
464 (interactive)
465 (if (= (point) (wesnoth-wml-start-pos))
467 (let ((init-element (save-excursion (wesnoth-next-element)))
468 last-element cur-element)
469 (save-match-data
470 (search-backward-regexp "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|#ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)" (point-min) t))
471 (while (< (cadr (setq cur-element (wesnoth-next-element)))
472 (cadr init-element))
473 (when (car cur-element)
474 (setq last-element cur-element)))
475 (goto-char (or (cadr last-element) (cadr init-element) (point-min))))
476 ;; Determine whether currently within a string, and move to its beginning
477 ;; where necessary.
478 (save-excursion
479 (let ((details (wesnoth-element-type (point))))
480 (save-match-data
481 (when (cadr details)
482 (goto-char (cadr details))
483 (while (nth 3 (parse-partial-sexp
484 (save-excursion (search-backward-regexp
485 (wesnoth-element t) (point-min) t)
486 (point))
487 (point)))
488 (search-backward "\"" (point-min) t))))
489 details))))
491 (defun wesnoth-element-type (point)
492 "Return details regarding the element at POINT.
493 A list is returned, the elements of the list represent the
494 following, respectively: A symbol representing the type of
495 element; the position of the start of the element and the
496 position of the end of the element. POINT must be at the start
497 of the element."
498 (let ((element-matches
499 '(("{\\(.*?[/\]\\)+}" . nil) ;; pathnames
500 ("{\\(\\w\\|_\\)+" . macro)
501 ("\\[\\+?[^/]+?\\]" . tag-opening)
502 ("\\[/.+?\\]" . tag-closing)
503 ("\\(\\w\\|_\\)+[\t ]*=" . attribute)
504 ("#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\
505 \\(ifn?\\|un\\)def \\)"
506 . preprocessor)
507 ("#.*$" . comment)
508 ("[^\t ]+") . nil)))
509 (catch 'result
510 (dolist (pair element-matches)
511 (when (looking-at (car pair))
512 (throw 'result (list (cdr pair)
513 (match-beginning 0)
514 (min (save-excursion (forward-line 1) (point))
515 (match-end 0)))))))))
517 (defun wesnoth-estimate-element-type (point)
518 "Return match data for a partial element at POINT."
519 (save-excursion
520 (goto-char point)
521 (let ((element-matches
522 '(("{\\(.*?[/\]\\)+$" . nil) ; pathnames
523 ("{\\(\\w\\|_\\)*$" . macro)
524 ("\\[/\\(\\w\\|_\\)*$" . tag-closing)
525 ("\\[\\+?\\(\\w\\|_\\)*$" . tag-opening)
526 ("^[\t ]*\\(\\w\\|_\\)+$" . attribute)
527 ("[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\
528 \\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
529 . nil) ; not a partial match
530 ("[\t ]*#\\w*$" . preprocessor))))
531 (catch 'result
532 (dolist (pair element-matches)
533 (when (looking-at (car pair))
534 (throw 'result (list (cdr pair)
535 (match-beginning 0)
536 (match-end 0)))))))))
538 (defun wesnoth-guess-element-type (point)
539 "Return details for the the element near POINT.
540 Locate the start of the element before determining details.
541 BOUND is the limit to search backwards."
542 (let ((details (wesnoth-estimate-element-type point))
543 (bound (save-excursion
544 (goto-char point)
545 (beginning-of-line)
546 (point))))
547 (while (and (not (car details)) (> point bound))
548 (setq point (1- point)
549 details (wesnoth-estimate-element-type point)))
550 (and (nth 1 details)
551 (>= (point) (nth 1 details))
552 (nth 2 details)
553 (<= (point) (nth 2 details))
554 details)))
557 ;;; Insertion and completion
558 (defmacro wesnoth-completion-method (prompt completions)
559 "Determine a suitable completion method for use.
560 See `wesnoth-use-alternate-completion' for more information."
561 (cond
562 ((and (featurep 'ido)
563 (or (eq wesnoth-use-alternate-completion 'ido)
564 (eq wesnoth-use-alternate-completion 'guess)))
565 `(ido-completing-read ,prompt ,completions
566 nil nil element
567 'wesnoth-history-list))
569 `(completing-read ,prompt ,completions
570 nil nil element
571 'wesnoth-history-list))))
573 (defmacro wesnoth-element-completion (completions prompt partial
574 &optional completep)
575 "Process completion of COMPLETIONS, displaying PROMPT.
576 PARTIAL is the partial string on which to attempt completion.
577 If COMPLETEP is non-nil, do not prompt if no completion is found."
578 `(let* ((element (when ,partial (try-completion ,partial ,completions))))
579 (cond ((eq element t)
580 ,partial)
581 ((and ,completep (null element))
582 nil)
583 ((and element (eq (try-completion element ,completions) t))
584 element)
585 ((> (length (all-completions (or element "") ,completions)) 1)
586 (wesnoth-completion-method ,prompt ,completions))
587 ((= (length ,completions) 1)
588 (car ,completions))
590 element))))
592 (defun wesnoth-active-parent-tag ()
593 "Return the name of the active parent tag.
594 Finds the relevant parent tag, ignoring any conditional tags."
595 (save-excursion
596 (let ((parent (wesnoth-parent-tag)))
597 (while (and (stringp (car parent))
598 (string-match "else\\|then"
599 (car parent)))
600 (goto-char (cdr parent))
601 (setq parent (wesnoth-parent-tag))
602 (when (string= (car parent) "if")
603 (goto-char (cdr parent))
604 (setq parent (wesnoth-parent-tag))))
605 (car parent))))
607 (defun wesnoth-parent-tag (&optional exact)
608 "Return the name of the parent tag.
609 If the parent is a preprocessor statement, return non-nil.
610 If the element does not have a parent, return nil.
611 Otherwise, return a string containing the name of the parent tag.
612 If EXACT is non-nil a more accurate, yet slower method is used."
613 (save-excursion
614 (let ((start-point (point))
615 (depth 1))
616 (when (save-excursion (> (point) (progn (back-to-indentation)
617 (point))))
618 (end-of-line))
619 (while (and (> depth 0)
620 (if exact
621 (wesnoth-find-previous
622 '(tag-opening tag-closing preprocessor))
623 (search-backward-regexp (wesnoth-element t) (point-min) t)))
624 (if (string-match "[\t ]*\\[/\\|#enddef" (match-string 0))
625 (setq depth (1+ depth))
626 (setq depth (1- depth))))
627 (beginning-of-line)
628 (if (> depth 0)
629 (cons nil nil)
630 (when (looking-at (wesnoth-element-opening))
631 (let ((parent (match-string-no-properties 1))
632 (position (point)))
633 (if (or (string-match wesnoth-preprocessor-opening-regexp parent)
634 ;; Check if we're immediately within a macro
635 (and (goto-char start-point)
636 (search-backward-regexp "[}{]" (point-min) t)
637 (string= (match-string 0) "{")
638 (goto-char start-point)
639 (not (and (search-backward parent (point-min) t)
640 (search-backward-regexp "[}{]" (point-min)
642 (string= (match-string 0) "{")))))
643 (cons t position)
644 (cons (substring parent (if (string-match "[\\+.+]" parent)
647 (1- (length parent))) position))))))))
649 (defun wesnoth-partial-macro-p ()
650 "Return non-nil if point is in a partial macro."
651 (save-excursion
652 (let ((opened 0))
653 (search-backward-regexp "{" (point-min) t)
654 (while (search-forward-regexp "[{}]" (point-max) t)
655 (if (string= (match-string 0) "{")
656 (setq opened (1+ opened))
657 (setq opened (1- opened))))
658 (> opened 0))))
660 (defun wesnoth-indent-or-complete (&optional elements)
661 "Indent or complete the line at point, depending on context.
662 ELEMENTS is the number of elements to wrap around if inserting
663 matching tags."
664 (interactive "P")
665 (or elements (setq elements 0))
666 (let ((details (wesnoth-guess-element-type (point))))
667 (cond
668 ((eq (car details) 'tag-opening)
669 (wesnoth-complete-tag elements t))
670 ((and (eq (car details) 'macro)
671 (wesnoth-partial-macro-p))
672 (wesnoth-complete-macro t))
673 ((eq (car details) 'preprocessor)
674 (wesnoth-complete-preprocessor elements t))
675 ((eq (car details) 'tag-closing)
676 ;; FIXME: Solve incorrect behaviour when partial closing is "[/"
677 (insert "a")
678 (and (wesnoth-insert-missing-closing t)
679 (delete-region (nth 1 details)
680 (save-excursion (beginning-of-line) (point))))
681 (end-of-line)
682 (wesnoth-indent))
683 ((eq (car details) 'attribute)
684 (wesnoth-complete-attribute t))
686 (wesnoth-indent)))))
688 (defun wesnoth-preprocessor-closed-p (preprocessor)
689 "Determine whether PREPROCESSOR has been closed.
690 PREPROCESSOR is a string matching the preprocessor statement to
691 be inserted."
692 (save-excursion
693 (back-to-indentation)
694 (wesnoth-jump-to-matching preprocessor)
695 (looking-at
696 (if (string= preprocessor "#define ")
697 "#enddef"
698 "#endif"))))
700 (defun wesnoth-complete-preprocessor (&optional elements completep)
701 "Complete and insert the preprocessor at point.
702 ELEMENTS is the number of elements to wrap around.
703 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
704 (interactive "P")
705 (or elements (setq elements 0))
706 (let* ((completions (wesnoth-emacs-completion-formats
707 '("define" "else" "ifdef" "ifndef"
708 "enddef" "endif" "undef")))
709 (partial (when completep
710 (save-excursion
711 (back-to-indentation)
712 (when (looking-at "#\\(\\w*\\)$")
713 (match-string-no-properties 1)))))
714 (preprocessor (wesnoth-element-completion
715 completions "Preprocessor: " partial completep))
716 (details (wesnoth-guess-element-type (point)))
717 (closedp
718 (save-excursion
719 (when preprocessor
720 (unless (string= "#" (substring preprocessor 0 1))
721 (setq preprocessor (concat "#" preprocessor)))
722 (when (string-match "#\\(define\\|ifn?def\\|undef\\)"
723 preprocessor)
724 (setq preprocessor (concat preprocessor " ")))
725 (when partial
726 (delete-region (nth 1 details) (nth 2 details)))
727 (wesnoth-preprocessor-closed-p preprocessor)))))
728 (when preprocessor
729 (when partial
730 (delete-region
731 (save-excursion
732 (progn (search-backward
733 "#" (save-excursion (back-to-indentation)
734 (point))
736 (point)))
737 (point)))
738 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor)
739 (not closedp))
740 (progn
741 (wesnoth-insert-tag elements preprocessor)
742 (forward-line -1)
743 (end-of-line))
744 (wesnoth-insert-element-separately preprocessor)))))
746 (defun wesnoth-macro-arguments ()
747 "Find any current macro arguments."
748 (let ((results '())
749 (depth (wesnoth-within-define (point))))
750 (save-excursion
751 (while (> depth 0)
752 (save-match-data
753 (search-backward-regexp
754 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
755 (point-min) t)
756 (when (<= (wesnoth-within-define (point)) depth)
757 (and (> depth 0)
758 (setq results
759 (append (mapcar (lambda (macro)
760 (list macro nil))
761 (split-string
762 (match-string-no-properties 1)))
763 results)))
764 (setq depth (1- depth)))))
765 results)))
767 (defun wesnoth-complete-macro (&optional completep)
768 "Complete and insert the macro at point.
769 If COMPLETEP is non-nil, attempt to complete the macro at point."
770 (interactive)
771 (wesnoth-update-project-information)
772 (let* ((macro-information (wesnoth-merge-macro-data
773 wesnoth-macro-data
774 (wesnoth-macro-additions)
775 wesnoth-local-macro-data
776 (wesnoth-macro-arguments)))
777 (completions (wesnoth-emacs-completion-formats
778 (mapcar 'car macro-information)))
779 (details (wesnoth-guess-element-type (point)))
780 (partial (when
781 (save-excursion
782 (and completep
783 (eq (car details) 'macro)
784 (goto-char (cadr details))
785 (looking-at "{\\(\\(\\w\\|_\\)*\\)")))
786 (match-string-no-properties 1)))
787 (macro (wesnoth-element-completion completions "Macro: " partial
788 completep))
789 (args (cadr (assoc macro macro-information))))
790 (when macro
791 (if partial
792 (progn
793 ;; Delete the region corresponding to the current macro.
794 (delete-region (nth 1 details) (nth 2 details))
795 (insert "{" macro (if args " }" "}"))
796 (save-excursion
797 (wesnoth-indent)))
798 (wesnoth-insert-and-indent "{" macro (if args " }" "}")))
799 (forward-char -1)
800 (when args
801 (let ((input (read-string (concat (car args) ": "))))
802 (insert input (if (and (cdr args)
803 (not (string= input "")))
804 " " ""))
805 (while (and (setq args (cdr args)) (not (string= input "")))
806 (insert (setq input (read-string (concat (car args) ": ")))
807 (if (and (not (string= input ""))
808 (cdr args))
809 " " "")))))
810 (when (null args) (forward-char 1)))))
812 (defun wesnoth-complete-attribute (&optional completep)
813 "Insert the attribute at point.
814 If COMPLETEP is non-nil, attempt to complete the attribute at point."
815 (interactive)
816 (wesnoth-refresh-wml-data)
817 (let* ((details (save-excursion
818 (back-to-indentation)
819 (wesnoth-guess-element-type (point))))
820 (completions (save-excursion (when (nth 1 details)
821 (goto-char (nth 1 details)))
822 (wesnoth-build-completion 1)))
823 (partial (when completep
824 (when (save-excursion
825 (back-to-indentation)
826 (looking-at "\\(\\(\\w\\|_\\)+\\)"))
827 (match-string-no-properties 1))))
828 (attribute (wesnoth-element-completion completions "Attribute: "
829 partial completep)))
830 (when attribute
831 (if partial
832 (progn
833 (delete-region (nth 1 details) (nth 2 details))
834 (insert attribute "="))
835 (wesnoth-insert-element-separately attribute
836 (if (string-match "=" attribute)
838 "=")))
839 (save-excursion
840 (wesnoth-indent)))))
842 (defun wesnoth-complete-tag (&optional elements completep)
843 "Complete and insert the tag at point.
844 ELEMENTS is the number of elements to wrap around.
845 If COMPLETEP is non-nil, attempt to complete tag at point."
846 (interactive "P")
847 (or elements (setq elements 0))
848 (let* ((details (wesnoth-guess-element-type (point)))
849 (completions (save-excursion (and (nth 1 details)
850 (goto-char (nth 1 details)))
851 (wesnoth-build-completion 0)))
852 (partial (save-excursion
853 (when (and completep
854 (eq (car details) 'tag-opening)
855 (goto-char (cadr details))
856 (looking-at "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$"))
857 (match-string-no-properties 1))))
858 (tag (wesnoth-element-completion completions "Tag: " partial
859 completep))
860 (closedp
861 (save-excursion
862 (wesnoth-jump-to-matching (concat "[" tag "]"))
863 (back-to-indentation)
864 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
865 (string= tag (match-string 1))))))
866 (if tag
867 (progn
868 (if completep
869 (progn
870 (delete-region (nth 1 details) (nth 2 details))
871 (if closedp
872 (progn
873 (wesnoth-insert-and-indent "[" tag "]")
874 (end-of-line))
875 (wesnoth-insert-tag elements tag)))
876 (wesnoth-insert-tag elements tag)))
877 (or completep (wesnoth-insert-tag elements)))))
879 (defun wesnoth-build-completion (position)
880 "Create a new list for tag completion if necessary.
881 Rebuilding list is required for versions of GNU Emacs earlier
882 than 22. POSITION is the argument passed to `nth' for
883 `wesnoth-tag-data'."
884 (interactive "P")
885 (let ((parent (wesnoth-active-parent-tag))
886 (tag-data (wesnoth-refresh-wml-data)))
887 (wesnoth-emacs-completion-formats
888 (if (or (stringp parent) (null parent))
889 (nth position (gethash parent wesnoth-tag-hash-table))
890 (mapcar 'car tag-data)))))
892 (defun wesnoth-emacs-completion-formats (candidates)
893 "Return the completions in the correct format for `emacs-major-version'.
894 CANDIDATES is a list of all possible completions."
895 (if (> emacs-major-version 21)
896 candidates
897 (let ((tags '())
898 (iter 0))
899 (dolist (tag candidates)
900 (setq iter (1+ iter))
901 (setq tags (append tags (list (cons tag iter)))))
902 tags)))
904 (defun wesnoth-insert-tag (&optional elements tagname)
905 "Insert the specified opening tag and it's matching closing tag.
906 Both the opening and closing tags will be placed on their own
907 lines with point positioned between them. Completion of tags at
908 the prompt uses `wesnoth-tags-list'.
910 ELEMENTS is specifies the number of following blocks which the
911 tag should wrap around.
913 TAGNAME is the name of the tag to be inserted."
914 (interactive "Ps")
915 (unless tagname
916 (setq tagname (wesnoth-completion-method "Tag: "
917 (wesnoth-build-completion 0))))
918 (when (or (not elements)
919 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
920 wesnoth-preprocessor-regexp "\\)")))
921 (setq elements 0))
922 (let ((start (save-excursion (forward-line -1) (point)))
923 (end (unless (= elements 0)
924 ;; Work around some strange behaviour when the target is at the
925 ;; end of the buffer.
926 (save-excursion
927 (goto-char (point-max))
928 (beginning-of-line)
929 (unless (looking-at "^[\t ]*$")
930 (end-of-line)
931 (newline)))
932 (wesnoth-nth-pair-position elements))))
933 (if (string-match wesnoth-preprocessor-regexp tagname)
934 (wesnoth-insert-element-separately tagname)
935 (wesnoth-insert-element-separately "[" tagname "]"))
936 (save-excursion
937 (if end
938 (goto-char (marker-position end))
939 (newline (if (string-match wesnoth-preprocessor-regexp tagname) 1 2)))
940 (if (string-match wesnoth-preprocessor-opening-regexp tagname)
941 (wesnoth-insert-element-separately
942 (if (string= tagname "#define ")
943 "#enddef"
944 "#endif"))
945 (wesnoth-insert-element-separately "[/" (if (string-match "^+"
946 tagname)
947 (substring tagname 1)
948 tagname)
949 "]"))
950 (indent-region start (point) nil))
951 (unless end
952 (forward-line 1)))
953 (wesnoth-indent))
955 (defun wesnoth-insert-element-separately (&rest strings)
956 "Concatenate STRINGS and insert them on a line of their own."
957 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
958 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
959 (progn
960 (forward-line 1)
961 (end-of-line))
962 (end-of-line)
963 (newline))
964 (beginning-of-line)
965 (if (looking-at "^[\t ]*$")
966 (end-of-line)
967 (open-line 1)))
968 (insert (apply 'concat strings)))
970 (defun wesnoth-insert-missing-closing (&optional completep)
971 "Insert the next expected closing element at point.
972 If COMPLETEP is non-nil, do not move forward a line when scanning
973 for the matching tag."
974 (interactive)
975 (let ((match nil)
976 (skip t))
977 (save-excursion
978 (when (and (null completep)
979 (<= (point) (save-excursion (back-to-indentation) (point))))
980 (if (save-excursion (beginning-of-line)
981 (looking-at (wesnoth-element-opening)))
982 (forward-line -1)
983 (when
984 (save-excursion (beginning-of-line)
985 (looking-at (wesnoth-element-closing)))
986 (setq skip nil))))
987 (when (wesnoth-search-for-matching-tag
988 'search-backward-regexp (wesnoth-element-opening) 'point-min
989 (and skip (if completep nil 1)))
990 (setq match (and (looking-at (wesnoth-element-opening))
991 (match-string-no-properties 1)))))
992 (when match
993 (if (string= (substring match 0 1) "[")
994 (wesnoth-insert-element-separately
995 "[/"
996 (let ((tagname (substring match 1 (1- (length match)))))
997 (if (string-match "^+" tagname)
998 (substring tagname 1)
999 tagname))
1000 "]")
1001 (wesnoth-insert-element-separately
1002 (cdr (assoc match '(("#define " . "#enddef")
1003 ("#ifndef " . "#endif")
1004 ("#ifdef " . "#endif")))))))
1005 (wesnoth-indent)
1006 (end-of-line)
1007 match))
1009 (defun wesnoth-insert-and-indent (&rest args)
1010 "Concatenate and insert the given string(s) before indenting.
1012 ARGS is a list of strings to be inserted."
1013 (insert (apply 'concat args))
1014 (wesnoth-indent))
1016 (defun wesnoth-newline (&optional indent)
1017 "Indent the current line and create a newline.
1018 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1019 performed. Indentation can be forced by setting INDENT to
1020 non-nil."
1021 (interactive "P")
1022 (newline)
1023 (save-excursion
1024 (forward-line -1)
1025 (when (and (or wesnoth-auto-indent-flag indent)
1026 (not (looking-at "^[\t ]*$")))
1027 (wesnoth-indent))))
1029 ;;; Movement
1030 (defun wesnoth-navigate-element (repeat search-function bound)
1031 "Move point to the tag in the given direction REPEAT times.
1033 SEARCH-FUNCTION is the symbol of the function for searching in
1034 the required direction, with BOUND marking the furthest point to
1035 search."
1036 (or repeat (setq repeat 1))
1037 (while (> repeat 0)
1038 (and (eq search-function 'search-forward-regexp) (end-of-line))
1039 (funcall search-function (wesnoth-element-opening) bound t)
1040 (back-to-indentation)
1041 (setq repeat (1- repeat))))
1043 (defun wesnoth-nth-pair-position (count)
1044 "Return `point' after COUNT number of matching element pairs.
1045 COUNT is a positive number representing the number of balanced
1046 pairs to move across.
1047 `point' is returned as a marker object."
1048 (save-excursion
1049 (let ((failed nil))
1050 (if (> (point) (save-excursion (back-to-indentation) (point)))
1051 (end-of-line)
1052 (beginning-of-line))
1053 (while (> count 0)
1054 ;; Currently looking-at target tag. Stop here to avoid
1055 ;; incorrect nesting.
1056 (unless (wesnoth-search-for-matching-tag
1057 'search-forward-regexp (wesnoth-element-closing) 'point-max)
1058 (setq count 0)
1059 (setq failed t))
1060 (and (> (setq count (1- count)) 0) (forward-line 1)))
1061 (if failed
1062 (beginning-of-line)
1063 (end-of-line))
1064 (point-marker))))
1066 (defun wesnoth-forward-element (repeat)
1067 "Move point to the end of the next tag.
1068 REPEAT is an optional numeric argument. If REPEAT is non-nil,
1069 jump forward the specified number of tags."
1070 (interactive "p")
1071 (if (< repeat 0)
1072 (wesnoth-backward-element (abs repeat))
1073 (wesnoth-navigate-element repeat 'search-forward-regexp (point-max))))
1075 (defun wesnoth-backward-element (repeat)
1076 "Move point to the beginning of the previous tag.
1077 REPEAT is an optional numeric argument. If REPEAT is non-nil,
1078 jump backward the specified number of tags."
1079 (interactive "p")
1080 (if (< repeat 0)
1081 (wesnoth-forward-element (abs repeat))
1082 (wesnoth-navigate-element repeat 'search-backward-regexp (point-min))))
1084 (defun wesnoth-search-for-matching-tag (search-function
1085 search-string bound &optional skip)
1086 "Search for the matching tag for the current line.
1087 SEARCH-FUNCTION is the name of the function used to perform the search.
1088 SEARCH-STRING is a string representing the matching tag type.
1089 BOUND is the bound to be passed to the search function.
1090 If SKIP is non-nil, skip the first element and continue from there."
1091 (let ((depth 1))
1092 (when (and (or (and (numberp skip) (forward-line skip))
1093 (funcall search-function (wesnoth-element)
1094 (funcall bound) t))
1095 (or skip (not (string-match search-string (match-string 0)))))
1096 (while (and (> depth 0)
1097 (funcall search-function (wesnoth-element)
1098 (funcall bound) t))
1099 (if (string-match search-string (match-string 0))
1100 (setq depth (1- depth))
1101 (setq depth (1+ depth))))
1102 (= depth 0))))
1104 (defun wesnoth-jump-to-matching (&optional element)
1105 "Jump point to the matching opening/closing tag.
1106 ELEMENT is an element to find a match for."
1107 (interactive)
1108 (beginning-of-line)
1109 (let ((target nil)
1110 (first-element nil))
1111 (save-excursion
1112 (cond
1113 ((or (and (stringp element)
1114 (string-match (wesnoth-element-opening) element))
1115 (looking-at (wesnoth-element-opening)))
1116 (setq first-element (match-string-no-properties 0 element))
1117 (when (wesnoth-search-for-matching-tag
1118 'search-forward-regexp (wesnoth-element-closing) 'point-max
1119 (and (stringp element) 1))
1120 (beginning-of-line)
1121 (if (and (string-match wesnoth-preprocessor-opening-regexp
1122 first-element)
1123 (looking-at (wesnoth-element-closing)))
1124 (when (string= (match-string-no-properties 0)
1125 (cdr (assoc first-element
1126 '(("#define " . "#enddef")
1127 ("#ifndef " . "#endif")
1128 ("#ifdef " . "#endif")))))
1129 (setq target (point)))
1130 (setq target (point)))))
1131 ((or (and (stringp element)
1132 (string-match (wesnoth-element-closing) element))
1133 (looking-at (wesnoth-element-closing)))
1134 (end-of-line)
1135 (setq first-element (match-string-no-properties 0 element))
1136 (when (wesnoth-search-for-matching-tag
1137 'search-backward-regexp (wesnoth-element-opening)
1138 'wesnoth-wml-start-pos (and (stringp element) -1))
1139 (if (and (string-match wesnoth-preprocessor-closing-regexp
1140 first-element)
1141 (looking-at (wesnoth-element-opening)))
1142 (progn
1143 (when (or (and (string= "#enddef" first-element)
1144 (string= "#define "
1145 (match-string-no-properties
1146 0)))
1147 (and (string= "#endif" first-element)
1148 (string-match
1149 "#ifn?def "
1150 (match-string-no-properties
1151 0))))
1152 (setq target (point))))
1153 (setq target (point)))))
1155 (search-backward-regexp (wesnoth-element-opening) (point-min) t)
1156 (setq target (point)))))
1157 (if target
1158 (progn
1159 (goto-char target)
1160 (back-to-indentation))
1161 (when (interactive-p)
1162 (message "%s" "Tag does not appear to be matched")))))
1164 ;;; Indentation
1165 (defun wesnoth-wml-start-pos ()
1166 "Determine the position of `point' relative to where the actual WML begins.
1167 Return the likely starting position of the WML if it is found.
1168 Otherwise return nil."
1169 (save-excursion
1170 (goto-char (point-min))
1171 (when (search-forward-regexp (wesnoth-element) (point-max) t)
1172 (beginning-of-line)
1173 (point))))
1175 (defun wesnoth-first-column-indent-p (point)
1176 "Return non-nil if the current line should not be indented.
1178 POINT is the position in the buffer to check.
1179 CONTEXT represents the type of element which precedes the current element."
1180 (or (not (wesnoth-wml-start-pos))
1181 (<= (point) (wesnoth-wml-start-pos))
1182 (nth 3 (parse-partial-sexp
1183 (save-excursion (search-backward-regexp
1184 (wesnoth-element t) (point-min) t)
1185 (point))
1186 point))
1187 (and (looking-at wesnoth-preprocessor-regexp)
1188 wesnoth-indent-preprocessor-bol)))
1190 (defun wesnoth-indent ()
1191 "Indent the current line as WML."
1192 (save-excursion
1193 (beginning-of-line)
1194 (let* ((cur-indent 0)
1195 (context-data (wesnoth-determine-context (point)))
1196 (context (car context-data))
1197 (ref-indent (cdr context-data)))
1198 (unless (wesnoth-first-column-indent-p (point))
1199 (cond
1200 ((eq context 'opening)
1201 (if (and (looking-at "^[\t ]*#else")
1202 (not wesnoth-indent-preprocessor-bol))
1203 (setq cur-indent ref-indent)
1204 (if (or (and wesnoth-indent-savefile
1205 (or (looking-at "[\t ]*{NEXT ")
1206 (and (not
1207 (looking-at (wesnoth-element-closing t)))
1208 (not (looking-at "[\t ]*{NEXT ")))))
1209 (looking-at (wesnoth-element-opening t))
1210 (looking-at "[\t ]*{FOREACH "))
1211 (setq cur-indent (+ ref-indent wesnoth-base-indent))
1212 (setq cur-indent ref-indent))))
1213 ((eq context 'closing)
1214 (if (and (looking-at "^[\t ]*#else")
1215 (not wesnoth-indent-preprocessor-bol))
1216 (setq cur-indent (- ref-indent wesnoth-base-indent))
1217 (if (or (looking-at
1218 (concat "^[\t ]*\\(\\[/\\|\\#enddef"
1219 (if (not wesnoth-indent-preprocessor-bol)
1220 "\\|#endif"
1222 "\\)"))
1223 (and (not wesnoth-indent-savefile)
1224 (not (looking-at (wesnoth-element-opening t)))
1225 (not (looking-at "[\t ]*{FOREACH "))))
1226 (setq cur-indent (- ref-indent wesnoth-base-indent))
1227 (setq cur-indent ref-indent))))))
1228 (indent-line-to (max cur-indent 0))))
1229 (when (> (save-excursion (back-to-indentation) (point))
1230 (point))
1231 (back-to-indentation)))
1233 (defun wesnoth-within-define (position)
1234 "Determine whether point is currently inside a #define block.
1235 POSITION is the initial cursor position."
1236 (save-match-data
1237 (let ((depth 0)
1238 (defblocks (or wesnoth-define-blocks
1239 (wesnoth-find-macro-definitions))))
1240 (unless (equal (car defblocks) 'none)
1241 (dolist (element defblocks)
1242 (when (= (cadr (sort (append (mapcar 'marker-position
1243 (cadr element))
1244 (list position)) '>)) position)
1245 (setq depth (max (car element) depth)))))
1246 depth)))
1248 (defun wesnoth-find-macro-definitions ()
1249 "Return information regarding positioning of macro definitions."
1250 (save-excursion
1251 (goto-char (point-min))
1252 (let ((depth 0)
1253 openings cache)
1254 (while (search-forward-regexp "#define\\|#enddef" (point-max) t)
1255 (and (string= (match-string 0) "#define") (beginning-of-line))
1256 (setq depth
1257 (if (string= (match-string 0) "#define")
1258 (progn
1259 (add-to-list 'openings (point-marker))
1260 (1+ depth))
1261 (if openings
1262 (progn
1263 (add-to-list 'cache
1264 (list depth (list (car openings)
1265 (point-marker))))
1266 (setq openings (cdr openings))
1267 (1- depth))
1268 depth)))
1269 (end-of-line))
1270 (or cache (list 'none)))))
1272 (defun wesnoth-indent-region (start end)
1273 "Indent the region from START to END.
1274 Creates and destroys a cache of macro definition details as necessary."
1275 (interactive "r")
1276 (unwind-protect
1277 (save-excursion
1278 (goto-char end)
1279 (setq end (point-marker))
1280 (goto-char start)
1281 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1282 (or (bolp) (forward-line 1))
1283 (while (< (point) end)
1284 (if (looking-at "^[\t ]*$")
1285 (indent-line-to 0)
1286 (funcall indent-line-function))
1287 (forward-line 1)))
1288 (setq wesnoth-define-blocks nil)))
1290 (defun wesnoth-determine-context (position)
1291 "Determine the type of the last relevant element.
1292 POSITION is the buffer position of the element for which to
1293 determine the context."
1294 (save-excursion
1295 (let* ((elements (concat (substring (wesnoth-element t)
1296 0 (- (length (wesnoth-element t)) 2))
1297 "\\|{FOREACH .+}\\|{NEXT .+}\\)"))
1298 (match (or
1299 (and (search-backward-regexp
1300 elements (point-min) t)
1301 (progn
1302 (while
1303 (save-match-data
1304 (looking-at "^[\t ]*\\[[^/].+\\]\\[/.+\\]"))
1305 (search-backward-regexp elements
1306 (point-min) t))
1308 (match-string 1))
1309 ""))
1310 (depth (wesnoth-within-define position)))
1311 (while (and (wesnoth-wml-start-pos)
1312 (> (wesnoth-within-define (point)) depth)
1313 (not (= (point) (wesnoth-wml-start-pos))))
1314 (search-backward-regexp elements
1315 (wesnoth-wml-start-pos) t)
1316 (setq match (match-string 1)))
1317 (when (and (wesnoth-wml-start-pos)
1318 (= (point) (wesnoth-wml-start-pos))
1319 (= depth 0)
1320 (string-match "#define" match))
1321 ;; Found nothing of use; reset match and assume top-level tag.
1322 (setq match ""))
1323 (cond
1324 ((string-match (concat "\\[/\\|#enddef"
1325 (if (not wesnoth-indent-preprocessor-bol)
1326 "\\|#endif"
1327 ""))
1328 match)
1329 (cons 'closing (current-indentation)))
1330 ((string-match "{NEXT " match)
1331 (cons 'closing (if wesnoth-indent-savefile
1332 (- (current-indentation) wesnoth-base-indent)
1333 (current-indentation))))
1334 ((string-match (concat "\\[[^/]?\\|#define\\|{FOREACH "
1335 (if (not wesnoth-indent-preprocessor-bol)
1336 "\\|#ifn?def \\|#else"
1337 "")) match)
1338 (cons 'opening (current-indentation)))))))
1340 (defun wesnoth-newline-and-indent (&optional indent)
1341 "Indent both the current line and the newline created.
1342 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1343 performed.
1345 If the optional argument, INDENT is non-nil, force indentation to
1346 be performed."
1347 (interactive)
1348 (wesnoth-newline)
1349 (when (or wesnoth-auto-indent-flag indent)
1350 (wesnoth-indent)))
1352 ;;; WML checks
1353 (defun wesnoth-check-element-type (position &optional exact)
1354 "Determine the context of the element.
1355 POSITION is the position of the element in the list. If EXACT is
1356 non-nil, a more accurate, yet slower method is used. This is
1357 enabled when the attempt to match initially fails."
1358 (let ((parent (save-match-data (car (wesnoth-parent-tag exact))))
1359 (result '()))
1360 (if (or (stringp parent) (null parent))
1361 (setq result (member (match-string-no-properties 1)
1362 (nth position (gethash parent
1363 wesnoth-tag-hash-table))))
1364 (member (match-string-no-properties 1)
1365 (mapc
1366 '(lambda (x)
1367 (let ((value (nth position (cdr x))))
1368 (and value (mapc '(lambda (y)
1369 (setq result (cons y result)))
1370 value))))
1371 (or wesnoth-tmp-tag-data (wesnoth-refresh-wml-data)))))
1372 ;; If unsuccessful and more accurate checking is available, use it.
1373 ;; Otherwise, return the result found.
1374 (if (and wesnoth-allow-accurate-validation (not exact) (not result))
1375 (wesnoth-check-element-type position t)
1376 result)))
1378 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
1379 (defun wesnoth-line-number-at-pos (&optional pos)
1380 "Return (narrowed) buffer line number at position POS.
1381 If POS is nil, use current buffer location.
1382 Counting starts at (point-min), so the value refers
1383 to the contents of the accessible portion of the buffer."
1384 (let ((opoint (or pos (point))) start)
1385 (save-excursion
1386 (goto-char (point-min))
1387 (setq start (point))
1388 (goto-char opoint)
1389 (forward-line 0)
1390 (1+ (count-lines start (point))))))
1392 (defun wesnoth-check-output (buffer format-string &rest args)
1393 "Output the string as passed to `format'.
1394 BUFFER is the buffer to output the result.
1395 FORMAT-STRING is the string as the first argument of `format'.
1396 ARGS is any additional data required by `format' to handle FORMAT-STRING."
1397 (save-excursion
1398 (let ((lnap (wesnoth-line-number-at-pos))
1399 (source-buffer (buffer-name)))
1400 (set-buffer buffer)
1401 (let ((buffer-read-only nil))
1402 (insert (apply 'format (concat "%s: %d: " format-string "\n")
1403 source-buffer lnap args))))))
1405 (defun wesnoth-extract-macro-details (macro-arguments)
1406 "Return a list of all macros in MACRO-ARGUMENTS."
1407 (when macro-arguments
1408 (let ((results '()))
1409 (dolist (macro (split-string macro-arguments "[{}][\t ]*"))
1410 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
1411 macro)
1412 (add-to-list 'results (match-string-no-properties 1 macro))))
1413 results)))
1415 (defmacro wesnoth-check-process (format-string &rest args)
1416 "Output to buffer where requested and position overlays as required.
1417 FORMAT-STRING is the string to pass as the first argument to
1418 `format' for the error. ARGS is a list of arguments required by
1419 FORMAT-STRING."
1420 `(progn
1421 (wesnoth-check-output outbuf ,format-string ,@args)
1422 (wesnoth-place-overlay (match-beginning 0) (match-end 0))))
1424 (defmacro wesnoth-overlay-at-pos-p (position)
1425 "Return non-nil when there is an overlay at POSITION."
1426 `(and (overlays-at (goto-char ,position))
1427 (overlay-get (car (overlays-at (point))) 'wesnoth-error)
1428 (overlay-start (car (overlays-at (point))))))
1430 (defmacro wesnoth-locate-warning (string start end)
1431 "Search for STRING and move to the warning in the given direction.
1432 Searching starts from `point' and will wrap from START if no
1433 match was found. STRING is a form to locate the warning in the
1434 required direction. START is the start of the region searched.
1435 END is the end of the region searched."
1436 `(let ((target nil))
1437 (save-excursion
1438 (cond
1439 ((setq target (wesnoth-overlay-at-pos-p ,string)))
1440 ((setq target (wesnoth-overlay-at-pos-p ,start)))
1441 ((and (not (= (goto-char ,string) ,end))
1442 (setq target (wesnoth-overlay-at-pos-p (point)))))))
1443 target))
1445 (defun wesnoth-forward-warning ()
1446 "Move to the next warning."
1447 (interactive)
1448 (let ((target
1449 (if (fboundp 'next-overlay-change)
1450 (save-excursion
1451 (end-of-line)
1452 (wesnoth-locate-warning (next-overlay-change (point))
1453 (point-min)
1454 (point-max)))
1455 (wesnoth-target-position '< '>))))
1456 (if target
1457 (goto-char target)
1458 (message "%s" "No warnings found"))))
1460 (defun wesnoth-backward-warning ()
1461 "Move to the previous warning."
1462 (interactive)
1463 (let ((target
1464 (if (fboundp 'previous-overlay-change)
1465 (save-excursion
1466 (beginning-of-line)
1467 (wesnoth-locate-warning (1- (previous-overlay-change (point)))
1468 (point-max)
1469 (point-min)))
1470 (wesnoth-target-position '> '< t))))
1471 (if target
1472 (goto-char target)
1473 (message "%s" "No warnings found"))))
1475 (defun wesnoth-target-position (predicate search &optional lastp)
1476 "Return the target marker position.
1477 PREDICATE is the function to use to sort
1478 `wesnoth-warning-markers'. SEARCH must be a function which
1479 returns non-nil when the match is correct. If LASTP is non-nil,
1480 swap the order of the sorted positions when attempting to
1481 fallback."
1482 (let ((positions (sort (mapcar 'marker-position wesnoth-warning-markers)
1483 predicate)))
1484 (or (catch 'pos
1485 (dolist (position positions)
1486 (when (funcall search position (point))
1487 (throw 'pos position))))
1488 (car positions))))
1490 (defun wesnoth-place-overlay (start end)
1491 "Place overlay in the region and apply necessary properties.
1492 START is the start of the region to place the overlay. END is
1493 the end of the region to place the overlay."
1494 (if (fboundp 'overlay-put)
1495 (let ((overlay (make-overlay start end)))
1496 (overlay-put overlay 'wesnoth-error t)
1497 (overlay-put overlay 'face 'wesnoth-warning-face))
1498 (add-to-list 'wesnoth-warning-markers (save-excursion
1499 (goto-char start)
1500 (point-marker)))))
1502 (defun wesnoth-kill-block (arg)
1503 "Kill ARG blocks at point."
1504 (interactive "p")
1505 (save-excursion
1506 (while (> arg 0)
1507 (let ((kill-whole-line t))
1508 (while (and (looking-at "[\t ]*$") (= (forward-line 1) 0)))
1509 (if (looking-at "[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1510 #ifn?def\\)")
1511 (kill-region (point)
1512 (save-excursion
1513 (wesnoth-jump-to-matching)
1514 (forward-line 1)
1515 (point)))
1516 (kill-line))
1517 (setq arg (1- arg)))
1518 (wesnoth-indent))))
1520 (defun wesnoth-mark-block ()
1521 "Mark the block at point."
1522 (interactive)
1523 (save-excursion
1524 (while (and (save-excursion (beginning-of-line)
1525 (looking-at "[\t ]*$"))
1526 (= (forward-line 1) 0)))
1527 (when (looking-at "[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1528 #ifn?def\\)")
1529 (push-mark (save-excursion
1530 (wesnoth-jump-to-matching)
1531 (end-of-line)
1532 (point))))))
1534 (defun wesnoth-down-list (&optional arg)
1535 "Move forward down ARG levels of elements.
1536 If ARG is not specified, move forward down one level."
1537 (interactive "p")
1538 (unless arg
1539 (setq arg 1))
1540 (when (looking-at "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1541 #ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)")
1542 (forward-char 1))
1543 (let ((target nil))
1544 (save-excursion
1545 (when (looking-at "\\(\\w\\|_\\)+[\t ]*=")
1546 (if (and (search-forward-regexp (wesnoth-element) (point-max) t)
1547 (progn (beginning-of-line)
1548 (looking-at (wesnoth-element-opening t))))
1549 (setq target (point))
1550 (message "%s" "Innermost level"))))
1551 (when target (goto-char target)))
1552 (while (> arg 0)
1553 (when (> (save-excursion (wesnoth-jump-to-matching) (point)) (point))
1554 (search-forward-regexp
1555 "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|#ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)"
1556 (save-excursion
1557 (wesnoth-jump-to-matching)
1558 (point))
1560 (setq arg (1- arg)))
1561 (back-to-indentation))
1563 (defun wesnoth-backward-up-list (&optional arg)
1564 "Move backward up ARG levels of elements.
1565 If ARG is not specified, move backward up one level."
1566 (interactive "p")
1567 (unless arg
1568 (setq arg 1))
1569 (let ((parent nil))
1570 (while (> arg 0)
1571 (setq parent (cdr (wesnoth-parent-tag)))
1572 (if (numberp parent)
1573 (progn
1574 (goto-char parent)
1575 (setq arg (1- arg)))
1576 (message "%s" "Outermost level")
1577 (setq arg 0))
1578 (when (numberp parent)
1579 (back-to-indentation)))))
1581 (defun wesnoth-forward-list (arg)
1582 "Move to forward ARG elements at the current depth.
1583 If ARG is not specifed, move forward one element."
1584 (interactive "p")
1585 (while (and (save-excursion (beginning-of-line)
1586 (looking-at "[\t ]*$"))
1587 (= (forward-line 1) 0)))
1588 (when (save-excursion (beginning-of-line)
1589 (looking-at (wesnoth-element-opening)))
1590 (wesnoth-jump-to-matching)
1591 (setq arg (1- arg)))
1592 (let ((revert-target (point)))
1593 (while (> arg 0)
1594 (beginning-of-line)
1595 (unless (looking-at (wesnoth-element-closing))
1596 (wesnoth-jump-to-matching))
1597 (end-of-line)
1598 (if (and (search-forward-regexp (wesnoth-element) (point-max) t)
1599 (save-excursion (beginning-of-line)
1600 (looking-at (wesnoth-element-opening))))
1601 (setq revert-target (point))
1602 (goto-char revert-target)
1603 (message "%s" "End of block"))
1604 (setq arg (1- arg))))
1605 (when (save-excursion (beginning-of-line)
1606 (looking-at (wesnoth-element-opening)))
1607 (wesnoth-jump-to-matching))
1608 (end-of-line))
1610 (defun wesnoth-backward-list (arg)
1611 "Move to backward ARG elements at the current depth.
1612 If ARG is not specifed, move backward one element."
1613 (interactive "p")
1614 (while (and (save-excursion (beginning-of-line) (looking-at "[\t ]*$"))
1615 (= (forward-line -1) 0)))
1616 (when (save-excursion (beginning-of-line)
1617 (looking-at (wesnoth-element-closing)))
1618 (wesnoth-jump-to-matching)
1619 (setq arg (1- arg)))
1620 (let ((revert-target (point)))
1621 (while (> arg 0)
1622 (beginning-of-line)
1623 (unless (looking-at (wesnoth-element-opening))
1624 (wesnoth-jump-to-matching))
1625 (if (and (search-backward-regexp (wesnoth-element) (point-min) t)
1626 (save-excursion (beginning-of-line)
1627 (looking-at (wesnoth-element-closing))))
1628 (setq revert-target (point))
1629 (goto-char revert-target)
1630 (message "%s" "Beginning of block"))
1631 (setq arg (1- arg))))
1632 (when (looking-at (wesnoth-element-closing))
1633 (wesnoth-jump-to-matching)))
1635 (defun wesnoth-check-wml (&optional preserve-buffer)
1636 "Perform context-sensitive analysis of WML-code.
1637 If PRESERVE-BUFFER is non-nil, the contents of *WML* will be
1638 maintained through successive calls."
1639 (interactive)
1640 ;; Temporarily cache all tag-data.
1641 (setq wesnoth-tmp-tag-data (wesnoth-refresh-wml-data))
1642 (wesnoth-update-project-information)
1643 (if (fboundp 'delete-overlay)
1644 (dolist (overlay (overlays-in (point-min) (point-max)))
1645 (if (eq 'wesnoth-warning-face (overlay-get overlay 'face))
1646 (delete-overlay overlay)))
1647 (setq wesnoth-warning-markers nil))
1648 (when (= 0 (hash-table-count wesnoth-tag-hash-table))
1649 (error "WML data not available; unable to generate report"))
1650 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1651 (let ((unmatched '())
1652 (source-buffer (buffer-name))
1653 (outbuf (get-buffer-create "*WML*"))
1654 (last-match-pos 1)
1655 (details nil)
1656 (foreach '())
1657 (map (make-sparse-keymap)))
1658 (define-key map (kbd "q") 'bury-buffer)
1659 (save-excursion
1660 (set-buffer outbuf)
1661 (use-local-map map)
1662 (let ((buffer (buffer-name))
1663 (buffer-read-only nil))
1664 (if preserve-buffer
1665 (goto-char (point-max))
1666 (erase-buffer))
1667 (message (format "Checking %s..." source-buffer))))
1668 (save-excursion
1669 (goto-char (point-min))
1670 (while (setq details (wesnoth-find-next
1671 '(tag-opening tag-closing preprocessor attribute
1672 macro)))
1673 (save-excursion
1674 (goto-char (match-beginning 0))
1675 (cond ((nth 3 (parse-partial-sexp last-match-pos (point)))
1676 nil)
1677 ((eq (car details) 'macro)
1678 (dolist (macro (save-match-data
1679 (wesnoth-extract-macro-details
1680 (match-string-no-properties 0))))
1681 (unless (assoc macro
1682 (wesnoth-merge-macro-data
1683 wesnoth-macro-data
1684 (wesnoth-macro-additions)
1685 wesnoth-local-macro-data
1686 (wesnoth-macro-arguments)))
1687 (wesnoth-check-process "Unknown macro: '%s'"
1688 macro)))
1689 (save-match-data
1690 (when
1691 (looking-at
1692 "{\\(FOREACH\\|NEXT\\).*[\t ]+\
1693 \\(\\(?:\\w\\|_\\)+\\)}")
1694 (if (string= (match-string-no-properties 1) "FOREACH")
1695 (setq foreach
1696 (cons (match-string-no-properties 2) foreach))
1697 (if (string= (match-string-no-properties 1) "NEXT")
1698 (progn
1699 (unless (string= (car foreach)
1700 (match-string-no-properties 2))
1701 (wesnoth-check-process
1702 (concat "NEXT does not match corresponding "
1703 "FOREACH: '%s' found; '%s' expected.")
1704 (match-string-no-properties 2)
1705 (car foreach)))
1706 (setq foreach (cdr foreach))))))))
1707 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
1708 (unless (wesnoth-check-element-type 0)
1709 (wesnoth-check-process
1710 "Tag not available in this context: '%s'"
1711 (match-string-no-properties 1)))
1712 (setq unmatched (cons (match-string-no-properties 1)
1713 unmatched)))
1714 ((looking-at
1715 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1716 "\\( \\(\\w\\|_\\)+\\)*"))
1717 (unless (match-string-no-properties 2)
1718 (wesnoth-check-process
1719 (concat "Preprocessor statement has no argument: "
1720 (match-string-no-properties 1))))
1721 (unless (string= (match-string-no-properties 1) "#undef")
1722 (setq unmatched (cons (match-string-no-properties 1)
1723 unmatched))))
1724 ((looking-at wesnoth-preprocessor-closing-regexp)
1725 (when (and unmatched
1726 (not (string-match
1727 (cdr (assoc (match-string-no-properties 1)
1728 '(("enddef" . "#define")
1729 ("endif" . "#ifn?def"))))
1730 (car unmatched))))
1731 (wesnoth-check-process
1732 "Preprocessor statement does not nest correctly"))
1733 (setq unmatched (cdr unmatched)))
1734 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)[\t ]*=\\(.+\\)?")
1735 (unless (wesnoth-check-element-type 1)
1736 (wesnoth-check-process
1737 "Key not available in this context: '%s'"
1738 (match-string-no-properties 1)))
1739 (unless (match-string 3)
1740 (wesnoth-check-process
1741 "Attribute has no value")))
1742 ((looking-at "[\t ]*#else")
1743 (unless (string-match "ifn?def" (car unmatched))
1744 (if (string= (car unmatched) "#define")
1745 (wesnoth-check-process "Expecting: '%s'"
1746 (car unmatched))
1747 (wesnoth-check-process "Expecting: '[/%s]'"
1748 (car unmatched)))))
1749 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1750 (when (and unmatched
1751 (not (string= (match-string-no-properties 1)
1752 (car unmatched))))
1753 (wesnoth-check-process
1754 "Expecting '%s'"
1755 (or (cdr (assoc (car unmatched)
1756 '(("#define" . "#enddef")
1757 ("#ifdef" . "#endif")
1758 ("#ifndef" . "#endif"))))
1759 (concat "[/" (car unmatched) "]"))))
1760 (setq unmatched (cdr unmatched)
1761 last-match-pos (point))))))
1762 (when foreach
1763 (dolist (var foreach)
1764 (wesnoth-check-process "Unmatched FOREACH: '%s'" var)))
1765 (when unmatched
1766 (dolist (element unmatched)
1767 (wesnoth-check-process "Unmatched element: '%s'" element))))
1768 (when (or (not preserve-buffer)
1769 (and preserve-buffer (not (cdr wesnoth-found-cfgs))))
1770 (save-excursion
1771 (setq wesnoth-define-blocks nil
1772 wesnoth-tmp-tag-data nil)
1773 (set-buffer outbuf)
1774 (toggle-read-only t)
1775 (let ((buffer (buffer-name))
1776 (buffer-read-only nil))
1777 (display-buffer outbuf t)
1778 (let ((warnings (- (wesnoth-line-number-at-pos
1779 (goto-char (point-max))) 1)))
1780 (insert (format (concat "\nCheck complete. %d warning"
1781 (if (= warnings 1) "." "s.")) warnings)))
1782 (message (format "Checking %s...done" source-buffer)))))))
1784 ;; TODO: This will not load dependant files in the right order. Need to
1785 ;; process from _main.cfg and similar.
1786 (defun wesnoth-check-directory (dir)
1787 "Check all WML in DIR.
1788 Where DIR is a directory containing WML files."
1789 (interactive "DDirectory: ")
1790 (setq wesnoth-found-cfgs nil)
1791 (when (member (mapcar 'buffer-name (buffer-list)) "*WML*")
1792 (kill-buffer "*WML*"))
1793 (wesnoth-fetch-all-dirs dir)
1794 (let ((tmp-cfgs wesnoth-found-cfgs))
1795 (while tmp-cfgs
1796 (find-file (car wesnoth-found-cfgs))
1797 (kill-buffer)
1798 (setq tmp-cfgs (cdr tmp-cfgs))))
1799 (save-excursion
1800 (while wesnoth-found-cfgs
1801 (find-file (car wesnoth-found-cfgs))
1802 (wesnoth-check-wml t)
1803 (kill-buffer)
1804 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))
1806 (defun wesnoth-file-cfg-p (file)
1807 "Return non-nil if FILE has a '.cfg' extension."
1808 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
1810 (defun wesnoth-fetch-all-dirs (dir)
1811 "Retrieve a list of subdirectories to scan.
1812 DIR is the directory to check."
1813 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
1814 (while dirs-to-scan
1815 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
1816 dirs-to-scan)))))
1818 (defun wesnoth-files-in-dir (dir)
1819 "Add cfgs to `wesnoth-files-in-dir'.
1820 Returns a list of sub-directories in DIR."
1821 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
1822 (when cfgs
1823 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
1824 (let ((dirs '()))
1825 (dolist (file (directory-files dir t))
1826 (unless (string-match "^\\..*" (file-name-nondirectory file))
1827 (cond ((file-directory-p file)
1828 (add-to-list 'dirs file))
1829 ((wesnoth-file-cfg-p file)
1830 (add-to-list 'wesnoth-found-cfgs file)))))
1831 dirs))
1833 (defun wesnoth-cfg-files-in-dir (dir)
1834 "Return all cfg files in DIR."
1835 (let ((result '()))
1836 (dolist (file (directory-files dir t))
1837 (and (wesnoth-file-cfg-p file)
1838 (add-to-list 'result file)))
1839 result))
1842 ;;; wesnoth-mode
1843 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
1844 "Major mode for editing WML."
1845 (kill-all-local-variables)
1846 (use-local-map wesnoth-mode-map)
1847 (setq major-mode 'wesnoth-mode)
1848 (setq mode-name "WML")
1849 (set-syntax-table wesnoth-syntax-table)
1850 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
1851 (set (make-local-variable 'comment-start) "#")
1852 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
1853 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
1854 (set (make-local-variable 'wesnoth-warning-markers) '())
1855 (set (make-local-variable 'font-lock-defaults)
1856 '(wesnoth-font-lock-keywords
1857 nil t nil nil
1858 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
1859 (setq indent-tabs-mode nil)
1860 (easy-menu-add wesnoth-menu wesnoth-mode-map)
1861 (wesnoth-refresh-wml-data)
1862 (wesnoth-update-project-information)
1863 (run-hooks 'wesnoth-mode-hook))
1865 (provide 'wesnoth-mode)
1867 ;;; wesnoth-mode.el ends here