Skip to content

Commit

Permalink
Added cursor position observer for Vim (CUI)
Browse files Browse the repository at this point in the history
  • Loading branch information
tacahiroy committed May 5, 2012
1 parent bc5d176 commit 1000d62
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 34 deletions.
20 changes: 10 additions & 10 deletions README.md
Expand Up @@ -27,20 +27,20 @@ bestfriend.vim provide 2 commands to show your best friend.

* `:BestFriend` shows edit time of the current file.
```
1: ~/.vimrc 00:56 (01:04)
1: ~/.vimrc 00:56 (01:04)
```

* `:BestFriends` shows ranking of edit time.
```
1: ~/.vimrc 00:56 (01:04)
2: ~/foo/bar/baz.pl 00:33 (00:33)
3: ~/hoge/hige/huga.rb 00:01 (00:01)
4: ~/.zshrc 00:00 (00:00)
6: ~/Projects/vim/vim-bestfriend/autoload/bestfriend.vim 00:00 (00:03)
7: ~/Projects/vim/vim-bestfriend/plugin/bestfriend.vim 00:00 (00:01)
8: ~/Projects/vim/vim-bestfriend/README.md 00:00 (00:13)
9: ~/.zshenv 00:00 (00:00)
10: ~/.tmux.conf 00:00 (00:00)
1: ~/.vimrc 00:56 (01:04)
2: ~/foo/bar/baz.pl 00:33 (00:33)
3: ~/hoge/hige/huga.rb 00:01 (00:01)
4: ~/.zshrc 00:00 (00:00)
6: ~/Projects/vim/vim-bestfriend/autoload/bestfriend.vim 00:00 (00:03)
7: ~/Projects/vim/vim-bestfriend/plugin/bestfriend.vim 00:00 (00:01)
8: ~/Projects/vim/vim-bestfriend/README.md 00:00 (00:13)
9: ~/.zshenv 00:00 (00:00)
10: ~/.tmux.conf 00:00 (00:00)
```

Actually bestfriend.vim provides several options.
Expand Down
54 changes: 44 additions & 10 deletions autoload/bestfriend.vim
@@ -1,14 +1,22 @@
" autoload/bestfriend.vim
" Author: Takahiro YOSHIHARA <tacahiroy```AT```gmail.com>
" License: MIT License
" Version: 0.0.1

let s:saved_cpo = &cpo
set cpo&vim


let s:STOP = 0
let s:START = 1

