On 1 September 2016, Nikolay Aleksandrovich Pavlov <[email protected]>
wrote:
> 2016-09-01 15:47 GMT+03:00 LCD 47 <[email protected]>:
> > What is the recommended way to get the buffer number of a file
> > starting from the filename?
> >
[...]
> > Based on the above (and taking a peek at the sources), I came up
> > with this attempt:
> >
> > function! Name2Buf(fname) abort
> > if exists('+shellslash')
> > let old_shellslash = &shellslash
> > let &shellslash = 1
> > let buf = bufnr(escape( fnamemodify(a:fname, ':p'),
> > '\*?,{}[' ))
> > let &shellslash = old_shellslash
> > else
> > let buf = bufnr(escape( fnamemodify(a:fname, ':p'),
> > '\*?,{}[' ))
> > endif
> >
> > return buf
> > endfunction
> >
> > It mostly works, until I try it on a file named a,b\{2,3\}.txt:
> >
> > :echo expand('%:p')
> > /home/lcd047/tmp/a,b\{3.4}.txt
> >
> > :Name2Buf(expand('%:p'))
> > -1
> >
> > However the naive bufnr(expand('%:p')) works, but it shouldn't,
> > because of the two commas ",":
> >
> > echomsg bufnr(expand('%:p'))
> > 1
> >
> > So, what _is_ the right way to do this?
>
> I would go with `fnameescape()`, but this does not work with your file
> name either. Additional problem is that `fnameescape()` does not work
> correctly on Windows.
fnameescape() is definitely wrong here, it escapes characters
that have nothing to do with the "file-pattern" set (f.i. '%' and
'#'). Looking at the code, the "bad" characters are a mixture of glob
characters with regexp characters. I don't think it's possible to
disarm them all with a simple escape(). I wonder why it's possible
to do all those weird globbing in bufnr() (does anybody actually use
them?), but it isn't possible to do an exact string match.
/lcd
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.