Sunday, May 31, 2020

Re: function definition for only one file, not for other simultaneously opened files

I believe that as Gary hinted the best bet in this case is to inspect the file extension (".md" I guess) with the help of 

if matchstr(bufname(), '\.md$')
  " do something
endif


which should return a true value (the string '.md') if the extension matches and a false value otherwise. This might make the function a little more complicated, so that you may want to load it from an (autoload) file.

I actually have a small function which takes an extension (not a regex!) with or without a leading dot and a file name and returns true of false according to whether the extension is present on the filename or not.

" e.g. let bool =                  bpj#util#ext_is('md',              bufname())                       
fun! bpj#util#ext_is (ext,         filename) abort                  
    " prepend dot to ext if none   
  let wanted = substitute(a:       ext, '\v^[^\.]+$', '.&','')      
    " get ext incl dot from fn     
   let actual = matchstr(a:         filename, '\v\.[^\.]+$')         
   " return result of comparison     return actual ==# wanted        
 endfun

You may want to make that a case insensitive comparison (use `==?` instead of `==#`). I prefer not to. I can always pass the lowercased version of the file name if I want to ignore case.

--
Better --help|less than helpless

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CADAJKhBisf6v%3D3wDRkkii_KcOTUUt7dym%3D%3DvkOKWYbNo04271A%40mail.gmail.com.

No comments:

Post a Comment