How to simplify this pasting calculator - vim

I have implemented a command in vim which pastes the result of a calculation into your file, i.e. you type
:CalP 34 * 89
and it should paste the result after your cursor.
The code is as follows:
command! -nargs=+ CalP :call Calculator(<q-args>) | normal! p
py from math import *
fun Calculator(arg)
redir #"
execute "py print " a:arg
redir END
let #" = strpart(#", 1)
endfun
This works but is messier than I would like for a simple operation, mainly because:
I don't know a better way to redirect the output of py print ... to the " register
I have to write execute "py print " a:arg because just py print a:arg doesn't work
The let #" = strpart(#", 1) removes the stray newline at the front of the register which py print creates, ideally this should be removed
I think this should be do-able in one line but I don't know enough vimscript.

No scripting is needed for this. In insert mode, you can use <Ctrl-R>=34*89<CR> to insert the result of that calculation.
:help i_CTRL-R
:help expression

I'll second #Amadan's suggestion. If you prefer Python over Vimscript, you can use the pyeval() function, e.g. directly from insert mode:
<C-R>=pyeval('34 * 89')<CR>
If you would like to keep your custom command, that's possible, too:
command! -nargs=+ CalP execute 'normal! a' . pyeval(<q-args>) . "\<Esc>"

Related

Vim: Close a fold on a specific line

I want to programatically close a fold in vim based on whether or not it matches a regular expression. I've defined a function in my vimrc to do so:
" Support python 2 and 3
if has('python')
command! -nargs=1 Python2or3 python <args>
elseif has('python3')
command! -nargs=1 Python2or3 python3 <args>
else
echo "Error: Requires Vim compiled with +python or +python3"
finish
endif
" Define function
func! FoldCopyrightHeader()
Python2or3 << EOF
import re
import vim
# Look at the first 30 lines
header = '\n'.join(vim.current.buffer[0:30])
pattern = 'Copyright .* by .* THE POSSIBILITY OF SUCH DAMAGE'
match = re.search(pattern, header, flags=re.MULTILINE | re.DOTALL)
if match:
# Find the line number of the block to fold
lineno = header[:match.start()].count('\n')
# Remember the current position
row, col = vim.current.window.cursor
# move cursor to the fold block
vim.command('cal cursor({},{})'.format(lineno, 0))
# close the fold
vim.command('call feedkeys("zc")')
# move back to the original position
vim.command('cal cursor({},{})'.format(row, col))
EOF
endfunc
The idea is to search for the pattern, if it exists, then move to where the pattern is, enter the key commands zc to close the fold, and then move back to your original position.
However, this doesn't quite work. If I call this function via :call FoldCopyrightHeader(), then it closes whatever fold the cursor is currently on, and does nothing else.
My guess is that the feedkeys command is asynchronous vim.command('call feedkeys("zc")') and is happening before/after the move cursor commands get executed.
Is there anything I can do to prevent this?
And I solved it as I was typing the question.
Using vim.command(':foldclose') instead of vim.command('call feedkeys("zc")') seems to do the trick.

How to make vim's `:global` command confirm-able before executing the ex Command?

