Minor improvements to documentation.
[wesnoth-mode.git] / wesnoth-mode.el
blob307b9caf1c3aa32193301e7702ebd99cf5d84a32
1 ;;; wesnoth-mode.el --- A major mode for editing WML.
2 ;; Copyright (C) 2006, 2007, 2008, 2009 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.")
237 (defcustom wesnoth-base-indent 4
238 "The number of columns to indent WML."
239 :type 'integer
240 :group 'wesnoth-mode)
242 (defcustom wesnoth-allow-accurate-validation nil
243 "Whether to allow a more accurate method of WML validation.
244 If non-nil, allow a more accurate method (which is able to better
245 determine the current element's parent) to be used. Note that
246 this may cause WML validation to slow significantly when checking
247 error-prone or messy WML. If WML checked is rarely incorrect it
248 is recommended to set this to non-nil."
249 :type 'boolean
250 :group 'wesnoth-mode)
252 (defconst wesnoth-preprocessor-regexp
253 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\
254 \\|\\(ifn?\\|un\\)def \\)"
255 "Regular expression to match all preprocessor statements.")
257 (defconst wesnoth-preprocessor-opening-regexp
258 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
259 "Regular expression to match \"opening\" preprocessor statements.")
261 (defconst wesnoth-preprocessor-closing-regexp
262 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
263 "Regular expression to match \"closing\" preprocessor statements.")
265 (defvar wesnoth-define-blocks '()
266 "Cache of all toplevel #define and #enddef pairs.")
268 (defvar wesnoth-history-list '()
269 "History of inserted WML elements.")
271 (defvar wesnoth-warning-markers '()
272 "Markers for warnings in the buffer.")
274 (defvar wesnoth-found-cfgs '()
275 "Temporary list of all .cfg files found.")
277 (defvar wesnoth-mode-hook nil)
279 (defface wesnoth-warning-face
280 '((t (:underline "tomato1")))
281 "Face to use for warnings in wesnoth-mode"
282 :group 'wesnoth-mode)
284 (defvar wesnoth-mode-map
285 (let ((map (make-sparse-keymap)))
286 (define-key map (kbd "C-M-a") 'wesnoth-backward-element)
287 (define-key map (kbd "C-M-e") 'wesnoth-forward-element)
288 (define-key map (kbd "C-M-n") 'wesnoth-forward-list)
289 (define-key map (kbd "C-M-p") 'wesnoth-backward-list)
290 (define-key map (kbd "C-M-d") 'wesnoth-down-list)
291 (define-key map (kbd "C-M-u") 'wesnoth-backward-up-list)
292 (define-key map (kbd "C-M-k") 'wesnoth-kill-block)
293 (define-key map (kbd "C-M-SPC") 'wesnoth-mark-block)
294 (define-key map (kbd "C-m") 'wesnoth-newline)
295 (define-key map (kbd "C-j") 'wesnoth-newline-and-indent)
296 (define-key map (kbd "C-c C-c") 'wesnoth-check-wml)
297 (define-key map (kbd "C-c C-a") 'wesnoth-complete-attribute)
298 (define-key map (kbd "C-c C-t") 'wesnoth-complete-tag)
299 (define-key map (kbd "C-c C-p") 'wesnoth-complete-preprocessor)
300 (define-key map (kbd "C-c C-u") 'wesnoth-update-project-information)
301 (define-key map (kbd "M-TAB") 'wesnoth-complete-tag)
302 (define-key map (kbd "C-c C-m") 'wesnoth-complete-macro)
303 (define-key map (kbd "C-c C-o") 'wesnoth-jump-to-matching)
304 (define-key map (kbd "C-c C-f") 'wesnoth-forward-warning)
305 (define-key map (kbd "C-x `") 'wesnoth-forward-warning)
306 (define-key map (kbd "C-c C-b") 'wesnoth-backward-warning)
307 (define-key map (kbd "C-c C-/") 'wesnoth-insert-missing-closing)
308 (define-key map (kbd "TAB") 'wesnoth-indent-or-complete)
309 map)
310 "Keymap used in `wesnoth-mode'.")
312 (easy-menu-define wesnoth-menu wesnoth-mode-map "Menu for wesnoth-mode"
313 '("WML"
314 ["Check WML" wesnoth-check-wml t]
315 ["Indent or Complete" wesnoth-indent-or-complete t]
316 ["Indent buffer" (lambda ()
317 (interactive)
318 (wesnoth-indent-region (point-min) (point-max))) t]
319 ["Insert Tag" wesnoth-complete-tag t]
320 ["Insert Attribute" wesnoth-complete-attribute t]
321 ["Insert Macro" wesnoth-complete-macro t]
322 ["Insert Preprocessor" wesnoth-complete-preprocessor t]
323 ["Insert Missing Tag" wesnoth-insert-missing-closing t]
324 ["Jump to Matching" wesnoth-jump-to-matching t]
325 ["Update Macros" wesnoth-update-project-information t]))
327 (defvar wesnoth-syntax-table
328 (let ((wesnoth-syntax-table (make-syntax-table)))
329 (modify-syntax-entry ?# "<" wesnoth-syntax-table)
330 (modify-syntax-entry ?\" "\"" wesnoth-syntax-table)
331 (modify-syntax-entry ?= "." wesnoth-syntax-table)
332 (modify-syntax-entry ?| "w" wesnoth-syntax-table)
333 (modify-syntax-entry ?_ "_" wesnoth-syntax-table)
334 (modify-syntax-entry ?- "_" wesnoth-syntax-table)
335 (modify-syntax-entry ?. "_" wesnoth-syntax-table)
336 (modify-syntax-entry ?\n ">" wesnoth-syntax-table)
337 (modify-syntax-entry ?\r ">" wesnoth-syntax-table)
338 wesnoth-syntax-table)
339 "Syntax table for `wesnoth-mode'.")
341 ;; Prevents automatic syntax-highlighting of elements which might be
342 ;; pre-processor statements.
343 (defvar wesnoth-syntactic-keywords
344 (list
345 '("\\([\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\
346 \\(?:ifn?\\|un\\)def \\)\\)\\)" 1 "w"))
347 "Syntactic keywords for preprocessor statements within `wesnoth-mode'.")
349 (defvar wesnoth-font-lock-keywords
350 (list
351 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face)
352 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
353 font-lock-keyword-face)
354 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
355 2 font-lock-function-name-face)
356 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face))
357 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face))
358 '("}" . font-lock-function-name-face)
359 '("[\t ]*\\(\\[/?[^$]\\(\\w\\|_\\)+\\]\\)" 1 font-lock-type-face)
360 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face)
361 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)[\t ]*="
362 1 font-lock-variable-name-face))
363 "Syntax highlighting for `wesnoth-mode'.")
365 (defun wesnoth-element-closing (&optional limited)
366 "Return the regexp to match a closing element.
367 If LIMITED is non-nil, return a regexp which matches only the
368 #enddef preprocessor."
369 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\]\\|"
370 (if limited
371 "#enddef"
372 "#end\\(?:def\\|if\\)")
373 (if (and (not wesnoth-indent-preprocessor-bol) limited)
374 "\\|#endif"
376 "\\)"))
378 (defun wesnoth-element-opening (&optional limited)
379 "Return the regexp to match an opening element.
380 If LIMITED is non-nil, return a regexp which matches only the
381 #define preprocessor."
382 (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define "
383 (if limited
384 "\\|{FOREACH .+}"
385 "\\|#ifn?def ")
386 (if (and (not wesnoth-indent-preprocessor-bol) limited)
387 "\\|#ifn?def \\|#else"
389 "\\)"))
391 (defun wesnoth-element (&optional limited)
392 "Return the regexp to match any element.
393 If LIMITED is non-nil, return a regexp which matches only the
394 #define and #enddef preprocessors."
395 (concat "^[\t ]*\\(\\[[+/]?\\(\\w\\|_\\)+\\]?\\|"
396 (if limited
397 "#define \\|#enddef"
398 (substring wesnoth-preprocessor-regexp 5))
399 (if (and (not wesnoth-indent-preprocessor-bol) limited)
400 "\\|#\\(ifn?def \\|endif\\|else\\)"
402 "\\)"))
404 (defun wesnoth-find-next (type)
405 "Find the next element of TYPE.
406 TYPE is a symbol representing an element type, or a list of
407 element types to find."
408 (let ((element (wesnoth-next-element)))
409 (while (and element (if (listp type)
410 (not (member (car element) type))
411 (not (eq (car element) type))))
412 (setq element (wesnoth-next-element)))
413 (when (if (listp type)
414 (member (car element) type)
415 (eq (car element) type))
416 element)))
418 (defun wesnoth-next-element ()
419 "Move to the next element in the buffer.
420 Return non-nil when an element is found. Otherwise, return nil."
421 (interactive)
422 (save-match-data
423 (and (or (eolp) (looking-at "[}\t ]"))
424 (search-forward-regexp "[^}
425 \t ]" (point-max) t)
426 (forward-char -1)))
427 (let ((details (wesnoth-element-type (point))))
428 (save-match-data
429 (when (nth 2 details)
430 (goto-char (nth 2 details))
431 (while (nth 3 (parse-partial-sexp
432 (save-excursion (search-backward-regexp
433 (wesnoth-element t) (point-min) t)
434 (point))
435 (point)))
436 (search-forward "\"" (point-max) t))))
437 details))
439 (defun wesnoth-find-previous (type)
440 "Find the previous element of TYPE.
441 TYPE is a symbol representing an element type, or a list of
442 element types to find."
443 (let ((element (wesnoth-previous-element)))
444 (while (and element (if (listp type)
445 (not (member (car element) type))
446 (not (eq (car element) type))))
447 (setq element (wesnoth-previous-element)))
448 (when (if (listp type)
449 (member (car element) type)
450 (eq (car element) type))
451 element)))
453 ;; TODO: This is currently quite inefficient, and therefore is only used where
454 ;; necessary. (i.e., when the parent element is initially may not be
455 ;; correctly detected.)
456 (defun wesnoth-previous-element ()
457 "Move to the previous element in the buffer.
458 Return non-nil when an element is found. Otherwise, return nil."
459 (interactive)
460 (if (= (point) (wesnoth-wml-start-pos))
462 (let ((init-element (save-excursion (wesnoth-next-element)))
463 last-element cur-element)
464 (save-match-data
465 (search-backward-regexp "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|#ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)" (point-min) t))
466 (while (< (cadr (setq cur-element (wesnoth-next-element)))
467 (cadr init-element))
468 (when (car cur-element)
469 (setq last-element cur-element)))
470 (goto-char (or (cadr last-element) (cadr init-element) (point-min))))
471 ;; Determine whether currently within a string, and move to its beginning
472 ;; where necessary.
473 (save-excursion
474 (let ((details (wesnoth-element-type (point))))
475 (save-match-data
476 (when (cadr details)
477 (goto-char (cadr details))
478 (while (nth 3 (parse-partial-sexp
479 (save-excursion (search-backward-regexp
480 (wesnoth-element t) (point-min) t)
481 (point))
482 (point)))
483 (search-backward "\"" (point-min) t))))
484 details))))
486 (defun wesnoth-element-type (point)
487 "Return details regarding the element at POINT.
488 A list is returned, the elements of the list represent the
489 following, respectively: A symbol representing the type of
490 element; the position of the start of the element and the
491 position of the end of the element. POINT must be at the start
492 of the element."
493 (let ((element-matches
494 '(("{\\(.*?[/\]\\)+}" . nil) ;; pathnames
495 ("{\\(\\w\\|_\\)+" . macro)
496 ("\\[\\+?[^/]+?\\]" . tag-opening)
497 ("\\[/.+?\\]" . tag-closing)
498 ("\\(\\w\\|_\\)+[\t ]*=" . attribute)
499 ("#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\
500 \\(ifn?\\|un\\)def \\)"
501 . preprocessor)
502 ("#.*$" . comment)
503 ("[^\t ]+") . nil)))
504 (catch 'result
505 (dolist (pair element-matches)
506 (when (looking-at (car pair))
507 (throw 'result (list (cdr pair)
508 (match-beginning 0)
509 (min (save-excursion (forward-line 1) (point))
510 (match-end 0)))))))))
512 (defun wesnoth-estimate-element-type (point)
513 "Return match data for a partial element at POINT."
514 (save-excursion
515 (goto-char point)
516 (let ((element-matches
517 '(("{\\(.*?[/\]\\)+$" . nil) ; pathnames
518 ("{\\(\\w\\|_\\)*$" . macro)
519 ("\\[/\\(\\w\\|_\\)*$" . tag-closing)
520 ("\\[\\+?\\(\\w\\|_\\)*$" . tag-opening)
521 ("^[\t ]*\\(\\w\\|_\\)+$" . attribute)
522 ("[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\
523 \\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
524 . nil) ; not a partial match
525 ("[\t ]*#\\w*$" . preprocessor))))
526 (catch 'result
527 (dolist (pair element-matches)
528 (when (looking-at (car pair))
529 (throw 'result (list (cdr pair)
530 (match-beginning 0)
531 (match-end 0)))))))))
533 (defun wesnoth-guess-element-type (point)
534 "Return details for the the element near POINT.
535 Locate the start of the element before determining details.
536 BOUND is the limit to search backwards."
537 (let ((details (wesnoth-estimate-element-type point))
538 (bound (save-excursion
539 (goto-char point)
540 (beginning-of-line)
541 (point))))
542 (while (and (not (car details)) (> point bound))
543 (setq point (1- point)
544 details (wesnoth-estimate-element-type point)))
545 (and (nth 1 details)
546 (>= (point) (nth 1 details))
547 (nth 2 details)
548 (<= (point) (nth 2 details))
549 details)))
552 ;;; Insertion and completion
553 (defmacro wesnoth-completion-method (prompt completions)
554 "Determine a suitable completion method for use.
555 See `wesnoth-use-alternate-completion' for more information."
556 (cond
557 ((and (featurep 'ido)
558 (or (eq wesnoth-use-alternate-completion 'ido)
559 (eq wesnoth-use-alternate-completion 'guess)))
560 `(ido-completing-read ,prompt ,completions
561 nil nil element
562 'wesnoth-history-list))
564 `(completing-read ,prompt ,completions
565 nil nil element
566 'wesnoth-history-list))))
568 (defmacro wesnoth-element-completion (completions prompt partial
569 &optional completep)
570 "Process completion of COMPLETIONS, displaying PROMPT.
571 PARTIAL is the partial string on which to attempt completion.
572 If COMPLETEP is non-nil, do not prompt if no completion is found."
573 `(let* ((element (when ,partial (try-completion ,partial ,completions))))
574 (cond ((eq element t)
575 ,partial)
576 ((and ,completep (null element))
577 nil)
578 ((and element (eq (try-completion element ,completions) t))
579 element)
580 ((> (length (all-completions (or element "") ,completions)) 1)
581 (wesnoth-completion-method ,prompt ,completions))
582 ((= (length ,completions) 1)
583 (car ,completions))
585 element))))
587 (defun wesnoth-active-parent-tag ()
588 "Return the name of the active parent tag.
589 Finds the relevant parent tag, ignoring any conditional tags."
590 (save-excursion
591 (let ((parent (wesnoth-parent-tag)))
592 (while (and (stringp (car parent))
593 (string-match "else\\|then"
594 (car parent)))
595 (goto-char (cdr parent))
596 (setq parent (wesnoth-parent-tag))
597 (when (string= (car parent) "if")
598 (goto-char (cdr parent))
599 (setq parent (wesnoth-parent-tag))))
600 (car parent))))
602 (defun wesnoth-parent-tag (&optional exact)
603 "Return the name of the parent tag.
604 If the parent is a preprocessor statement, return non-nil.
605 If the element does not have a parent, return nil.
606 Otherwise, return a string containing the name of the parent tag.
607 If EXACT is non-nil a more accurate, yet slower method is used."
608 (save-excursion
609 (let ((start-point (point))
610 (depth 1))
611 (when (save-excursion (> (point) (progn (back-to-indentation)
612 (point))))
613 (end-of-line))
614 (while (and (> depth 0)
615 (if exact
616 (wesnoth-find-previous
617 '(tag-opening tag-closing preprocessor))
618 (search-backward-regexp (wesnoth-element t) (point-min) t)))
619 (if (string-match "[\t ]*\\[/\\|#enddef" (match-string 0))
620 (setq depth (1+ depth))
621 (setq depth (1- depth))))
622 (beginning-of-line)
623 (if (> depth 0)
624 (cons nil nil)
625 (when (looking-at (wesnoth-element-opening))
626 (let ((parent (match-string-no-properties 1))
627 (position (point)))
628 (if (or (string-match wesnoth-preprocessor-opening-regexp parent)
629 ;; Check if we're immediately within a macro
630 (and (goto-char start-point)
631 (search-backward-regexp "[}{]" (point-min) t)
632 (string= (match-string 0) "{")
633 (goto-char start-point)
634 (not (and (search-backward parent (point-min) t)
635 (search-backward-regexp "[}{]" (point-min)
637 (string= (match-string 0) "{")))))
638 (cons t position)
639 (cons (substring parent (if (string-match "[\\+.+]" parent)
642 (1- (length parent))) position))))))))
644 (defun wesnoth-partial-macro-p ()
645 "Return non-nil if point is in a partial macro."
646 (save-excursion
647 (let ((opened 0))
648 (search-backward-regexp "{" (point-min) t)
649 (while (search-forward-regexp "[{}]" (point-max) t)
650 (if (string= (match-string 0) "{")
651 (setq opened (1+ opened))
652 (setq opened (1- opened))))
653 (> opened 0))))
655 (defun wesnoth-indent-or-complete (&optional elements)
656 "Indent or complete the line at point, depending on context.
657 ELEMENTS is the number of elements to wrap around if inserting
658 matching tags."
659 (interactive "P")
660 (or elements (setq elements 0))
661 (let ((details (wesnoth-guess-element-type (point))))
662 (cond
663 ((eq (car details) 'tag-opening)
664 (wesnoth-complete-tag elements t))
665 ((and (eq (car details) 'macro)
666 (wesnoth-partial-macro-p))
667 (wesnoth-complete-macro t))
668 ((eq (car details) 'preprocessor)
669 (wesnoth-complete-preprocessor elements t))
670 ((eq (car details) 'tag-closing)
671 ;; FIXME: Solve incorrect behaviour when partial closing is "[/"
672 (insert "a")
673 (and (wesnoth-insert-missing-closing t)
674 (delete-region (nth 1 details)
675 (save-excursion (beginning-of-line) (point))))
676 (end-of-line)
677 (wesnoth-indent))
678 ((eq (car details) 'attribute)
679 (wesnoth-complete-attribute t))
681 (wesnoth-indent)))))
683 (defun wesnoth-preprocessor-closed-p (preprocessor)
684 "Determine whether PREPROCESSOR has been closed.
685 PREPROCESSOR is a string matching the preprocessor statement to
686 be inserted."
687 (save-excursion
688 (back-to-indentation)
689 (wesnoth-jump-to-matching preprocessor)
690 (looking-at
691 (if (string= preprocessor "#define ")
692 "#enddef"
693 "#endif"))))
695 (defun wesnoth-complete-preprocessor (&optional elements completep)
696 "Complete and insert the preprocessor at point.
697 ELEMENTS is the number of elements to wrap around.
698 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
699 (interactive "P")
700 (or elements (setq elements 0))
701 (let* ((completions (wesnoth-emacs-completion-formats
702 '("define" "else" "ifdef" "ifndef"
703 "enddef" "endif" "undef")))
704 (partial (when completep
705 (save-excursion
706 (back-to-indentation)
707 (when (looking-at "#\\(\\w*\\)$")
708 (match-string-no-properties 1)))))
709 (preprocessor (wesnoth-element-completion
710 completions "Preprocessor: " partial completep))
711 (details (wesnoth-guess-element-type (point)))
712 (closedp
713 (save-excursion
714 (when preprocessor
715 (unless (string= "#" (substring preprocessor 0 1))
716 (setq preprocessor (concat "#" preprocessor)))
717 (when (string-match "#\\(define\\|ifn?def\\|undef\\)"
718 preprocessor)
719 (setq preprocessor (concat preprocessor " ")))
720 (when partial
721 (delete-region (nth 1 details) (nth 2 details)))
722 (wesnoth-preprocessor-closed-p preprocessor)))))
723 (when preprocessor
724 (when partial
725 (delete-region
726 (save-excursion
727 (progn (search-backward
728 "#" (save-excursion (back-to-indentation)
729 (point))
731 (point)))
732 (point)))
733 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor)
734 (not closedp))
735 (progn
736 (wesnoth-insert-tag elements preprocessor)
737 (forward-line -1)
738 (end-of-line))
739 (wesnoth-insert-element-separately preprocessor)))))
741 (defun wesnoth-macro-arguments ()
742 "Find any current macro arguments."
743 (let ((results '())
744 (depth (wesnoth-within-define (point))))
745 (save-excursion
746 (while (> depth 0)
747 (save-match-data
748 (search-backward-regexp
749 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
750 (point-min) t)
751 (when (<= (wesnoth-within-define (point)) depth)
752 (and (> depth 0)
753 (setq results
754 (append (mapcar (lambda (macro)
755 (list macro nil))
756 (split-string
757 (match-string-no-properties 1)))
758 results)))
759 (setq depth (1- depth)))))
760 results)))
762 (defun wesnoth-complete-macro (&optional completep)
763 "Complete and insert the macro at point.
764 If COMPLETEP is non-nil, attempt to complete the macro at point."
765 (interactive)
766 (wesnoth-update-project-information)
767 (let* ((macro-information (wesnoth-merge-macro-data
768 wesnoth-macro-data
769 (wesnoth-macro-additions)
770 wesnoth-local-macro-data
771 (wesnoth-macro-arguments)))
772 (completions (wesnoth-emacs-completion-formats
773 (mapcar 'car macro-information)))
774 (details (wesnoth-guess-element-type (point)))
775 (partial (when
776 (save-excursion
777 (and completep
778 (eq (car details) 'macro)
779 (goto-char (cadr details))
780 (looking-at "{\\(\\(\\w\\|_\\)*\\)")))
781 (match-string-no-properties 1)))
782 (macro (wesnoth-element-completion completions "Macro: " partial
783 completep))
784 (args (cadr (assoc macro macro-information))))
785 (when macro
786 (if partial
787 (progn
788 ;; Delete the region corresponding to the current macro.
789 (delete-region (nth 1 details) (nth 2 details))
790 (insert "{" macro (if args " }" "}"))
791 (save-excursion
792 (wesnoth-indent)))
793 (wesnoth-insert-and-indent "{" macro (if args " }" "}")))
794 (forward-char -1)
795 (when args
796 (let ((input (read-string (concat (car args) ": "))))
797 (insert input (if (and (cdr args)
798 (not (string= input "")))
799 " " ""))
800 (while (and (setq args (cdr args)) (not (string= input "")))
801 (insert (setq input (read-string (concat (car args) ": ")))
802 (if (and (not (string= input ""))
803 (cdr args))
804 " " "")))))
805 (when (null args) (forward-char 1)))))
807 (defun wesnoth-complete-attribute (&optional completep)
808 "Insert the attribute at point.
809 If COMPLETEP is non-nil, attempt to complete the attribute at point."
810 (interactive)
811 (wesnoth-refresh-wml-data)
812 (let* ((details (save-excursion
813 (back-to-indentation)
814 (wesnoth-guess-element-type (point))))
815 (completions (save-excursion (when (nth 1 details)
816 (goto-char (nth 1 details)))
817 (wesnoth-build-completion 1)))
818 (partial (when completep
819 (when (save-excursion
820 (back-to-indentation)
821 (looking-at "\\(\\(\\w\\|_\\)+\\)"))
822 (match-string-no-properties 1))))
823 (attribute (wesnoth-element-completion completions "Attribute: "
824 partial completep)))
825 (when attribute
826 (if partial
827 (progn
828 (delete-region (nth 1 details) (nth 2 details))
829 (insert attribute "="))
830 (wesnoth-insert-element-separately attribute
831 (if (string-match "=" attribute)
833 "=")))
834 (save-excursion
835 (wesnoth-indent)))))
837 (defun wesnoth-complete-tag (&optional elements completep)
838 "Complete and insert the tag at point.
839 ELEMENTS is the number of elements to wrap around.
840 If COMPLETEP is non-nil, attempt to complete tag at point."
841 (interactive "P")
842 (or elements (setq elements 0))
843 (let* ((details (wesnoth-guess-element-type (point)))
844 (completions (save-excursion (and (nth 1 details)
845 (goto-char (nth 1 details)))
846 (wesnoth-build-completion 0)))
847 (partial (save-excursion
848 (when (and completep
849 (eq (car details) 'tag-opening)
850 (goto-char (cadr details))
851 (looking-at "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$"))
852 (match-string-no-properties 1))))
853 (tag (wesnoth-element-completion completions "Tag: " partial
854 completep))
855 (closedp
856 (save-excursion
857 (wesnoth-jump-to-matching (concat "[" tag "]"))
858 (back-to-indentation)
859 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
860 (string= tag (match-string 1))))))
861 (if tag
862 (progn
863 (if completep
864 (progn
865 (delete-region (nth 1 details) (nth 2 details))
866 (if closedp
867 (progn
868 (wesnoth-insert-and-indent "[" tag "]")
869 (end-of-line))
870 (wesnoth-insert-tag elements tag)))
871 (wesnoth-insert-tag elements tag)))
872 (or completep (wesnoth-insert-tag elements)))))
874 (defun wesnoth-build-completion (position)
875 "Create a new list for tag completion if necessary.
876 Rebuilding list is required for versions of GNU Emacs earlier
877 than 22. POSITION is the argument passed to `nth' for
878 `wesnoth-tag-data'."
879 (interactive "P")
880 (let ((parent (wesnoth-active-parent-tag))
881 (tag-data (wesnoth-refresh-wml-data)))
882 (wesnoth-emacs-completion-formats
883 (if (or (stringp parent) (null parent))
884 (nth position (gethash parent wesnoth-tag-hash-table))
885 (mapcar 'car tag-data)))))
887 (defun wesnoth-emacs-completion-formats (candidates)
888 "Return the completions in the correct format for `emacs-major-version'.
889 CANDIDATES is a list of all possible completions."
890 (if (> emacs-major-version 21)
891 candidates
892 (let ((tags '())
893 (iter 0))
894 (dolist (tag candidates)
895 (setq iter (1+ iter))
896 (setq tags (append tags (list (cons tag iter)))))
897 tags)))
899 (defun wesnoth-insert-tag (&optional elements tagname)
900 "Insert the specified opening tag and it's matching closing tag.
901 Both the opening and closing tags will be placed on their own
902 lines with point positioned between them. Completion of tags at
903 the prompt uses `wesnoth-tags-list'.
905 ELEMENTS is specifies the number of following blocks which the
906 tag should wrap around.
908 TAGNAME is the name of the tag to be inserted."
909 (interactive "Ps")
910 (unless tagname
911 (setq tagname (wesnoth-completion-method "Tag: "
912 (wesnoth-build-completion 0))))
913 (when (or (not elements)
914 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
915 wesnoth-preprocessor-regexp "\\)")))
916 (setq elements 0))
917 (let ((start (save-excursion (forward-line -1) (point)))
918 (end (unless (= elements 0)
919 ;; Work around some strange behaviour when the target is at the
920 ;; end of the buffer.
921 (save-excursion
922 (goto-char (point-max))
923 (beginning-of-line)
924 (unless (looking-at "^[\t ]*$")
925 (end-of-line)
926 (newline)))
927 (wesnoth-nth-pair-position elements))))
928 (if (string-match wesnoth-preprocessor-regexp tagname)
929 (wesnoth-insert-element-separately tagname)
930 (wesnoth-insert-element-separately "[" tagname "]"))
931 (save-excursion
932 (if end
933 (goto-char (marker-position end))
934 (newline (if (string-match wesnoth-preprocessor-regexp tagname) 1 2)))
935 (if (string-match wesnoth-preprocessor-opening-regexp tagname)
936 (wesnoth-insert-element-separately
937 (if (string= tagname "#define ")
938 "#enddef"
939 "#endif"))
940 (wesnoth-insert-element-separately "[/" (if (string-match "^+"
941 tagname)
942 (substring tagname 1)
943 tagname)
944 "]"))
945 (indent-region start (point) nil))
946 (unless end
947 (forward-line 1)))
948 (wesnoth-indent))
950 (defun wesnoth-insert-element-separately (&rest strings)
951 "Concatenate STRINGS and insert them on a line of their own."
952 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
953 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
954 (progn
955 (forward-line 1)
956 (end-of-line))
957 (end-of-line)
958 (newline))
959 (beginning-of-line)
960 (if (looking-at "^[\t ]*$")
961 (end-of-line)
962 (open-line 1)))
963 (insert (apply 'concat strings)))
965 (defun wesnoth-insert-missing-closing (&optional completep)
966 "Insert the next expected closing element at point.
967 If COMPLETEP is non-nil, do not move forward a line when scanning
968 for the matching tag."
969 (interactive)
970 (let ((match nil)
971 (skip t))
972 (save-excursion
973 (when (and (null completep)
974 (<= (point) (save-excursion (back-to-indentation) (point))))
975 (if (save-excursion (beginning-of-line)
976 (looking-at (wesnoth-element-opening)))
977 (forward-line -1)
978 (when
979 (save-excursion (beginning-of-line)
980 (looking-at (wesnoth-element-closing)))
981 (setq skip nil))))
982 (when (wesnoth-search-for-matching-tag
983 'search-backward-regexp (wesnoth-element-opening) 'point-min
984 (and skip (if completep nil 1)))
985 (setq match (and (looking-at (wesnoth-element-opening))
986 (match-string-no-properties 1)))))
987 (when match
988 (if (string= (substring match 0 1) "[")
989 (wesnoth-insert-element-separately
990 "[/"
991 (let ((tagname (substring match 1 (1- (length match)))))
992 (if (string-match "^+" tagname)
993 (substring tagname 1)
994 tagname))
995 "]")
996 (wesnoth-insert-element-separately
997 (cdr (assoc match '(("#define " . "#enddef")
998 ("#ifndef " . "#endif")
999 ("#ifdef " . "#endif")))))))
1000 (wesnoth-indent)
1001 (end-of-line)
1002 match))
1004 (defun wesnoth-insert-and-indent (&rest args)
1005 "Concatenate and insert the given string(s) before indenting.
1007 ARGS is a list of strings to be inserted."
1008 (insert (apply 'concat args))
1009 (wesnoth-indent))
1011 (defun wesnoth-newline (&optional indent)
1012 "Indent the current line and create a newline.
1013 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1014 performed. Indentation can be forced by setting INDENT to
1015 non-nil."
1016 (interactive "P")
1017 (newline)
1018 (save-excursion
1019 (forward-line -1)
1020 (when (and (or wesnoth-auto-indent-flag indent)
1021 (not (looking-at "^[\t ]*$")))
1022 (wesnoth-indent))))
1024 ;;; Movement
1025 (defun wesnoth-navigate-element (repeat search-function bound)
1026 "Move point to the tag in the given direction REPEAT times.
1028 SEARCH-FUNCTION is the symbol of the function for searching in
1029 the required direction, with BOUND marking the furthest point to
1030 search."
1031 (or repeat (setq repeat 1))
1032 (while (> repeat 0)
1033 (and (eq search-function 'search-forward-regexp) (end-of-line))
1034 (funcall search-function (wesnoth-element-opening) bound t)
1035 (back-to-indentation)
1036 (setq repeat (1- repeat))))
1038 (defun wesnoth-nth-pair-position (count)
1039 "Return `point' after COUNT number of matching element pairs.
1040 COUNT is a positive number representing the number of balanced
1041 pairs to move across.
1042 `point' is returned as a marker object."
1043 (save-excursion
1044 (let ((failed nil))
1045 (if (> (point) (save-excursion (back-to-indentation) (point)))
1046 (end-of-line)
1047 (beginning-of-line))
1048 (while (> count 0)
1049 ;; Currently looking-at target tag. Stop here to avoid
1050 ;; incorrect nesting.
1051 (unless (wesnoth-search-for-matching-tag
1052 'search-forward-regexp (wesnoth-element-closing) 'point-max)
1053 (setq count 0)
1054 (setq failed t))
1055 (and (> (setq count (1- count)) 0) (forward-line 1)))
1056 (if failed
1057 (beginning-of-line)
1058 (end-of-line))
1059 (point-marker))))
1061 (defun wesnoth-forward-element (repeat)
1062 "Move point to the end of the next tag.
1063 REPEAT is an optional numeric argument. If REPEAT is non-nil,
1064 jump forward the specified number of tags."
1065 (interactive "p")
1066 (if (< repeat 0)
1067 (wesnoth-backward-element (abs repeat))
1068 (wesnoth-navigate-element repeat 'search-forward-regexp (point-max))))
1070 (defun wesnoth-backward-element (repeat)
1071 "Move point to the beginning of the previous tag.
1072 REPEAT is an optional numeric argument. If REPEAT is non-nil,
1073 jump backward the specified number of tags."
1074 (interactive "p")
1075 (if (< repeat 0)
1076 (wesnoth-forward-element (abs repeat))
1077 (wesnoth-navigate-element repeat 'search-backward-regexp (point-min))))
1079 (defun wesnoth-search-for-matching-tag (search-function
1080 search-string bound &optional skip)
1081 "Search for the matching tag for the current line.
1082 SEARCH-FUNCTION is the name of the function used to perform the search.
1083 SEARCH-STRING is a string representing the matching tag type.
1084 BOUND is the bound to be passed to the search function.
1085 If SKIP is non-nil, skip the first element and continue from there."
1086 (let ((depth 1))
1087 (when (and (or (and (numberp skip) (forward-line skip))
1088 (funcall search-function (wesnoth-element)
1089 (funcall bound) t))
1090 (or skip (not (string-match search-string (match-string 0)))))
1091 (while (and (> depth 0)
1092 (funcall search-function (wesnoth-element)
1093 (funcall bound) t))
1094 (if (string-match search-string (match-string 0))
1095 (setq depth (1- depth))
1096 (setq depth (1+ depth))))
1097 (= depth 0))))
1099 (defun wesnoth-jump-to-matching (&optional element)
1100 "Jump point to the matching opening/closing tag.
1101 ELEMENT is an element to find a match for."
1102 (interactive)
1103 (beginning-of-line)
1104 (let ((target nil)
1105 (first-element nil))
1106 (save-excursion
1107 (cond
1108 ((or (and (stringp element)
1109 (string-match (wesnoth-element-opening) element))
1110 (looking-at (wesnoth-element-opening)))
1111 (setq first-element (match-string-no-properties 0 element))
1112 (when (wesnoth-search-for-matching-tag
1113 'search-forward-regexp (wesnoth-element-closing) 'point-max
1114 (and (stringp element) 1))
1115 (beginning-of-line)
1116 (if (and (string-match wesnoth-preprocessor-opening-regexp
1117 first-element)
1118 (looking-at (wesnoth-element-closing)))
1119 (when (string= (match-string-no-properties 0)
1120 (cdr (assoc first-element
1121 '(("#define " . "#enddef")
1122 ("#ifndef " . "#endif")
1123 ("#ifdef " . "#endif")))))
1124 (setq target (point)))
1125 (setq target (point)))))
1126 ((or (and (stringp element)
1127 (string-match (wesnoth-element-closing) element))
1128 (looking-at (wesnoth-element-closing)))
1129 (end-of-line)
1130 (setq first-element (match-string-no-properties 0 element))
1131 (when (wesnoth-search-for-matching-tag
1132 'search-backward-regexp (wesnoth-element-opening)
1133 'wesnoth-wml-start-pos (and (stringp element) -1))
1134 (if (and (string-match wesnoth-preprocessor-closing-regexp
1135 first-element)
1136 (looking-at (wesnoth-element-opening)))
1137 (progn
1138 (when (or (and (string= "#enddef" first-element)
1139 (string= "#define "
1140 (match-string-no-properties
1141 0)))
1142 (and (string= "#endif" first-element)
1143 (string-match
1144 "#ifn?def "
1145 (match-string-no-properties
1146 0))))
1147 (setq target (point))))
1148 (setq target (point)))))
1150 (search-backward-regexp (wesnoth-element-opening) (point-min) t)
1151 (setq target (point)))))
1152 (if target
1153 (progn
1154 (goto-char target)
1155 (back-to-indentation))
1156 (when (interactive-p)
1157 (message "%s" "Tag does not appear to be matched")))))
1159 ;;; Indentation
1160 (defun wesnoth-wml-start-pos ()
1161 "Determine the position of `point' relative to where the actual WML begins.
1162 Return the likely starting position of the WML if it is found.
1163 Otherwise return nil."
1164 (save-excursion
1165 (goto-char (point-min))
1166 (when (search-forward-regexp (wesnoth-element) (point-max) t)
1167 (beginning-of-line)
1168 (point))))
1170 (defun wesnoth-first-column-indent-p (point)
1171 "Return non-nil if the current line should not be indented.
1173 POINT is the position in the buffer to check.
1174 CONTEXT represents the type of element which precedes the current element."
1175 (or (not (wesnoth-wml-start-pos))
1176 (<= (point) (wesnoth-wml-start-pos))
1177 (nth 3 (parse-partial-sexp
1178 (save-excursion (search-backward-regexp
1179 (wesnoth-element t) (point-min) t)
1180 (point))
1181 point))
1182 (and (looking-at wesnoth-preprocessor-regexp)
1183 wesnoth-indent-preprocessor-bol)))
1185 (defun wesnoth-indent ()
1186 "Indent the current line as WML."
1187 (save-excursion
1188 (beginning-of-line)
1189 (let* ((cur-indent 0)
1190 (context-data (wesnoth-determine-context (point)))
1191 (context (car context-data))
1192 (ref-indent (cdr context-data)))
1193 (unless (wesnoth-first-column-indent-p (point))
1194 (cond
1195 ((eq context 'opening)
1196 (if (and (looking-at "^[\t ]*#else")
1197 (not wesnoth-indent-preprocessor-bol))
1198 (setq cur-indent ref-indent)
1199 (if (or (and wesnoth-indent-savefile
1200 (or (looking-at "[\t ]*{NEXT ")
1201 (and (not
1202 (looking-at (wesnoth-element-closing t)))
1203 (not (looking-at "[\t ]*{NEXT ")))))
1204 (looking-at (wesnoth-element-opening t))
1205 (looking-at "[\t ]*{FOREACH "))
1206 (setq cur-indent (+ ref-indent wesnoth-base-indent))
1207 (setq cur-indent ref-indent))))
1208 ((eq context 'closing)
1209 (if (and (looking-at "^[\t ]*#else")
1210 (not wesnoth-indent-preprocessor-bol))
1211 (setq cur-indent (- ref-indent wesnoth-base-indent))
1212 (if (or (looking-at
1213 (concat "^[\t ]*\\(\\[/\\|\\#enddef"
1214 (if (not wesnoth-indent-preprocessor-bol)
1215 "\\|#endif"
1217 "\\)"))
1218 (and (not wesnoth-indent-savefile)
1219 (not (looking-at (wesnoth-element-opening t)))
1220 (not (looking-at "[\t ]*{FOREACH "))))
1221 (setq cur-indent (- ref-indent wesnoth-base-indent))
1222 (setq cur-indent ref-indent))))))
1223 (indent-line-to (max cur-indent 0))))
1224 (when (> (save-excursion (back-to-indentation) (point))
1225 (point))
1226 (back-to-indentation)))
1228 (defun wesnoth-within-define (position)
1229 "Determine whether point is currently inside a #define block.
1230 POSITION is the initial cursor position."
1231 (save-match-data
1232 (let ((depth 0)
1233 (defblocks (or wesnoth-define-blocks
1234 (wesnoth-find-macro-definitions))))
1235 (unless (equal (car defblocks) 'none)
1236 (dolist (element defblocks)
1237 (when (= (cadr (sort (append (mapcar 'marker-position
1238 (cadr element))
1239 (list position)) '>)) position)
1240 (setq depth (max (car element) depth)))))
1241 depth)))
1243 (defun wesnoth-find-macro-definitions ()
1244 "Return information regarding positioning of macro definitions."
1245 (save-excursion
1246 (goto-char (point-min))
1247 (let ((depth 0)
1248 openings cache)
1249 (while (search-forward-regexp "#define\\|#enddef" (point-max) t)
1250 (and (string= (match-string 0) "#define") (beginning-of-line))
1251 (setq depth
1252 (if (string= (match-string 0) "#define")
1253 (progn
1254 (add-to-list 'openings (point-marker))
1255 (1+ depth))
1256 (if openings
1257 (progn
1258 (add-to-list 'cache
1259 (list depth (list (car openings)
1260 (point-marker))))
1261 (setq openings (cdr openings))
1262 (1- depth))
1263 depth)))
1264 (end-of-line))
1265 (or cache (list 'none)))))
1267 (defun wesnoth-indent-region (start end)
1268 "Indent the region from START to END.
1269 Creates and destroys a cache of macro definition details as necessary."
1270 (interactive "r")
1271 (unwind-protect
1272 (save-excursion
1273 (goto-char end)
1274 (setq end (point-marker))
1275 (goto-char start)
1276 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1277 (or (bolp) (forward-line 1))
1278 (while (< (point) end)
1279 (if (looking-at "^[\t ]*$")
1280 (indent-line-to 0)
1281 (funcall indent-line-function))
1282 (forward-line 1)))
1283 (setq wesnoth-define-blocks nil)))
1285 (defun wesnoth-determine-context (position)
1286 "Determine the type of the last relevant element.
1287 POSITION is the buffer position of the element for which to
1288 determine the context."
1289 (save-excursion
1290 (let* ((elements (concat (substring (wesnoth-element t)
1291 0 (- (length (wesnoth-element t)) 2))
1292 "\\|{FOREACH .+}\\|{NEXT .+}\\)"))
1293 (match (or
1294 (and (search-backward-regexp
1295 elements (point-min) t)
1296 (progn
1297 (while
1298 (save-match-data
1299 (looking-at "^[\t ]*\\[[^/].+\\]\\[/.+\\]"))
1300 (search-backward-regexp elements
1301 (point-min) t))
1303 (match-string 1))
1304 ""))
1305 (depth (wesnoth-within-define position)))
1306 (while (and (wesnoth-wml-start-pos)
1307 (> (wesnoth-within-define (point)) depth)
1308 (not (= (point) (wesnoth-wml-start-pos))))
1309 (search-backward-regexp elements
1310 (wesnoth-wml-start-pos) t)
1311 (setq match (match-string 1)))
1312 (when (and (wesnoth-wml-start-pos)
1313 (= (point) (wesnoth-wml-start-pos))
1314 (= depth 0)
1315 (string-match "#define" match))
1316 ;; Found nothing of use; reset match and assume top-level tag.
1317 (setq match ""))
1318 (cond
1319 ((string-match (concat "\\[/\\|#enddef"
1320 (if (not wesnoth-indent-preprocessor-bol)
1321 "\\|#endif"
1322 ""))
1323 match)
1324 (cons 'closing (current-indentation)))
1325 ((string-match "{NEXT " match)
1326 (cons 'closing (if wesnoth-indent-savefile
1327 (- (current-indentation) wesnoth-base-indent)
1328 (current-indentation))))
1329 ((string-match (concat "\\[[^/]?\\|#define\\|{FOREACH "
1330 (if (not wesnoth-indent-preprocessor-bol)
1331 "\\|#ifn?def \\|#else"
1332 "")) match)
1333 (cons 'opening (current-indentation)))))))
1335 (defun wesnoth-newline-and-indent (&optional indent)
1336 "Indent both the current line and the newline created.
1337 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1338 performed.
1340 If the optional argument, INDENT is non-nil, force indentation to
1341 be performed."
1342 (interactive)
1343 (wesnoth-newline)
1344 (when (or wesnoth-auto-indent-flag indent)
1345 (wesnoth-indent)))
1347 ;;; WML checks
1348 (defun wesnoth-check-element-type (position &optional exact)
1349 "Determine the context of the element.
1350 POSITION is the position of the element in the list. If EXACT is
1351 non-nil, a more accurate, yet slower method is used. This is
1352 enabled when the attempt to match initially fails."
1353 (let ((parent (save-match-data (car (wesnoth-parent-tag exact))))
1354 (result '()))
1355 (if (or (stringp parent) (null parent))
1356 (setq result (member (match-string-no-properties 1)
1357 (nth position (gethash parent
1358 wesnoth-tag-hash-table))))
1359 (member (match-string-no-properties 1)
1360 (mapc
1361 '(lambda (x)
1362 (let ((value (nth position (cdr x))))
1363 (and value (mapc '(lambda (y)
1364 (setq result (cons y result)))
1365 value))))
1366 (or wesnoth-tmp-tag-data (wesnoth-refresh-wml-data)))))
1367 ;; If unsuccessful and more accurate checking is available, use it.
1368 ;; Otherwise, return the result found.
1369 (if (and wesnoth-allow-accurate-validation (not exact) (not result))
1370 (wesnoth-check-element-type position t)
1371 result)))
1373 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
1374 (defun wesnoth-line-number-at-pos (&optional pos)
1375 "Return (narrowed) buffer line number at position POS.
1376 If POS is nil, use current buffer location.
1377 Counting starts at (point-min), so the value refers
1378 to the contents of the accessible portion of the buffer."
1379 (let ((opoint (or pos (point))) start)
1380 (save-excursion
1381 (goto-char (point-min))
1382 (setq start (point))
1383 (goto-char opoint)
1384 (forward-line 0)
1385 (1+ (count-lines start (point))))))
1387 (defun wesnoth-check-output (buffer format-string &rest args)
1388 "Output the string as passed to `format'.
1389 BUFFER is the buffer to output the result.
1390 FORMAT-STRING is the string as the first argument of `format'.
1391 ARGS is any additional data required by `format' to handle FORMAT-STRING."
1392 (save-excursion
1393 (let ((lnap (wesnoth-line-number-at-pos))
1394 (source-buffer (buffer-name)))
1395 (set-buffer buffer)
1396 (let ((buffer-read-only nil))
1397 (insert (apply 'format (concat "%s: %d: " format-string "\n")
1398 source-buffer lnap args))))))
1400 (defun wesnoth-extract-macro-details (macro-arguments)
1401 "Return a list of all macros in MACRO-ARGUMENTS."
1402 (when macro-arguments
1403 (let ((results '()))
1404 (dolist (macro (split-string macro-arguments "[{}][\t ]*"))
1405 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
1406 macro)
1407 (add-to-list 'results (match-string-no-properties 1 macro))))
1408 results)))
1410 (defmacro wesnoth-check-process (format-string &rest args)
1411 "Output to buffer where requested and position overlays as required.
1412 FORMAT-STRING is the string to pass as the first argument to
1413 `format' for the error. ARGS is a list of arguments required by
1414 FORMAT-STRING."
1415 `(progn
1416 (wesnoth-check-output outbuf ,format-string ,@args)
1417 (wesnoth-place-overlay (match-beginning 0) (match-end 0))))
1419 (defmacro wesnoth-overlay-at-pos-p (position)
1420 "Return non-nil when there is an overlay at POSITION."
1421 `(and (overlays-at (goto-char ,position))
1422 (overlay-get (car (overlays-at (point))) 'wesnoth-error)
1423 (overlay-start (car (overlays-at (point))))))
1425 (defmacro wesnoth-locate-warning (string start end)
1426 "Search for STRING and move to the warning in the given direction.
1427 Searching starts from `point' and will wrap from START if no
1428 match was found. STRING is a form to locate the warning in the
1429 required direction. START is the start of the region searched.
1430 END is the end of the region searched."
1431 `(let ((target nil))
1432 (save-excursion
1433 (cond
1434 ((setq target (wesnoth-overlay-at-pos-p ,string)))
1435 ((setq target (wesnoth-overlay-at-pos-p ,start)))
1436 ((and (not (= (goto-char ,string) ,end))
1437 (setq target (wesnoth-overlay-at-pos-p (point)))))))
1438 target))
1440 (defun wesnoth-forward-warning ()
1441 "Move to the next warning."
1442 (interactive)
1443 (let ((target
1444 (if (fboundp 'next-overlay-change)
1445 (save-excursion
1446 (end-of-line)
1447 (wesnoth-locate-warning (next-overlay-change (point))
1448 (point-min)
1449 (point-max)))
1450 (wesnoth-target-position '< '>))))
1451 (if target
1452 (goto-char target)
1453 (message "%s" "No warnings found"))))
1455 (defun wesnoth-backward-warning ()
1456 "Move to the previous warning."
1457 (interactive)
1458 (let ((target
1459 (if (fboundp 'previous-overlay-change)
1460 (save-excursion
1461 (beginning-of-line)
1462 (wesnoth-locate-warning (1- (previous-overlay-change (point)))
1463 (point-max)
1464 (point-min)))
1465 (wesnoth-target-position '> '< t))))
1466 (if target
1467 (goto-char target)
1468 (message "%s" "No warnings found"))))
1470 (defun wesnoth-target-position (predicate search &optional lastp)
1471 "Return the target marker position.
1472 PREDICATE is the function to use to sort
1473 `wesnoth-warning-markers'. SEARCH must be a function which
1474 returns non-nil when the match is correct. If LASTP is non-nil,
1475 swap the order of the sorted positions when attempting to
1476 fallback."
1477 (let ((positions (sort (mapcar 'marker-position wesnoth-warning-markers)
1478 predicate)))
1479 (or (catch 'pos
1480 (dolist (position positions)
1481 (when (funcall search position (point))
1482 (throw 'pos position))))
1483 (car positions))))
1485 (defun wesnoth-place-overlay (start end)
1486 "Place overlay in the region and apply necessary properties.
1487 START is the start of the region to place the overlay. END is
1488 the end of the region to place the overlay."
1489 (if (fboundp 'overlay-put)
1490 (let ((overlay (make-overlay start end)))
1491 (overlay-put overlay 'wesnoth-error t)
1492 (overlay-put overlay 'face 'wesnoth-warning-face))
1493 (add-to-list 'wesnoth-warning-markers (save-excursion
1494 (goto-char start)
1495 (point-marker)))))
1497 (defun wesnoth-kill-block (arg)
1498 "Kill ARG blocks at point."
1499 (interactive "p")
1500 (save-excursion
1501 (while (> arg 0)
1502 (let ((kill-whole-line t))
1503 (while (and (looking-at "[\t ]*$") (= (forward-line 1) 0)))
1504 (if (looking-at "[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1505 #ifn?def\\)")
1506 (kill-region (point)
1507 (save-excursion
1508 (wesnoth-jump-to-matching)
1509 (forward-line 1)
1510 (point)))
1511 (kill-line))
1512 (setq arg (1- arg)))
1513 (wesnoth-indent))))
1515 (defun wesnoth-mark-block ()
1516 "Mark the block at point."
1517 (interactive)
1518 (save-excursion
1519 (while (and (save-excursion (beginning-of-line)
1520 (looking-at "[\t ]*$"))
1521 (= (forward-line 1) 0)))
1522 (when (looking-at "[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1523 #ifn?def\\)")
1524 (push-mark (save-excursion
1525 (wesnoth-jump-to-matching)
1526 (end-of-line)
1527 (point))))))
1529 (defun wesnoth-down-list (&optional arg)
1530 "Move forward down ARG levels of elements.
1531 If ARG is not specified, move forward down one level."
1532 (interactive "p")
1533 (unless arg
1534 (setq arg 1))
1535 (when (looking-at "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|\
1536 #ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)")
1537 (forward-char 1))
1538 (let ((target nil))
1539 (save-excursion
1540 (when (looking-at "\\(\\w\\|_\\)+[\t ]*=")
1541 (if (and (search-forward-regexp (wesnoth-element) (point-max) t)
1542 (progn (beginning-of-line)
1543 (looking-at (wesnoth-element-opening t))))
1544 (setq target (point))
1545 (message "%s" "Innermost level"))))
1546 (when target (goto-char target)))
1547 (while (> arg 0)
1548 (when (> (save-excursion (wesnoth-jump-to-matching) (point)) (point))
1549 (search-forward-regexp
1550 "\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define\\|#ifn?def\\|\\(\\w\\|_\\)+[\t ]*=\\)"
1551 (save-excursion
1552 (wesnoth-jump-to-matching)
1553 (point))
1555 (setq arg (1- arg)))
1556 (back-to-indentation))
1558 (defun wesnoth-backward-up-list (&optional arg)
1559 "Move backward up ARG levels of elements.
1560 If ARG is not specified, move backward up one level."
1561 (interactive "p")
1562 (unless arg
1563 (setq arg 1))
1564 (let ((parent nil))
1565 (while (> arg 0)
1566 (setq parent (cdr (wesnoth-parent-tag)))
1567 (if (numberp parent)
1568 (progn
1569 (goto-char parent)
1570 (setq arg (1- arg)))
1571 (message "%s" "Outermost level")
1572 (setq arg 0))
1573 (when (numberp parent)
1574 (back-to-indentation)))))
1576 (defun wesnoth-forward-list (arg)
1577 "Move to forward ARG elements at the current depth.
1578 If ARG is not specifed, move forward one element."
1579 (interactive "p")
1580 (while (and (save-excursion (beginning-of-line)
1581 (looking-at "[\t ]*$"))
1582 (= (forward-line 1) 0)))
1583 (when (save-excursion (beginning-of-line)
1584 (looking-at (wesnoth-element-opening)))
1585 (wesnoth-jump-to-matching)
1586 (setq arg (1- arg)))
1587 (let ((revert-target (point)))
1588 (while (> arg 0)
1589 (beginning-of-line)
1590 (unless (looking-at (wesnoth-element-closing))
1591 (wesnoth-jump-to-matching))
1592 (end-of-line)
1593 (if (and (search-forward-regexp (wesnoth-element) (point-max) t)
1594 (save-excursion (beginning-of-line)
1595 (looking-at (wesnoth-element-opening))))
1596 (setq revert-target (point))
1597 (goto-char revert-target)
1598 (message "%s" "End of block"))
1599 (setq arg (1- arg))))
1600 (when (save-excursion (beginning-of-line)
1601 (looking-at (wesnoth-element-opening)))
1602 (wesnoth-jump-to-matching))
1603 (end-of-line))
1605 (defun wesnoth-backward-list (arg)
1606 "Move to backward ARG elements at the current depth.
1607 If ARG is not specifed, move backward one element."
1608 (interactive "p")
1609 (while (and (save-excursion (beginning-of-line) (looking-at "[\t ]*$"))
1610 (= (forward-line -1) 0)))
1611 (when (save-excursion (beginning-of-line)
1612 (looking-at (wesnoth-element-closing)))
1613 (wesnoth-jump-to-matching)
1614 (setq arg (1- arg)))
1615 (let ((revert-target (point)))
1616 (while (> arg 0)
1617 (beginning-of-line)
1618 (unless (looking-at (wesnoth-element-opening))
1619 (wesnoth-jump-to-matching))
1620 (if (and (search-backward-regexp (wesnoth-element) (point-min) t)
1621 (save-excursion (beginning-of-line)
1622 (looking-at (wesnoth-element-closing))))
1623 (setq revert-target (point))
1624 (goto-char revert-target)
1625 (message "%s" "Beginning of block"))
1626 (setq arg (1- arg))))
1627 (when (looking-at (wesnoth-element-closing))
1628 (wesnoth-jump-to-matching)))
1630 (defun wesnoth-check-wml (&optional preserve-buffer)
1631 "Perform context-sensitive analysis of WML-code.
1632 If PRESERVE-BUFFER is non-nil, the contents of *WML* will be
1633 maintained through successive calls."
1634 (interactive)
1635 ;; Temporarily cache all tag-data.
1636 (setq wesnoth-tmp-tag-data (wesnoth-refresh-wml-data))
1637 (wesnoth-update-project-information)
1638 (if (fboundp 'delete-overlay)
1639 (dolist (overlay (overlays-in (point-min) (point-max)))
1640 (if (eq 'wesnoth-warning-face (overlay-get overlay 'face))
1641 (delete-overlay overlay)))
1642 (setq wesnoth-warning-markers nil))
1643 (when (= 0 (hash-table-count wesnoth-tag-hash-table))
1644 (error "WML data not available; unable to generate report"))
1645 (setq wesnoth-define-blocks (wesnoth-find-macro-definitions))
1646 (let ((unmatched '())
1647 (source-buffer (buffer-name))
1648 (outbuf (get-buffer-create "*WML*"))
1649 (last-match-pos 1)
1650 (details nil)
1651 (foreach '())
1652 (map (make-sparse-keymap)))
1653 (define-key map (kbd "q") 'bury-buffer)
1654 (save-excursion
1655 (set-buffer outbuf)
1656 (use-local-map map)
1657 (let ((buffer (buffer-name))
1658 (buffer-read-only nil))
1659 (if preserve-buffer
1660 (goto-char (point-max))
1661 (erase-buffer))
1662 (message (format "Checking %s..." source-buffer))))
1663 (save-excursion
1664 (goto-char (point-min))
1665 (while (setq details (wesnoth-find-next
1666 '(tag-opening tag-closing preprocessor attribute
1667 macro)))
1668 (save-excursion
1669 (goto-char (match-beginning 0))
1670 (cond ((nth 3 (parse-partial-sexp last-match-pos (point)))
1671 nil)
1672 ((eq (car details) 'macro)
1673 (dolist (macro (save-match-data
1674 (wesnoth-extract-macro-details
1675 (match-string-no-properties 0))))
1676 (unless (assoc macro
1677 (wesnoth-merge-macro-data
1678 wesnoth-macro-data
1679 (wesnoth-macro-additions)
1680 wesnoth-local-macro-data
1681 (wesnoth-macro-arguments)))
1682 (wesnoth-check-process "Unknown macro: '%s'"
1683 macro)))
1684 (save-match-data
1685 (when
1686 (looking-at
1687 "{\\(FOREACH\\|NEXT\\).*[\t ]+\
1688 \\(\\(?:\\w\\|_\\)+\\)}")
1689 (if (string= (match-string-no-properties 1) "FOREACH")
1690 (setq foreach
1691 (cons (match-string-no-properties 2) foreach))
1692 (if (string= (match-string-no-properties 1) "NEXT")
1693 (progn
1694 (unless (string= (car foreach)
1695 (match-string-no-properties 2))
1696 (wesnoth-check-process
1697 (concat "NEXT does not match corresponding "
1698 "FOREACH: '%s' found; '%s' expected.")
1699 (match-string-no-properties 2)
1700 (car foreach)))
1701 (setq foreach (cdr foreach))))))))
1702 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
1703 (unless (wesnoth-check-element-type 0)
1704 (wesnoth-check-process
1705 "Tag not available in this context: '%s'"
1706 (match-string-no-properties 1)))
1707 (setq unmatched (cons (match-string-no-properties 1)
1708 unmatched)))
1709 ((looking-at
1710 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1711 "\\( \\(\\w\\|_\\)+\\)*"))
1712 (unless (match-string-no-properties 2)
1713 (wesnoth-check-process
1714 (concat "Preprocessor statement has no argument: "
1715 (match-string-no-properties 1))))
1716 (unless (string= (match-string-no-properties 1) "#undef")
1717 (setq unmatched (cons (match-string-no-properties 1)
1718 unmatched))))
1719 ((looking-at wesnoth-preprocessor-closing-regexp)
1720 (when (and unmatched
1721 (not (string-match
1722 (cdr (assoc (match-string-no-properties 1)
1723 '(("enddef" . "#define")
1724 ("endif" . "#ifn?def"))))
1725 (car unmatched))))
1726 (wesnoth-check-process
1727 "Preprocessor statement does not nest correctly"))
1728 (setq unmatched (cdr unmatched)))
1729 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)[\t ]*=\\(.+\\)?")
1730 (unless (wesnoth-check-element-type 1)
1731 (wesnoth-check-process
1732 "Key not available in this context: '%s'"
1733 (match-string-no-properties 1)))
1734 (unless (match-string 3)
1735 (wesnoth-check-process
1736 "Attribute has no value")))
1737 ((looking-at "[\t ]*#else")
1738 (unless (string-match "ifn?def" (car unmatched))
1739 (if (string= (car unmatched) "#define")
1740 (wesnoth-check-process "Expecting: '%s'"
1741 (car unmatched))
1742 (wesnoth-check-process "Expecting: '[/%s]'"
1743 (car unmatched)))))
1744 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1745 (when (and unmatched
1746 (not (string= (match-string-no-properties 1)
1747 (car unmatched))))
1748 (wesnoth-check-process
1749 "Expecting '%s'"
1750 (or (cdr (assoc (car unmatched)
1751 '(("#define" . "#enddef")
1752 ("#ifdef" . "#endif")
1753 ("#ifndef" . "#endif"))))
1754 (concat "[/" (car unmatched) "]"))))
1755 (setq unmatched (cdr unmatched)
1756 last-match-pos (point))))))
1757 (when foreach
1758 (dolist (var foreach)
1759 (wesnoth-check-process "Unmatched FOREACH: '%s'" var)))
1760 (when unmatched
1761 (dolist (element unmatched)
1762 (wesnoth-check-process "Unmatched element: '%s'" element))))
1763 (when (or (not preserve-buffer)
1764 (and preserve-buffer (not (cdr wesnoth-found-cfgs))))
1765 (save-excursion
1766 (setq wesnoth-define-blocks nil
1767 wesnoth-tmp-tag-data nil)
1768 (set-buffer outbuf)
1769 (toggle-read-only t)
1770 (let ((buffer (buffer-name))
1771 (buffer-read-only nil))
1772 (display-buffer outbuf t)
1773 (let ((warnings (- (wesnoth-line-number-at-pos
1774 (goto-char (point-max))) 1)))
1775 (insert (format (concat "\nCheck complete. %d warning"
1776 (if (= warnings 1) "." "s.")) warnings)))
1777 (message (format "Checking %s...done" source-buffer)))))))
1779 ;; TODO: This will not load dependant files in the right order. Need to
1780 ;; process from _main.cfg and similar.
1781 (defun wesnoth-check-directory (dir)
1782 "Check all WML in DIR.
1783 Where DIR is a directory containing WML files."
1784 (interactive "DDirectory: ")
1785 (setq wesnoth-found-cfgs nil)
1786 (when (member (mapcar 'buffer-name (buffer-list)) "*WML*")
1787 (kill-buffer "*WML*"))
1788 (wesnoth-fetch-all-dirs dir)
1789 (let ((tmp-cfgs wesnoth-found-cfgs))
1790 (while tmp-cfgs
1791 (find-file (car wesnoth-found-cfgs))
1792 (kill-buffer)
1793 (setq tmp-cfgs (cdr tmp-cfgs))))
1794 (save-excursion
1795 (while wesnoth-found-cfgs
1796 (find-file (car wesnoth-found-cfgs))
1797 (wesnoth-check-wml t)
1798 (kill-buffer)
1799 (setq wesnoth-found-cfgs (cdr wesnoth-found-cfgs)))))
1801 (defun wesnoth-file-cfg-p (file)
1802 "Return non-nil if FILE has a '.cfg' extension."
1803 (and (not (file-directory-p file)) (string-match "\\.cfg$" file)))
1805 (defun wesnoth-fetch-all-dirs (dir)
1806 "Retrieve a list of subdirectories to scan.
1807 DIR is the directory to check."
1808 (let ((dirs-to-scan (wesnoth-files-in-dir dir)))
1809 (while dirs-to-scan
1810 (setq dirs-to-scan (append (wesnoth-files-in-dir (pop dirs-to-scan))
1811 dirs-to-scan)))))
1813 (defun wesnoth-files-in-dir (dir)
1814 "Add cfgs to `wesnoth-files-in-dir'.
1815 Returns a list of sub-directories in DIR."
1816 (let ((cfgs (wesnoth-cfg-files-in-dir dir)))
1817 (when cfgs
1818 (setq wesnoth-found-cfgs (append cfgs wesnoth-found-cfgs))))
1819 (let ((dirs '()))
1820 (dolist (file (directory-files dir t))
1821 (unless (string-match "^\\..*" (file-name-nondirectory file))
1822 (cond ((file-directory-p file)
1823 (add-to-list 'dirs file))
1824 ((wesnoth-file-cfg-p file)
1825 (add-to-list 'wesnoth-found-cfgs file)))))
1826 dirs))
1828 (defun wesnoth-cfg-files-in-dir (dir)
1829 "Return all cfg files in DIR."
1830 (let ((result '()))
1831 (dolist (file (directory-files dir t))
1832 (and (wesnoth-file-cfg-p file)
1833 (add-to-list 'result file)))
1834 result))
1837 ;;; wesnoth-mode
1838 (define-derived-mode wesnoth-mode fundamental-mode "wesnoth-mode"
1839 "Major mode for editing WML."
1840 (kill-all-local-variables)
1841 (use-local-map wesnoth-mode-map)
1842 (setq major-mode 'wesnoth-mode)
1843 (setq mode-name "WML")
1844 (set-syntax-table wesnoth-syntax-table)
1845 (set (make-local-variable 'outline-regexp) "[\t ]*#define")
1846 (set (make-local-variable 'comment-start) "#")
1847 (set (make-local-variable 'indent-line-function) 'wesnoth-indent)
1848 (set (make-local-variable 'indent-region-function) 'wesnoth-indent-region)
1849 (set (make-local-variable 'wesnoth-warning-markers) '())
1850 (set (make-local-variable 'font-lock-defaults)
1851 '(wesnoth-font-lock-keywords
1852 nil t nil nil
1853 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords)))
1854 (setq indent-tabs-mode nil)
1855 (easy-menu-add wesnoth-menu wesnoth-mode-map)
1856 (wesnoth-refresh-wml-data)
1857 (wesnoth-update-project-information)
1858 (run-hooks 'wesnoth-mode-hook))
1860 (provide 'wesnoth-mode)
1862 ;;; wesnoth-mode.el ends here