qqzsxyz / vimwiki

Automatically exported from code.google.com/p/vimwiki
0 stars 0 forks source link

Orphan wikies doesn't work #305

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Cut from Issue 300.

@tpospich8: I think I have tracked down the cause of the failure for "orphan" 
wikis.  Back in r491, you added code inside setup_filetype() to partially 
initialize the wiki state, even when idx == -1:

  "XXX when idx = -1?
  let g:vimwiki_current_idx = idx
  let b:vimwiki_idx = idx

However, upon subsequent BufWinEnter, the setup_buffer_enter() function was 
prevented from creating a temporary wiki for the orphan file because 
b:vimwiki_idx has already been defined.

  if exists("b:vimwiki_idx")
    let g:vimwiki_current_idx = b:vimwiki_idx
  else
    ... snip ...
    if idx == -1
      call add(g:vimwiki_list, {'path': path, 'ext': ext, 'temp': 1})
      let g:vimwiki_current_idx = len(g:vimwiki_list) - 1
    else
      let g:vimwiki_current_idx = idx
    endif
    let b:vimwiki_idx = g:vimwiki_current_idx
  endif

In my testing, the orphan page is partially functional (syntax highlighting 
appears to work), although the temporary orphan wiki is not created.

Now, fast forward to the present.  When I added per-buffer caching of the wiki 
state in r511, I assumed that b:vimwiki_idx would always refer to a valid wiki 
idx.  Since -1 is invalid, Vimwiki gets stuck somewhere inside 
setup_buffer_enter() > recall_wiki_state() ... or so it seems. 

I am now considering ways to solve this problem.  In one solution, I have 
copied the code (that initializes the orphan wiki) from setup_buffer_enter() 
and put it inside setup_filetype().  This seems redundant, so I am looking for 
an alternative.

I would like to understand why you added the initialization of b:vimwiki_idx 
inside setup_filetype().  Do you need to access to the per-wiki options after a 
BufNewFile or BufRead event, but before a BufWinEnter event?  

If you have a chance, please take a look and let me know.
Thanks, 
- S.

habamax, Apr 27, 2012
Stu, I have tried to open tests/wiki/index.wiki and then follow any link inside 
it with no luck. Is this because of orphan wiki is not created anymore?

Original issue reported on code.google.com by habamax on 9 May 2012 at 2:05

GoogleCodeExporter commented 8 years ago

@Maxim: Orphan wikis are working for me.  Are you using the latest version?

I have added a note ("TODO: refactor") around the copied/duplicated code in 
setup_filetype() and setup_buffer_enter().

Here's some session information with explanation for you to inspect.  Hint: it 
reads better in vimwiki!

-----------------------------

 * Setup
   * Install r6dad432a64c5
   * Open vim

 * Notice that I have only one wiki registered in `g:vimwiki_list`
   * `:echo g:vimwiki_list`

{{{ vim
  [{
    'maxhi'            : 1,
    'css_name'         : 'style.css',
    'auto_export'      : 0,
    'diary_index'      : 'LogBook',
    'template_default' : '',
    'nested_syntaxes'  : {'vim': 'vim', 'css': 'css', 'R': 'R', 'javascript': 'javascript', 'xml': 'html', 'sh': 'sh', 'ascript': 'applescript', 'html': 'html', 'ruby': 'ruby', 'jscript': 'javascript', 'objc': 'objc', 'c++': 'cpp', 'python': 'python', 'bash': 'sh'}, 
    'diary_sort'       : 'desc',
    'path'             : '$NOTES/Notes/',
    'diary_link_fmt'   : '%Y-%m-%d',
    'template_ext'     : '',
    'syntax'           : 'markdown',
    'custom_wiki2html' : '$BIN/trunk2html_wrapper.sh',
    'index'            : 'WikiIndex',
    'diary_header'     : 'Entries',
    'ext'              : '.md',
    'path_html'        : '$NOTES/Html/',
    'temp'             : 0,
    'template_path'    : '',
    'diary_rel_path'   : ''
  }]
}}}

 * Also, notice that I have initialized a mapping that associates extensions with a default syntax (see ":help ext2syntax")
   * `:echo g:vimwiki_ext2syntax`

