Permalink
Browse files

Version 0.1.0: Initial upload

  • Loading branch information...
0 parents commit e2f3c0e0c6c2f8d58262dd1b3ba860f1d171e4f1 thinca committed with Jan 7, 2011
Showing with 272 additions and 0 deletions.
  1. +25 −0 README
  2. +82 −0 autoload/localrc.vim
  3. +128 −0 doc/localrc.txt
  4. +37 −0 plugin/localrc.vim
25 README
@@ -0,0 +1,25 @@
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=3393
+
+You can put a configuration file for the files below the certain directory.
+
+The configuration file is a Vim script of a decided file name like .vimrc.
+The file name is specified by |g:localrc_filename|.
+
+Configuration file applied only to a specific filetype can be put. The file
+name is specified by |g:localrc_filetype|.
+
+When two or more configuration files exist, it is sequentially loaded from a
+shallow hierarchy.
+For example:
+ ~/
+ |- .local.vimrc (1)
+ `- project/
+ |- .local.vimrc (2)
+ `- src/
+ |- .local.vimrc (3)
+ `- main.c
+In this case, when main.c is loaded, the configuration files are loaded in order
+of the number.
+
+Latest Version:
+https://github.com/thinca/vim-localrc
@@ -0,0 +1,82 @@
+" Enable configuration file of each directory.
+" Version: 0.1.0
+" Author : thinca <thinca+vim@gmail.com>
+" License: Creative Commons Attribution 2.1 Japan License
+" <http://creativecommons.org/licenses/by/2.1/jp/deed.en>
+
+let s:save_cpo = &cpo
+set cpo&vim
+
+
+function! localrc#load(fnames, ...) " {{{2
+ for file in localrc#search(a:fnames,
+ \ 1 <= a:0 ? a:1 : expand('%:p:h'),
+ \ 2 <= a:0 ? a:2 : -1)
+ " XXX: Handle error?
+ source `=file`
+ endfor
+endfunction
+
+function! localrc#search(fnames, ...) " {{{2
+ let path = 1 <= a:0 ? a:1 : expand('%:p:h')
+ if empty(a:fnames) || !isdirectory(path)
+ return []
+ endif
+
+ let depth = 2 <= a:0 ? a:2 : -1
+ let targets = []
+ let dir = fnamemodify(path, ':p:h')
+ let updir = ''
+ while depth != 0 && dir !=# updir
+ let targets = s:match_files(dir, a:fnames) + targets
+ let updir = dir
+ let dir = fnamemodify(dir, ':h')
+ let depth -= 1
+ endwhile
+ return targets
+endfunction
+
+function! s:match_files(path, fname) " {{{2
+ if type(a:fname) == type([])
+ let files = []
+ for f in a:fname
+ let files += s:match_files(a:path, f)
+ endfor
+ return s:uniq(files)
+ endif
+
+ let path = escape(a:path, '*?[,')
+ if a:fname[0] == '/'
+ let files = split(globpath(path, '/.*', 1), "\n")
+ \ + split(globpath(path, '/*' , 1), "\n")
+ let pat = a:fname[1:]
+ call filter(map(files, 'fnamemodify(v:val, ":t")'), 'v:val =~# pat')
+
+ else
+ let files = map(split(globpath(path, a:fname, 1), "\n"),
+ \ 'fnamemodify(v:val, ":t")')
+ endif
+
+ return filter(map(files, 'a:path . "/" . v:val'), 'filereadable(v:val)')
+endfunction
+
+" - string only.
+" - can not treat a empty string.
+function! s:uniq(list) " {{{2
+ let i = 0
+ let len = len(a:list)
+ let seen = {}
+ while i < len
+ if has_key(seen, a:list[i])
+ call remove(a:list, i)
+ else
+ let seen[a:list[i]] = 1
+ let i += 1
+ endif
+ endwhile
+ return a:list
+endfunction
+
+
+let &cpo = s:save_cpo
+unlet s:save_cpo
@@ -0,0 +1,128 @@
+*localrc.txt* Enable configuration file of each directory.
+
+Version: 0.1.0
+Author : thinca <thinca+vim@gmail.com>
+License: Creative Commons Attribution 2.1 Japan License
+ <http://creativecommons.org/licenses/by/2.1/jp/deed.en>
+
+==============================================================================
+CONTENTS *localrc-contents*
+
+INTRODUCTION |localrc-introduction|
+USAGE |localrc-usage|
+INTERFACE |localrc-interface|
+ FUNCTIONS |localrc-functions|
+CUSTOMIZING |localrc-customizing|
+EXAMPLES |localrc-examples|
+CHANGELOG |localrc-changelog|
+
+
+==============================================================================
+INTRODUCTION *localrc-introduction*
+
+*localrc* is a Vim plugin to enable configuration file of each directory.
+
+
+Requirements:
+- Vim 7.2 or later
+
+Latest version:
+https://github.com/thinca/vim-localrc
+
+
+
+==============================================================================
+USAGE *localrc-usage*
+
+You can put a configuration file for the files below the certain directory.
+
+The configuration file is a Vim script of a decided file name like .vimrc.
+The file name is specified by |g:localrc_filename|.
+
+Configuration file applied only to a specific filetype can be put. The file
+name is specified by |g:localrc_filetype|.
+
+When two or more configuration files exist, it is sequentially loaded from a
+shallow hierarchy.
+For example: >
+ ~/
+ |- .local.vimrc (1)
+ `- project/
+ |- .local.vimrc (2)
+ `- src/
+ |- .local.vimrc (3)
+ `- main.c
+In this case, when main.c is loaded, the configuration files are loaded in order
+of the number.
+
+
+
+==============================================================================
+INTERFACE *localrc-interface*
+
+------------------------------------------------------------------------------
+FUNCTIONS *localrc-functions*
+
+localrc#load({fnames} [, {path} [, {depth}]]) *localrc#load()*
+ Load settings with |:source|.
+
+ {fnames} is file names to load. This is a |List| or a string.
+ |wildcards| can be used for this. Additionally, this is treated as a
+ regular expression if it starts with "/".
+ {path} is a directory for search base directory. The directory must
+ exist. If omitted, a directory with a file of current buffer is used.
+ {depth} is a number of hierarchies of the traced directory. -1 is
+ all. If omitted, -1 is used.
+
+localrc#search({fnames} [, {path} [, {depth}]]) *localrc#search()*
+ Search the files to load and return it as |List|.
+ The arguments are same to |localrc#load()|.
+
+
+
+==============================================================================
+CUSTOMIZING *localrc-customizing*
+
+g:localrc_filename *g:localrc_filename*
+ The configuration file name. This is passed to {fnames} argument of
+ |localrc#load()|.
+
+ default: ".local.vimrc"
+
+g:localrc_filetype *g:localrc_filetype*
+ THe name of configuration file for specific filetype.
+ "%s" is replaced by filetype.
+
+ default: ".local.%s.vimrc"
+
+
+
+==============================================================================
+EXAMPLES *localrc-examples*
+
+Examples of configuration file.
+>
+ " cd to the directory with the configuration file.
+ lcd <sfile>:h
+
+ " Setting of indent to project.
+ setlocal tabstop=4 noexpandtab
+<
+You can load configuration files by favorite timing.
+For example, in vimrc...
+>
+ " Read the configuration file for the directory that starts Vim.
+ call localrc#load('.init.vimrc', getcwd())
+<
+
+
+
+==============================================================================
+CHANGELOG *localrc-changelog*
+
+0.1.0 2011-01-08
+ - Initial version.
+
+
+==============================================================================
+vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl
@@ -0,0 +1,37 @@
+" Enable configuration file of each directory.
+" Version: 0.1.0
+" Author : thinca <thinca+vim@gmail.com>
+" License: Creative Commons Attribution 2.1 Japan License
+" <http://creativecommons.org/licenses/by/2.1/jp/deed.en>
+
+if exists('g:loaded_localrc')
+ finish
+endif
+let g:loaded_localrc = 1
+
+let s:save_cpo = &cpo
+set cpo&vim
+
+if !exists('g:localrc_filename')
+ let g:localrc_filename = '.local.vimrc'
+endif
+
+if !exists('g:localrc_filetype')
+ let g:localrc_filetype = '.local.%s.vimrc'
+endif
+
+
+augroup plugin-localrc " {{{
+ autocmd!
+ autocmd BufNewFile,BufReadPost *
+ \ call localrc#load(g:localrc_filename, expand('<afile>:p:h'))
+ autocmd FileType *
+ \ call localrc#load(
+ \ map(type(g:localrc_filetype) == type([]) ? copy(g:localrc_filetype)
+ \ : [g:localrc_filetype],
+ \ 'printf(v:val, expand("<amatch>"))'), expand('<afile>:p:h'))
+augroup END " }}}
+
+
+let &cpo = s:save_cpo
+unlet s:save_cpo

0 comments on commit e2f3c0e

Please sign in to comment.