startify.vim 26 KB

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