startify.vim 28 KB

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