startify.vim 31 KB

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