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 ;; * Completion history available is now specific to wesnoth-mode.
38 ;; * Significantly improved performance of completion and WML checking.
39 ;; * Improved performance for inserting missing tags.
40 ;; * Fixed a bug where #ifdef was never matched when checking WML.
41 ;; * Added completion for preprocessor statements.
43 ;; * Added support for Xemacs.
44 ;; * WML checking is now context sensitive; checks attributes and macros.
45 ;; * WML checks are now always performed on the entire buffer, with results
46 ;; displayed in a temporary buffer.
47 ;; * Context-sensitive completion for attributes and tags.
48 ;; * Completion for built-in and project-specific macros.
49 ;; * Changed the following bindings:
50 ;; `wesnoth-insert-tag' - C-c e -> C-c t
51 ;; `wesnoth-jump-to-matching' - C-c m -> C-c o
52 ;; `wesnoth-check-structure' -> `wesnoth-check-wml' - C-c c
53 ;; * Added the following bindings:
54 ;; `wesnoth-complete-attribute' - C-c a
55 ;; `wesnoth-complete-macro' - C-c m
56 ;; `wesnoth-complete-tag' - C-c t
57 ;; * Removed the following bindings:
58 ;; `wesnoth-check-tag-names' - C-c n
59 ;; * Removed `wesnoth-check-tag-names'. Replaced by `wesnoth-check-wml'.
60 ;; * Completion for an incomplete attribute, tag, or macro at point is
63 ;; * Fixed support for GNU Emacs 21.
64 ;; * Added several new tags to `wesnoth-tags-list'.
65 ;; * Added M-TAB binding for `wesnoth-insert-tag'.
66 ;; * `wesnoth-insert-tag' now takes an optional numeric argument indicating
67 ;; how many blocks to wrap across instead of a region.
68 ;; * Support for `wesnoth-indent-preprocessor-bol' removed.
69 ;; * Fixed a bug in `wesnoth-insert-tag' and `wesnoth-insert-missing-closing'
70 ;; causing tags not to be inserted in the correct position.
71 ;; * Fixed highlighting of array indexes as tags.
73 ;; * Improved syntax-highlighting for macro calls.
74 ;; * Underscore is now treated as whitespace.
75 ;; * Fixed incorrect indentation when preprocessor preceeded by whitespace.
76 ;; * Point is now placed at the first non-whitespace character of the line,
77 ;; instead of the last.
78 ;; * Corrected minor indentation bugs.
79 ;; * Indenting across large regions is now much more efficient.
80 ;; * Fix hooks modifying wesnoth-mode-map causing default bindings not being
83 ;; * Now compatible with GNU Emacs 21.4.
84 ;; * Added support for several new tags.
85 ;; * Added menu entry for wesnoth-mode.
86 ;; * Significant speed increase to indentation.
87 ;; * Indentation can now be customised using `wesnoth-indent-preprocessor-bol'
88 ;; and `wesnoth-indent-savefile'; support for `wesnoth-indentation-function'
90 ;; * Trailing whitespace is no longer created when creating a second
91 ;; consecutive newline.
92 ;; * Spurious newlines are no longer created when inserting a tag elements
95 ;; * Added functions: `wesnoth-indent', `wesnoth-element-closing',
96 ;; `wesnoth-element', `wesnoth-element-opening',
97 ;; `wesnoth-insert-and-indent', `wesnoth-insert-missing-closing'.
98 ;; * Renamed `wesnoth-indent-line-default', `wesnoth-indent-line-savefile' and
99 ;; `wesnoth-jump-backward', `wesnoth-jump-forward' to
100 ;; `wesnoth-indent-withtags-inline', `wesnoth-indent-default-inline' and
101 ;; `wesnoth-backward-tag', `wesnoth-forward-tag', respectively.
102 ;; * Fixed a bug in indentation where content was needed between elements pairs
103 ;; for indentation to work.
104 ;; * Fixed `wesnoth-newline-and-indent' ignoring the state of
105 ;; `wesnoth-auto-indent-flag'.
106 ;; * Fixed `{...}' and `#endif' not font-locking correctly.
107 ;; * Added indentation styles: `wesnoth-indent-default',
108 ;; `wesnoth-indent-withtags' which implement a a similar indentation
109 ;; style to the existing styles, however all preprocessor statements are
110 ;; indented to the first column.
111 ;; * Added support for several new tags.
112 ;; * Modified `wesnoth-newline' to behave more consistently.
113 ;; * `wesnoth-jump-to-matching', `wesnoth-forward-tag', `wesnoth-backward-tag'
114 ;; now leaves point at the beginning (when moving backward) or end (when
115 ;; moving forward) of the match.
116 ;; * `wesnoth-jump-to-matching' now attempts to find a target if necessary and
117 ;; will now work on preprocessor statements. Will now warn if jump
118 ;; destination may not be correct (due to errors in WML structure).
119 ;; * Indentation style is now determined by `wesnoth-indentation-function'.
120 ;; * `wesnoth-check-structure' can now be applied over an active region and
121 ;; now checks preprocessor statements for correct nesting.
122 ;; * `wesnoth-newline' and `wesnoth-newline-and-indent' can now be forced to
123 ;; perform indentation by providing a prefix argument.
124 ;; * Indentation styles now leave point at the first non-whitespace character
126 ;; * `wesnoth-check-tag-names' now reports on success.
127 ;; * `wesnoth-insert-tag' is now able to insert tags around a region.
128 ;; * `outline-minor-mode' now works on macro definitions.
130 ;; * Base indent now defaults to 4.
131 ;; * Added support for #ifndef.
137 (require 'wesnoth-update
)
138 (require 'wesnoth-wml-data
)
140 (defconst wesnoth-mode-version
"1.3.0+git"
141 "The current version of `wesnoth-mode'.")
143 (defgroup wesnoth-mode nil
"Wesnoth-mode access"
147 (defcustom wesnoth-auto-indent-flag t
148 "Non-nil means indent the current line upon creating a newline."
150 :group
'wesnoth-mode
)
152 (defcustom wesnoth-indent-savefile t
153 "Non-nil means to use the current indentation conventions.
154 If nil, use the old convention for indentation.
155 The current convention is all attributes are indented a level deeper
156 than their parent; in the past attributes were indented to the same
157 level as their parent.")
159 (defcustom wesnoth-base-indent
4
160 "The number of columns to indent WML."
162 :group
'wesnoth-mode
)
164 (defconst wesnoth-preprocessor-regexp
165 "[\t ]*#\\(enddef\\|define\\|e\\(lse\\|nd\\(\\(de\\|i\\)f\\)\\)\\|\\(ifn?\\|un\\)def\\)"
166 "Regular expression to match all preprocessor statements.")
168 (defconst wesnoth-preprocessor-opening-regexp
169 "[\t ]*#\\(define \\|else\\|ifdef \\|ifndef \\)"
170 "Regular expression to match \"opening\" preprocessor statements.")
172 (defconst wesnoth-preprocessor-closing-regexp
173 "[\t ]*#\\(end\\(\\(de\\|i\\)f\\)\\)"
174 "Regular expression to match \"closing\" preprocessor statements.")
176 (defvar wesnoth-define-blocks
'()
177 "Cache of all toplevel #define and #enddef pairs.")
179 (defvar wesnoth-history-list
'()
180 "History of inserted WML elements.")
182 (defvar wesnoth-mode-hook nil
)
184 (defvar wesnoth-mode-map
185 (let ((map (make-sparse-keymap)))
186 (define-key map
(kbd "C-M-a") 'wesnoth-backward-element
)
187 (define-key map
(kbd "C-M-e") 'wesnoth-forward-element
)
188 (define-key map
(kbd "C-m") 'wesnoth-newline
)
189 (define-key map
(kbd "C-j") 'wesnoth-newline-and-indent
)
190 (define-key map
(kbd "C-c C-c") 'wesnoth-check-wml
)
191 (define-key map
(kbd "C-c C-a") 'wesnoth-complete-attribute
)
192 (define-key map
(kbd "C-c C-t") 'wesnoth-complete-tag
)
193 (define-key map
(kbd "C-c C-p") 'wesnoth-complete-preprocessor
)
194 (define-key map
(kbd "M-TAB") 'wesnoth-complete-tag
)
195 (define-key map
(kbd "C-c C-m") 'wesnoth-complete-macro
)
196 (define-key map
(kbd "C-c C-o") 'wesnoth-jump-to-matching
)
197 (define-key map
(kbd "C-c C-/") 'wesnoth-insert-missing-closing
)
198 (define-key map
(kbd "TAB") 'wesnoth-indent-or-complete
)
200 "Keymap used in `wesnoth-mode'.")
202 (easy-menu-define wesnoth-menu wesnoth-mode-map
"Menu for wesnoth-mode"
204 ["Check WML" wesnoth-check-wml t
]
205 ["Indent or Complete" wesnoth-indent-or-complete t
]
206 ["Indent buffer" (lambda ()
208 (wesnoth-indent-region (point-min) (point-max))) t
]
209 ["Insert Tag" wesnoth-complete-tag t
]
210 ["Insert Attribute" wesnoth-complete-attribute t
]
211 ["Insert Macro" wesnoth-complete-macro t
]
212 ["Insert Preprocessor" wesnoth-complete-preprocessor t
]
213 ["Insert Missing Tag" wesnoth-insert-missing-closing t
]
214 ["Jump to Matching" wesnoth-jump-to-matching t
]))
216 (defvar wesnoth-syntax-table
217 (let ((wesnoth-syntax-table (make-syntax-table)))
218 (modify-syntax-entry ?
= "." wesnoth-syntax-table
)
219 (modify-syntax-entry ?_
"_" wesnoth-syntax-table
)
220 (modify-syntax-entry ?-
"_" wesnoth-syntax-table
)
221 (modify-syntax-entry ?.
"_" wesnoth-syntax-table
)
222 (modify-syntax-entry ?
\n ">" wesnoth-syntax-table
)
223 (modify-syntax-entry ?
\r ">" wesnoth-syntax-table
)
224 wesnoth-syntax-table
)
225 "Syntax table for `wesnoth-mode'.")
227 ;; Prevents automatic syntax-highlighting of elements which might be
228 ;; pre-processor statements.
229 (defvar wesnoth-syntactic-keywords
231 '("\\(^[\t ]*\\(#\\(?:define \\|e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\|\\(?:ifn?\\|un\\)def \\)\\)\\)" 1 "w")
232 '("\\(#[\t ]*.*$\\)" 1 "<"))
233 "Highlighting syntactic keywords within `wesnoth-mode'.")
235 (defvar wesnoth-font-lock-keywords
237 '("#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)" . font-lock-keyword-face
)
238 '("\\(#e\\(?:lse\\|nd\\(?:\\(?:de\\|i\\)f\\)\\)\\)" .
239 font-lock-keyword-face
)
240 '("\\(#\\(?:define\\|\\(?:ifn?\\|un\\)def\\)\\)[\t ]+\\(\\(\\w\\|_\\)+\\)"
241 2 font-lock-function-name-face
)
242 '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face
))
243 '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face
))
244 '("}" . font-lock-function-name-face
)
245 '("^[\t ]*\\(\\[[^]]+\\]\\)" 1 font-lock-type-face
)
246 '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face
)
247 '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)="
248 1 font-lock-variable-name-face
))
249 "Syntax highlighting for `wesnoth-mode'.")
251 (defun wesnoth-element-closing (&optional limited
)
252 "Return the regexp to match a closing element.
253 If LIMITED is non-nil, return a regexp which matches only the
254 #enddef preprocessor."
255 (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\|"
258 "#end\\(?:def\\|if\\)")
261 (defun wesnoth-element-opening (&optional limited
)
262 "Return the regexp to match a closing element.
263 If LIMITED is non-nil, return a regexp which matches only the
264 #define preprocessor."
265 (concat "^[\t ]*\\(\\[\\(\\w\\|_\\)+\\]\\|#define "
271 (defun wesnoth-element (&optional limited
)
272 "Return the regexp to match a closing element.
273 If LIMITED is non-nil, return a regexp which matches only the
274 #define and #enddef preprocessors."
275 (concat "^[\t ]*\\(\\[/?\\(\\w\\|_\\)+\\]?\\|"
278 (substring wesnoth-preprocessor-regexp
5))
282 ;;; Insertion and completion
283 (defmacro wesnoth-element-completion
(completions prompt partial
)
284 "Process completion of COMPLETIONS, displaying PROMPT.
285 PARTIAL is the partial string on which to attempt completion."
286 `(let* ((element (when ,partial
(try-completion ,partial
,completions
))))
287 (cond ((eq element t
)
291 (completing-read ,prompt
,completions nil nil nil
292 'wesnoth-history-list
)))
293 ((not (if (listp (car ,completions
))
294 (assoc element
,completions
)
295 (member element
,completions
)))
297 (completing-read ,prompt
,completions
299 'wesnoth-history-list
))))
302 (defun wesnoth-parent-tag ()
303 "Return the name of the parent tag.
304 If the parent is a preprocessor statement, return non-nil.
305 If the element does not have a parent, return nil.
306 Otherwise, return a string containing the name of the parent tag."
308 (let ((start-point (point))
310 (when (save-excursion (> (point) (progn (back-to-indentation)
313 (while (and (> depth
0)
314 (search-backward-regexp (wesnoth-element)
316 (if (string-match "[\t ]*\\[/" (match-string 0))
322 (when (looking-at (wesnoth-element-opening))
323 (let ((parent (match-string-no-properties 1)))
324 (if (string-match wesnoth-preprocessor-opening-regexp parent
)
326 (substring parent
1 (1- (length parent
))))))))))
328 (defun wesnoth-indent-or-complete (&optional elements
)
329 "Indent or complete the line at point, depending on context.
330 ELEMENTS is the number of elements to wrap around if inserting
333 (or elements
(setq elements
0))
336 (back-to-indentation)
337 (cond ((looking-at "\\(\\(\\w\\|_\\)+\\)[\t ]*$")
338 (wesnoth-complete-attribute t
))
339 ((looking-at "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$")
340 (wesnoth-complete-tag elements t
))
341 ((looking-at "{\\(\\(\\w\\|_\\)*\\)[\t ]*$")
342 (wesnoth-complete-macro t
))
343 ((looking-at "^#\\w+$")
344 (wesnoth-complete-preprocessor elements t
))
345 ((looking-at "\\[/\\(\\(\\w\\|_\\)*\\)[\t ]*$")
346 (delete-region (point) (progn (end-of-line) (point)))
347 (wesnoth-insert-missing-closing)
351 (setq target
(point)))
354 (defun wesnoth-preprocessor-closed-p (preprocessor)
355 "Determine whether PREPROCESSOR has been closed.
356 PREPROCESSOR is a string matching the preprocessor statement to
359 (back-to-indentation)
360 (wesnoth-jump-to-matching preprocessor
)
362 (if (string= preprocessor
"#define ")
366 (defun wesnoth-complete-preprocessor (&optional elements completep
)
367 "Complete and insert the preprocessor at point.
368 ELEMENTS is the number of elements to wrap around.
369 If COMPLETEP is non-nil, attempt to complete preprocessor at point."
371 (or elements
(setq elements
0))
372 (let* ((completions (wesnoth-emacs-completion-formats
373 '("define" "else" "ifdef" "ifndef"
374 "enddef" "endif" "undef")))
375 (partial (when completep
377 (back-to-indentation)
378 (when (looking-at "#\\(\\w+\\)$")
380 (preprocessor (or (wesnoth-element-completion
381 completions
"Preprocessor: " partial
)
386 (unless (string= "#" (substring preprocessor
0 1))
387 (setq preprocessor
(concat "#" preprocessor
)))
388 (when (string-match "#\\(define\\|ifn?def\\|undef\\)" preprocessor
)
389 (setq preprocessor
(concat preprocessor
" ")))
391 (delete-region (progn (back-to-indentation) (point))
392 (progn (end-of-line) (point))))
393 (wesnoth-preprocessor-closed-p preprocessor
)))))
396 (delete-region (progn (back-to-indentation) (point))
397 (progn (end-of-line) (point))))
398 (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor
)
401 (wesnoth-insert-tag elements preprocessor
)
404 (wesnoth-insert-element-separately preprocessor
)))))
406 (defun wesnoth-macro-arguments ()
407 "Find any current macro arguments."
409 (unless (= 0 (wesnoth-within-define (point)))
411 (search-backward-regexp
412 "[\t ]*#define \\(?:\\w+\\|_\\)*\\(\\( \\(\\w\\|_\\)+\\)*\\)"
414 (mapcar (lambda (macro) (list macro nil
))
416 (match-string-no-properties 1)))))))
418 (defun wesnoth-complete-macro (&optional completep
)
419 "Complete and insert the macro at point.
420 If COMPLETEP is non-nil, attempt to complete the macro at point."
422 (wesnoth-update-project-information)
423 (let* ((macro-information (append (wesnoth-macro-arguments)
425 wesnoth-local-macro-data
))
426 (completions (wesnoth-emacs-completion-formats
427 (mapcar 'car macro-information
)))
428 (partial (when completep
430 (back-to-indentation)
431 (when (looking-at "{\\(\\(\\w\\|_\\)*\\)")
433 (macro (or (wesnoth-element-completion completions
"Macro: " partial
)
435 (args (cadr (assoc macro macro-information
))))
439 (delete-region (progn (back-to-indentation) (point))
440 (progn (end-of-line) (point)))
441 (insert "{" macro
(if args
" }" "}")))
442 (wesnoth-insert-element-separately "{" macro
(if args
" }" "}")))
446 (forward-char -
1)))))
448 (defun wesnoth-complete-attribute (&optional completep
)
449 "Insert the attribute at point.
450 If COMPLETEP is non-nil, attempt to complete the attribute at point."
452 (let* ((completions (wesnoth-build-completion 2))
453 (partial (when completep
455 (back-to-indentation)
456 (when (looking-at "\\(\\(\\w\\|_\\)+\\)")
458 (attribute (or (wesnoth-element-completion completions
"Attribute: "
462 (if (and partial completep
)
464 (delete-region (progn (back-to-indentation) (point))
465 (progn (end-of-line) (point)))
466 (insert attribute
"="))
467 (wesnoth-insert-element-separately attribute
468 (if (string-match "=" attribute
)
474 (defun wesnoth-complete-tag (&optional elements completep
)
475 "Complete and insert the tag at point.
476 ELEMENTS is the number of elements to wrap around.
477 If COMPLETEP is non-nil, attempt to complete tag at point."
479 (or elements
(setq elements
0))
480 (let* ((completions (wesnoth-build-completion 1))
481 (partial (when completep
483 (back-to-indentation)
484 (when (looking-at "\\[\\(\\(\\w\\|_\\)+\\)")
486 (tag (or (wesnoth-element-completion completions
"Tag: " partial
)
490 (wesnoth-jump-to-matching)
491 (back-to-indentation)
492 (and (looking-at "\\[/\\(\\(\\w\\|_\\)+\\)")
493 (string= tag
(match-string 1))))))
495 (delete-region (progn (back-to-indentation) (point))
496 (progn (end-of-line) (point))))
497 (if (and closedp completep
)
499 (wesnoth-insert-and-indent "[" tag
"]")
501 (wesnoth-insert-tag elements tag
))))
503 (defun wesnoth-build-completion (position)
504 "Create a new list for tag completion if necessary.
505 Rebuilding list is required for versions of GNU Emacs earlier
506 than 22. POSITION is the argument passed to `nth' for
509 (let* ((parent (wesnoth-parent-tag))
511 (if (or (stringp parent
) (null parent
))
512 (nth (1- position
) (gethash parent wesnoth-tag-hash-table
))
513 (mapcar 'car wesnoth-tag-data
))))
514 (wesnoth-emacs-completion-formats candidates
)))
516 (defun wesnoth-emacs-completion-formats (candidates)
517 "Return the completions in the correct format for `emacs-major-version'.
518 CANDIDATES is a list of all possible completions."
519 (if (> emacs-major-version
21)
523 (dolist (tag candidates
)
524 (setq iter
(1+ iter
))
525 (setq tags
(append tags
(list (cons tag iter
)))))
528 (defun wesnoth-insert-tag (&optional elements tagname
)
529 "Insert the specified opening tag and it's matching closing tag.
530 Both the opening and closing tags will be placed on their own
531 lines with point positioned between them. Completion of tags at
532 the prompt uses `wesnoth-tags-list'.
534 ELEMENTS is specifies the number of following blocks which the
535 tag should wrap around.
537 TAGNAME is the name of the tag to be inserted."
540 (setq tagname
(completing-read "Tag: " (wesnoth-build-completion 1)
541 nil nil nil wesnoth-history-list
)))
542 (when (or (not elements
)
543 (looking-at (concat "[\t ]*\\(:?\\[/\\|"
544 wesnoth-preprocessor-regexp
"\\)")))
547 (start (save-excursion (forward-line -
1) (point)))
548 (end (unless (= elements
0)
549 (wesnoth-nth-pair-position elements
))))
550 (if (string-match wesnoth-preprocessor-regexp tagname
)
551 (wesnoth-insert-element-separately tagname
)
552 (wesnoth-insert-element-separately "[" tagname
"]"))
555 (goto-char (marker-position end
))
557 (if (string-match wesnoth-preprocessor-opening-regexp tagname
)
558 (wesnoth-insert-element-separately
559 (if (string= tagname
"#define ")
562 (wesnoth-insert-element-separately "[/" tagname
"]"))
563 (indent-region start
(point) nil
))
568 (defun wesnoth-insert-element-separately (&rest strings
)
569 "Concatenate STRINGS and insert them on a line of their own."
570 (let ((create-newline (save-excursion
572 (if (looking-at "^[\t ]*$") nil t
))))
574 (if (> (point) (save-excursion (back-to-indentation) (point)))
580 (insert (apply 'concat strings
))))
582 (defun wesnoth-insert-missing-closing (&optional start end
)
583 "Insert the next expected closing element at point.
585 START and END define the region to check for missing closing
586 elements. If function `transient-mark-mode' is enabled, the region
587 specified will be used as START and END. Otherwise, START and
588 END will be the minimum and current positions of the buffer,
591 (if (and (boundp 'transient-mark-mode
)
592 transient-mark-mode mark-active
)
593 (setq start
(region-beginning)
594 end
(copy-marker (region-end)))
595 (setq start
(point-min)
597 (let ((element (wesnoth-check-structure start end
)))
599 (error "%s" "Unable to find element to insert")
600 (when (string= element
"Unexpected end of file")
601 (error "%s" element
))
602 (wesnoth-insert-element-separately element
)))
606 (defun wesnoth-insert-and-indent (&rest args
)
607 "Concatenate and insert the given string(s) before indenting.
609 ARGS is a list of strings to be inserted."
610 (insert (apply 'concat args
))
613 (defun wesnoth-newline (&optional indent
)
614 "Indent both the current line and the newline created.
615 If `wesnoth-auto-indent-flag' is nil, indentation will not be
616 performed. Indentation can be forced by setting INDENT to
620 (when (and (or wesnoth-auto-indent-flag indent
)
621 (not (looking-at "^[\t ]*$")))
626 (defmacro wesnoth-navigate-element
(repeat search-function bound
)
627 "Move point to the tag in the given direction REPEAT times.
629 SEARCH-FUNCTION is the symbol of the function for searching in
630 the required direction, with BOUND marking the furthest point to
633 (or ,repeat
(setq ,repeat
1))
635 (and (eq ,search-function
'search-forward-regexp
) (end-of-line))
636 (funcall ,search-function
(wesnoth-element-opening) ,bound t
)
637 (back-to-indentation)
640 (defun wesnoth-nth-pair-position (count)
641 "Return `point' after COUNT number of matching element pairs.
642 COUNT is a positive number representing the number of balanced
643 pairs to move across.
644 `point' is returned as a marker object."
646 (let ((start (point))
648 (if (> (point) (save-excursion (back-to-indentation) (point)))
652 ;; Currently looking-at target tag. Stop here to avoid
653 ;; incorrect nesting.
654 (unless (wesnoth-search-for-matching-tag
655 'search-forward-regexp
(wesnoth-element-closing) 'point-max
)
657 (unless (or (= (point) (point-max))
658 (progn (beginning-of-line)
659 (search-backward-regexp (wesnoth-element-closing)
662 (and (> (decf count
) 0) (forward-line 1)))
668 (defun wesnoth-forward-element (repeat)
669 "Move point to the end of the next tag.
670 REPEAT is an optional numeric argument. If REPEAT is non-nil,
671 jump forward the specified number of tags."
674 (wesnoth-backward-element (abs repeat
))
675 (wesnoth-navigate-element repeat
'search-forward-regexp
(point-max))))
677 (defun wesnoth-backward-element (repeat)
678 "Move point to the beginning of the previous tag.
679 REPEAT is an optional numeric argument. If REPEAT is non-nil,
680 jump backward the specified number of tags."
683 (wesnoth-forward-element (abs repeat
))
684 (wesnoth-navigate-element repeat
'search-backward-regexp
(point-min))))
686 (defmacro wesnoth-search-for-matching-tag
(search-function
687 search-string bound
&optional skip
)
688 "Search for the matching tag for the current line.
690 SEARCH-FUNCTION is the name of the function used to perform the search.
691 SEARCH-STRING is a string representing the matching tag type.
692 BOUND is the bound to be passed to the search function.
693 If SKIP is non-nil, skip the first element and continue from there."
695 (when (or (and ,skip
(forward-line 1))
696 (funcall ,search-function
(wesnoth-element) (funcall ,bound
) t
))
697 (when (or ,skip
(not (string-match ,search-string
(match-string 0))))
698 (while (and (> depth
0)
699 (funcall ,search-function
(wesnoth-element)
701 (if (string-match ,search-string
(match-string 0))
706 (defun wesnoth-jump-to-matching (&optional opening
)
707 "Jump point to the matching opening/closing tag.
708 OPENING is an opening preprocessor statement to attempt to find a match for."
714 (if (or (and (stringp opening
)
715 (string-match (wesnoth-element-opening) opening
))
716 (looking-at (wesnoth-element-opening)))
718 (setq first-element
(match-string-no-properties 0 opening
))
719 (when (wesnoth-search-for-matching-tag
720 'search-forward-regexp
(wesnoth-element-closing) 'point-max
723 (if (and (string-match wesnoth-preprocessor-opening-regexp
725 (looking-at (wesnoth-element-closing)))
726 (when (string= (match-string-no-properties 0)
727 (cdr (assoc first-element
728 '(("#define " .
"#enddef")
729 ("#ifndef " .
"#endif")
730 ("#ifdef " .
"#endif")))))
731 (setq target
(point)))
732 (setq target
(point)))))
733 (when (looking-at (wesnoth-element-closing))
734 (setq first-element
(match-string-no-properties 0))
736 (when (wesnoth-search-for-matching-tag
737 'search-backward-regexp
(wesnoth-element-opening)
738 'wesnoth-wml-start-pos
)
739 (if (and (string-match wesnoth-preprocessor-closing-regexp
741 (looking-at (wesnoth-element-opening)))
743 (when (or (and (string= "#enddef" first-element
)
745 (match-string-no-properties 0)))
746 (and (string= "#endif" first-element
)
749 (match-string-no-properties 0))))
750 (setq target
(point))))
751 (setq target
(point)))))))
754 (when (interactive-p)
755 (error "Tag does not appear to be matched"))))
756 (back-to-indentation))
759 (defun wesnoth-wml-start-pos ()
760 "Determine the position of `point' relative to where the actual WML begins.
761 Return the likely starting position of the WML if it is found.
762 Otherwise return nil."
764 (goto-char (point-min))
765 (when (search-forward-regexp (wesnoth-element) (point-max) t
)
769 (defun first-column-indent-p (point)
770 "Return non-nil if the current line should not be indented.
772 POINT is the position in the buffer to check.
773 CONTEXT represents the type of element which precedes the current element."
774 (or (not (wesnoth-wml-start-pos))
775 (<= (point) (wesnoth-wml-start-pos))
776 (nth 3 (parse-partial-sexp (point-min) point
))
777 (looking-at wesnoth-preprocessor-regexp
)))
779 (defun wesnoth-indent ()
780 "Indent the current line as WML."
782 (let ((cur-indent 0))
783 (unless (first-column-indent-p (point))
784 (multiple-value-bind (context ref-indent
)
785 (wesnoth-determine-context (point))
787 ((eq context
'opening
)
788 (if (or (and wesnoth-indent-savefile
789 (not (looking-at (wesnoth-element-closing t
))))
790 (looking-at (wesnoth-element-opening t
)))
791 (setq cur-indent
(+ ref-indent wesnoth-base-indent
))
792 (setq cur-indent ref-indent
)))
793 ((eq context
'closing
)
794 (if (or (looking-at "^[\t ]*\\[/")
795 (and (not wesnoth-indent-savefile
)
796 (not (looking-at (wesnoth-element-opening t
)))))
797 (setq cur-indent
(- ref-indent wesnoth-base-indent
))
798 (setq cur-indent ref-indent
))))))
799 (indent-line-to (max cur-indent
0))))
801 (defun wesnoth-within-define (position)
802 "Determine whether point is currently inside a #define block.
803 POSITION is the initial cursor position."
806 (dolist (element (or wesnoth-define-blocks
807 (wesnoth-find-macro-definitions)))
808 (when (= (cadr (sort (append (mapcar 'marker-position
(cadr element
))
809 (list position
)) '>)) position
)
810 (setq depth
(max (car element
) depth
))))
813 (defun wesnoth-find-macro-definitions ()
814 "Return information regarding positioning of macro definitions."
816 (goto-char (point-min))
819 (while (search-forward-regexp "^[\t ]*\\(#define\\|#enddef\\)" (point-max) t
)
820 (and (string= (match-string 1) "#define") (beginning-of-line))
822 (if (string= (match-string 1) "#define")
824 (add-to-list 'openings
(point-marker))
829 (list depth
(list (car openings
)
831 (setq openings
(cdr openings
))
837 (defun wesnoth-indent-region (start end
)
838 "Indent the region from START to END.
840 Creates and destroys a cache of macro definition details as necessary."
845 (setq end
(point-marker))
847 (setq wesnoth-define-blocks
(wesnoth-find-macro-definitions))
848 (or (bolp) (forward-line 1))
849 (while (< (point) end
)
850 (if (looking-at "^[\t ]*$")
852 (funcall indent-line-function
))
854 (setq wesnoth-define-blocks nil
)))
856 (defun wesnoth-determine-context (position)
857 "Determine the type of the last relevant element.
859 POSITION is the buffer position of the element for which to
860 determine the context."
862 (search-backward-regexp (wesnoth-element t
)
863 (wesnoth-wml-start-pos) t
)
864 (let ((match (or (match-string 1) ""))
865 (depth (wesnoth-within-define position
)))
866 (while (and (> (wesnoth-within-define (point)) depth
)
867 (not (= (point) (wesnoth-wml-start-pos))))
868 (search-backward-regexp (wesnoth-element t
)
869 (wesnoth-wml-start-pos) t
)
870 (setq match
(match-string 1)))
871 (when (and (= (point) (wesnoth-wml-start-pos)) (= depth
0)
872 (string-match "#define" match
))
873 ;; Found nothing of use; reset match and assume top-level tag.
876 ((string-match "\\[/\\|#enddef" match
)
877 (values 'closing
(current-indentation)))
878 ((string-match "\\[[^/]?\\|#define" match
)
879 (values 'opening
(current-indentation)))))))
881 (defun wesnoth-newline-and-indent (&optional indent
)
882 "Indent both the current line and the newline created.
883 If `wesnoth-auto-indent-flag' is nil, indentation will not be
886 If the optional argument, INDENT is non-nil, force indentation to
890 (when (or wesnoth-auto-indent-flag indent
)
894 (defun wesnoth-check-element-type (position last-tag
)
895 "Determine the context of the element.
896 POSITION is the position of the element in the list.
897 LAST-TAG is the parent element."
899 (if (or (not last-tag
)
900 (string-match "#\\(?:define\\|ifn?def\\)" last-tag
))
901 (member (match-string-no-properties 1)
902 (mapcar 'car wesnoth-tag-data
))
903 (member (match-string-no-properties 1)
904 (nth position
(gethash last-tag
905 wesnoth-tag-hash-table
))))))
907 ;; Provide `line-number-at-pos' implementation (not available in Emacs 21).
908 (defun wesnoth-line-number-at-pos (&optional pos
)
909 "Return (narrowed) buffer line number at position POS.
910 If POS is nil, use current buffer location.
911 Counting starts at (point-min), so the value refers
912 to the contents of the accessible portion of the buffer."
913 (let ((opoint (or pos
(point))) start
)
915 (goto-char (point-min))
919 (1+ (count-lines start
(point))))))
921 (defun wesnoth-check-output (buffer format-string
&rest args
)
922 "Output the string as passed to `format'.
923 BUFFER is the buffer to output the result.
924 FORMAT-STRING is the string as the first argument of `format'.
925 ARGS is any additional data required by `format' to handle FORMAT-STRING."
927 (let ((lnap (wesnoth-line-number-at-pos)))
929 (insert (apply 'format
(concat "Line %d: " format-string
"\n")
932 (defun wesnoth-check-wml ()
933 "Perform context-sensitive analysis of WML-code."
935 (wesnoth-update-project-information)
936 (when (= 0 (hash-table-count wesnoth-tag-hash-table
))
937 (error "WML data not available; can not generate report"))
938 (let ((unmatched '())
939 (outbuf (get-buffer-create "*WML*")))
941 (let ((buffer (buffer-name)))
944 (insert (format "Checking %s...\n" buffer
))
945 (message (format "Checking %s..." buffer
))))
947 (goto-char (or (wesnoth-wml-start-pos) (point-min)))
948 (while (search-forward-regexp
949 ;; Match tags, preprocessor statements, macros and attributes.
950 (concat "^[\t ]*\\(\\[[+/]?\\(\\(\\w\\|_\\)+\\)\\]\\|"
951 "\\(\\w\\|_\\)+=\\|{\\(\\(\\w\\|_\\)+\\).*}\\|"
952 wesnoth-preprocessor-regexp
"\\)")
955 (cond ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]")
956 (unless (wesnoth-check-element-type 0 (car unmatched
))
957 (wesnoth-check-output outbuf
958 "Tag not available in this context: '%s'"
959 (match-string-no-properties 1)))
960 (setq unmatched
(cons (match-string-no-properties 1)
963 (concat "[\t ]*\\(#define\\|#ifdef\\|#ifndef\\|#undef\\)"
964 "\\( \\(\\w\\|_\\)+\\)*"))
965 (unless (match-string-no-properties 2)
966 (wesnoth-check-output
967 outbuf
(concat "Preprocessor statement has no argument: "
968 (match-string-no-properties 1))))
969 (unless (string= (match-string-no-properties 1) "#undef")
970 (setq unmatched
(cons (match-string-no-properties 1)
972 ((looking-at wesnoth-preprocessor-closing-regexp
)
975 (cdr (assoc (match-string-no-properties 1)
976 '(("enddef" .
"#define")
977 ("endif" .
"#ifn?def"))))
979 (wesnoth-check-output
981 "Preprocessor statement does not nest correctly"))
982 (setq unmatched
(cdr unmatched
)))
983 ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=\\(.+\\)?")
984 (unless (wesnoth-check-element-type 1 (car unmatched
))
985 (wesnoth-check-output
986 outbuf
"Attribute not available in this context: '%s'"
987 (match-string-no-properties 1)))
988 (unless (match-string 3)
989 (wesnoth-check-output
990 outbuf
"Attribute has no value")))
991 ((looking-at "^[\t ]*#else")
992 (unless (string-match "ifn?def" (car unmatched
))
993 (if (string= (car unmatched
) "#define")
994 (wesnoth-check-output outbuf
"Expecting: '%s'"
996 (wesnoth-check-output outbuf
"Expecting: '[/%s]'"
998 ((looking-at "^[\t ]*{\\(\\(\\w\\|_\\)+\\).*}")
999 (unless (assoc (match-string-no-properties 1)
1000 (append (wesnoth-macro-arguments)
1001 wesnoth-local-macro-data
1002 wesnoth-macro-data
))
1003 (wesnoth-check-output outbuf
"Unknown macro definition: '{%s}'"
1004 (match-string-no-properties 1))))
1005 ((looking-at "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]")
1006 (when (and unmatched
1007 (not (string= (match-string-no-properties 1)
1009 (wesnoth-check-output outbuf
"Expecting: '[/%s]'"
1011 (setq unmatched
(cdr unmatched
))))
1014 (dolist (tag unmatched
)
1015 (wesnoth-check-output outbuf
"Unmatched element: '%s'"
1018 (let ((buffer (buffer-name)))
1020 (display-buffer outbuf t
)
1021 (let ((warnings (- (wesnoth-line-number-at-pos
1022 (save-excursion (goto-char (point-max)))) 2)))
1023 (insert (format (concat "\nCheck complete. %d warning"
1024 (if (= warnings
1) "." "s.")) warnings
)))
1025 (message (format "Checking %s...done" buffer
))))))
1027 (defmacro wesnoth-element-requires
(element requirement
&optional pop
)
1028 "Process requirements for corresponding preprocessor elements.
1029 ELEMENT is the current element being tested.
1030 REQUIREMENT is the element required to exist for correct nesting.
1031 POP is an optional argument indicating the element should be
1032 removed from the list of unmatched elements."
1033 `(when (string= ,element
(match-string-no-properties 1))
1035 (if (string-match ,requirement
(car unmatched
))
1037 (and ,pop
(setq unmatched
(cdr unmatched
)))
1039 (setq error-position
(point)))))
1041 (defmacro wesnoth-structure-result
(position element
)
1042 "Process results of the structure check.
1043 POSITION is the error position or nil, if no error was found.
1044 ELEMENT is the last unmatched element, or nil if all opening
1045 elements have been matched."
1046 `(let ((expected nil
))
1048 (cond ((string= ,element
"define ") (setq expected
"#enddef"))
1049 ((string-match "ifn?def " ,element
) (setq expected
"#endif"))))
1051 (if (or ,element
,position
)
1053 (and ,position
(goto-char ,position
))
1054 (message "Error: Expecting %s" (or expected
1055 (concat "[/" ,element
"]"))))
1056 (message "%s" "Structure appears consistent."))
1057 (when (or expected
,element
)
1058 (or expected
(concat "[/" ,element
"]"))))))
1060 (defun wesnoth-check-structure (&optional start end
)
1061 "Check the buffer for correct nesting of elements.
1062 If a problem is found in the structure, point will be placed at
1063 the location which an element was expected and the expected
1064 element will be displayed in the mini-buffer.
1066 START and END define the region to be checked. If
1067 function `transient-mark-mode' is enabled, the region specified will be
1068 checked. Otherwise START and END will be the minimum and maximum
1069 positions of the buffer, respectively."
1071 (unless (or start end
)
1072 (if (and (boundp 'transient-mark-mode
)
1073 transient-mark-mode mark-active
)
1074 (setq start
(region-beginning)
1075 end
(copy-marker (region-end)))
1076 (setq start
(point-min)
1078 (let ((unmatched '())
1079 (error-position nil
))
1081 (and start
(goto-char start
))
1082 (while (and (search-forward-regexp
1083 (concat "^\\([\t ]*\\[\\(/?\\(\\w\\|_\\)+\\)\\]\\|"
1084 wesnoth-preprocessor-regexp
"\\)") end t
)
1085 (not error-position
))
1087 (if (or (looking-at "^[\t ]*\\[\\(\\(\\w\\|_\\)+\\)\\]")
1088 (looking-at "[\t ]*#\\(define \\|ifdef \\|ifndef \\)"))
1089 (setq unmatched
(cons (match-string-no-properties 1)
1092 ((wesnoth-element-requires "#else" "ifn?def "))
1093 ((wesnoth-element-requires "#endif" "ifn?def " t
))
1094 ((wesnoth-element-requires "#enddef" "define " t
))
1095 ((looking-at (concat "^[\t ]*\\[/\\(\\(\\w\\|_\\)+\\)\\]\\|"
1096 wesnoth-preprocessor-closing-regexp
))
1097 (if (string= (match-string-no-properties 1)
1099 (setq unmatched
(cdr unmatched
))
1100 (setq error-position
(point))))))
1102 (wesnoth-structure-result error-position
(car unmatched
))))
1105 (define-derived-mode wesnoth-mode fundamental-mode
"wesnoth-mode"
1106 "Major mode for editing WML."
1107 (kill-all-local-variables)
1108 (use-local-map wesnoth-mode-map
)
1109 (setq major-mode
'wesnoth-mode
)
1110 (setq mode-name
"WML")
1111 (set-syntax-table wesnoth-syntax-table
)
1112 (set (make-local-variable 'outline-regexp
) "[\t ]*#define")
1113 (set (make-local-variable 'comment-start
) "#")
1114 (set (make-local-variable 'indent-line-function
) 'wesnoth-indent
)
1115 (set (make-local-variable 'indent-region-function
) 'wesnoth-indent-region
)
1116 (set (make-local-variable 'font-lock-defaults
)
1117 '(wesnoth-font-lock-keywords
1119 (font-lock-syntactic-keywords . wesnoth-syntactic-keywords
)))
1120 (setq indent-tabs-mode nil
)
1121 (easy-menu-add wesnoth-menu wesnoth-mode-map
)
1122 (wesnoth-create-wml-hash-table)
1123 (wesnoth-update-project-information)
1124 (run-hooks 'wesnoth-mode-hook
))
1126 (provide 'wesnoth-mode
)
1128 ;;; wesnoth-mode.el ends here