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