startify.vim 26 KB

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