startify.vim 32 KB

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