How can I see the places a function is being called in vim? - perl

I have been using vim and ctags for going over Perl files.
I was wondering the following:
Is there a way/plugin I can use so that I can press a key combination and be able to see all the places a function is called (as easily as I can jump to the definition of a function or view the list of all functions with the same name in all files of my project with ctags)?
I have read about cscope but I am not sure if it is usable for Perl or there is a solution for scripting languages.

Maybe this is what you are looking for:
" Lists lines containing the word under the cursor. Type the line number
" followed by <CR> to jump to the corresponding line.
nnoremap [I [I:
xnoremap [I "vy:<C-u>ilist /<C-r>v<CR>:
" Same as above but for 'defines'
nnoremap [D [D:
xnoremap [D "vy:<C-u>dlist /<C-r>v<CR>:
Then position the cursor under a word and type [I or [D.

Related

How to use vimgrep to grep work that's high-lighted by vim?

I've installed vimgrep plugin, inside vim, under normal mode I can type:
:vimgrep mywords %
to search "mywords" for the documents under current directory.
But I wish that in normal mode, when I highlight a word using gd, or in visual mode use 'viw' to select a word, I use a hot key to vimgrep. So I add in my .vimrc and restart vim:
vnoremap <F8> :vimgrep expand('<cword>') %<CR>
Well it didn't work for me, when I put focus on one word and select it, I press F8, no response in vim. How to achieve it?
Thanks!
Vimscript is evaluated exactly like the Ex commands typed in the : command-line. There were no variables in ex, so there's no way to specify them. When typing a command interactively, you'd probably use <C-R>= to insert variable contents:
:vimgrep <C-R>=expand('<cword>')<CR> '%'<CR>
... but in a script, :execute must be used. All the literal parts of the Ex command must be quoted (single or double quotes), and then concatenated with the variables:
execute 'vimgrep' expand('<cword>') '%'
Actually, there's a built-in command for inserting the current word into the command-line: :help c_CTRL-R_CTRL-W:
:vimgrep <C-R><C-W>
Your mapping
You could use all three approaches; let's use the last:
vnoremap <F8> :<C-u>vimgrep <C-r><C-w> %<CR>
The <C-u> clears the '<,'> range that is automatically inserted.
What you probably wanted
Using the current word from visual mode is strange. You probably wanted to search for the current selection. There's no expand() for that. Easiest is to yank, as outlined by #ryuichiro's answer:
vnoremap <F8> y:vimgrep /<C-r>"/ %<CR>
Still missing is escaping of the literal text (:vimgrep searches for a regular expression pattern), and of the / delimiter:
vnoremap <F8> y:execute 'vimgrep /\V' . escape(##, '/\') . '/ %'<CR>
Robust plugin solution
Now, if you also want to avoid the clobbering of the default register, it gets really complicated. Have a look at my GrepHere plugin; it provides a {Visual}<A-M> mapping for exactly that.
Try
vnoremap <F8> y:vimgrep "<c-r>"" %<CR>
:h y
:h <C-r>
Recommended reading: Mapping keys in Vim - Tutorial (Part 1)

Emacs - multiline search

Is there a module to do multiline search in Emacs ?
I have used grep, pt and now ag, and they are great. But sometimes when exploring a codebase you search for things that are on multiple lines (and therefore get nothing), but reducing the search to fewer words yields a lot of results.
Is there a way to get multiline search in Emacs in a whole project ?
Of course there is. There are multiple ways.
You don't say what kind of search you are trying to do: single file? multiple buffers? regexp? Do you need a fixed list of search hits or do you want incremental search?
Here's one simple answer:
In a Dired buffer, mark some files you want to search, then hit A.
Type a regexp to match. Use C-q C-j to enter a newline char to match. Hit RET to enter the regexp.
That searches through the marked files, stopping at each search hit in turn. Use M-, to go to the next hit, etc.
For example, to search for doc strings of variable definitions, search with this regexp or similar:
Search marked files (regexp): (defvar.*
.*"Non-nil
There's a newline char after the first .*, which you enter using C-q C-j.
There are lots of other ways to search in Emacs. The best place to start is the Search and Replace category of Emacs Wiki. There you can find ways to search broken down by main characteristics and described.

use :tjump instead of :tag vim on pressing ctrl-]

in vim,
when i press ctrl-]
1) the usual behavior:
it goes to the first matching tag with the word under the cursor,
after that we need to do :tjump separately to see a list of all matching tags and then jump to the desired tag.
2) what i want is:
vim should search the tag file,
if there are multiple matches, show me a list of all the matching tags
if there is one match, simply jump to the matching tag
(just like :tjump does)
this behavior(2) already happens when we use g-ctrl-], but i want it to happen with ctrl-]
i have seen behavior(2) using ctrl-] in some vims in some linuses.
please tell me how i can obtain behavior(2). in other words,
please tell me how i can make ctrl-] to behave like g-ctrl-] using .vimrc or whatever
This will map <c-]> to g<c-]> for both normal and visual modes.
nnoremap <c-]> g<c-]>
vnoremap <c-]> g<c-]>
I suggest you map g<c-]> to <c-]>. In other words just swap the commands.
nnoremap g<c-]> <c-]>
vnoremap g<c-]> <c-]>

