startify.vim 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. " vim: et sw=2 sts=2
  2. " Plugin: https://github.com/mhinz/vim-startify
  3. " Description: A fancy start screen for Vim.
  4. " Maintainer: Marco Hinz <http://github.com/mhinz>
  5. if exists('g:autoloaded_startify') || &compatible
  6. finish
  7. endif
  8. let g:autoloaded_startify = 1
  9. " Init: values {{{1
  10. let s:numfiles = get(g:, 'startify_files_number', 10)
  11. let s:show_special = get(g:, 'startify_enable_special', 1)
  12. let s:relative_path = get(g:, 'startify_relative_path') ? ':~:.' : ':p:~'
  13. let s:session_dir = resolve(expand(get(g:, 'startify_session_dir',
  14. \ has('win32') ? '$HOME\vimfiles\session' : '~/.vim/session')))
  15. let s:tf = exists('g:startify_transformations')
  16. let s:skiplist = get(g:, 'startify_skiplist', [
  17. \ 'COMMIT_EDITMSG',
  18. \ 'runtime/doc/.*\.txt',
  19. \ 'bundle/.*/doc/.*\.txt',
  20. \ 'plugged/.*/doc/.*\.txt',
  21. \ escape(fnamemodify(resolve($VIMRUNTIME), ':p'), '\') .'doc/.*\.txt',
  22. \ ])
  23. let s:padding_left = repeat(' ', get(g:, 'startify_padding_left', 3))
  24. let s:fixed_column = len(s:padding_left) + 2
  25. " Function: #get_separator {{{1
  26. function! startify#get_separator() abort
  27. return !exists('+shellslash') || &shellslash ? '/' : '\'
  28. endfunction
  29. let s:sep = startify#get_separator()
  30. " Function: #get_lastline {{{1
  31. function! startify#get_lastline() abort
  32. return b:startify.lastline + 1
  33. endfunction
  34. " Function: #insane_in_the_membrane {{{1
  35. function! startify#insane_in_the_membrane() abort
  36. " Handle vim -y, vim -M.
  37. if &insertmode
  38. \ || (!&modifiable && &buftype != 'terminal' && &filetype != 'startify')
  39. return
  40. endif
  41. if !&hidden && &modified
  42. call s:warn('Save your changes first.')
  43. return
  44. endif
  45. if !empty(v:servername) && exists('g:startify_skiplist_server')
  46. for servname in g:startify_skiplist_server
  47. if servname == v:servername
  48. return
  49. endif
  50. endfor
  51. endif
  52. if line2byte('$') != -1
  53. noautocmd enew
  54. endif
  55. silent! setlocal
  56. \ bufhidden=wipe
  57. \ colorcolumn=
  58. \ nobuflisted
  59. \ nocursorcolumn
  60. \ nocursorline
  61. \ nolist
  62. \ nonumber
  63. \ norelativenumber
  64. \ nospell
  65. \ noswapfile
  66. \ matchpairs=
  67. if empty(&statusline)
  68. setlocal statusline=\ startify
  69. endif
  70. " Must be global so that it can be read by syntax/startify.vim.
  71. let g:startify_header = exists('g:startify_custom_header')
  72. \ ? s:set_custom_section(g:startify_custom_header)
  73. \ : startify#fortune#cowsay()
  74. if !empty(g:startify_header)
  75. let g:startify_header += [''] " add blank line
  76. endif
  77. call append('$', g:startify_header)
  78. let b:startify = { 'tick': 0, 'entries': {}, 'indices': [] }
  79. if s:show_special
  80. call append('$', [s:padding_left .'[e] <empty buffer>', ''])
  81. endif
  82. call s:register(line('$')-1, 'e', 'special', 'enew', '')
  83. let b:startify.entry_number = 0
  84. if filereadable('Session.vim')
  85. call append('$', [s:padding_left .'[0] '. getcwd() . s:sep .'Session.vim', ''])
  86. call s:register(line('$')-1, '0', 'session',
  87. \ 'call startify#session_delete_buffers() | source', 'Session.vim')
  88. let b:startify.entry_number = 1
  89. let l:show_session = 1
  90. endif
  91. if empty(v:oldfiles)
  92. call s:warn("Can't read viminfo file. Read :help startify-faq-02")
  93. endif
  94. let b:startify.section_header_lines = []
  95. let lists = s:get_lists()
  96. call s:show_lists(lists)
  97. silent $delete _
  98. if s:show_special
  99. call append('$', ['', s:padding_left .'[q] <quit>'])
  100. call s:register(line('$'), 'q', 'special', 'call s:close()', '')
  101. else
  102. " Don't overwrite the last regular entry, thus +1
  103. call s:register(line('$')+1, 'q', 'special', 'call s:close()', '')
  104. endif
  105. " compute first line offset
  106. let b:startify.firstline = 2
  107. let b:startify.firstline += len(g:startify_header)
  108. " no special, no local Session.vim, but a section header
  109. if !s:show_special && !exists('l:show_session') && has_key(lists[0], 'header')
  110. let b:startify.firstline += len(lists[0].header) + 1
  111. endif
  112. let b:startify.lastline = line('$')
  113. let footer = exists('g:startify_custom_footer')
  114. \ ? s:set_custom_section(g:startify_custom_footer)
  115. \ : []
  116. if !empty(footer)
  117. let footer = [''] + footer
  118. endif
  119. call append('$', footer)
  120. setlocal nomodifiable nomodified
  121. call s:set_mappings()
  122. call cursor(b:startify.firstline, 5)
  123. autocmd startify CursorMoved <buffer> call s:set_cursor()
  124. silent! %foldopen!
  125. set filetype=startify
  126. if exists('##DirChanged')
  127. autocmd startify DirChanged <buffer> Startify
  128. endif
  129. if exists('#User#Startified')
  130. doautocmd <nomodeline> User Startified
  131. endif
  132. if exists('#User#StartifyReady')
  133. doautocmd <nomodeline> User StartifyReady
  134. endif
  135. endfunction
  136. " Function: #session_load {{{1
  137. function! startify#session_load(...) abort
  138. if !isdirectory(s:session_dir)
  139. echomsg 'The session directory does not exist: '. s:session_dir
  140. return
  141. elseif empty(startify#session_list_as_string(''))
  142. echomsg 'There are no sessions...'
  143. return
  144. endif
  145. let spath = s:session_dir . s:sep
  146. if a:0
  147. let spath .= a:1
  148. else
  149. if has('win32')
  150. call inputsave()
  151. let spath .= input(
  152. \ 'Load this session: ',
  153. \ fnamemodify(v:this_session, ':t'),
  154. \ 'custom,startify#session_list_as_string') | redraw
  155. call inputrestore()
  156. else
  157. let spath .= '__LAST__'
  158. endif
  159. endif
  160. if filereadable(spath)
  161. if get(g:, 'startify_session_persistence') && filewritable(v:this_session)
  162. call startify#session_write(fnameescape(v:this_session))
  163. endif
  164. call startify#session_delete_buffers()
  165. execute 'source '. fnameescape(spath)
  166. call s:create_last_session_link(spath)
  167. else
  168. echo 'No such file: '. spath
  169. endif
  170. endfunction
  171. " Function: #session_save {{{1
  172. function! startify#session_save(bang, ...) abort
  173. if !isdirectory(s:session_dir)
  174. if exists('*mkdir')
  175. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  176. if (nr2char(getchar()) == 'y')
  177. call mkdir(s:session_dir, 'p')
  178. else
  179. echo
  180. return
  181. endif
  182. else
  183. echo 'The session directory does not exist: '. s:session_dir
  184. return
  185. endif
  186. endif
  187. call inputsave()
  188. let vsession = fnamemodify(v:this_session, ':t')
  189. if vsession ==# '__LAST__'
  190. let vsession = ''
  191. endif
  192. let sname = exists('a:1')
  193. \ ? a:1
  194. \ : input('Save under this session name: ', vsession, 'custom,startify#session_list_as_string')
  195. \ | redraw
  196. call inputrestore()
  197. if empty(sname)
  198. echo 'You gave an empty name!'
  199. return
  200. endif
  201. let spath = s:session_dir . s:sep . sname
  202. if !filereadable(spath)
  203. call startify#session_write(fnameescape(spath))
  204. echo 'Session saved under: '. spath
  205. return
  206. endif
  207. echo 'Session already exists. Overwrite? [y/n]' | redraw
  208. if a:bang || nr2char(getchar()) == 'y'
  209. call startify#session_write(fnameescape(spath))
  210. echo 'Session saved under: '. spath
  211. else
  212. echo 'Did NOT save the session!'
  213. endif
  214. endfunction
  215. " Function: #session_close {{{1
  216. function! startify#session_close() abort
  217. if exists('v:this_session') && filewritable(v:this_session)
  218. call startify#session_write(fnameescape(v:this_session))
  219. let v:this_session = ''
  220. endif
  221. call startify#session_delete_buffers()
  222. Startify
  223. endfunction
  224. " Function: #session_write {{{1
  225. function! startify#session_write(spath)
  226. " preserve existing variables from savevars
  227. if exists('g:startify_session_savevars')
  228. let savevars = map(filter(copy(g:startify_session_savevars), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))')
  229. endif
  230. " if this function is called while being in the Startify buffer
  231. " (by loading another session or running :SSave/:SLoad directly)
  232. " switch back to the previous buffer before saving the session
  233. if &filetype == 'startify'
  234. let callingbuffer = bufnr('#')
  235. if callingbuffer > 0
  236. execute 'buffer' callingbuffer
  237. endif
  238. endif
  239. " prevent saving already deleted buffers that were in the arglist
  240. for arg in argv()
  241. if !buflisted(arg)
  242. execute 'silent! argdelete' fnameescape(arg)
  243. endif
  244. endfor
  245. " clean up session before saving it
  246. for cmd in get(g:, 'startify_session_before_save', [])
  247. execute cmd
  248. endfor
  249. let ssop = &sessionoptions
  250. set sessionoptions-=options
  251. try
  252. execute 'mksession!' a:spath
  253. catch
  254. echohl ErrorMsg
  255. echomsg v:exception
  256. echohl NONE
  257. return
  258. finally
  259. let &sessionoptions = ssop
  260. endtry
  261. if exists('g:startify_session_remove_lines')
  262. \ || exists('g:startify_session_savevars')
  263. \ || exists('g:startify_session_savecmds')
  264. silent execute 'split' a:spath
  265. " remove lines from the session file
  266. if exists('g:startify_session_remove_lines')
  267. for pattern in g:startify_session_remove_lines
  268. execute 'silent global/'. pattern .'/delete _'
  269. endfor
  270. endif
  271. " put variables from savevars into session file
  272. if exists('savevars') && !empty(savevars)
  273. call append(line('$')-3, savevars)
  274. endif
  275. " put commands from savecmds into session file
  276. if exists('g:startify_session_savecmds')
  277. call append(line('$')-3, g:startify_session_savecmds)
  278. endif
  279. setlocal bufhidden=delete
  280. silent update
  281. silent hide
  282. endif
  283. call s:create_last_session_link(a:spath)
  284. endfunction
  285. " Function: #session_delete {{{1
  286. function! startify#session_delete(bang, ...) abort
  287. if !isdirectory(s:session_dir)
  288. echo 'The session directory does not exist: '. s:session_dir
  289. return
  290. elseif empty(startify#session_list_as_string(''))
  291. echo 'There are no sessions...'
  292. return
  293. endif
  294. call inputsave()
  295. let spath = s:session_dir . s:sep . (exists('a:1')
  296. \ ? a:1
  297. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  298. \ | redraw
  299. call inputrestore()
  300. if !filereadable(spath)
  301. echomsg 'No such session: '. spath
  302. return
  303. endif
  304. echo 'Really delete '. spath .'? [y/n]' | redraw
  305. if a:bang || nr2char(getchar()) == 'y'
  306. if delete(spath) == 0
  307. echo 'Deleted session '. spath .'!'
  308. else
  309. echo 'Deletion failed!'
  310. endif
  311. else
  312. echo 'Deletion aborted!'
  313. endif
  314. endfunction
  315. " Function: #session_delete_buffers {{{1
  316. function! startify#session_delete_buffers()
  317. if get(g:, 'startify_session_delete_buffers', 1)
  318. silent! %bdelete
  319. endif
  320. endfunction
  321. " Function: #session_list {{{1
  322. function! startify#session_list(lead, ...) abort
  323. return filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"')
  324. endfunction
  325. " Function: #session_list_as_string {{{1
  326. function! startify#session_list_as_string(lead, ...) abort
  327. return join(filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"'), "\n")
  328. endfunction
  329. " Function: #debug {{{1
  330. function! startify#debug()
  331. if exists('b:startify.entries')
  332. for k in sort(keys(b:startify.entries))
  333. echomsg '['. k .'] = '. string(b:startify.entries[k])
  334. endfor
  335. else
  336. call s:warn('This is no Startify buffer!')
  337. endif
  338. endfunction
  339. " Function: #open_buffers {{{1
  340. function! startify#open_buffers(...) abort
  341. if exists('a:1') " used in mappings
  342. call s:open_buffer(b:startify.entries[a:1])
  343. return
  344. endif
  345. let marked = filter(copy(b:startify.entries), 'v:val.marked')
  346. if empty(marked) " open current entry
  347. call s:open_buffer(b:startify.entries[line('.')])
  348. return
  349. endif
  350. enew
  351. setlocal nobuflisted
  352. " Open all marked entries.
  353. for entry in sort(values(marked), 's:sort_by_tick')
  354. call s:open_buffer(entry)
  355. endfor
  356. wincmd =
  357. if exists('#User#StartifyAllBuffersOpened')
  358. autocmd <nomodeline> User StartifyAllBuffersOpened
  359. endif
  360. endfunction
  361. " Function: s:get_lists {{{1
  362. function! s:get_lists() abort
  363. if exists('g:startify_lists')
  364. return g:startify_lists
  365. elseif exists('g:startify_list_order')
  366. " Convert old g:startify_list_order format to newer g:startify_lists format.
  367. let lists = []
  368. for item in g:startify_list_order
  369. if type(item) == type([])
  370. let header = item
  371. else
  372. if exists('header')
  373. let lists += [{ 'type': item, 'header': header }]
  374. unlet header
  375. else
  376. let lists += [{ 'type': item }]
  377. endif
  378. endif
  379. unlet item
  380. endfor
  381. return lists
  382. else
  383. return [
  384. \ { 'header': [s:padding_left .'MRU'], 'type': 'files' },
  385. \ { 'header': [s:padding_left .'MRU '. getcwd()], 'type': 'dir' },
  386. \ { 'header': [s:padding_left .'Sessions'], 'type': 'sessions' },
  387. \ { 'header': [s:padding_left .'Bookmarks'], 'type': 'bookmarks' },
  388. \ { 'header': [s:padding_left .'Commands'], 'type': 'commands' },
  389. \ ]
  390. endif
  391. endfunction
  392. " Function: s:show_lists {{{1
  393. function! s:show_lists(lists) abort
  394. for list in a:lists
  395. if !has_key(list, 'type')
  396. continue
  397. endif
  398. let b:startify.indices = copy(get(list, 'indices', []))
  399. if type(list.type) == type('')
  400. if has_key(list, 'header')
  401. let s:last_message = list.header
  402. endif
  403. call s:show_{list.type}()
  404. elseif type(list.type) == type(function('tr'))
  405. try
  406. let entries = list.type()
  407. catch
  408. call s:warn(v:exception)
  409. continue
  410. endtry
  411. if empty(entries)
  412. unlet! s:last_message
  413. continue
  414. endif
  415. if has_key(list, 'header')
  416. let s:last_message = list.header
  417. call s:print_section_header()
  418. endif
  419. for entry in entries
  420. let cmd = get(entry, 'cmd', 'edit')
  421. let path = get(entry, 'path', '')
  422. let type = get(entry, 'type', empty(path) ? 'special' : 'file')
  423. let index = s:get_index_as_string()
  424. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry.line)
  425. call s:register(line('$'), index, type, cmd, path)
  426. endfor
  427. call append('$', '')
  428. else
  429. call s:warn('Wrong format for g:startify_lists: '. string(list))
  430. endif
  431. endfor
  432. endfunction
  433. " Function: s:open_buffer {{{1
  434. function! s:open_buffer(entry)
  435. if a:entry.type == 'special'
  436. execute a:entry.cmd
  437. elseif a:entry.type == 'session'
  438. execute a:entry.cmd a:entry.path
  439. elseif a:entry.type == 'file'
  440. if line2byte('$') == -1
  441. execute 'edit' a:entry.path
  442. else
  443. if a:entry.cmd == 'tabnew'
  444. wincmd =
  445. endif
  446. execute a:entry.cmd a:entry.path
  447. endif
  448. call s:check_user_options(a:entry.path)
  449. endif
  450. if exists('#User#StartifyBufferOpened')
  451. autocmd <nomodeline> User StartifyBufferOpened
  452. endif
  453. endfunction
  454. " Function: s:set_custom_section {{{1
  455. function! s:set_custom_section(section) abort
  456. if type(a:section) == type([])
  457. return copy(a:section)
  458. elseif type(a:section) == type('')
  459. return empty(a:section) ? [] : eval(a:section)
  460. endif
  461. return []
  462. endfunction
  463. " Function: s:display_by_path {{{1
  464. function! s:display_by_path(path_prefix, path_format, use_env) abort
  465. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  466. \ [a:path_prefix, a:path_format, a:use_env])
  467. let entry_format = "s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) ."
  468. if exists('*StartifyEntryFormat')
  469. let entry_format .= StartifyEntryFormat()
  470. else
  471. let entry_format .= 'entry_path'
  472. endif
  473. if !empty(oldfiles)
  474. if exists('s:last_message')
  475. call s:print_section_header()
  476. endif
  477. for [absolute_path, entry_path] in oldfiles
  478. let index = s:get_index_as_string()
  479. call append('$', eval(entry_format))
  480. if has('win32')
  481. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  482. endif
  483. call s:register(line('$'), index, 'file', 'edit', absolute_path)
  484. endfor
  485. call append('$', '')
  486. endif
  487. endfunction
  488. " Function: s:filter_oldfiles {{{1
  489. function! s:filter_oldfiles(path_prefix, path_format, use_env) abort
  490. let path_prefix = '\V'. escape(a:path_prefix, '\')
  491. let counter = s:numfiles
  492. let entries = {}
  493. let oldfiles = []
  494. for fname in v:oldfiles
  495. if counter <= 0
  496. break
  497. endif
  498. let absolute_path = fnamemodify(resolve(fname), ":p")
  499. " filter duplicates, bookmarks and entries from the skiplist
  500. if has_key(entries, absolute_path)
  501. \ || !filereadable(absolute_path)
  502. \ || s:is_in_skiplist(absolute_path)
  503. \ || match(absolute_path, path_prefix)
  504. continue
  505. endif
  506. let entry_path = ''
  507. if s:tf
  508. let entry_path = s:transform(absolute_path)
  509. endif
  510. if empty(entry_path)
  511. let entry_path = fnamemodify(absolute_path, a:path_format)
  512. endif
  513. let entries[absolute_path] = 1
  514. let counter -= 1
  515. if !has('win32')
  516. let absolute_path = fnameescape(absolute_path)
  517. endif
  518. let oldfiles += [[absolute_path, entry_path]]
  519. endfor
  520. if a:use_env
  521. call s:init_env()
  522. for i in range(len(oldfiles))
  523. for [k,v] in s:env
  524. let p = oldfiles[i][0]
  525. if !stridx(tolower(p), tolower(v))
  526. let oldfiles[i][1] = printf('$%s%s', k, p[len(v):])
  527. break
  528. endif
  529. endfor
  530. endfor
  531. endif
  532. return oldfiles
  533. endfun
  534. " Function: s:filter_oldfiles_unsafe {{{1
  535. function! s:filter_oldfiles_unsafe(path_prefix, path_format, use_env) abort
  536. let path_prefix = '\V'. escape(a:path_prefix, '\')
  537. let counter = s:numfiles
  538. let entries = {}
  539. let oldfiles = []
  540. let is_dir = escape(s:sep, '\') . '$'
  541. for fname in v:oldfiles
  542. if counter <= 0
  543. break
  544. endif
  545. let absolute_path = glob(fnamemodify(fname, ":p"))
  546. if empty(absolute_path)
  547. \ || has_key(entries, absolute_path)
  548. \ || (absolute_path =~ is_dir)
  549. \ || match(absolute_path, path_prefix)
  550. \ || s:is_in_skiplist(absolute_path)
  551. continue
  552. endif
  553. let entry_path = fnamemodify(absolute_path, a:path_format)
  554. let entries[absolute_path] = 1
  555. let counter -= 1
  556. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  557. endfor
  558. return oldfiles
  559. endfun
  560. " Function: s:show_dir {{{1
  561. function! s:show_dir() abort
  562. return s:display_by_path(getcwd() . s:sep, ':.', 0)
  563. endfunction
  564. " Function: s:show_files {{{1
  565. function! s:show_files() abort
  566. return s:display_by_path('', s:relative_path, get(g:, 'startify_use_env'))
  567. endfunction
  568. " Function: s:show_sessions {{{1
  569. function! s:show_sessions() abort
  570. let limit = get(g:, 'startify_session_number', 999) - 1
  571. if limit <= -1
  572. return
  573. endif
  574. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  575. let sfiles = filter(sfiles, 'v:val !~# "__LAST__$"')
  576. let sfiles = filter(sfiles,
  577. \ '!(v:val =~# "x\.vim$" && index(sfiles, v:val[:-6].".vim") >= 0)')
  578. if empty(sfiles)
  579. if exists('s:last_message')
  580. unlet s:last_message
  581. endif
  582. return
  583. endif
  584. if exists('s:last_message')
  585. call s:print_section_header()
  586. endif
  587. if get(g:, 'startify_session_sort')
  588. function! s:sort_by_mtime(foo, bar)
  589. let foo = getftime(a:foo)
  590. let bar = getftime(a:bar)
  591. return foo == bar ? 0 : (foo < bar ? 1 : -1)
  592. endfunction
  593. call sort(sfiles, 's:sort_by_mtime')
  594. endif
  595. for i in range(len(sfiles))
  596. let index = s:get_index_as_string()
  597. let fname = fnamemodify(sfiles[i], ':t')
  598. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  599. if has('win32')
  600. let fname = substitute(fname, '\[', '\[[]', 'g')
  601. endif
  602. call s:register(line('$'), index, 'session', 'SLoad', fname)
  603. if i == limit
  604. break
  605. endif
  606. endfor
  607. call append('$', '')
  608. endfunction
  609. " Function: s:show_bookmarks {{{1
  610. function! s:show_bookmarks() abort
  611. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  612. return
  613. endif
  614. if exists('s:last_message')
  615. call s:print_section_header()
  616. endif
  617. for bookmark in g:startify_bookmarks
  618. if type(bookmark) == type({})
  619. let [index, path] = items(bookmark)[0]
  620. else " string
  621. let [index, path] = [s:get_index_as_string(), bookmark]
  622. endif
  623. let entry_path = ''
  624. if s:tf
  625. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  626. endif
  627. if empty(entry_path)
  628. let entry_path = path
  629. endif
  630. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  631. if has('win32')
  632. let path = substitute(path, '\[', '\[[]', 'g')
  633. endif
  634. call s:register(line('$'), index, 'file', 'edit', fnameescape(expand(path)))
  635. unlet bookmark " avoid type mismatch for heterogeneous lists
  636. endfor
  637. call append('$', '')
  638. endfunction
  639. " Function: s:show_commands {{{1
  640. function! s:show_commands() abort
  641. if !exists('g:startify_commands') || empty(g:startify_commands)
  642. return
  643. endif
  644. if exists('s:last_message')
  645. call s:print_section_header()
  646. endif
  647. for entry in g:startify_commands
  648. if type(entry) == type({}) " with custom index
  649. let [index, command] = items(entry)[0]
  650. else
  651. let command = entry
  652. let index = s:get_index_as_string()
  653. endif
  654. " If no list is given, the description is the command itself.
  655. let [desc, cmd] = type(command) == type([]) ? command : [command, command]
  656. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . desc)
  657. call s:register(line('$'), index, 'special', cmd, '')
  658. unlet entry command
  659. endfor
  660. call append('$', '')
  661. endfunction
  662. " Function: s:is_in_skiplist {{{1
  663. function! s:is_in_skiplist(arg) abort
  664. for regexp in s:skiplist
  665. try
  666. if a:arg =~# regexp
  667. return 1
  668. endif
  669. catch
  670. call s:warn('Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist')
  671. endtry
  672. endfor
  673. endfunction
  674. " Function: s:set_cursor {{{1
  675. function! s:set_cursor() abort
  676. let b:startify.oldline = exists('b:startify.newline') ? b:startify.newline : 2 + len(s:padding_left)
  677. let b:startify.newline = line('.')
  678. " going up (-1) or down (1)
  679. if b:startify.oldline == b:startify.newline && col('.') != s:fixed_column
  680. let movement = 2 * (col('.') > s:fixed_column) - 1
  681. let b:startify.newline += movement
  682. else
  683. let movement = 2 * (b:startify.newline > b:startify.oldline) - 1
  684. endif
  685. " skip section headers lines until an entry is found
  686. while index(b:startify.section_header_lines, b:startify.newline) != -1
  687. let b:startify.newline += movement
  688. endwhile
  689. " skip blank lines between lists
  690. if empty(getline(b:startify.newline))
  691. let b:startify.newline += movement
  692. endif
  693. " don't go beyond first or last entry
  694. let b:startify.newline = max([b:startify.firstline, min([b:startify.lastline, b:startify.newline])])
  695. call cursor(b:startify.newline, s:fixed_column)
  696. endfunction
  697. " Function: s:set_mappings {{{1
  698. function! s:set_mappings() abort
  699. nnoremap <buffer><nowait><silent> i :enew <bar> startinsert<cr>
  700. nnoremap <buffer><nowait><silent> <insert> :enew <bar> startinsert<cr>
  701. nnoremap <buffer><nowait><silent> b :call <sid>set_mark('B')<cr>
  702. nnoremap <buffer><nowait><silent> s :call <sid>set_mark('S')<cr>
  703. nnoremap <buffer><nowait><silent> t :call <sid>set_mark('T')<cr>
  704. nnoremap <buffer><nowait><silent> v :call <sid>set_mark('V')<cr>
  705. nnoremap <buffer><nowait><silent> <cr> :call startify#open_buffers()<cr>
  706. nnoremap <buffer><nowait><silent> <2-LeftMouse> :call startify#open_buffers()<cr>
  707. nnoremap <buffer><nowait><silent> <MiddleMouse> :enew <bar> execute 'normal! "'.(v:register=='"'?'*':v:register).'gp'<cr>
  708. " Without these mappings n/N wouldn't work properly, since autocmds always
  709. " force the cursor back on the index.
  710. nnoremap <buffer><expr> n ' j'[v:searchforward].'n'
  711. nnoremap <buffer><expr> N 'j '[v:searchforward].'N'
  712. function! s:compare_by_index(foo, bar)
  713. return a:foo.index - a:bar.index
  714. endfunction
  715. for entry in sort(values(b:startify.entries), 's:compare_by_index')
  716. execute 'nnoremap <buffer><silent><nowait>' entry.index
  717. \ ':call startify#open_buffers('. string(entry.line) .')<cr>'
  718. endfor
  719. endfunction
  720. " Function: s:set_mark {{{1
  721. function! s:set_mark(type, ...) abort
  722. let index = expand('<cword>')
  723. let line = exists('a:1') ? a:1 : line('.')
  724. let entry = b:startify.entries[line]
  725. if entry.type != 'file'
  726. return
  727. endif
  728. let default_cmds = {
  729. \ 'B': 'edit',
  730. \ 'S': 'split',
  731. \ 'V': 'vsplit',
  732. \ 'T': 'tabnew',
  733. \ }
  734. setlocal modifiable
  735. if entry.marked && index[0] == a:type
  736. let entry.cmd = 'edit'
  737. let entry.marked = 0
  738. execute 'normal! ci]'. entry.index
  739. else
  740. let entry.cmd = default_cmds[a:type]
  741. let entry.marked = 1
  742. let entry.tick = b:startify.tick
  743. let b:startify.tick += 1
  744. execute 'normal! ci]'. repeat(a:type, len(index))
  745. endif
  746. " Reset cursor to fixed column, which is important for s:set_cursor().
  747. call cursor(line('.'), s:fixed_column)
  748. setlocal nomodifiable nomodified
  749. endfunction
  750. " Function: s:sort_by_tick {{{1
  751. function! s:sort_by_tick(one, two)
  752. return a:one.tick - a:two.tick
  753. endfunction
  754. " Function: s:check_user_options {{{1
  755. function! s:check_user_options(path) abort
  756. let session = a:path . s:sep .'Session.vim'
  757. if get(g:, 'startify_session_autoload') && filereadable(glob(session))
  758. execute 'silent bwipeout' a:path
  759. call startify#session_delete_buffers()
  760. execute 'source' session
  761. elseif get(g:, 'startify_change_to_vcs_root')
  762. call s:cd_to_vcs_root(a:path)
  763. elseif get(g:, 'startify_change_to_dir', 1)
  764. if isdirectory(a:path)
  765. execute 'lcd' a:path
  766. else
  767. let dir = fnamemodify(a:path, ':h')
  768. if isdirectory(dir)
  769. execute 'lcd' dir
  770. else
  771. " Do nothing. E.g. a:path == `scp://foo/bar`
  772. endif
  773. endif
  774. endif
  775. endfunction
  776. " Function: s:cd_to_vcs_root {{{1
  777. function! s:cd_to_vcs_root(path) abort
  778. let dir = fnamemodify(a:path, ':p:h')
  779. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  780. let root = finddir(vcs, dir .';')
  781. if !empty(root)
  782. execute 'cd '. fnameescape(fnamemodify(root, ':h'))
  783. return
  784. endif
  785. endfor
  786. endfunction
  787. " Function: s:close {{{1
  788. function! s:close() abort
  789. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  790. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  791. buffer #
  792. else
  793. bnext
  794. endif
  795. else
  796. quit
  797. endif
  798. endfunction
  799. " Function: s:get_index_as_string {{{1
  800. function! s:get_index_as_string() abort
  801. if !empty(b:startify.indices)
  802. return remove(b:startify.indices, 0)
  803. elseif exists('g:startify_custom_indices')
  804. let listlen = len(g:startify_custom_indices)
  805. if b:startify.entry_number < listlen
  806. let idx = g:startify_custom_indices[b:startify.entry_number]
  807. else
  808. let idx = string(b:startify.entry_number - listlen)
  809. endif
  810. else
  811. let idx = string(b:startify.entry_number)
  812. endif
  813. let b:startify.entry_number += 1
  814. return idx
  815. endfunction
  816. " Function: s:print_section_header {{{1
  817. function! s:print_section_header() abort
  818. $
  819. let curline = line('.')
  820. for lnum in range(curline, curline + len(s:last_message) + 1)
  821. call add(b:startify.section_header_lines, lnum)
  822. endfor
  823. call append('$', s:last_message + [''])
  824. unlet s:last_message
  825. endfunction
  826. " Function: s:register {{{1
  827. function! s:register(line, index, type, cmd, path)
  828. let b:startify.entries[a:line] = {
  829. \ 'index': a:index,
  830. \ 'type': a:type,
  831. \ 'line': a:line,
  832. \ 'cmd': a:cmd,
  833. \ 'path': a:path,
  834. \ 'marked': 0,
  835. \ }
  836. endfunction
  837. " Function: s:create_last_session_link {{{1
  838. function! s:create_last_session_link(spath)
  839. if !has('win32') && a:spath !~# '__LAST__$'
  840. let cmd = printf('ln -sf %s %s',
  841. \ shellescape(fnamemodify(a:spath, ':t')),
  842. \ shellescape(s:session_dir .'/__LAST__'))
  843. call system(cmd)
  844. if v:shell_error
  845. call s:warn("Can't create 'last used session' symlink.")
  846. endif
  847. endif
  848. endfunction
  849. " Function: s:init_env {{{1
  850. function! s:init_env()
  851. let s:env = []
  852. let ignore = {
  853. \ 'HOME': 1,
  854. \ 'OLDPWD': 1,
  855. \ 'PWD': 1,
  856. \ }
  857. function! s:get_env()
  858. redir => s
  859. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  860. redir END
  861. redraw
  862. return split(s)
  863. endfunction
  864. function! s:compare_by_key_len(foo, bar)
  865. return len(a:foo[0]) - len(a:bar[0])
  866. endfunction
  867. function! s:compare_by_val_len(foo, bar)
  868. return len(a:bar[1]) - len(a:foo[1])
  869. endfunction
  870. for k in s:get_env()
  871. silent! execute "let v = eval('$'.k)"
  872. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  873. \ || has_key(ignore, k)
  874. \ || len(k) > len(v)
  875. continue
  876. endif
  877. call insert(s:env, [k,v], 0)
  878. endfor
  879. let s:env = sort(s:env, 's:compare_by_key_len')
  880. let s:env = sort(s:env, 's:compare_by_val_len')
  881. endfunction
  882. " Function: s:transform {{{1
  883. function s:transform(absolute_path)
  884. for [k,V] in g:startify_transformations
  885. if a:absolute_path =~ k
  886. return type(V) == type('') ? V : V(a:absolute_path)
  887. endif
  888. unlet V
  889. endfor
  890. return ''
  891. endfunction
  892. " Function: s:warn {{{1
  893. function! s:warn(msg) abort
  894. echohl WarningMsg
  895. echomsg 'startify: '. a:msg
  896. echohl NONE
  897. endfunction