Skip to content

Commit

Permalink
Provide alternative method of configuration when sourcing .vimrc when…
Browse files Browse the repository at this point in the history
… dictwatcheradd feature is not available (#181, #151)
  • Loading branch information
SidOfc committed Feb 24, 2023
1 parent 6d7208e commit fe78624
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 95 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,14 @@ To overwrite a setting, write it as seen in `:h mkdx-settings` in your _.vimrc_:
let g:mkdx#settings = { 'enter': { 'enable': 0 } }
```

**NOTE:** The method used above somewhat relies on `exists('*dictwatcheradd')` which is Neovim only.
If you intend to `:so %` your `.vimrc` file, please use the following instead:

```viml
" :h mkdx-settings
call mkdx#configure({ 'enter': { 'enable': 0 } })
```

The above will disable the [`g:mkdx#settings.enter.enable`](#gmkdxsettingsenterenable) setting.
For backwards compatibility, `g:mkdx#` variables are merged into the defaults.
This happens before any `g:mkdx#settings` hash defined in _.vimrc_ is merged with the defaults.
Expand Down
31 changes: 31 additions & 0 deletions autoload/mkdx.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ else
endfunc
endif

let g:mkdx#defaults = {
\ 'image_extension_pattern': 'a\?png\|jpe\?g\|gif',
\ 'insert_indent_mappings': 0,
\ 'restore_visual': 1,
\ 'gf_on_steroids': 0,
\ 'enter': { 'enable': 1, 'shift': 0, 'malformed': 1, 'o': 1, 'shifto': 1, 'increment': 1, 'close_pum': 0 },
\ 'tab': { 'enable': 1 },
\ 'map': { 'prefix': '<leader>', 'enable': 1 },
\ 'tokens': { 'enter': ['-', '*', '>'], 'bold': '**', 'italic': '*',
\ 'list': '-', 'fence': '', 'header': '#', 'strike': '' },
\ 'checkbox': { 'toggles': [' ', '-', 'x'], 'update_tree': 2, 'initial_state': ' ', 'match_attrs': { 'mkdxCheckboxEmpty': '', 'mkdxCheckboxPending': '', 'mkdxCheckboxComplete': '' } },
\ 'toc': { 'text': 'TOC', 'list_token': '-', 'position': 0, 'update_on_write': 0,
\ 'details': { 'enable': 0, 'summary': 'Click to expand {{toc.text}}',
\ 'nesting_level': -1, 'child_count': 5, 'child_summary': 'show {{count}} items' } },
\ 'table': { 'divider': '|', 'header_divider': '-',
\ 'align': { 'left': [], 'center': [], 'right': [],
\ 'default': 'center' } },
\ 'links': { 'external': { 'enable': 0, 'timeout': 3, 'host': '', 'relative': 1,
\ 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/9001.0.0000.000 vim-mkdx/1.10.0' },
\ 'fragment': { 'jumplist': 1, 'complete': 1, 'pumheight': 15, 'completeopt': 'noinsert,menuone' },
\ 'conceal': 1 },
\ 'highlight': { 'enable': 0, 'frontmatter': { 'yaml': 1, 'toml': 0, 'json': 0 } },
\ 'auto_update': { 'enable': 1 },
\ 'fold': { 'enable': 0, 'components': ['toc', 'fence'] }
\ }


let s:_is_nvim = has('nvim')
let s:_has_curl = executable('curl')
let s:_can_async = s:_is_nvim || has('job')
Expand Down Expand Up @@ -1301,6 +1328,10 @@ fun! mkdx#guard_settings()
endif
endfun

function! mkdx#configure(...)
let g:mkdx#settings = mkdx#MergeSettings(g:mkdx#defaults, get(a:000, 0, {}))
endfunction

fun! mkdx#MergeSettings(...)
let a = get(a:000, 0, {})
let b = get(a:000, 1, {})
Expand Down
88 changes: 44 additions & 44 deletions doc/mkdx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -380,50 +380,50 @@ value defined in the settings hash will end up being used, e.g. the

Below a mapping of settings to global variables:

s:defaults.insert_indent_mappings `=>`
s:defaults.gf_on_steroids `=>`
s:defaults.links.external.enable `=>`
s:defaults.links.external.host `=>`
s:defaults.links.external.timeout `=>`
s:defaults.links.external.relative `=>`
s:defaults.links.external.user_agent `=>`
s:defaults.links.fragment.jumplist `=>`
s:defaults.links.fragment.complete `=>`
s:defaults.links.conceal `=>`
s:defaults.image_extension_pattern `=>` g:mkdx#link_as_img_pat
s:defaults.restore_visual `=>` g:mkdx#restore_visual
s:defaults.map.prefix `=>` g:mkdx#map_prefix
s:defaults.map.enable `=>` g:mkdx#map_keys
s:defaults.checkbox.toggles `=>` g:mkdx#checkbox_toggles
s:defaults.checkbox.update_tree `=>` g:mkdx#checklist_update_tree
s:defaults.checkbox.initial_state `=>` g:mkdx#checkbox_initial_state
s:defaults.table.header_divider `=>` g:mkdx#table_header_divider
s:defaults.table.divider `=>` g:mkdx#table_divider
s:defaults.tokens.header `=>` g:mkdx#header_style
s:defaults.tokens.enter `=>` g:mkdx#list_tokens
s:defaults.tokens.fence `=>` g:mkdx#fence_style
s:defaults.tokens.bold `=>` g:mkdx#bold_token
s:defaults.tokens.strike `=>`
s:defaults.tokens.italic `=>` g:mkdx#italic_token
s:defaults.tokens.list `=>` g:mkdx#list_token
s:defaults.enter.enable `=>` g:mkdx#enhance_enter
s:defaults.enter.malformed `=>` g:mkdx#handle_malformed_indent
s:defaults.enter.o `=>`
s:defaults.enter.shifto `=>`
s:defaults.enter.shift `=>`
s:defaults.toc.list_token `=>` g:mkdx#toc_list_token
s:defaults.toc.text `=>` g:mkdx#toc_text
s:defaults.toc.position `=>`
s:defaults.toc.update_on_write `=>`
s:defaults.toc.details.enable `=>`
s:defaults.toc.details.summary `=>`
s:defaults.toc.details.nesting_level `=>`
s:defaults.toc.details.child_count `=>`
s:defaults.toc.details.child_summary `=>`
s:defaults.highlight.enable `=>`
s:defaults.highlight.frontmatter.yaml `=>`
s:defaults.highlight.frontmatter.toml `=>`
s:defaults.highlight.frontmatter.json `=>`
g:mkdx#defaults.insert_indent_mappings `=>`
g:mkdx#defaults.gf_on_steroids `=>`
g:mkdx#defaults.links.external.enable `=>`
g:mkdx#defaults.links.external.host `=>`
g:mkdx#defaults.links.external.timeout `=>`
g:mkdx#defaults.links.external.relative `=>`
g:mkdx#defaults.links.external.user_agent `=>`
g:mkdx#defaults.links.fragment.jumplist `=>`
g:mkdx#defaults.links.fragment.complete `=>`
g:mkdx#defaults.links.conceal `=>`
g:mkdx#defaults.image_extension_pattern `=>` g:mkdx#link_as_img_pat
g:mkdx#defaults.restore_visual `=>` g:mkdx#restore_visual
g:mkdx#defaults.map.prefix `=>` g:mkdx#map_prefix
g:mkdx#defaults.map.enable `=>` g:mkdx#map_keys
g:mkdx#defaults.checkbox.toggles `=>` g:mkdx#checkbox_toggles
g:mkdx#defaults.checkbox.update_tree `=>` g:mkdx#checklist_update_tree
g:mkdx#defaults.checkbox.initial_state `=>` g:mkdx#checkbox_initial_state
g:mkdx#defaults.table.header_divider `=>` g:mkdx#table_header_divider
g:mkdx#defaults.table.divider `=>` g:mkdx#table_divider
g:mkdx#defaults.tokens.header `=>` g:mkdx#header_style
g:mkdx#defaults.tokens.enter `=>` g:mkdx#list_tokens
g:mkdx#defaults.tokens.fence `=>` g:mkdx#fence_style
g:mkdx#defaults.tokens.bold `=>` g:mkdx#bold_token
g:mkdx#defaults.tokens.strike `=>`
g:mkdx#defaults.tokens.italic `=>` g:mkdx#italic_token
g:mkdx#defaults.tokens.list `=>` g:mkdx#list_token
g:mkdx#defaults.enter.enable `=>` g:mkdx#enhance_enter
g:mkdx#defaults.enter.malformed `=>` g:mkdx#handle_malformed_indent
g:mkdx#defaults.enter.o `=>`
g:mkdx#defaults.enter.shifto `=>`
g:mkdx#defaults.enter.shift `=>`
g:mkdx#defaults.toc.list_token `=>` g:mkdx#toc_list_token
g:mkdx#defaults.toc.text `=>` g:mkdx#toc_text
g:mkdx#defaults.toc.position `=>`
g:mkdx#defaults.toc.update_on_write `=>`
g:mkdx#defaults.toc.details.enable `=>`
g:mkdx#defaults.toc.details.summary `=>`
g:mkdx#defaults.toc.details.nesting_level `=>`
g:mkdx#defaults.toc.details.child_count `=>`
g:mkdx#defaults.toc.details.child_summary `=>`
g:mkdx#defaults.highlight.enable `=>`
g:mkdx#defaults.highlight.frontmatter.yaml `=>`
g:mkdx#defaults.highlight.frontmatter.toml `=>`
g:mkdx#defaults.highlight.frontmatter.json `=>`

==============================================================================
`g:mkdx#settings.insert_indent_mappings = 0` *mkdx-setting-insert-indent-mappings*
Expand Down
72 changes: 21 additions & 51 deletions plugin/mkdx.vim
Original file line number Diff line number Diff line change
@@ -1,55 +1,25 @@
let s:defaults = {
\ 'image_extension_pattern': 'a\?png\|jpe\?g\|gif',
\ 'insert_indent_mappings': 0,
\ 'restore_visual': 1,
\ 'gf_on_steroids': 0,
\ 'enter': { 'enable': 1, 'shift': 0, 'malformed': 1, 'o': 1, 'shifto': 1, 'increment': 1, 'close_pum': 0 },
\ 'tab': { 'enable': 1 },
\ 'map': { 'prefix': '<leader>', 'enable': 1 },
\ 'tokens': { 'enter': ['-', '*', '>'], 'bold': '**', 'italic': '*',
\ 'list': '-', 'fence': '', 'header': '#', 'strike': '' },
\ 'checkbox': { 'toggles': [' ', '-', 'x'], 'update_tree': 2, 'initial_state': ' ', 'match_attrs': { 'mkdxCheckboxEmpty': '', 'mkdxCheckboxPending': '', 'mkdxCheckboxComplete': '' } },
\ 'toc': { 'text': 'TOC', 'list_token': '-', 'position': 0, 'update_on_write': 0,
\ 'details': { 'enable': 0, 'summary': 'Click to expand {{toc.text}}',
\ 'nesting_level': -1, 'child_count': 5, 'child_summary': 'show {{count}} items' } },
\ 'table': { 'divider': '|', 'header_divider': '-',
\ 'align': { 'left': [], 'center': [], 'right': [],
\ 'default': 'center' } },
\ 'links': { 'external': { 'enable': 0, 'timeout': 3, 'host': '', 'relative': 1,
\ 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/9001.0.0000.000 vim-mkdx/1.10.0' },
\ 'fragment': { 'jumplist': 1, 'complete': 1, 'pumheight': 15, 'completeopt': 'noinsert,menuone' },
\ 'conceal': 1 },
\ 'highlight': { 'enable': 0, 'frontmatter': { 'yaml': 1, 'toml': 0, 'json': 0 } },
\ 'auto_update': { 'enable': 1 },
\ 'fold': { 'enable': 0, 'components': ['toc', 'fence'] }
\ }
if exists('g:mkdx#map_prefix') | let g:mkdx#defaults.map.prefix = g:mkdx#map_prefix | endif
if exists('g:mkdx#map_keys') | let g:mkdx#defaults.map.enable = g:mkdx#map_keys | endif
if exists('g:mkdx#checkbox_toggles') | let g:mkdx#defaults.checkbox.toggles = g:mkdx#checkbox_toggles | endif
if exists('g:mkdx#checklist_update_tree') | let g:mkdx#defaults.checkbox.update_tree = g:mkdx#checklist_update_tree | endif
if exists('g:mkdx#restore_visual') | let g:mkdx#defaults.restore_visual = g:mkdx#restore_visual | endif
if exists('g:mkdx#header_style') | let g:mkdx#defaults.tokens.header = g:mkdx#header_style | endif
if exists('g:mkdx#table_header_divider') | let g:mkdx#defaults.table.header_divider = g:mkdx#table_header_divider | endif
if exists('g:mkdx#table_divider') | let g:mkdx#defaults.table.divider = g:mkdx#table_divider | endif
if exists('g:mkdx#enhance_enter') | let g:mkdx#defaults.enter.enable = g:mkdx#enhance_enter | endif
if exists('g:mkdx#list_tokens') | let g:mkdx#defaults.tokens.enter = g:mkdx#list_tokens | endif
if exists('g:mkdx#fence_style') | let g:mkdx#defaults.tokens.fence = g:mkdx#fence_style | endif
if exists('g:mkdx#handle_malformed_indent') | let g:mkdx#defaults.enter.malformed = g:mkdx#handle_malformed_indent | endif
if exists('g:mkdx#link_as_img_pat') | let g:mkdx#defaults.image_extension_pattern = g:mkdx#link_as_img_pat | endif
if exists('g:mkdx#bold_token') | let g:mkdx#defaults.tokens.bold = g:mkdx#bold_token | endif
if exists('g:mkdx#italic_token') | let g:mkdx#defaults.tokens.italic = g:mkdx#italic_token | endif
if exists('g:mkdx#list_token') | let g:mkdx#defaults.tokens.list = g:mkdx#list_token | endif
if exists('g:mkdx#toc_list_token') | let g:mkdx#defaults.toc.list_token = g:mkdx#toc_list_token | endif
if exists('g:mkdx#toc_text') | let g:mkdx#defaults.toc.text = g:mkdx#toc_text | endif
if exists('g:mkdx#checkbox_initial_state') | let g:mkdx#defaults.checkbox.initial_state = g:mkdx#checkbox_initial_state | endif

if exists('g:mkdx#map_prefix') | let s:defaults.map.prefix = g:mkdx#map_prefix | endif
if exists('g:mkdx#map_keys') | let s:defaults.map.enable = g:mkdx#map_keys | endif
if exists('g:mkdx#checkbox_toggles') | let s:defaults.checkbox.toggles = g:mkdx#checkbox_toggles | endif
if exists('g:mkdx#checklist_update_tree') | let s:defaults.checkbox.update_tree = g:mkdx#checklist_update_tree | endif
if exists('g:mkdx#restore_visual') | let s:defaults.restore_visual = g:mkdx#restore_visual | endif
if exists('g:mkdx#header_style') | let s:defaults.tokens.header = g:mkdx#header_style | endif
if exists('g:mkdx#table_header_divider') | let s:defaults.table.header_divider = g:mkdx#table_header_divider | endif
if exists('g:mkdx#table_divider') | let s:defaults.table.divider = g:mkdx#table_divider | endif
if exists('g:mkdx#enhance_enter') | let s:defaults.enter.enable = g:mkdx#enhance_enter | endif
if exists('g:mkdx#list_tokens') | let s:defaults.tokens.enter = g:mkdx#list_tokens | endif
if exists('g:mkdx#fence_style') | let s:defaults.tokens.fence = g:mkdx#fence_style | endif
if exists('g:mkdx#handle_malformed_indent') | let s:defaults.enter.malformed = g:mkdx#handle_malformed_indent | endif
if exists('g:mkdx#link_as_img_pat') | let s:defaults.image_extension_pattern = g:mkdx#link_as_img_pat | endif
if exists('g:mkdx#bold_token') | let s:defaults.tokens.bold = g:mkdx#bold_token | endif
if exists('g:mkdx#italic_token') | let s:defaults.tokens.italic = g:mkdx#italic_token | endif
if exists('g:mkdx#list_token') | let s:defaults.tokens.list = g:mkdx#list_token | endif
if exists('g:mkdx#toc_list_token') | let s:defaults.toc.list_token = g:mkdx#toc_list_token | endif
if exists('g:mkdx#toc_text') | let s:defaults.toc.text = g:mkdx#toc_text | endif
if exists('g:mkdx#checkbox_initial_state') | let s:defaults.checkbox.initial_state = g:mkdx#checkbox_initial_state | endif

if (!exists('g:mkdx#settings_initialized'))
let g:mkdx#settings = mkdx#MergeSettings(s:defaults, exists('g:mkdx#settings') ? g:mkdx#settings : {})
if (!exists('g:mkdx#settings_initialized') || !exists('*dictwatcheradd'))
let g:mkdx#settings = mkdx#MergeSettings(g:mkdx#defaults, exists('g:mkdx#settings') ? g:mkdx#settings : {})
let g:mkdx#settings_initialized = 1
call mkdx#guard_settings()
endif

function! mkdx#configure(...)
let g:mkdx#settings = mkdx#MergeSettings(s:defaults, get(a:000, 0, {}))
endfunction

0 comments on commit fe78624

Please sign in to comment.