Is there a feature in Emacs for function call hierarchy

I am maintaining someone else's code. The code is written in C using GCC 4.4.3 on Linux platform. However, the code jumps around a lot and its difficult to find out where all the functions are called from.
In Visual Studio, there is a feature called 'Call Hierarchy' which will display where functions are called from and called to. Does Emacs (23.1.1) have any such feature?
The classic Emacs way to jump to calls is to use TAGS file and use the M-. command. I recommend using Exuberant C Tags with the following command in the root directory of your project :
ctags -e --c-kinds=+pxd -R .
Then using visit-tags-table you can open the TAGS file. With M-. you can jump to each definition or call to your keyword. Use C-u M-. to jump to another occurrence. Use C-x z z z... to repeat the search.
If you have many projects, you can create a TAGS file for each one of them and then call visit-tags-file to add the TAGS file to your list of TAGS files to search from.
Another classic way (the un*x way), is to use the command M-x find-grep to search for occurrences of your keyword.
http://cedet.sourceforge.net/symref.shtml

How can I script vim to run perltidy on a buffer?

At my current job, we have coding-style standards that are different from the ones I normally follow. Fortunately, we have a canned RC file for perltidy that I can apply to reformat files before I submit them to our review process.
I have code for emacs that I use to run a command over a buffer and replace the buffer with the output, which I have adapted for this. But I sometimes alternate between emacs and vim, and would like to have the same capabilities there. I'm sure that this or something similar is simple and had been done and re-done many times over. But I've not had much luck finding any examples of vim-script that seem to do what I need. Which is, in essence, to be able to hit a key combo (like Ctrl-F6, what I use in emacs) and have the buffer be reformatted in-place by perltidy. While I'm a comfortable vim-user, I'm completely clueless at writing this sort of thing for vim.
After trying #hobbs answer I noticed that when filtering the entire buffer through perltidy the cursor returned to byte 1, and I had to make a mental note of the original line number so I could go back after :Tidy completed.
So building on #hobbs' and #Ignacio's answers, I added the following to my .vimrc:
"define :Tidy command to run perltidy on visual selection || entire buffer"
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy
"run :Tidy on entire buffer and return cursor to (approximate) original position"
fun DoTidy()
let l = line(".")
let c = col(".")
:Tidy
call cursor(l, c)
endfun
"shortcut for normal mode to run on entire buffer then return to current line"
au Filetype perl nmap <F2> :call DoTidy()<CR>
"shortcut for visual mode to run on the current visual selection"
au Filetype perl vmap <F2> :Tidy<CR>
(closing " added to comments for SO syntax highlighting purposes (not required, but valid vim syntax))
DoTidy() will return the cursor to its original position plus or minus at most X bytes, where X is the number of bytes added/removed by perltidy relative to the original cursor position. But this is fairly trivial as long as you keep things tidy :).
[Vim version: 7.2]
EDIT: Updated DoTidy() to incorporate #mikew's comment for readability and for compatibility with Vim 7.0
My tidy command:
command -range=% -nargs=* Tidy <line1>,<line2>!
\perltidy (your default options go here) <args>
If you use a visual selection or provide a range then it will tidy the selected range, otherwise it will use the whole file. You can put a set of default options (if you have any) at the point where I wrote (your default options go here), but any arguments that you provide to :Tidy will be appended to the perltidy commandline, overriding your defaults. (If you use a .perltidyrc you might not have default args -- that's fine -- but then again you might want to have a default like --profile=vim that sets up defaults only for when you're working in vim. Whatever works.)
The command to filter the entire buffer through an external program is:
:%!command
Put the following in ~/.vimrc to bind it to Ctrl-F6 in normal mode:
:nmap <C-F6> :%!command<CR>
For added fun:
:au Filetype perl nmap <C-F6> :%!command<CR>
This will only map the filter if editing a Perl file.
Taking hobbs' answer a step further, you can map that command to a shortcut key:
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
noremap <C-F6> :Tidy<CR>
And another step further: Only map the command when you're in a Perl buffer (since you probably wouldn't want to run perltidy on any other language):
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm command! -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm noremap <C-F6> :Tidy<CR>
Now you can press Ctrl-F6 without an active selection to format the whole file, or with an active selection to format just that section.
Instead of creating a new keyboard shortcut, how about replacing the meaning of the = command which is already in people's finger memory for indenting stuff? Yes, perlcritic does more than just indent but when you use perlcritic anyways, then you probably don't want to go back to the inferior "just indent" = command. So lets overwrite it!
filetype plugin indent on
autocmd FileType perl setlocal equalprg=perltidy
And now we can use = just like before but with the added functionality of perlcritic that goes beyond just indenting lines:
== run perlcritic on the current line
5== run perlcritic on five lines
=i{ Re-indent the 'inner block', i.e. the contents of the block
=a{ Re-indent 'a block', i.e. block and containing braces
=2a{ Re-indent '2 blocks', i.e. this block and containing block
gg=G run perlcritic on the entire buffer
And the best part is, that you don't have to learn any new shortcuts but can continue using the ones you already used with more power. :)
I'm used to select text using line oriented visual Shift+V and then I press : an I have !perltidy -pbp -et4 somewhere in history so I hit once or more up arrow ⇧.