vim-pandoc-devel.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. DEVELOPING FOR VIM-PANDOC *vim-pandoc-devel*
  2. HOW TO CREATE A NEW MODULE *vim-pandoc-new-module*
  3. This is a guide to developing modules for |vim-pandoc|. It is aimed at anyone
  4. who wants to customize vim-pandoc further (or contribute some feature), and it
  5. doesn't presuppose much knowledge on the reader's part.
  6. |vim-pandoc|'s modules are viml files located in the `autoload/pandoc`
  7. directory, which are called by the pandoc filetype plugin depending on the
  8. value of |g:pandoc#modules#enabled| and |g:pandoc#modules#disabled|.
  9. *pandoc#MODULE#Init()*
  10. Every module must export a function called |pandoc#MODULE#Init()|, which
  11. should handle the module initial configuration (setting up defaults), and any
  12. functionality definition. MODULE here stands for the module filename. For
  13. example, for the module defined in `autoload/toc.vim`, this function is called
  14. `pandoc#toc#Init()`.
  15. Let's suppose we want to create a new module for fancy stuff. We should create
  16. a file called `autoload/fancy.vim`. This file should contain something like
  17. this: >
  18. function! pandoc#fancy#Init()
  19. endfunction
  20. <
  21. Now, we want it to export a command, `:PandocFancy`. This command simply calls
  22. this function (defined in the same file): >
  23. function! pandoc#fancy#Fancy()
  24. echom "this is so fancy!"
  25. endfunction
  26. <
  27. We should modify Init() so it reads: >
  28. function! pandoc#fancy#Init()
  29. command! -buffer PandocFancy call pandoc#fancy#Fancy()
  30. endfunction
  31. <
  32. Note the `command!` command passes the `-buffer` arg. This means this command
  33. will only be available in the current buffer. If a vim-pandoc module exports
  34. a command, it SHOULD be buffer-local. See |:command-buffer|
  35. This is probably not fancy enough! We want to be able to change the message
  36. `PandocFancy` outputs through a setting. We will provide a default message,
  37. and a configuration variable so we can override it.
  38. First, we define the default message modifying the Init() function: >
  39. function! pandoc#fancy#Init()
  40. if !exists("g:pandoc#fancy#message")
  41. let g:pandoc#fancy#message = "this is so fancy!"
  42. endif
  43. command! -buffer PandocFancy call pandoc#fancy#Fancy()
  44. endfunction
  45. <
  46. Note the configuration variable name follows the g:pandoc#MODULE#VARIABLE
  47. schema.
  48. We must change the Fancy() function so it picks up the configuration: >
  49. function! pandoc#fancy#Fancy()
  50. echom g:pandoc#fancy#message
  51. endfunction
  52. <
  53. Now the behavior of the Fancy function changes depending on the value of
  54. `g:pandoc#fancy#message`.
  55. We can also support buffer-local configuration variables that override the
  56. global configuration variable: >
  57. function! pandoc#fancy#Fancy()
  58. if exists("b:pandoc_fancy_message")
  59. echom b:pandoc_fancy_message
  60. else
  61. echom g:pandoc#fancy#message
  62. endif
  63. endfunction
  64. <
  65. Note the buffer-scoped variable uses underscores instead of hashes (b:
  66. variables' names can't contain them).
  67. If we wanted to add a keyboard mapping to call the :PandocFancy command more
  68. easily, we should add something like: >
  69. noremap <buffer> <LocalLeader>ff :PandocFancy<cr>
  70. <
  71. to the Init() function.
  72. Note the mapping is buffer-local.
  73. Note the mapping uses <LocalLeader>. This is recommended so the mapping
  74. doesn't interfere with others.
  75. Note If you need to change a vim setting, use |:setlocal|. Changing the global
  76. values of vim settings (or vim settings which are global only) is not
  77. recommended. If you must, save the original value and restore it after using
  78. it.
  79. vim:tw=78:ts=8:noet:ft=help:fdm=marker