startify.vim 24 KB

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