startify.vim 33 KB

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