startify.vim 27 KB

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