startify.vim 29 KB

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