Skip to content

Commit

Permalink
first commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
osyo-manga committed Apr 11, 2015
0 parents commit f9759e7
Show file tree
Hide file tree
Showing 70 changed files with 6,085 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
doc/tags-ja
37 changes: 37 additions & 0 deletions README.md
@@ -0,0 +1,37 @@
# hopping.vim


## Introduction

hopping.vim is incremental buffer line filtering Vim plugin.

## Installation

[Neobundle](https://github.com/Shougo/neobundle.vim) / [Vundle](https://github.com/gmarik/Vundle.vim) / [vim-plug](https://github.com/junegunn/vim-plug)

```vim
NeoBundle 'osyo-manga/vim-hopping'
Plugin 'osyo-manga/vim-hopping'
Plug 'osyo-manga/vim-hopping'
```

[pathogen](https://github.com/tpope/vim-pathogen)

```
git clone https://github.com/osyo-manga/vim-hopping ~/.vim/bundle/vim-hopping
```

## Screencapture


## Usage

```vim
" Start buffer line filtering.
:HoppingStart
" Example key mapping
nmap <Space>/ <Plug>(hopping-start)
```


230 changes: 230 additions & 0 deletions autoload/hopping.vim
@@ -0,0 +1,230 @@
scriptencoding utf-8
let s:save_cpo = &cpo
set cpo&vim

let g:hopping#debug_vital = get(g:, "hopping#debug_vital", 0)


function! hopping#load_vital()
if exists("s:V")
return s:V
endif
if g:hopping#debug_vital
let s:V = vital#of("vital")
else
let s:V = vital#of("hopping")
endif

let s:U = s:V.import("Unlocker.Rocker")
let s:H = s:V.import("Unlocker.Holder")
let s:Highlight = s:V.import("Coaster.Highlight")
let s:Position = s:V.import("Coaster.Position")
let s:Commandline = s:V.import("Over.Commandline")

return s:V
endfunction


function! hopping#reload_vital()
unlet! s:V
let s:V = hopping#vital()
endfunction
call hopping#load_vital()


let s:buffer = {}

function! s:buffer.pack(pat, cursor)
let text = []
let self.lnums = {}
let pos = s:Position.as(a:cursor)
for i in range(0, len(self.__text)-1)
let line = self.__text[i]
if line =~ a:pat
let self.lnums[len(text)+1] = i+1
call add(text, line)
if i+1 == pos[0]
let pos[0] = len(text)
endif
endif
endfor
return [pos, text]
endfunction


function! s:buffer.unpack(cursor)
if !exists("self.lnums")
return [a:cursor, self.__text]
endif
let pos = s:Position.as(a:cursor)
if has_key(self.lnums, pos[0])
let pos[0] = self.lnums[pos[0]]
endif
unlet self.lnums
return [pos, self.__text]
endfunction


function! s:make_packer(text)
let result = deepcopy(s:buffer)
let result.__text = a:text
return result
endfunction


let s:filter = {
\ "name" : "IncFilter"
\}


function! s:filter.set_buffer_text(text)
" if len(a:text) == len(get(self, "prev_text", []))
" return
" endif
" let self.prev_text = a:text

silent % delete _
call setline(1, a:text)
" call self.buffer.set(a:text)

let &modified = 0
endfunction


function! s:filter.reset()
call s:Highlight.clear_all()
call s:Highlight.highlight("cursor", "Cursor", s:Position.as_pattern(getpos(".")))
endfunction


function! s:filter.highlight(pat, pos)
if a:pat == ""
call s:Highlight.clear("search")
else
call s:Highlight.highlight("search", "Search", a:pat)
endif
call s:Highlight.highlight("cursor", "Cursor", s:Position.as_pattern(a:pos))
endfunction


function! s:filter.update(pat)
if a:pat != ""
call searchpos(a:pat, "c")
let @/ = a:pat
endif

let [pos, before] = self.buffer_packer.unpack(getpos("."))
let [pos, text] = self.buffer_packer.pack(a:pat, pos)

if empty(text)
let text = before
endif
call self.set_buffer_text(text)

if a:pat == ""
call self.view.relock()
else
call cursor(pos[0], pos[1])
endif

call self.highlight(a:pat, pos)

" call self.highlight(a:pat, getpos("."))
endfunction


function! s:filter.on_char_pre(cmdline)
if a:cmdline.is_input("\<A-n>")
silent! normal! n
call a:cmdline.setchar("")
let self.is_stay = 1
endif
if a:cmdline.is_input("\<A-p>")
silent! normal! N
call a:cmdline.setchar("")
let self.is_stay = 1
endif
if a:cmdline.is_input("\<A-r>")
redraw
execute "normal" input(":normal ")
call a:cmdline.setchar("")
endif
endfunction


function! s:filter.on_char(cmdline)
let input = a:cmdline.getline()
if !a:cmdline._is_exit()
" if a:cmdline.char() != ""
call self.update(input)
endif
endfunction


function! s:filter.on_execute_pre(cmdline)
let self.is_execute = 1
if self.is_stay
call a:cmdline.setline("")
let [pos, text] = self.buffer_packer.unpack(getpos("."))
call self.set_buffer_text(text)
call cursor(pos[0], pos[1])
endif
endfunction


function! s:filter.on_enter(cmdline)
let self.buffer = s:H.make("Buffer.Text", "%")

let self.buffer_lnum = line("$")

let self.view = s:U.lock(s:H.make("Winview"))
let self.buffer_packer = s:make_packer(self.buffer.get())
let self.is_stay = 0
let self.locker = s:U.lock(
\ self.buffer,
\ "&modified",
\ "&modifiable",
\ "&statusline",
\ "&cursorline",
\ s:H.make("Buffer.Undofile"),
\ )
let &modifiable = 1
let &cursorline = 1
call self.update("")
endfunction


function! s:filter.on_leave(cmdline)
call s:Highlight.clear_all()
let [pos, text] = self.buffer_packer.unpack(getpos("."))
call self.locker.unlock()
if self.is_stay == 0
call self.view.unlock()
endif
endfunction


let s:cmdline = s:Commandline.make_standard_search("Input:> ")
call s:cmdline.connect(s:filter)


let g:hopping#prompt = get(g:, "hopping#prompt", "Input:> ")

function! s:start(config)
call s:cmdline.set_prompt(a:config.prompt)
let exit_code = s:cmdline.start(a:config.input)
return exit_code
endfunction


function! hopping#start(...)
return s:start({
\ "prompt" : g:hopping#prompt,
\ "input" : get(a:, 1, "")
\ })
endfunction



let &cpo = s:save_cpo
unlet s:save_cpo
12 changes: 12 additions & 0 deletions autoload/vital.vim
@@ -0,0 +1,12 @@
function! vital#of(name) abort
let files = globpath(&runtimepath, 'autoload/vital/' . a:name . '.vital')
let file = split(files, "\n")
if empty(file)
throw 'vital: version file not found: ' . a:name
endif
let ver = readfile(file[0], 'b')
if empty(ver)
throw 'vital: invalid version file: ' . a:name
endif
return vital#_{substitute(ver[0], '\W', '', 'g')}#new()
endfunction

0 comments on commit f9759e7

Please sign in to comment.