Tune cindent "switch" indentation - vim

Nemerle is a C-like language and mostly works very well with cindent. However, its construct analogous to switch is called match:
match (x) // switch (x)
{ // {
| "Hello World" => ... // case "Hello World": ...
| _ => ... // default: ...
} // }
Is it possible to get the cinoptions for switch statements to apply to this construct, instead? Maybe there is a regular expression I can set somewhere. If not, can I get the vertical bars to align with the braces another way?
Update
Here is what I came up with:
" Vim indent file
" Language: Nemerle
" Maintainer: Alexey Badalov
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
" Nemerle is C-like, but without switch statements or labels.
setlocal cindent cinoptions=L0
" Enable '|', disable ':'.
setlocal indentkeys=0{,0},0),0#,0\|,!^F,o,O,e
setlocal indentexpr=GetNemerleIndent()
let b:undo_indent = "setl cin< cino< indentkeys< indentexpr<"
function! GetNemerleIndent()
" Nemerle is C-like; use built-in C indentation as a basis.
let indent = cindent(v:lnum)
" Set alignment for lines starting with '|' in line with the opening
" brace. Use default indentation outside of blocks.
if getline(v:lnum) =~ '^\s*|'
call cursor(v:lnum, 1)
silent! normal [{
if line('.') == v:lnum
return indent
endif
return indent(line('.'))
endif
return indent
endfunction

See :h indent-expression to get a foothold in the Vim documentation. Basically I think you will want to write your own "indent file" for your filetype, which will return an indentexpr with appropriate spaces for your match structure, and otherwise (assuming that's only change) return the usual cindent() value. It involves a little more than just setting a regular expression, the indent file will have Vim commands and structures to evaluate lines and return correct value. As documentation says, best way to learn how they work is to look at some of the indent files for other languages. . . . (C doesn't have an indent file for you to look at because it's all integrated into Vim's own c source code, but most other languages have indent files using Vimscript.)

Related

Vim - add one line above the fold to the folding

When I am coding in vim, I use set foldmethod=syntax which folds my code.
It looks something like this then:
How can I add the line above the { to the fold? So that it would look something like this:
So the idea is that it (always) takes the line above the fold into the fold.
How can I make this happen?
This needs a custom fold expression (h fold-expr) indenpendently from:
foldtext <- you want to fold one more line
foldignore <- you still want to fold { and } whatever the indent
In your vimrc:
" Callback: Fold level <- next line indent
function! FoldMethod(lnum)
let l:indent = max([indent(a:lnum+1), indent(a:lnum)])
return l:indent / &shiftwidth
endfunction
set foldmethod=expr
set foldexpr=FoldMethod(v:lnum)

Mapping/macro to 'smartly' auto-create pairs of apostrophes in vim (and ignore contractions)

I'm currently using closepairs for my auto-closing needs, and it works pretty well. However, there is one caveat -- apostrophes. Don't get me wrong, I need apostrophes closed all the time. I don't want to just disable them. But whenever I type in plain text, whenever there are any contractions (I'm, Don't, Can't)...these apostrophes get made.
Now I could just type to delete them as soon as they can, but doing it every time is a bit impractical.
Does anyone know how I can possibly modify the closepairs script to only autoclose single quotes/apostrophes if they are the start of a word? That is, they are preceded by a whitespace character?
Here is the current code:
inoremap <expr> " <SID>pairquotes('"')
inoremap <expr> ' <SID>pairquotes("'")
function! s:pairquotes(pair)
let l:col = col('.')
let l:line = getline('.')
let l:chr = l:line[l:col-1]
if a:pair == l:chr
return "\<right>"
else
return a:pair.a:pair."\<left>"
endf
I don't know closepairs, but the AutoClose - Inserts matching bracket, paren, brace or quote plugin handles this well. You'll find a list of plugin alternatives on the Vim Tips Wiki.
Are you sure you want to autocomplete only after whitespace? In that case, something like function('string') would not autocomplete after the parenthesis.
Regardless, you can check the previous character against some regex. For example, to avoid autocompletion after letters:
function! s:pairquotes(pair)
let l:line = getline('.')
let l:col = col('.')
let l:chr = l:line[l:col - 1]
let l:prev = l:line[l:col - 2]
if l:chr == a:pair
return "\<right>"
elseif l:prev !~ "[A-Za-z]"
return a:pair . a:pair . "\<left>"
else
return a:pair
endif
endfunction
Note that there are exceptions even with this conservative example, like typing r'regex' in Python, so it might also make sense to define filetype-specific behavior.

How to ignore space after comments when calculating indent level in Vim

Consider writing a JavaDoc-style comment which includes an indented list (when expandtab is set and softtabstop=2):
/**
* First line:
* - Indented text
*/
Currently, after typing First line: and hitting return, Vim will correctly insert *<space>. However, when I hit tab to indent the second line, only one space will be inserted instead of two.
Is it possible to fix this, so the space after * will be ignored during indent calculations?
I am still a beginner at VimScript, but I cooked this up for you. Give it a try and let me know what you think.
function AdjustSoftTabStop()
" Only act if we are in a /* */ comment region
if match(getline('.'), '\s*\*') == 0
" Compensate for switching out of insert mode sometimes removing lone
" final space
if match(getline('.'), '\*$') != -1
" Put back in the space that was removed
substitute/\*$/\* /
" Adjust position of the cursor accordingly
normal l
endif
" Temporary new value for softtabstop; use the currect column like a
" base to extend off of the normal distance
let &softtabstop+=col('.')
endif
endfunction
function ResetSoftTabStop()
" Note that you will want to change this if you do not like your tabstop
" and softtabstop equal.
let &softtabstop=&tabstop
endfunction
" Create mapping to call the function when <TAB> is pressed. Note that because
" this is mapped with inoremap (mapping in insert mode disallowing remapping of
" character on the RHS), it does not result in infinite recursion.
inoremap <TAB> <ESC>:call AdjustSoftTabStop()<CR>a<TAB><ESC>:call ResetSoftTabStop()<CR>a

vim - set auto indent to fill the leading space with space or tabstop

It seems if we enable 'ai', vim will fill the the leading space with tabstop.
I can make it fill with just space with 'et'. I don't like a C file mixed with space and tabstop.
My vimrc:
set ts=4 et
set ai
set hlsearch
syntax on
filetype plugin indent on
autocmd FileType make setlocal noexpandtab
However, in some condition I do need to input tabstop when I hit the 'TAB' on keyboard, for example, in makefile and some others.
The 'autocmd FileType' command is not good: I can't add every file type in vimrc.
What I want is simple:
autoindent to fill leading area with
space;
when hit 'TAB' on keyboard, tabstop
input, not space (so no 'et')
How to do it?
inoremap <expr> <tab> ((getline('.')[:col('.')-2]=~'\S')?("\<C-v>\t"):(repeat(' ', &ts-((virtcol('.')-1)%&ts))))
It does the same as #Lynch answer if I read it correctly.
You can also use <C-v><Tab>: this will insert <Tab> without invoking any mappings and ignores expandtab unless you remapped <C-v> or <C-v><Tab> for some reason.
If you want to just insert tab do
inoremap <Tab> <C-v><Tab>
It will ignore expandtab setting.
I did it using a function. I tested it, but maybe in some particular case you will have to fix some bugs. Try adding this to your vimrc:
set et
function! Inserttab()
let insert = ""
let line = getline('.')
let pos = getpos('.')[2]
let before = ""
let after = line
if pos != 1
let before = line[ 0: pos - 1]
let after = line[pos : strlen(line) ]
endif
if pos != 1 && substitute(before, "[ \t]", "", "g") != ""
let insert = "\t"
else
let insert = " "
endif
let line = before . insert . after
call setline('.', line)
call cursor(line('.'), strlen(before . insert))
endfunction
inoremap <tab> <esc>:call Inserttab()<CR>a
Basicaly it does remap your key in visual mode to the function Inserttab(). Also note that if you change ts for something other than 4 it will still output 4 spaces instead of two because the value is hard coded.
Also im not very familiar with vim scripts, but I think all the variables used will be global which is a bad thing.
I forgot to mention that to "see" white spaces you can use set list. You disable this with set nolist. Also in normal mode you can use ga to see information about the character your cursor is on.
Edit
I realise that you may want to insert tab at the beginin of the line. My script insert space at the begining and tab anywhere else.
If you really want a tab every time you hit tab key you could simply use this:
set et
function! Inserttab()
let insert = ""
let line = getline('.')
let pos = getpos('.')[2]
let before = ""
let after = line
if pos != 1
let before = line[ 0: pos - 1]
let after = line[pos : strlen(line) ]
endif
let insert = "\t"
let line = before . insert . after
call setline('.', line)
call cursor(line('.'), strlen(before . insert))
endfunction
inoremap <tab> <esc>:call Inserttab()<CR>a
But I dont see the point, with this version you will never be able to indent manually from insert mode.
One way to do it is
:set sw=4 (or whatever you want)
:set ts=46 (or some large number)
Then autoindent will not insert tabs unless you reach 46 spaces, in which case you can put in a higher number.
Only drag about this is if someone else is using tabs, then you have to reset ts to agree with the file you are editing. On the other hand, it will make the tabs immediately obvious, which can be desirable as well.

How do I define indents in vim based on curly braces?

I use https://github.com/cakebaker/scss-syntax.vim for syntax highlighting SCSS (or SASS) files on vim, which works very well for syntax highlighting. However, the plugin does not come with an indent file and am having trouble writing one.
I would like to set the indent to look like this:
However, if i do gg=G, I get:
I suspect that it does not understand nested indent based on braces. I tried all the different combinations of
set cindent
set nocindent
set autoindent
set smartindent
and tried to use the code from Tab key == 4 spaces and auto-indent after curly braces in Vim , including
set tabstop=2
set shiftwidth=2
set expandtab
...but nested braces indent never seems to work.
I believe that I might want to write a custom indent file, and all I need is indentation based on braces with nested levels. How should I go about this? If someone has an indentation file for filetypes with similar syntax, that will be great as well.
This is a quick hack, based on the built-in perl indentation code (in indent/perl.vim). Hopefully you can use it to get what you want to do. See the more detailed comments in either the perl indentation code or another one of the files in the indent directory for more details.
setlocal indentexpr=GetMyIndent()
function! GetMyIndent()
let cline = getline(v:lnum)
" Find a non-blank line above the current line.
let lnum = prevnonblank(v:lnum - 1)
" Hit the start of the file, use zero indent.
if lnum == 0
return 0
endif
let line = getline(lnum)
let ind = indent(lnum)
" Indent blocks enclosed by {}, (), or []
" Find a real opening brace
let bracepos = match(line, '[(){}\[\]]', matchend(line, '^\s*[)}\]]'))
while bracepos != -1
let brace = strpart(line, bracepos, 1)
if brace == '(' || brace == '{' || brace == '['
let ind = ind + &sw
else
let ind = ind - &sw
endif
let bracepos = match(line, '[(){}\[\]]', bracepos + 1)
endwhile
let bracepos = matchend(cline, '^\s*[)}\]]')
if bracepos != -1
let ind = ind - &sw
endif
return ind
endfunction
Save that file as ~/.vim/indent/something.vim where something is your file type (replace ~/.vim with the path to vimfiles if you're on Windows.
You might also want to stick this at the start of the file (but only if there isn't some other indent declaration that might be loaded first):
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1

Resources