I guess this question could be taken in two ways...
(Generic) - is there a way to specify settings 'local' to a function (setlocal changes seem to persist after the function call)...
(Specific) - I have a function which gets called from an imap mapping (which takes a user input to pass into the function. The function works perfectly if I run set paste or set noai | set nosi either just before running my shortcut, or added into the function itself. The problem is, whichever way I do it, those setting changes persist after my function call.
Essentially, my workflow is:
In insert mode, type //// at which point I get prompted for input text, which I enter and press enter.
The function is called with my input. I need the function to disable indenting, return my string and then re-enable the previous settings. The string would just be a PHP-block comment like this:
/**
* Blah {INPUT TEXT}
*/
Any suggestions appreciated. My script currently looks like this:
function CommentInjector(txt)
return "\/**" ."\<CR>"
\ . " * foo " . a:txt . " bar " . "\<CR>"
\ . " */"
endfunction
imap <silent> //// <C-R>=CommentInjector(input("Enter some text:"))<CR>
UPDATE
Managed to figure it out at least how to dump a comment in... Would appreciate knowing how to get/restore settings though...
function! CommentInjector(txt)
set paste
exe "normal! i/**\<CR>"
\ . " * fooo " . a:txt . " bar\<CR>"
\ . " */\<Esc>"
set nopaste
endfunction
map <C-C><C-C><C-C> :call CommentInjector(input("Enter some text:"))<CR>
Using this you can just pres Ctrl+C 3 time, enter text when prompted and you get a nice comment written in. It assumes you had "set paste" disabled before running though...
Since you've posted an update and are really just looking at how to save/restore settings, I'll give a general solution.
At the start of your function save the initial value of the setting: let save_paste = &paste
Make any changes to paste that you'd like to make
Restore it at the end: let &paste = save_paste
An example of this can be found in the documentation with :help use-cpo-save where they talk about saving and restoring the value of cpoptions.
Related
I created unite source cmdmatch which lets you among other things fuzzy complete wildmenu items. I would like to find a better method to obtain the completion list (method I use is given here and is problematic because with large number of completion items screen will be filled up entirely [to see why just press :<c-a> if you didn't remap <c-a>])
The other solution would be to hide vim's cmd line completely while I am grabbing the list although I don't think that is possible in vim (or at least limit the amount of text it displays so it doesn't fill up screen). Any ideas ?
EDIT
First try has the same flickering problem although it looks like it works faster
fu! GetCompletion(input)
call feedkeys(":" . a:input . "FF\<esc>\<cr>")
endf
cno FF <C-A><C-\>eg:Save()<CR>
fu! g:Save()
let g:x = getcmdline()
endf
Result can be seen as:
:call GetCompletion('help a')
:echo x
SOLUTION
let g:cmdmatch = {}
fu! g:cmdmatch.set_c( base ) dict
let self.c = a:base
endfu
fu! GetCommandCompletion( base )
cno [MATCH] <c-a><c-\>eg:cmdmatch.set_c(getcmdline())<cr>
sil! exe 'norm :' . a:base . '[MATCH]'
cu [MATCH]
retu g:unite_cmdmatch.c
endf
Test: echo GetCommandCompletion("help '")
The trick is to execute the entire completion with :silent, so that the output won't actually happen (but completion is still magically done). The following function retrieves the completions already parsed into a List, by triggering the completion and then wrapping the output in :return split('...'). Of course, you can also :return '...' if you need a single string.
function! GetCommandCompletion( base )
silent execute "normal! :" a:base . "\<C-a>')\<C-b>return split('\<CR>"
endfunction
demo
:let cmds = GetCommandCompletion('no')
:echo cmds
['noautocmd', 'nohlsearch', 'noreabbrev', 'noremap', 'noremenu', 'normal']
I'm trying to run a function whenever a line is altered, but there doesn't seem to be a specific autocommand for this. I could run the function on CursorMoved, but it would slow down editing. I could also map the function for all the major editing movements, but it could get very messy as I'm trying to keep each line independent of the others. If there's no solution, I could possibly do a diff every few seconds to see what's changed and run the function on the changed lines, but again it's a messy solution.
Any ideas?
If you can get Vim 7.4, have a look at the TextChanged (and TextChangedI, for insert mode) events. (Note that this will monitor changes throughout the entire buffer.)
You could do something like this with a BufWritePre event. Have a master file that defines your lines and tokens, then in the slave files, a reference to their master, like:
master-Foo.vim:
let b:Dom_slaves = ['/foo/bar/slave.cpp', '...', ...]
let b:Dom_map = {
\ 10 : "do your laundry",
\ 20 : "prepare your lunch"
\ }
slave.cpp
/* Dom_master = master-Foo.vim */
...
cout << "I will gladly /* Dom-id:10 */ and /* Dom-id:20 */.\n";
Dominate.vim
let MSMap = {}
autocmd BufWritePre * call s:Dominate()
function! s:Dominate()
" if current buffer's Dom_slaves and Dom_map defined
" read & update all slaves with Dom_map mappings
" else see if 'Dom_master = somefile' appears in the buffer
" update mapped values from cache or read master file, cache and update
" endif
endfunction
By the way, this is a horrible thing to do to your vim. :P
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(Vim)diff two subroutines in same file
Sometimes I see a block of code I suspect to be identical to another block in the same file, but it's a bit too long for visual inspection and I may just be missing something. I've tried to visually select the block and yank to the the default register, put that register into / (find), but it didn't match even the original block.
Is there a way to select a section, yank it in a register, select another section then diff the two, without creating a bunch of new files? I imagine the diff results opening in a new buffer in a tab or split.
EDIT: My question is basically a duplicate of This one. I found this answer to be the most helpful & closest to what I was looking for. The only thing I'd change is to make it output in Unified format so it looks like the diff output I'm used to (it has more info as well). I suppose this means using a different diff utility.
Inspired from my lh#path#strip_common() function:
echo matchstr(#a.'##'.#b, '^\zs\(.*\)\ze.\{-}##\1.*$')
will show what is common between registers #a and #b.
You can show more information with:
function ShowDiff(a,b)
" I expect neither string to contain '##'
let start = matchstr(a:a.'##'.a:b, '^\zs\(.*\)\ze.\{-}##\1.*$')
let end= matchstr(a:a.'##'.a:b, '^.\{-}\zs\(.*\)\ze##.\{-}\1$')
let a = a:a[len(start): -len(end)-1]
let b = a:b[len(start): -len(end)-1]
echo "identical beginning: ".strlen(start )." chars -> ".start
echo "identical ending : ".strlen(end)." chars -> ".end
echo "typical to a : ".strlen(a)." chars -> ".a
echo "typical to b : ".strlen(b)." chars -> ".b
endfunction
Used with:
:call ShowDiff(#a, #b)
You could use the following sequence assuming that the two segments are already in registers, 'a and 'b. Could probably be put into a macro or function.
new
only
put a
diffthis
vnew
put b
diffthis
This creates a new buffer, makes it the only visible buffer, puts 'a into it, sets it up to be diff'd, then opens a new buffer in a vertical split, puts 'b into this split empty buffer and also sets it up to diff. Immediately vim (or gvim) will show the differences.
When done, type :ls to get the list of buffers, use :buffer *N* to return back to the original file and use :bdel! *N* to delete the created buffers (named "[No Name]").
Here's a function to open two new windows side by side, each containing the specified register contents (called as DiffRegs(#a, #1), for instance) and diff them. The new buffers will not be written or modifiable:
" A list for bookkeeping..
let g:diffreg_buffers = []
function! DiffRegs(reg1, reg2)
" Preserve the unnamed register
let s:nonamereg = ##
let ## = a:reg1
" new window
:new
normal P
setlocal nomodifiable
setlocal buftype=nofile
diffthis
call add(g:diffreg_buffers, bufnr('%'))
let ## = a:reg2
:vsp +enew
normal P
setlocal nomodifiable
setlocal buftype=nofile
diffthis
call add(g:diffreg_buffers, bufnr('%'))
let ## = s:nonamereg
endfunction " DiffRegs(reg1, reg2)
" Function to wipe all buffers we're diffing with the function above
function! EndDiffs()
for buffer in g:diffreg_buffers
exe ':buffer ' . buffer
diffoff
quit
endfor
let g:diffreg_buffers = []
endfunction " EndDiffs()
You can bind those to key combinations of your choice, but if you don't call EndDiffs() after each call to DiffRegs(), you'll run into issues.
To compare quickly two different parts of a file, you can split the view in two by using:
:sp horizontal split
or
:vsp vertical split
Once you have splitted the screen, you must use :diffthis in each window to hightlight the differences. (Then :diffoff to leave diff mode)
Then to go back to a single window you can quit one of them with :q or use CTRLwo
I want to make a vim cheat sheet plugin. It's real simple:
I want to toggle my cheatsheets. A vertsplit toggle, like Taglist or NERDTree.
I want the cheatsheet to be filetype specific. So I toggle my c++ cheatsheet when I have opened a .cpp file.
I want the cheatsheet to be horizontally split. So it shows two files, my syntax cheat sheet and my snippet trigger cheat sheet.
I already have a collection of these cheatsheets, in vimhelp format, but now I have to manually open them.
I haven't really done any vim scripting, but I imagine this would be really simple to put together. I'm sorta sick of googling unrelated codesnippets, so what I'm asking here is:
Could anyone give me a short sum-up of what I need to learn in regards to vim scripting to piece this together. What I have a hard time finding is how to toggle the buffer window.
If you know any intro tutorials that covers the material I need to get this up and running, please provide a link.
tx,
aktivb
The function below may not do exactly what you want, and I haven't tested it, but it should give you some ideas.
The main idea is that the function reads the filetype of the current buffer (you can test this by typing :echo &ft) and then sets the path of the appropriate cheat sheat. If it exists, this path is then opened (read-only and non-modifiable) in a split window. You can then call this function any way you wish, for example by mapping it to the {F5} key as shown.
I'm not sure about the toggling possibilities (is this really easier than just closing the split window?) but you could look at the bufloaded() function, which returns whether or not a given file is currently being accessed.
function! Load_Cheat_Sheet()
let l:ft = &ft
if l:ft == 'html'
let l:path = 'path/to/html/cheat/sheet'
elseif l:ft == 'c'
let l:path = 'path/to/c/cheat/sheet'
elseif l:ft == 'tex'
let l:path = 'path/to/tex/cheat/sheet'
endif
if l:path != '' && filereadable(l:path)
execute ':split +setlocal\ noma\ ro ' l:path
endif
endfunction
map <F5> :call Load_Cheat_Sheet()<CR>
Hope this helps. Just shout if anything is unclear, or you want to know more.
I had forgotten about this until I got a notice about Eduan's answer. Since I posted this question I've done quite a bit of vim scripting, including getting this to work:
let g:cheatsheet_dir = "~/.vim/bundle/cheatsheet/doc/"
let g:cheatsheet_ext = ".cs.txt"
command! -nargs=? -complete=customlist,CheatSheetComplete CS call ToggleCheatSheet(<f-args>)
nmap <F5> :CS<CR>
" strip extension from complete list
function! CheatSheetComplete(A,L,P)
return map(split(globpath(g:cheatsheet_dir, a:A.'*'.g:cheatsheet_ext)),
\ "v:val[".strlen(expand(g:cheatsheet_dir)).
\ ":-".(strlen(g:cheatsheet_ext) + 1)."]")
endfun
" specify cheatsheet or use filetype of open buffer as default
" instead of saving window status in a boolean variable,
" test if the file is open (by name). If a boolean is used,
" you'll run into trouble if you close the window manually with :wq etc
function! ToggleCheatSheet(...)
if a:0
let s:file = g:cheatsheet_dir.a:1.g:cheatsheet_ext
else
if !exists("s:file") || bufwinnr(s:file) == -1
let s:file = g:cheatsheet_dir.&ft.g:cheatsheet_ext
endif
endif
if bufwinnr(s:file) != -1
call ToggleWindowClose(s:file)
else
call ToggleWindowOpen(s:file)
endif
endfun
" stateless open and close so it can be used with other plugins
function! ToggleWindowOpen(file)
let splitr = &splitright
set splitright
exe ":vsp ".a:file
exe ":vertical resize 84"
if !splitr
set splitright
endif
endfun
function! ToggleWindowClose(file)
let w_orig = bufwinnr('%')
let w = bufwinnr(a:file)
exe w.'wincmd w'
exe ':silent wq!'
if w != w_orig
exe w_orig.'wincmd w'
endif
endfun
Thought I would add to Goulash's answer.
I think in order to implement the toggle you would simply use some if statements and a global variable.
let g:cheatsheet_toggle_on=0
if (g:cheatsheet_toggle_on == 0)
" Turn the cheatsheet on
" Also make sure to know that the toggle is on:
let g:cheatsheet_toggle_on=1
elseif (g:cheatsheet_toggle_on=1
" Do whatever you need to turn it off, here
endif
Hope this figures out that logic. :)
If I have an existing highlight group in vim via link, for example
hi link my_highlight_group my_default_color
Is it possible to add 'bold' to my_highlight_group without changing my_default_color? Following does not work:
hi my_highlight_group gui=bold
Surprisingly, I can add bold if my_highlight group is defined directly (not via link):
hi my_highlight_group guifg=#F0000
hi my_highlight_group gui=bold
As "too much php" has said, there is no direct way to say "create a value that looks like that one and add bold". The best way is to modify your colour scheme. If you're not using a custom colour scheme, copy one from the main vim installation directory to your ~/.vim/colors directory and edit it to suit you. Alternatively, search on the vim scripts page and try some of the many that are available.
Shameless plug: if you want one that's easier to edit than the standard format, try my "Bandit" colour scheme.
If you really want to be able to add bold on the fly, you'll need a fairly complex script, like the one below. Note that this won't be saved for your next session unless you call it automatically after loading your colour scheme or by doing something like:
:autocmd ColorScheme AddBoldToGroup my_highlight_group
The script in all it's enormity is below. As far as I am aware, there is no significantly quicker way of doing this! Obviously you could save a few lines by writing less verbose code, but the general idea of using redir and silent hi repeatedly is the only way.
" Call this with something like
"
" :AddBoldToGroup perlRepeat
"
command! -complete=highlight -nargs=1 AddBoldToGroup call AddBoldToGroup(<f-args>)
function! AddBoldToGroup(group)
" Redirect the output of the "hi" command into a variable
" and find the highlighting
redir => GroupDetails
exe "silent hi " . a:group
redir END
" Resolve linked groups to find the root highlighting scheme
while GroupDetails =~ "links to"
let index = stridx(GroupDetails, "links to") + len("links to")
let LinkedGroup = strpart(GroupDetails, index + 1)
redir => GroupDetails
exe "silent hi " . LinkedGroup
redir END
endwhile
" Extract the highlighting details (the bit after "xxx")
let MatchGroups = matchlist(GroupDetails, '\<xxx\>\s\+\(.*\)')
let ExistingHighlight = MatchGroups[1]
" Check whether there's an existing gui= block
let MatchGroups = matchlist(ExistingHighlight, '^\(.\{-}\) gui=\([^ ]\+\)\( .\{-}\)\?$')
if MatchGroups != []
" If there is, check whether "bold" is already in it
let StartHighlight = MatchGroups[1]
let GuiHighlight = MatchGroups[2]
let EndHighlight = MatchGroups[3]
if GuiHighlight =~ '.*bold.*'
" Already done
return
endif
" Add "bold" to the gui block
let GuiHighlight .= ',bold'
let NewHighlight = StartHighlight . GuiHighlight . EndHighlight
else
" If there's no GUI block, just add one with bold in it
let NewHighlight = ExistingHighlight . " gui=bold"
endif
" Create the highlighting group
exe "hi " . a:group . " " NewHighlight
endfunction
Changing the attributes on a group which is linked to another will disconnect the link. AFAIK there is no easy way to copy the colors from my_default_color into my_highlight_group. You will just have to copy the color values by hand.
This shouldn't be a big issue though, you should have all your highlight groups defined in your colorscheme file, so just put those two next to each other:
hi my_default_color guifg=#000088
hi my_highlight_group guifg=#000088 gui=bold
Years had passed and now the question has a much more straightforward solution: hlget() and hlset() functions.
It's still true that if you want to modify a color scheme, copying it first and modify the copied version would be a better solution.
An example of adding bold attribute to types' highlight:
" Get the highlight group, resolving links
let hl = hlget("Type", v:true)[0]
" Set GUI attributes
let hl.gui = hl->get("gui", {})->extend(#{ bold: v:true })
" Set the highlight group
call hlset([hl])
Assuming you only want this for one syntax type, you should just make a new group name, my_bold_default_color, and apply the bold attribute to that.