startify.vim 23 KB

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