" Utilities " {{{
" returns current file's absolute path
function! s:debug(msg)
if s:is_debug
echomsg a:msg
endif
endfunction

function! s:curfile()
return expand('%:p')
endfunction
Expand Down Expand Up @@ -102,23 +110,23 @@ function! s:BestFriend.start(f) dict
call self.add_file(a:f)
endif

if has_key(self.files[a:f], 'cursor_pos')
if getpos('.') != self.files[a:f].cursor_pos
call self.stop(a:f)
endif
if self.files[a:f].status == s:START
return
endif

" date might be changed when the file is being edited
call self.set_db(bestfriend#dbname(0))
let self.files[a:f].start = reltime()
let self.files[a:f].cursor_pos = getpos('.')
let self.files[a:f].status = s:START
let self.files[a:f].cursor_position = getpos('.')
endfunction

function! s:BestFriend.stop(f) dict
if !has_key(self.files, a:f)
if !self.has_file(a:f)
return
endif
if empty(self.files[a:f].start)

if self.files[a:f].status == s:STOP
return
endif

Expand All @@ -128,17 +136,40 @@ function! s:BestFriend.stop(f) dict
call self.reset(a:f)
endfunction

function! s:BestFriend.detect_cursor_move(f) dict
if !self.is_detect_cursor_move
return
endif

if !self.has_file(a:f)
return
endif

let cur_pos = getpos('.')

if self.files[a:f].cursor_position == cur_pos
call self.stop(a:f)
else
if self.files[a:f].status == s:STOP
call self.start(a:f)
else
let self.files[a:f].cursor_position = cur_pos
endif
endif
endfunction

function! s:BestFriend.has_file(f) dict
return has_key(self.files, a:f)
endfunction

function! s:BestFriend.add_file(f) dict
let self.files[a:f] = { 'start': [], 'end': [], 'total': 0 }
let self.files[a:f] = { 'start': [], 'end': [], 'total': 0, 'status': s:STOP }
endfunction

function! s:BestFriend.reset(f) dict
let self.files[a:f].start = []
let self.files[a:f].end = []
let self.files[a:f].status = s:STOP
endfunction

function! s:BestFriend.get_total(k) dict
Expand Down Expand Up @@ -182,7 +213,7 @@ function! s:BestFriend.load() dict
for f in files
for [k, v] in items(eval(f))
let self.files[k] = v
call extend(self.files[k], {'start': [], 'end': []})
call extend(self.files[k], {'start': [], 'end': [], 'status': s:STOP})
endfor
endfor
endfunction
Expand Down Expand Up @@ -356,6 +387,9 @@ let s:BestFriend.sort_function = s:BestFriend[s:sort_method]

let s:BestFriend.is_sort_base_today = get(g:, 'bestfriend_is_sort_base_today', 1)
let s:BestFriend.is_sort_order_desc = get(g:, 'bestfriend_is_sort_order_desc', 1)

let s:BestFriend.is_detect_cursor_move = get(g:, 'bestfriend_is_detect_cursor_move',
\ has('gui_running') ? 0 : 1)
" }}}


Expand Down
28 changes: 23 additions & 5 deletions doc/bestfriend.txt
@@ -1,7 +1,7 @@
*bestfriend.txt* Know your best friend.

*bestfriend.vim*
Version: 0.0.1
Version: 0.0.3
Author: tacahiroy <https://github.com/tacahiroy>


Expand Down Expand Up @@ -59,13 +59,21 @@ Note that pattern is compared to absolute path.
>
let g:bestfriend_accept_path_pattern = ''
<
>
e.g.:
let g:bestfriend_accept_path_pattern = '\(^~/project/\|^/etc\)'
<

*g:bestfriend_ignore_path_pattern*
A file is NOT managed that matched this pattern.
Note that this variable is prior to |g:bestfriend_accept_path_pattern|.>

Note that this variable is prior to |g:bestfriend_accept_path_pattern|.
>
let g:bestfriend_ignore_path_pattern = ''
<
>
e.g.:
let g:bestfriend_ignore_path_pattern = '\(^/tmp\|tags$\|NERD_tree_\d\+\)'
<

*g:bestfriend_is_display_zero*
If you want to see files that edit time is less than a minute, you must
Expand All @@ -76,7 +84,7 @@ set 1 (enabled) this variable.

*g:bestfriend_display_limit*
This variable controls display limit when you run command |:BestFriends|.
If 0 is specified, you can see ranking of all managed files.
If 0 (no limit) is specified, you can see ranking of all managed files.
>
let g:bestfriend_display_limit = 10
<
Expand All @@ -91,7 +99,17 @@ today's edit time. If 0 (disabled), total edit time.
This variable controls sort order. If 1 (enabled), sort order is descending.
If 0 (disabled), ascending.
>
let g:bestfriend_is_sort_order_desc = 1
let g:bestfriend_is_sort_order_desc = 1
<
*g:bestfriend_is_observe_cursor_position*
This variable is set to 1 (enabled), then |bestfriend.vim| observe cursor
position every |updatetime|. And then, if cursor position is not changed, time
calculation will be stopped. Basically, |bestfriend.vim| observe |FocusGained|,
|FocusLost| on gVim, but there are NOT these events on Vim (CUI). This
variable is used to understand the file is being edited or not on Vim.
>
" default: Vim: 1, gVim: 0
let g:bestfriend_is_observe_cursor_position = 0/1
<


Expand Down
13 changes: 4 additions & 9 deletions plugin/bestfriend.vim
@@ -1,7 +1,6 @@
" bestfriend.vim
" plugin/bestfriend.vim
" Author: Takahiro YOSHIHARA <tacahiroy```AT```gmail.com>
" License: MIT License
" Version: 0.0.1

if exists('g:loaded_bestfriend') || &cp
finish
Expand All @@ -11,6 +10,7 @@ let g:loaded_bestfriend = 1
if !has('reltime')
finish
endif

if !has('float')
finish
endif
Expand All @@ -25,9 +25,6 @@ let s:bf = bestfriend#new(bestfriend#dbname(0))
" total
let s:bf.summary = bestfriend#new(bestfriend#dbname(1))

let is_detect_cursor_move = get(g:, 'bestfriend_is_detect_cursor_move',
\ has('gui_running') ? 0 : 1)

" Command
command! -nargs=0 BestFriend call s:bf.show()
command! -nargs=0 BestFriends call s:bf.show('all')
Expand All @@ -38,10 +35,7 @@ augroup BestFriend

autocmd BufEnter,FocusGained * call s:bf.start(expand('%:p'))
autocmd BufLeave,FocusLost,VimLeave * call s:bf.stop(expand('%:p'))

if is_detect_cursor_move
autocmd CursorHold,CursorHoldI * call s:bf.start(expand('%:p'))
endif
autocmd CursorHold,CursorHoldI * call s:bf.detect_cursor_move(expand('%:p'))
augroup END


Expand All @@ -50,3 +44,4 @@ unlet s:saved_cpo

"__END__
" vim: fen fdm=marker ft=vim ts=2 sw=2 sts=2:

0 comments on commit 1000d62

Please sign in to comment.