1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-04-14 07:09:11 +08:00

refactor(neoyank): use bundle neoyank.vim

This commit is contained in:
wsdjeg 2022-02-06 22:37:28 +08:00
parent 1c57952d25
commit 7c453d71bf
13 changed files with 475 additions and 6 deletions

View File

@ -51,7 +51,7 @@ function! SpaceVim#layers#ctrlp#plugins() abort
\ ['hara/ctrlp-colorscheme', {'on_cmd' : 'CtrlPColorscheme'}],
\ ]
call add(plugins, ['wsdjeg/ctrlp-menu', {'merged' : 0}])
call add(plugins, ['Shougo/neoyank.vim', {'merged' : 0}])
call add(plugins, [g:_spacevim_root_dir . 'bundle/neoyank.vim', {'merged' : 0}])
call add(plugins, ['wsdjeg/ctrlp-yank', {'merged' : 0}])
call add(plugins, ['wsdjeg/vim-ctrlp-message', {'merged' : 0}])
return plugins

View File

@ -18,7 +18,7 @@ function! SpaceVim#layers#denite#plugins() abort
call add(plugins, ['chemzqm/unite-location', {'merged' : 0}])
call add(plugins, ['Shougo/unite-outline', {'merged' : 0}])
call add(plugins, ['ozelentok/denite-gtags', {'merged' : 0}])
call add(plugins, ['Shougo/neoyank.vim', {'merged' : 0}])
call add(plugins, [g:_spacevim_root_dir . 'bundle/neoyank.vim', {'merged' : 0}])
call add(plugins, ['Shougo/neomru.vim', {'merged' : 0}])
call add(plugins, ['SpaceVim/Denite-sources', {'merged' : 0}])

View File

@ -10,7 +10,7 @@ function! SpaceVim#layers#fuzzy#plugins() abort
let plugins = [
\ ['wsdjeg/vim-fuzzy-search', {'loadconf' : 1, 'merged' : 0}],
\ ]
call add(plugins, ['Shougo/neoyank.vim', {'merged' : 0}])
call add(plugins, [g:_spacevim_root_dir . 'bundle/neoyank.vim', {'merged' : 0}])
return plugins
endfunction

View File

@ -47,7 +47,7 @@ endfunction
function! SpaceVim#layers#fzf#plugins() abort
let plugins = []
call add(plugins, ['junegunn/fzf', { 'merged' : 0}])
call add(plugins, ['Shougo/neoyank.vim', {'merged' : 0}])
call add(plugins, [g:_spacevim_root_dir . 'bundle/neoyank.vim', {'merged' : 0}])
call add(plugins, ['Shougo/neomru.vim', {'merged' : 0}])
call add(plugins, ['SpaceVim/fzf-neoyank', { 'merged' : 0}])
return plugins

View File

@ -57,7 +57,7 @@ function! SpaceVim#layers#leaderf#plugins() abort
\ 'merged' : 0,
\ }])
call add(plugins, ['Shougo/neomru.vim', {'merged' : 0}])
call add(plugins, ['Shougo/neoyank.vim', {'merged' : 0}])
call add(plugins, [g:_spacevim_root_dir . 'bundle/neoyank.vim', { 'merged' : 0}])
" use this repo unicode data
call add(plugins, ['SpaceVim/Unite-sources', {'merged' : 0}])

View File

@ -55,7 +55,7 @@ function! SpaceVim#layers#unite#plugins() abort
" \ ['dyng/ctrlsf.vim',{'on_cmd' : 'CtrlSF', 'on_map' : '<Plug>CtrlSF', 'loadconf' : 1 , 'loadconf_before' : 1}],
" history/yank source <Leader>fh
call add(plugins, ['Shougo/neoyank.vim', {'merged' : 0}])
call add(plugins, [g:_spacevim_root_dir . 'bundle/neoyank.vim', {'merged' : 0}])
" quickfix source <Leader>fq
call add(plugins, ['osyo-manga/unite-quickfix', {'merged' : 0}])
" outline source <Leader>fo

