startify.vim 32 KB

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