1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--
$Id: modes.html,v 1.4 2009/12/29 11:40:09 tom Exp $
-->
<HTML>
<HEAD>
<TITLE>
Major (and minor) modes in Vile
</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rev="made" href="mailto:dickey@invisible-island.net">
<link rel="SHORTCUT ICON" href="/img/icons/vile.ico" type="image/x-icon">
</HEAD>
<BODY>
<h1 id="toplevel-id"><a name="toplevel">Major (and minor) modes in Vile</a></h1>
<p>
This document was originally written in May, 1998, and has been updated
periodically to reflect the proposed implementation of and use of major modes
in vile.
<p>
My goal was to extend the notion of the C mode (cmode)
to allow runtime definable major modes.
<p>
Originally, vile supported a C mode that included a collection of modes
useful for editing C program source:
<ul>
<li>c-suffixes
<li>c-tabstop
<li>c-shiftwidth
</ul><p>
as well as this builtin functionality:
<ul>
<li>autoindention of C statements taking into account curly braces
<li>fence matching for /*...*/ comments and C preprocessor statements
<li>comment formatting
</ul><p>
Both the modes and functionality are extensions of other features in
vile. It would be useful to combine modes to support other languages
in a similar fashion. Likewise, the autoindention, etc., could be
parameterized and made reusable to support other languages. For an
initial implementation, I focused on the combining of modes,
providing a structure for the parameterization.
<p>
One thing that was not clear to many users was the manner in which the C
mode was attached to a buffer. It was set as a boolean - if active before
a buffer was loaded, then vile checked the file suffix to see if it matched
the c-suffixes mode, and if so, set the C mode for the buffer. C mode
could also be explicitly set by a ":setl cmode", and unset by ":setl
nocmode". In the new scheme,
<ul>
<li>vile searches the list of all active majormodes, first
testing if any match with the majormode's suffixes, then if
any preamble (first line regular expression) matches.
<p>
The search is in normally alphabetic order, by majormode name.
The first match each, of suffixes and preamble terminate the
search, but a match of suffixes overrides a match of preamble.
Use "after" and "before" qualifiers to handle special cases.
<p>
In rare circumstances, both suffixes and preamble are needed to
distinguish a given mode from another. Use
<!--{{atr2html--><p style="font-family: monospace;">
qualifiers=all<br>
<!--atr2html}}--></p>
to specify this. The default, "qualifiers=any", allows either
suffixes or preamble to determine the majormode.
<p>
The suffixes and preamble rules can be overridden altogether by
specifying $majormode-hook to a script which sets the majormode
for the current buffer.
<li>majormodes can be disabled (e.g., ":set nocmode"), as before.
However, there is no global sense of majormode; unsetting a
buffer's local value of a majormode simply makes it not have a
majormode associated with it.
</ul>
<h2 id="neededcommands-id"><a name="neededcommands">Commands</a></h2>
<p>
These are the commands which I think are necessary:
<ul>
<li>define-majormode {majormode}
<li>define-submode {majormode} {minormode}[={value}]
<li>remove-majormode {majormode}
<li>remove-submode {majormode} {minormode}
<li>setl {majormode}
<li>setl no{majormode}
</ul><p>
The {majormode} is a new symbol.
<p>
The {minormode} can be any one of the existing buffer modes, except for
a {majormode}. To make name-completion simple, we use the term 'submode'.
<h3 id="example-id"><a name="example">Example</a></h3>
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">define-majormode</font></strong> <strong><font color="#000080">c</font></strong><br>
<font color="#800000">; Declares a mode 'c', and corresponding symbol 'cmode'</font><br>
<br>
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">c</font></strong> <strong><font color="#000080">suffixes</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"\\.\\(\\([Cchisyl]\\)\\|CC\\|cc|cpp\\|cxx\\|hxx\\|scm\\)$"</font><br>
<font color="#800000">; Specifies the filename suffixes which control whether a newly-loaded</font><br>
<font color="#800000">; buffer is set to 'c' mode.</font><br>
<br>
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">c</font></strong> <strong><font color="#000080">tabstop</font></strong><strong><font color="#800000">=</font></strong><strong><font color="#008080">4</font></strong><br>
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">c</font></strong> <strong><font color="#000080">shiftwidth</font></strong><strong><font color="#800000">=</font></strong><strong><font color="#008080">4</font></strong><br>
<font color="#800000">; Defines the 'c' tabstop and shiftwidth. If no "define-submode"</font><br>
<font color="#800000">; command is given, no separate symbol is defined.</font><br>
<!--atr2html}}--></p>
As an example, to define a new major mode for perl programming, you might
include the following in your .vilerc file:
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">define-majormode</font></strong> <strong><font color="#000080">perl</font></strong><br>
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">perl</font></strong> <strong><font color="#000080">preamble</font></strong> <font color="#800080">"^#.*perl\\>"</font><br>
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">perl</font></strong> <strong><font color="#000080">suffixes</font></strong> <font color="#800080">'\.\(pm\|t\)$'</font><br>
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">perl</font></strong> <strong><font color="#000080">shiftwidth</font></strong> <strong><font color="#008080">4</font></strong><br>
<!--atr2html}}--></p>
To avoid the tediousness of this syntax, use the ~with and ~endwidth
keywords, like so:
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">define-majormode</font></strong> <strong><font color="#000080">perl</font></strong><br>
<font color="#008000">~with</font> <strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">perl</font></strong><br>
<strong><font color="#000080">preamble</font></strong> <font color="#800080">"^#.*perl\\>"</font><br>
<strong><font color="#000080">suffixes</font></strong> <font color="#800080">'\.\(pm\|t\)$'</font><br>
<strong><font color="#000080">shiftwidth</font></strong> <strong><font color="#008080">4</font></strong><br>
<font color="#008000">~endwith</font><br>
<!--atr2html}}--></p>
You can define several regions using ~with and (after the first) ~elsewith
blocks. Each block specifies a set of tokens which are substituted at the
beginning of each line. Each ~elsewith block should have a group identifier
to distinguish it from the others. The reason for providing multiple
blocks is to implement complex fence-matching for different language features.
<p>
For example, in the definition of cshmode, the symbols "fence-XXX" give
patterns which vile can use to move the cursor from one if/elif/else/fi
marker to the next as you press "%". The other settings such as "suf"
in the first block are settings that apply to the majormode itself:
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">define-mode</font></strong> <em>csh</em><br>
<font color="#008000">~with</font> <strong><font color="#000080">define-submode</font></strong> <em>csh</em><br>
<strong><font color="#000080">suf</font></strong> <font color="#800080">'\.\(csh[^/]*\|login\|logout\)$'</font><br>
<strong><font color="#000080">pre</font></strong> <font color="#800080">'^#!\s*\/.*csh\>\(\s*-[a-z]\+\)*\s*$'</font><br>
<strong><font color="#000080">filtername</font></strong> <font color="#800080">'vile-sh-filt -k csh'</font><br>
<strong><font color="#000080">comment-prefix</font></strong> <font color="#800080">'^\s*#'</font><br>
<strong><font color="#000080">comments</font></strong> <font color="#800080">'^\s*#\s*$'</font><br>
<strong><font color="#000080">fence-if</font></strong> <font color="#800080">'^\s*\<if\>.*\<then\>'</font><br>
<strong><font color="#000080">fence-elif</font></strong> <font color="#800080">'^\s*\<else\s*if\>'</font><br>
<strong><font color="#000080">fence-else</font></strong> <font color="#800080">'^\s*\<else\>'</font><br>
<strong><font color="#000080">fence-fi</font></strong> <font color="#800080">'^\s*\<endif\>'</font><br>
<font color="#008000">~elsewith</font> <strong><font color="#000080">define-submode</font></strong> <em>csh</em> <strong><font color="#000080">group</font></strong> <font color="#800080">'case'</font><br>
<strong><font color="#000080">fence-if</font></strong> <font color="#800080">'^\s*\<switch\>\s*(.*)'</font><br>
<strong><font color="#000080">fence-elif</font></strong> <font color="#800080">'^\s*\<case\>.*:'</font><br>
<strong><font color="#000080">fence-else</font></strong> <font color="#800080">'^\s*\<default\>\s*:'</font><br>
<strong><font color="#000080">fence-fi</font></strong> <font color="#800080">'^\s*\<endsw\>'</font><br>
<font color="#008000">~elsewith</font> <strong><font color="#000080">define-submode</font></strong> <em>csh</em> <strong><font color="#000080">group</font></strong> <font color="#800080">'loop'</font><br>
<strong><font color="#000080">fence-if</font></strong> <font color="#800080">'^\s*\<foreach\s\+.*\|while\>\s*(.*)'</font><br>
<strong><font color="#000080">fence-fi</font></strong> <font color="#800080">'^\s*\<end\>'</font><br>
<font color="#008000">~endwith</font><br>
<!--atr2html}}-->
<h3 id="definemajormode-id"><a name="definemajormode">The "define-majormode" command</a></h3>
<p>
This takes a single argument, a majormode name. To follow existing
convention, the string "mode" is automatically appended to the given
name. Associated modes are defined or modified with the define-submode
command. Vile maintains a list of majormodes. Only one majormode can
be associated with a buffer (none need be associated). After
definition, a majormode can be set or unset just like any other buffer
mode:
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">define-majormode</font></strong> <strong><font color="#000080">c</font></strong><br>
<font color="#800000">; defines "cmode"</font><br>
<br>
<strong><font color="#000080">setl</font></strong> <strong><font color="#000080">cmode</font></strong><br>
<font color="#800000">; sets the mode for the current buffer</font><br>
<br>
<strong><font color="#000080">setl</font></strong> <strong><font color="#000080">nocmode</font></strong><br>
<font color="#800000">; clear c mode (existing implementation)</font><br>
<br>
<strong><font color="#000080">unsetl</font></strong> <strong><font color="#000080">cmode</font></strong><br>
<font color="#800000">; clear c mode</font><br>
<!--atr2html}}--></p>
The restriction to a single majormode is because mode values are
internally represented as structures with a pointer. The pointer
denotes which value (currently local or global) is used. The
majormode implementation adds a level to this, e.g.,
<pre>
value -> self (local)
value -> global (global)
value -> major (majormode)
</pre><p>
When a majormode is defined, an array of the existing minor mode values
is allocated, all pointing to the global modes. The define-submode
command modifies these to make them local pointers. When a buffer is
associated with a majormode, all of its buffer mode values are pointed
to the majormode's values. (To keep the bookkeeping straight, modifying
a global buffer mode must also modify the copies of non-local buffer
mode values).
<h3 id="definesubmode-id"><a name="definesubmode">The "define-submode" command</a></h3>
<p>
This command sets local values of buffer modes for the given majormode,
e.g.,
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">c</font></strong> <strong><font color="#000080">autoindent</font></strong><br>
<!--atr2html}}--></p>
The majormode name is required. The names after the majormode name are
names of buffer modes with a corresponding value. Any number of modes
can be specified in a single command, e.g.,
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">c</font></strong> <strong><font color="#000080">autoindent</font></strong> <strong><font color="#000080">tabstop</font></strong><strong><font color="#800000">=</font></strong><strong><font color="#008080">4</font></strong><br>
<!--atr2html}}--></p>
For each mode given, vile defines corresponding names by which they can
be referenced, e.g., "c-autoindent" (from the long mode name
"autoindent") and "cai" (from the short mode name "ai" for autoindent).
The long mode name is always appended with a hyphen, and the short mode
name is appended without a hyphen.
<p>
The term "submode" is used in the command rather than the more natural
"minor mode" to simplify name-completion.
<p>
The following are keywords that aren't minor modes, but are recognized
solely by the define-submode command:
<dl>
<dt>mode-pathname<dd>
The full pathname which is tested to trigger association with a
majormode, e.g., "RCS/*,v".
<dt>mode-filename<dd>
The filename which is tested to trigger association with a
majormode, e.g., "Makefile".
<dt>suffixes<dd>
The filename suffix which is tested to trigger
association with a majormode (e.g., c-suffixes)
Note that since the default value for the c-suffixes mode
is a regular expression that will match some other file
types (C++ files, for instance), if you define a new major
mode for one of those suffixes you may want to reset
c-suffixes to something less inclusive.
<dt>preamble<dd>
Regular expression, matched against the beginning of a
file (the first line) used to trigger association with
a majormode (e.g., "^!#.*\/perl[^a-z]").
<dt>filtername<dd>
A shell command telling which filter to call, and how to do
that. The form of the command is limited, since it is
interpreted for internal calls:
<pre>
{filter} {options}
</pre><p>
where {filter} is the filename for the filter, e.g.,
<pre>
vile-c-filt
</pre><p>
and options include:
<dl>
<dt>-k mode<dd>
The keyword filename, omitting ".keywords".
For instance, the C filter vile-c-filt is used
for C, C++, JavaScript and Java by changing the
keyword file.
<dt>-q<dd>
Quit after loading the keyword definitions.
<dt>-t tabs<dd>
Set the tabstops, used in the imake filter
to check for coincidental matches between a
tab and spaces that are the same number of
columns.
<dt>-v
<dt>-vv<dd>
Debug traces, mainly to show which keyword
files are loaded, and what information is
parsed from them.
</dl><p>
Other options may be implemented that are specific to a filter
program. For instance, vile-c-filt recognizes a -p option to
mark preprocessor lines with an error (used for Java).
<dt>before
<dt>after<dd>
A string that tells vile how to rank the majormode.
Normally vile checks each mode in order by name, but there
are special cases where you may want to check one mode
before another. For example, the C++ majormode cppmode
would be checked after cmode, but that uses suffixes which
are a subset of the built-in cmode's suffixes and would not
be found. So cppmode is qualified before="c".
<dt>group<dd>
followed by a name, defines an alternative set of submode
values.
<p>
Currently used only for complex fences, this could be applied
to simple fences, and (with new flags not yet defined) extend
both styles of fences for indentation and formatting.
<dt>fences<dd>
These are "complex" fences, which are matched one expression
per line. The names are "fence-" followed by any of the
following keywords with a regular expression:
<pre>
if, elif, else, fi
</pre><p>
Vile searches through all groups of complex fences for a
match before trying simple fences.
<dt>comments<dd>
These are "simple" fences, which can be matched any number
of times per line. The pairs need not appear on the same
line. Like complex fences, they have an implied order.
The default values support C-style comments, with the '%' going
between "/*" and "*/". The names use "fence-" followed any of
the following keywords with a regular expression: begin, end.
<dt>indent (not implemented)<dd>
The keyword "cstyle", or any of the following keywords
with a regular expression: begin, end, if, then, else,
elif, endif.
</dl><p>
Other features which should be controlled by majormodes include limiting
the scope of the entab and detab commands.
<h3 id="removemajormode-id"><a name="removemajormode">The "remove-majormode" command</a></h3>
<p>
This command has two forms:
<dl>
<dt>remove-majormode {majormode}<dd>
This removes the definition of the majormode. Buffers
that were associated with the mode revert to no
majormode.
</dl><p>
or
<dl>
<dt>remove-majormode {majormode} {name}<dd>
This removes the value of {name} from {majormode},
leaving it set to the global value, if any.
</dl>
<h3 id="removesubmode-id"><a name="removesubmode">The "remove-submode" command</a></h3>
<p>
Remove the special association of a submode from a majormode.
<h3 id="example2-id"><a name="example2">Example</a></h3>
<p>
The original builtin C/C++ majormode description is equivalent to
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">define-mode</font></strong> <strong><font color="#000080">c</font></strong><br>
<font color="#008000">~with</font> <strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">c</font></strong><br>
<strong><font color="#000080">suffix</font></strong> <font color="#800080">"\\.\\(\\([Cchisyl]\\)\\|CC\\|cc|cpp\\|cxx\\|hxx\\|scm\\)$"</font><br>
<strong><font color="#000080">comment-prefix</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"^\\s*\\(\\s*[#*>]\\)\\+"</font><br>
<strong><font color="#000080">comments</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"^\\s*/\\?\\(\\s*[#*>]\\)\\+/\\?\\s*$"</font><br>
<strong><font color="#000080">fence-begin</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"/\\*"</font><br>
<strong><font color="#000080">fence-end</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"\\*/"</font><br>
<strong><font color="#000080">fence-if</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"^\\s*#\\s*if"</font><br>
<strong><font color="#000080">fence-elif</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"^\\s*#\\s*elif\\>"</font><br>
<strong><font color="#000080">fence-else</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"^\\s*#\\s*else\\>"</font><br>
<strong><font color="#000080">fence-fi</font></strong><strong><font color="#800000">=</font></strong><font color="#800080">"^\\s*#\\s*endif\\>"</font><br>
<strong><font color="#000080">cindent</font></strong><br>
<font color="#008000">~endwith</font><br>
<!--atr2html}}--></p>
Note that the following are equivalent once you have defined the majormode "c":
<!--{{atr2html--><p style="font-family: monospace;">
<strong><font color="#000080">set</font></strong> <strong><font color="#000080">cts</font></strong><strong><font color="#800000">=</font></strong><strong><font color="#008080">8</font></strong><br>
<strong><font color="#000080">set</font></strong> <strong><font color="#000080">c-tabstop</font></strong><strong><font color="#800000">=</font></strong><strong><font color="#008080">8</font></strong><br>
<strong><font color="#000080">define-submode</font></strong> <strong><font color="#000080">c</font></strong> <strong><font color="#000080">tabstop</font></strong><strong><font color="#800000">=</font></strong><strong><font color="#008080">8</font></strong><br>
<!--atr2html}}--></p>
<h2 id="credits-id"><a name="credits">Credits</a></h2>
Most of this was written by Thomas Dickey, with fixes from Clark Morgan and
Steven Lembark.
|