startify.vim 29 KB

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