summaryrefslogtreecommitdiff
path: root/autoload/fuf/bookmarkfile.vim
blob: 12ac80f6cb698ab8fd39b35ef1e0af1af89daf2d (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
199
"=============================================================================
" Copyright (c) 2010 Takeshi NISHIDA
"
"=============================================================================
" LOAD GUARD {{{1

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

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

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

"
function fuf#bookmarkfile#getSwitchOrder()
  return g:fuf_bookmarkfile_switchOrder
endfunction

"
function fuf#bookmarkfile#getEditableDataNames()
  return ['items']
endfunction

"
function fuf#bookmarkfile#renewCache()
endfunction

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

"
function fuf#bookmarkfile#onInit()
  call fuf#defineLaunchCommand('FufBookmarkFile', s:MODE_NAME, '""', [])
  command! -bang -narg=?        FufBookmarkFileAdd               call s:bookmarkHere(<q-args>)
  command! -bang -narg=0 -range FufBookmarkFileAddAsSelectedText call s:bookmarkHere(l9#getSelectedText())
endfunction

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

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

" opens a:path and jumps to the line matching to a:pattern from a:lnum within
" a:range. if not found, jumps to a:lnum.
function s:jumpToBookmark(path, mode, pattern, lnum)
  call fuf#openFile(a:path, a:mode, g:fuf_reuseWindow)
  call cursor(s:getMatchingLineNumber(getline(1, '$'), a:pattern, a:lnum), 0)
  normal! zvzz
endfunction

"
function s:getMatchingLineNumber(lines, pattern, lnumBegin)
  let l = min([a:lnumBegin, len(a:lines)])
  for [l0, l1] in map(range(0, g:fuf_bookmarkfile_searchRange),
        \             '[l + v:val, l - v:val]')
    if l0 <= len(a:lines) && a:lines[l0 - 1] =~# a:pattern
      return l0
    elseif l1 >= 0 && a:lines[l1 - 1] =~# a:pattern
      return l1
    endif
  endfor
  return l
endfunction

"
function s:getLinePattern(lnum)
  return '\C\V\^' . escape(getline(a:lnum), '\') . '\$'
endfunction

"
function s:bookmarkHere(word)
  if !empty(&buftype) || expand('%') !~ '\S'
    call fuf#echoWarning('Can''t bookmark this buffer.')
    return
  endif
  let item = {
        \   'word' : (a:word =~# '\S' ? substitute(a:word, '\n', ' ', 'g')
        \                             : pathshorten(expand('%:p:~')) . '|' . line('.') . '| ' . getline('.')),
        \   'path' : expand('%:p'),
        \   'lnum' : line('.'),
        \   'pattern' : s:getLinePattern(line('.')),
        \   'time' : localtime(),
        \ }
  let item.word = l9#inputHl('Question', '[fuf] Bookmark as:', item.word)
  if item.word !~ '\S'
    call fuf#echoWarning('Canceled')
    return
  endif
  let items = fuf#loadDataFile(s:MODE_NAME, 'items')
  call insert(items, item)
  call fuf#saveDataFile(s:MODE_NAME, 'items', items)
endfunction

"
function s:findItem(items, word)
  for item in a:items
    if item.word ==# a:word
      return item
    endif
  endfor
  return {}
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_bookmarkfile_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 item = s:findItem(fuf#loadDataFile(s:MODE_NAME, 'items'), a:word)
  let lines = fuf#getFileLines(item.path)
  if empty(lines)
    return []
  endif
  let index = s:getMatchingLineNumber(lines, item.pattern, item.lnum) - 1
  return fuf#makePreviewLinesAround(
        \ lines, [index], a:count, self.getPreviewHeight())
endfunction

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

"
function s:handler.onOpen(word, mode)
  if a:mode ==# s:OPEN_TYPE_DELETE
    let items = fuf#loadDataFile(s:MODE_NAME, 'items')
    call filter(items, 'v:val.word !=# a:word')
    call fuf#saveDataFile(s:MODE_NAME, 'items', items)
    let self.reservedMode = self.getModeName()
    return
  else
    let item = s:findItem(fuf#loadDataFile(s:MODE_NAME, 'items'), a:word)
    if !empty(item)
        call s:jumpToBookmark(item.path, a:mode, item.pattern, item.lnum)
    endif
  endif
endfunction

"
function s:handler.onModeEnterPre()
endfunction

"
function s:handler.onModeEnterPost()
  call fuf#defineKeyMappingInHandler(g:fuf_bookmarkfile_keyDelete,
        \                            'onCr(' . s:OPEN_TYPE_DELETE . ')')
  let self.items = fuf#loadDataFile(s:MODE_NAME, 'items')
  call map(self.items, 'fuf#makeNonPathItem(v:val.word, strftime(g:fuf_timeFormat, v:val.time))')
  call fuf#mapToSetSerialIndex(self.items, 1)
  call map(self.items, 'fuf#setAbbrWithFormattedWord(v:val, 1)')
endfunction

"
function s:handler.onModeLeavePost(opened)
endfunction

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