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