startify.vim 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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('*WebDevIconsGetFileTypeSymbol') && get(g:, 'webdevicons_enable')
  400. " support for vim-devicons
  401. let entry_format .= ". WebDevIconsGetFileTypeSymbol(entry_path) .' '. entry_path"
  402. else
  403. let entry_format .= '. entry_path'
  404. endif
  405. if !empty(oldfiles)
  406. if exists('s:last_message')
  407. call s:print_section_header()
  408. endif
  409. for [absolute_path, entry_path] in oldfiles
  410. let index = s:get_index_as_string(b:startify.entry_number)
  411. call append('$', eval(entry_format))
  412. if has('win32')
  413. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  414. endif
  415. call s:register(line('$'), index, 'file', 'edit', absolute_path)
  416. let b:startify.entry_number += 1
  417. endfor
  418. call append('$', '')
  419. endif
  420. endfunction
  421. " Function: s:filter_oldfiles {{{1
  422. function! s:filter_oldfiles(path_prefix, path_format, use_env) abort
  423. let path_prefix = '\V'. escape(a:path_prefix, '\')
  424. let counter = s:numfiles
  425. let entries = {}
  426. let oldfiles = []
  427. for fname in v:oldfiles
  428. if counter <= 0
  429. break
  430. endif
  431. let absolute_path = fnamemodify(resolve(fname), ":p")
  432. " filter duplicates, bookmarks and entries from the skiplist
  433. if has_key(entries, absolute_path)
  434. \ || !filereadable(absolute_path)
  435. \ || s:is_in_skiplist(absolute_path)
  436. \ || match(absolute_path, path_prefix)
  437. continue
  438. endif
  439. let entry_path = ''
  440. if s:tf
  441. let entry_path = s:transform(absolute_path)
  442. endif
  443. if empty(entry_path)
  444. let entry_path = fnamemodify(absolute_path, a:path_format)
  445. endif
  446. let entries[absolute_path] = 1
  447. let counter -= 1
  448. if !has('win32')
  449. let absolute_path = fnameescape(absolute_path)
  450. endif
  451. let oldfiles += [[absolute_path, entry_path]]
  452. endfor
  453. if a:use_env
  454. call s:init_env()
  455. for i in range(len(oldfiles))
  456. for [k,v] in s:env
  457. let p = oldfiles[i][0]
  458. if !stridx(tolower(p), tolower(v))
  459. let oldfiles[i][1] = printf('$%s%s', k, p[len(v):])
  460. break
  461. endif
  462. endfor
  463. endfor
  464. endif
  465. return oldfiles
  466. endfun
  467. " Function: s:filter_oldfiles_unsafe {{{1
  468. function! s:filter_oldfiles_unsafe(path_prefix, path_format, use_env) abort
  469. let path_prefix = '\V'. escape(a:path_prefix, '\')
  470. let counter = s:numfiles
  471. let entries = {}
  472. let oldfiles = []
  473. let is_dir = escape(s:sep, '\') . '$'
  474. for fname in v:oldfiles
  475. if counter <= 0
  476. break
  477. endif
  478. let absolute_path = glob(fnamemodify(fname, ":p"))
  479. if empty(absolute_path)
  480. \ || has_key(entries, absolute_path)
  481. \ || (absolute_path =~ is_dir)
  482. \ || match(absolute_path, path_prefix)
  483. \ || s:is_in_skiplist(absolute_path)
  484. continue
  485. endif
  486. let entry_path = fnamemodify(absolute_path, a:path_format)
  487. let entries[absolute_path] = 1
  488. let counter -= 1
  489. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  490. endfor
  491. return oldfiles
  492. endfun
  493. " Function: s:show_dir {{{1
  494. function! s:show_dir() abort
  495. return s:display_by_path(getcwd() . s:sep, ':.', 0)
  496. endfunction
  497. " Function: s:show_files {{{1
  498. function! s:show_files() abort
  499. return s:display_by_path('', s:relative_path, get(g:, 'startify_use_env'))
  500. endfunction
  501. " Function: s:show_sessions {{{1
  502. function! s:show_sessions() abort
  503. let limit = get(g:, 'startify_session_number', 999) - 1
  504. if limit <= -1
  505. return
  506. endif
  507. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  508. let sfiles = filter(sfiles, 'v:val !~# "__LAST__$"')
  509. let sfiles = filter(sfiles,
  510. \ '!(v:val =~# "x\.vim$" && index(sfiles, v:val[:-6].".vim") >= 0)')
  511. if empty(sfiles)
  512. if exists('s:last_message')
  513. unlet s:last_message
  514. endif
  515. return
  516. endif
  517. if exists('s:last_message')
  518. call s:print_section_header()
  519. endif
  520. if get(g:, 'startify_session_sort')
  521. function! s:sort_by_mtime(foo, bar)
  522. let foo = getftime(a:foo)
  523. let bar = getftime(a:bar)
  524. return foo == bar ? 0 : (foo < bar ? 1 : -1)
  525. endfunction
  526. call sort(sfiles, 's:sort_by_mtime')
  527. endif
  528. for i in range(len(sfiles))
  529. let index = s:get_index_as_string(b:startify.entry_number)
  530. let fname = fnamemodify(sfiles[i], ':t')
  531. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  532. if has('win32')
  533. let fname = substitute(fname, '\[', '\[[]', 'g')
  534. endif
  535. call s:register(line('$'), index, 'session', 'SLoad', fname)
  536. let b:startify.entry_number += 1
  537. if i == limit
  538. break
  539. endif
  540. endfor
  541. call append('$', '')
  542. endfunction
  543. " Function: s:show_bookmarks {{{1
  544. function! s:show_bookmarks() abort
  545. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  546. return
  547. endif
  548. if exists('s:last_message')
  549. call s:print_section_header()
  550. endif
  551. for bookmark in g:startify_bookmarks
  552. if type(bookmark) == type({})
  553. let [index, path] = items(bookmark)[0]
  554. else " string
  555. let [index, path] = [s:get_index_as_string(b:startify.entry_number), bookmark]
  556. let b:startify.entry_number += 1
  557. endif
  558. let entry_path = ''
  559. if s:tf
  560. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  561. endif
  562. if empty(entry_path)
  563. let entry_path = path
  564. endif
  565. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  566. if has('win32')
  567. let path = substitute(path, '\[', '\[[]', 'g')
  568. endif
  569. call s:register(line('$'), index, 'file', 'edit', fnameescape(expand(path)))
  570. unlet bookmark " avoid type mismatch for heterogeneous lists
  571. endfor
  572. call append('$', '')
  573. endfunction
  574. " Function: s:show_commands {{{1
  575. function! s:show_commands() abort
  576. if !exists('g:startify_commands') || empty(g:startify_commands)
  577. return
  578. endif
  579. if exists('s:last_message')
  580. call s:print_section_header()
  581. endif
  582. for entry in g:startify_commands
  583. if type(entry) == type({}) " with custom index
  584. let [index, command] = items(entry)[0]
  585. else
  586. let command = entry
  587. let index = s:get_index_as_string(b:startify.entry_number)
  588. let b:startify.entry_number += 1
  589. endif
  590. " If no list is given, the description is the command itself.
  591. let [desc, cmd] = type(command) == type([]) ? command : [command, command]
  592. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . desc)
  593. call s:register(line('$'), index, 'special', cmd, '')
  594. unlet entry command
  595. endfor
  596. call append('$', '')
  597. endfunction
  598. " Function: s:is_in_skiplist {{{1
  599. function! s:is_in_skiplist(arg) abort
  600. for regexp in s:skiplist
  601. try
  602. if a:arg =~# regexp
  603. return 1
  604. endif
  605. catch
  606. call s:warn('startify: Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist')
  607. endtry
  608. endfor
  609. endfunction
  610. " Function: s:set_cursor {{{1
  611. function! s:set_cursor() abort
  612. let b:startify.oldline = exists('b:startify.newline') ? b:startify.newline : 2 + len(s:padding_left)
  613. let b:startify.newline = line('.')
  614. " going up (-1) or down (1)
  615. if b:startify.oldline == b:startify.newline && col('.') != s:fixed_column
  616. let movement = 2 * (col('.') > s:fixed_column) - 1
  617. let b:startify.newline += movement
  618. else
  619. let movement = 2 * (b:startify.newline > b:startify.oldline) - 1
  620. endif
  621. " skip section headers lines until an entry is found
  622. while index(b:startify.section_header_lines, b:startify.newline) != -1
  623. let b:startify.newline += movement
  624. endwhile
  625. " skip blank lines between lists
  626. if empty(getline(b:startify.newline))
  627. let b:startify.newline += movement
  628. endif
  629. " don't go beyond first or last entry
  630. let b:startify.newline = max([b:startify.firstline, min([b:startify.lastline, b:startify.newline])])
  631. call cursor(b:startify.newline, s:fixed_column)
  632. endfunction
  633. " Function: s:set_mappings {{{1
  634. function! s:set_mappings() abort
  635. execute "nnoremap <buffer>". s:nowait ."<silent> i :enew <bar> startinsert<cr>"
  636. execute "nnoremap <buffer>". s:nowait ."<silent> <insert> :enew <bar> startinsert<cr>"
  637. execute "nnoremap <buffer>". s:nowait ."<silent> b :call <sid>set_mark('B')<cr>"
  638. execute "nnoremap <buffer>". s:nowait ."<silent> s :call <sid>set_mark('S')<cr>"
  639. execute "nnoremap <buffer>". s:nowait ."<silent> t :call <sid>set_mark('T')<cr>"
  640. execute "nnoremap <buffer>". s:nowait ."<silent> v :call <sid>set_mark('V')<cr>"
  641. execute "nnoremap <buffer>". s:nowait ."<silent> <cr> :call startify#open_buffers()<cr>"
  642. execute "nnoremap <buffer>". s:nowait ."<silent> <2-LeftMouse> :call startify#open_buffers()<cr>"
  643. execute "nnoremap <buffer>". s:nowait ."<silent> <MiddleMouse> :enew <bar> execute 'normal! \"'.(v:register=='\"'?'*':v:register).'gp'<cr>"
  644. " Without these mappings n/N wouldn't work properly, since autocmds always
  645. " force the cursor back on the index.
  646. nnoremap <buffer><expr> n ' j'[v:searchforward].'n'
  647. nnoremap <buffer><expr> N 'j '[v:searchforward].'N'
  648. function! s:compare_by_index(foo, bar)
  649. return a:foo.index - a:bar.index
  650. endfunction
  651. for entry in sort(values(b:startify.entries), 's:compare_by_index')
  652. execute 'nnoremap <buffer><silent>'. s:nowait entry.index
  653. \ ':call startify#open_buffers('. string(entry.line) .')<cr>'
  654. endfor
  655. endfunction
  656. " Function: s:set_mark {{{1
  657. function! s:set_mark(type, ...) abort
  658. let index = expand('<cword>')
  659. let line = exists('a:1') ? a:1 : line('.')
  660. let entry = b:startify.entries[line]
  661. if entry.type != 'file'
  662. return
  663. endif
  664. let default_cmds = {
  665. \ 'B': 'edit',
  666. \ 'S': 'split',
  667. \ 'V': 'vsplit',
  668. \ 'T': 'tabnew',
  669. \ }
  670. setlocal noreadonly modifiable
  671. if entry.marked && index[0] == a:type
  672. let entry.cmd = 'edit'
  673. let entry.marked = 0
  674. execute 'normal! ci]'. entry.index
  675. else
  676. let entry.cmd = default_cmds[a:type]
  677. let entry.marked = 1
  678. let entry.tick = b:startify.tick
  679. let b:startify.tick += 1
  680. execute 'normal! ci]'. repeat(a:type, len(index))
  681. endif
  682. " Reset cursor to fixed column, which is important for s:set_cursor().
  683. call cursor(line('.'), s:fixed_column)
  684. setlocal readonly nomodifiable nomodified
  685. endfunction
  686. " Function: s:sort_by_tick {{{1
  687. function! s:sort_by_tick(one, two)
  688. return a:one.tick - a:two.tick
  689. endfunction
  690. " Function: s:check_user_options {{{1
  691. function! s:check_user_options(path) abort
  692. let session = a:path . s:sep .'Session.vim'
  693. if get(g:, 'startify_session_autoload') && filereadable(glob(session))
  694. execute 'silent bwipeout' a:path
  695. call startify#session_delete_buffers()
  696. execute 'source' session
  697. elseif get(g:, 'startify_change_to_vcs_root')
  698. call s:cd_to_vcs_root(a:path)
  699. elseif get(g:, 'startify_change_to_dir', 1)
  700. if isdirectory(a:path)
  701. execute 'lcd' a:path
  702. else
  703. let dir = fnamemodify(a:path, ':h')
  704. if isdirectory(dir)
  705. execute 'lcd' dir
  706. else
  707. " Do nothing. E.g. a:path == `scp://foo/bar`
  708. endif
  709. endif
  710. endif
  711. endfunction
  712. " Function: s:cd_to_vcs_root {{{1
  713. function! s:cd_to_vcs_root(path) abort
  714. let dir = fnamemodify(a:path, ':p:h')
  715. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  716. let root = finddir(vcs, dir .';')
  717. if !empty(root)
  718. execute 'cd '. fnameescape(fnamemodify(root, ':h'))
  719. return
  720. endif
  721. endfor
  722. endfunction
  723. " Function: s:close {{{1
  724. function! s:close() abort
  725. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  726. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  727. buffer #
  728. else
  729. bnext
  730. endif
  731. else
  732. quit
  733. endif
  734. endfunction
  735. " Function: s:get_index_as_string {{{1
  736. function! s:get_index_as_string(idx) abort
  737. if exists('g:startify_custom_indices')
  738. let listlen = len(g:startify_custom_indices)
  739. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  740. else
  741. return string(a:idx)
  742. endif
  743. endfunction
  744. " Function: s:print_section_header {{{1
  745. function! s:print_section_header() abort
  746. $
  747. let curline = line('.')
  748. for lnum in range(curline, curline + len(s:last_message) + 1)
  749. call add(b:startify.section_header_lines, lnum)
  750. endfor
  751. call append('$', s:last_message + [''])
  752. unlet s:last_message
  753. endfunction
  754. " Function: s:register {{{1
  755. function! s:register(line, index, type, cmd, path)
  756. let b:startify.entries[a:line] = {
  757. \ 'index': a:index,
  758. \ 'type': a:type,
  759. \ 'line': a:line,
  760. \ 'cmd': a:cmd,
  761. \ 'path': a:path,
  762. \ 'marked': 0,
  763. \ }
  764. endfunction
  765. " Function: s:create_last_session_link {{{1
  766. function! s:create_last_session_link(spath)
  767. if !has('win32') && a:spath !~# '__LAST__$'
  768. let cmd = printf('ln -sf %s %s',
  769. \ shellescape(fnamemodify(a:spath, ':t')),
  770. \ shellescape(s:session_dir .'/__LAST__'))
  771. call system(cmd)
  772. if v:shell_error
  773. echomsg "startify: Can't create 'last used session' symlink."
  774. endif
  775. endif
  776. endfunction
  777. " Function: s:init_env {{{1
  778. function! s:init_env()
  779. let s:env = []
  780. let ignore = {
  781. \ 'HOME': 1,
  782. \ 'OLDPWD': 1,
  783. \ 'PWD': 1,
  784. \ }
  785. function! s:get_env()
  786. redir => s
  787. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  788. redir END
  789. redraw
  790. return split(s)
  791. endfunction
  792. function! s:compare_by_key_len(foo, bar)
  793. return len(a:foo[0]) - len(a:bar[0])
  794. endfunction
  795. function! s:compare_by_val_len(foo, bar)
  796. return len(a:bar[1]) - len(a:foo[1])
  797. endfunction
  798. for k in s:get_env()
  799. silent! execute "let v = eval('$'.k)"
  800. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  801. \ || has_key(ignore, k)
  802. \ || len(k) > len(v)
  803. continue
  804. endif
  805. call insert(s:env, [k,v], 0)
  806. endfor
  807. let s:env = sort(s:env, 's:compare_by_key_len')
  808. let s:env = sort(s:env, 's:compare_by_val_len')
  809. endfunction
  810. " Function: s:transform {{{1
  811. function s:transform(absolute_path)
  812. for [k,V] in g:startify_transformations
  813. if a:absolute_path =~ k
  814. return type(V) == type('') ? V : V(a:absolute_path)
  815. endif
  816. unlet V
  817. endfor
  818. return ''
  819. endfunction
  820. " Function: s:warn {{{1
  821. function! s:warn(msg) abort
  822. echohl WarningMsg
  823. echomsg a:msg
  824. echohl NONE
  825. endfunction