startify.vim 27 KB

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