startify.vim 28 KB

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