startify.vim 29 KB

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