startify.vim 29 KB

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