aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Odding <peter@peterodding.com>2010-08-09 17:52:56 +0200
committerPeter Odding <peter@peterodding.com>2010-08-09 17:52:56 +0200
commit89f9205ff840329aa75166249a99b1d0f227d661 (patch)
tree539040f39d4f9079ebdddecffe7e4502406fda7a
parent9d4c5c8823411cddaeee61a58141e453ea897da0 (diff)
downloadvim-easytags-89f9205ff840329aa75166249a99b1d0f227d661.tar.gz
Enable commands like :UpdateTags -R ~/.vim
* The :UpdateTags command now passes any arguments given to it on to Exuberant Ctags, which sounds simple enough but required a complex implementation. This means users can now do things like: :UpdateTags -R ~/.vim Because Ctags can be run easily from inside Vim, users won't have to run it manually from the command line. The plug-in also makes sure the required command-line options to Ctags are always used. * Fixed a problem most users will probably never encounter. I'm not sure whether this is by design or an implementation detail but it's possible for the "!_TAG_FILE_SORTED" header to appear after one or more tags and Vim will apparently still use the header! For this reason the easytags#write_tagsfile() function should also recognize it, otherwise Vim might complain with "E432: Tags file not sorted". * Always sort tags in easytags#write_tagsfile() (understands foldcase) * Finally documented the :UpdateTags and :HighlightTags commands. * The dynamic highlighting can now be disabled for individual buffers, which can come in handy when the highlighting conflicts with another plug-in: Just set the 'b:easytags_nohl' variable to any value. * Removed hard wrapping from README.md because I've recently switched to a 21" external monitor ;-)
-rw-r--r--README.md174
-rw-r--r--TODO.md6
-rw-r--r--autoload.vim251
-rw-r--r--easytags.vim8
4 files changed, 229 insertions, 210 deletions
diff --git a/README.md b/README.md
index 2ffc638..f8c2f27 100644
--- a/README.md
+++ b/README.md
@@ -1,132 +1,80 @@
# Automated tag generation and syntax highlighting in Vim
-[Vim] [vim] has long been my favorite text editor and combined with [Exuberant
-Ctags] [exuberant_ctags] it has the potential to provide most of what I expect
-from an [integrated development environment] [ide]. Exuberant Ctags is the
-latest incarnation of a [family of computer programs] [ctags] that scan
-source code files to create an index of identifiers (tags) and where they are
-defined. Vim uses this index (a so-called tags file) to enable you to jump to
-the definition of any identifier using the [Control-\]][jump_to_tag] mapping.
-
-When you're familiar with integrated development environments you may recognize
-this feature as "Go-to definition". One advantage of the combination of Vim and
-Exuberant Ctags over integrated development environments is that Vim supports
-syntax highlighting for [over 500 file types] [vim_support] (!) and Exuberant
-Ctags can generate tags for [over 40 file types] [ctags_support] as well...
-
-There's just one problem: You have to manually keep your tags files up-to-date
-and this turns out to be a royal pain in the ass! So I set out to write a Vim
-plug-in that would do this boring work for me. When I finished the plug-in's
-basic functionality (one automatic command and a call to [system()][system]
-later) I became interested in dynamic syntax highlighting, so I added that as
-well to see if it would work -- surprisingly well I'm happy to report!
-
-## Install & first use
-
-Unzip the most recent [ZIP archive] [latest_zip] file inside your Vim profile
-directory (usually this is `~/.vim` on UNIX and `%USERPROFILE%\vimfiles` on
-Windows), restart Vim and execute the command `:helptags ~/.vim/doc` (use
-`:helptags ~\vimfiles\doc` instead on Windows). Now try it out: Edit any file
-type supported by Exuberant Ctags and within ten seconds the plug-in should
-create/update your tags file (`~/.vimtags` on UNIX, `~/_vimtags` on Windows)
-with the tags defined in the file you just edited! This means that whatever
-file you're editing in Vim (as long as its on the local file system), tags will
-always be available by the time you need them!
-
-Additionally if the file you just opened is a C, Lua, PHP, Python or Vim source
-file you should also notice that the function and type names defined in the
-file have been syntax highlighted.
-
-If the plug-in warns you that `ctags` isn't installed you can download it from
-its [homepage] [exuberant_ctags], or if you're running Debian/Ubuntu you can
-install it by executing the following shell command:
+[Vim] [vim] has long been my favorite text editor and combined with [Exuberant Ctags] [exuberant_ctags] it has the potential to provide most of what I expect from an [integrated development environment] [ide]. Exuberant Ctags is the latest incarnation of a [family of computer programs] [ctags] that scan source code files to create an index of identifiers (tags) and where they are defined. Vim uses this index (a so-called tags file) to enable you to jump to the definition of any identifier using the [Control-\]][jump_to_tag] mapping.
- $ sudo apt-get install exuberant-ctags
+When you're familiar with integrated development environments you may recognize this feature as "Go-to definition". One advantage of the combination of Vim and Exuberant Ctags over integrated development environments is that Vim supports syntax highlighting for [over 500 file types] [vim_support] (!) and Exuberant Ctags can generate tags for [over 40 file types] [ctags_support] as well...
+
+There's just one problem: You have to manually keep your tags files up-to-date and this turns out to be a royal pain in the ass! So I set out to write a Vim plug-in that would do this boring work for me. When I finished the plug-in's basic functionality (one automatic command and a call to [system()][system] later) I became interested in dynamic syntax highlighting, so I added that as well to see if it would work -- surprisingly well I'm happy to report!
+
+## Install & usage
+
+Unzip the most recent [ZIP archive] [latest_zip] file inside your Vim profile directory (usually this is `~/.vim` on UNIX and `%USERPROFILE%\vimfiles` on Windows), restart Vim and execute the command `:helptags ~/.vim/doc` (use `:helptags ~\vimfiles\doc` instead on Windows). Now try it out: Edit any file type supported by Exuberant Ctags and within ten seconds the plug-in should create/update your tags file (`~/.vimtags` on UNIX, `~/_vimtags` on Windows) with the tags defined in the file you just edited! This means that whatever file you're editing in Vim (as long as it's on the local file system), tags will always be available by the time you need them!
-## Configuration
+Additionally if the file you just opened is a C, C++, Objective-C, Java, Lua, Python, PHP or Vim source file you should also notice that the function and type names defined in the file have been syntax highlighted.
-The plug-in is intended to work without configuration but can be customized by
-changing the following options:
+The `easytags.vim` plug-in is intended to work automatically once it's installed, but if you want to change how it works there are several options you can change and commands you can execute from your own mappings and/or automatic commands. These are all documented below.
+
+Note that if the plug-in warns you `ctags` isn't installed you'll have to download it from its [homepage] [exuberant_ctags], or if you're running Debian/Ubuntu you can install it by executing the following shell command:
+
+ $ sudo apt-get install exuberant-ctags
### The `g:easytags_cmd` option
-The plug-in will try to determine the location where Exuberant Ctags is
-installed on its own but this might not always work because any given
-executable named `ctags` in your `$PATH` might not in fact be Exuberant Ctags
-but some older, more primitive `ctags` implementation which doesn't support the
-same command-line options and thus breaks the `easytags.vim` plug-in. If this
-is the case you can set the global variable `g:easytags_cmd` to the location
-where you've installed Exuberant Ctags, e.g.:
+The plug-in will try to determine the location where Exuberant Ctags is installed on its own but this might not always work because any given executable named `ctags` in your `$PATH` might not in fact be Exuberant Ctags but some older, more primitive `ctags` implementation which doesn't support the same command-line options and thus breaks the `easytags.vim` plug-in. If this is the case you can set the global variable `g:easytags_cmd` to the location where you've installed Exuberant Ctags, e.g.:
:let g:easytags_cmd = '/usr/local/bin/ctags'
### The `g:easytags_file` option
-As mentioned above the plug-in will store your tags in `~/.vimtags` on UNIX and
-`~/_vimtags` on Windows. To change the location of this file, set the global
-variable `g:easytags_file`, e.g.:
+As mentioned above the plug-in will store your tags in `~/.vimtags` on UNIX and `~/_vimtags` on Windows. To change the location of this file, set the global variable `g:easytags_file`, e.g.:
:let g:easytags_file = '~/.vim/tags'
-A leading `~` in the `g:easytags_file` variable is expanded to your current home
-directory (`$HOME` on UNIX, `%USERPROFILE%` on Windows).
+A leading `~` in the `g:easytags_file` variable is expanded to your current home directory (`$HOME` on UNIX, `%USERPROFILE%` on Windows).
### The `g:easytags_always_enabled` option
-By default the plug-in automatically generates and highlights tags when you
-stop typing for a few seconds (this works using the [CursorHold][cursorhold]
-automatic command). This means that when you edit a file, the dynamic
-highlighting won't appear until you pause for a moment. If you don't like this
-you can configure the plug-in to always enable dynamic highlighting:
+By default the plug-in automatically generates and highlights tags when you stop typing for a few seconds (this works using the [CursorHold][cursorhold] automatic command). This means that when you edit a file, the dynamic highlighting won't appear until you pause for a moment. If you don't like this you can configure the plug-in to always enable dynamic highlighting:
:let g:easytags_always_enabled = 1
-Be warned that after setting this option you'll probably notice why it's
-disabled by default: Every time you edit a file in Vim, the plug-in will first
-run Exuberant Ctags and then highlight the tags, and this slows Vim down quite
-a lot. I have some ideas on how to improve this latency by running Exuberant
-Ctags in the background (see my [shell.vim][shell] plug-in) so stay tuned!
+Be warned that after setting this option you'll probably notice why it's disabled by default: Every time you edit a file in Vim, the plug-in will first run Exuberant Ctags and then highlight the tags, and this slows Vim down quite a lot. I have some ideas on how to improve this latency by running Exuberant Ctags in the background (see my [shell.vim][shell] plug-in) so stay tuned!
-Note: If you change this option it won't apply until you restart Vim, so you'll
-have to set this option in your [vimrc script][vimrc].
+Note: If you change this option it won't apply until you restart Vim, so you'll have to set this option in your [vimrc script][vimrc].
### The `g:easytags_on_cursorhold` option
-As I explained above the plug-in by default doesn't update or highlight your
-tags until you stop typing for a moment. The plug-in tries hard to do the least
-amount of work possible in this break but it might still interrupt your
-workflow. If it does you can disable the periodic update:
+As I explained above the plug-in by default doesn't update or highlight your tags until you stop typing for a moment. The plug-in tries hard to do the least amount of work possible in this break but it might still interrupt your workflow. If it does you can disable the periodic update:
:let g:easytags_on_cursorhold = 0
-Note: Like the `g:easytags_always_enabled` option, if you change this option it
-won't apply until you restart Vim, so you'll have to set this option in
-your [vimrc script][vimrc].
+Note: Like the `g:easytags_always_enabled` option, if you change this option it won't apply until you restart Vim, so you'll have to set this option in your [vimrc script][vimrc].
### The `g:easytags_resolve_links` option
-UNIX has [symbolic links] [symlinks] and [hard links] [hardlinks], both of
-which conflict with the concept of having one unique location for every
-identifier. With regards to hard links there's not much anyone can do, but
-because I use symbolic links quite a lot I've added this option. It's disabled
-by default since it has a small performance impact and might not do what
-unknowing users expect it to: When you enable this option the plug-in will
-resolve symbolic links in pathnames, which means your tags file will only
-contain entries with [canonical pathnames] [canon]. To enable this option
-(which I strongly suggest doing when you run UNIX and use symbolic links)
-execute the following Vim command:
+UNIX has [symbolic links] [symlinks] and [hard links] [hardlinks], both of which conflict with the concept of having one unique location for every identifier. With regards to hard links there's not much anyone can do, but because I use symbolic links quite a lot I've added this option. It's disabled by default since it has a small performance impact and might not do what unknowing users expect it to: When you enable this option the plug-in will resolve symbolic links in pathnames, which means your tags file will only contain entries with [canonical pathnames] [canon]. To enable this option (which I strongly suggest doing when you run UNIX and use symbolic links) execute the following Vim command:
:let g:easytags_resolve_links = 1
+### The `:UpdateTags` command
+
+This command executes [Exuberant Ctags] [exuberant_ctags] from inside Vim to update the global tags file defined by `g:easytags_file`. When no arguments are given the tags for the current file are updated, otherwise the arguments are passed on to `ctags`. For example when you execute the Vim command `:UpdateTags -R ~/.vim` (or `:UpdateTags -R ~\vimfiles` on Windows) the plug-in will execute `ctags -R ~/.vim` for you (with some additional arguments).
+
+When you execute this command like `:UpdateTags!` (including the bang!) then all tags whose files are missing will be filtered from the global tags file.
+
+Note that this command will be executed automatically every once in a while, assuming you haven't changed `g:easytags_on_cursorhold`.
+
+### The `:HighlightTags` command
+
+When you execute this command while editing one of the supported file types (see above) the relevant tags in the current file are highlighted. The tags to highlight are gathered from all tags files known to Vim (through the ['tags' option](http://vimdoc.sourceforge.net/htmldoc/options.html#'tags')).
+
+Note that this command will be executed automatically every once in a while, assuming you haven't changed `g:easytags_on_cursorhold`.
+
## Troubleshooting
### The plug-in complains that Exuberant Ctags isn't installed
-After a Mac OS X user found out the hard way that the `ctags` executable isn't
-always Exuberant Ctags and we spend a few hours debugging the problem I added
-proper version detection: The plug-in executes `ctags --version` when Vim is
-started to verify that Exuberant Ctags 5.5 or newer is installed. If it isn't
-Vim will show the following message on startup:
+After a Mac OS X user found out the hard way that the `ctags` executable isn't always Exuberant Ctags and we spend a few hours debugging the problem I added proper version detection: The plug-in executes `ctags --version` when Vim is started to verify that Exuberant Ctags 5.5 or newer is installed. If it isn't Vim will show the following message on startup:
easytags.vim: Plug-in not loaded because Exuberant Ctags isn't installed!
Please download & install Exuberant Ctags from http://ctags.sf.net
@@ -136,65 +84,37 @@ If the installed Exuberant Ctags version is too old the plug-in will complain:
easytags.vim: Plug-in not loaded because Exuberant Ctags 5.5
or newer is required while you have version %s installed!
-If you have the right version of Exuberant Ctags installed but the plug-in
-still complains, try executing the following command from inside Vim:
+If you have the right version of Exuberant Ctags installed but the plug-in still complains, try executing the following command from inside Vim:
:!which ctags
-If this doesn't print the location where you installed Exuberant Ctags it means
-your system already had a `ctags` executable but it isn't compatible with
-Exuberant Ctags 5.5 and you'll need to set the `g:easytags_cmd` option (see
-above) so the plug-in knows which `ctags` to run.
+If this doesn't print the location where you installed Exuberant Ctags it means your system already had a `ctags` executable but it isn't compatible with Exuberant Ctags 5.5 and you'll need to set the `g:easytags_cmd` option (see above) so the plug-in knows which `ctags` to run.
### Vim locks up while the plug-in is running
-Once or twice now in several years I've experienced Exuberant Ctags getting
-into an infinite loop when given garbage input. In my case this happened by
-accident a few days ago :-|. Because my plug-in executes `ctags` in the
-foreground this will block Vim indefinitely! If this happens you might be
-able to kill `ctags` by pressing [Control-C][control_c] but if that doesn't
-work you can also kill it without stopping Vim using a task manager or the
-`pkill` command:
+Once or twice now in several years I've experienced Exuberant Ctags getting into an infinite loop when given garbage input. In my case this happened by accident a few days ago :-|. Because my plug-in executes `ctags` in the foreground this will block Vim indefinitely! If this happens you might be able to kill `ctags` by pressing [Control-C][control_c] but if that doesn't work you can also kill it without stopping Vim using a task manager or the `pkill` command (available on most UNIX systems):
$ pkill -KILL ctags
-If Vim seems very slow and you suspect this plug-in might be the one to blame,
-increase Vim's verbosity level:
+If Vim seems very slow and you suspect this plug-in might be the one to blame, increase Vim's verbosity level:
:set vbs=1
-Every time the plug-in executes it will time how long the execution takes and
-add the results to Vim's message history, which you can view by executing the
-[:messages][messages] command.
+Every time the plug-in executes it will time how long the execution takes and add the results to Vim's message history, which you can view by executing the [:messages][messages] command.
### Failed to highlight tags because pattern is too big!
-If the `easytags.vim` plug-in fails to highlight your tags and the error
-message mentions that the pattern is too big, your tags file has grown too
-large for Vim to be able to highlight all tagged identifiers! I've had this
-happen to me with 50 KB patterns because I added most of the headers in
-`/usr/include/` to my tags file. Internally Vim raises the error [E339: Pattern
-too long] [E339] and unfortunately the only way to avoid this problem once it
-occurs is to reduce the number of tagged identifiers...
+If the `easytags.vim` plug-in fails to highlight your tags and the error message mentions that the pattern is too big, your tags file has grown too large for Vim to be able to highlight all tagged identifiers! I've had this happen to me with 50 KB patterns because I added most of the headers in `/usr/include/` to my tags file. Internally Vim raises the error [E339: Pattern too long] [E339] and unfortunately the only way to avoid this problem once it occurs is to reduce the number of tagged identifiers...
-In my case the solution was to move most of the tags from `/usr/include/` over
-to project specific tags files which are automatically loaded by Vim when I
-edit files in different projects because I've set the ['tags' option]
-[tags_option] as follows:
+In my case the solution was to move most of the tags from `/usr/include/` over to project specific tags files which are automatically loaded by Vim when I edit files in different projects because I've set the ['tags' option] [tags_option] as follows:
:set tags=./.tags;,~/.vimtags
-Once you've executed the above command, Vim will automatically look for a file
-named `.tags` in the directory of the current file. Because of the `;` Vim also
-recurses upwards so that you can nest files arbitrarily deep under your project
-directories.
+Once you've executed the above command, Vim will automatically look for a file named `.tags` in the directory of the current file. Because of the `;` Vim also recurses upwards so that you can nest files arbitrarily deep under your project directories.
## Contact
-If you have questions, bug reports, suggestions, etc. the author can be
-contacted at <peter@peterodding.com>. The latest version is available at
-<http://peterodding.com/code/vim/easytags/> and <http://github.com/xolox/vim-easytags>.
-If you like this plug-in please vote for it on [www.vim.org] [vim_scripts_entry].
+If you have questions, bug reports, suggestions, etc. the author can be contacted at <peter@peterodding.com>. The latest version is available at <http://peterodding.com/code/vim/easytags/> and <http://github.com/xolox/vim-easytags>. If you like this plug-in please vote for it on [www.vim.org] [vim_scripts_entry].
## License
diff --git a/TODO.md b/TODO.md
index 4bc7b19..ff54689 100644
--- a/TODO.md
+++ b/TODO.md
@@ -34,10 +34,4 @@
`~/Development/Vim/vim-easytags/autoload.vim`). I was already editing
`~/.vim/autoload/easytags.vim` in another Vim buffer.
- * Convert `easytags#update_cmd()` to a function that can be executed on a
- list of files and will only execute Exuberant Ctags once to do so, then
- change `:UpdateTags` to accept an optional file glob, e.g. `:UpdateTags
- /usr/include/lua5.1/*.h` and call `easytags#update_cmd()` on the matching
- files.
-
vim: ai nofen
diff --git a/autoload.vim b/autoload.vim
index d4e041f..35fee2c 100644
--- a/autoload.vim
+++ b/autoload.vim
@@ -1,6 +1,6 @@
" Vim script
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: July 20, 2010
+" Last Change: August 9, 2010
" URL: http://peterodding.com/code/vim/easytags/
let s:script = expand('<sfile>:p:~')
@@ -14,17 +14,17 @@ function! easytags#autoload() " {{{2
if pathname != ''
let tags_outdated = getftime(pathname) > getftime(easytags#get_tagsfile())
if tags_outdated || !easytags#file_has_tags(pathname)
- UpdateTags
+ call easytags#update(1, 0)
endif
endif
" Apply highlighting of tags in global tags file to current buffer?
if &eventignore !~? '\<syntax\>'
if !exists('b:easytags_last_highlighted')
- HighlightTags
+ call easytags#highlight()
else
for tagfile in tagfiles()
if getftime(tagfile) > b:easytags_last_highlighted
- HighlightTags
+ call easytags#highlight()
break
endif
endfor
@@ -36,68 +36,143 @@ function! easytags#autoload() " {{{2
endtry
endfunction
-function! easytags#update_cmd(filter_invalid_tags) " {{{2
+function! easytags#update(silent, filter_tags, ...) " {{{2
try
- let filename = s:resolve(expand('%:p'))
- let ft_supported = index(easytags#supported_filetypes(), &ft) >= 0
- let ft_ignored = g:easytags_ignored_filetypes != '' && &ft =~ g:easytags_ignored_filetypes
- let update_tags = (filename != '') && ft_supported && !ft_ignored
- if update_tags || a:filter_invalid_tags
- let start = xolox#timer#start()
- let tagsfile = easytags#get_tagsfile()
- let command = [g:easytags_cmd, '-f', shellescape(tagsfile), '--fields=+l']
- if !filereadable(tagsfile)
- call add(command, '--sort=' . (&ic ? 'foldcase' : 'yes'))
- else
- call add(command, '-a')
- let filter_file_tags = update_tags && easytags#file_has_tags(filename)
- if a:filter_invalid_tags || filter_file_tags
- let [header, entries] = easytags#read_tagsfile(tagsfile)
- let num_entries = len(entries)
- call s:set_tagged_files(entries)
- let filters = []
- if filter_file_tags
- let filename_pattern = '\t' . xolox#escape#pattern(filename) . '\t'
- call add(filters, 'v:val !~ filename_pattern')
- endif
- if a:filter_invalid_tags
- call add(filters, 'filereadable(get(split(v:val, "\t"), 1))')
- endif
- call filter(entries, join(filters, ' && '))
- if len(entries) != num_entries
- if !easytags#write_tagsfile(tagsfile, header, entries)
- let msg = "Failed to write filtered tags file %s!"
- throw printf(msg, fnamemodify(tagsfile, ':~'))
- endif
- endif
- endif
- endif
- if update_tags
- call add(command, '--language-force=' . easytags#to_ctags_ft(&ft))
- call add(command, shellescape(filename))
- let listing = system(join(command))
- if v:shell_error
- let msg = "Failed to update tags file %s: %s!"
- throw printf(msg, fnamemodify(tagsfile, ':~'), strtrans(listing))
- endif
- call easytags#add_tagged_file(filename)
+ let starttime = xolox#timer#start()
+ let cfile = s:check_cfile(a:silent, a:filter_tags, a:0 > 0)
+ let tagsfile = easytags#get_tagsfile()
+ let firstrun = !filereadable(tagsfile)
+ let cmdline = s:prep_cmdline(cfile, tagsfile, firstrun, a:000)
+ let output = s:run_ctags(starttime, cfile, tagsfile, firstrun, cmdline)
+ if !firstrun
+ let num_filtered = s:filter_merge_tags(a:filter_tags, tagsfile, output)
+ if cfile != ''
let msg = "%s: Updated tags for %s in %s."
- call xolox#timer#stop(msg, s:script, expand('%:p:~'), start)
+ call xolox#timer#stop(msg, s:script, expand('%:p:~'), starttime)
+ elseif a:0 > 0
+ let msg = "%s: Updated tags in %s."
+ call xolox#timer#stop(msg, s:script, starttime)
else
- let msg = "%s: Filtered invalid tags in %s."
- call xolox#timer#stop(msg, s:script, start)
+ let msg = "%s: Filtered %i invalid tags in %s."
+ call xolox#timer#stop(msg, s:script, num_filtered, starttime)
endif
- return 1
endif
- return 0
+ return 1
catch
call xolox#warning("%s: %s (at %s)", s:script, v:exception, v:throwpoint)
endtry
endfunction
-function! easytags#highlight_cmd() " {{{2
+function! s:check_cfile(silent, filter_tags, have_args) " {{{3
+ if a:have_args
+ return ''
+ endif
+ let cfile = s:resolve(expand('%:p'))
+ let silent = a:silent || a:filter_tags
+ if cfile == '' || !filereadable(cfile)
+ if silent | return '' | endif
+ throw "You'll need to save your file before using :UpdateTags!"
+ elseif g:easytags_ignored_filetypes != '' && &ft =~ g:easytags_ignored_filetypes
+ if silent | return '' | endif
+ throw "The " . string(&ft) . " file type is explicitly ignored."
+ elseif index(easytags#supported_filetypes(), &ft) == -1
+ if silent | return '' | endif
+ throw "Exuberant Ctags doesn't support the " . string(&ft) . " file type!"
+ endif
+ return cfile
+endfunction
+
+function! s:prep_cmdline(cfile, tagsfile, firstrun, arguments) " {{{3
+ let cmdline = [g:easytags_cmd, '--fields=+l']
+ if a:firstrun
+ call add(cmdline, shellescape('-f' . a:tagsfile))
+ call add(cmdline, '--sort=' . (&ic ? 'foldcase' : 'yes'))
+ else
+ call add(cmdline, '--sort=no')
+ call add(cmdline, '-f-')
+ endif
+ if a:cfile != ''
+ let filetype = easytags#to_ctags_ft(&filetype)
+ call add(cmdline, shellescape('--language-force=' . filetype))
+ call add(cmdline, shellescape(a:cfile))
+ else
+ for fname in a:arguments
+ let matches = split(expand(fname), "\n")
+ call extend(cmdline, map(matches, 'shellescape(v:val)'))
+ endfor
+ endif
+ " No need to run Exuberant Ctags without any filename arguments!
+ return len(cmdline) > 4 ? join(cmdline) : ''
+endfunction
+
+function! s:run_ctags(starttime, cfile, tagsfile, firstrun, cmdline) " {{{3
+ let output = ''
+ if a:cmdline != ''
+ let output = system(a:cmdline)
+ if v:shell_error
+ let msg = "Failed to update tags file %s: %s!"
+ throw printf(msg, fnamemodify(a:tagsfile, ':~'), strtrans(output))
+ elseif a:firstrun
+ if a:cfile != ''
+ call easytags#add_tagged_file(a:cfile)
+ call xolox#timer#stop("%s: Created tags for %s in %s.", s:script, expand('%:p:~'), a:starttime)
+ else
+ call xolox#timer#stop("%s: Created tags in %s.", s:script, a:starttime)
+ endif
+ endif
+ endif
+ return output
+endfunction
+
+function! s:filter_merge_tags(filter_tags, tagsfile, output) " {{{3
+ let [headers, entries] = easytags#read_tagsfile(a:tagsfile)
+ call s:set_tagged_files(entries)
+ let filters = []
+ let s:cached_filenames = {}
+ let new_entries = split(a:output, "\n")
+ let tagged_files = s:find_tagged_files(new_entries)
+ if !empty(tagged_files)
+ call add(filters, '!has_key(tagged_files, s:canonicalize(get(v:val, 1)))')
+ endif
+ if a:filter_tags
+ call add(filters, 'filereadable(get(v:val, 1))')
+ endif
+ let num_old_entries = len(entries)
+ if !empty(filters)
+ call filter(entries, join(filters, ' && '))
+ endif
+ unlet s:cached_filenames
+ let num_filtered = num_old_entries - len(entries)
+ call map(entries, 'join(v:val, "\t")')
+ call extend(entries, new_entries)
+ if !easytags#write_tagsfile(a:tagsfile, headers, entries)
+ let msg = "Failed to write filtered tags file %s!"
+ throw printf(msg, fnamemodify(a:tagsfile, ':~'))
+ endif
+ return num_filtered
+endfunction
+
+function! s:find_tagged_files(new_entries) " {{{3
+ let tagged_files = {}
+ for line in a:new_entries
+ " Never corrupt the tags file by merging an invalid line
+ " (probably an error message) with the existing tags!
+ if match(line, '^[^\t]\+\t[^\t]\+\t.\+$') == -1
+ throw "Exuberant Ctags returned invalid data: " . strtrans(line)
+ endif
+ let filename = matchstr(line, '^[^\t]\+\t\zs[^\t]\+')
+ if !has_key(tagged_files, filename)
+ let filename = s:canonicalize(filename)
+ let tagged_files[filename] = 1
+ call easytags#add_tagged_file(filename)
+ endif
+ endfor
+ return tagged_files
+endfunction
+
+function! easytags#highlight() " {{{2
try
- if exists('g:syntax_on') && has_key(s:tagkinds, &ft)
+ if exists('g:syntax_on') && has_key(s:tagkinds, &ft) && !exists('b:easytags_nohl')
let start = xolox#timer#start()
if !has_key(s:aliases, &ft)
let taglist = filter(taglist('.'), "get(v:val, 'language', '') ==? &ft")
@@ -133,6 +208,7 @@ function! easytags#highlight_cmd() " {{{2
endif
let msg = "%s: Highlighted tags in %s in %s."
call xolox#timer#stop(msg, s:script, bufname, start)
+ return 1
endif
catch
call xolox#warning("%s: %s (at %s)", s:script, v:exception, v:throwpoint)
@@ -158,28 +234,48 @@ function! easytags#supported_filetypes() " {{{2
endfunction
function! easytags#read_tagsfile(tagsfile) " {{{2
- let lines = readfile(a:tagsfile)
- let header = []
- while lines != [] && lines[0] =~# '^!_TAG_'
- call insert(header, remove(lines, 0))
- endwhile
- while lines != [] && lines[-1] == ''
- call remove(lines, -1)
- endwhile
- return [header, lines]
+ " I'm not sure whether this is by design or an implementation detail but
+ " it's possible for the "!_TAG_FILE_SORTED" header to appear after one or
+ " more tags and Vim will apparently still use the header! For this reason
+ " the easytags#write_tagsfile() function should also recognize it, otherwise
+ " Vim might complain with "E432: Tags file not sorted".
+ let headers = []
+ let entries = []
+ let pattern = '^\([^\t]\+\)\t\([^\t]\+\)\t\(.\+\)$'
+ for line in readfile(a:tagsfile)
+ if line =~# '^!_TAG_'
+ call add(headers, line)
+ else
+ call add(entries, matchlist(line, pattern)[1:3])
+ endif
+ endfor
+ return [headers, entries]
endfunction
-function! easytags#write_tagsfile(tagsfile, header, entries) " {{{2
+function! easytags#write_tagsfile(tagsfile, headers, entries) " {{{2
+ " This function always sorts the tags file but understands "foldcase".
+ let sort_order = 1
+ for line in a:headers
+ if match(line, '^!_TAG_FILE_SORTED\t2') == 0
+ let sort_order = 2
+ endif
+ endfor
+ if sort_order == 1
+ call sort(a:entries)
+ else
+ call sort(a:entries, 1)
+ endif
let lines = []
if has('win32') || has('win64')
- for line in a:header
+ " Exuberant Ctags on Windows requires \r\n but Vim's writefile() doesn't add them!
+ for line in a:headers
call add(lines, line . "\r")
endfor
- for entry in a:entries
- call add(lines, entry . "\r")
+ for line in a:entries
+ call add(lines, line . "\r")
endfor
else
- call extend(lines, a:header)
+ call extend(lines, a:headers)
call extend(lines, a:entries)
endif
return writefile(lines, a:tagsfile) == 0
@@ -269,11 +365,21 @@ function! s:resolve(filename) " {{{2
endif
endfunction
+function! s:canonicalize(filename) " {{{2
+ if has_key(s:cached_filenames, a:filename)
+ return s:cached_filenames[a:filename]
+ endif
+ let canonical = s:resolve(fnamemodify(a:filename, ':p'))
+ let s:cached_filenames[a:filename] = canonical
+ return canonical
+ endif
+endfunction
+
function! s:cache_tagged_files() " {{{2
if !exists('s:tagged_files')
let tagsfile = easytags#get_tagsfile()
try
- let [header, entries] = easytags#read_tagsfile(tagsfile)
+ let [headers, entries] = easytags#read_tagsfile(tagsfile)
call s:set_tagged_files(entries)
catch /\<E484\>/
" Ignore missing tags file.
@@ -287,10 +393,9 @@ function! s:set_tagged_files(entries) " {{{2
" automatically used :-)
let s:tagged_files = {}
for entry in a:entries
- let filename = matchstr(entry, '^[^\t]\+\t\zs[^\t]\+')
+ let filename = get(entry, 1, '')
if filename != ''
- let filename = s:resolve(filename)
- let s:tagged_files[filename] = 1
+ let s:tagged_files[s:resolve(filename)] = 1
endif
endfor
endfunction
diff --git a/easytags.vim b/easytags.vim
index e692532..22845fb 100644
--- a/easytags.vim
+++ b/easytags.vim
@@ -1,10 +1,10 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: July 20, 2010
+" Last Change: August 9, 2010
" URL: http://peterodding.com/code/vim/easytags/
" Requires: Exuberant Ctags (http://ctags.sf.net)
" License: MIT
-" Version: 1.9.7
+" Version: 2.0
" Support for automatic update using the GLVS plug-in.
" GetLatestVimScripts: 3114 1 :AutoInstall: easytags.zip
@@ -131,8 +131,8 @@ call s:RegisterTagsFile()
" The :UpdateTags and :HighlightTags commands. {{{1
-command! -bar -bang UpdateTags call easytags#update_cmd(<q-bang> == '!')
-command! -bar HighlightTags call easytags#highlight_cmd()
+command! -bar -bang -nargs=* -complete=file UpdateTags call easytags#update(0, <q-bang> == '!', <f-args>)
+command! -bar HighlightTags call easytags#highlight()
" Automatic commands. {{{1