spacevim/autoload/SpaceVim/layers/tools/mpv.vim
JIe 2bb7059579
Some checks failed
Detach Plugins / check (FlyGrep.vim) (push) Has been cancelled
Detach Plugins / check (GitHub.vim) (push) Has been cancelled
Detach Plugins / check (JavaUnit.vim) (push) Has been cancelled
Detach Plugins / check (SourceCounter.vim) (push) Has been cancelled
Detach Plugins / check (cpicker.nvim) (push) Has been cancelled
Detach Plugins / check (dein-ui.vim) (push) Has been cancelled
Detach Plugins / check (git.vim) (push) Has been cancelled
Detach Plugins / check (iedit.vim) (push) Has been cancelled
Detach Plugins / check (scrollbar.vim) (push) Has been cancelled
Detach Plugins / check (vim-chat) (push) Has been cancelled
Detach Plugins / check (vim-cheat) (push) Has been cancelled
Detach Plugins / check (vim-todo) (push) Has been cancelled
Detach Plugins / check (xmake.vim) (push) Has been cancelled
test / Linux (nvim, nightly) (push) Has been cancelled
test / Linux (nvim, v0.3.8) (push) Has been cancelled
test / Linux (nvim, v0.4.0) (push) Has been cancelled
test / Linux (nvim, v0.4.2) (push) Has been cancelled
test / Linux (nvim, v0.4.3) (push) Has been cancelled
test / Linux (nvim, v0.4.4) (push) Has been cancelled
test / Linux (nvim, v0.5.0) (push) Has been cancelled
test / Linux (nvim, v0.5.1) (push) Has been cancelled
test / Linux (nvim, v0.6.0) (push) Has been cancelled
test / Linux (nvim, v0.6.1) (push) Has been cancelled
test / Linux (nvim, v0.7.0) (push) Has been cancelled
test / Linux (nvim, v0.7.2) (push) Has been cancelled
test / Linux (nvim, v0.8.0) (push) Has been cancelled
test / Linux (nvim, v0.8.1) (push) Has been cancelled
test / Linux (nvim, v0.8.2) (push) Has been cancelled
test / Linux (nvim, v0.8.3) (push) Has been cancelled
test / Linux (nvim, v0.9.0) (push) Has been cancelled
test / Linux (nvim, v0.9.1) (push) Has been cancelled
test / Linux (true, vim, v7.4.052) (push) Has been cancelled
test / Linux (true, vim, v7.4.1689) (push) Has been cancelled
test / Linux (true, vim, v7.4.629) (push) Has been cancelled
test / Linux (true, vim, v8.0.0027) (push) Has been cancelled
test / Linux (true, vim, v8.0.0183) (push) Has been cancelled
test / Linux (vim, nightly) (push) Has been cancelled
test / Linux (vim, v8.0.0184) (push) Has been cancelled
test / Linux (vim, v8.0.1453) (push) Has been cancelled
test / Linux (vim, v8.1.2269) (push) Has been cancelled
test / Linux (vim, v8.2.2434) (push) Has been cancelled
test / Linux (vim, v8.2.3995) (push) Has been cancelled
test / Windows (nvim, nightly) (push) Has been cancelled
test / Windows (nvim, v0.3.8) (push) Has been cancelled
test / Windows (nvim, v0.4.2) (push) Has been cancelled
test / Windows (nvim, v0.4.3) (push) Has been cancelled
test / Windows (nvim, v0.4.4) (push) Has been cancelled
test / Windows (nvim, v0.5.0) (push) Has been cancelled
test / Windows (nvim, v0.5.1) (push) Has been cancelled
test / Windows (nvim, v0.6.0) (push) Has been cancelled
test / Windows (nvim, v0.6.1) (push) Has been cancelled
test / Windows (nvim, v0.7.0) (push) Has been cancelled
test / Windows (nvim, v0.7.2) (push) Has been cancelled
test / Windows (nvim, v0.8.0) (push) Has been cancelled
test / Windows (nvim, v0.8.1) (push) Has been cancelled
test / Windows (nvim, v0.8.2) (push) Has been cancelled
test / Windows (nvim, v0.8.3) (push) Has been cancelled
test / Windows (nvim, v0.9.0) (push) Has been cancelled
test / Windows (nvim, v0.9.1) (push) Has been cancelled
test / Windows (vim, nightly) (push) Has been cancelled
test / Windows (vim, v7.4.1185) (push) Has been cancelled
test / Windows (vim, v7.4.1689) (push) Has been cancelled
test / Windows (vim, v8.0.0027) (push) Has been cancelled
test / Windows (vim, v8.0.1453) (push) Has been cancelled
test / Windows (vim, v8.1.2269) (push) Has been cancelled
test / Windows (vim, v8.2.2434) (push) Has been cancelled
test / Windows (vim, v8.2.3995) (push) Has been cancelled
docker / docker (push) Has been cancelled
mirror / check (coding) (push) Has been cancelled
mirror / check (gitee) (push) Has been cancelled
mirror / check (gitlab) (push) Has been cancelled
init
2024-08-21 14:17:26 +08:00

