(nvim-dap) Clear breakpoints? - neovim

How can I clear all current breakpoints at ones in nvim-dap?
UPD: I wrote this little function to achieve that:
function! ClearBreakpoints()
exec "lua require'dap'.list_breakpoints()"
for item in getqflist()
exec "exe " . item.lnum . "|lua require'dap'.toggle_breakpoint()"
endfor
endfunction

Looks like this was added literally the next day from when you asked.
lua require'dap'.clear_breakpoints()

Related

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

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.

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.

In vim, how do I redirect the output of a vimscript function?

I have this vimscript function
function! Env()
redir => s
sil! exe "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
redir END
return split(s)
endfunction
This function was obtained from this question:
How to list all the environment variables in Vim?
When I do :call Env() I don't see any output, but :echo Env() displays as output the names of all environment variables.
I'd rather copy and paste this output somehow. I know about :redir. However this doesn't work:
:redir #A
:echo Env()
:redir END
"ap
Instead, a blank line is pasted.
I have tried many combinations of the :redir command (to registers and/or files) and variations on call Env() without success. Is this because the output generated is from calling a function? I thought it might be because the function returns a list, but :echo string(Env()) isn't captured by :redir either.
Edit:
This is the modified solution I used from the answer below.
function! Env()
redir => s
sil! exe "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
redir END
let #s = string(split(s))
endfunction
One can then execute :call Env() and then "sp to paste.
related question:
How to redirect ex command output into current buffer or file?
As I said in the comment, this is a problem with :redir that's being discussed on vim_dev. In short, nested redirection with :redir isn't possible.
But of course you can simply modify your Env() function to redirect output to a register or global variable.
Just change redir => s to either redir => g:s (global variable g:s) or redir #s (register s), and remove the return statement. Let's use the register:
function! Env()
redir #s
sil! exe "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
redir END
endfunction
After calling the function, the output is stored in register s and you can put it with "sp, of course.
:let #a+=Env()
instead of
:redir #A
:echo Env()
:redir END
is much more concise, but doesn't print Env()'s output to the screen.
(The asker #Ein's modified solution does neither.)

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