mirror of
				https://github.com/amix/vimrc
				synced 2025-10-31 14:43:35 +08:00 
			
		
		
		
	Add markdown preview, update rust.vim
This commit is contained in:
		| @ -0,0 +1,22 @@ | ||||
| " init preview key action | ||||
| function! mkdp#autocmd#init() abort | ||||
|   execute 'augroup MKDP_REFRESH_INIT' . bufnr('%') | ||||
|     autocmd! | ||||
|     " refresh autocmd | ||||
|     if g:mkdp_refresh_slow | ||||
|       autocmd CursorHold,BufWrite,InsertLeave <buffer> call mkdp#rpc#preview_refresh() | ||||
|     else | ||||
|       autocmd CursorHold,CursorHoldI,CursorMoved,CursorMovedI <buffer> call mkdp#rpc#preview_refresh() | ||||
|     endif | ||||
|     " autoclose autocmd | ||||
|     if g:mkdp_auto_close | ||||
|       autocmd BufHidden <buffer> call mkdp#rpc#preview_close() | ||||
|     endif | ||||
|     " server close autocmd | ||||
|     autocmd VimLeave * call mkdp#rpc#stop_server() | ||||
|   augroup END | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#autocmd#clear_buf() abort | ||||
|   execute 'autocmd! ' . 'MKDP_REFRESH_INIT' . bufnr('%') | ||||
| endfunction | ||||
							
								
								
									
										155
									
								
								sources_non_forked/markdown-preview.nvim/autoload/mkdp/rpc.vim
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								sources_non_forked/markdown-preview.nvim/autoload/mkdp/rpc.vim
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,155 @@ | ||||
| let s:mkdp_root_dir = expand('<sfile>:h:h:h') | ||||
| let s:mkdp_opts = {} | ||||
| let s:is_vim = !has('nvim') | ||||
| let s:mkdp_channel_id = s:is_vim ? v:null : -1 | ||||
|  | ||||
| function! s:on_stdout(chan_id, msgs, ...) abort | ||||
|   call mkdp#util#echo_messages('Error', a:msgs) | ||||
| endfunction | ||||
| function! s:on_stderr(chan_id, msgs, ...) abort | ||||
|   call mkdp#util#echo_messages('Error', a:msgs) | ||||
| endfunction | ||||
| function! s:on_exit(chan_id, code, ...) abort | ||||
|   let s:mkdp_channel_id = s:is_vim ? v:null : -1 | ||||
| endfunction | ||||
|  | ||||
| function! s:start_vim_server(cmd) abort | ||||
|   let options = { | ||||
|         \ 'in_mode': 'json', | ||||
|         \ 'out_mode': 'json', | ||||
|         \ 'err_mode': 'nl', | ||||
|         \ 'out_cb': function('s:on_stdout'), | ||||
|         \ 'err_cb': function('s:on_stderr'), | ||||
|         \ 'exit_cb': function('s:on_exit'), | ||||
|         \ 'env': { | ||||
|         \   'VIM_NODE_RPC': 1, | ||||
|         \ } | ||||
|         \} | ||||
|   if has("patch-8.1.350") | ||||
|     let options['noblock'] = 1 | ||||
|   endif | ||||
|   let l:job = job_start(a:cmd, options) | ||||
|   let l:status = job_status(l:job) | ||||
|   if l:status !=# 'run' | ||||
|     echohl Error | echon 'Failed to start vim-node-rpc service' | echohl None | ||||
|     return | ||||
|   endif | ||||
|   let s:mkdp_channel_id = l:job | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#rpc#start_server() abort | ||||
|   let l:mkdp_server_script = s:mkdp_root_dir . '/app/bin/markdown-preview-' . mkdp#util#get_platform() | ||||
|   if executable(l:mkdp_server_script) | ||||
|     let l:cmd = [l:mkdp_server_script, '--path', s:mkdp_root_dir . '/app/server.js'] | ||||
|   elseif executable('node') | ||||
|     let l:mkdp_server_script = s:mkdp_root_dir . '/app/index.js' | ||||
|     let l:cmd = ['node', l:mkdp_server_script, '--path', s:mkdp_root_dir . '/app/server.js'] | ||||
|   endif | ||||
|   if exists('l:cmd') | ||||
|     if s:is_vim | ||||
|       call s:start_vim_server(l:cmd) | ||||
|     else | ||||
|       let l:nvim_optons = { | ||||
|             \ 'rpc': 1, | ||||
|             \ 'on_stdout': function('s:on_stdout'), | ||||
|             \ 'on_stderr': function('s:on_stderr'), | ||||
|             \ 'on_exit': function('s:on_exit') | ||||
|             \ } | ||||
|       let s:mkdp_channel_id = jobstart(l:cmd, l:nvim_optons) | ||||
|     endif | ||||
|   else | ||||
|     call mkdp#util#echo_messages('Error', 'Pre build and node is not found') | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#rpc#stop_server() abort | ||||
|   if s:is_vim | ||||
|     if s:mkdp_channel_id !=# v:null | ||||
|       let l:status = job_status(s:mkdp_channel_id) | ||||
|       if l:status ==# 'run' | ||||
|         call mkdp#rpc#request(s:mkdp_channel_id, 'close_all_pages') | ||||
|         try | ||||
|           call job_stop(s:mkdp_channel_id) | ||||
|         catch /.*/ | ||||
|         endtry | ||||
|       endif | ||||
|     endif | ||||
|     let s:mkdp_channel_id = v:null | ||||
|   else | ||||
|     if s:mkdp_channel_id !=# -1 | ||||
|       call rpcrequest(s:mkdp_channel_id, 'close_all_pages') | ||||
|       try | ||||
|         call jobstop(s:mkdp_channel_id) | ||||
|       catch /.*/ | ||||
|       endtry | ||||
|     endif | ||||
|     let s:mkdp_channel_id = -1 | ||||
|   endif | ||||
|   let b:MarkdownPreviewToggleBool = 0 | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#rpc#get_server_status() abort | ||||
|   if s:is_vim && s:mkdp_channel_id ==# v:null | ||||
|     return -1 | ||||
|   elseif !s:is_vim && s:mkdp_channel_id ==# -1 | ||||
|     return -1 | ||||
|   endif | ||||
|   return 1 | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#rpc#preview_refresh() abort | ||||
|   if s:is_vim | ||||
|     if s:mkdp_channel_id !=# v:null | ||||
|       call mkdp#rpc#notify(s:mkdp_channel_id, 'refresh_content', { 'bufnr': bufnr('%') }) | ||||
|     endif | ||||
|   else | ||||
|     if s:mkdp_channel_id !=# -1 | ||||
|       call rpcnotify(s:mkdp_channel_id, 'refresh_content', { 'bufnr': bufnr('%') }) | ||||
|     endif | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#rpc#preview_close() abort | ||||
|   if s:is_vim | ||||
|     if s:mkdp_channel_id !=# v:null | ||||
|       call mkdp#rpc#notify(s:mkdp_channel_id, 'close_page', { 'bufnr': bufnr('%') }) | ||||
|     endif | ||||
|   else | ||||
|     if s:mkdp_channel_id !=# -1 | ||||
|       call rpcnotify(s:mkdp_channel_id, 'close_page', { 'bufnr': bufnr('%') }) | ||||
|     endif | ||||
|   endif | ||||
|   let b:MarkdownPreviewToggleBool = 0 | ||||
|   call mkdp#autocmd#clear_buf() | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#rpc#open_browser() abort | ||||
|   if s:is_vim | ||||
|     if s:mkdp_channel_id !=# v:null | ||||
|       call mkdp#rpc#notify(s:mkdp_channel_id, 'open_browser', { 'bufnr': bufnr('%') }) | ||||
|     endif | ||||
|   else | ||||
|     if s:mkdp_channel_id !=# -1 | ||||
|       call rpcnotify(s:mkdp_channel_id, 'open_browser', { 'bufnr': bufnr('%') }) | ||||
|     endif | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#rpc#request(clientId, method, ...) abort | ||||
|   let args = get(a:, 1, []) | ||||
|   let res = ch_evalexpr(a:clientId, [a:method, args], {'timeout': 5000}) | ||||
|   if type(res) == 1 && res ==# '' | return '' | endif | ||||
|   let [l:errmsg, res] =  res | ||||
|   if l:errmsg | ||||
|     echohl Error | echon '[rpc.vim] client error: '.l:errmsg | echohl None | ||||
|   else | ||||
|     return res | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#rpc#notify(clientId, method, ...) abort | ||||
|   let args = get(a:000, 0, []) | ||||
|   " use 0 as vim request id | ||||
|   let data = json_encode([0, [a:method, args]]) | ||||
|   call ch_sendraw(s:mkdp_channel_id, data."\n") | ||||
| endfunction | ||||
							
								
								
									
										185
									
								
								sources_non_forked/markdown-preview.nvim/autoload/mkdp/util.vim
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										185
									
								
								sources_non_forked/markdown-preview.nvim/autoload/mkdp/util.vim
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,185 @@ | ||||
| let s:mkdp_root_dir = expand('<sfile>:h:h:h') | ||||
| let s:pre_build = s:mkdp_root_dir . '/app/bin/markdown-preview-' | ||||
| let s:package_file = s:mkdp_root_dir . '/package.json' | ||||
|  | ||||
| " echo message | ||||
| function! mkdp#util#echo_messages(hl, msgs) | ||||
|   if empty(a:msgs) | return | endif | ||||
|   execute 'echohl '.a:hl | ||||
|   if type(a:msgs) ==# 1 | ||||
|     echomsg a:msgs | ||||
|   else | ||||
|     for msg in a:msgs | ||||
|       echom msg | ||||
|     endfor | ||||
|   endif | ||||
|   echohl None | ||||
| endfunction | ||||
|  | ||||
| " echo url | ||||
| function! mkdp#util#echo_url(url) | ||||
|   let l:url = 'Preview page: ' . a:url | ||||
|   call mkdp#util#echo_messages('Type', l:url) | ||||
| endfunction | ||||
|  | ||||
| " try open preview page | ||||
| function! s:try_open_preview_page(timer_id) abort | ||||
|   let l:server_status = mkdp#rpc#get_server_status() | ||||
|   if l:server_status !=# 1 | ||||
|     let s:try_id = '' | ||||
|     call mkdp#rpc#stop_server() | ||||
|     call mkdp#rpc#start_server() | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| " open preview page | ||||
| function! mkdp#util#open_preview_page() abort | ||||
|   if get(s:, 'try_id', '') !=# '' | ||||
|     return | ||||
|   endif | ||||
|   let l:server_status = mkdp#rpc#get_server_status() | ||||
|   if l:server_status ==# -1 | ||||
|     call mkdp#rpc#start_server() | ||||
|   elseif l:server_status ==# 0 | ||||
|     let s:try_id = timer_start(1000, function('s:try_open_preview_page')) | ||||
|   else | ||||
|     call mkdp#util#open_browser() | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| " open browser | ||||
| function! mkdp#util#open_browser() abort | ||||
|   call mkdp#rpc#open_browser() | ||||
|   call mkdp#autocmd#init() | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#util#stop_preview() abort | ||||
|   " TODO: delete autocmd | ||||
|   call mkdp#rpc#stop_server() | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#util#get_platform() abort | ||||
|   if has('win32') || has('win64') | ||||
|     return 'win' | ||||
|   elseif has('mac') || has('macvim') | ||||
|     return 'macos' | ||||
|   endif | ||||
|   return 'linux' | ||||
| endfunction | ||||
|  | ||||
| function! s:on_exit(autoclose, bufnr, Callback, job_id, status, ...) | ||||
|   let content = join(getbufline(a:bufnr, 1, '$'), "\n") | ||||
|   if a:status == 0 && a:autoclose == 1 | ||||
|     execute 'silent! bd! '.a:bufnr | ||||
|   endif | ||||
|   if !empty(a:Callback) | ||||
|     call call(a:Callback, [a:status, a:bufnr, content]) | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#util#open_terminal(opts) abort | ||||
|   if get(a:opts, 'position', 'bottom') ==# 'bottom' | ||||
|     let p = '5new' | ||||
|   else | ||||
|     let p = 'vnew' | ||||
|   endif | ||||
|   execute 'belowright '.p.' +setl\ buftype=nofile ' | ||||
|   setl buftype=nofile | ||||
|   setl winfixheight | ||||
|   setl norelativenumber | ||||
|   setl nonumber | ||||
|   setl bufhidden=wipe | ||||
|   let cmd = get(a:opts, 'cmd', '') | ||||
|   let autoclose = get(a:opts, 'autoclose', 1) | ||||
|   if empty(cmd) | ||||
|     throw 'command required!' | ||||
|   endif | ||||
|   let cwd = get(a:opts, 'cwd', '') | ||||
|   if !empty(cwd) | execute 'lcd '.cwd | endif | ||||
|   let keepfocus = get(a:opts, 'keepfocus', 0) | ||||
|   let bufnr = bufnr('%') | ||||
|   let Callback = get(a:opts, 'Callback', v:null) | ||||
|   if has('nvim') | ||||
|     call termopen(cmd, { | ||||
|           \ 'on_exit': function('s:on_exit', [autoclose, bufnr, Callback]), | ||||
|           \}) | ||||
|   else | ||||
|     call term_start(cmd, { | ||||
|           \ 'exit_cb': function('s:on_exit', [autoclose, bufnr, Callback]), | ||||
|           \ 'curwin': 1, | ||||
|           \}) | ||||
|   endif | ||||
|   if keepfocus | ||||
|     wincmd p | ||||
|   endif | ||||
|   return bufnr | ||||
| endfunction | ||||
|  | ||||
| function! s:markdown_preview_installed(status, ...) abort | ||||
|   if a:status != 0 | ||||
|     call mkdp#util#echo_messages('Error', '[markdown-preview]: install fail') | ||||
|     return | ||||
|   endif | ||||
|   echo '[markdown-preview.nvim]: install completed' | ||||
| endfunction | ||||
|  | ||||
| function! s:trim(str) abort | ||||
|   return substitute(a:str, '\v^(\s|\\n)*|(\s|\\n)*$', '', 'g') | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#util#install(...) | ||||
|   let l:version = mkdp#util#pre_build_version() | ||||
|   let l:info = json_decode(join(readfile(s:mkdp_root_dir . '/package.json'), '')) | ||||
|   if s:trim(l:version) ==# s:trim(l:info.version) | ||||
|     return | ||||
|   endif | ||||
|   let obj = json_decode(join(readfile(s:package_file))) | ||||
|   let cmd = (mkdp#util#get_platform() ==# 'win' ? 'install.cmd' : './install.sh') . ' v'.obj['version'] | ||||
|   if get(a:, '1', v:false) ==# v:true | ||||
|     execute 'lcd ' . s:mkdp_root_dir . '/app' | ||||
|     execute '!' . cmd | ||||
|   else | ||||
|     call mkdp#util#open_terminal({ | ||||
|           \ 'cmd': cmd, | ||||
|           \ 'cwd': s:mkdp_root_dir . '/app', | ||||
|           \ 'Callback': function('s:markdown_preview_installed') | ||||
|           \}) | ||||
|     wincmd p | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#util#install_sync(...) | ||||
|   if get(a:, '1', v:false) ==# v:true | ||||
|     silent call mkdp#util#install(v:true) | ||||
|   else | ||||
|     call mkdp#util#install(v:true) | ||||
|   endif | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#util#pre_build_version() abort | ||||
|   let l:pre_build = s:pre_build . mkdp#util#get_platform() | ||||
|   if has('win32') || has('win64') | ||||
|     let l:pre_build .= '.exe' | ||||
|   endif | ||||
|   if filereadable(l:pre_build) | ||||
|     let l:info = system(l:pre_build . ' --version') | ||||
|     if l:info ==# '' | ||||
|       call mkdp#util#echo_messages('Type', "[markdown-preview.nvim]: Can not execute pre build binary bundle to get version, will download latest pre build binary bundle") | ||||
|       return '' | ||||
|     endif | ||||
|     let l:info = split(l:info, '\n') | ||||
|     return l:info[0] | ||||
|   endif | ||||
|   return '' | ||||
| endfunction | ||||
|  | ||||
| function! mkdp#util#toggle_preview() abort | ||||
|     if !get(b:, 'MarkdownPreviewToggleBool') | ||||
|         call mkdp#util#open_preview_page() | ||||
|         let b:MarkdownPreviewToggleBool=1 | ||||
|     else | ||||
|         call mkdp#util#stop_preview() | ||||
|         let b:MarkdownPreviewToggleBool=0 | ||||
|     endif | ||||
| endfunction | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Kurtis Moxley
					Kurtis Moxley