On Thu, Sep 1, 2016 at 4:20 PM, LCD 47 <[email protected]> wrote: > 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.
I use basic regex with bufnr(), to *avoid* bufnr() useless magic: https://github.com/justinmk/vim-dirvish/blob/master/autoload/dirvish.vim#L306-L330 It seems to me that long ago the decision should have been to only have "magic" in _commands_, not functions. Plugin authors (and vimrc authors) usually do not want user-configured behavior, it is a headache. E.g. there is zero reason that substitute() should care about any global option. For typical editing one invokes :substitute, not substitute(). This sort of inconsistency was not "inherited from Vi", since FEAT_EVAL was not in Vi. --- Justin M. Keyes -- -- 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.