{{{ vim
 {'.markdown': 'markdown', '.md': 'markdown', '.mkd': 'markdown', '.wiki': 'default'}
}}}

 * Now, let's open my main page.  
   * `:e $NOTES/Home.md`
   * `:echo g:vimwiki_current_idx`

{{{ vim
  0
}}}

 * Since this page belongs to the wiki registered in `g:vimwiki_list`, there is no need to create another wiki.  Let's check that:
   * `:echo g:vimwiki_list`

{{{ vim
  [{
    'maxhi'            : 1,
    'css_name'         : 'style.css',
    'auto_export'      : 0,
    'diary_index'      : 'LogBook',
    'template_default' : '',
    'nested_syntaxes'  : {'vim': 'vim', 'css': 'css', 'R': 'R', 'javascript': 'javascript', 'xml': 'html', 'sh': 'sh', 'ascript': 'applescript', 'html': 'html', 'ruby': 'ruby', 'jscript': 'javascript', 'objc': 'objc', 'c++': 'cpp', 'python': 'python', 'bash': 'sh'}, 
    'diary_sort'       : 'desc',
    'path'             : '$NOTES/Notes/',
    'diary_link_fmt'   : '%Y-%m-%d',
    'template_ext'     : '',
    'syntax'           : 'markdown',
    'custom_wiki2html' : '$BIN/trunk2html_wrapper.sh',
    'index'            : 'WikiIndex',
    'diary_header'     : 'Entries',
    'ext'              : '.md',
    'path_html'        : '$NOTES/Html/',
    'temp'             : 0,
    'template_path'    : '',
    'diary_rel_path'   : ''
  }]
}}}

 * As a developer, it is useful to see what wiki state variables are active (g:vimwiki_current_XXX), and which have been cached with the buffer (b:vimwiki_XXX).
   * `:VimwikiPrintWikiState`

