fiatjaf / neuron.vim

📝 Manage your Zettelkasten in {n}vim.
MIT License
83 stars 21 forks source link

Resolve bareword shadowing related errors for custom ID generators #26

Closed flyinggrizzly closed 4 years ago

flyinggrizzly commented 4 years ago

Before this PR, if a custom ID generator was defined in .vimrc to override the random ID generator, vim would throw errors out when starting (but otherwise worked fine).

This PR changes the name used to provide custom generators so that it does not shadow or clash with the bareword used by the plugin (g:NeuronGenerateID).

It also updates the documentation in the readme.

After this commit, to change the ID generation behavior, a g:CustomNeuronIDGenerator function must be defined, that accepts a title parameter, and returns a string.

If one is not defined, the plugin continues to return IDs generated by the existing RandomID function.


Testing

This change has been tested manually in the following cases:

For reference/further testing, the generator used to test this was:

function! g:CustomNeuronIDGenerator(title)
  if empty(a:title)
   " Copied from the default generator
    return system("od -An -N 4 -t 'x4' /dev/random")
  else
    let l:lower = tolower(a:title)

    let l:text = substitute(l:lower, '\W', ' ', 'g')

    let l:text_elements = split(l:text)

    let l:kebabed_title = join(l:text_elements, '-')

    let l:random_suffix = substitute(system("od -An -N 4 -t 'x4' /dev/random"), '\W', '', 'g')
    return join([l:kebabed_title, l:random_suffix], '--')
  endif
endfunction

Other considerations

This change has been made by converting what had been let g:NeuronGenerateID into a function, because I was running into issues where function refs were being treated as strings and Vim was throwing errors. Given that this achieves the same result, and my web searching didn't turn up an immediately identifiable fix, I went with this. Like I said before, vimscript is new to me, so I'm very open to the idea that I've reinvted a wheel somehow or something :sweat_smile:

flyinggrizzly commented 4 years ago

Thanks!

fiatjaf commented 4 years ago

Thanks!