# Link creation: my favorite (tinmarino)
# You know, when pressing Enter:
#   in mode normal, visual
#   in OS windows, linux
# Seems easy but tests are reaaly needed here

# Links Renaming inside #1138 {{{1

Given vimwiki (Link with toto):
  [if nothing goes right, go left](www.the_bad_jokes.com)

Do(Rename description):
# Change in bracket
  ci[
  new description
  \<Esc>

Expect(New description):
  [new description](www.the_bad_jokes.com)

Do(Rename url):
# Move to (
  %l
# Change in parenthesis
  ci(
  www.new_url.com

Expect(New URL):
  [if nothing goes right, go left](www.new_url.com)


# Links with dot {{{1
# Issue #924
# See for spec: https://github.com/vimwiki/vimwiki/issues/924#issuecomment-672837685
####################

Given vimwiki (filename filename.dot):
  filename
  filename.dot

Do (Press filename):
  :call SetSyntax('default')\<CR>
  \<CR>\<CR>
  :AssertEqual 'filename.wiki', expand('%:t')\<CR>
  :call DeleteFile('%')\<CR>

Do (Press filename.dot):
  j
  \<CR>\<CR>
  :AssertEqual 'filename.dot.wiki', expand('%:t')\<CR>
  :call DeleteFile('%')\<CR>

Expect (Nothing left):
  

# Linkify function {{{1
# Issue #994
####################

Given vimwiki (abc def ghi jkl):
  https://github.com/vimwiki/vimwiki

Do(wiki: call linkify):
  :if v:version >= 704\<CR>
    call vimwiki#base#linkify()\<CR>
  else\<CR>
    let stg = '[[https://github.com/vimwiki/vimwiki|GitHub - vimwiki/vimwiki: Personal Wiki for Vim]]'\<CR>
    0put =stg\<CR>
    $d\<CR>
  endif\<CR>
#  else\<CR>
#  endif\<CR>

Expect(Wiki link):
  [[https://github.com/vimwiki/vimwiki|GitHub - vimwiki/vimwiki: Personal Wiki for Vim]]

Do(md: call linkify):
  :call SetSyntax('markdown')\<CR>
  :if v:version >= 704\<CR>
    call vimwiki#base#linkify()\<CR>
  else\<CR>
    0put ='[GitHub - vimwiki/vimwiki: Personal Wiki for Vim](https://github.com/vimwiki/vimwiki)'\<CR>
    $d\<CR>
  endif\<CR>

Expect(Markdown link):
  [GitHub - vimwiki/vimwiki: Personal Wiki for Vim](https://github.com/vimwiki/vimwiki)

# Link Normalisation {{{1
# And configuration
# Issues: #892
####################

Execute (Log):
  Log 'Markdown change Link1 : Pressing enter to create a [[double bracket]] #892'

Given vimwiki (abc def ghi jkl):
  abc def ghi jkl

Execute (Set filename wiki_test.md):
  call SetSyntax('markdown')
  let save_link = g:vimwiki_syntaxlocal_vars.markdown.Link1
  let g:vimwiki_syntaxlocal_vars.markdown.Link1 = vimwiki#vars#get_global('WikiLinkTemplate1')

Do (vee<CR>):
  vee\<CR>

Expect (append md suffix):
  [[abc def]] ghi jkl

Execute (restore):
  let g:vimwiki_syntaxlocal_vars.markdown.Link1 = save_link


# vimwiki_markdown_link_ext {{{1
####################

Execute (Log):
  Log 'vimwiki_markdown_link_ext'

Given vimwiki (abc def ghi jkl):
  abc def ghi jkl

Execute (Set filename wiki_test.md):
  Log '>> Visual creation, markdown syntax'
  file wiki_test.md
  let g:vimwiki_markdown_link_ext = 1
  call ReloadVars()
  call SetSyntax('markdown')
  AssertEqual vimwiki#vars#get_wikilocal('syntax'), 'markdown'
  AssertEqual vimwiki#vars#get_wikilocal('markdown_link_ext'), 1

Do (vee<CR>):
  vee\<CR>

Expect (append md suffix):
  [abc def](abc def.md) ghi jkl

Execute (Restore variable g:vimwiki_markdown_link_ext):
  unlet g:vimwiki_markdown_link_ext
  call ReloadVars()


# Visual Creation {{{1
# Issues: #382
####################

Execute (Log):
  Log 'Visual Creation'

# For markdown {{{2
# ------------------

Given vimwiki (abc def ghi jkl):
  abc def ghi jkl

Execute (Set filename wiki_test.md):
  Log '>> Visual creation, markdown syntax'
  file wiki_test.md
  call SetSyntax('markdown')
  AssertEqual vimwiki#vars#get_wikilocal('syntax'), 'markdown'

Do (v3e):
  v3e\<Cr>

Expect (3 Words []()):
  [abc def ghi](abc def ghi) jkl

Do (v3e):
  wv2e\<Cr>

Expect (2 Words []()):
  abc [def ghi](def ghi) jkl

Do (selection=exclusive v3e):
  :set selection=exclusive\<Cr>
  wv2e\<Cr>

Expect (2 Words []()):
  abc [def ghi](def ghi) jkl

Do (selection=exclusive wv$):
  :set selection=exclusive\<Cr>
  wv$\<Cr>

Expect (3 Words []()):
  abc [def ghi jkl](def ghi jkl)


# For Wiki {{{2
# ------------------

Given vimwiki (abc def ghi jkl):
  abc def ghi jkl

Execute (Set filename wiki_test.md):
  Log '>> Visual creation, wiki syntax'
  file wiki_test.wiki
  call SetSyntax('default')

Do (v3e):
  v3e\<Cr>

Expect (3 Words []()):
  [[abc def ghi]] jkl

Do (v3e):
  wv2e\<Cr>

Expect (2 Words []()):
  abc [[def ghi]] jkl

Do (selection=exclusive v3e):
  :set selection=exclusive\<Cr>
  wv2e\<Cr>

Expect (2 Words []()):
  abc [[def ghi]] jkl

Do (selection=exclusive wv$):
  :set selection=exclusive\<Cr>
  wv$\<Cr>

Expect (3 Words []()):
  abc [[def ghi jkl]]


# Absolute links {{{1
####################

Execute (Log):
  Log 'Absolute links: full paths and in-wiki'

# For markdown {{{2
# ------------------

Execute (Set filename wiki_test.md):
  Log '>> Absolute link, markdown syntax'
  file wiki_test.md
  call SetSyntax('markdown')

Given vimwiki(some wiki link):
  [test1](//$HOME/in_home1)
  [test2](//~/in_home2)
  [test3](///tmp/in_tmp)
  [test4](/in_current_wiki)

Do (Check in_home1):
  \<Cr>
  :AssertEqual expand('%'), $HOME.'/in_home1.md'\<Cr>

Do (Check in_home2):
  j\<Cr>
  :AssertEqual expand('%'), $HOME.'/in_home2.md'\<Cr>

Do (Check in_tmp):
  jj\<Cr>
  :AssertEqual expand('%'), '/tmp/in_tmp.md'\<Cr>

# Here, assuming that "current wiki" means the working directory, since
# no wiki is currently defined:
Do (Check in_current_wiki):
  jjj\<Cr>
  :AssertEqual expand('%'), '/testplugin/in_current_wiki.md'\<Cr>

# For Wiki {{{2
# ------------------

Execute (Set filename wiki_test.wiki):
  Log '>> Absolute link, wiki syntax'
  file wiki_test.wiki
  call SetSyntax('default')

Given vimwiki(some wiki link):
  [[//$HOME/in_home1]]
  [[//~/in_home2]]
  [[///tmp/in_tmp]]
  [[/in_current_wiki]]

Do (Check in_home1):
  \<Cr>
  :AssertEqual expand('%'), $HOME.'/in_home1.wiki'\<Cr>

Do (Check in_home2):
  j\<Cr>
  :AssertEqual expand('%'), $HOME.'/in_home2.wiki'\<Cr>

Do (Check in_tmp):
  jj\<Cr>
  :AssertEqual expand('%'), '/tmp/in_tmp.wiki'\<Cr>

# Here, assuming that "current wiki" means the working directory, since
# no wiki is currently defined:
Do (Check in_current_wiki):
  jjj\<Cr>
  :AssertEqual expand('%'), '/testplugin/in_current_wiki.wiki'\<Cr>

Execute(Clean: temporary):
  call ReloadVimwiki()
  call DeleteFile('$HOME/in_home1.md')
  call DeleteFile('~/in_home2.md')
  call DeleteFile('/tmp/in_tmp.md')

# Link with dot {{{1
####################

Execute (Log):
  Log 'Link  with dot'

Given vimwiki (filenames with dots):
  part1.part2.part3
  part1.part2.part3.md
  noext

Execute (Set filename wiki_test.md):
  file wiki_test.md
  call SetSyntax('markdown')

Do (Linkify all):
  \<Cr>
  j\<Cr>
  j\<Cr>

Expect (Full Words are made as links, no extension addded . -> space):
  [part1 part2 part3](part1.part2.part3)
  [part1 part2 part3](part1.part2.part3.md)
  [noext](noext)

Given vimwiki (filnames with dots):
  part1.part2.part3
  part1.part2.part3.md
  noext

Do (Follow link witout markdown):
  \<Cr>\<Cr>
  " See issue #950 always adding the extension
  :AssertEqual 'part1.part2.part3.md', expand('%:t')\<Cr>

Do (Follow link with .md extension):
  j\<Cr>\<Cr>
  :AssertEqual 'part1.part2.part3.md', expand('%:t')\<Cr>


# Rest {{{1
##########################

Execute (Log):
  Log 'And more'

Given vimwiki (Text that is not a wikilink):
  test
  www.google.com
  https://www.google.com
  multiple words
  let's
  let's
  file.wiki
  file.md
  file.mw

Execute (Set syntax to default):
  call SetSyntax('default')

Do (Create links default syntax):
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>

Expect (Vimwiki links):
  [[test]]
  [[www.google.com]]
  [[https://www.google.com]]
  [[multiple words]]
  [[let's]]
  [[let]]'s
  [[file.wiki]]
  [[file.md]]
  [[file.mw]]

Execute (Set syntax to markdown):
  call SetSyntax('markdown')

Do (Create links markdown syntax):
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>

Expect (Markdown links):
  [test](test)
  [www.google.com](www.google.com)
  [https://www.google.com](https://www.google.com)
  [multiple words](multiple words)
  [let's](let's)
  [let](let)'s
  [file wiki](file.wiki)
  [file](file.md)
  [file mw](file.mw)

Execute (Set syntax to mediawiki):
  call SetSyntax('media')

Do (Create links mediawiki syntax):
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  v$
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>
  j
  \<Enter>

Expect (Mediawiki links):
  [[test]]
  [[www.google.com]]
  [[https://www.google.com]]
  [[multiple words]]
  [[let's]]
  [[let]]'s
  [[file.wiki]]
  [[file.md]]
  [[file.mw]]

Execute(Clean: Icreated many temporary wiki):
  call ReloadVimwiki()

# vim: foldmethod=marker foldlevel=30
