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