Vim: Highlight C++ variables using scope? - c++

I would like to have C++ variables highlighted by scope. E.g. variables should have different coloring depending on file, class, global or local scope. Is it possible?
UPDATE: External helpers (e.g. ctags/clang and vim scripts) are welcome.
UPDATE 2: This really should be possible using libclang. Take a look at Doug Gregor presentations here: http://llvm.org/devmtg/2010-11/ I think just no one has done it yet... or?

http://www.vim.org/scripts/script.php?script_id=2646
From the script website:
This set of scripts is designed to increase the number of highlighting groups used by Vim. This makes it quicker and easier to spot errors in your code. By using ctags and parsing the output, the typedefs, #defines, enumerated names etc are all clearly highlighted in different colors.
There are some screenshots available to show the comparison.
http://sites.google.com/site/abudden/contents/Vim-Scripts/ctags-highlighting

One trick I have seen but don;t use:
If you declare your variables with the appropriate prefix g_, m_ etc.. You can use this to get vim to colour them differently:

Related

fortran modules -- finding where variables are defined/assigned

I am trying to extract a portion of a large fortran to make it its own program. A particular subroutine imports many modules (only two shown here as an example):
subroutine myroutine(aa,bb)
use xx_module
use yy_module
...
end subroutine myroutine
There are a lot of variables introduced in the ... portion that are imported from these modules. Is there a good way (or good tools) to find out which variables come from which module, and so on? Or I have to look through each module to see where each is defined, and then assigned (which may possibly occur in a different module...)?
On a UNIX/Linux system:
grep -ni "variable" filenames
is what I commonly do from a command line. Here, variable is the name of the variable you are looking for, filenames is name of the file (or more files) that you are searching through. This should give you insight right away about what variables come from what module. You can take on detective work from there. When in doubt, type "man grep".
SciTools Understand does, amongst many others, just that sort of thing.
Double click on a variable, takes you to the definition. Then search through
occurances.
In case you use eclipse, there is Photran, a plugin for working with Fortran projects. I don't use it myself, so I'm not 100 % sure, but I think it should be able to do what you require.

"How can I jump to functions in a C++ project with vi

I am making a c++ program with vi. It has only one file but it's getting kind of big. It would be nice if I could easily see all the functions I created and jump to any one of them without having to search for them. Can vi do this, or is there a similar program that can?
This seems like a dup of Jump to function definition in vim.
To sum up that answer, use ctags, and take a look at Vim and Ctags tips and tricks.
I use a vim plugin to do this :
http://www.vim.org/scripts/script.php?script_id=273
It summarizes classes, struct, function, with jump functionality.

Vim modules for navigating C++ code base?

Besides ctags / cscope,
what are good vim plugins for navigating C++ code base?
Ideally, when my cursor is on a variable name, I would like to be able to know:
what is the class of this variable,
what functions can I call on this varaible,
jump to the class of this varaible ...
and do this across multiple namespaces.
What tools do you suggest?
You might look into Eclim, Vim and Eclipse integration. Best of both worlds.

How to update all C/C++ identifier names in a project

After frequently coming across recommendation not to use leading and double underscores in C/C++ identifiers I decided to fix all our sources once and for all. What I need to do now is convert _Identifier to Identifier_.
Should I use a specialized tool for this task of regular expressions will do for this job? In latter case what is the pattern to match C/C++ identifier?
Although I am one of those that frequently points out that names with leading underscores may be reserved, I strongly recommend you don't do this unless you are experiencing problems caused by the names. Making this global change will make your version control system less useful than it might otherwise be by causing all sorts of spurious diffs. Also, there is a chance of creating duplicate names.
Also, there are many underscore prefixed names that re perfectly valid. One thinks immediately of __LINE__ and __FILE__ etc, and of all the names of non-standard functions that may be provided by
your specific implementation. Filtering out those names will be far from trivial (I would say next to impossible), certainly a simple Perl or sed script will not be enough.
So instead, I would encourage you to change the names on a file by file basis as you make other changes to the code. Change your programming standards to avoid the leading underscore names and write new code in accordance with the standards.
If you use Visual Studio, there are refactoring plugins such as Visual Assist X to help you with this.
Perl should do the job, but there's Coccinelle for when it gets tricky.
Netbeans can do this for the whole project, using the Refactor->Rename menu command. But it only works for a single identifier a time, so you'll need to reiterate for every identifier you need to change.
If your regression tests are solid, then you should have no problems if you just write a quick perl script to replace everything and run the test suite. If you don't have solid regression tests...well, then you can do the perl script replacement and just rebuild the code. If the compilation works, then chances are pretty good that everything is fine. In other words, try the quick solution, and only use a specialized tool if that doesn't work.

