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,
22 ;; wesnoth-mode is a major mode for Emacs which assists in the editing
23 ;; of Wesnoth Markup Language (WML) files. Currently, this major-mode
24 ;; features syntax highlighting support, automatic indentation,
25 ;; context-sensitive completion and WML checking.
28 ;; Add the following to your .emacs:
29 ;; (add-to-list 'load-path "path/to/wesnoth-mode")
30 ;; (autoload 'wesnoth-mode "wesnoth-mode" "Major mode for editing WML." t)
32 ;; (add-to-list 'auto-mode-alist '("\\.cfg\\'" . wesnoth-mode))
33 ;; to automatically load wesnoth-mode for all files ending in '.cfg'.
37 ;; * Fixed some errors produced when wesnoth-mode.el is byte-compiled.
38 ;; * Improve detection and position of inserted closing tags in some
40 ;; * Improved context detection for completion in some circumstances.
41 ;; * Read `wesnoth-addition-file' as needed; M-x wesnoth-update no longer
43 ;; * `wesnoth-indent-preprocessor-bol' has been re-introduced to control
44 ;; whether preprocessor statements are indented to the beginning of the line
46 ;; * Many minor bug fixes.
48 ;; * Improve performance when inserting missing elements. Support for
49 ;; searching for missing elements over a region has been removed;
50 ;; `narrow-to-region' can be used to provide the functionality when
52 ;; * All warnings found when checking WML are now underlined in the buffer.
53 ;; * Next and previous warning can be jumped to using C-c C-f (or C-x `) and
54 ;; C-c C-b, respectively.
55 ;; * Any macro arguments are now prompted for and inserted when performing
56 ;; completing, as suggested by fabi.
57 ;; * Improved handling of completion; no longer prompt when no completion
58 ;; found, as suggested by uzytkownik.
59 ;; * Added indentation for FOREACH, as suggested by fabi.
60 ;; * Several bugs and inconsistencies corrected.
62 ;; * Major performance improvements to indentation and WML checking.
63 ;; * Fixed a bug where nesting could break when inserting multiple elements
64 ;; near the last element in a buffer.
65 ;; * Fixed a bug where attributes immediately within #ifn?def were always
66 ;; reported to be illegal.
67 ;; * Fixed a bug where tags immediately within #ifn?def were always legal.
68 ;; * Fixed a bug where when inserting missing tags, scanning would only be
69 ;; performed up to point.
70 ;; * Fixed a bug when jumping between preprocessor statements.
72 ;; * Completion history available is now specific to wesnoth-mode.
73 ;; * Added binding to explicitly update macro information from the current
75 ;; * Significantly improved performance of completion and WML checking.
76 ;; * Improved performance for inserting missing tags.
77 ;; * Fixed a bug where #ifdef was never matched when checking WML.
78 ;; * Added completion for preprocessor statements.
79 ;; * Improved macro completion and checking.
81 ;; * Added support for Xemacs.
82 ;; * WML checking is now context sensitive; checks attributes and macros.
83 ;; * WML checks are now always performed on the entire buffer, with results
84 ;; displayed in a temporary buffer.
85 ;; * Context-sensitive completion for attributes and tags.
86 ;; * Completion for built-in and project-specific macros.
87 ;; * Changed the following bindings:
88 ;; `wesnoth-insert-tag' - C-c e -> C-c t
89 ;; `wesnoth-jump-to-matching' - C-c m -> C-c o
90 ;; `wesnoth-check-structure' -> `wesnoth-check-wml' - C-c c
91 ;; * Added the following bindings:
92 ;; `wesnoth-complete-attribute' - C-c a
93 ;; `wesnoth-complete-macro' - C-c m
94 ;; `wesnoth-complete-tag' - C-c t
95 ;; * Removed the following bindings:
96 ;; `wesnoth-check-tag-names' - C-c n
97 ;; * Removed `wesnoth-check-tag-names'. Replaced by `wesnoth-check-wml'.
98 ;; * Completion for an incomplete attribute, tag, or macro at point is
101 ;; * Fixed support for GNU Emacs 21.
102 ;; * Added several new tags to `wesnoth-tags-list'.
103 ;; * Added M-TAB binding for `wesnoth-insert-tag'.
104 ;; * `wesnoth-insert-tag' now takes an optional numeric argument indicating
105 ;; how many blocks to wrap across instead of a region.
106 ;; * Support for `wesnoth-indent-preprocessor-bol' removed.
107 ;; * Fixed a bug in `wesnoth-insert-tag' and `wesnoth-insert-missing-closing'
108 ;; causing tags not to be inserted in the correct position.
109 ;; * Fixed highlighting of array indexes as tags.
111 ;; * Improved syntax-highlighting for macro calls.
112 ;; * Underscore is now treated as whitespace.
113 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
114 ;; * Point is now placed at the first non-whitespace character of the line,
115 ;; instead of the last.
116 ;; * Corrected minor indentation bugs.
117 ;; * Indenting across large regions is now much more efficient.
118 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
121 ;; * Now compatible with GNU Emacs 21.4.
122 ;; * Added support for several new tags.
123 ;; * Added menu entry for wesnoth-mode.
124 ;; * Significant speed increase to indentation.
125 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
126 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
128 ;; * Trailing whitespace is no longer created when creating a second
129 ;; consecutive newline.
130 ;; * Spurious newlines are no longer created when inserting a tag elements
133 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
134 ;; `wesnoth-element', `wesnoth-element-opening',
135 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
136 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
137 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
138 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
139 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
140 ;; * Fixed a bug in indentation where content was needed between elements pairs
141 ;; for indentation to work.
142 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
143 ;; `wesnoth-auto-indent-flag'.
144 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
145 ;; * Added indentation styles: `wesnoth-indent-default',
146 ;; `wesnoth-indent-withtags' which implement a a similar indentation
147 ;; style to the existing styles, however all preprocessor statements are
148 ;; indented to the first column.
149 ;; * Added support for several new tags.
150 ;; * Modified `wesnoth-newline' to behave more consistently.
151 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
152 ;; now leaves point at the beginning (when moving backward) or end (when
153 ;; moving forward) of the match.
154 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
155 ;; will now work on preprocessor statements. Will now warn if jump
156 ;; destination may not be correct (due to errors in WML structure).
157 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
158 ;; * `wesnoth-check-structure' can now be applied over an active region and
159 ;; now checks preprocessor statements for correct nesting.
160 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
161 ;; perform indentation by providing a prefix argument.
162 ;; * Indentation styles now leave point at the first non-whitespace character
164 ;; * `wesnoth-check-tag-names' now reports on success.
165 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
166 ;; * `outline-minor-mode' now works on macro definitions.
168 ;; * Base indent now defaults to 4.
169 ;; * Added support for #ifndef.
173 (require 'wesnoth-update
)
174 (require 'wesnoth-wml-data
)
176 (defconst wesnoth-mode-version
"1.3.4"
177 "The current version of `wesnoth-mode'.")
179 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
183 (defcustom wesnoth-auto-indent-flag t
184 "Non-nil means indent the current line upon creating a newline."
186 :group
'wesnoth-mode
)
188 (defcustom wesnoth-indent-preprocessor-bol t
189 "Whether to indent Preprocessor statements to the beginning of the line."
191 :group
'wesnoth-mode
)
193 (defcustom wesnoth-indent-savefile t
194 "Non-nil means to use the current indentation conventions.
195 If nil, use the old convention for indentation.
196 The current convention is all attributes are indented a level deeper
197 than their parent; in the past attributes were indented to the same
198 level as their parent.")
200 (defcustom wesnoth-base-indent
4
201 "The number of columns to indent WML."
203 :group
'wesnoth-mode
)
205 (defconst wesnoth-preprocessor-regexp
206 "[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
207 "Regular expression to match all preprocessor statements.")
209 (defconst wesnoth-preprocessor-opening-regexp
210 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
211 "Regular expression to match \"opening\" preprocessor statements.")
213 (defconst wesnoth-preprocessor-closing-regexp
214 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
215 "Regular expression to match \"closing\" preprocessor statements.")
217 (defvar wesnoth-define-blocks
'()
218 "Cache of all toplevel #define and #enddef pairs.")
220 (defvar wesnoth-history-list
'()
221 "History of inserted WML elements.")
223 (defvar wesnoth-warning-markers
'()
224 "Markers for warnings in the buffer.")
226 (defvar wesnoth-mode-hook nil
)
228 (defface wesnoth-warning-face
229 '((t (:underline
"tomato1")))
230 "Face to use for warnings in wesnoth-mode"
231 :group
'wesnoth-mode
)
233 (defvar wesnoth-mode-map
234 (let ((map (make-sparse-keymap)))
235 (define-key map
(kbd "C-M-a") 'wesnoth-backward-element
)
236 (define-key map
(kbd "C-M-e") 'wesnoth-forward-element
)
237 (define-key map
(kbd "C-m") 'wesnoth-newline
)
238 (define-key map
(kbd "C-j") 'wesnoth-newline-and-indent
)
239 (define-key map
(kbd "C-c C-c") 'wesnoth-check-wml
)
240 (define-key map
(kbd "C-c C-a") 'wesnoth-complete-attribute
)
241 (define-key map
(kbd "C-c C-t") 'wesnoth-complete-tag
)
242 (define-key map
(kbd "C-c C-p") 'wesnoth-complete-preprocessor
)
243 (define-key map
(kbd "C-c C-u") 'wesnoth-update-project-information
)
244 (define-key map
(kbd "M-TAB") 'wesnoth-complete-tag
)
245 (define-key map
(kbd "C-c C-m") 'wesnoth-complete-macro
)
246 (define-key map
(kbd "C-c C-o") 'wesnoth-jump-to-matching
)
247 (define-key map
(kbd "C-c C-f") 'wesnoth-forward-warning
)
248 (define-key map
(kbd "C-x `") 'wesnoth-forward-warning
)
249 (define-key map
(kbd "C-c C-b") 'wesnoth-backward-warning
)
250 (define-key map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
251 (define-key map
(kbd "TAB") 'wesnoth-indent-or-complete
)
253 "Keymap used in `wesnoth-mode'.")
255 (easy-menu-define wesnoth-menu wesnoth-mode-map
"Menu for wesnoth-mode"
257 ["Check WML" wesnoth-check-wml t
]
258 ["Indent or Complete" wesnoth-indent-or-complete t
]
259 ["Indent buffer" (lambda ()
261 (wesnoth-indent-region (point-min) (point-max))) t
]
262 ["Insert Tag" wesnoth-complete-tag t
]
263 ["Insert Attribute" wesnoth-complete-attribute t
]
264 ["Insert Macro" wesnoth-complete-macro t
]
265 ["Insert Preprocessor" wesnoth-complete-preprocessor t
]
266 ["Insert Missing Tag" wesnoth-insert-missing-closing t
]
267 ["Jump to Matching" wesnoth-jump-to-matching t
]
268 ["Update Macros" wesnoth-update-project-information t
]))
270 (defvar wesnoth-syntax-table
271 (let ((wesnoth-syntax-table (make-syntax-table)))
272 (modify-syntax-entry ?
# "<" wesnoth-syntax-table
)
273 (modify-syntax-entry ?
\" "\"" wesnoth-syntax-table
)
274 (modify-syntax-entry ?
= "." wesnoth-syntax-table
)
275 (modify-syntax-entry ?|
"w" wesnoth-syntax-table
)
276 (modify-syntax-entry ?_
"_" wesnoth-syntax-table
)
277 (modify-syntax-entry ?-
"_" wesnoth-syntax-table
)
278 (modify-syntax-entry ?.
"_" wesnoth-syntax-table
)
279 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
280 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
281 wesnoth-syntax-table
)
282 "Syntax table for `wesnoth-mode'.")
284 ;; Prevents automatic syntax-highlighting of elements which might be
285 ;; pre-processor statements.
286 (defvar wesnoth-syntactic-keywords
288 '("\\([\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\)" 1 "w"))
289 "Syntactic keywords for preprocessor statements within `wesnoth-mode'.")
291 (defvar wesnoth-font-lock-keywords
293 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face
)
294 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
295 font-lock-keyword-face
)
296 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
297 2 font-lock-function-name-face
)
298 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face
))
299 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face
))
300 '("}" . font-lock-function-name-face
)
301 '("[\t ]*\\(\\[/?[^$]\\(\\w\\|_\\)+\\]\\)" 1 font-lock-type-face
)
302 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face
)
303 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
304 1 font-lock-variable-name-face
))
305 "Syntax highlighting for `wesnoth-mode'.")
307 (defun wesnoth-element-closing (&optional limited
)
308 "Return the regexp to match a closing element.
309 If LIMITED is non-nil, return a regexp which matches only the
310 #enddef preprocessor."
311 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\]\\|"
314 "#end\\(?:def\\|if\\)")
317 (defun wesnoth-element-opening (&optional limited
)
318 "Return the regexp to match a closing element.
319 If LIMITED is non-nil, return a regexp which matches only the
320 #define preprocessor."
321 (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define "
327 (defun wesnoth-element (&optional limited
)
328 "Return the regexp to match a closing element.
329 If LIMITED is non-nil, return a regexp which matches only the
330 #define and #enddef preprocessors."
331 (concat "^[\t ]*\\(\\[[+/]?\\(\\w\\|_\\)+\\]?\\|"
334 (substring wesnoth-preprocessor-regexp
5))
337 (defun wesnoth-find-next (type)
338 "Find the next element of TYPE.
339 TYPE is a symbol representing an element type, or a list of
340 element types to find."
341 (let ((element (wesnoth-next-element)))
342 (while (and element
(if (listp type
)
343 (not (member (car element
) type
))
344 (not (eq (car element
) type
))))
345 (setq element
(wesnoth-next-element)))
346 (when (if (listp type
)
347 (member (car element
) type
)
348 (eq (car element
) type
))
351 (defun wesnoth-next-element ()
352 "Move to the next element in the buffer.
353 Return non-nil when an element is found. Otherwise, return nil."
356 (and (or (eolp) (looking-at "[}\t ]"))
357 (search-forward-regexp "[^}
360 (let ((details (wesnoth-element-type (point))))
362 (when (nth 2 details
)
363 (goto-char (nth 2 details
))
364 (while (nth 3 (parse-partial-sexp
365 (save-excursion (search-backward-regexp
366 (wesnoth-element t
) (point-min) t
)
369 (search-forward "\"" (point-max) t
))))
372 (defun wesnoth-element-type (point)
373 "Return details regarding the element at POINT.
374 A list is returned, the elements of the list represent the
375 following, respectively: A symbol representing the type of
376 element; the position of the start of the element and the
377 position of the end of the element. POINT must be at the start
379 (let ((element-matches
380 '(("{\\(.*?[/\]\\)+}" . nil
) ;; pathnames
381 ("{\\(\\w\\|_\\)+" . macro
)
382 ("\\[\\+?[^/]+?\\]" . tag-opening
)
383 ("\\[/.+?\\]" . tag-closing
)
384 ("\\(\\w\\|_\\)+[\t ]*=" . attribute
)
385 ("#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
390 (dolist (pair element-matches
)
391 (when (looking-at (car pair
))
392 (throw 'result
(list (cdr pair
)
394 (min (save-excursion (forward-line 1) (point))
395 (match-end 0)))))))))
397 (defun wesnoth-estimate-element-type (point)
398 "Return match data for a partial element at POINT."
401 (let ((element-matches
402 '(("{\\(.*?[/\]\\)+$" . nil
) ; pathnames
403 ("{\\(\\w\\|_\\)*$" . macro
)
404 ("\\[/\\(\\w\\|_\\)*$" . tag-closing
)
405 ("\\[\\+?\\(\\w\\|_\\)*$" . tag-opening
)
406 ("^[\t ]*\\(\\w\\|_\\)+$" . attribute
)
407 ("[\t ]*#\\(enddef\\|define \\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def \\)"
408 . nil
) ; not a partial match
409 ("[\t ]*#\\w*$" . preprocessor
))))
411 (dolist (pair element-matches
)
412 (when (looking-at (car pair
))
413 (throw 'result
(list (cdr pair
)
415 (match-end 0)))))))))
417 (defun wesnoth-guess-element-type (point)
418 "Return details for the the element near POINT.
419 Locate the start of the element before determining details.
420 BOUND is the limit to search backwards."
421 (let ((details (wesnoth-estimate-element-type point
))
422 (bound (save-excursion
426 (while (and (not (car details
)) (> point bound
))
427 (setq point
(1- point
)
428 details
(wesnoth-estimate-element-type point
)))
430 (>= (point) (nth 1 details
))
432 (<= (point) (nth 2 details
))
436 ;;; Insertion and completion
437 (defmacro wesnoth-element-completion
(completions prompt partial
439 "Process completion of COMPLETIONS, displaying PROMPT.
440 PARTIAL is the partial string on which to attempt completion.
441 If COMPLETEP is non-nil, do not prompt if no completion is found."
442 `(let* ((element (when ,partial
(try-completion ,partial
,completions
))))
443 (cond ((eq element t
)
445 ((and completep
(null element
))
447 ((and element
(eq (try-completion element
,completions
) t
))
449 ((> (length (all-completions (or element
"") ,completions
)) 1)
450 (completing-read ,prompt
,completions
452 'wesnoth-history-list
))
456 (defun wesnoth-active-parent-tag ()
457 "Return the name of the active parent tag.
458 Finds the relevant parent tag, ignoring any conditional tags."
460 (let ((parent (wesnoth-parent-tag)))
461 (while (and (car parent
) (string-match "else\\|then"
463 (goto-char (cdr parent
))
464 (setq parent
(wesnoth-parent-tag))
465 (when (string= (car parent
) "if")
466 (goto-char (cdr parent
))
467 (setq parent
(wesnoth-parent-tag))))
470 (defun wesnoth-parent-tag ()
471 "Return the name of the parent tag.
472 If the parent is a preprocessor statement, return non-nil.
473 If the element does not have a parent, return nil.
474 Otherwise, return a string containing the name of the parent tag."
476 (let ((start-point (point))
478 (when (save-excursion (> (point) (progn (back-to-indentation)
481 (while (and (> depth
0)
482 (search-backward-regexp (wesnoth-element t
)
484 (if (string-match "[\t ]*\\[/" (match-string 0))
485 (setq depth
(1+ depth
))
486 (setq depth
(1- depth
))))
490 (when (looking-at (wesnoth-element-opening))
491 (let ((parent (match-string-no-properties 1))
493 (if (or (string-match wesnoth-preprocessor-opening-regexp parent
)
494 ;; Check if we're immediately within a macro
495 (and (goto-char start-point
)
496 (search-backward-regexp "[}{]" (point-min) t
)
497 (string= (match-string 0) "{")
498 (goto-char start-point
)
499 (not (and (search-backward parent
(point-min) t
)
500 (search-backward-regexp "[}{]" (point-min) t
)
501 (string= (match-string 0) "{")))))
503 (cons (substring parent
1 (1- (length parent
))) position
))))))))
505 (defun wesnoth-partial-macro-p ()
506 "Return non-nil if point is in a partial macro."
509 (search-backward-regexp "{" (point-min) t
)
510 (while (search-forward-regexp "[{}]" (point-max) t
)
511 (if (string= (match-string 0) "{")
512 (setq opened
(1+ opened
))
513 (setq opened
(1- opened
))))
516 (defun wesnoth-indent-or-complete (&optional elements
)
517 "Indent or complete the line at point, depending on context.
518 ELEMENTS is the number of elements to wrap around if inserting
521 (or elements
(setq elements
0))
522 (let ((details (wesnoth-guess-element-type (point))))
524 ((eq (car details
) 'tag-opening
)
525 (wesnoth-complete-tag elements t
))
526 ((and (eq (car details
) 'macro
)
527 (wesnoth-partial-macro-p))
528 (wesnoth-complete-macro t
))
529 ((eq (car details
) 'preprocessor
)
530 (wesnoth-complete-preprocessor elements t
))
531 ((eq (car details
) 'tag-closing
)
532 ;; FIXME: Solve incorrect behaviour when partial closing is "[/"
534 (and (wesnoth-insert-missing-closing t
)
535 (delete-region (nth 1 details
)
536 (save-excursion (beginning-of-line) (point))))
539 ((eq (car details
) 'attribute
)
540 (wesnoth-complete-attribute t
))
544 (defun wesnoth-preprocessor-closed-p (preprocessor)
545 "Determine whether PREPROCESSOR has been closed.
546 PREPROCESSOR is a string matching the preprocessor statement to
549 (back-to-indentation)
550 (wesnoth-jump-to-matching preprocessor
)
552 (if (string= preprocessor
"#define ")
556 (defun wesnoth-complete-preprocessor (&optional elements completep
)
557 "Complete and insert the preprocessor at point.
558 ELEMENTS is the number of elements to wrap around.
559 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
561 (or elements
(setq elements
0))
562 (let* ((completions (wesnoth-emacs-completion-formats
563 '("define" "else" "ifdef" "ifndef"
564 "enddef" "endif" "undef")))
565 (partial (when completep
567 (back-to-indentation)
568 (when (looking-at "#\\(\\w*\\)$")
569 (match-string-no-properties 1)))))
570 (preprocessor (wesnoth-element-completion
571 completions
"Preprocessor: " partial completep
))
572 (details (wesnoth-guess-element-type (point)))
576 (unless (string= "#" (substring preprocessor
0 1))
577 (setq preprocessor
(concat "#" preprocessor
)))
578 (when (string-match "#\\(define\\|ifn?def\\|undef\\)" preprocessor
)
579 (setq preprocessor
(concat preprocessor
" ")))
581 (delete-region (nth 1 details
) (nth 2 details
)))
582 (wesnoth-preprocessor-closed-p preprocessor
)))))
587 (progn (search-backward
588 "#" (save-excursion (back-to-indentation)
593 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor
)
596 (wesnoth-insert-tag elements preprocessor
)
599 (wesnoth-insert-element-separately preprocessor
)))))
601 (defun wesnoth-macro-arguments ()
602 "Find any current macro arguments."
604 (depth (wesnoth-within-define (point))))
608 (search-backward-regexp
609 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\([\t ]*\\(\\w\\|_\\)+\\)*\\)"
611 (when (<= (wesnoth-within-define (point)) depth
)
614 (append (mapcar (lambda (macro)
617 (match-string-no-properties 1)))
619 (setq depth
(1- depth
)))))
622 (defun wesnoth-complete-macro (&optional completep
)
623 "Complete and insert the macro at point.
624 If COMPLETEP is non-nil, attempt to complete the macro at point."
626 (wesnoth-update-project-information)
627 (let* ((macro-information (wesnoth-merge-macro-data
629 (wesnoth-macro-additions)
630 wesnoth-local-macro-data
631 (wesnoth-macro-arguments)))
632 (completions (wesnoth-emacs-completion-formats
633 (mapcar 'car macro-information
)))
634 (details (wesnoth-guess-element-type (point)))
638 (eq (car details
) 'macro
)
639 (goto-char (cadr details
))
640 (looking-at "{\\(\\(\\w\\|_\\)*\\)")))
641 (match-string-no-properties 1)))
642 (macro (wesnoth-element-completion completions
"Macro: " partial
644 (args (cadr (assoc macro macro-information
))))
648 ;; Delete the region corresponding to the current macro.
649 (delete-region (nth 1 details
) (nth 2 details
))
650 (insert "{" macro
(if args
" }" "}")))
651 (wesnoth-insert-element-separately "{" macro
(if args
" }" "}")))
656 (let ((input (read-string (concat (car args
) ": "))))
657 (insert input
(if (and (cdr args
)
658 (not (string= input
"")))
660 (while (and (setq args
(cdr args
)) (not (string= input
"")))
661 (insert (setq input
(read-string (concat (car args
) ": ")))
662 (if (and (not (string= input
""))
665 (when (null args
) (forward-char 1)))))
667 (defun wesnoth-complete-attribute (&optional completep
)
668 "Insert the attribute at point.
669 If COMPLETEP is non-nil, attempt to complete the attribute at point."
671 (wesnoth-refresh-wml-data)
672 (let* ((details (save-excursion
673 (back-to-indentation)
674 (wesnoth-guess-element-type (point))))
675 (completions (save-excursion (when (nth 1 details
)
676 (goto-char (nth 1 details
)))
677 (wesnoth-build-completion 1)))
678 (partial (when completep
679 (when (save-excursion
680 (back-to-indentation)
681 (looking-at "\\(\\(\\w\\|_\\)+\\)"))
682 (match-string-no-properties 1))))
683 (attribute (wesnoth-element-completion completions
"Attribute: "
688 (delete-region (nth 1 details
) (nth 2 details
))
689 (insert attribute
"="))
690 (wesnoth-insert-element-separately attribute
691 (if (string-match "=" attribute
)
697 (defun wesnoth-complete-tag (&optional elements completep
)
698 "Complete and insert the tag at point.
699 ELEMENTS is the number of elements to wrap around.
700 If COMPLETEP is non-nil, attempt to complete tag at point."
702 (or elements
(setq elements
0))
703 (let* ((details (wesnoth-guess-element-type (point)))
704 (completions (save-excursion (and (nth 1 details
)
705 (goto-char (nth 1 details
)))
706 (wesnoth-build-completion 0)))
707 (partial (save-excursion
709 (eq (car details
) 'tag-opening
)
710 (goto-char (cadr details
))
711 (looking-at "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$"))
712 (match-string-no-properties 1))))
713 (tag (wesnoth-element-completion completions
"Tag: " partial
717 (wesnoth-jump-to-matching (concat "[" tag
"]"))
718 (back-to-indentation)
719 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
720 (string= tag
(match-string 1))))))
725 (delete-region (nth 1 details
) (nth 2 details
))
728 (wesnoth-insert-and-indent "[" tag
"]")
730 (wesnoth-insert-tag elements tag
)))
731 (wesnoth-insert-tag elements tag
)))
732 (or completep
(wesnoth-insert-tag elements
)))))
734 (defun wesnoth-build-completion (position)
735 "Create a new list for tag completion if necessary.
736 Rebuilding list is required for versions of GNU Emacs earlier
737 than 22. POSITION is the argument passed to `nth' for
740 (let ((parent (wesnoth-active-parent-tag))
741 (tag-data (wesnoth-refresh-wml-data)))
742 (wesnoth-emacs-completion-formats
743 (if (or (stringp parent
) (null parent
))
744 (nth position
(gethash parent wesnoth-tag-hash-table
))
745 (mapcar 'car tag-data
)))))
747 (defun wesnoth-emacs-completion-formats (candidates)
748 "Return the completions in the correct format for `emacs-major-version'.
749 CANDIDATES is a list of all possible completions."
750 (if (> emacs-major-version
21)
754 (dolist (tag candidates
)
755 (setq iter
(1+ iter
))
756 (setq tags
(append tags
(list (cons tag iter
)))))
759 (defun wesnoth-insert-tag (&optional elements tagname
)
760 "Insert the specified opening tag and it's matching closing tag.
761 Both the opening and closing tags will be placed on their own
762 lines with point positioned between them. Completion of tags at
763 the prompt uses `wesnoth-tags-list'.
765 ELEMENTS is specifies the number of following blocks which the
766 tag should wrap around.
768 TAGNAME is the name of the tag to be inserted."
771 (setq tagname
(completing-read "Tag: " (wesnoth-build-completion 0)
772 nil nil nil
'wesnoth-history-list
)))
773 (when (or (not elements
)
774 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
775 wesnoth-preprocessor-regexp
"\\)")))
777 (let ((start (save-excursion (forward-line -
1) (point)))
778 (end (unless (= elements
0)
779 ;; Work around some strange behaviour when the target is at the
780 ;; end of the buffer.
782 (goto-char (point-max))
784 (unless (looking-at "^[\t ]*$")
787 (wesnoth-nth-pair-position elements
))))
788 (if (string-match wesnoth-preprocessor-regexp tagname
)
789 (wesnoth-insert-element-separately tagname
)
790 (wesnoth-insert-element-separately "[" tagname
"]"))
793 (goto-char (marker-position end
))
794 (newline (if (string-match wesnoth-preprocessor-regexp tagname
) 1 2)))
795 (if (string-match wesnoth-preprocessor-opening-regexp tagname
)
796 (wesnoth-insert-element-separately
797 (if (string= tagname
"#define ")
800 (wesnoth-insert-element-separately "[/" tagname
"]"))
801 (indent-region start
(point) nil
))
806 (defun wesnoth-insert-element-separately (&rest strings
)
807 "Concatenate STRINGS and insert them on a line of their own."
808 (if (save-excursion (and (> (point) (progn (back-to-indentation) (point)))))
809 (if (save-excursion (forward-line 1) (looking-at "^[\t ]*$"))
816 (if (looking-at "^[\t ]*$")
819 (insert (apply 'concat strings
)))
821 (defun wesnoth-insert-missing-closing (&optional completep
)
822 "Insert the next expected closing element at point.
823 If COMPLETEP is non-nil, do not move forward a line when scanning
824 for the matching tag."
829 (when (and (null completep
)
830 (<= (point) (save-excursion (back-to-indentation) (point))))
831 (if (save-excursion (beginning-of-line)
832 (looking-at (wesnoth-element-opening)))
835 (save-excursion (beginning-of-line)
836 (looking-at (wesnoth-element-closing)))
838 (when (wesnoth-search-for-matching-tag
839 'search-backward-regexp
(wesnoth-element-opening) 'point-min
840 (and skip
(if completep nil
1)))
841 (setq match
(and (looking-at (wesnoth-element-opening))
842 (match-string-no-properties 1)))))
844 (if (string= (substring match
0 1) "[")
845 (wesnoth-insert-element-separately
846 "[/" (substring match
1 (1- (length match
))) "]")
847 (wesnoth-insert-element-separately
848 (cdr (assoc match
'(("#define " .
"#enddef")
849 ("#ifndef " .
"#endif")
850 ("#ifdef " .
"#endif")))))))
855 (defun wesnoth-insert-and-indent (&rest args
)
856 "Concatenate and insert the given string(s) before indenting.
858 ARGS is a list of strings to be inserted."
859 (insert (apply 'concat args
))
862 (defun wesnoth-newline (&optional indent
)
863 "Indent the current line and create a newline.
864 If `wesnoth-auto-indent-flag' is nil, indentation will not be
865 performed. Indentation can be forced by setting INDENT to
871 (when (and (or wesnoth-auto-indent-flag indent
)
872 (not (looking-at "^[\t ]*$")))
876 (defun wesnoth-navigate-element (repeat search-function bound
)
877 "Move point to the tag in the given direction REPEAT times.
879 SEARCH-FUNCTION is the symbol of the function for searching in
880 the required direction, with BOUND marking the furthest point to
882 (or repeat
(setq repeat
1))
884 (and (eq search-function
'search-forward-regexp
) (end-of-line))
885 (funcall search-function
(wesnoth-element-opening) bound t
)
886 (back-to-indentation)
887 (setq repeat
(1- repeat
))))
889 (defun wesnoth-nth-pair-position (count)
890 "Return `point' after COUNT number of matching element pairs.
891 COUNT is a positive number representing the number of balanced
892 pairs to move across.
893 `point' is returned as a marker object."
896 (if (> (point) (save-excursion (back-to-indentation) (point)))
900 ;; Currently looking-at target tag. Stop here to avoid
901 ;; incorrect nesting.
902 (unless (wesnoth-search-for-matching-tag
903 'search-forward-regexp
(wesnoth-element-closing) 'point-max
)
906 (and (> (setq count
(1- count
)) 0) (forward-line 1)))
912 (defun wesnoth-forward-element (repeat)
913 "Move point to the end of the next tag.
914 REPEAT is an optional numeric argument. If REPEAT is non-nil,
915 jump forward the specified number of tags."
918 (wesnoth-backward-element (abs repeat
))
919 (wesnoth-navigate-element repeat
'search-forward-regexp
(point-max))))
921 (defun wesnoth-backward-element (repeat)
922 "Move point to the beginning of the previous tag.
923 REPEAT is an optional numeric argument. If REPEAT is non-nil,
924 jump backward the specified number of tags."
927 (wesnoth-forward-element (abs repeat
))
928 (wesnoth-navigate-element repeat
'search-backward-regexp
(point-min))))
930 (defun wesnoth-search-for-matching-tag (search-function
931 search-string bound
&optional skip
)
932 "Search for the matching tag for the current line.
933 SEARCH-FUNCTION is the name of the function used to perform the search.
934 SEARCH-STRING is a string representing the matching tag type.
935 BOUND is the bound to be passed to the search function.
936 If SKIP is non-nil, skip the first element and continue from there."
938 (when (and (or (and (numberp skip
) (forward-line skip
))
939 (funcall search-function
(wesnoth-element) (funcall bound
) t
))
940 (or skip
(not (string-match search-string
(match-string 0)))))
941 (while (and (> depth
0)
942 (funcall search-function
(wesnoth-element)
944 (if (string-match search-string
(match-string 0))
945 (setq depth
(1- depth
))
946 (setq depth
(1+ depth
))))
949 (defun wesnoth-jump-to-matching (&optional element
)
950 "Jump point to the matching opening/closing tag.
951 ELEMENT is an element to find a match for."
958 ((or (and (stringp element
)
959 (string-match (wesnoth-element-opening) element
))
960 (looking-at (wesnoth-element-opening)))
961 (setq first-element
(match-string-no-properties 0 element
))
962 (when (wesnoth-search-for-matching-tag
963 'search-forward-regexp
(wesnoth-element-closing) 'point-max
964 (and (stringp element
) 1))
966 (if (and (string-match wesnoth-preprocessor-opening-regexp
968 (looking-at (wesnoth-element-closing)))
969 (when (string= (match-string-no-properties 0)
970 (cdr (assoc first-element
971 '(("#define " .
"#enddef")
972 ("#ifndef " .
"#endif")
973 ("#ifdef " .
"#endif")))))
974 (setq target
(point)))
975 (setq target
(point)))))
976 ((or (and (stringp element
)
977 (string-match (wesnoth-element-closing) element
))
978 (looking-at (wesnoth-element-closing)))
980 (setq first-element
(match-string-no-properties 0 element
))
981 (when (wesnoth-search-for-matching-tag
982 'search-backward-regexp
(wesnoth-element-opening)
983 'wesnoth-wml-start-pos
(and (stringp element
) -
1))
984 (if (and (string-match wesnoth-preprocessor-closing-regexp
986 (looking-at (wesnoth-element-opening)))
988 (when (or (and (string= "#enddef" first-element
)
990 (match-string-no-properties
992 (and (string= "#endif" first-element
)
995 (match-string-no-properties
997 (setq target
(point))))
998 (setq target
(point)))))
1000 (search-backward-regexp (wesnoth-element-opening) (point-min) t
)
1001 (setq target
(point)))))
1005 (back-to-indentation))
1006 (when (interactive-p)
1007 (message "%s" "Tag does not appear to be matched")))))
1010 (defun wesnoth-wml-start-pos ()
1011 "Determine the position of `point' relative to where the actual WML begins.
1012 Return the likely starting position of the WML if it is found.
1013 Otherwise return nil."
1015 (goto-char (point-min))
1016 (when (search-forward-regexp (wesnoth-element) (point-max) t
)
1020 (defun wesnoth-first-column-indent-p (point)
1021 "Return non-nil if the current line should not be indented.
1023 POINT is the position in the buffer to check.
1024 CONTEXT represents the type of element which precedes the current element."
1025 (or (not (wesnoth-wml-start-pos))
1026 (<= (point) (wesnoth-wml-start-pos))
1027 (nth 3 (parse-partial-sexp
1028 (save-excursion (search-backward-regexp
1029 (wesnoth-element t
) (point-min) t
)
1032 (and (looking-at wesnoth-preprocessor-regexp
)
1033 wesnoth-indent-preprocessor-bol
)))
1035 (defun wesnoth-indent ()
1036 "Indent the current line as WML."
1039 (let* ((cur-indent 0)
1040 (context-data (wesnoth-determine-context (point)))
1041 (context (car context-data
))
1042 (ref-indent (cdr context-data
)))
1043 (unless (wesnoth-first-column-indent-p (point))
1045 ((eq context
'opening
)
1046 (if (or (and wesnoth-indent-savefile
1047 (or (looking-at "[\t ]*{NEXT ")
1048 (and (not (looking-at (wesnoth-element-closing t
)))
1049 (not (looking-at "[\t ]*{NEXT ")))))
1050 (looking-at (wesnoth-element-opening t
))
1051 (looking-at "[\t ]*{FOREACH "))
1052 (setq cur-indent
(+ ref-indent wesnoth-base-indent
))
1053 (setq cur-indent ref-indent
)))
1054 ((eq context
'closing
)
1055 (if (or (looking-at "^[\t ]*\\(\\[/\\|\\#enddef\\)")
1056 (and (not wesnoth-indent-savefile
)
1057 (not (looking-at (wesnoth-element-opening t
)))
1058 (not (looking-at "[\t ]*{FOREACH "))))
1059 (setq cur-indent
(- ref-indent wesnoth-base-indent
))
1060 (setq cur-indent ref-indent
)))))
1061 (indent-line-to (max cur-indent
0))))
1062 (when (> (save-excursion (back-to-indentation) (point))
1064 (back-to-indentation)))
1066 (defun wesnoth-within-define (position)
1067 "Determine whether point is currently inside a #define block.
1068 POSITION is the initial cursor position."
1071 (defblocks (or wesnoth-define-blocks
1072 (wesnoth-find-macro-definitions))))
1073 (unless (equal (car defblocks
) 'none
)
1074 (dolist (element defblocks
)
1075 (when (= (cadr (sort (append (mapcar 'marker-position
(cadr element
))
1076 (list position
)) '>)) position
)
1077 (setq depth
(max (car element
) depth
)))))
1080 (defun wesnoth-find-macro-definitions ()
1081 "Return information regarding positioning of macro definitions."
1083 (goto-char (point-min))
1086 (while (search-forward-regexp "#define\\|#enddef" (point-max) t
)
1087 (and (string= (match-string 0) "#define") (beginning-of-line))
1089 (if (string= (match-string 0) "#define")
1091 (add-to-list 'openings
(point-marker))
1096 (list depth
(list (car openings
)
1098 (setq openings
(cdr openings
))
1102 (or cache
(list 'none
)))))
1104 (defun wesnoth-indent-region (start end
)
1105 "Indent the region from START to END.
1106 Creates and destroys a cache of macro definition details as necessary."
1111 (setq end
(point-marker))
1113 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
1114 (or (bolp) (forward-line 1))
1115 (while (< (point) end
)
1116 (if (looking-at "^[\t ]*$")
1118 (funcall indent-line-function
))
1120 (setq wesnoth-define-blocks nil
)))
1122 (defun wesnoth-determine-context (position)
1123 "Determine the type of the last relevant element.
1124 POSITION is the buffer position of the element for which to
1125 determine the context."
1127 (let* ((elements (concat (substring (wesnoth-element t
)
1128 0 (- (length (wesnoth-element t
)) 3))
1129 "\\|{FOREACH .+}\\|{NEXT .+}\\)"))
1131 (and (search-backward-regexp
1132 elements
(point-min) t
)
1134 (while (save-match-data
1135 (looking-at "^[\t ]*\\[[^/].+\\]\\[/.+\\]"))
1136 (search-backward-regexp elements
1141 (depth (wesnoth-within-define position
)))
1142 (while (and (wesnoth-wml-start-pos)
1143 (> (wesnoth-within-define (point)) depth
)
1144 (not (= (point) (wesnoth-wml-start-pos))))
1145 (search-backward-regexp elements
1146 (wesnoth-wml-start-pos) t
)
1147 (setq match
(match-string 1)))
1148 (when (and (wesnoth-wml-start-pos)
1149 (= (point) (wesnoth-wml-start-pos))
1151 (string-match "#define" match
))
1152 ;; Found nothing of use; reset match and assume top-level tag.
1155 ((string-match "\\[/\\|#enddef" match
)
1156 (cons 'closing
(current-indentation)))
1157 ((string-match "{NEXT " match
)
1158 (cons 'closing
(if wesnoth-indent-savefile
1159 (- (current-indentation) wesnoth-base-indent
)
1160 (current-indentation))))
1161 ((string-match "\\[[^/]?\\|#define\\|{FOREACH " match
)
1162 (cons 'opening
(current-indentation)))))))
1164 (defun wesnoth-newline-and-indent (&optional indent
)
1165 "Indent both the current line and the newline created.
1166 If `wesnoth-auto-indent-flag' is nil, indentation will not be
1169 If the optional argument, INDENT is non-nil, force indentation to
1173 (when (or wesnoth-auto-indent-flag indent
)
1177 (defun wesnoth-check-element-type (position)
1178 "Determine the context of the element.
1179 POSITION is the position of the element in the list."
1180 (let ((parent (save-match-data (car (wesnoth-parent-tag)))))
1181 (if (or (stringp parent
) (null parent
))
1182 (member (match-string-no-properties 1)
1183 (nth position
(gethash parent wesnoth-tag-hash-table
)))
1184 (member (match-string-no-properties 1)
1188 (let ((value (nth position
(cdr x
))))
1189 (and value
(mapc '(lambda (y)
1190 (setq result
(cons y result
)))
1192 (or wesnoth-tmp-tag-data
(wesnoth-refresh-wml-data)))
1195 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
1196 (defun wesnoth-line-number-at-pos (&optional pos
)
1197 "Return (narrowed) buffer line number at position POS.
1198 If POS is nil, use current buffer location.
1199 Counting starts at (point-min), so the value refers
1200 to the contents of the accessible portion of the buffer."
1201 (let ((opoint (or pos
(point))) start
)
1203 (goto-char (point-min))
1204 (setq start
(point))
1207 (1+ (count-lines start
(point))))))
1209 (defun wesnoth-check-output (buffer format-string
&rest args
)
1210 "Output the string as passed to `format'.
1211 BUFFER is the buffer to output the result.
1212 FORMAT-STRING is the string as the first argument of `format'.
1213 ARGS is any additional data required by `format' to handle FORMAT-STRING."
1215 (let ((lnap (wesnoth-line-number-at-pos)))
1217 (let ((buffer-read-only nil
))
1218 (insert (apply 'format
(concat "Line %d: " format-string
"\n")
1221 (defun wesnoth-extract-macro-details (macro-arguments)
1222 "Return a list of all macros in MACRO-ARGUMENTS."
1223 (when macro-arguments
1224 (let ((results '()))
1225 (dolist (macro (split-string macro-arguments
"[{}][\t ]*"))
1226 (when (string-match "^\\(\\(?:\\w\\|_\\)+\\)"
1228 (add-to-list 'results
(match-string-no-properties 1 macro
))))
1231 (defmacro wesnoth-check-process
(format-string &rest args
)
1232 "Output to buffer where requested and position overlays as required.
1233 FORMAT-STRING is the string to pass as the first argument to
1234 `format' for the error. ARGS is a list of arguments required by
1237 (wesnoth-check-output outbuf
,format-string
,@args
)
1238 (wesnoth-place-overlay (match-beginning 0) (match-end 0))))
1240 (defmacro wesnoth-overlay-at-pos-p
(position)
1241 "Return non-nil when there is an overlay at POSITION."
1242 `(and (overlays-at (goto-char ,position
))
1243 (overlay-get (car (overlays-at (point))) 'wesnoth-error
)
1244 (overlay-start (car (overlays-at (point))))))
1246 (defmacro wesnoth-locate-warning
(string start end
)
1247 "Search for STRING and move to the warning in the given direction.
1248 Searching starts from `point' and will wrap from START if no
1249 match was found. STRING is a form to locate the warning in the
1250 required direction. START is the start of the region searched.
1251 END is the end of the region searched."
1252 `(let ((target nil
))
1255 ((setq target
(wesnoth-overlay-at-pos-p ,string
)))
1256 ((setq target
(wesnoth-overlay-at-pos-p ,start
)))
1257 ((and (not (= (goto-char ,string
) ,end
))
1258 (setq target
(wesnoth-overlay-at-pos-p (point)))))))
1261 (defun wesnoth-forward-warning ()
1262 "Move to the next warning."
1265 (if (fboundp 'next-overlay-change
)
1268 (wesnoth-locate-warning (next-overlay-change (point))
1271 (wesnoth-target-position '< '>))))
1274 (message "%s" "No warnings found"))))
1276 (defun wesnoth-backward-warning ()
1277 "Move to the previous warning."
1280 (if (fboundp 'previous-overlay-change
)
1283 (wesnoth-locate-warning (1- (previous-overlay-change (point)))
1286 (wesnoth-target-position '> '< t
))))
1289 (message "%s" "No warnings found"))))
1291 (defun wesnoth-target-position (predicate search
&optional lastp
)
1292 "Return the target marker position.
1293 PREDICATE is the function to use to sort
1294 `wesnoth-warning-markers'. SEARCH must be a function which
1295 returns non-nil when the match is correct. If LASTP is non-nil,
1296 swap the order of the sorted positions when attempting to
1298 (let ((positions (sort (mapcar 'marker-position wesnoth-warning-markers
)
1301 (dolist (position positions
)
1302 (when (funcall search position
(point))
1303 (throw 'pos position
))))
1306 (defun wesnoth-place-overlay (start end
)
1307 "Place overlay in the region and apply necessary properties.
1308 START is the start of the region to place the overlay. END is
1309 the end of the region to place the overlay."
1310 (if (fboundp 'overlay-put
)
1311 (let ((overlay (make-overlay start end
)))
1312 (overlay-put overlay
'wesnoth-error t
)
1313 (overlay-put overlay
'face
'wesnoth-warning-face
))
1314 (add-to-list 'wesnoth-warning-markers
(save-excursion
1318 (defun wesnoth-check-wml ()
1319 "Perform context-sensitive analysis of WML-code."
1321 ;; Temporarily cache all tag-data.
1322 (setq wesnoth-tmp-tag-data
(wesnoth-refresh-wml-data))
1323 (wesnoth-update-project-information)
1324 (if (fboundp 'delete-overlay
)
1325 (dolist (overlay (overlays-in (point-min) (point-max)))
1326 (if (eq 'wesnoth-warning-face
(overlay-get overlay
'face
))
1327 (delete-overlay overlay
)))
1328 (setq wesnoth-warning-markers nil
))
1329 (when (= 0 (hash-table-count wesnoth-tag-hash-table
))
1330 (error "WML data not available; unable to generate report"))
1331 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
1332 (let ((unmatched '())
1333 (outbuf (and (interactive-p) (get-buffer-create "*WML*")))
1339 (let ((buffer (buffer-name))
1340 (buffer-read-only nil
))
1342 (insert (format "Checking %s...\n" buffer
))
1343 (message (format "Checking %s..." buffer
))))
1345 (goto-char (point-min))
1346 (while (setq details
(wesnoth-find-next
1347 '(tag-opening tag-closing preprocessor attribute
1350 (goto-char (match-beginning 0))
1351 (cond ((nth 3 (parse-partial-sexp last-match-pos
(point)))
1353 ((eq (car details
) 'macro
)
1354 (dolist (macro (save-match-data
1355 (wesnoth-extract-macro-details
1356 (match-string-no-properties 0))))
1357 (unless (assoc macro
1358 (wesnoth-merge-macro-data
1360 (wesnoth-macro-additions)
1361 wesnoth-local-macro-data
1362 (wesnoth-macro-arguments)))
1363 (wesnoth-check-process "Unknown macro: '%s'"
1368 "{\\(FOREACH\\|NEXT\\).*[\t ]+\\(\\(?:\\w\\|_\\)+\\)}")
1369 (if (string= (match-string-no-properties 1) "FOREACH")
1371 (cons (match-string-no-properties 2) foreach
))
1372 (if (string= (match-string-no-properties 1) "NEXT")
1374 (unless (string= (car foreach
)
1375 (match-string-no-properties 2))
1376 (wesnoth-check-process
1377 (concat "NEXT does not match corresponding "
1378 "FOREACH: '%s' found; '%s' expected.")
1379 (match-string-no-properties 2)
1381 (setq foreach
(cdr foreach
))))))))
1382 ((looking-at "[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
1383 (unless (wesnoth-check-element-type 0)
1384 (wesnoth-check-process
1385 "Tag not available in this context: '%s'"
1386 (match-string-no-properties 1)))
1387 (setq unmatched
(cons (match-string-no-properties 1)
1390 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
1391 "\\( \\(\\w\\|_\\)+\\)*"))
1392 (unless (match-string-no-properties 2)
1393 (wesnoth-check-process
1394 (concat "Preprocessor statement has no argument: "
1395 (match-string-no-properties 1))))
1396 (unless (string= (match-string-no-properties 1) "#undef")
1397 (setq unmatched
(cons (match-string-no-properties 1)
1399 ((looking-at wesnoth-preprocessor-closing-regexp
)
1400 (when (and unmatched
1402 (cdr (assoc (match-string-no-properties 1)
1403 '(("enddef" .
"#define")
1404 ("endif" .
"#ifn?def"))))
1406 (wesnoth-check-process
1407 "Preprocessor statement does not nest correctly"))
1408 (setq unmatched
(cdr unmatched
)))
1409 ((looking-at "[\t ]*\\(\\(\\w\\|_\\)+\\)=\\(.+\\)?")
1410 (unless (wesnoth-check-element-type 1)
1411 (wesnoth-check-process
1412 "Attribute not available in this context: '%s'"
1413 (match-string-no-properties 1)))
1414 (unless (match-string 3)
1415 (wesnoth-check-process
1416 "Attribute has no value")))
1417 ((looking-at "[\t ]*#else")
1418 (unless (string-match "ifn?def" (car unmatched
))
1419 (if (string= (car unmatched
) "#define")
1420 (wesnoth-check-process "Expecting: '%s'"
1422 (wesnoth-check-process "Expecting: '[/%s]'"
1424 ((looking-at "[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1425 (when (and unmatched
1426 (not (string= (match-string-no-properties 1)
1428 (wesnoth-check-process
1430 (or (cdr (assoc (car unmatched
)
1431 '(("#define" .
"#enddef")
1432 ("#ifdef" .
"#endif")
1433 ("#ifndef" .
"#endif"))))
1434 (concat "[/" (car unmatched
) "]"))))
1435 (setq unmatched
(cdr unmatched
)
1436 last-match-pos
(point))))))
1438 (dolist (var foreach
)
1439 (wesnoth-check-process "Unmatched FOREACH: '%s'" var
)))
1441 (dolist (element unmatched
)
1442 (wesnoth-check-process "Unmatched element: '%s'" element
))))
1444 (setq wesnoth-define-blocks nil
1445 wesnoth-tmp-tag-data nil
)
1447 (toggle-read-only t
)
1448 (let ((buffer (buffer-name))
1449 (buffer-read-only nil
))
1450 (display-buffer outbuf t
)
1451 (let ((warnings (- (wesnoth-line-number-at-pos
1452 (save-excursion (goto-char (point-max)))) 2)))
1453 (insert (format (concat "\nCheck complete. %d warning"
1454 (if (= warnings
1) "." "s.")) warnings
)))
1455 (message (format "Checking %s...done" buffer
))))))
1459 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
1460 "Major mode for editing WML."
1461 (kill-all-local-variables)
1462 (use-local-map wesnoth-mode-map
)
1463 (setq major-mode
'wesnoth-mode
)
1464 (setq mode-name
"WML")
1465 (set-syntax-table wesnoth-syntax-table
)
1466 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
1467 (set (make-local-variable 'comment-start
) "#")
1468 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
1469 (set (make-local-variable 'indent-region-function
) 'wesnoth-indent-region
)
1470 (set (make-local-variable 'wesnoth-warning-markers
) '())
1471 (set (make-local-variable 'font-lock-defaults
)
1472 '(wesnoth-font-lock-keywords
1474 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
1475 (setq indent-tabs-mode nil
)
1476 (easy-menu-add wesnoth-menu wesnoth-mode-map
)
1477 (wesnoth-refresh-wml-data)
1478 (wesnoth-update-project-information)
1479 (run-hooks 'wesnoth-mode-hook
))
1481 (provide 'wesnoth-mode
)
1483 ;;; wesnoth-mode.el ends here