How can I make vim's :global command ask the user if they want to execute the ex command? Similar to what happens with the :substite command with the 'c' option, for example %s:Foo:Fighters:gc
I tried:
:g/mypattern/.s:.*\n::gc
and
:g/mypattern/s:.*\n::gc
but if there is a match on below line it is jumped. For example:
MATCH
NONMATCH
MATCH
MATCH
MATCH
The result is:
NONMATCH
MATCH <<-- this should be erased.
A promptable g/FOO/d would be perfect.
There is no native way to do this. The typical method would be to record a macro and repeat a macro. Making sure you n or / at the end of the macro to advance to the next match. Skipping is now simply n and ## to execute the macro.
Custom :Global command
However if you truly want to have :global command with a confirm you can sort of mimic this by using confirm() inside the your command. The general idea is to do something like this:
:g/pat/if confirm("&yes\n&no", 2) == 1 | cmd | endif
This doesn't quite work for the following reasons:
You have no idea where your cursor is. Need something like :match and :redraw
Does not abort well. Need a way to throw an exception to abort
Very unwieldily to type this all out
I have come up with the following confirming :Global/:G command
command! -nargs=+ -range=% -complete=command Global <line1>,<line2>call <SID>global_confirm(<q-args>)
command! -nargs=+ -range=% -complete=command G <line1>,<line2>call <SID>global_confirm(<q-args>)
function! s:global_confirm(args) range
let args = a:args
let sep = args[0]
let [pat, cmd; _] = split(args[1:], '\v([^\\](\\\\)*\\)#<!%d' . char2nr(sep), 1) + ['', '']
match none
let options = ['throw "Global: Abort"', cmd, '', 'throw "Global: Abort"']
let cmd = 'exe ''match IncSearch /\c\%''.line(''.'').''l''.#/.''/'''
let cmd .= '| redraw'
let cmd .= '| exe get(options, confirm("Execute?", "&yes\n&no\n&abort", 2))'
try
execute a:firstline . ',' . a:lastline . 'g'.sep.pat.sep.cmd
catch /Global: Abort/
finally
match none
endtry
endfunction
Note: Use as-is. Uses IncSearch for highlight and forces \c.
Now you can run :G/foo/d.
Custom :Confirm command
If you rather use a similar technique to the one #Randy Morris provided and use the following :Confirm {cmd} command to confirm {cmd} before execution.
command! -nargs=+ -complete=command Confirm execute <SID>confirm(<q-args>) | match none
function! s:confirm(cmd)
let abort = 'match none | throw "Confirm: Abort"'
let options = [abort, a:cmd, '', abort]
match none
execute 'match IncSearch /\c\%' . line('.') . 'l' . #/ . '/'
redraw
return get(options, confirm('Execute?', "&yes\n&no\n&abort", 2), abort)
endfunction
This will allow you to use :g/foo/Confirm d
For more help see:
:h #
:h q
:h confirm()
:h :exe
:h get()
:h :match
:h :redraw
As far as I know there is no way to do this natively. I think I've hacked together a way to do this but it's probably buggy as I haven't written vimscript in a long time. In this I've defined a command C which accepts an ex command as its arguments. Each line returned via :global is then passed to this ex command if you press y or Y. Any other key causes this line to be skipped.
let s:GlobalConfirmSignNumber = 42
sign define GlobalConfirmMarker text=>> texthl=Search
function GlobalConfirm(cmd)
let line = getpos(".")[1]
execute "sign place " . s:GlobalConfirmSignNumber . " line=" . line . " name=GlobalConfirmMarker file=" . expand("%:p")
redraw!
echomsg "Execute? (y/N) "
try
let char = nr2char(getchar())
if (char == "y" || char == "Y")
execute a:cmd
endif
finally
" Ensure signs are cleaned up if execution is aborted.
execute "sign unplace " . s:GlobalConfirmSignNumber
endtry
redraw!
endfunction
command -nargs=* C call GlobalConfirm(<q-args>)
Here's a gif of it in action. In this gif I'm running the command norm! gUU for every line which contains ba. In this case I confirmed every match by pressing y three times.
If anyone can make improvements to this (especially the signs bit) please edit at will.

can I config the maximum number for the entries in the jumplist

I just want to list the most recent motion locations (say, 10 or less) that I have just located when using the (:jumplist), the default value 100 is too big that I have to page down to the most recent ones.
First of all, there is no command :jumplist, I guess you meant :jumps
There is no option for that setting. but what you could do is, do :jumps as usual, when the "long" list displayed, pressing G will bring you to the most recent ones.
Here is a definition of :Tail (and the corresponding :Head) command, which allows you to limit the output of any Vim command to 10 lines:
:Tail jumps
(showing only the last 10 jumps)
Scriptlet
":[N]Head {cmd} Show only the first 10 / [N] lines of {cmd}'s output.
":[N]Tail {cmd} Show only the last 10 / [N] lines of {cmd}'s output.
function! s:CaptureCommand( command )
redir => l:commandOutput
silent! execute a:command
redir END
redraw " This is necessary because of the :redir done earlier.
return split(l:commandOutput, "\n")
endfunction
function! s:Head( count, command )
let l:lines = s:CaptureCommand(a:command)
for l:line in l:lines[0:(a:count ? a:count : 10)]
echo l:line
endfor
endfunction
function! s:Tail( count, command )
let l:lines = s:CaptureCommand(a:command)
for l:line in l:lines[-1 * min([(a:count ? a:count : 10), len(l:lines)]):-1]
echo l:line
endfor
endfunction
command! -range=0 -nargs=+ Head call <SID>Head(<count>, <q-args>)
command! -range=0 -nargs=+ Tail call <SID>Tail(<count>, <q-args>)
More ideas
You can also use this to define a custom :Jumps command:
command! -range=0 Jumps <count>Tail jumps
With the cmdalias.vim - Create aliases for Vim commands plugin, you can even override the built-in :jumps with it.

Yanking all marked lines in vim

