Wesnoth Mode

From The Battle for Wesnoth Wiki

Introduction

Summary

Wesnoth Mode is a major mode for Emacs which assists in the editing of the markup language extensively used in Wesnoth, a turn-based fantasy strategy game. From the Wesnoth Wiki: "The Wesnoth Markup Language (WML) is used to code almost everything in Wesnoth, including scenarios, units, savefiles, and the user interface layout." (1)

Wesnoth Mode is supported under GNU Emacs 21 onwards and (with some minor limitations) XEmacs 21. Wesnoth Mode adds support for syntax highlighting, automatic indentation, context-sensitive completion, validation and much more when editing WML.

This documentation attempts to provide a comprehensive guide to functionality available within Wesnoth Mode 1.3.4, and assumes you are familiar with basic usage, terminology and customisation of Emacs. For more information, please refer to the Emacs manual. (2)

Footnotes
(1) ReferenceWML
(2) http://www.gnu.org/software/emacs/manual/html_node/emacs/

Getting Started

To use Wesnoth Mode, add the following to your '.emacs':

    (add-to-list 'load-path "path/to/wesnoth-mode")
    (autoload 'wesnoth-mode "wesnoth-mode" "Major mode for editing WML." t)

Optionally adding:

    (add-to-list 'auto-mode-alist '("\\.cfg\\'" . wesnoth-mode))

to automatically load Wesnoth Mode for all files with a .cfg extension.

Wesnoth Mode can be activated as the current major-mode for a buffer via 'M-x wesnoth-mode'.

The latest version of Wesnoth Mode along with release notes can be found at http://www.wesnoth.org/forum/viewtopic.php?t=13798.

Alternatively, the development version is available using git. To checkout the latest changes, the following command can be used:

 git clone git://repo.or.cz/wesnoth-mode.git

Basic Usage

Indentation

By default, Wesnoth Mode provides a style of indentation equivalent to that provided by 'wmlindent' tool distributed with Wesnoth. 'TAB' can be used to automatically indent the current line to the correct depth. 'TAB' can perform two actions depending on the context of point. When there is an incomplete element immediately preceding point, completion will be attempted, otherwise 'TAB' will perform indentation. See Tab Completion.

Wesnoth Mode will, by default, indent the current line appropriately before inserting a newline when 'RET' or 'C-j' is used. ('C-j' will also attempt to indent the new line.)

See Modifying Indentation for information on how to customise the behaviour of automatic indentation.

Navigation

Built-in commands for navigation are available. This section describes additional or modified navigation commands specific to Wesnoth Mode. Next and previous opening elements can be navigated using 'C-M-e' and 'C-M-a', respectively. In each case, point will be positioned immediately before the element. When no more elements are available in that direction, the position of point will not change.

Moving to the matching element in a pair or locating the parent element (depending on the position of point) can be performed via 'C-c C-o'. When point is on the same line as an opening element, such as an opening tag or opening preprocessor statement it will be moved to the start of the matching closing element. Otherwise, the jump will position point at the beginning of the corresponding opening element.

Structured navigation is available with 'C-M-n' and 'C-M-p' providing the ability to move to the next and previous blocks (respectively) which are at the same depth as the element where the command was invoked. 'C-M-d' will move to the child of the element at point and 'C-M-u' moving to the corresponding parent element.

Editing Extras

Additional commands have been added to assist editing regions of WML. 'C-M-k' can be used to kill the element at point up to the corresponding closing element, including all contents. Also, when point is at an opening element, the mark can be placed at the end of the corresponding closing element using 'C-M-SPC'.

Inserting Elements

Standard Completion

Tags can be inserted via 'C-c C-t' and alternatively 'M-TAB' (when this is not shadowed by the Window Manager, etc.). This will prompt for the tag to add. The tag entered into the mini-buffer prompt and its matching closing tag will be inserted and point positioned between.

Attributes can be inserted via 'C-c C-a'. The attribute entered at the prompt will be inserted along with the '=', with point immediately after.

Both tag and attribute completion is context-sensitive. If an element is available in WML and not listed for completion, you may want to add it to your 'addition file'. See Wesnoth Update for more information.

Macro insertion can be performed via 'C-c C-m'. Any arguments known to be required for the macro will be prompted and inserted in order. Insertion will be aborted if no value for an argument is provided. When within a macro definition, the arguments for the current macro are also available for completion. Any project-local or custom macros can be made available for completion by opening the file in which they are defined. For information on completing project-local macros, see Macro Definitions.

Preprocessor statements are available for insertion via 'C-c C-p'. Closing elements for preprocessor statements will be automatically inserted where possible, with point positioned between. Otherwise, point will be placed immediately after the inserted text.

Tab Completion

Completion can also be performed immediately within the buffer via 'TAB' on a partial element. For example:

    [scenario]
        vil-!-

Where '-!-' is the position of point. Using 'TAB' in the example will complete to 'vil' to 'village_gold='. Tags, macros and preprocessor statements can be completed similarly. When the current line does not contain a partially completed element, 'TAB' will perform indentation on the current line.

When there is more than one possible completion, a minibuffer prompt will be provided, with the partial element entered. 'TAB' can be used here to perform completion. If multiple matches are available, completion will be performed up to the smallest common substring with further completion available via the minibuffer prompt. When no matches are found for the partial element, completion will not be prompted.

When completing opening preprocessor statements and tags, Wesnoth Mode will also attempt to insert a matching closing element if one is not already available, acting in much the same way as if the element was added via the minibuffer prompt. However, if an matching closing element is available, only the element at point will be completed.

A numeric argument can be provided when performing tab completion of opening elements to wrap around the next 'n' blocks. For example, to wrap the completed opening and closing pair around the next three blocks, 'C-u 3 TAB' can be used. Refer to Wrapping Elements for more information.

Wrapping Elements

When inserting tags and some preprocessor statements, either via their respective insertion command or via 'TAB', an optional numeric argument can be provided to specify the number of 'blocks' to wrap the element around. For example:

    [multiplayer]
     -!-[part]
            ...
        [/part]
        [part]
            ...
        [/part]
        [event]
            ...

Where point is at the position indicated by '-!-'. A pair of 'story' tags can be inserted around both existing 'part' tags using 'C-u 2 C-c C-t story'. When the number of blocks specified to wrap around exceeds the number of blocks available, Wesnoth Mode will only wrap around the number of available so that the nesting of elements is correct.

Missing Elements

Missing closing elements can be inserted using 'C-c C-/'. By default, this will insert the first missing closing element found in the current buffer at point. If all elements appear to be matched or if there is an excess of closing tags, a notification will be displayed and no element will be inserted.

Validating WML

Usage and Capabilities

Validating WML in the current buffer can be performed using 'C-c C-c'. A summary of all warnings located will be provided in a separate buffer. Point can be jumped to the next and previous warning using 'C-c C-f' (or 'C-x `') and 'C-c C-b', respectively. For visibility, warnings will be underlined in red by default. Note: Warning underlines may not be available in XEmacs.

The WML validation built-in to Wesnoth Mode is not intended to be an alternative to tools such as `wmllint', but may often be a convenient substitute while editing WML.

The following conditions can be tested in Wesnoth Mode:

  • Correct nesting of tags and preprocessor statements
  • Known macro definitions (1)
  • Availability of elements within the given context
  • Arguments are given to preprocessor statements when required
  • Attributes have a value assigned
  • Corresponding FOREACH..NEXT pairs

WML validation is specific to the version of WML known by Wesnoth Mode. See Wesnoth Update for more information.

Footnotes
(1) Macro Definitions

Customisation

Modifying Indentation

The style of indentation can be customised using 'wesnoth-indent-savefile'. The default value is 'non-nil', which results in all children being indented a level deeper than their parent. When set to 'nil', children will be indented to the same level as their parent element. This option is provided only for +consistency when editing (very) old WML. It is recommended that this be non-nil for all new WML.

'wesnoth-indent-preprocessor-bol' controls how preprocessor statements should be indented. If non-nil, all preprocessor statements will be indented to the beginning of the line. Otherwise, preprocessor statements will be indented as to the level indicated by its context.

By default, Wesnoth Mode will attempt indentation of the current line and create a newline and when 'RET' or 'C-j' are used. 'wesnoth-auto-indent-flag' controls whether Wesnoth Mode will attempt to indent the current line automatically. Setting this to 'nil' will prevent automatic indentation. Note that this does not affect the behaviour of 'C-j' performing indentation following the newline; this only determines whether indentation will be automatically performed on the current line.

The following can be used to change 'RET' to automatically indent the newline:

(add-hook 'wesnoth-mode-hook
  '(lambda ()
    (define-key wesnoth-mode-map (kbd "C-m")
    'wesnoth-newline-and-indent)))

'wesnoth-base-indent' controls the depth of indentation for each level. Its value should be an integer. This is set to '4' by default, which is the convention used when indenting WML.

By default, Wesnoth Mode will use spaces for indentation. Tabs can instead be used with the following:

(add-hook 'wesnoth-mode-hook
  '(lambda ()
    (setq indent-tabs-mode t
          tab-width wesnoth-base-indent)))


'wesnoth-warning-face' is the face used to display the overlay for warnings detected when checking WML (See Validating WML). By default this is a red underline. Note: use of this face may not be supported under XEmacs.

Wesnoth Update

Wesnoth Update is an extension to Wesnoth Mode which retrieves information regarding valid WML structure using WML included with Wesnoth. To update this information, two variables need to be set appropriately: 'wesnoth-root-directory' and 'wesnoth-update-output-directory'.

'wesnoth-root-directory' should be the path to the root directory of a Wesnoth installation or Wesnoth source code. Wesnoth Update will search recursively in this directory for WML, using the information found to provide context-sensitive completion and WML validation.

'wesnoth-update-output-directory' specifies the path to store the WML data found. This path should be within the 'load-path', and preferably, in the same directory as Wesnoth Mode.

For example:

(setq wesnoth-root-directory "/usr/local/share/wesnoth/"
      wesnoth-update-output-directory "~/.emacs.d/wesnoth-mode/")

Once set, 'M-x wesnoth-update' will generate and load a new cache of WML data ready for use for the current and future sessions. Note: 'wesnoth-update' may take some time to run.

Wesnoth Mode 1.3.6 uses WML data retrieved from Wesnoth 1.5.11, by default.

Addition File

'wesnoth-addition-file' specifies the `addition file' to use. An addition file is an outline of a valid WML structure which is processed to provide more comprehensive completion of validation by providing access to element which may not have been available when running Wesnoth Update

The value of 'wesnoth-addition-file' shield be set as the path to a suitable addition file. A sample addition file is included with Wesnoth Mode.

For example, the following could be used to specify an addition file:

(setq wesnoth-addition-file
     "~/.emacs.d/wesnoth-mode/wesnoth-wml-additions.cfg")

The addition file is read and information regarding WML structure updated any time WML information is required. Therefore, no further action is required to use the updated WML data once any changes to the addition file are saved to disk.

Macro Definitions

While built-in macros are always available, local macro definitions are automatically scanned and made known to Wesnoth Mode for each WML file which is loaded in the session. 'C-c C-u' can be used to update the known macro definitions for any buffer which has since been modified. (Note that this is not required when the macro is defined in the WML file currently being edited as such definitions will be automatically updated when needed.) 'C-u C-c C-u' can be used to clear known local macro definitions.

This page was last edited on 7 May 2023, at 03:24.