VIM and custom tagging - vim

I am using vim in windows to edit assembly code. It is a nonstandard language and disassembly of the binary is done by a custom script so I define the format myself. I would like to use tags to be able to jump through the code for subroutine calls. I have searched around quite a bit and all roads seem to lead to using ctags to generate a tags file, but obviously this won't work in my case as I am not dealing with C code. How is it possible to create a custom tag file? Here is an example of the code. First, each subroutine is defined by the keyword and the hex offset (the first column).
Subroutine e2b7
e2b7 2c c0 11 03 BBS [Branch if bits are '1'] #$03, $11c0, 00e2ce ($12)
e2bc a9 00 LDA [Load A with mem] #$00
. blah
. blah
. blah
And somewhere in the code a jump to the sub is executed;
d9ad 20 b7 e2 JSR $e2b7
Thanks for any help you can provide

Use ctags together with the taglist-plugin (http://vim.sourceforge.net/scripts/script.php?script_id=273).
The source package of ctags contains the file EXTENDING.html which
describes how to define an extension. I did this for several languages.
Here two examples (make(1) and POD (perl old document)):
%%%%%%%%%% file '~/.ctags' %%%%%%%%%%%%%%%%%%%%
--langmap=perl:+.pod
--regex-perl=/^=head1[[:space:]]*(.+)/\1/o,pod/
--regex-perl=/^=head2[[:space:]]*(.+)$/. \1/o,pod/
--regex-perl=/^=head3[[:space:]]*(.+)$/.. \1/o,pod/
--regex-perl=/^=head4[[:space:]]*(.+)$/... \1/o,pod/
--regex-perl=/^=for[[:space:]]+([^:]+):(.*)$/*\1:\2/o,pod/
--regex-perl=/^__(DATA|END)__$/__\1__/l,labels/
--regex-make=/^([^:# \t]+)[ \t]*:($|[^=]+)/\1/t,targets/
To use this with taglist you need two additional lines in ~/.vimrc .
For the above examples:
%%%%%%%%%% file '~/.vimrc' %%%%%%%%%%%%%%%%%%%%
let tlist_perl_settings = 'perl;c:constants;f:formats;l:labels;p:packages;s:subroutines;d:subroutines;o:POD'
let tlist_make_settings = 'make;m:makros;t:targets'
This screenshot shows the taglist navigation window
with additional POD section.

ctags supports a lot of languages, including assembly -- if your favorite variant isn't included, perhaps you could add it in...?

Related

How to paste previously prepared code snippets in Vim?

In Windows, using the AutoHotkey utility, it's possible to write simple scripts to expand some text in an editor of choice (e.g. Visual Studio's editor).
For example, if in Visual Studio editor I type:
d1 [TAB]
(i.e. press the keys in sequence: d,1,Tab) the above "d1" text can be replaced with one or more lines of code snippets. The mapping between "d1" and the expanded lines of code is specified in a AutoHotkey script.
This is very convenient e.g. for demos; for example: at some point if I'd like to enter a whole function body, assuming that I associated it to e.g. "d3", I can simply press d3Tab on the keyboard, and I get the function body automatically pasted in the editor in current cursor location; and I can have different code snippets associated to different key combinations, e.g.
d1 --> DoSomething() function definition
d2 --> class Foo definition
d3 --> test code xyz...
Is it possible to achieve the same goal using Vim?
In other words, I'd like to have a set of code snippets previously prepared, and I'd like to paste each one of them in my currently edited source code file in Vim, in a way similar to what I described above.
Basic expansion can be done via the built-in abbreviations, for example:
:inoreabb d1 DoSomething()<CR>{<CR><CR>}<CR><Up><Up>
Read more at :help abbreviations.
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.
I have previously used snipMate that does something like what you're describing.
http://www.vim.org/scripts/script.php%3Fscript_id%3D2540

How to split lines in Zen Coding for vim

Whenever I use Zen Coding for vim I usually have the same issue: When I want to wrap a paragraph with p the output would look like this:
<p>Text</p>
However, I'd like it to output like this:
<p>
Text
</p>
How do I tell the zencoding plugin to split the output onto several lines?
In official Zen Coding (your Vim plugin has unofficial implementation, but it tries to stick close to it), you need to apply xml filter to your abbreviation. It will apply XML profile with different formatting options.
So, in your case, you may try to wrap text with p|xml abbreviation.
For further information on output tweaking, you may want to read the following:
http://docs.emmet.io/customization/syntax-profiles/
http://docs.emmet.io/filters/

How to find all occurrences of a variable in Vim?

In vim, how to I find all occurrences of a variable in files under a certain directory?
I know vimgrep works sometimes, but it looks for text only and doesn't work if other classes have variables of the same name and I only want the variable under a specific class.
What should I do? Or should I get an IDE instead?
Why would you want to use another IDE when you already have one? Vim is an IDE that is configurable and usable for different languages..
You could use cscope to build a database of your code. This database
Allows searching code for:
all references to a symbol
global definitions
functions called by a function
functions calling a function
text string
regular expression pattern
a file
files including a file
Further features of Cscope:
Curses based (text screen)
An information database is generated for faster searches and later reference
The fuzzy parser supports C, but is flexible enough to be useful for C++ and Java, and for use as a generalized 'grep database' (use it to browse large text documents!)
Has a command line mode for inclusion in scripts or as a backend to a GUI/frontend
Runs on all flavors of Unix, plus most monopoly-controlled operating systems.
Once your database is created, you could browse through the usages of your variables, functions, etc.
Edit (slightly off-topic):
another cool thing that's quite handy when working with Vim on code is the taglist plugin that uses Ctags:
The "Tag List" plugin is a source code browser plugin for Vim and
provides an overview of the structure of source code files and allows
you to efficiently browse through source code files for different
programming languages.
cscope step by step example
Go to the base directory of your project, and run:
cscope -Rb
This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.
Note that cscope is not designed to work with other languages other than C. Sometimes it does work for other C-like syntax languages like Python, and you can force it to recognize those files with hacks such as cscope -Rb -s * and others mentioned at: Using cscope to browse Python code with VIM? but it won't work as well as for C.
Open vim, and run:
:cs add cscope.out
:cs find s my_func
s is a mnemonic for symbol. The other cscope provided queries are also possible.
The cscope interface (ouside Vim) also has a variable assignment query (subset of symbol occurrences) which Vim does not seem to offer (?)
This adds a list of the callers to the quickfix list, which you can open with:
:copen
Go to the line that interests you and hit enter to jump there.
See also:
automatically add the nearest database (parent directories) when you enter a file: how to auto load cscope.out in vim
for function calls: How to find the callers and callee of a function in C code in vi/vim?

Search tags only in current file

I am using ":ta " to jump to a method.
For example i got two classes named A.java and B.java. They both have a foo() method and B.java have another method called fooBar(). Then i open A.java and input :ta foo then press TAB then i will got two completion : foo and fooBar. But what i want to jump now is just tag in current file, i don't like tag in other file to display.
And i found tagslist does very good in this job. So if i can use the tag generated by taglist to search from, it will be very nice.
Depending on how many times you call your methods a couple of * may be enough.
Without using tags, gd can be used to go to the local declaration of the method under your cursor. I tend to choose the most low-tech solution usually, so I would go with this one.
But ctags is also able to generate tags for a single file only or for an arbitrary selection of files. It can be done in a few steps but it's definetely not as straightforward as what you are accustomed to do…
Create a file with the name(s) of the file(s) you want to scan. Let's say it's called files.txt and it's located at the root of your working directory.
Generate your tags file using the -L <file> argument: ctags -L files.txt.
At this point you should have a tags file containing only the tags present in the file(s) specified at step 1.
Generating different tags files for the whole project and for single files may be useful, here. A short script generating a tags file named after the current file and making it the sole tags source may make the whole thing easier.
EDIT
Actually, TagList and TagBar don't generate tags files. The output of the ctags <options> command they run is used internally and parsed with all kinds of regexp to filter by scope or filename or whatever.
Unfortunately this cannot be done using ctags. Ctags does not respect context, it is a pure list of all possible "functions". Try to open a tag file with an editor (e.g. vim) and you will see it is just a list of "functions" (in case of Java they are "methods"). Example:
getDesc src/com/redhat/rhn/internal/doclet/Handler.java /^ public String getDesc() {$/;" m class:Handler
getDoc src/com/redhat/rhn/internal/doclet/ApiCall.java /^ public String getDoc() {$/;" m class:ApiCall
Vim just search the file "as is" without giving it any context - it just search for a "function". It is able to search for files, classes, methods, enums etc. Tags format is described in more detail here: http://ctags.sourceforge.net/FORMAT
In Vim you have few possibilities. There are several plugins that gives Vim some context sensitivity, but you cannot use tags for that. Vim itself has a feature called OmniComplete and there are few plugins dedicated for Java. Then you can use Ctrl-X Ctrl-O to start a completition. I recommend you to map this to a different key (maybe Ctrl-Space if you like). More info about Java OmniComplete plugins here:
Vim omnicompletion for Java
Eclim (http://eclim.org/) is very comperhensive, but difficult to setup (you need to run Eclipse in the background). JDE script is easier and also robust (http://www.vim.org/scripts/script.php?script_id=1213). And please note IntelliJ IDEA Community Edition (free) also has a very nice Vim plugin that is free to use. But I understand you - Vim is Vim.
Good luck!
Not exactly an answer to your question, but it seems like there's no way to do exactly what you need, so, i would recommend you the following: for your Java development in Vim, try eclim.
This tool helps you to use your favorite text editor Vim with power of an Eclipse (IDE).
I can't find analogue for tab-completion of :ta, but i know a smart analogue for g] : this is a command :JavaSearchContext. You can map it to something.
For example, if you have two classes A and B, and you have method foo() in each class, then g] will ask you every time you want to jump to foo(), but :JavaSearchContext will always jump to the proper declaration of foo().
Of course, there are many other features.

Lookup a specific kind of tag in Vim

So here's my problem. I've gotten exuberant ctags working with Vim, and it works great, most of the time. One thing that still irks me though is whenever I try to search for a function that is named the same as some variable name. I sometimes get the right tag on the first try, sometimes not. Then after I pull up the list of alternate tags with :tselect, it comes up with a list of tags for both function definitions or variable definitions/assignments. (I'm in PHP so definitions and assignments are syntactically indistinguishable).
However, I notice that there's a column labeled 'kind' that has a value of 'f' or 'v', for function and variable, respectively. I can't seem to find a whole lot of information about this field, it seems like it may not be exactly standardized or widely used. My question is: can you filter tag results in Vim by "kind"?
Ideally, the default would be to search the whole tags file, but by specifying some extra flag, you could search a specific ('f' or 'v') kind only.
This is such a small problem for me as it doesn't come up THAT often, but sometimes it's the small problems that really annoy you.
You can certainly generate ctag files with any combination of php-kinds that you want (see the ouput of the command ctags --list-kinds.)
If you feel it's worth the effort you can make a vim function tagkind and bind it to a command. The tagkind function can overwrite the current tags vim variable to point at only the tag file with the kinds that you are interested in and call :tag. Optionally, it can store the previous version of the tags variable and restore it after this one call.
Unfortunately, I don't know of anyway other than this. Perhaps someone else would know.
I generate python ctags with --python-kinds=-i to exclude tags for import statements (which are useless). Maybe you could generate with --php-kinds=-v and drop a class of tags completely.
You can read :help tag-priority. Apparently the "highest-priority" tag is chosen based on some hard-coded logic.
fzf with fzf.vim has a :Tags (for the whole project) and :BTags for the current file option that generates ctags on the fly.
An issue raised on the plugin 'Skip tag kinds in :BTags and :Tags' gives the following code that you can use to only generated tags for a particular kind. I've modified the below so that it should only search for the PHP f kind.
command! BTagsEnhanced
\ call fzf#vim#buffer_tags(<q-args>, [
\ printf('ctags -f - --sort=no --php-kinds=f --excmd=number --language-force=%s %s', &filetype, expand('%:S'))], {})
Note as per my comment on the question, there is a potential Vim tagfinder.vim plugin via a blog post on Vim and Ctags: Finding Tag Definitions. But I haven't tried it.

Resources