startify.vim 29 KB

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