File: utils.vim

package info (click to toggle)
vim-link-vim 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 524 kB
  • sloc: python: 66; makefile: 30
file content (177 lines) | stat: -rw-r--r-- 4,699 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function! linkvim#url#utils#add_extension(filename) abort
  " Input: Filename with possibly missing file extension
  " Output: Filename with resolved extension

  " Collect extension candidates
  let l:extensions = linkvim#u#uniq_unsorted(g:linkvim_filetypes
        \ + (exists('b:wiki.extension') ? [b:wiki.extension] : []))

  if index(l:extensions, fnamemodify(a:filename, ':e')) >= 0
    return a:filename
  endif

  " Determine the proper extension (if necessary)
  for l:ext in l:extensions
    let l:newpath = a:filename . '.' . l:ext
    if filereadable(l:newpath) | return l:newpath | endif
  endfor

  " Fallback
  return a:filename . '.' . l:extensions[0]
endfunction

function! linkvim#url#utils#extract_anchor(stripped) abort
  let l:parts = split(a:stripped, '#', 1)

  return len(l:parts) > 1
        \ ? substitute(join(l:parts[1:], '#'), '#$', '', '')
        \ : ''
endfunction

function! linkvim#url#utils#resolve_path(filename, origin) abort
  let l:filename = a:filename
  if l:filename =~# '/$'
    let l:filename .= get(get(b:, 'wiki', {}), 'index_name', '')
  endif

  " Link within same page has empty filename
  if empty(l:filename) | return a:origin | endif

  let l:path = l:filename[0] ==# '/'
        \ ? linkvim#get_root() . l:filename
        \ : (empty(a:origin)
        \   ? linkvim#get_root()
        \   : fnamemodify(a:origin, ':p:h')) . '/' . l:filename
  let l:path = linkvim#paths#s(l:path)

  return linkvim#url#utils#add_extension(l:path)
endfunction


function! linkvim#url#utils#go_to_file(path, edit_cmd, url_stripped, do_edit) abort
  if !a:do_edit | return | endif

  " Check if dir exists
  let l:dir = fnamemodify(a:path, ':p:h')
  if !isdirectory(l:dir)
    call mkdir(l:dir, 'p')
  endif

  try
    execute a:edit_cmd fnameescape(a:path)
  catch /E325:/
  endtry

  let b:wiki = get(b:, 'wiki', {})

  if !filereadable(a:path)
    redraw!
    call linkvim#log#info('Opened new page "' . a:url_stripped . '"')
  end
endfunction

function! linkvim#url#utils#go_to_anchor_adoc(anchor, do_edit) abort
  if empty(a:anchor) | return | endif

  " Manually add position to jumplist (necessary if we're in same file)
  if !a:do_edit
    normal! m'
  endif

  let l:match = matchlist(a:anchor, '\(.*\)[- _]\(\d\+\)$')
  if empty(l:match)
    let l:re = a:anchor
    let l:num = 1
  else
    let l:re = l:match[1]
    let l:num = l:match[2]
  endif

  let l:re = substitute(l:re, '^_', '', '')
  let l:re = substitute(l:re, '[- _]', '[- _]', 'g')
  let l:re = '\c^=\{1,6}\s*' . l:re

  let l:old_pos = getpos('.')
  call cursor(1, 1)

  for l:_ in range(l:num)
    if !search(l:re, l:_ == 0 ? 'Wc' : 'W')
      call setpos('.', l:old_pos)
      break
    endif
    let l:old_pos = getpos('.')
  endfor
endfunction

function! linkvim#url#utils#go_to_anchor_wiki(anchor, do_edit) abort
  if empty(a:anchor) | return | endif

  " Manually add position to jumplist (necessary if we're in same file)
  if !a:do_edit
    normal! m'
  endif

  let l:old_pos = getcurpos('.')
  call cursor(1, 1)

  for l:part in split(a:anchor, '#', 0)
    let l:part = substitute(l:part, '[- ]', '[- ]', 'g')
    let l:header = '^\c#\{1,6}\s*' . l:part . '\s*$'
    let l:headerid =
          \ '^\C#\{1,6}\s*\w.*\s{#'
          \ .. substitute(l:part, ' ', '-', 'g')
          \ .. '}\s*$'
    let l:bold = linkvim#rx#surrounded(l:part, '*')

    if !(search(l:header, 'Wc')
          \ || search(l:bold, 'Wc')
          \ || search(l:headerid, 'Wc'))
      call setpos('.', l:old_pos)
      break
    endif
    let l:old_pos = getcurpos('.')
  endfor
endfunction

function! linkvim#url#utils#focus(do_edit) abort
  if !&foldenable | return | endif

  if a:do_edit
    normal! zx
  else
    normal! zv
  endif
endfunction


function! linkvim#url#utils#url_encode(str) abort
  " This code is based on Tip Pope's vim-unimpaired:
  " https://github.com/tpope/vim-unimpaired
  return substitute(
        \ a:str,
        \ '[^A-Za-z0-9_.~-]',
        \ '\="%".printf("%02X",char2nr(submatch(0)))',
        \ 'g'
        \)
endfunction

function! linkvim#url#utils#url_decode(str) abort
  " This code is based on Tip Pope's vim-unimpaired:
  " https://github.com/tpope/vim-unimpaired
  let l:str =
        \ substitute(
        \   substitute(
        \     substitute(a:str, '%0[Aa]\n$', '%0A', ''),
        \     '%0[Aa]', '\n', 'g'),
        \   '+', ' ', 'g')
  return substitute(l:str, '%\(\x\x\)', '\=nr2char("0x".submatch(1))', 'g')
endfunction

function! linkvim#url#utils#url_encode_specific(str, chars) abort
  return substitute(
        \ a:str,
        \ '[' .. a:chars .. ']',
        \ '\="%"..printf("%02X", char2nr(submatch(0)))',
        \ 'g'
        \)
endfunction