How can I get neovim to return the cursor to the original line after formatting the file? - neovim

In order to solve the file formatting problem of vim, I simply wrote a function:
function FileFormat()
let cursorLine = col(".")
let filetype = &filetype
if filetype == 'json'
%!jq .
execute cursorLine
elseif filetype == 'cpp'
%!astyle --style=attach --pad-oper --lineend=linux -N -C -L -xw -xW -w
execute cursorLine
else
echo "Formatting of " . filetype . " files is not currently supported."
endif
endfunction
And map a shortcut key for this function:
:nnoremap <C-f> :call FileFormat()<cr>
But I found that after formatting the file, the cursor is still at the beginning of the line. I know this is because the cursor disappears when neovim enters command mode, causing the col() function to not get a valid line number.
Is there any other way to solve this problem?
neovim version: 0.6.1

The reason is that I use the wrong function, I should not use the col function, I should get the cursor line number through line("."), this function will not be affected by the mode switch.

Related

Reuse old buffer for the same command in VIM for competitive programming

Recently I've been into VIM and had many attempts to use it for competitive programming. So I tried google stuffs for information and up until now I've gathered codes from other's .vimrc while avoid using plugins (for some specific reasons). So finally I'm now using a pretty decent compile function that I borrowed from Mr.Michael Lan. Now part of my .vimrc look like this :
function! TermWrapper(command) abort
if !exists('g:split_term_style') | let g:split_term_style = 'vertical' | endif
if g:split_term_style ==# 'vertical'
let buffercmd = 'vnew'
elseif g:split_term_style ==# 'horizontal'
let buffercmd = 'new'
else
echoerr 'ERROR! g:split_term_style is not a valid value (must be ''horizontal'' or ''vertical'' but is currently set to ''' . g:split_term_style . ''')'
throw 'ERROR! g:split_term_style is not a valid value (must be ''horizontal'' or ''vertical'')'
endif
exec buffercmd
if exists('g:split_term_resize_cmd')
exec g:split_term_resize_cmd
endif
exec 'term ' . a:command
exec 'setlocal nornu nonu'
exec 'startinsert'
autocmd BufEnter <buffer> startinsert
endfunction
let g:split_term_style = 'vertical'
let g:split_term_resize_cmd = 'vertical resize 80'
command! -nargs=0 CompileAndRun call TermWrapper(printf('g++ -std=c++11 %s && ./a.out', expand('%')))
autocmd FileType cpp nnoremap <leader>fw :CompileAndRun<CR>
As you can see, if I try :CompileAndRun, it will open a new buffer to the right side of my screen, run the .cpp file, etc ... But there is one problem I'm having with this. If you try :CompileAndRun for the second time, it will open a new buffer to run instead of using the old one, which I find ... annoying (well it kinda bug you if your screen for the main cpp file keep getting smaller and smaller, especially in a running contest am I right ?). And I don't see deleting these buffers manually as an option, as it is not very convenient. So can any of you guys help me to cope with this tedious task. Remember that I still want to keep using the :CompileAndRun command, and just want to reuse the buffer opened by the previous command.

Call custom vim completetion menu with Information from Perl-Script

I wrote a script analyzing perl-files (totally without PPI, because it will be used on Servers where the admins don't want PPI to be installed and so on and so forth, but let's not talk about that).
Now, let's say I have this code:
my $object = MySQL->new();
my $ob2 = $object;
$ob2->
(Where MySQL is one of our modules).
My script correctly identifies that $ob2 is a MySQL-Object and sees where it came from, and then returns a list of found subs in that module.
My idea was, that, since I use vim for editing, this could be a really cool way for "CTRL-n"-Completetion.
So, when...
$ob2->[CTRL-n]
It shows the CTRL-n-Box which opens my Perl-Script and gives it a few parameters (I would need: The line that I am actually on, the cursor position and the whole file as it is in vim).
I already found things like vim-perl, which allows me to write something like
if has('perl')
function DefPerl()
perl << EOF
use MyModule;
return call_to_my_function(); # returns all the methods from the object for example
EOF
endfunction
call DefPerl()
endif
But somehow this does not get executed (I tried writing something to a file with a system call just for the sake of testing)...
So, in short:
Does anyone here know how to achieve that? Calling a perl-function from vim by pressing CTRL-n with the full file-code and the line vim is actually in and the position, and then opening a completetion-menu with the results it got from the perl-script?
I hope someone knows what I mean here. Any help would be appreciated.
The details and tips for invoking embedded Perl code from Vim can be found in this Vim Tips Wiki article. Your attempts are already pretty close, but to return stuff from Perl, you need to use Vim's Perl API:
VIM::DoCommand "let retVal=". aMeaningfullThingToReturn
For the completion menu, your Perl code needs to return a List of Vim objects that adhere to the format as described by :help complete-items. And :help complete-functions shows how to trigger the completion. Basically, you define an insert-mode mapping that sets 'completefunc' and then trigger your function via <C-x><C-u>. Here's a skeleton to get your started:
function! ExampleComplete( findstart, base )
if a:findstart
" Locate the start of the keyword.
let l:startCol = searchpos('\k*\%#', 'bn', line('.'))[1]
if l:startCol == 0
let l:startCol = col('.')
endif
return l:startCol - 1 " Return byte index, not column.
else
" Find matches starting with a:base.
let l:matches = [{'word': 'example1'}, {'word': 'example2'}]
" TODO: Invoke your Perl function here, input: a:base, output: l:matches
return l:matches
endif
endfunction
function! ExampleCompleteExpr()
set completefunc=ExampleComplete
return "\<C-x>\<C-u>"
endfunction
inoremap <script> <expr> <Plug>(ExampleComplete) ExampleCompleteExpr()
if ! hasmapto('<Plug>(ExampleComplete)', 'i')
imap <C-x><C-z> <Plug>(ExampleComplete)
endif

Vim script for automatic function insertion

Say I have a class evilLord declared in the file evil_lair.hh and is implemented in the file evil_lair.cc. Now, I want to add the function bool minionDo(std::string command). Is there any script which will put the declaration and empty function definition in the respective files automatically?
I am using c-support vim-plugin which I find useful. Maybe this can be added as a functionality to this script...
The task is not that trivial -- if we want to correctly report the scope of the function. I've already done the work in my :GOTOIMPL (and :MOVEIMPL) command, from my lh-cpp ftplugin suite.
Here is a script which will work:
:let lines = ["bool minionDo(std::string command)"]
:e evil_lair.hh
:call append( line('$'), lines )
:wq
:e evil_lair.cc
:call append( line('$'), lines )
:call append( line('$'), "{}" )
:wq

Vim: change formatting of variables in a script

I am using vim to edit a shell script (did not use the right coding standard). I need to change all of my variables from camel-hum-notation startTime to caps-and-underscore-notation START_TIME.
I do not want to change the way method names are represented.
I was thinking one way to do this would be to write a function and map it to a key. The function could do something like generating this on the command line:
s/<word under cursor>/<leave cursor here to type what to replace with>
I think that this function could be applyable to other situations which would be handy. Two questions:
Question 1: How would I go about creating that function.
I have created functions in vim before the biggest thing I am clueless about is how to capture movement. Ie if you press dw in vim it will delete the rest of a word. How do you capture that?
Also can you leave an uncompleted command on the vim command line?
Question 2: Got a better solution for me? How would you approach this task?
Use a plugin
Check the COERCION section at the bottom of the page:
http://www.vim.org/scripts/script.php?script_id=1545
Get the :s command to the command line
:nnoremap \c :%s/<C-r><C-w>/
<C-r><C-w> gets the word under the cursor to command-line
Change the word under the cursor with :s
:nnoremap \c lb:s/\%#<C-r><C-w>/\=toupper(substitute(submatch(0), '\<\#!\u', '_&', 'g'))/<Cr>
lb move right, then to beginning of the word. We need to do this to get
the cursor before the word we wish to change because we want to change only
the word under the cursor and the regex is anchored to the current cursor
position. The moving around needs to be done because b at the
start of a word moves to the start of the previous word.
\%# match the current cursor position
\= When the substitute string starts with "\=" the remainder is interpreted as an expression. :h sub-replace-\=
submatch(0) Whole match for the :s command we are dealing with
\< word boundary
\#! do not match the previous atom (this is to not match at the start of a
word. Without this, FooBar would be changed to _FOO_BAR)
& in replace expressions, this means the whole match
Change the word under the cursor, all matches in the file
:nnoremap \a :%s/<C-r><C-w>/\=toupper(substitute(submatch(0), '\<\#!\u', '_&', 'g'))/g<Cr>
See 3. for explanation.
Change the word under the cursor with normal mode commands
/\u<Cr> find next uppercase character
i_ insert an underscore.
nn Search the last searched string twice (two times because after exiting insert mode, you move back one character).
. Repeat the last change, in this case inserting the underscore.
Repeat nn. until all camelcases have an underscore added before them, that is, FooBarBaz has become Foo_Bar_Baz
gUiw uppercase current inner word
http://vim.wikia.com/wiki/Converting_variables_to_camelCase
I am not sure what you understand under 'capturing movements'. That
said, for a starter, I'd use something like this for the function:
fu! ChangeWord()
let l:the_word = expand('<cword>')
" Modify according to your rules
let l:new_var_name = toupper(l:the_word)
normal b
let l:col_b = col(".")
normal e
let l:col_e = col(".")
let l:line = getline(".")
let l:line = substitute(
\ l:line,
\ '^\(' . repeat('.', l:col_b-1) . '\)' . repeat('.', l:col_e - l:col_b+1),
\ '\1' . l:new_var_name,
\ '')
call setline(".", l:line)
endfu
As to leaving an uncompleted command on the vim command line, I think you're after
:map ,x :call ChangeWord(
which then can be invoked in normal mode by pressing ,x.
Update
After thinking about it, this following function is a bit shorter:
fu! ChangeWordUnderCursor()
let l:the_word = expand('<cword>')
"" Modify according to your rules
let l:new_var_name = '!' . toupper(l:the_word) . '!'
normal b
let l:col_b = col(".")
normal e
let l:col_e = col(".")
let l:line = getline(".")
exe 's/\%' . l:col_b . 'c.*\%' . (l:col_e+1) .'c/' . l:new_var_name . '/'
endfu

How can I autofold POD in Perl with vim?

I'm trying to edit files with vim and get the POD automatically folded (just the POD, not the Perl). I can't get it to work. I can get folding itself to work because I can manually highlight the lines and type zF and it folds properly.
Can anyone shed light on this?
You forgot
:set foldmethod=syntax
:setf perl
:syntax on
:set foldenable
:syn region POD start=/^=head[123]/ end=/^=cut/ fold
Paste the following at the end of your ~/.vimrc file:
fu! MyFoldSettings()
set foldmethod=expr
set foldexpression=MyFoldLevel(v:lnum)
set foldenable
set foldminlines=1
endf
fu! MyFoldLevel(lev)
let mytext = getline(a:lev)
let result="="
if mytext =~ '^=item'
let result=">3"
elsei mytext =~ '^=back'
let result="<2"
elsei mytext =~ '^=over'
let result=">2"
elsei mytext =~ '^=cut'
let result="<1"
elsei mytext =~ '^=\w\+'
let result='>1'
en
return result
endf
augroup MyFoldSettings_AutoCommands
au!
au BufReadPost {*.pl,*.pod,*.pm} MyFoldSettings()
augroup END
This should work in most cases -- It's just a quick and dirty example, but you can add more too it if you need more functionality. Also, to make certain that this is enabled you might want to add the modeline:
# vim:fen:fdo=hor:fcl=all:fdm=expr:fde=MyFoldLevel(v:lnum):fml=1
If you dont see folded text at this point, try setting the 'foldopen=hor' and 'foldclose=all' which will make navigation into/out of folds easier. Also, if you need this to be applied to other files or filetyps, you can set up the au (autocommand) accordingly. Cheers.
You can put this in your .vimrc:
au FileType perl
\ setlocal foldexpr=getline(v:lnum)=~'^=cut'?'<1':getline(v:lnum)=~'^='?'1':'=' |
\ setlocal foldmethod=expr
Just define perl_fold_* variables, the system perl syntax highlighter will take care of the rest.
In .vimrc, just add
let perl_fold=1
let perl_fold_blocks=1
This is what I'm using,
set syntax
augroup perl_folding
au!
autocmd FileType perl setlocal foldmethod=syntax
autocmd FileType perl setlocal foldlevel=1
autocmd FileType perl let perl_fold = 1
autocmd FileType perl let perl_fold_blocks = 1
autocmd FileType perl setlocal foldenable
au BufRead,BufNewFile *.pm set filetype=perl
augroup END