startify.vim 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. \ foldcolumn=0
  41. \ matchpairs=
  42. \ nobuflisted
  43. \ nocursorcolumn
  44. \ nocursorline
  45. \ nolist
  46. \ nonumber
  47. \ norelativenumber
  48. \ nospell
  49. \ noswapfile
  50. \ signcolumn=no
  51. if empty(&statusline)
  52. setlocal statusline=\ startify
  53. endif
  54. " Must be global so that it can be read by syntax/startify.vim.
  55. let g:startify_header = exists('g:startify_custom_header')
  56. \ ? s:set_custom_section(g:startify_custom_header)
  57. \ : startify#fortune#cowsay()
  58. if !empty(g:startify_header)
  59. let g:startify_header += [''] " add blank line
  60. endif
  61. call append('$', g:startify_header)
  62. let b:startify = { 'tick': 0, 'entries': {}, 'indices': [] }
  63. if s:show_special
  64. call append('$', [s:padding_left .'[e] <empty buffer>', ''])
  65. endif
  66. call s:register(line('$')-1, 'e', 'special', 'enew', '')
  67. let b:startify.entry_number = 0
  68. if filereadable('Session.vim')
  69. call append('$', [s:padding_left .'[0] '. getcwd() . s:sep .'Session.vim', ''])
  70. call s:register(line('$')-1, '0', 'session',
  71. \ 'call startify#session_delete_buffers() | source', 'Session.vim')
  72. let b:startify.entry_number = 1
  73. let l:show_session = 1
  74. endif
  75. if empty(v:oldfiles)
  76. call s:warn("Can't read viminfo file. Read :help startify-faq-02")
  77. endif
  78. let b:startify.section_header_lines = []
  79. let lists = s:get_lists()
  80. call s:show_lists(lists)
  81. silent $delete _
  82. if s:show_special
  83. call append('$', ['', s:padding_left .'[q] <quit>'])
  84. call s:register(line('$'), 'q', 'special', 'call s:close()', '')
  85. else
  86. " Don't overwrite the last regular entry, thus +1
  87. call s:register(line('$')+1, 'q', 'special', 'call s:close()', '')
  88. endif
  89. " compute first line offset
  90. let b:startify.firstline = 2
  91. let b:startify.firstline += len(g:startify_header)
  92. " no special, no local Session.vim, but a section header
  93. if !s:show_special && !exists('l:show_session') && has_key(lists[0], 'header')
  94. let b:startify.firstline += len(lists[0].header) + 1
  95. endif
  96. let b:startify.lastline = line('$')
  97. let footer = exists('g:startify_custom_footer')
  98. \ ? s:set_custom_section(g:startify_custom_footer)
  99. \ : []
  100. if !empty(footer)
  101. let footer = [''] + footer
  102. endif
  103. call append('$', footer)
  104. setlocal nomodifiable nomodified
  105. call s:set_mappings()
  106. call cursor(b:startify.firstline, 5)
  107. autocmd startify CursorMoved <buffer> call s:set_cursor()
  108. silent! %foldopen!
  109. set filetype=startify
  110. if exists('##DirChanged')
  111. let b:startify.cwd = getcwd()
  112. autocmd startify DirChanged <buffer> if getcwd() !=# get(get(b:, 'startify', {}), 'cwd') | Startify | endif
  113. endif
  114. if exists('#User#Startified')
  115. doautocmd <nomodeline> User Startified
  116. endif
  117. if exists('#User#StartifyReady')
  118. doautocmd <nomodeline> User StartifyReady
  119. endif
  120. endfunction
  121. " Function: #session_load {{{1
  122. function! startify#session_load(source_last_session, ...) abort
  123. if !isdirectory(s:session_dir)
  124. echomsg 'The session directory does not exist: '. s:session_dir
  125. return
  126. elseif empty(startify#session_list_as_string(''))
  127. echomsg 'There are no sessions...'
  128. return
  129. endif
  130. let session_path = s:session_dir . s:sep
  131. if a:0
  132. let session_path .= a:1
  133. elseif a:source_last_session && !has('win32')
  134. let session_path .= '__LAST__'
  135. else
  136. call inputsave()
  137. let session_path .= input(
  138. \ 'Load this session: ',
  139. \ fnamemodify(v:this_session, ':t'),
  140. \ 'custom,startify#session_list_as_string') | redraw
  141. call inputrestore()
  142. endif
  143. if filereadable(session_path)
  144. if get(g:, 'startify_session_persistence') && filewritable(v:this_session)
  145. call startify#session_write(fnameescape(v:this_session))
  146. endif
  147. call startify#session_delete_buffers()
  148. execute 'source '. fnameescape(session_path)
  149. call s:create_last_session_link(session_path)
  150. else
  151. echo 'No such file: '. session_path
  152. endif
  153. endfunction
  154. " Function: #session_save {{{1
  155. function! startify#session_save(bang, ...) abort
  156. if !isdirectory(s:session_dir)
  157. if exists('*mkdir')
  158. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  159. if (nr2char(getchar()) == 'y')
  160. call mkdir(s:session_dir, 'p')
  161. else
  162. echo
  163. return
  164. endif
  165. else
  166. echo 'The session directory does not exist: '. s:session_dir
  167. return
  168. endif
  169. endif
  170. call inputsave()
  171. let this_session = fnamemodify(v:this_session, ':t')
  172. if this_session ==# '__LAST__'
  173. let this_session = ''
  174. endif
  175. let session_name = exists('a:1')
  176. \ ? a:1
  177. \ : input('Save under this session name: ', this_session, 'custom,startify#session_list_as_string') | redraw
  178. call inputrestore()
  179. if empty(session_name)
  180. echo 'You gave an empty name!'
  181. return
  182. endif
  183. let session_path = s:session_dir . s:sep . session_name
  184. if !filereadable(session_path)
  185. call startify#session_write(fnameescape(session_path))
  186. echo 'Session saved under: '. session_path
  187. return
  188. endif
  189. echo 'Session already exists. Overwrite? [y/n]' | redraw
  190. if a:bang || nr2char(getchar()) == 'y'
  191. call startify#session_write(fnameescape(session_path))
  192. echo 'Session saved under: '. session_path
  193. else
  194. echo 'Did NOT save the session!'
  195. endif
  196. endfunction
  197. " Function: #session_close {{{1
  198. function! startify#session_close() abort
  199. if exists('v:this_session') && filewritable(v:this_session)
  200. call startify#session_write(fnameescape(v:this_session))
  201. let v:this_session = ''
  202. endif
  203. call startify#session_delete_buffers()
  204. Startify
  205. endfunction
  206. " Function: #session_write {{{1
  207. function! startify#session_write(session_path)
  208. " preserve existing variables from savevars
  209. if exists('g:startify_session_savevars')
  210. let savevars = map(filter(copy(g:startify_session_savevars), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))')
  211. endif
  212. " if this function is called while being in the Startify buffer
  213. " (by loading another session or running :SSave/:SLoad directly)
  214. " switch back to the previous buffer before saving the session
  215. if &filetype == 'startify'
  216. let callingbuffer = bufnr('#')
  217. if callingbuffer > 0
  218. execute 'buffer' callingbuffer
  219. endif
  220. endif
  221. " prevent saving already deleted buffers that were in the arglist
  222. for arg in argv()
  223. if !buflisted(arg)
  224. execute 'silent! argdelete' fnameescape(arg)
  225. endif
  226. endfor
  227. " clean up session before saving it
  228. for cmd in get(g:, 'startify_session_before_save', [])
  229. execute cmd
  230. endfor
  231. let ssop = &sessionoptions
  232. set sessionoptions-=options
  233. try
  234. execute 'mksession!' a:session_path
  235. catch
  236. echohl ErrorMsg
  237. echomsg v:exception
  238. echohl NONE
  239. return
  240. finally
  241. let &sessionoptions = ssop
  242. endtry
  243. if exists('g:startify_session_remove_lines')
  244. \ || exists('g:startify_session_savevars')
  245. \ || exists('g:startify_session_savecmds')
  246. silent execute 'split' a:session_path
  247. " remove lines from the session file
  248. if exists('g:startify_session_remove_lines')
  249. for pattern in g:startify_session_remove_lines
  250. execute 'silent global/'. pattern .'/delete _'
  251. endfor
  252. endif
  253. " put variables from savevars into session file
  254. if exists('savevars') && !empty(savevars)
  255. call append(line('$')-3, savevars)
  256. endif
  257. " put commands from savecmds into session file
  258. if exists('g:startify_session_savecmds')
  259. call append(line('$')-3, g:startify_session_savecmds)
  260. endif
  261. setlocal bufhidden=delete
  262. silent update
  263. silent hide
  264. endif
  265. call s:create_last_session_link(a:session_path)
  266. endfunction
  267. " Function: #session_delete {{{1
  268. function! startify#session_delete(bang, ...) abort
  269. if !isdirectory(s:session_dir)
  270. echo 'The session directory does not exist: '. s:session_dir
  271. return
  272. elseif empty(startify#session_list_as_string(''))
  273. echo 'There are no sessions...'
  274. return
  275. endif
  276. call inputsave()
  277. let seesion_path = s:session_dir . s:sep . (exists('a:1')
  278. \ ? a:1
  279. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  280. call inputrestore()
  281. if !filereadable(session_path)
  282. echomsg 'No such session: '. session_path
  283. return
  284. endif
  285. echo 'Really delete '. session_path .'? [y/n]'
  286. if a:bang || nr2char(getchar()) == 'y'
  287. if delete(session_path) == 0
  288. echo 'Deleted session '. session_path .'!'
  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. let dname = sfiles[i] ==# v:this_session ? fname.' (*)' : fname
  585. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . dname)
  586. if has('win32')
  587. let fname = substitute(fname, '\[', '\[[]', 'g')
  588. endif
  589. call s:register(line('$'), index, 'session', 'SLoad', fname)
  590. if i == limit
  591. break
  592. endif
  593. endfor
  594. call append('$', '')
  595. endfunction
  596. " Function: s:show_bookmarks {{{1
  597. function! s:show_bookmarks() abort
  598. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  599. return
  600. endif
  601. if exists('s:last_message')
  602. call s:print_section_header()
  603. endif
  604. for bookmark in g:startify_bookmarks
  605. if type(bookmark) == type({})
  606. let [index, path] = items(bookmark)[0]
  607. else " string
  608. let [index, path] = [s:get_index_as_string(), bookmark]
  609. endif
  610. let entry_path = ''
  611. if s:tf
  612. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  613. endif
  614. if empty(entry_path)
  615. let entry_path = path
  616. endif
  617. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  618. if has('win32')
  619. let path = substitute(path, '\[', '\[[]', 'g')
  620. endif
  621. call s:register(line('$'), index, 'file', 'edit', fnameescape(expand(path)))
  622. unlet bookmark " avoid type mismatch for heterogeneous lists
  623. endfor
  624. call append('$', '')
  625. endfunction
  626. " Function: s:show_commands {{{1
  627. function! s:show_commands() abort
  628. if !exists('g:startify_commands') || empty(g:startify_commands)
  629. return
  630. endif
  631. if exists('s:last_message')
  632. call s:print_section_header()
  633. endif
  634. for entry in g:startify_commands
  635. if type(entry) == type({}) " with custom index
  636. let [index, command] = items(entry)[0]
  637. else
  638. let command = entry
  639. let index = s:get_index_as_string()
  640. endif
  641. " If no list is given, the description is the command itself.
  642. let [desc, cmd] = type(command) == type([]) ? command : [command, command]
  643. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . desc)
  644. call s:register(line('$'), index, 'special', cmd, '')
  645. unlet entry command
  646. endfor
  647. call append('$', '')
  648. endfunction
  649. " Function: s:is_in_skiplist {{{1
  650. function! s:is_in_skiplist(arg) abort
  651. for regexp in s:skiplist
  652. try
  653. if a:arg =~# regexp
  654. return 1
  655. endif
  656. catch
  657. call s:warn('Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist')
  658. endtry
  659. endfor
  660. endfunction
  661. " Function: s:set_cursor {{{1
  662. function! s:set_cursor() abort
  663. let b:startify.oldline = exists('b:startify.newline') ? b:startify.newline : 2 + len(s:padding_left)
  664. let b:startify.newline = line('.')
  665. " going up (-1) or down (1)
  666. if b:startify.oldline == b:startify.newline && col('.') != s:fixed_column
  667. let movement = 2 * (col('.') > s:fixed_column) - 1
  668. let b:startify.newline += movement
  669. else
  670. let movement = 2 * (b:startify.newline > b:startify.oldline) - 1
  671. endif
  672. " skip section headers lines until an entry is found
  673. while index(b:startify.section_header_lines, b:startify.newline) != -1
  674. let b:startify.newline += movement
  675. endwhile
  676. " skip blank lines between lists
  677. if empty(getline(b:startify.newline))
  678. let b:startify.newline += movement
  679. endif
  680. " don't go beyond first or last entry
  681. let b:startify.newline = max([b:startify.firstline, min([b:startify.lastline, b:startify.newline])])
  682. call cursor(b:startify.newline, s:fixed_column)
  683. endfunction
  684. " Function: s:set_mappings {{{1
  685. function! s:set_mappings() abort
  686. nnoremap <buffer><nowait><silent> i :enew <bar> startinsert<cr>
  687. nnoremap <buffer><nowait><silent> <insert> :enew <bar> startinsert<cr>
  688. nnoremap <buffer><nowait><silent> b :call <sid>set_mark('B')<cr>
  689. nnoremap <buffer><nowait><silent> s :call <sid>set_mark('S')<cr>
  690. nnoremap <buffer><nowait><silent> t :call <sid>set_mark('T')<cr>
  691. nnoremap <buffer><nowait><silent> v :call <sid>set_mark('V')<cr>
  692. nnoremap <buffer><nowait><silent> <cr> :call startify#open_buffers()<cr>
  693. nnoremap <buffer><nowait><silent> <2-LeftMouse> :call startify#open_buffers()<cr>
  694. nnoremap <buffer><nowait><silent> <MiddleMouse> :enew <bar> execute 'normal! "'.(v:register=='"'?'*':v:register).'gp'<cr>
  695. " Without these mappings n/N wouldn't work properly, since autocmds always
  696. " force the cursor back on the index.
  697. nnoremap <buffer><expr> n ' j'[v:searchforward].'n'
  698. nnoremap <buffer><expr> N 'j '[v:searchforward].'N'
  699. function! s:compare_by_index(foo, bar)
  700. return a:foo.index - a:bar.index
  701. endfunction
  702. for entry in sort(values(b:startify.entries), 's:compare_by_index')
  703. execute 'nnoremap <buffer><silent><nowait>' entry.index
  704. \ ':call startify#open_buffers('. string(entry.line) .')<cr>'
  705. endfor
  706. endfunction
  707. " Function: s:set_mark {{{1
  708. function! s:set_mark(type, ...) abort
  709. let index = expand('<cword>')
  710. let line = exists('a:1') ? a:1 : line('.')
  711. let entry = b:startify.entries[line]
  712. if entry.type != 'file'
  713. return
  714. endif
  715. let default_cmds = {
  716. \ 'B': 'edit',
  717. \ 'S': 'split',
  718. \ 'V': 'vsplit',
  719. \ 'T': 'tabnew',
  720. \ }
  721. setlocal modifiable
  722. if entry.marked && index[0] == a:type
  723. let entry.cmd = 'edit'
  724. let entry.marked = 0
  725. execute 'normal! ci]'. entry.index
  726. else
  727. let entry.cmd = default_cmds[a:type]
  728. let entry.marked = 1
  729. let entry.tick = b:startify.tick
  730. let b:startify.tick += 1
  731. execute 'normal! ci]'. repeat(a:type, len(index))
  732. endif
  733. " Reset cursor to fixed column, which is important for s:set_cursor().
  734. call cursor(line('.'), s:fixed_column)
  735. setlocal nomodifiable nomodified
  736. endfunction
  737. " Function: s:sort_by_tick {{{1
  738. function! s:sort_by_tick(one, two)
  739. return a:one.tick - a:two.tick
  740. endfunction
  741. " Function: s:check_user_options {{{1
  742. function! s:check_user_options(path) abort
  743. let session = a:path . s:sep .'Session.vim'
  744. if get(g:, 'startify_session_autoload') && filereadable(glob(session))
  745. execute 'silent bwipeout' a:path
  746. call startify#session_delete_buffers()
  747. execute 'source' session
  748. return
  749. endif
  750. if get(g:, 'startify_change_to_vcs_root') && s:cd_to_vcs_root(a:path)
  751. return
  752. endif
  753. if get(g:, 'startify_change_to_dir', 1)
  754. if isdirectory(a:path)
  755. execute 'lcd' a:path
  756. else
  757. let dir = fnamemodify(a:path, ':h')
  758. if isdirectory(dir)
  759. execute 'lcd' dir
  760. else
  761. " Do nothing. E.g. a:path == `scp://foo/bar`
  762. endif
  763. endif
  764. endif
  765. endfunction
  766. " Function: s:cd_to_vcs_root {{{1
  767. function! s:cd_to_vcs_root(path) abort
  768. let dir = fnamemodify(a:path, ':p:h')
  769. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  770. let root = finddir(vcs, dir .';')
  771. if !empty(root)
  772. execute 'lcd' fnameescape(fnamemodify(root, ':h'))
  773. return 1
  774. endif
  775. endfor
  776. return 0
  777. endfunction
  778. " Function: s:close {{{1
  779. function! s:close() abort
  780. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  781. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  782. buffer #
  783. else
  784. bnext
  785. endif
  786. else
  787. quit
  788. endif
  789. endfunction
  790. " Function: s:get_index_as_string {{{1
  791. function! s:get_index_as_string() abort
  792. if !empty(b:startify.indices)
  793. return remove(b:startify.indices, 0)
  794. elseif exists('g:startify_custom_indices')
  795. let listlen = len(g:startify_custom_indices)
  796. if b:startify.entry_number < listlen
  797. let idx = g:startify_custom_indices[b:startify.entry_number]
  798. else
  799. let idx = string(b:startify.entry_number - listlen)
  800. endif
  801. else
  802. let idx = string(b:startify.entry_number)
  803. endif
  804. let b:startify.entry_number += 1
  805. return idx
  806. endfunction
  807. " Function: s:print_section_header {{{1
  808. function! s:print_section_header() abort
  809. $
  810. let curline = line('.')
  811. for lnum in range(curline, curline + len(s:last_message) + 1)
  812. call add(b:startify.section_header_lines, lnum)
  813. endfor
  814. call append('$', s:last_message + [''])
  815. unlet s:last_message
  816. endfunction
  817. " Function: s:register {{{1
  818. function! s:register(line, index, type, cmd, path)
  819. let b:startify.entries[a:line] = {
  820. \ 'index': a:index,
  821. \ 'type': a:type,
  822. \ 'line': a:line,
  823. \ 'cmd': a:cmd,
  824. \ 'path': a:path,
  825. \ 'marked': 0,
  826. \ }
  827. endfunction
  828. " Function: s:create_last_session_link {{{1
  829. function! s:create_last_session_link(session_path)
  830. if !has('win32') && a:session_path !~# '__LAST__$'
  831. let cmd = printf('ln -sf %s %s',
  832. \ shellescape(fnamemodify(a:session_path, ':t')),
  833. \ shellescape(s:session_dir .'/__LAST__'))
  834. call system(cmd)
  835. if v:shell_error
  836. call s:warn("Can't create 'last used session' symlink.")
  837. endif
  838. endif
  839. endfunction
  840. " Function: s:init_env {{{1
  841. function! s:init_env()
  842. let s:env = []
  843. let ignore = {
  844. \ 'HOME': 1,
  845. \ 'OLDPWD': 1,
  846. \ 'PWD': 1,
  847. \ }
  848. function! s:get_env()
  849. redir => s
  850. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  851. redir END
  852. redraw
  853. return split(s)
  854. endfunction
  855. function! s:compare_by_key_len(foo, bar)
  856. return len(a:foo[0]) - len(a:bar[0])
  857. endfunction
  858. function! s:compare_by_val_len(foo, bar)
  859. return len(a:bar[1]) - len(a:foo[1])
  860. endfunction
  861. for k in s:get_env()
  862. silent! execute "let v = eval('$'.k)"
  863. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  864. \ || has_key(ignore, k)
  865. \ || len(k) > len(v)
  866. continue
  867. endif
  868. call insert(s:env, [k,v], 0)
  869. endfor
  870. let s:env = sort(s:env, 's:compare_by_key_len')
  871. let s:env = sort(s:env, 's:compare_by_val_len')
  872. endfunction
  873. " Function: s:transform {{{1
  874. function s:transform(absolute_path)
  875. for [k,V] in g:startify_transformations
  876. if a:absolute_path =~ k
  877. return type(V) == type('') ? V : V(a:absolute_path)
  878. endif
  879. unlet V
  880. endfor
  881. return ''
  882. endfunction
  883. " Function: s:warn {{{1
  884. function! s:warn(msg) abort
  885. echohl WarningMsg
  886. echomsg 'startify: '. a:msg
  887. echohl NONE
  888. endfunction
  889. " Function: s:get_session_path {{{1
  890. function! s:get_session_path() abort
  891. if exists('g:startify_session_dir')
  892. let path = g:startify_session_dir
  893. elseif has('nvim')
  894. let path = has('nvim-0.3.1')
  895. \ ? stdpath('data').'/session'
  896. \ : has('win32')
  897. \ ? '~/AppData/Local/nvim-data/session'
  898. \ : '~/.local/share/nvim/session'
  899. else " Vim
  900. let path = has('win32')
  901. \ ? '~/vimfiles/session'
  902. \ : '~/.vim/session'
  903. endif
  904. return resolve(expand(path))
  905. endfunction
  906. " Init: values {{{1
  907. let s:sep = startify#get_separator()
  908. let s:numfiles = get(g:, 'startify_files_number', 10)
  909. let s:show_special = get(g:, 'startify_enable_special', 1)
  910. let s:relative_path = get(g:, 'startify_relative_path') ? ':~:.' : ':p:~'
  911. let s:tf = exists('g:startify_transformations')
  912. let s:session_dir = s:get_session_path()
  913. let s:skiplist = get(g:, 'startify_skiplist', [
  914. \ 'COMMIT_EDITMSG',
  915. \ 'runtime/doc/.*\.txt',
  916. \ 'bundle/.*/doc/.*\.txt',
  917. \ 'plugged/.*/doc/.*\.txt',
  918. \ escape(fnamemodify(resolve($VIMRUNTIME), ':p'), '\') .'doc/.*\.txt',
  919. \ ])
  920. let s:padding_left = repeat(' ', get(g:, 'startify_padding_left', 3))
  921. let s:fixed_column = len(s:padding_left) + 2