summaryrefslogtreecommitdiff
path: root/autoload/fuf/buffer.vim
blob: 08b954ab4306cc2f8f2fa6a2ee054c465c18b668 (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
"=============================================================================
" 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#buffer#createHandler(base)
  return a:base.concretize(copy(s:handler))
endfunction

"
function fuf#buffer#getSwitchOrder()
  return g:fuf_buffer_switchOrder
endfunction

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

"
function fuf#buffer#renewCache()
endfunction

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

"
function fuf#buffer#onInit()
  call fuf#defineLaunchCommand('FufBuffer', s:MODE_NAME, '""', [])
  augroup fuf#buffer
    autocmd!
    autocmd BufEnter     * call s:updateBufTimes()
    autocmd BufWritePost * call s:updateBufTimes()
  augroup END
endfunction

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

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

let s:bufTimes = {}

"
function s:updateBufTimes()
  let s:bufTimes[bufnr('%')] = localtime()
endfunction

"
function s:makeItem(nr)
  let fname = (empty(bufname(a:nr))
        \      ? '[No Name]'
        \      : fnamemodify(bufname(a:nr), ':p:~:.'))
  let time = (exists('s:bufTimes[a:nr]') ? s:bufTimes[a:nr] : 0)
  let item = fuf#makePathItem(fname, strftime(g:fuf_timeFormat, time), 0)
  let item.index = a:nr
  let item.bufNr = a:nr
  let item.time = time
  let item.abbrPrefix = s:getBufIndicator(a:nr) . ' '
  return item
endfunction

"
function s:getBufIndicator(bufNr)
  if !getbufvar(a:bufNr, '&modifiable')
    return '[-]'
  elseif getbufvar(a:bufNr, '&modified')
    return '[+]'
  elseif getbufvar(a:bufNr, '&readonly')
    return '[R]'
  else
    return '   '
  endif
endfunction

"
function s:compareTimeDescending(i1, i2)
  return a:i1.time == a:i2.time ? 0 : a:i1.time > a:i2.time ? -1 : +1
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_buffer_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:interpretPrimaryPatternForPath',
        \                   self.partialMatching)
endfunction

"
function s:handler.makePreviewLines(word, count)
  let item = s:findItem(self.items, a:word)
  if empty(item)
    return []
  endif
  return fuf#makePreviewLinesForFile(item.bufNr, a:count, self.getPreviewHeight())
endfunction

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

"
function s:handler.onOpen(word, mode)
  " not use bufnr(a:word) in order to handle unnamed buffer
  let item = s:findItem(self.items, a:word)
  if empty(item)
    " do nothing
  elseif a:mode ==# s:OPEN_TYPE_DELETE
    execute item.bufNr . 'bdelete'
    let self.reservedMode = self.getModeName()
  else
    call fuf#openBuffer(item.bufNr, a:mode, g:fuf_reuseWindow)
  endif
endfunction

"
function s:handler.onModeEnterPre()
endfunction

"
function s:handler.onModeEnterPost()
  call fuf#defineKeyMappingInHandler(g:fuf_buffer_keyDelete,
        \                            'onCr(' . s:OPEN_TYPE_DELETE . ')')
  let self.items = range(1, bufnr('$'))
  call filter(self.items, 'buflisted(v:val) && v:val != self.bufNrPrev && v:val != bufnr("%")')
  call map(self.items, 's:makeItem(v:val)')
  if g:fuf_buffer_mruOrder
    call sort(self.items, 's:compareTimeDescending')
    call fuf#mapToSetSerialIndex(self.items, 1)
  endif
  let self.items = fuf#mapToSetAbbrWithSnippedWordAsPath(self.items)
endfunction

"
function s:handler.onModeLeavePost(opened)
endfunction

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