As has been stated, your list of exceptions is by no means comprehensive and you will probably find that this often creates more problems than it solves.
However, it's still doable and it was kind of fun to write out the code to do so, so here goes:
"When hitting Enter, if the line doesn't start with a /*, insert a semicolon
:inoremap <CR> <C-R>=match(getline('.'), '\s*/\*')==-1?';':''<CR><CR>
Feel free to expand this regex to capture your conditions above. I didn't want to write all of that out as I feel like it would obfuscate what I was writing and would still not be a complete solution for what your intent is.
You would probably want to only do this for certain filetypes, in which case you'd want to add the <buffer>
argument to this command and do it inside an autocmd Filetype
block.
To disable the mapping, you would say :iunmap <CR>
;
, for example when breaking a function call with many arguments over 2 or more lines... Scripting all of these cases is difficult... You will quickly get used to adding the semicolons, so just hang in there for a little while :-)etc.
in my exceptions. This is also why I want to toggle it on and off.;
insertion could even be dangerous if it is not perfect (and no code is perfect). Imagine the following confusing set of lines:if (this > is
followed by&& a + /* check */
followed bythat * is_very(important |
followed bysecurity | wise))
followed bydo_something();
. If your automatic;
insertion makes a mistake and puts a;
at the end of theif
, then you immediately get a hard-to-detect security bug.#define UNTIL(x) while (!(x))
. Then when you writeUNTIL(out_of_bounds)
, it's impossible to know if that's a function call and therefore needs a terminating;
or a macro that expands towhile
and therefore doesn't need a terminating;
. If you do insert the;
, you are in trouble.;
, they wouldn't have added;
to the language now, would they? ;) Since that's not possible, you can't have a program that correctly inserts it for you either.