startify.vim 29 KB

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