startify.vim 29 KB

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