1
0
mirror of https://github.com/amix/vimrc synced 2025-06-16 01:25:00 +08:00

lets try again...

This commit is contained in:
amix
2012-08-16 23:41:25 -04:00
parent 85888ddbd3
commit bb9c85e523
306 changed files with 47995 additions and 21 deletions

View File

@ -0,0 +1,24 @@
" buf_identifier is either a buf_nr or a filename
" If any window shows the buffer move to the buffer
" If not show it in current window (by c-w s c^ you can always
" reshow the last buffer
"
" Example: buf_utils#GotoBuf("/tmp/tfile.txt", {'create': 1})
" returns: The command which was used to switch to the buffer
fun! buf_utils#GotoBuf(buf_identifier, opts)
let buf_nr = bufnr(a:buf_identifier)
if buf_nr == -1 && ( get(a:opts, 'create', 0) || has_key(a:opts, 'create_cmd'))
exec get(a:opts,'create_cmd','e').' '.fnameescape(a:buf_identifier)
return "e"
else
let win_nr = bufwinnr(buf_nr)
if win_nr == -1
exec 'b '.buf_nr
return "b"
else
exec win_nr.'wincmd w'
return "w"
endif
wincmd w"
endif
endf

View File

@ -0,0 +1,104 @@
" cached_file_contents.vim
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Last Change: 2010-01-03.
" @Revision: 0.3.0
"exec vam#DefineAndBind('s:c','g:cache_dir_options','{}')
if !exists('g:cache_dir_options') | let g:cache_dir_options = {} | endif | let s:c = g:cache_dir_options
let s:c['cache_dir'] = get(s:c, 'cache_dir', expand('$HOME').'/.vim-cache')
let s:c['scanned_files'] = get(s:c, 'scanned_files', {})
let s:scanned_files = s:c['scanned_files']
let s:define_cache_file = "let this_dir = s:c['cache_dir'].'/cached-file-contents' | let cache_file = expand(this_dir.'/'.substitute(string([func_as_string, a:file]),'[[\\]{}:/\\,''\"# ]\\+','_','g'))"
" read a file, run function to extract contents and cache the result returned
" by that function in memory. Optionally the result can be cached on disk as
" because VimL can be slow!
"
" file : the file to be read
" func: { 'func': function which will be called by funcref#Call
" , 'version' : if this version changes cache will be invalidate automatically
" , 'ftime_check': optional, default 1. if set to 0 cache isn't updated when file changes and file is in cache
" }
"
" default: what to return if file doesn't exist
" think twice about adding lines. This function is called many times.
function! cached_file_contents#CachedFileContents(file, func, ...) abort
let ignore_ftime = a:0 > 0 ? a:1 : 0
" using string for default so that is evaluated when needed only
let use_file_cache = get(a:func, 'use_file_cache', 0)
" simple kind of normalization. necessary when using file caching
" this seems to be slower:
" let file = fnamemodify(a:file, ':p') " simple kind of normalization. necessary when using file caching
" / = assume its an absolute path
" let file = a:file[0] == '/' ? a:file : expand(a:file, ':p')
let file = a:file[0] == '/' ? a:file : fnamemodify(a:file, ':p') " simple kind of normalization. necessary when using file caching
let func_as_string = string(a:func['func'])
if (!has_key(s:scanned_files, func_as_string))
let s:scanned_files[func_as_string] = {}
endif
let dict = s:scanned_files[func_as_string]
if use_file_cache && !has_key(dict, a:file)
exec s:define_cache_file
if filereadable(cache_file)
let dict[file] = eval(readfile(cache_file,'b')[0])
endif
endif
if has_key(dict, a:file)
let d = dict[a:file]
if use_file_cache
\ && (ignore_ftime || getftime(a:file) <= d['ftime'])
\ && d['version'] == a:func['version']
return dict[a:file]['scan_result']
endif
endif
let scan_result = funcref#Call(a:func['func'], [a:file] )
let dict[a:file] = {"ftime": getftime(a:file), 'version': a:func['version'], "scan_result": scan_result }
if use_file_cache
if !exists('cache_file') | exec s:define_cache_file | endif
if !isdirectory(this_dir) | call mkdir(this_dir,'p',0700) | endif
call writefile([string(dict[a:file])], cache_file)
endif
return scan_result
endfunction
fun! cached_file_contents#ClearScanCache()
let s:c['scanned_files'] = {}
" Don't run rm -fr. Ask user to run it. It cache_dir may have been set to
" $HOME ! (should nevere be the case but who knows
echoe "run manually in your shell: rm -fr ".shellescape(s:c['cache_dir'])."/*"
endf
fun! cached_file_contents#Test()
" usually you use a global option so that the function can be reused
let my_interpreting_func = {'func' : funcref#Function('return len(readfile(ARGS[0]))'), 'version': 2, 'use_file_cache':1}
let my_interpreting_func2 = {'func' : funcref#Function('return ARGS[0]') , 'version': 2, 'use_file_cache':1}
let tmp = tempname()
call writefile(['some text','2nd line'], tmp)
let r = [ cached_file_contents#CachedFileContents(tmp, my_interpreting_func)
\ , cached_file_contents#CachedFileContents(tmp, my_interpreting_func2) ]
if r != [2, tmp]
throw "test failed 1, got ".string(r)
endif
unlet r
sleep 3
" now let's change contents
call writefile(['some text','2nd line','3rd line'], tmp)
let r = cached_file_contents#CachedFileContents(tmp, my_interpreting_func)
if 3 != r
throw "test failed 2, got ".string(r)
endif
echo "test passed"
endf

View File

@ -0,0 +1,12 @@
" in sh/bash you can type export to get a list of environment variables
" This function assigns those env vars to Vim.
" Does not delete env vars yet
" Example: env_reload#ReloadEnv(system("sh -c 'export'")
fun! env_reload#ReloadEnv(bash_export_command_output)
for i in split(a:bash_export_command_output,"\n")
let m = matchlist(i, 'export \([^=]\+\)="\(.*\)"')
if empty(m) | continue | endif
" don't care about quoted values right now.
exec 'let $'.m[1].'='.string(m[2])
endfor
endf

View File

@ -0,0 +1,95 @@
" funcref.vim
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Last Change: 2010-01-03.
" @Revision: 0.1.0
" documentation see doc/funcref.txt
" usage:
" funcref#Function("filename#Function")
" optionally pass arguments:
" funcref#Function("filename#Function",{'args': [2]})
" optionally define self:
" funcref#Function("filename#Function",{'self': object})
function! funcref#Function(name,...)
let d = a:0 > 0 ? a:1 : {}
let d['faked_function_reference'] = a:name
return d
endfunction
" args : same as used for call(f,[args], self)
" f must be either
" - a string which can be evaled (use "return 'value'" to return a value)
" - a Vim function reference created by function('..')
" - a faked function reference created by funcref#Function(..)
"
" the last "self" argument can be overriden by the function reference
" You can pass arguments in a closure like style
function! funcref#Call(...)
let args = copy(a:000)
" add parameters:
if (len(args) < 2)
call add(args, [])
endif
let isDict = type(args[0]) == type({})
" prepend parameters which were passed by faked function reference:
if isDict && has_key(args[0], 'args')
let args[1] = args[0]['args']+args[1]
endif
" always pass self. this way you can call functions from dictionaries not
" refering to self
if (len(args) < 3)
call add(args, {})
endif
" the funcref overrides self:
if isDict && has_key(args[0], 'self')
let args[2] = args[0]['self']
endif
if type(a:1) == 2
" funcref: function must have been laoded
return call(function('call'), args)
elseif isDict && has_key(args[0], 'faked_function_reference')
let Fun = args[0]['faked_function_reference']
if type(Fun) == type('')
\ && (Fun[:len('return ')-1] == 'return '
\ || Fun[:len('call ')-1] == 'call '
\ || Fun[:len('if ')-1] == 'if '
\ || Fun[:len('let ')-1] == 'let '
\ || Fun[:len('echo ')-1] == 'echo '
\ || Fun[:len('exec ')-1] == 'exec '
\ || Fun[:len('debug ')-1] == 'debug ')
" it doesn't make sense to list all vim commands here
" So if you want to execute another action consider using
" funcref#Function('exec '.string('aw')) or such
" function is a String, call exec
let ARGS = args[1]
let SELF = args[2]
exec Fun
else
" pseudo function, let's load it..
if type(Fun) == 1
if !exists('*'.Fun)
" lazily load function
let file = substitute(substitute(Fun,'#[^#]*$','',''),'#','/','g')
exec 'runtime /autoload/'.file.'.vim'
endif
let Fun2 = function(Fun)
else
let Fun2 = Fun
endif
let args[0] = Fun
return call(function('call'), args)
endif
else
" no function, return the value
return args[0]
endif
endfunction

View File

@ -0,0 +1,27 @@
exec vam#DefineAndBind('s:c','g:glob_like', '{}')
" ignore vcs stuff, Don't think you want those..
let s:c['regex_ignore_directory'] = '\<\%([_.]darcs\|\.git\|.svn\|.hg\|.cvs\|.bzr\)\>'
let s:c['glob_cache'] = get(s:c, 'glob_cache', {})
let s:glob_cache = s:c['glob_cache']
fun! glob#Glob(pattern, ...)
let pattern = a:pattern
if pattern[0] == '~'
let pattern = $HOME.pattern[1:]
endif
let opts = a:0 > 0 ? a:1 : {}
" never cache current directory. You're very likely to edit files in it.
let c = getcwd()
let cachable = get(opts, 'cachable', 0) && pattern[:len(c)-1] != c
if cachable && has_key(s:glob_cache, pattern)
return s:glob_cache[pattern]
endif
" FIXME: don't recurse into \.git directory (thus reimplement glob in vimL!)
let r = filter(split(glob(pattern),"\n"),'v:val !~ '.string(s:c['regex_ignore_directory']))
if cachable | let s:glob_cache[pattern] = r | endif
return r
endf

View File

@ -0,0 +1,19 @@
" vim suffers:
exec vam#DefineAndBind('s:c','g:vim_tiny_cmd', '{}')
fun! tiny_cmd#Put(a)
let new = get(s:c,'next',0) +1
let s:c['next'] = new
let s:c[new] = a:a
return new
endf
fun! tiny_cmd#Get(nr)
return s:c[a:nr]
endf
" Get and remove item
fun! tiny_cmd#Pop(nr)
let r = s:c[a:nr] | unlet s:c[a:nr] | return r
endf