how to dynamically change menu title in VIM's toolbar? - vim

I am doing a vim plugin for personal use, and it triggers from toolbar.
when I click Plugin in GVIM toolbar, it create a sub menu called 'common'.
When I click common, it start check my vim settings and create sub menu, for instance, show/hide line number. But the following code has some problem, and cannot work. What's wrong with it?
amenu &Plugin.&Common :call <SID>createSubMenu()<CR>
fun! s:createSubMenu()
let isNum = &number
if isNum == '1'
amenu &Plugin.&Common.Hide\ Line\ &Number :set nonu
else
amenu &Plugin.&Common.Show\ Line\ &Number :set nu
endif
endfunction
--------------------- Resolved -----------------------
The code above cannot be used. It's completed wrong.
Please refer to Luc Hermitte's answer.

You need to :unmenu the old entry to remove it.
BTW, I already have a fully functional toggling/cycling engine for values, than also updates menus. It's done with a function from lh-vim-lib, see the test file to have examples of use, and the documentation for more explanations.

:amenu works like map commands: you must include <CR> at the end.
:amenu &Plugin.&Common.Hide\ Line\ &Number :set nonu<CR>
Also, <SID> and s: are not the same.
You should also probably scope isNum to your script with s:
An additional remark: why don't you have a single menu entry that proposes to toggle line numbering and calls :set nu!?

Related

GVim Highlight multiple words plugin ans saving these Highligts

