startify.vim 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. let entry_format .= exists('*StartifyEntryFormat') ? StartifyEntryFormat() : 'entry_path'
  491. if !empty(oldfiles)
  492. if exists('s:last_message')
  493. call s:print_section_header()
  494. endif
  495. for [absolute_path, entry_path] in oldfiles
  496. let index = s:get_index_as_string()
  497. call append('$', eval(entry_format))
  498. if has('win32')
  499. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  500. endif
  501. call s:register(line('$'), index, 'file', 'edit', absolute_path)
  502. endfor
  503. call append('$', '')
  504. endif
  505. endfunction
  506. " Function: s:filter_oldfiles {{{1
  507. function! s:filter_oldfiles(path_prefix, path_format, use_env) abort
  508. let path_prefix = '\V'. escape(a:path_prefix, '\')
  509. let counter = g:startify_files_number
  510. let entries = {}
  511. let oldfiles = []
  512. for fname in v:oldfiles
  513. if counter <= 0
  514. break
  515. endif
  516. if s:is_in_skiplist(fname)
  517. " https://github.com/mhinz/vim-startify/issues/353
  518. continue
  519. endif
  520. let absolute_path = fnamemodify(resolve(fname), ":p")
  521. " filter duplicates, bookmarks and entries from the skiplist
  522. if has_key(entries, absolute_path)
  523. \ || !filereadable(absolute_path)
  524. \ || s:is_in_skiplist(absolute_path)
  525. \ || match(absolute_path, path_prefix)
  526. continue
  527. endif
  528. let entry_path = ''
  529. if !empty(g:startify_transformations)
  530. let entry_path = s:transform(absolute_path)
  531. endif
  532. if empty(entry_path)
  533. let entry_path = fnamemodify(absolute_path, a:path_format)
  534. endif
  535. let entries[absolute_path] = 1
  536. let counter -= 1
  537. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  538. endfor
  539. if a:use_env
  540. call s:init_env()
  541. for i in range(len(oldfiles))
  542. for [k,v] in s:env
  543. let p = oldfiles[i][0]
  544. if !stridx(tolower(p), tolower(v))
  545. let oldfiles[i][1] = printf('$%s%s', k, p[len(v):])
  546. break
  547. endif
  548. endfor
  549. endfor
  550. endif
  551. return oldfiles
  552. endfunction
  553. " Function: s:filter_oldfiles_unsafe {{{1
  554. function! s:filter_oldfiles_unsafe(path_prefix, path_format, use_env) abort
  555. let path_prefix = '\V'. escape(a:path_prefix, '\')
  556. let counter = g:startify_files_number
  557. let entries = {}
  558. let oldfiles = []
  559. let is_dir = escape(s:sep, '\') . '$'
  560. for fname in v:oldfiles
  561. if counter <= 0
  562. break
  563. endif
  564. if s:is_in_skiplist(fname)
  565. " https://github.com/mhinz/vim-startify/issues/353
  566. continue
  567. endif
  568. let absolute_path = glob(fnamemodify(fname, ":p"))
  569. if empty(absolute_path)
  570. \ || has_key(entries, absolute_path)
  571. \ || (absolute_path =~ is_dir)
  572. \ || match(absolute_path, path_prefix)
  573. \ || s:is_in_skiplist(absolute_path)
  574. continue
  575. endif
  576. let entry_path = fnamemodify(absolute_path, a:path_format)
  577. let entries[absolute_path] = 1
  578. let counter -= 1
  579. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  580. endfor
  581. return oldfiles
  582. endfunction
  583. " Function: s:show_dir {{{1
  584. function! s:show_dir() abort
  585. return s:display_by_path(getcwd() . s:sep, ':.', 0)
  586. endfunction
  587. " Function: s:show_files {{{1
  588. function! s:show_files() abort
  589. return s:display_by_path('', g:startify_relative_path, get(g:, 'startify_use_env'))
  590. endfunction
  591. " Function: s:show_sessions {{{1
  592. function! s:show_sessions() abort
  593. let limit = get(g:, 'startify_session_number', 999) - 1
  594. if limit <= -1
  595. return
  596. endif
  597. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  598. let sfiles = filter(sfiles, 'v:val !~# "__LAST__$"')
  599. let sfiles = filter(sfiles,
  600. \ '!(v:val =~# "x\.vim$" && index(sfiles, v:val[:-6].".vim") >= 0)')
  601. if empty(sfiles)
  602. if exists('s:last_message')
  603. unlet s:last_message
  604. endif
  605. return
  606. endif
  607. if exists('s:last_message')
  608. call s:print_section_header()
  609. endif
  610. if get(g:, 'startify_session_sort')
  611. function! s:sort_by_mtime(foo, bar)
  612. let foo = getftime(a:foo)
  613. let bar = getftime(a:bar)
  614. return foo == bar ? 0 : (foo < bar ? 1 : -1)
  615. endfunction
  616. call sort(sfiles, 's:sort_by_mtime')
  617. endif
  618. for i in range(len(sfiles))
  619. let index = s:get_index_as_string()
  620. let fname = fnamemodify(sfiles[i], ':t')
  621. let dname = sfiles[i] ==# v:this_session ? fname.' (*)' : fname
  622. call append('$', s:leftpad .'['. index .']'. repeat(' ', (3 - strlen(index))) . dname)
  623. if has('win32')
  624. let fname = substitute(fname, '\[', '\[[]', 'g')
  625. endif
  626. call s:register(line('$'), index, 'session', 'SLoad', fname)
  627. if i == limit
  628. break
  629. endif
  630. endfor
  631. call append('$', '')
  632. endfunction
  633. " Function: s:show_bookmarks {{{1
  634. function! s:show_bookmarks() abort
  635. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  636. return
  637. endif
  638. if exists('s:last_message')
  639. call s:print_section_header()
  640. endif
  641. let entry_format = "s:leftpad .'['. index .']'. repeat(' ', (3 - strlen(index))) ."
  642. let entry_format .= exists('*StartifyEntryFormat') ? StartifyEntryFormat() : 'entry_path'
  643. for bookmark in g:startify_bookmarks
  644. if type(bookmark) == type({})
  645. let [index, path] = items(bookmark)[0]
  646. else " string
  647. let [index, path] = [s:get_index_as_string(), bookmark]
  648. endif
  649. let absolute_path = path
  650. let entry_path = ''
  651. if !empty(g:startify_transformations)
  652. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  653. endif
  654. if empty(entry_path)
  655. let entry_path = path
  656. endif
  657. call append('$', eval(entry_format))
  658. if has('win32')
  659. let path = substitute(path, '\[', '\[[]', 'g')
  660. endif
  661. call s:register(line('$'), index, 'file', 'edit', fnameescape(expand(path)))
  662. unlet bookmark " avoid type mismatch for heterogeneous lists
  663. endfor
  664. call append('$', '')
  665. endfunction
  666. " Function: s:show_commands {{{1
  667. function! s:show_commands() abort
  668. if !exists('g:startify_commands') || empty(g:startify_commands)
  669. return
  670. endif
  671. if exists('s:last_message')
  672. call s:print_section_header()
  673. endif
  674. for entry in g:startify_commands
  675. if type(entry) == type({}) " with custom index
  676. let [index, command] = items(entry)[0]
  677. else
  678. let command = entry
  679. let index = s:get_index_as_string()
  680. endif
  681. " If no list is given, the description is the command itself.
  682. let [desc, cmd] = type(command) == type([]) ? command : [command, command]
  683. call append('$', s:leftpad .'['. index .']'. repeat(' ', (3 - strlen(index))) . desc)
  684. call s:register(line('$'), index, 'special', cmd, '')
  685. unlet entry command
  686. endfor
  687. call append('$', '')
  688. endfunction
  689. " Function: s:is_in_skiplist {{{1
  690. function! s:is_in_skiplist(arg) abort
  691. for regexp in g:startify_skiplist
  692. try
  693. if a:arg =~# regexp
  694. return 1
  695. endif
  696. catch
  697. call s:warn('Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist')
  698. endtry
  699. endfor
  700. endfunction
  701. " Function: s:set_cursor {{{1
  702. function! s:set_cursor() abort
  703. let b:startify.oldline = exists('b:startify.newline') ? b:startify.newline : s:fixed_column
  704. let b:startify.newline = line('.')
  705. " going up (-1) or down (1)
  706. if b:startify.oldline == b:startify.newline
  707. \ && col('.') != s:fixed_column
  708. \ && !b:startify.leftmouse
  709. let movement = 2 * (col('.') > s:fixed_column) - 1
  710. let b:startify.newline += movement
  711. else
  712. let movement = 2 * (b:startify.newline > b:startify.oldline) - 1
  713. let b:startify.leftmouse = 0
  714. endif
  715. " skip section headers lines until an entry is found
  716. while index(b:startify.section_header_lines, b:startify.newline) != -1
  717. let b:startify.newline += movement
  718. endwhile
  719. " skip blank lines between lists
  720. if empty(getline(b:startify.newline))
  721. let b:startify.newline += movement
  722. endif
  723. " don't go beyond first or last entry
  724. let b:startify.newline = max([b:startify.firstline, min([b:startify.lastline, b:startify.newline])])
  725. call cursor(b:startify.newline, s:fixed_column)
  726. endfunction
  727. " Function: s:set_mappings {{{1
  728. function! s:set_mappings() abort
  729. nnoremap <buffer><nowait><silent> i :enew <bar> startinsert<cr>
  730. nnoremap <buffer><nowait><silent> <insert> :enew <bar> startinsert<cr>
  731. nnoremap <buffer><nowait><silent> b :call startify#set_mark('B')<cr>
  732. nnoremap <buffer><nowait><silent> s :call startify#set_mark('S')<cr>
  733. nnoremap <buffer><nowait><silent> t :call startify#set_mark('T')<cr>
  734. nnoremap <buffer><nowait><silent> v :call startify#set_mark('V')<cr>
  735. nnoremap <buffer><nowait><silent> B :call startify#set_batchmode('B')<cr>
  736. nnoremap <buffer><nowait><silent> S :call startify#set_batchmode('S')<cr>
  737. nnoremap <buffer><nowait><silent> T :call startify#set_batchmode('T')<cr>
  738. nnoremap <buffer><nowait><silent> V :call startify#set_batchmode('V')<cr>
  739. nnoremap <buffer><nowait><silent> <cr> :call startify#open_buffers()<cr>
  740. nnoremap <buffer><nowait><silent> <LeftMouse> :call <sid>leftmouse()<cr>
  741. nnoremap <buffer><nowait><silent> <2-LeftMouse> :call startify#open_buffers()<cr>
  742. nnoremap <buffer><nowait><silent> <MiddleMouse> :enew <bar> execute 'normal! "'.(v:register=='"'?'*':v:register).'gp'<cr>
  743. " Without these mappings n/N wouldn't work properly, since autocmds always
  744. " force the cursor back on the index.
  745. nnoremap <buffer><expr> n ' j'[v:searchforward].'n'
  746. nnoremap <buffer><expr> N 'j '[v:searchforward].'N'
  747. function! s:leftmouse()
  748. " feedkeys() triggers CursorMoved which calls s:set_cursor() which checks
  749. " .leftmouse.
  750. let b:startify.leftmouse = 1
  751. call feedkeys("\<LeftMouse>", 'nt')
  752. endfunction
  753. function! s:compare_by_index(foo, bar)
  754. return a:foo.index - a:bar.index
  755. endfunction
  756. for entry in sort(values(b:startify.entries), 's:compare_by_index')
  757. execute 'nnoremap <buffer><silent><nowait>' entry.index
  758. \ ':call startify#open_buffers('. string(entry.line) .')<cr>'
  759. endfor
  760. endfunction
  761. " Function: #set_batchmode {{{1
  762. function! startify#set_batchmode(batchmode) abort
  763. let s:batchmode = (a:batchmode == s:batchmode) ? '' : a:batchmode
  764. echo empty(s:batchmode) ? 'Batchmode off' : 'Batchmode: '. s:batchmode
  765. endfunction
  766. " Function: #set_mark {{{1
  767. function! startify#set_mark(type, ...) abort
  768. if a:0
  769. let entryline = a:1
  770. else
  771. call startify#set_batchmode('')
  772. let entryline = line('.')
  773. endif
  774. let entry = b:startify.entries[entryline]
  775. if entry.type != 'file'
  776. return
  777. endif
  778. let default_cmds = {
  779. \ 'B': 'edit',
  780. \ 'S': 'split',
  781. \ 'V': 'vsplit',
  782. \ 'T': 'tabnew',
  783. \ }
  784. let origline = line('.')
  785. execute entryline
  786. let index = expand('<cword>')
  787. setlocal modifiable
  788. if entry.marked && index[0] == a:type
  789. let entry.cmd = 'edit'
  790. let entry.marked = 0
  791. execute 'normal! "_ci]'. entry.index
  792. else
  793. let entry.cmd = default_cmds[a:type]
  794. let entry.marked = 1
  795. let entry.tick = b:startify.tick
  796. let b:startify.tick += 1
  797. execute 'normal! "_ci]'. repeat(a:type, len(index))
  798. endif
  799. setlocal nomodifiable nomodified
  800. " Reset cursor to fixed column, which is important for s:set_cursor().
  801. call cursor(origline, s:fixed_column)
  802. endfunction
  803. " Function: s:sort_by_tick {{{1
  804. function! s:sort_by_tick(one, two)
  805. return a:one.tick - a:two.tick
  806. endfunction
  807. " Function: s:check_user_options {{{1
  808. function! s:check_user_options(path) abort
  809. let session = a:path . s:sep .'Session.vim'
  810. if get(g:, 'startify_session_autoload') && filereadable(glob(session))
  811. execute 'silent bwipeout' a:path
  812. call startify#session_delete_buffers()
  813. execute 'source' session
  814. return
  815. endif
  816. if get(g:, 'startify_change_to_vcs_root') && s:cd_to_vcs_root(a:path)
  817. return
  818. endif
  819. if get(g:, 'startify_change_to_dir', 1)
  820. if isdirectory(a:path)
  821. execute s:cd_cmd() a:path
  822. else
  823. let dir = fnamemodify(a:path, ':h')
  824. if isdirectory(dir)
  825. execute s:cd_cmd() dir
  826. else
  827. " Do nothing. E.g. a:path == `scp://foo/bar`
  828. endif
  829. endif
  830. endif
  831. endfunction
  832. " Function: s:cd_to_vcs_root {{{1
  833. function! s:cd_to_vcs_root(path) abort
  834. let dir = fnamemodify(a:path, ':p:h')
  835. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  836. let root = finddir(vcs, dir .';')
  837. if !empty(root)
  838. execute s:cd_cmd() fnameescape(fnamemodify(root, ':h'))
  839. return 1
  840. endif
  841. endfor
  842. return 0
  843. endfunction
  844. " Function: s:cd_cmd {{{1
  845. function! s:cd_cmd() abort
  846. let g:startify_change_cmd = get(g:, 'startify_change_cmd', 'lcd')
  847. if g:startify_change_cmd !~# '^[lt]\?cd$'
  848. call s:warn('Invalid value for g:startify_change_cmd. Defaulting to :lcd')
  849. let g:startify_change_cmd = 'lcd'
  850. endif
  851. return g:startify_change_cmd
  852. endfunction
  853. " Function: s:close {{{1
  854. function! s:close() abort
  855. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  856. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  857. buffer #
  858. else
  859. bnext
  860. endif
  861. else
  862. quit
  863. endif
  864. endfunction
  865. " Function: s:get_index_as_string {{{1
  866. function! s:get_index_as_string() abort
  867. if !empty(b:startify.indices)
  868. return remove(b:startify.indices, 0)
  869. elseif exists('g:startify_custom_indices')
  870. let listlen = len(g:startify_custom_indices)
  871. if b:startify.entry_number < listlen
  872. let idx = g:startify_custom_indices[b:startify.entry_number]
  873. else
  874. let idx = string(b:startify.entry_number - listlen)
  875. endif
  876. else
  877. let idx = string(b:startify.entry_number)
  878. endif
  879. let b:startify.entry_number += 1
  880. return idx
  881. endfunction
  882. " Function: s:print_section_header {{{1
  883. function! s:print_section_header() abort
  884. $
  885. let curline = line('.')
  886. for lnum in range(curline, curline + len(s:last_message) + 1)
  887. call add(b:startify.section_header_lines, lnum)
  888. endfor
  889. call append('$', s:last_message + [''])
  890. unlet s:last_message
  891. endfunction
  892. " Function: s:register {{{1
  893. function! s:register(line, index, type, cmd, path)
  894. let b:startify.entries[a:line] = {
  895. \ 'index': a:index,
  896. \ 'type': a:type,
  897. \ 'line': a:line,
  898. \ 'cmd': a:cmd,
  899. \ 'path': a:path,
  900. \ 'marked': 0,
  901. \ }
  902. endfunction
  903. " Function: s:create_last_session_link {{{1
  904. function! s:create_last_session_link(session_path)
  905. if !has('win32') && a:session_path !~# '__LAST__$'
  906. let cmd = printf('ln -sf %s %s',
  907. \ shellescape(fnamemodify(a:session_path, ':t')),
  908. \ shellescape(s:session_dir .'/__LAST__'))
  909. silent call system(cmd)
  910. if v:shell_error
  911. call s:warn("Can't create 'last used session' symlink.")
  912. endif
  913. endif
  914. endfunction
  915. " Function: s:init_env {{{1
  916. function! s:init_env()
  917. let s:env = []
  918. let ignore = {
  919. \ 'HOME': 1,
  920. \ 'OLDPWD': 1,
  921. \ 'PWD': 1,
  922. \ }
  923. if exists('*environ')
  924. let env = items(environ())
  925. else
  926. redir => s
  927. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  928. redir END
  929. redraw
  930. let env = map(split(s), '[v:val, eval("$".v:val)]')
  931. endif
  932. for [var, val] in env
  933. if has('win32') ? (val[1] != ':') : (val[0] != '/')
  934. \ || has_key(ignore, var)
  935. \ || len(var) > len(val)
  936. continue
  937. endif
  938. call insert(s:env, [var, val], 0)
  939. endfor
  940. function! s:compare_by_key_len(foo, bar)
  941. return len(a:foo[0]) - len(a:bar[0])
  942. endfunction
  943. function! s:compare_by_val_len(foo, bar)
  944. return len(a:bar[1]) - len(a:foo[1])
  945. endfunction
  946. let s:env = sort(s:env, 's:compare_by_key_len')
  947. let s:env = sort(s:env, 's:compare_by_val_len')
  948. endfunction
  949. " Function: s:transform {{{1
  950. function s:transform(absolute_path)
  951. for [k,V] in g:startify_transformations
  952. if a:absolute_path =~ k
  953. return type(V) == type('') ? V : V(a:absolute_path)
  954. endif
  955. unlet V
  956. endfor
  957. return ''
  958. endfunction
  959. " Function: s:warn {{{1
  960. function! s:warn(msg) abort
  961. echohl WarningMsg
  962. echomsg 'startify: '. a:msg
  963. echohl NONE
  964. endfunction
  965. " Init: values {{{1
  966. let s:sep = startify#get_separator()
  967. let g:startify_files_number = get(g:, 'startify_files_number', 10)
  968. let g:startify_enable_special = get(g:, 'startify_enable_special', 1)
  969. let g:startify_relative_path = get(g:, 'startify_relative_path') ? ':~:.' : ':p:~'
  970. let s:session_dir = startify#get_session_path()
  971. let g:startify_transformations = get(g:, 'startify_transformations', [])
  972. let g:startify_skiplist = extend(get(g:, 'startify_skiplist', []), [
  973. \ 'runtime/doc/.*\.txt$',
  974. \ 'bundle/.*/doc/.*\.txt$',
  975. \ 'plugged/.*/doc/.*\.txt$',
  976. \ '/.git/',
  977. \ 'fugitiveblame$',
  978. \ escape(fnamemodify(resolve($VIMRUNTIME), ':p'), '\') .'doc/.*\.txt$',
  979. \ ], 'keep')
  980. let g:startify_padding_left = get(g:, 'startify_padding_left', 3)
  981. let s:leftpad = repeat(' ', g:startify_padding_left)
  982. let s:fixed_column = g:startify_padding_left + 2
  983. let s:batchmode = ''