Often times when reviewing log files in vim, I'll highlight interesting lines using marks. At some point, I'd like to be able to copy all of the interesting lines (either all marked lines, or a list of marks) to either a register or another file (it doesn't really matter which; the goal is to facilitate writing a summary). I haven't been able to find any built in way to do this; is it possible in vim?
I suppose it's probably a fairly straightforward function; probably looking something like this, but my vimscript abilities are very weak:
for cur_mark in list_of_marks
goto mark
yank current line and append to register
Has anyone ever written anything similar that they can point me to?
Thanks
EDIT: I posted the accepted solution at https://github.com/mikeage/vim-yankmarks
As always, there are few things that are more motivating than asking for help. Here's what I came up with; feedback welcome.
function! Yankmark()
let save_cursor = getpos(".")
let n = 0
" I should really make this a parameter...
let marks_to_yank="abcdefghijklmnopqrstuvwxyz"
let nummarks = strlen(marks_to_yank)
" Clear the a register
let #a=''
while n < nummarks
let c = strpart(marks_to_yank, n, 1)
" Is the mark defined
if getpos("'".c)[2] != 0
" using g' instead of ' doesn't mess with the jumplist
exec "normal g'".c
normal "Ayy
endif
let n = n + 1
endwhile
call setpos('.', save_cursor)
endfunction
Mikeage had a great idea; here's a more refined version of his function turned into a command:
":YankMarks [{marks}] [{register}]
" Yank all marked (with [a-z] / {marks} marks) lines into
" the default register / {register} (in the order of the
" marks).
function! s:YankMarks( ... )
let l:marks = 'abcdefghijklmnopqrstuvwxyz'
let l:register = '"'
if a:0 > 2
echohl ErrorMsg
echomsg 'Too many arguments'
echohl None
return
elseif a:0 == 2
let l:marks = a:1
let l:register = a:2
elseif a:0 == 1
if len(a:1) == 1
let l:register = a:1
else
let l:marks = a:1
endif
endif
let l:lines = ''
let l:yankedMarks = ''
for l:mark in split(l:marks, '\zs')
let l:lnum = line("'" . l:mark)
if l:lnum > 0
let l:yankedMarks .= l:mark
let l:lines .= getline(l:lnum) . "\n"
endif
endfor
call setreg(l:register, l:lines, 'V')
echomsg printf('Yanked %d line%s from mark%s %s',
\ len(l:yankedMarks),
\ len(l:yankedMarks) == 1 ? '' : 's',
\ len(l:yankedMarks) == 1 ? '' : 's',
\ l:yankedMarks
\) . (l:register ==# '"' ? '' : ' into register ' . l:register)
endfunction
command! -bar -nargs=* YankMarks call <SID>YankMarks(<f-args>)
A different way of accomplishing this might be using the :global command. The global command takes the form :g/{pattern}/{cmd}. The command, {cmd}, will be executed on all lines matching {pattern}.
Append lines matching a pattern to a register:
:g/pattern/yank A
Append matching line to a log file:
:g/pattern/w >> file.log
Of course if you want to find line matching a mark you can match it in your pattern. The following pattern matches a line with mark m.
:g/\%'m/w >> file.log
To do something like this. (Note: I am using \v to turn on very magic)
:g/\v(%'a|%'b|%'m)/yank A
Of course if a pattern won't work you can do this by hand. Instead of marking the lines just build up the lines as you go. Just yank a line to an uppercase register to append.
"Ayy
Or do a write append with a range of a single line
:.w >> file.log
For more help see
:h :g
:h :w_a
:h /\%'m
:h /\v
You can do something like:
:redir #a
:silent marks XYZN
:redir END
"ap
That way the output of the :marks command will be redirected to the a register. Note, that it will only lists (in the above case) the X, Y, Z and N marks (as the arguments), and if there was an a register, it will be deleted/overwritten.
Also note, that it might not give the desired output, but gives you a starting point...
I like the solution from Mikeage, though I would probably solve this with the multiselect - Create multiple selections and operate plugin. This also has the benefit that you don't run out of marks.
With the plugin, you can select lines with <Leader>msa or :MSAdd. Finally, yank all lines with:
:let #a=''
:MSExecCmd yank A
If you use an upper-case register name when yanking into a specific register, Vim will append the yanked content instead of overwriting the register's value.
So, for example:
"ayy - yank current line to register a, overwriting
[move]
"Ayy - append this line to register a
[move]
"ap - paste all yanked material
See :help quotea for more details.

How to make Vim's global command :g/ work on per occurrence basis

Typically Vim's global command :g// works on per line basis. Is it possible to make it work on per occurrence basis as there could be more than one occurrence on a line.
Not a direct answer, but you could use something like :rubydo, which will run some ruby scriptlet per line of code. Combining that with gsub in ruby should get you the ability to do just about anything per occurrence of a match. Of course, you will need to do it with ruby code, which may not give you access to everything that you might need without hassle (register appending would be annoying for instance)
:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
[range], with $_ being set to the text of each line in
turn, without a trailing <EOL>. Setting $_ will change
the text, but note that it is not possible to add or
delete lines using this command.
The default for [range] is the whole file: "1,$".
You can try:
command! -bang -nargs=1 -range OGlobal
\ <line1>,<line2>call s:Global("<bang>", <f-args>)
function! s:Global(bang, param) range
let inverse = a:bang == '!'
" obtain the separator character
let sep = a:param[0]
" obtain all fields in the initial command
let fields = split(a:param, sep)
" todo: handle inverse
let l = a:firstline
while 1
let l = search(fields[0], 'W')
if l == -1 || l > a:lastline
break
endif
exe fields[1]
endwhile
endfunction
Which you can use as :global, except that the command name is different (and that the bang option as not been implemented yet)

Resources