startify.vim 29 KB

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