Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 0.0.1: Refactoring.

  • Loading branch information...
commit 30135bb8d473d266a8e423b88ed52d03ae43668a 1 parent e86368b
@tyru tyru authored committed
Showing with 42 additions and 99 deletions.
  1. +41 −98 autoload/operator/camelize.vim
  2. +1 −1  doc/operator-camelize.txt
View
139 autoload/operator/camelize.vim
@@ -7,110 +7,53 @@ set cpo&vim
" }}}
-function! s:get_selected_text(motion_wiseness, begin_pos, end_pos) "{{{
- let [begin_lnum, begin_col] = a:begin_pos[1:2]
- let [end_lnum , end_col] = a:end_pos[1:2]
-
- if a:motion_wiseness ==# 'char'
- if begin_lnum == end_lnum
- let firstline = [getline(begin_lnum)[begin_col - 1 : end_col - 1]]
- let lastline = []
+function! s:select(motion_wiseness) "{{{
+ " From http://gist.github.com/356290
+ " But specialized to operator-user.
+
+ try
+ " For saving &selection. See :help :map-operator
+ let sel_save = &l:selection
+ let &l:selection = "inclusive"
+ " Save @@.
+ let reg_save = getreg('z', 1)
+ let regtype_save = getregtype('z')
+
+ if a:motion_wiseness == 'char'
+ let ex = '`[v`]'
+ elseif a:motion_wiseness == 'line'
+ let ex = '`[V`]'
+ elseif a:motion_wiseness == 'block'
+ let ex = "`[\<C-v>`]"
else
- let firstline = [getline(begin_lnum)[begin_col - 1 :]]
- let lastline = [getline(end_lnum)[: end_col - 1]]
+ " silent execute 'normal! `<' . a:motion_wiseness . '`>'
+ echoerr 'internal error, sorry: this block never be reached'
endif
- else
- " 'line' and 'block' are treated as same.
- if begin_lnum == end_lnum
- let firstline = [getline(begin_lnum)]
- let lastline = []
- else
- let firstline = [getline(begin_lnum)]
- let lastline = [getline(end_lnum)]
- endif
- endif
+ finally
+ let &l:selection = sel_save
+ call setreg('z', reg_save, regtype_save)
+ endtry
- return
- \ firstline
- \ + (begin_lnum + 1 <= end_lnum - 1 ?
- \ getline(begin_lnum + 1, end_lnum - 1)
- \ : [])
- \ + lastline
-endfunction "}}}
-function! s:zip(list, list2) "{{{
- let ret = []
- let i = 0
- while s:has_idx(a:list, i) || s:has_idx(a:list2, i)
- call add(ret,
- \ (s:has_idx(a:list, i) ? [a:list[i]] : [])
- \ + (s:has_idx(a:list2, i) ? [a:list2[i]] : []))
-
- let i += 1
- endwhile
- return ret
-endfunction "}}}
-function! s:has_idx(list, idx) "{{{
- let idx = a:idx
- " Support negative index?
- " let idx = a:idx >= 0 ? a:idx : len(a:list) + a:idx
- return 0 <= idx && idx < len(a:list)
+ execute 'silent normal!' ex
endfunction "}}}
function! s:operate_on_word(funcname, motion_wiseness) "{{{
- let [begin, end] = [getpos("'["), getpos("']")]
- let lines = s:get_selected_text(a:motion_wiseness, begin, end)
-
- if a:motion_wiseness ==# 'char'
- let [firstline_noreplace, lastline_noreplace] = [
- \ (begin[2] == 1 ? '' : getline(begin[1])[: begin[2] - 2]),
- \ (end[2] == strlen(getline(end[1])) + 1 ? '' : getline(end[1])[end[2] :])
- \]
- else
- let [firstline_noreplace, lastline_noreplace] = ['', '']
- endif
-
- let pat = '\w\+'
- let sub = '\=' . a:funcname . '(submatch(0))'
- let flags = 'g'
-
- if len(lines) == 1
- " First line
- let line = substitute(lines[0], pat, sub, flags)
- call setline(begin[1], firstline_noreplace . line . lastline_noreplace)
-
- elseif len(lines) == 2
- let [first_line, last_line] = [lines[0], lines[-1]]
-
- " First line
- let line = substitute(first_line, pat, sub, flags)
- call setline(begin[1], firstline_noreplace . line)
- " Second line
- let line = substitute(last_line, pat, sub, flags)
- call setline(end[1], line . lastline_noreplace)
-
- elseif len(lines) >= 3
- let [first_line, last_line] = [lines[0], lines[-1]]
- let middle_lines = lines[1:-2]
- let lnums = (begin[1] + 1 <= end[1] - 1 ? range(begin[1] + 1, end[1] - 1) : [])
-
- " First line
- let line = substitute(first_line, pat, sub, flags)
- call setline(begin[1], firstline_noreplace . line)
- " Middle lines
- for [line, lnum] in s:zip(middle_lines, lnums)
- let line = substitute(line, pat, sub, flags)
- call setline(lnum, line)
- endfor
- " Last line
- let line = substitute(last_line, pat, sub, flags)
- call setline(end[1], line . lastline_noreplace)
-
- else
- echoerr 'internal error'
- endif
+ " Select previously-selected range in visual mode.
+ call s:select(a:motion_wiseness)
+
+ let reg_z_save = getreg('z', 1)
+ let regtype_z_save = getregtype('z')
+
+ try
+ " Filter selected range with `{a:funcname}(selected_text)`.
+ let cut_with_reg_z = '"zc'
+ execute printf("normal! %s\<C-r>=%s(@z)\<CR>", cut_with_reg_z, a:funcname)
+ finally
+ call setreg('z', reg_z_save, regtype_z_save)
+ endtry
endfunction "}}}
-" snake_case -> SnakeCase
+" s:camelize_word('snake_case') " => 'SnakeCase'
function! s:camelize_word(word) "{{{
" NOTE: Nested sub-replace-expression is not recognized...omg
" (:help sub-replace-expression)
@@ -139,7 +82,7 @@ function! operator#camelize#camelize(motion_wiseness) "{{{
endfunction "}}}
-" CamelCase -> camel_case
+" s:decamelize_word('CamelCase') " => 'camel_case'
function! s:decamelize_word(word) "{{{
" NOTE: Nested sub-replace-expression is not recognized...omg
" (:help sub-replace-expression)
@@ -171,7 +114,7 @@ function! s:decamelize_word(word) "{{{
endfunction "}}}
function! operator#camelize#decamelize(motion_wiseness) "{{{
- call s:operate_on_word('s:decamelize_word', a:motion_wiseness)
+ call s:operate_on_word('<SID>decamelize_word', a:motion_wiseness)
endfunction "}}}
View
2  doc/operator-camelize.txt
@@ -2,7 +2,7 @@
Author:
tyru <tyru.exe@gmail.com>
-Version: 0.0.0
+Version: 0.0.1
License:
" NEW BSD LICENSE {{{
" Copyright (c) 2009, tyru
Please sign in to comment.
Something went wrong with that request. Please try again.