startify.vim 25 KB

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