startify.vim 23 KB

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