I like the Highlight multiple words plugin in my GVim but when i quit and re-open this editor all highlights disappears.
plugin description is here
:Hsave and :Hrestore do the job but the global variable contain the word highlighted ans disparead when i close GVim.
I've used :let g: to check
:set viminfo^=! is inside viminfo
I've seen mksession and source in your answers
So, what's not right ?
Thanks.
For troubleshooting, try to manually define a persistent variable, e.g.:
:let g:TEST_VAR = 'abc'
With :set viminfo+=!, the value should persist after a Vim restart:
:echo g:TEST_VAR
abc
If this works, the plugin's persistence should, too. If not, check the contents of the viminfo file and any modifications of 'viminfo'.
Note that my Mark plugin is an alternative, and also has marks persistence. It relies on the same persistent variable mechanism.
Thx to have decrypted my english :)
I'v trying this :
:echo has('viminfo')
show 1
:set viminfo?
show viminfo='100,<50,s10,h
:set history?
show history=20
My work is :
\m
(many times...)
On the word "Fabrice" i press 1 on keypad, it become red for foreground.
:Hsave CHIN
:Hclear
:Hrestore CHIN
(it's right)
:let g:
This is store :
HI_SAVE_CHIN hl1^I\<Fabrice\>^I-1^I101
and:
:let g:TEST_VAR = 'HI_SAVE_'
(or HI_SAVE_CHIN ?)
:set viminfo+=!
:echo g:TEST_VAR
It shows :
HI_SAVE_
In .viminfo :
Variables globales:
!HI_SAVE_CHIN STR hl1 \<Fabrice\> -1 101
!TEST_VAR STR HI_SAVE_
I'have doubt about HI_SAVE_ or HI_SAVE_CHIN.
These lines stays in .vimfo when GVim is open but i can't :Hrestore CHIN (Chin or chin), let g: nothing.
And it be deleted when i quit.
I's hard job ! i will try your plugin, maybe it's more easy for me !
I may not have respect the rules of this site but I have found the solution to my problem so I am writing this to all. It may be an incompatibility with unnecessary files (I do not believe it) can be settings, nocompatility mainly but I have disabled all in vimrc and I put :set viminfo+=! because he did not want to get into viminfo. Thanks for your elegant plugin Ingo and your help.

Is it possible to grep the quickfix window in Vim?

Let’s say I use the ag.vim plugin to search for the string disabled through multiple files. It returns me some results in the quickfix window:
1 first_file.rb|1 col 1| disabled something something
2 second_file.rb|1 col 2| disabled another something
Is it possible to pick the quickfix results as an input, grep through them, and open results in new quickfix window? In other words, if I would enter :quickfix_grep first_file, new quickfix would pop up with only one entry:
1 first_file.rb|1 col 1| disabled something something
Update
A vim plugin has been written for this requirement: https://github.com/sk1418/QFGrep
Original Answer:
My understanding of your goal is:
Your grep result is somehow huge in your quickfix, you want to narrow
your view of it. by entering a command with regex, filter the grep
result. The filtered result should also be displayed in QuickFix
window, so that you could open/jump to the file.
If the above is what you want, check out the following:
source this function and the command line:
function! GrepQuickFix(pat)
let all = getqflist()
for d in all
if bufname(d['bufnr']) !~ a:pat && d['text'] !~ a:pat
call remove(all, index(all,d))
endif
endfor
call setqflist(all)
endfunction
command! -nargs=* GrepQF call GrepQuickFix(<q-args>)
then after your grep/ack/whatever show stuffs in your quickfix, you could type
:GrepQF <regex>
to do filtering in your quickfix.
Here I add an GIF animation. I am using Ack instead of grep, but it makes no difference. The given regex will match filename and the text showing in quickfix. I did filtering twice to show that.
hope it helps.
New official vim plugin cfilter
Since 21.8.2018 (patch: 8.1.0311) the plugin cfilter is distributed with vim in $VIMRUNTIME. It is documented under :h cfilter-plugin.
Load plugin cfilter when needed or load it always in your vimrc
:packadd cfilter
Filter quickfix list with
:Cfilter DPUST
My solution to this problem has always been to make the quickfix
buffer modifiable by default:
:autocmd BufReadPost quickfix set modifiable
(The above command is to be put in the .vimrc file.)
Doing this opens a whole range of possibilities for any appropriate
edits of the grepping results, such as removing unrelated entries
manually or by filtering using the :global and :vglobal commands
(which provide the functionality desired in the question for free);
grouping and reordering related entries; adding blank lines or
comments in free form; etc.
Here's a shorter & neater version of #Kent's answer:
function! GrepQuickFix(pat)
call setqflist(filter(getqflist(), "bufname(v:val['bufnr']) !~# a:pat"))
endfunction
command! -nargs=* GrepQF call GrepQuickFix(<q-args>)
It is the same code, just neater and shorter, I don't believe it deserves a separate plugin.

How to detect a function in Vim?

If the cursor is in somewhere within a very long function, is there a way to let Vim tell the user in which function he/she is editing?
By the way, I use taglist but seems that taglist does not auto update where you are even if you have moved the cursor to a different function.
The taglist plugin provides this feature. The function in which
the cursor is currently positioned is highlighted automatically in the list of
functions of taglist.
Make sure that Tlist_Auto_Highlight_Tag is not equal 0 to enable this feature.
'updatetime' defines the time of no activity which must elapse before
taglist highlights the current function. Default is 4 seconds.
:help taglist.txt See section "Highlighting the current tag"
As a quick test:
Type :TlistHighlightTag to force taglist to highlight the current function.
If this works I suppose that you have disabled the automatic highlighting in any
way (see Tlist_Auto_Highlight_Tag).
As an addition to Habi's answer, if you want to do it without using taglist, you can quite easily define a function that will work it out. It depends what language you're programming in, but for C-like languages, you can do this:
nmap ,f call ShowFuncName()
" Show the name of the current function (designed for C/C++, Perl, Java etc)
fun! ShowFuncName()
let lnum = line(".")
let col = col(".")
echohl ModeMsg
echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW'))
echohl None
call search("\\%" . lnum . "l" . "\\%" . col . "c")
endfun
Put that it in your vimrc and then press ,f to see the current function.
Taken from here.

How do I turn on search highlighting from a vim script?

If I do either of the following two:
call search("searchString")
exec "/ searchString"
From a script, then vim does the search but does not highlight the results, even though hlsearch. Doing the same searches from outside a script highlights the results.
Just found out the answer myself:
call search(l:searchString)
call matchadd('Search', l:searchString)
The
feedkeys()
function is the key (pun intended):
call feedkeys("/pattern\<CR>")
or cleaner:
" highlights – or doesn’t – according to 'hlsearch' option
function SearcH(pattern)
let #/ = a:pattern
call feedkeys("/\<CR>")
endfunction
I know this is late. However when I searched for the answer to this problem this page came up. So I feel compelled to help fix it.
call search(l:searchString)
call matchadd('Search', l:searchString)
Did not work for me. (when run from inside a function) It did higlight the words I wanted to search for but n/N wouldn't cycle between them. Also when I performed a new search the "l:serachStirng" pattern still remained highlighted. This answer on this link worked much better
Vim search and highlighting control from a script
Which gave me:
let #/ = l:searchString
then run
normal n
outside the funciton (so the highlighting is done immediately without the user needing to press n)
To turn on, press ESC type :set hls
To turn off, press ESC type :set nohls
Found answer here:
http://vim.1045645.n5.nabble.com/highlighting-search-results-from-within-a-function-tt5709191.html#a5709193
```
One solution would be
function! XXXX()
execute '/this'
return #/
endfunction
and to use the following instead of ":call XXXX()".
:let #/ = XXXX()
```
I believe this works from inside a function
(to just enable highlighting and nothing more):
call feedkeys(":\<C-u>set hlsearch \<enter>")
You need to put this in your .vimrc file
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
The .vimrc file is usually located in your home directory, or you can find it using "locate .vimrc"

Remove the Toolbar in GVim

How do I remove the Toolbar (which has the open save buttons) in GVim?
:set guioptions-=T
See also:
:help 'guioptions'
The best way to do this is to use the -= operator (as shown in the current accepted answer by #a paid nerd). Add this line to the configuration file ($MYVIMRC)
:set guioptions-=T
In order to find out what the above line does (see below), besides issuing a help command, you can also find it here.
I have my .vimrc set as follows
set guioptions=aegimrLt
where the definition for each of the options can be found by issuing
:help guioptions
Another one:
:set toolbar=
:help 'toolbar'

Resources