startify.vim 26 KB

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