├── autoload ├── editvar.vim └── unite │ └── sources │ └── variable.vim ├── doc ├── editvar.txt └── unite-variable.txt └── plugin └── editvar.vim /autoload/editvar.vim: -------------------------------------------------------------------------------- 1 | " Edits vim variable in buffer. 2 | " Version: 2.0 3 | " Author : thinca 4 | " License: zlib License 5 | 6 | let s:save_cpo = &cpo 7 | set cpo&vim 8 | 9 | let g:editvar#opener = get(g:, 'editvar#opener', 'new') 10 | let g:editvar#string = get(g:, 'editvar#string', 1) 11 | 12 | function! editvar#open(varname) 13 | let [args, bufnr] = s:normalize(a:varname, bufnr('%')) 14 | if bufnr 15 | let args = bufnr . '/' . args 16 | endif 17 | execute g:editvar#opener '`="editvar://" . args`' 18 | endfunction 19 | 20 | function! editvar#read(bufname) 21 | call s:do_cmd(a:bufname, 'read') 22 | endfunction 23 | 24 | function! editvar#write(bufname) 25 | call s:do_cmd(a:bufname, 'write') 26 | endfunction 27 | 28 | function! s:normalize(var, defbufnr) 29 | let bufnr = 0 30 | let varname = a:var 31 | if varname =~# '^\d\+/' 32 | let bufnr = matchstr(varname, '^\d\+') - 0 33 | let varname = matchstr(varname, '^\d\+/\zs.*') 34 | endif 35 | if varname !~# '^@.$' && varname[1] !=# ':' 36 | let varname = (bufnr ? 'b:' : 'g:') . varname 37 | endif 38 | if varname[: 1] ==# 'b:' && bufnr == 0 39 | let bufnr = a:defbufnr 40 | endif 41 | return [varname, bufnr] 42 | endfunction 43 | 44 | function! s:do_cmd(bufname, method) abort 45 | let varname = matchstr(a:bufname, 'editvar://\zs.*') 46 | let bufnr = get(b:, 'editvar_bufnr', bufnr('#')) 47 | let [varname, bufnr] = s:normalize(varname, bufnr) 48 | 49 | let name = matchstr(varname, '^\%(@.\|[[:alnum:]_:#.]\+\)$') 50 | if name ==# '' 51 | throw 'editvar: Invalid variable name: ' . varname 52 | endif 53 | 54 | if a:method ==# 'read' 55 | if bufnr 56 | let value = getbufvar(bufnr, '') 57 | for n in split(name[2 :], '\.') 58 | if !has_key(value, n) 59 | unlet! value 60 | break 61 | endif 62 | let l:['value'] = value[n] 63 | endfor 64 | else 65 | try 66 | let value = eval(name) 67 | catch /^Vim(let):E121:/ 68 | endtry 69 | endif 70 | setlocal buftype=acwrite 71 | let b:editvar_string = 0 72 | if exists('value') 73 | if g:editvar#string && type(value) == type('') 74 | let b:editvar_string = g:editvar#string 75 | let str = value 76 | if str =~# "\n$" 77 | let str .= "\n" 78 | endif 79 | let b:editvar_sep = "\n" 80 | elseif exists('*PP') 81 | let pp_string = g:prettyprint_string 82 | let g:prettyprint_string = ['split', 'raw'] 83 | try 84 | let str = PP(value) 85 | finally 86 | let g:prettyprint_string = pp_string 87 | endtry 88 | else 89 | let str = string(value) 90 | let b:editvar_sep = "\n" 91 | endif 92 | silent put =str 93 | silent 1 delete _ 94 | endif 95 | if !b:editvar_string 96 | setlocal filetype=vim 97 | endif 98 | 99 | elseif a:method ==# 'write' 100 | let valstr = join(getline(1, '$'), get(b:, 'editvar_sep', '')) 101 | if b:editvar_string 102 | let value = valstr 103 | elseif valstr =~# '\S' 104 | sandbox let value = eval(valstr) 105 | endif 106 | if bufnr 107 | call s:write_var(getbufvar(bufnr, ''), name[2 :], l:) 108 | elseif name =~# '^@.$' 109 | execute 'let' name '= value' 110 | else 111 | call s:write_var(g:, name[2 :], l:) 112 | endif 113 | endif 114 | setlocal nomodified 115 | endfunction 116 | 117 | function! s:write_var(dict, name, l) 118 | let dict = a:dict 119 | let names = split(a:name, '\.') 120 | for n in names[: -2] 121 | if !has_key(dict, n) || type(dict[n]) != type({}) 122 | let dict[n] = {} 123 | endif 124 | let dict = dict[n] 125 | endfor 126 | let key = empty(names) ? '' : names[-1] 127 | if has_key(a:l, 'value') 128 | if key ==# '' 129 | call extend(dict, a:l.value) 130 | else 131 | let dict[key] = a:l.value 132 | endif 133 | elseif has_key(dict, key) 134 | call remove(dict, key) 135 | endif 136 | endfunction 137 | 138 | let &cpo = s:save_cpo 139 | -------------------------------------------------------------------------------- /autoload/unite/sources/variable.vim: -------------------------------------------------------------------------------- 1 | " unite source: variable 2 | " Version: 1.0 3 | " Author : thinca 4 | " License: zlib License 5 | 6 | let s:save_cpo = &cpo 7 | set cpo&vim 8 | 9 | let s:source = { 10 | \ 'name': 'variable', 11 | \ 'description': 'candidates from variable', 12 | \ 'hooks': {}, 13 | \ 'default_action': 'edit', 14 | \ 'action_table': { 15 | \ 'edit': { 16 | \ 'description': 'edit the variable', 17 | \ }, 18 | \ 'preview': { 19 | \ 'description': 'preview the variable', 20 | \ 'is_quit': 0, 21 | \ }, 22 | \ 'delete': { 23 | \ 'description': 'unlet the variables', 24 | \ 'is_quit' : 0, 25 | \ 'is_selectable': 1, 26 | \ 'is_invalidate_cache': 1, 27 | \ }, 28 | \ }, 29 | \ } 30 | 31 | function! s:source.hooks.on_init(args, context) 32 | let var = get(a:args, 0, 'g:') 33 | let prefix = var 34 | if prefix[-1 :] !=# ':' 35 | let prefix .= '.' 36 | endif 37 | let a:context.source__prefix = prefix 38 | let a:context.source__target = eval(var) 39 | endfunction 40 | 41 | function! s:source.gather_candidates(args, context) 42 | let target = a:context.source__target 43 | let result = [] 44 | for key in keys(target) 45 | let text = s:string(target[key]) 46 | let result += [{ 47 | \ "word": key, 48 | \ "abbr": a:context.source__prefix . key . " = " . text, 49 | \ "action__name": a:context.source__prefix . key, 50 | \ "action__text": text, 51 | \ }] 52 | endfor 53 | return unite#util#sort_by(result, 'v:val.word') 54 | endfunction 55 | 56 | function! s:source.action_table.edit.func(candidate) 57 | call editvar#open(a:candidate.action__name) 58 | endfunction 59 | function! s:source.action_table.preview.func(candidate) 60 | pedit `='editvar://' . a:candidate.action__name` 61 | endfunction 62 | function! s:source.action_table.delete.func(candidates) 63 | execute 'unlet!' join(map(copy(a:candidates), 'v:val.action__name')) 64 | endfunction 65 | 66 | 67 | function! s:string(expr) 68 | try 69 | return string(a:expr) 70 | catch /^Vim(return):/ 71 | endtry 72 | try 73 | redir => result 74 | silent echon a:expr 75 | redir END 76 | return result 77 | catch 78 | return v:exception 79 | endtry 80 | endfunction 81 | 82 | function! unite#sources#variable#define() 83 | return s:source 84 | endfunction 85 | 86 | let &cpo = s:save_cpo 87 | -------------------------------------------------------------------------------- /doc/editvar.txt: -------------------------------------------------------------------------------- 1 | *editvar.txt* Edits vim variable in buffer. 2 | 3 | Version: 2.0 4 | Author : thinca 5 | License: zlib License 6 | 7 | ============================================================================== 8 | CONTENTS *editvar-contents* 9 | 10 | INTRODUCTION |editvar-introduction| 11 | INTERFACE |editvar-interface| 12 | COMMANDS |editvar-commands| 13 | FUNCTIONS |editvar-functions| 14 | PATHS |editvar-paths| 15 | CUSTOMIZING |editvar-customizing| 16 | ISSUES |editvar-issues| 17 | CHANGELOG |editvar-changelog| 18 | 19 | 20 | 21 | ============================================================================== 22 | INTRODUCTION *editvar-introduction* 23 | 24 | *editvar* is a Vim plugin to edit vim variable in buffer. 25 | 26 | Requirements: 27 | - Vim 7.3 or later 28 | 29 | Optional: 30 | - prettyprint.vim (https://github.com/thinca/vim-prettyprint) 31 | - unite.vim (https://github.com/Shougo/unite.vim) 32 | - |unite-variable| 33 | 34 | Latest version: 35 | https://github.com/thinca/vim-editvar 36 | 37 | 38 | 39 | ============================================================================== 40 | INTERFACE *editvar-interface* 41 | 42 | ------------------------------------------------------------------------------ 43 | COMMANDS *editvar-commands* 44 | 45 | :Editvar {var-name} *:Editvar* 46 | Opens a buffer to edit a vim variable. You can update the value of 47 | variable by |:write|. 48 | {var-name} is like followings. 49 | - var_name (This is global variable) 50 | - g:TheVariable 51 | - g:editvar#opener 52 | - g:foo.bar.buz 53 | - b:var_name 54 | - 10/b:var_name 55 | - b: 56 | The form of dictionary value like g:dict["key"] is not supported. 57 | 58 | ------------------------------------------------------------------------------ 59 | FUNCTIONS *editvar-functions* 60 | 61 | editvar#open({var-name}) *editvar#open()* 62 | Function version of |:Editvar|. 63 | 64 | ------------------------------------------------------------------------------ 65 | PATHS *editvar-paths* 66 | 67 | The following buffer names are processed by |editvar| plugin. 68 | 69 | editvar://{var-name} 70 | A buffer to edit a global vim variable. 71 | 72 | editvar://{bufnr}/[b:]{var-name} 73 | A buffer to edit a buffer local vim variable. 74 | 75 | editvar://@{reg-name} 76 | A buffer to edit a |registers|. 77 | 78 | 79 | 80 | ============================================================================== 81 | CUSTOMIZING *editvar-customizing* 82 | 83 | g:editvar#opener *g:editvar#opener* 84 | |:Editvar| opens a buffer with this command. 85 | The default value is |:new|. 86 | 87 | g:editvar#string *g:editvar#string* 88 | If this value is true, enables string mode. 89 | If value of variable is string, you can edit it directly(not literal). 90 | The default value is 1. 91 | 92 | 93 | 94 | ============================================================================== 95 | ISSUES *editvar-issues* 96 | 97 | - Can't treat |numbered-function|. 98 | - ex) function('1') 99 | - This limitation is by Vim. 100 | - Can't treat nested structure. 101 | 102 | 103 | 104 | ============================================================================== 105 | CHANGELOG *editvar-changelog* 106 | 107 | 2.0 2012-02-21 108 | - Supported the autoload variable which has not loaded. 109 | - Added |unite-variable|. 110 | - Added |registers| support. 111 | - Added dictionary's value support. 112 | - Added |g:editvar#string| option. 113 | - Changed path for buffer local variable. 114 | 115 | 1.1 2011-11-21 116 | - |:unlet| by empty buffer. 117 | 118 | 1.0 2011-11-19 119 | - Initial version. 120 | 121 | 122 | ============================================================================== 123 | vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl 124 | -------------------------------------------------------------------------------- /doc/unite-variable.txt: -------------------------------------------------------------------------------- 1 | *unite-variable.txt* unite source: variable 2 | 3 | Version: 1.0 4 | Author : thinca 5 | License: zlib License 6 | 7 | ============================================================================== 8 | CONTENTS *unite-variable-contents* 9 | 10 | INTRODUCTION |unite-variable-introduction| 11 | SOURCES |unite-variable-sources| 12 | ACTIONS |unite-variable-actions| 13 | ISSUES |unite-variable-issues| 14 | CHANGELOG |unite-variable-changelog| 15 | 16 | 17 | 18 | ============================================================================== 19 | INTRODUCTION *unite-variable-introduction* 20 | 21 | *unite-variable* is a source of |unite| for vim variables. 22 | 23 | Requirements: 24 | - Vim 7.3 or later 25 | - unite.vim (https://github.com/Shougo/unite.vim) 26 | 27 | 28 | 29 | ============================================================================== 30 | SOURCES *unite-variable-sources* 31 | 32 | *unite-source-variable* 33 | variable Nominates |global-variable| as candidates. 34 | 35 | 36 | 37 | ============================================================================== 38 | ACTIONS *unite-variable-actions* 39 | 40 | variable *unite-action-variable* 41 | edit Edits this variable with |editvar| plugin. 42 | delete Unlets the variables. 43 | preview Previews the variable. 44 | 45 | 46 | 47 | ============================================================================== 48 | ISSUES *unite-variable-issues* 49 | 50 | - preview action doesn't work well with -auto-preview and some case. 51 | 52 | 53 | 54 | ============================================================================== 55 | CHANGELOG *unite-variable-changelog* 56 | 57 | 1.0 2012-02-20 58 | - Initial version. 59 | 60 | 61 | ============================================================================== 62 | vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl 63 | -------------------------------------------------------------------------------- /plugin/editvar.vim: -------------------------------------------------------------------------------- 1 | " Edits vim variable in buffer. 2 | " Version: 2.0 3 | " Author : thinca 4 | " License: zlib License 5 | 6 | if exists('g:loaded_editvar') 7 | finish 8 | endif 9 | let g:loaded_editvar = 1 10 | 11 | let s:save_cpo = &cpo 12 | set cpo&vim 13 | 14 | augroup plugin-editvar 15 | autocmd! 16 | autocmd BufReadCmd editvar://* call editvar#read(expand('')) 17 | autocmd BufWriteCmd editvar://* call editvar#write(expand('')) 18 | augroup END 19 | 20 | command! -bar -nargs=1 -complete=var Editvar call editvar#open() 21 | 22 | let &cpo = s:save_cpo 23 | unlet s:save_cpo 24 | --------------------------------------------------------------------------------