startify.vim 28 KB

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