Contents‎ > ‎Vim Scripts‎ > ‎

EnumToCase

This is a simple plugin for C programmers who use a lot of state machines or other switch statements on enumerated lists.

Having created your enumerated list, copy the enumerated lines (one per line) into a switch statement block.  Select the new lines and call the function with:

:'<,'>EnumToCase

The plugin will convert the each line into a correctly indented block with the case statement and the break.  The plugin can be downloaded from here: enumtocase.vim.

For example, 
 1 typedef enum
 2 {
 3     FirstSTATE,
 4     SecondSTATE,
 5     FinalSTATE
 6 } MyStateTYPE;
 7
 8 void RunStateMachine()
 9 {
10     static MyStateTYPE seState;
11     switch (eState)
12     {
13     FirstSTATE,
14     SecondSTATE,
15     FinalSTATE
16     }
17 }
Lines 13-15 are an exact duplicate of lines 3-5.
Becomes:
 1 typedef enum
 2 {
 3     FirstSTATE,
 4     SecondSTATE,
 5     FinalSTATE
 6 } MyStateTYPE;
 7
 8 void RunStateMachine()
 9 {
10     static MyStateTYPE seState;
11     switch (eState)
12     {
13         case FirstSTATE:
14             break;
15
16         case SecondSTATE:
17             break;
18
19         case FinalSTATE:
20             break;
21
22     }
23 }

Configuration

There are a number of configuration options that affect the way EnumToCase works.  These should be set in your vimrc and are listed below:

let g:EnumToCaseKeepEndOfLine = 1Copy anything that comes after the ',' on the input line after the ':' on the output line.  Default: off (0).
let g:EnumToCaseKeepEndOfLineComments = 1Copy any single comment on the end of the input line after the ':' on the output line.  Default: off (0).  This is ignored if g:EnumToCaseKeepEndOfLine is enabled.
let g:EnumToCaseKeepPrefixComments = 0
If there are any comments preceding an enumerated name, this is placed between the case statement and the break statement.  Default: on (1).

Updates

9th July 2009: Fixed bug in regular expression used to handle fixed value enums.
17th July 2009: Added support for comments in enum declaration (end-of-line comment suggestion from Timo Teifel).
17th July 2009: Added support for more end-of-line code (further suggestion from Timo Teifel).
ċ
enumtocase.vim
(4k)
Alan Budden,
17 Jul 2009, 07:14
Comments