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? > > This is what the help has to say about bufnr(): > > : bufnr({expr} [, {create}]) > : The result is the number of a buffer, as it is displayed by > : the ":ls" command. For the use of {expr}, see |bufname()| > : above. > > The help for bufname() explains: > > : If {expr} is a String, it is used as a |file-pattern| to match > : with the buffer names. This is always done like 'magic' is > : set and 'cpoptions' is empty. When there is more than one > : match an empty string is returned. > : "" or "%" can be used for the current buffer, "#" for the > : alternate buffer. > : A full match is preferred, otherwise a match at the start, end > : or middle of the buffer name is accepted. If you only want a > : full match then put "^" at the start and "$" at the end of the > : pattern. > : Listed buffers are found first. If there is a single match > : with a listed buffer, that one is returned. Next unlisted > : buffers are searched for. > > And then the help for file-pattern: > > > : *file-pattern* > : The pattern is interpreted like mostly used in file names: > : * matches any sequence of characters; Unusual: includes path > : separators > : ? matches any single character > : \? matches a '?' > : . matches a '.' > : ~ matches a '~' > : , separates patterns > : \, matches a ',' > : { } like \( \) in a |pattern| > : , inside { }: like \| in a |pattern| > : \} literal } > : \{ literal { > : \\\{n,m\} like \{n,m} in a |pattern| > : \ special meaning like in a |pattern| > : [ch] matches 'c' or 'h' > : [^ch] match any character but 'c' and 'h' > : > : Note that for all systems the '/' character is used for path separator (even > : MS-DOS and OS/2). This was done because the backslash is difficult to use > : in a pattern and to make the autocommands portable across different systems. > > 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. > > /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. -- -- 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.
