startify.vim 32 KB

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