3
bundle/neoyank.vim/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.py[cod]
doc/tags
vim-themis

View File

@ -0,0 +1,21 @@
License: MIT license
AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,214 @@
"=============================================================================
" FILE: neoyank.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
" License: MIT license
"=============================================================================
" Variables
let s:VERSION = '2.0'
let s:yank_histories = {}
let s:yank_histories_old = {}
" the last modified time of the yank histories file.
let s:yank_histories_file_mtime = 0
let s:prev_registers = {}
let s:prev_yankpost_event = {}
let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95')
function! s:set_default(var, val, ...) abort
if !exists(a:var) || type({a:var}) != type(a:val)
let alternate_var = get(a:000, 0, '')
unlet! {a:var}
let {a:var} = exists(alternate_var) ?
\ {alternate_var} : a:val
endif
endfunction
function! s:substitute_path_separator(path) abort
return s:is_windows ? substitute(a:path, '\\', '/', 'g') : a:path
endfunction
let s:base = expand(($XDG_CACHE_HOME != '' ?
\ $XDG_CACHE_HOME . '/neoyank' : '~/.cache/neoyank'), 1)
call s:set_default(
\ 'g:neoyank#file',
\ s:substitute_path_separator(s:base.'/history_yank'))
call s:set_default('g:neoyank#limit', 100)
call s:set_default('g:neoyank#length', 10000)
function! neoyank#default_register_from_clipboard()
if &clipboard == 'unnamed'
return "*"
elseif &clipboard == 'unnamedplus'
return "+"
else
return '"'
endif
endfunction
call s:set_default(
\ 'g:neoyank#save_registers',
\ [neoyank#default_register_from_clipboard()])
function! neoyank#update() abort
call neoyank#_append()
endfunction
function! neoyank#_append() abort
call neoyank#_load()
for register in g:neoyank#save_registers
call s:add_register(register,
\ [getreg(register), getregtype(register)])
endfor
call neoyank#_save()
endfunction
function! neoyank#_yankpost() abort
if v:event == s:prev_yankpost_event
return
endif
for register in g:neoyank#save_registers
call s:add_register(register,
\ [getreg(register), getregtype(register)])
endfor
let s:prev_yankpost_event = v:event
endfunction
function! neoyank#_get_yank_histories() abort
return s:yank_histories
endfunction
function! neoyank#_save() abort
if g:neoyank#file == ''
\ || s:is_sudo()
\ || (exists('g:neoyank#disable_write') && g:neoyank#disable_write)
\ || s:yank_histories ==# s:yank_histories_old
return
endif
call s:writefile(g:neoyank#file,
\ [s:VERSION, s:vim2json(s:yank_histories)])
let s:yank_histories_file_mtime =
\ getftime(g:neoyank#file)
let s:yank_histories_old = copy(s:yank_histories)
endfunction
function! neoyank#_load() abort
if !filereadable(g:neoyank#file)
\ || s:yank_histories_file_mtime == getftime(g:neoyank#file)
return
endif
let file = readfile(g:neoyank#file)
" Version check.
if empty(file) || len(file) != 2 || file[0] !=# s:VERSION
return
endif
try
let yank_histories = s:json2vim(file[1])
catch
unlet! yank_histories
let yank_histories = {}
endtry
if type(yank_histories) != type({})
unlet! yank_histories
let yank_histories = {}
endif
for register in g:neoyank#save_registers
if !has_key(s:yank_histories, register)
let s:yank_histories[register] = []
endif
let s:yank_histories[register] =
\ get(yank_histories, register, []) + s:yank_histories[register]
call s:uniq(register)
endfor
let s:yank_histories_file_mtime =
\ getftime(g:neoyank#file)
endfunction
function! s:add_register(name, reg) abort
" Append register value.
if !has_key(s:yank_histories, a:name)
let s:yank_histories[a:name] = []
endif
if get(s:yank_histories[a:name], 0, []) ==# a:reg
" Skip same register value.
return
endif
let len_history = len(a:reg[0])
" Ignore too long yank.
if len_history < 2 || len_history > g:neoyank#length
\ || a:reg[0] =~ '[\x00-\x08\x10-\x1a\x1c-\x1f]\{3,}'
return
endif
" Error check
try
call s:vim2json(a:reg)
catch
return
endtry
let s:prev_registers[a:name] = a:reg
call insert(s:yank_histories[a:name], a:reg)
call s:uniq(a:name)
endfunction
function! s:uniq(name) abort
let history = s:uniq_by(s:yank_histories[a:name], 'v:val')
if g:neoyank#limit < len(history)
let history = history[ : g:neoyank#limit - 1]
endif
let s:yank_histories[a:name] = history
endfunction
function! s:is_sudo() abort
return $SUDO_USER != '' && $USER !=# $SUDO_USER
\ && $HOME !=# expand('~'.$USER)
\ && $HOME ==# expand('~'.$SUDO_USER)
endfunction
" Removes duplicates from a list.
function! s:uniq_by(list, f) abort
let list = map(copy(a:list), printf('[v:val, %s]', a:f))
let i = 0
let seen = {}
while i < len(list)
let key = string(list[i][1])
if has_key(seen, key)
call remove(list, i)
else
let seen[key] = 1
let i += 1
endif
endwhile
return map(list, 'v:val[0]')
endfunction
function! s:writefile(path, list) abort
let path = fnamemodify(a:path, ':p')
if !isdirectory(fnamemodify(path, ':h'))
call mkdir(fnamemodify(path, ':h'), 'p')
endif
call writefile(a:list, path)
endfunction
function! s:vim2json(expr) abort
return exists('*json_encode') ? json_encode(a:expr) : string(a:expr)
endfunction
function! s:json2vim(expr) abort
sandbox return (exists('*json_encode') ? json_decode(a:expr) : eval(a:expr))
endfunction

View File

@ -0,0 +1,53 @@
"=============================================================================
" FILE: history_yank.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" License: MIT license
"=============================================================================
function! unite#sources#history_yank#define() abort
return s:source
endfunction
let s:source = {
\ 'name' : 'history/yank',
\ 'description' : 'candidates from yank history',
\ 'action_table' : {},
\ 'default_kind' : 'word',
\}
function! s:source.gather_candidates(args, context) abort
let registers = split(get(a:args, 0,
\ neoyank#default_register_from_clipboard()), '\zs')
call neoyank#update()
let candidates = []
for register in registers
let candidates += map(copy(get(
\ neoyank#_get_yank_histories(), register, [])), "{
\ 'word' : v:val[0],
\ 'abbr' : printf('%-2d - %s', v:key,
\ substitute(v:val[0], '\n', '& ', 'g')),
\ 'is_multiline' : 1,
\ 'action__regtype' : v:val[1],
\ }")
endfor
return candidates
endfunction
" Actions
let s:source.action_table.delete = {
\ 'description' : 'delete from yank history',
\ 'is_invalidate_cache' : 1,
\ 'is_quit' : 0,
\ 'is_selectable' : 1,
\ }
function! s:source.action_table.delete.func(candidates) abort
for candidate in a:candidates
call filter(neoyank#_get_yank_histories(),
\ 'v:val[0] !=# candidate.word')
endfor
call neoyank#update()
endfunction

View File

@ -0,0 +1,120 @@
*neoyank.txt* Saves yank history includes unite.vim/denite.nvim source.
Version: 1.0
Author : Shougo <Shougo.Matsu at gmail.com>
License: MIT license
CONTENTS *neoyank-contents*
Usage |neoyank-usage|
Install |neoyank-install|
Configuration Examples |neoyank-examples|
Interface |neoyank-interface|
Variables |neoyank-variables|
Functions |neoyank-functions|
Sources |neoyank-sources|
FAQ |neoyank-faq|
==============================================================================
USAGE *neoyank-usage*
To browse a history of yank registers.
>
:Unite history/yank
<
Or
>
:Denite neoyank
<
==============================================================================
INSTALL *neoyank-install*
Requirements: unite.vim or denite.nvim
https://github.com/Shougo/unite.vim
https://github.com/Shougo/denite.nvim
==============================================================================
INTERFACE *neoyank-interface*
------------------------------------------------------------------------------
VARIABLES *neoyank-variables*
*g:neoyank#limit*
g:neoyank#limit
The maximum number of |unite-source-history/yank| to save.
The default value is 100.
*g:neoyank#length*
g:neoyank#length
The maximum length of register.
The default value is 10000.
*g:neoyank#file*
g:neoyank#file
Specify the file to write the information of yank history.
If this variable is empty, saving history is disabled.
The default value is |g:unite_data_directory|; '/history_yank'
If you want to use another value than what's defined in
|g:unite_data_directory| you have to specify the full path.
Example:
>
let g:neoyank#file = $HOME.'/.vim/yankring.txt'
<
*g:neoyank#save_registers*
g:neoyank#save_registers
Specify the save registers to yank history.
The default value is ['"'].
*g:neoyank#disable_write*
g:neoyank#disable_write
Set to some nonzero value to prohibit neoyank from saving to
the yank history. Intended to prevent leaks from encrypted
files to the unencrypted yank history.
Note: This variable is global. Local variables would not work
as the yank history is shared by all buffers.
Example:
autocmd BufWinEnter \(*.asc\|*.gpg\) let g:neoyank_disable = 1
------------------------------------------------------------------------------
FUNCTIONS *neoyank-functions*
*neoyank#update()*
neoyank#update()
Update neoyank cache file manually.
------------------------------------------------------------------------------
SOURCES *neoyank-sources*
*unite-source-history/yank*
history/yank Nominates yanked words.
Note: Ignores yanked words which are too long.
Source arguments:
1. register names
*denite-source-neoyank*
neoyank Nominates yanked words.
==============================================================================
FAQ *neoyank-faq*
How can I configure the yankring paste position?
You can either pass the insert or append option.
e.g.: >
:Unite history/yank -default-action=append<CR>
<
==============================================================================
vim:tw=78:ts=8:ft=help:norl:noet:fen:

View File

@ -0,0 +1,29 @@
"=============================================================================
" FILE: neoyank.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
" License: MIT license
"=============================================================================
if exists('g:loaded_neoyank')
finish
endif
augroup neoyank
autocmd!
augroup END
if exists('##TextYankPost')
autocmd neoyank FocusGained,FocusLost *
\ silent call neoyank#_append()
autocmd neoyank TextYankPost *
\ silent call neoyank#_yankpost()
else
autocmd neoyank CursorMoved,FocusGained,FocusLost,VimLeavePre *
\ silent call neoyank#_append()
if v:version > 703 || v:version == 703 && has('patch867')
autocmd neoyank TextChanged *
\ silent call neoyank#_append()
endif
endif
let g:loaded_neoyank = 1

View File

@ -0,0 +1,29 @@
# ============================================================================
# FILE: neoyank.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================
from .base import Base
import re
class Source(Base):
def __init__(self, vim):
Base.__init__(self, vim)
self.name = 'neoyank'
self.kind = 'word'
def gather_candidates(self, context):
self.vim.call('neoyank#update')
candidates = []
for [register, history] in self.vim.call(
'neoyank#_get_yank_histories').items():
candidates += [{
'word': register + ': ' + re.sub(r'\n', r'\\n', x[0])[:200],
'action__text': x[0],
'action__regtype': x[1],
} for x in history]
return candidates