170 lines
4.6 KiB
VimL

"=============================================================================
" mpv.vim --- mpv layer for SpaceVim
" Copyright (c) 2016-2023 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
""
" @section tools#mpv, layers-tools-mpv
" @parentsection layers
" The `tools#mpv` layer provides mpv integration for SpaceVim.
" this layer is disabled by default, to enable this layer, add following
" snippet to your SpaceVim configuration file.
" >
" [[layers]]
" name = 'tools#mpv'
" <
"
" @subsection layer options
"
" The following options can be used with this layer:
"
" 1. `mpv_interpreter`: set the path of `mpv`.
" 2. `musics_directory`: set the path of directory where to store musics. The
" default directory is `~/Music`
"
" @subsection Key bindings
" >
" Key Function
" ---------------------------------------------
" SPC m m l fuzzy find musics
" SPC m m n next music
" SPC m m s stop mpv
" <
if exists('s:musics_directory')
finish
else
let s:musics_directory = '~/Music'
let s:mpv_interpreter = 'mpv'
let s:loop_mode = 'random'
let s:stop_mpv = 0
endif
let s:JOB = SpaceVim#api#import('job')
let s:NUM = SpaceVim#api#import('data#number')
let s:CMP = SpaceVim#api#import('vim#compatible')
function! SpaceVim#layers#tools#mpv#config() abort
let g:_spacevim_mappings_space.m.m = {'name' : '+mpv'}
call SpaceVim#mapping#space#def('nnoremap', ['m', 'm', 'l'], 'call call('
\ . string(s:_function('s:list_music')) . ', [])',
\ 'fuzzy-find-musics', 1)
call SpaceVim#mapping#space#def('nnoremap', ['m', 'm', 'n'], 'call call('
\ . string(s:_function('s:next')) . ', [])',
\ 'next-music', 1)
call SpaceVim#mapping#space#def('nnoremap', ['m', 'm', 's'], 'call call('
\ . string(s:_function('s:stop')) . ', [])',
\ 'stop-mpv', 1)
endfunction
function! s:list_music() abort
call s:load_musics()
if SpaceVim#layers#isLoaded('leaderf')
Leaderf menu --name MpvPlayer
elseif SpaceVim#layers#isLoaded('denite')
Denite menu:MpvPlayer
else
endif
endfunction
function! SpaceVim#layers#tools#mpv#set_variable(var) abort
let s:musics_directory = get(a:var, 'musics_directory', 1)
let s:mpv_interpreter = get(a:var, 'mpv_interpreter', 1)
endfunction
function! SpaceVim#layers#tools#mpv#play(fpath) abort
let s:stop_mpv = 0
if s:playId != 0
call s:JOB.stop(s:playId)
let s:playId = 0
endif
let s:playId = s:JOB.start([s:mpv_interpreter,'--vid=no',a:fpath],{
\ 'on_stdout': function('s:handler'),
\ 'on_stderr': function('s:handler'),
\ 'on_exit': function('s:handler'),
\ })
command! MStop call s:stop()
endfunction
function! s:load_musics() abort
let musics = s:CMP.globpath(s:musics_directory, '*.mp3')
let g:unite_source_menu_menus.MpvPlayer.command_candidates = []
for m in musics
call add(g:unite_source_menu_menus.MpvPlayer.command_candidates,
\ [fnamemodify(m, ':t'),
\ "call SpaceVim#layers#tools#mpv#play('" . m . "')"])
endfor
endfunction
function! SpaceVim#layers#tools#mpv#health() abort
call SpaceVim#layers#tools#mpv#config()
return 1
endfunction
function! SpaceVim#layers#tools#mpv#loadable() abort
return 1
endfunction
function! SpaceVim#layers#tools#mpv#plugins() abort
return []
endfunction
function! SpaceVim#layers#tools#mpv#get_options() abort
return ['musics_directory', 'mpv_interpreter']
endfunction
let s:playId = 0
fu! s:handler(id, data, event) abort
if a:event ==# 'exit'
if s:loop_mode ==# 'random' && !s:stop_mpv
let next = s:NUM.random(0, len(g:unite_source_menu_menus.MpvPlayer.command_candidates))
echohl TODO
echo 'playing:' . g:unite_source_menu_menus.MpvPlayer.command_candidates[next][0]
echohl NONE
exe g:unite_source_menu_menus.MpvPlayer.command_candidates[next][1]
endif
endif
endf
function! s:stop() abort
if s:playId != 0
call s:JOB.stop(s:playId)
let s:playId = 0
endif
delcommand MStop
let s:stop_mpv = 1
endfunction
function! s:next() abort
if s:playId != 0
call s:JOB.stop(s:playId)
let s:playId = 0
endif
endfunction
" function() wrapper
if v:version > 703 || v:version == 703 && has('patch1170')
function! s:_function(fstr) abort
return function(a:fstr)
endfunction
else
function! s:_SID() abort
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
endfunction
let s:_s = '<SNR>' . s:_SID() . '_'
function! s:_function(fstr) abort
return function(substitute(a:fstr, 's:', s:_s, 'g'))
endfunction
endif