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