startify.vim 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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': {} }
  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. if type(list.type) == type('')
  394. if has_key(list, 'header')
  395. let s:last_message = list.header
  396. endif
  397. call s:show_{list.type}()
  398. elseif type(list.type) == type(function('tr'))
  399. try
  400. let entries = list.type()
  401. catch
  402. call s:warn(v:exception)
  403. continue
  404. endtry
  405. if empty(entries)
  406. unlet! s:last_message
  407. continue
  408. endif
  409. if has_key(list, 'header')
  410. let s:last_message = list.header
  411. call s:print_section_header()
  412. endif
  413. for entry in entries
  414. let cmd = get(entry, 'cmd', 'edit')
  415. let path = get(entry, 'path', '')
  416. let type = get(entry, 'type', empty(path) ? 'special' : 'file')
  417. let index = s:get_index_as_string(b:startify.entry_number)
  418. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry.line)
  419. call s:register(line('$'), index, type, cmd, path)
  420. let b:startify.entry_number += 1
  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 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(b:startify.entry_number)
  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. let b:startify.entry_number += 1
  477. endfor
  478. call append('$', '')
  479. endif
  480. endfunction
  481. " Function: s:filter_oldfiles {{{1
  482. function! s:filter_oldfiles(path_prefix, path_format, use_env) abort
  483. let path_prefix = '\V'. escape(a:path_prefix, '\')
  484. let counter = s:numfiles
  485. let entries = {}
  486. let oldfiles = []
  487. for fname in v:oldfiles
  488. if counter <= 0
  489. break
  490. endif
  491. let absolute_path = fnamemodify(resolve(fname), ":p")
  492. " filter duplicates, bookmarks and entries from the skiplist
  493. if has_key(entries, absolute_path)
  494. \ || !filereadable(absolute_path)
  495. \ || s:is_in_skiplist(absolute_path)
  496. \ || match(absolute_path, path_prefix)
  497. continue
  498. endif
  499. let entry_path = ''
  500. if s:tf
  501. let entry_path = s:transform(absolute_path)
  502. endif
  503. if empty(entry_path)
  504. let entry_path = fnamemodify(absolute_path, a:path_format)
  505. endif
  506. let entries[absolute_path] = 1
  507. let counter -= 1
  508. if !has('win32')
  509. let absolute_path = fnameescape(absolute_path)
  510. endif
  511. let oldfiles += [[absolute_path, entry_path]]
  512. endfor
  513. if a:use_env
  514. call s:init_env()
  515. for i in range(len(oldfiles))
  516. for [k,v] in s:env
  517. let p = oldfiles[i][0]
  518. if !stridx(tolower(p), tolower(v))
  519. let oldfiles[i][1] = printf('$%s%s', k, p[len(v):])
  520. break
  521. endif
  522. endfor
  523. endfor
  524. endif
  525. return oldfiles
  526. endfun
  527. " Function: s:filter_oldfiles_unsafe {{{1
  528. function! s:filter_oldfiles_unsafe(path_prefix, path_format, use_env) abort
  529. let path_prefix = '\V'. escape(a:path_prefix, '\')
  530. let counter = s:numfiles
  531. let entries = {}
  532. let oldfiles = []
  533. let is_dir = escape(s:sep, '\') . '$'
  534. for fname in v:oldfiles
  535. if counter <= 0
  536. break
  537. endif
  538. let absolute_path = glob(fnamemodify(fname, ":p"))
  539. if empty(absolute_path)
  540. \ || has_key(entries, absolute_path)
  541. \ || (absolute_path =~ is_dir)
  542. \ || match(absolute_path, path_prefix)
  543. \ || s:is_in_skiplist(absolute_path)
  544. continue
  545. endif
  546. let entry_path = fnamemodify(absolute_path, a:path_format)
  547. let entries[absolute_path] = 1
  548. let counter -= 1
  549. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  550. endfor
  551. return oldfiles
  552. endfun
  553. " Function: s:show_dir {{{1
  554. function! s:show_dir() abort
  555. return s:display_by_path(getcwd() . s:sep, ':.', 0)
  556. endfunction
  557. " Function: s:show_files {{{1
  558. function! s:show_files() abort
  559. return s:display_by_path('', s:relative_path, get(g:, 'startify_use_env'))
  560. endfunction
  561. " Function: s:show_sessions {{{1
  562. function! s:show_sessions() abort
  563. let limit = get(g:, 'startify_session_number', 999) - 1
  564. if limit <= -1
  565. return
  566. endif
  567. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  568. let sfiles = filter(sfiles, 'v:val !~# "__LAST__$"')
  569. let sfiles = filter(sfiles,
  570. \ '!(v:val =~# "x\.vim$" && index(sfiles, v:val[:-6].".vim") >= 0)')
  571. if empty(sfiles)
  572. if exists('s:last_message')
  573. unlet s:last_message
  574. endif
  575. return
  576. endif
  577. if exists('s:last_message')
  578. call s:print_section_header()
  579. endif
  580. if get(g:, 'startify_session_sort')
  581. function! s:sort_by_mtime(foo, bar)
  582. let foo = getftime(a:foo)
  583. let bar = getftime(a:bar)
  584. return foo == bar ? 0 : (foo < bar ? 1 : -1)
  585. endfunction
  586. call sort(sfiles, 's:sort_by_mtime')
  587. endif
  588. for i in range(len(sfiles))
  589. let index = s:get_index_as_string(b:startify.entry_number)
  590. let fname = fnamemodify(sfiles[i], ':t')
  591. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  592. if has('win32')
  593. let fname = substitute(fname, '\[', '\[[]', 'g')
  594. endif
  595. call s:register(line('$'), index, 'session', 'SLoad', fname)
  596. let b:startify.entry_number += 1
  597. if i == limit
  598. break
  599. endif
  600. endfor
  601. call append('$', '')
  602. endfunction
  603. " Function: s:show_bookmarks {{{1
  604. function! s:show_bookmarks() abort
  605. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  606. return
  607. endif
  608. if exists('s:last_message')
  609. call s:print_section_header()
  610. endif
  611. for bookmark in g:startify_bookmarks
  612. if type(bookmark) == type({})
  613. let [index, path] = items(bookmark)[0]
  614. else " string
  615. let [index, path] = [s:get_index_as_string(b:startify.entry_number), bookmark]
  616. let b:startify.entry_number += 1
  617. endif
  618. let entry_path = ''
  619. if s:tf
  620. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  621. endif
  622. if empty(entry_path)
  623. let entry_path = path
  624. endif
  625. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  626. if has('win32')
  627. let path = substitute(path, '\[', '\[[]', 'g')
  628. endif
  629. call s:register(line('$'), index, 'file', 'edit', fnameescape(expand(path)))
  630. unlet bookmark " avoid type mismatch for heterogeneous lists
  631. endfor
  632. call append('$', '')
  633. endfunction
  634. " Function: s:show_commands {{{1
  635. function! s:show_commands() abort
  636. if !exists('g:startify_commands') || empty(g:startify_commands)
  637. return
  638. endif
  639. if exists('s:last_message')
  640. call s:print_section_header()
  641. endif
  642. for entry in g:startify_commands
  643. if type(entry) == type({}) " with custom index
  644. let [index, command] = items(entry)[0]
  645. else
  646. let command = entry
  647. let index = s:get_index_as_string(b:startify.entry_number)
  648. let b:startify.entry_number += 1
  649. endif
  650. " If no list is given, the description is the command itself.
  651. let [desc, cmd] = type(command) == type([]) ? command : [command, command]
  652. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . desc)
  653. call s:register(line('$'), index, 'special', cmd, '')
  654. unlet entry command
  655. endfor
  656. call append('$', '')
  657. endfunction
  658. " Function: s:is_in_skiplist {{{1
  659. function! s:is_in_skiplist(arg) abort
  660. for regexp in s:skiplist
  661. try
  662. if a:arg =~# regexp
  663. return 1
  664. endif
  665. catch
  666. call s:warn('Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist')
  667. endtry
  668. endfor
  669. endfunction
  670. " Function: s:set_cursor {{{1
  671. function! s:set_cursor() abort
  672. let b:startify.oldline = exists('b:startify.newline') ? b:startify.newline : 2 + len(s:padding_left)
  673. let b:startify.newline = line('.')
  674. " going up (-1) or down (1)
  675. if b:startify.oldline == b:startify.newline && col('.') != s:fixed_column
  676. let movement = 2 * (col('.') > s:fixed_column) - 1
  677. let b:startify.newline += movement
  678. else
  679. let movement = 2 * (b:startify.newline > b:startify.oldline) - 1
  680. endif
  681. " skip section headers lines until an entry is found
  682. while index(b:startify.section_header_lines, b:startify.newline) != -1
  683. let b:startify.newline += movement
  684. endwhile
  685. " skip blank lines between lists
  686. if empty(getline(b:startify.newline))
  687. let b:startify.newline += movement
  688. endif
  689. " don't go beyond first or last entry
  690. let b:startify.newline = max([b:startify.firstline, min([b:startify.lastline, b:startify.newline])])
  691. call cursor(b:startify.newline, s:fixed_column)
  692. endfunction
  693. " Function: s:set_mappings {{{1
  694. function! s:set_mappings() abort
  695. execute "nnoremap <buffer>". s:nowait ."<silent> i :enew <bar> startinsert<cr>"
  696. execute "nnoremap <buffer>". s:nowait ."<silent> <insert> :enew <bar> startinsert<cr>"
  697. execute "nnoremap <buffer>". s:nowait ."<silent> b :call <sid>set_mark('B')<cr>"
  698. execute "nnoremap <buffer>". s:nowait ."<silent> s :call <sid>set_mark('S')<cr>"
  699. execute "nnoremap <buffer>". s:nowait ."<silent> t :call <sid>set_mark('T')<cr>"
  700. execute "nnoremap <buffer>". s:nowait ."<silent> v :call <sid>set_mark('V')<cr>"
  701. execute "nnoremap <buffer>". s:nowait ."<silent> <cr> :call startify#open_buffers()<cr>"
  702. execute "nnoremap <buffer>". s:nowait ."<silent> <2-LeftMouse> :call startify#open_buffers()<cr>"
  703. execute "nnoremap <buffer>". s:nowait ."<silent> <MiddleMouse> :enew <bar> execute 'normal! \"'.(v:register=='\"'?'*':v:register).'gp'<cr>"
  704. " Without these mappings n/N wouldn't work properly, since autocmds always
  705. " force the cursor back on the index.
  706. nnoremap <buffer><expr> n ' j'[v:searchforward].'n'
  707. nnoremap <buffer><expr> N 'j '[v:searchforward].'N'
  708. function! s:compare_by_index(foo, bar)
  709. return a:foo.index - a:bar.index
  710. endfunction
  711. for entry in sort(values(b:startify.entries), 's:compare_by_index')
  712. execute 'nnoremap <buffer><silent>'. s:nowait entry.index
  713. \ ':call startify#open_buffers('. string(entry.line) .')<cr>'
  714. endfor
  715. endfunction
  716. " Function: s:set_mark {{{1
  717. function! s:set_mark(type, ...) abort
  718. let index = expand('<cword>')
  719. let line = exists('a:1') ? a:1 : line('.')
  720. let entry = b:startify.entries[line]
  721. if entry.type != 'file'
  722. return
  723. endif
  724. let default_cmds = {
  725. \ 'B': 'edit',
  726. \ 'S': 'split',
  727. \ 'V': 'vsplit',
  728. \ 'T': 'tabnew',
  729. \ }
  730. setlocal modifiable
  731. if entry.marked && index[0] == a:type
  732. let entry.cmd = 'edit'
  733. let entry.marked = 0
  734. execute 'normal! ci]'. entry.index
  735. else
  736. let entry.cmd = default_cmds[a:type]
  737. let entry.marked = 1
  738. let entry.tick = b:startify.tick
  739. let b:startify.tick += 1
  740. execute 'normal! ci]'. repeat(a:type, len(index))
  741. endif
  742. " Reset cursor to fixed column, which is important for s:set_cursor().
  743. call cursor(line('.'), s:fixed_column)
  744. setlocal nomodifiable nomodified
  745. endfunction
  746. " Function: s:sort_by_tick {{{1
  747. function! s:sort_by_tick(one, two)
  748. return a:one.tick - a:two.tick
  749. endfunction
  750. " Function: s:check_user_options {{{1
  751. function! s:check_user_options(path) abort
  752. let session = a:path . s:sep .'Session.vim'
  753. if get(g:, 'startify_session_autoload') && filereadable(glob(session))
  754. execute 'silent bwipeout' a:path
  755. call startify#session_delete_buffers()
  756. execute 'source' session
  757. elseif get(g:, 'startify_change_to_vcs_root')
  758. call s:cd_to_vcs_root(a:path)
  759. elseif get(g:, 'startify_change_to_dir', 1)
  760. if isdirectory(a:path)
  761. execute 'lcd' a:path
  762. else
  763. let dir = fnamemodify(a:path, ':h')
  764. if isdirectory(dir)
  765. execute 'lcd' dir
  766. else
  767. " Do nothing. E.g. a:path == `scp://foo/bar`
  768. endif
  769. endif
  770. endif
  771. endfunction
  772. " Function: s:cd_to_vcs_root {{{1
  773. function! s:cd_to_vcs_root(path) abort
  774. let dir = fnamemodify(a:path, ':p:h')
  775. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  776. let root = finddir(vcs, dir .';')
  777. if !empty(root)
  778. execute 'cd '. fnameescape(fnamemodify(root, ':h'))
  779. return
  780. endif
  781. endfor
  782. endfunction
  783. " Function: s:close {{{1
  784. function! s:close() abort
  785. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  786. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  787. buffer #
  788. else
  789. bnext
  790. endif
  791. else
  792. quit
  793. endif
  794. endfunction
  795. " Function: s:get_index_as_string {{{1
  796. function! s:get_index_as_string(idx) abort
  797. if exists('g:startify_custom_indices')
  798. let listlen = len(g:startify_custom_indices)
  799. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  800. else
  801. return string(a:idx)
  802. endif
  803. endfunction
  804. " Function: s:print_section_header {{{1
  805. function! s:print_section_header() abort
  806. $
  807. let curline = line('.')
  808. for lnum in range(curline, curline + len(s:last_message) + 1)
  809. call add(b:startify.section_header_lines, lnum)
  810. endfor
  811. call append('$', s:last_message + [''])
  812. unlet s:last_message
  813. endfunction
  814. " Function: s:register {{{1
  815. function! s:register(line, index, type, cmd, path)
  816. let b:startify.entries[a:line] = {
  817. \ 'index': a:index,
  818. \ 'type': a:type,
  819. \ 'line': a:line,
  820. \ 'cmd': a:cmd,
  821. \ 'path': a:path,
  822. \ 'marked': 0,
  823. \ }
  824. endfunction
  825. " Function: s:create_last_session_link {{{1
  826. function! s:create_last_session_link(spath)
  827. if !has('win32') && a:spath !~# '__LAST__$'
  828. let cmd = printf('ln -sf %s %s',
  829. \ shellescape(fnamemodify(a:spath, ':t')),
  830. \ shellescape(s:session_dir .'/__LAST__'))
  831. call system(cmd)
  832. if v:shell_error
  833. call s:warn("Can't create 'last used session' symlink.")
  834. endif
  835. endif
  836. endfunction
  837. " Function: s:init_env {{{1
  838. function! s:init_env()
  839. let s:env = []
  840. let ignore = {
  841. \ 'HOME': 1,
  842. \ 'OLDPWD': 1,
  843. \ 'PWD': 1,
  844. \ }
  845. function! s:get_env()
  846. redir => s
  847. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  848. redir END
  849. redraw
  850. return split(s)
  851. endfunction
  852. function! s:compare_by_key_len(foo, bar)
  853. return len(a:foo[0]) - len(a:bar[0])
  854. endfunction
  855. function! s:compare_by_val_len(foo, bar)
  856. return len(a:bar[1]) - len(a:foo[1])
  857. endfunction
  858. for k in s:get_env()
  859. silent! execute "let v = eval('$'.k)"
  860. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  861. \ || has_key(ignore, k)
  862. \ || len(k) > len(v)
  863. continue
  864. endif
  865. call insert(s:env, [k,v], 0)
  866. endfor
  867. let s:env = sort(s:env, 's:compare_by_key_len')
  868. let s:env = sort(s:env, 's:compare_by_val_len')
  869. endfunction
  870. " Function: s:transform {{{1
  871. function s:transform(absolute_path)
  872. for [k,V] in g:startify_transformations
  873. if a:absolute_path =~ k
  874. return type(V) == type('') ? V : V(a:absolute_path)
  875. endif
  876. unlet V
  877. endfor
  878. return ''
  879. endfunction
  880. " Function: s:warn {{{1
  881. function! s:warn(msg) abort
  882. echohl WarningMsg
  883. echomsg 'startify: '. a:msg
  884. echohl NONE
  885. endfunction