Package basic-mode provides a major mode for editing BASIC code in GNU Emacs. Features include syntax highlighting and indentation, as well as support for auto-numbering and renumbering of code lines.
The recommended way to install basic-mode is from MELPA.
To install manually, place basic-mode.el in your load-path, and add the following lines of code to your init file:
(autoload 'basic-generic-mode "basic-mode" "Major mode for editing BASIC code." t)
(add-to-list 'auto-mode-alist '("\\.bas\\'" . basic-generic-mode))
Package basic-mode supports some of the many BASIC dialects using sub modes derived from the main basic-mode. Section Available Sub Modes lists the currently existing sub modes. In addition, basic-mode also provides a generic sub mode that will be used if no sub mode is specified.
The sub mode configures, among other things, syntax highlighting and indentation, which differs between dialects.
By default, basic-mode will open BASIC files in sub mode basic-generic-mode. There are several ways to change that behaviour if you prefer a specific sub mode.
After opening a file in Emacs, you can type M-x MODE-NAME to set the sub mode, e.g. M-x basic-zx81-mode to set the Sinclair ZX81 sub mode.
You can specify the sub mode as a file variable in the first line of the file, see Specifying File Variables in the GNU Emacs Manual. For example, a file beginning with this line, will open in the TRS-80 Model 100 sub mode.
10 REM -*- basic-m100 -*-
If you are always working with a specific BASIC dialect, you can configure Emacs to always open BASIC files in that sub mode. For example, adding the below line to your init file will open all BASIC files in the Sinclair ZX81 sub mode.
(add-to-list 'auto-mode-alist '("\\.bas\\'" . basic-zx81-mode))
This table lists the existing sub modes. If you feel that a sub mode is missing, the best way to bring it into existence is to create it yourself, and submit a pull request to include it in basic-mode.
BASIC Dialect | Sub Mode |
---|---|
Dartmouth | basic-dartmouth-mode |
Microsoft QuickBasic 4.5 | basic-qb45-mode |
Sinclair ZX81 | basic-zx81-mode |
Sinclair ZX Spectrum | basic-spectrum-mode |
TRS-80 Model I and III | basic-trs80-mode |
TRS-80 Model 100 | basic-m100-mode |
TAB indents the current line of BASIC code, including line numbers if available. If the region is active, TAB indents all lines in the region.
C-c C-f formats the entire buffer; indents all lines, and removes any extra whitespace. If the region is active, C-c C-f formats all lines in the region.
For classic BASIC, basic-mode provides several functions to manage line numbers. Customize variables basic-auto-number and basic-line-number-cols to your liking to turn on line numbers.
With line numbers turned on, RET will start new lines with a fresh line number. The line number increment is defined by variable basic-auto-number. C-c C-r will renumber all lines in the entire buffer, including any jumps in the code. The line number increment used when renumbering is defined by variable basic-renumber-increment. If the region is active, C-c C-r renumbers all lines in the region instead.
With line numbers turned on, C-c C-f will also respect line numbers, and the value configured in basic-line-number-cols while formatting.
Package basic-mode also provides additional functionality to navigate in the source code. M-. will find and move to the definition of the identifier at point, and M-, will move back again. For more information, see function xref-find-definitions.
The following table lists the customizable variables that affect basic-mode in some way:
Name | Description | Default Value |
---|---|---|
basic-auto-number | If non-nil, auto-number new lines by incrementing the line number with this value. | nil |
basic-delete-trailing-whitespace | If non-nil, basic-format-code deletes trailing whitespace while formatting. | t |
basic-indent-offset | The number of columns to indent code blocks, for example inside an IF statement. | 4 |
basic-line-number-cols | The number of columns allocated to line numbers at the beginning of the line. | 0 |
basic-mode-hook | Hook run when entering BASIC mode. | nil |
basic-renumber-increment | Default line number increment when renumbering lines. | 10 |
basic-renumber-unnumbered-lines | If non-nil, lines without line numbers are also renumbered when renumbering lines. | t |
basic-syntax-highlighting-require-separator | If non-nil, only keywords separated by separators will be highlighted. If nil, keywords separated by numbers will also be highlighted. | t |
delete-trailing-lines (simple.el) | If non-nil, basic-format-code deletes trailing empty lines while formatting. | t |
Additionally, each sub mode will also define a hook variable, named like the sub mode, e.g. basic-generic-mode-hook for the generic sub mode.