startify.vim 25 KB

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