{{{ vim
  --- Internal Wiki State ---
  - Options -
    'b:vimwiki_maxhi':                    1
    'g:vimwiki_current_maxhi':            1
    'b:vimwiki_css_name':                 'style.css'
    'g:vimwiki_current_css_name':         'style.css'
    'b:vimwiki_auto_export':              0
    'g:vimwiki_current_auto_export':      0
    'b:vimwiki_diary_index':              'LogBook'
    'g:vimwiki_current_diary_index':      'LogBook'
    'b:vimwiki_template_default':         ''
    'g:vimwiki_current_template_default': ''
    'b:vimwiki_nested_syntaxes':          {'vim': 'vim', 'css': 'css', 'R': 'R', 'javascript': 'javascript', 'xml': 'html', 'sh': 'sh', 'ascript': 'applescript', 'html': 'html', 'r
  uby': 'ruby', 'jscript': 'javascript', 'objc': 'objc', 'c++': 'cpp', 'python': 'python', 'bash': 'sh'}
    'g:vimwiki_current_nested_syntaxes':  {'vim': 'vim', 'css': 'css', 'R': 'R', 'javascript': 'javascript', 'xml': 'html', 'sh': 'sh', 'ascript': 'applescript', 'html': 'html', 'r
  uby': 'ruby', 'jscript': 'javascript', 'objc': 'objc', 'c++': 'cpp', 'python': 'python', 'bash': 'sh'}
    'b:vimwiki_diary_sort':               'desc'
    'g:vimwiki_current_diary_sort':       'desc'
    'b:vimwiki_path':                     '$NOTES/Notes/'
    'g:vimwiki_current_path':             '$NOTES/Notes/'
    'b:vimwiki_diary_link_fmt':           '%Y-%m-%d'
    'g:vimwiki_current_diary_link_fmt':   '%Y-%m-%d'
    'b:vimwiki_template_ext':             ''
    'g:vimwiki_current_template_ext':     ''
    'b:vimwiki_syntax':                   'markdown'
    'g:vimwiki_current_syntax':           'markdown'
    'b:vimwiki_custom_wiki2html':         '$BIN/trunk2html_wrapper.sh'
    'g:vimwiki_current_custom_wiki2html': '$BIN/trunk2html_wrapper.sh'
    'b:vimwiki_index':                    'WikiIndex'
    'g:vimwiki_current_index':            'WikiIndex'
    'b:vimwiki_diary_header':             'Entries'
    'g:vimwiki_current_diary_header':     'Entries'
    'b:vimwiki_ext':                      '.md'
    'g:vimwiki_current_ext':              '.md'
    'b:vimwiki_path_html':                '$NOTES/Html/'
    'g:vimwiki_current_path_html':        '$NOTES/Html/'
    'b:vimwiki_temp':                     0
    'g:vimwiki_current_temp':             0
    'b:vimwiki_template_path':            ''
    'g:vimwiki_current_template_path':    ''
    'b:vimwiki_diary_rel_path':           ''
    'g:vimwiki_current_diary_rel_path':   ''
  - Cached Variables -
    'b:vimwiki_subdir':                   ''
    'g:vimwiki_current_subdir':           ''
    'b:vimwiki_idx':                      0
    'g:vimwiki_current_idx':              0
}}}

 * Now, when I open an orphan page (one that does not belong in one of the registered wikis), Vimwiki uses the extension to look up a syntax with which to initialize the orphan wiki.  In fact, we will open two orphan pages to ensure that "g:vimwiki_ext2syntax" is working.  (If "g:vimwiki_ext2syntax" is not defined, then the orphan wiki will be initialized with the default syntax).  Then we'll check that the list of registered wikis has been extended, and that the orphan wiki path and syntax are correct.
   * `:e $ORPHAN-WIKI/README.md`
   * `:echo g:vimwiki_current_idx`

{{{ vim
  1
}}}

   * `:e $VIMWIKI-MASTER/tests/wiki/index.wiki`
   * `:echo g:vimwiki_current_idx`

{{{ vim
  2
}}}

   * `:echo g:vimwiki_list`

{{{ vim
  [{
    'maxhi'            : 1,
    'css_name'         : 'style.css',
    'auto_export'      : 0,
    'diary_index'      : 'LogBook',
    'template_default' : '',
    'nested_syntaxes'  : {'vim': 'vim', 'css': 'css', 'R': 'R', 'javascript': 'javascript', 'xml': 'html', 'sh': 'sh', 'ascript': 'applescript', 'html': 'html', 'ruby': 'ruby', 'jscript': 'javascript', 'objc': 'objc', 'c++': 'cpp', 'python': 'python', 'bash': 'sh'}, 
    'diary_sort'       : 'desc',
    'path'             : '$NOTES/Notes/',
    'diary_link_fmt'   : '%Y-%m-%d',
    'template_ext'     : '',
    'syntax'           : 'markdown',
    'custom_wiki2html' : '$BIN/trunk2html_wrapper.sh',
    'index'            : 'WikiIndex',
    'diary_header'     : 'Entries',
    'ext'              : '.md',
    'path_html'        : '$NOTES/Html/',
    'temp'             : 0,
    'template_path'    : '',
    'diary_rel_path'   : ''
  }{
    'maxhi'            : 0,
    'css_name'         : 'style.css',
    'auto_export'      : 0,
    'diary_index'      : 'diary',
    'template_default' : '',
    'nested_syntaxes'  : {},
    'diary_sort'       : 'desc',
    'path'             : '$ORPHAN/',
    'diary_link_fmt'   : '%Y-%m-%d',
    'template_ext'     : '',
    'syntax'           : 'markdown',
    'custom_wiki2html' : '',
    'index'            : 'index',
    'path_html'        : '$ORPHAN_html/',
    'ext'              : '.md',
    'diary_header'     : 'Diary',
    'temp'             : 1,
    'template_path'    : '',
    'diary_rel_path'   : 'diary/'
  }{
    'maxhi'            : 0,
    'css_name'         : 'style.css',
    'auto_export'      : 0,
    'diary_index'      : 'diary',
    'template_default' : '',
    'nested_syntaxes'  : {},
    'diary_sort'       : 'desc',
    'path'             : '$VIMWIKI-MASTER/tests/wiki/',
    'diary_link_fmt'   : '%Y-%m-%d',
    'template_ext'     : '',
    'syntax'           : 'default',
    'custom_wiki2html' : '',
    'index'            : 'index',
    'path_html'        : '$VIMWIKI-MASTER/tests/wiki_html/',
    'ext'              : '.wiki',
    'diary_header'     : 'Diary',
    'temp'             : 1,
    'template_path'    : '',
    'diary_rel_path'   : 'diary/'
  }]
}}}

 * Similarly, the wiki state variables (for "tests/wiki/index.md") are:
   * `:VimwikiPrintWikiState`

{{{ vim
  --- Internal Wiki State ---
  - Options -
    'b:vimwiki_maxhi':                    0
    'g:vimwiki_current_maxhi':            0
    'b:vimwiki_css_name':                 'style.css'
    'g:vimwiki_current_css_name':         'style.css'
    'b:vimwiki_auto_export':              0
    'g:vimwiki_current_auto_export':      0
    'b:vimwiki_diary_index':              'diary'
    'g:vimwiki_current_diary_index':      'diary'
    'b:vimwiki_template_default':         ''
    'g:vimwiki_current_template_default': ''
    'b:vimwiki_nested_syntaxes':          {}
    'g:vimwiki_current_nested_syntaxes':  {}
    'b:vimwiki_diary_sort':               'desc'
    'g:vimwiki_current_diary_sort':       'desc'
    'b:vimwiki_path':                     '$VIMWIKI-MASTER/tests/wiki/'
    'g:vimwiki_current_path':             '$VIMWIKI-MASTER/tests/wiki/'
    'b:vimwiki_diary_link_fmt':           '%Y-%m-%d'
    'g:vimwiki_current_diary_link_fmt':   '%Y-%m-%d'
    'b:vimwiki_template_ext':             ''
    'g:vimwiki_current_template_ext':     ''
    'b:vimwiki_syntax':                   'default'
    'g:vimwiki_current_syntax':           'default'
    'b:vimwiki_custom_wiki2html':         ''
    'g:vimwiki_current_custom_wiki2html': ''
    'b:vimwiki_index':                    'index'
    'g:vimwiki_current_index':            'index'
    'b:vimwiki_diary_header':             'Diary'
    'g:vimwiki_current_diary_header':     'Diary'
    'b:vimwiki_ext':                      '.wiki'
    'g:vimwiki_current_ext':              '.wiki'
    'b:vimwiki_path_html':                '$VIMWIKI-MASTER/tests/wiki_html/'
    'g:vimwiki_current_path_html':        '$VIMWIKI-MASTER/tests/wiki_html/'
    'b:vimwiki_temp':                     1
    'g:vimwiki_current_temp':             1
    'b:vimwiki_template_path':            ''
    'g:vimwiki_current_template_path':    ''
    'b:vimwiki_diary_rel_path':           'diary/'
    'g:vimwiki_current_diary_rel_path':   'diary/'
  - Cached Variables -
    'b:vimwiki_subdir':                   ''
    'g:vimwiki_current_subdir':           ''
    'b:vimwiki_idx':                      1
    'g:vimwiki_current_idx':              1
}}}

 * When we follow a link to a subdirectory (e.g. `[[subwiki1/wikilinks1]]`), notice how the "subdir" variable is updated accordingly.

{{{ vim
  --- Internal Wiki State ---
  - Options -
    ...
    ...
  - Cached Variables -
    'b:vimwiki_subdir':                   'subwiki1/'
    'g:vimwiki_current_subdir':           'subwiki1/'
    'b:vimwiki_idx':                      1
    'g:vimwiki_current_idx':              1
}}}

Original comment by stu.andrews on 9 May 2012 at 5:02

GoogleCodeExporter commented 8 years ago
Hmm, I have checked again and it works for me too. False alarm. :)

Original comment by habamax on 9 May 2012 at 6:57