summaryrefslogtreecommitdiff
path: root/autoload/fuf/help.vim
blob: 8f03e36c2e6a8993219e34a20cf3160457cd2d9d (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"=============================================================================
" Copyright (c) 2007-2010 Takeshi NISHIDA
"
"=============================================================================
" LOAD GUARD {{{1

if !l9#guardScriptLoading(expand('<sfile>:p'), 0, 0, [])
  finish
endif

" }}}1
"=============================================================================
" GLOBAL FUNCTIONS {{{1

"
function fuf#help#createHandler(base)
  return a:base.concretize(copy(s:handler))
endfunction

"
function fuf#help#getSwitchOrder()
  return g:fuf_help_switchOrder
endfunction

"
function fuf#help#getEditableDataNames()
  return []
endfunction

"
function fuf#help#renewCache()
  let s:cache = {}
endfunction

"
function fuf#help#requiresOnCommandPre()
  return 0
endfunction

"
function fuf#help#onInit()
  call fuf#defineLaunchCommand('FufHelp'              , s:MODE_NAME, '""', [])
  call fuf#defineLaunchCommand('FufHelpWithCursorWord', s:MODE_NAME, 'expand(''<cword>'')', [])
endfunction

" }}}1
"=============================================================================
" LOCAL FUNCTIONS/VARIABLES {{{1

let s:MODE_NAME = expand('<sfile>:t:r')

"
function s:getCurrentHelpTagFiles()
  let prefix = 'doc' . l9#getPathSeparator()
  let tagFiles = split(globpath(&runtimepath, prefix . 'tags'   ), "\n")
        \      + split(globpath(&runtimepath, prefix . 'tags-??'), "\n")
  return sort(map(tagFiles, 'fnamemodify(v:val, ":p")'))
endfunction

"
function s:parseHelpTagEntry(line, tagFile)
  let elements = split(a:line, "\t")
  if len(elements) != 3 || elements[0][0] ==# '!'
    return {}
  endif
  let suffix = matchstr(a:tagFile, '-\zs..$')
  if empty(suffix) 
    let suffix = '@en'
  else
    let suffix = '@' . suffix
  endif
  let dir = fnamemodify(a:tagFile, ':h') . l9#getPathSeparator()
  return {
        \   'word'   : elements[0] . suffix,
        \   'path'   : dir . elements[1],
        \   'pattern': elements[2][1:],
        \ }
endfunction

"
function s:getHelpTagEntries(tagFile)
  let names = map(l9#readFile(a:tagFile), 's:parseHelpTagEntry(v:val, a:tagFile)')
  return filter(names, '!empty(v:val)')
endfunction

"
function s:parseHelpTagFiles(tagFiles, key)
  let cacheName = 'cache-' . l9#hash224(a:key)
  let cacheTime = fuf#getDataFileTime(s:MODE_NAME, cacheName)
  if cacheTime != -1 && fuf#countModifiedFiles(a:tagFiles, cacheTime) == 0
    return fuf#loadDataFile(s:MODE_NAME, cacheName)
  endif
  let items = l9#unique(l9#concat(map(copy(a:tagFiles), 's:getHelpTagEntries(v:val)')))
  let items = map(items, 'extend(v:val, fuf#makeNonPathItem(v:val.word, ""))')
  call fuf#mapToSetSerialIndex(items, 1)
  let items = map(items, 'fuf#setAbbrWithFormattedWord(v:val, 1)')
  call fuf#saveDataFile(s:MODE_NAME, cacheName, items)
  return items
endfunction

"
function s:enumHelpTags(tagFiles)
  if !len(a:tagFiles)
    return []
  endif
  let key = join([g:fuf_ignoreCase] + a:tagFiles, "\n")
  if !exists('s:cache[key]') || fuf#countModifiedFiles(a:tagFiles, s:cache[key].time)
    let s:cache[key] = {
          \   'time'  : localtime(),
          \   'items' : s:parseHelpTagFiles(a:tagFiles, key)
          \ }
  endif
  return s:cache[key].items
endfunction

"
function s:getMatchingIndex(lines, pattern)
  if empty(a:pattern)
    return -1
  endif
  for i in range(len(a:lines))
    if stridx(a:lines[i], a:pattern) >= 0
      return i
    endif
  endfor
  return -1
endfunction

" }}}1
"=============================================================================
" s:handler {{{1

let s:handler = {}

"
function s:handler.getModeName()
  return s:MODE_NAME
endfunction

"
function s:handler.getPrompt()
  return fuf#formatPrompt(g:fuf_help_prompt, self.partialMatching, '')
endfunction

"
function s:handler.getPreviewHeight()
  return g:fuf_previewHeight
endfunction

"
function s:handler.isOpenable(enteredPattern)
  return 1
endfunction

"
function s:handler.makePatternSet(patternBase)
  return fuf#makePatternSet(a:patternBase, 's:interpretPrimaryPatternForNonPath',
        \                   self.partialMatching)
endfunction

"
function s:handler.makePreviewLines(word, count)
  let items = filter(copy(s:enumHelpTags(self.tagFiles)), 'v:val.word ==# a:word')
  if empty(items)
    return []
  endif
  let lines = fuf#getFileLines(items[0].path)
  let index = s:getMatchingIndex(lines, items[0].pattern)
  return [items[0].path . ':'] + fuf#makePreviewLinesAround(
        \ lines, (index < 0 ? [] : [index]), a:count, self.getPreviewHeight() - 1)
endfunction

"
function s:handler.getCompleteItems(patternPrimary)
  return s:enumHelpTags(self.tagFiles)
endfunction

"
function s:handler.onOpen(word, mode)
  call fuf#openHelp(a:word, a:mode)
endfunction

"
function s:handler.onModeEnterPre()
  let self.tagFiles = s:getCurrentHelpTagFiles()
endfunction

"
function s:handler.onModeEnterPost()
endfunction

"
function s:handler.onModeLeavePost(opened)
endfunction

" }}}1
"=============================================================================
" vim: set fdm=marker: