startify.vim 29 KB

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