Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert hooking into VimEnter #37

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@

### Plugin for vim to enable opening a file in a given line

When you open a `file:line`, for instance when coping and pasting from an error from your
compiler vim tries to open a file with a colon in its name.
When you open a `file:line`, for instance when copying and pasting the output
from a compiler, Vim tries to open a file with a colon in its name.

Examples:

vim index.html:20
vim app/models/user.rb:1337

With this little script in your plugins folder if the stuff after the colon is a number and
a file exists with the name especified before the colon vim will open this file and take you
to the line you wished in the first place.
This plugin will handle the line number (and any column numbers) after the
filename, taking you to the correct location.

This script is licensed with GPLv3 and you can contribute to it on github at
This script is licensed under GPLv3 and you can contribute to it on Github at
[github.com/bogado/file-line](https://github.com/bogado/file-line).

## Install details

If you use `Bundle`, add this line to your `.vimrc`:

Bundle 'bogado/file-line'

And launch `:BundleInstall` in vim.

Or just copy the file into your plugins path (`$HOME/.vim/plugin` under unixes).
And launch `:BundleInstall` from Vim.

Or just copy the file into your plugins path (`$HOME/.vim/plugin` under
unixes).

## Configuration

There is an option to control when the plugin should get used.

If you want it to handle only files during Vim startup (when passing the files
as arguments), you can use the following setting

let g:file_line_only_on_vimenter = 1

The default value is 0.
53 changes: 19 additions & 34 deletions plugin/file_line.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ if exists('g:loaded_file_line') || (v:version < 701)
endif
let g:loaded_file_line = 1

if !exists('g:file_line_only_on_enter')
let g:file_line_only_on_vimenter = 0
endif

" list with all possible expressions :
" matches file(10) or file(line:col)
" Accept file:line:column: or file:line:column and file:line also
Expand All @@ -14,29 +18,31 @@ function! s:reopenAndGotoLine(file_name, line_num, col_num)
return
endif

let l:bufn = bufnr("%")
" Remove the original buffer when it's no longer visible.
" This does not break `vim -[poO]`, as with `:bwipeout`.
set bufhidden=wipe

exec "keepalt edit " . fnameescape(a:file_name)
exec a:line_num
exec "normal! " . a:col_num . '|'
if foldlevel(a:line_num) > 0
exec "normal! zv"
normal! zv
endif
exec "normal! zz"

exec "bwipeout " l:bufn
exec "filetype detect"
normal! zz
endfunction

function! s:gotoline()
if g:file_line_only_on_vimenter && !has('vim_starting')
return
endif
let file = bufname("%")

" :e command calls BufRead even though the file is a new one.
" As a workaround Jonas Pfenniger<[email protected]> added an
" AutoCmd BufRead, this will test if this file actually exists before
" searching for a file and line to goto.
if (filereadable(file) || file == '')
return file
return
endif

let l:names = []
Expand All @@ -48,34 +54,13 @@ function! s:gotoline()
let line_num = l:names[2] == ''? '0' : l:names[2]
let col_num = l:names[3] == ''? '0' : l:names[3]
call s:reopenAndGotoLine(file_name, line_num, col_num)
return file_name
return
endif
endfor
endfunction

" Handle entry in the argument list.
" This is called via `:argdo` when entering Vim.
function! s:handle_arg()
let argname = expand('%')
let fname = s:gotoline()
if fname != argname
let argidx = argidx()
exec (argidx+1).'argdelete'
exec (argidx)'argadd' fname
endif
endfunction

function! s:startup()
autocmd! BufNewFile * nested call s:gotoline()
autocmd! BufRead * nested call s:gotoline()

if argc() > 0
let argidx=argidx()
argdo call s:handle_arg()
exec (argidx+1).'argument'
" Manually call Syntax autocommands, ignored by `:argdo`.
doautocmd Syntax
endif
endfunction

autocmd VimEnter * call s:startup()
augroup file_line
au!
autocmd BufNewFile * nested call s:gotoline()
autocmd BufReadPost * nested call s:gotoline()
augroup END