literate_markdown_ftplugin.md (2096B)
1 <!-- :Tangle(vim) ../after/ftplugin/markdown.vim --> 2 # vim-literate-markdown: ftplugin 3 This file is activated after other markdown ftplugin files. 4 The general structure of the file is: 5 6 <!-- :Tangle(vim) <^> --> 7 ```vim 8 <<load guard>> 9 10 <<commands>> 11 12 <<mappings>> 13 14 <<b:undo_ftplugin>> 15 ``` 16 17 The load guard lets the user disable the autoloaded functions by setting the variable `g:loaded_literate_markdown`. 18 If it's set, the entire file is skipped. 19 I don't want to set it here, because otherwise it will only run once, and I want it to run every time a markdown file is loaded. 20 21 <!-- :Tangle(vim) <load guard> --> 22 ```vim 23 if exists('g:loaded_literate_markdown') 24 finish 25 endif 26 ``` 27 28 ## Commands 29 The plugin provides two different commands. 30 Both are buffer-local, because they should only be enabled in markdown buffers. 31 32 One is for tangling: 33 34 <!-- :Tangle(vim) <commands> --> 35 ```vim 36 command -buffer -bar Tangle call literate_markdown#Tangle() 37 ``` 38 39 And the other to execute blocks: 40 41 <!-- :Tangle(vim) <commands>+ --> 42 ```vim 43 command -buffer -bar ExecPrevBlock call literate_markdown#ExecPreviousBlock() 44 ``` 45 46 ## Mappings 47 The ftplugin also provides two buffer-local normal-mode mappings. 48 They are only `<Plug>` mappings, so as not to force mappings on users. 49 You can map them in your own `after/ftplugin/markdown.vim` with e.g. `nmap <buffer> <leader>ct <Plug>LitMdTangle`. 50 51 <!-- :Tangle(vim) <mappings> --> 52 ```vim 53 nnoremap <buffer> <Plug>LitMdExecPrevBlock :<c-u>ExecPrevBlock<CR> 54 nnoremap <buffer> <Plug>LitMdTangle :<c-u>Tangle<CR> 55 ``` 56 57 ## Undo ftplugin 58 Finally, the `b:undo_ftplugin` variable is set to undo the changes made in this file when the filetype is changed. 59 60 First, a small trick to either overwrite or extend the `b:undo_ftplugin` variable: 61 62 <!-- :Tangle(vim) <b:undo_ftplugin> --> 63 ```vim 64 let b:undo_ftplugin = (exists('b:undo_ftplugin') ? b:undo_ftplugin.'|' : '') 65 ``` 66 67 And then the actual settings: 68 69 <!-- :Tangle(vim) <b:undo_ftplugin>+ --> 70 ```vim 71 let b:undo_ftplugin .= 'delcommand Tangle | delcommand ExecPrevBlock' 72 let b:undo_ftplugin .= '| nunmap <buffer> <Plug>LitMdExecPrevBlock' 73 ```