Any program or trick to find the definition of a variable?

Many times when I am watching others code I just want to find where and how a variable is defined. Normally what I do now is look for the type of the variable until I find the definition, that is very time consuming. And I guess that there are some tools that can help me in this rutinary situation. Any suggestion in some tools or commands to help me in this task?.
I know that using a GUI and creating a project this is done automatically I am talking of a way to do this without a GUI. I am working with only text mode. I am running under Linux and I am using C/C++, but suggestions for other languages are welcome.
Thanks a lot.
A possible solution
Michel in one of his comments propose a simple an effective solution define again the variable, in that case in compilation time, the compiler will inform where is the previous definiton. Of course to apply this solution we need to think previously in the locality of the variable.
You've already given the most appropriate tool: an IDE. This is exactly the kind of thing which an IDE excels at. Why would you not want to use an IDE if you're finding development painful without one?
Note that Emacs, Vim etc can work as IDEs - I'm not talking about forcing you the world of GUIs if you want to stay in a text-only situation, e.g. because you're SSHing in.
(I'm really not trying to be rude here. I just think you've discounted the obvious solution without explaining why.)
Edit: OK, you say you're using C++. I'm editing my response. I would use the C preprocessor and then grep for the variable. It will appear in the first place.
cpp -I...(preprocessor options here) file.cpp | grep variable
The C preprocessor will join all the includes that the program uses, and the definition has to be before any usage of that variable in the file. Not a perfect thing, but without an IDE or a complete language description/managing tool, you only have the text.
Another option would be using ctags. It understands the C and C++ syntaxes (among others), and can be searched for variables and functions using command line tools, emacs and vi, among others.
I use cscope and ctags-exuberant religiously. Run it once on my code base and then in Vim, I can use various commands like ^] or [D or [I or similar to find any definitions or declarations for a given word.
This is similar to facilities provided by mega-IDEs like Visual Studio and Eclipse.
Cscope also functions as a stand-alone tool that performs these searches.
I use one of three methods:
I will use CTags to process my source tree (nightly) and then can easily use commands in Vim (or other editors) to jump right to the definition.
I will just use grep (linux) or findstr (windows) to look for all occurrences of the variable name or type. The definition is usually quite obvious.
In Vim, you can just search backward in the scope and often find what you are looking for.
Grep for common patterns for variable declarations. Example: *, &, > or an alphanumeric followed by one or more whitespace characters then the name of the variable. Or variable name followed by zero or more whitespace characters, then a left parenthesis or a semicolon. Unless it was defined under really weird circumstances (like with some kind of macro), it works every time.
In VIM you can use gd to see local variable declarations or gD to see global variable declarations, if they're defined in the current file. Reference Go_to_definition_using_g
You can also use [i to see the definition without jumping to it, or [I to see all occurrences of the variable in all the included files as well, which will naturally show the definition as well.
If you work in Microsoft Visual Studio (which I think you could use for C++ as well, but would require working on a Windows workstation) there's an easily accessible right-click menu option for "Go to Definition...", which will take you to the definition of any currently marked variable, type or method.
if you insist on staying text mode, you can do this with either emacs or vi with the appropriate plug-ins.
But really, move into the 21st century.
EDIT: You commented that you are doing this over SSH because you need the build speed of the remote server cluster.
In that case, mount the drive on your local machine and use an IDE, and just SSH in to kick off a build.