startify.vim 29 KB

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