Skip to content

Commit

Permalink
dot.vim/autoload/narrow.vim, dot.vim/doc/narrow.txt, dot.vim/plugin/n…
Browse files Browse the repository at this point in the history
…arrow.vim:

* Initial version.

Makefile:
* Add new vim plugin "narrow".


git-svn-id: file:///c/cygwin/home/kana/var/svn-repos/config/trunk@647 4142a4a8-9c22-0410-a14c-65946c90a037
  • Loading branch information
kana authored and kana committed Mar 28, 2010
0 parents commit d499281
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 0 deletions.
148 changes: 148 additions & 0 deletions autoload/narrow.vim
@@ -0,0 +1,148 @@
" narrow - Emulate Emacs' narrowing feature
" Version: 0.0
" Copyright (C) 2007 kana <http://nicht.s8.xrea.com/>
" License: MIT license (see <http://www.opensource.org/licenses/mit-license>)
" $Id$
" Interfaces "{{{1
" MEMO: narrow-to-motion: v{motion}:Narrow<Return>

function! narrow#Narrow(line1, line2)
" FIXME: set 'foldtext' for appearance.
" Note that if you want to modify more options, don't forget to update
" s:save_the_state_of_buffer() and s:load_the_state_of_buffer().
if exists('b:narrow_original_state')
echo 'The buffer is already narrowed.'
return 0
endif

let b:narrow_original_state = s:save_the_state_of_buffer()
setlocal foldmethod=manual
call s:adjust_cursor_if_invoked_via_visual_mode(a:line1, a:line2)
let pos = getpos('.')
call s:clear_all_folds()
call s:fold_before(a:line1)
call s:fold_after(a:line2)
call setpos('.', pos)
echo mode()
return 1
endfunction




function! narrow#Widen()
if !exists('b:narrow_original_state')
echo 'The buffer is not narrowed.'
return 0
endif

call s:clear_all_folds()
call s:load_the_state_of_buffer(b:narrow_original_state)
unlet b:narrow_original_state
return 1
endfunction








" Misc. "{{{1
function! s:adjust_cursor_if_invoked_via_visual_mode(line1, line2) "{{{2
" Without this adjustment, the cursor is always positioned at '<.
" BUGS: this discriminant isn't perfect but sufficient.
if ((line('.') == a:line1 || line('.') == a:line2)
\ && (line("'<") == a:line1)
\ && (line("'>") == a:line2))
execute 'normal!' "gv\<Esc>"
endif
endfunction




function! s:fold_before(line) "{{{2
if 1 < a:line
execute '1,' (a:line - 1) 'fold'
endif
endfunction




function! s:fold_after(line) "{{{2
if a:line < line('$')
execute (a:line + 1) ',$' 'fold'
endif
endfunction




function! s:clear_all_folds() "{{{2
if &l:foldmethod != 'manual'
throw '&l:foldmethod must be "manual", but ' . string(&l:foldmethod)
endif

normal! zE
endfunction




" view options "{{{2
function! s:set_view_options()
let s:original_viewdir = &viewdir
let &viewdir = s:original_viewdir . '/narrow'
let s:original_viewoptions = &viewoptions
let &viewoptions = 'folds,cursor'
endfunction

function! s:restore_view_options()
let &viewdir = s:original_viewdir
let &viewoptions = s:original_viewoptions
endfunction




function! s:save_the_state_of_buffer() "{{{2
call s:set_view_options()
" BUGS: :mkview doesn't create intermediate directories.
if !isdirectory(&viewdir)
call mkdir(&viewdir, 'p', 0700)
endif
" BUGS: :mkview doesn't save folds info when &l:buftype isn't ''.
let original_buftype = &l:buftype
let &l:buftype = ''
mkview
let &l:buftype = original_buftype
call s:restore_view_options()

let original_state = {}
let original_state.foldmethod = &l:foldmethod
return original_state
endfunction




function! s:load_the_state_of_buffer(original_state) "{{{2
call s:set_view_options()
loadview
call s:restore_view_options()

let &l:foldmethod = a:original_state.foldmethod
endfunction








" __END__ "{{{1
" vim: foldmethod=marker
97 changes: 97 additions & 0 deletions doc/narrow.txt
@@ -0,0 +1,97 @@
*narrow.txt* Emulate Emacs' narrowing feature

Version 0.0
Copyright (C) 2007 kana <http://nicht.s8.xrea.com/>
License: MIT license (see <http://www.opensource.org/licenses/mit-license>)
$Id$




==============================================================================
INTRODUCTION *narrow-introduction*

Narrow is a Vim plugin to emulate Emacs' narrowing feature.
From GNU Emacs Manual:

Narrowing means focusing in on some portion of the buffer, making the
rest temporarily inaccessible. The portion which you can still get to
is called the accessible portion. Canceling the narrowing, which makes
the entire buffer once again accessible, is called widening. The
bounds of narrowing in effect in a buffer are called the buffer's
restriction.

Narrowing can make it easier to concentrate on a single subroutine or
paragraph by eliminating clutter. It can also be used to limit the
range of operation of a replace command or repeating keyboard macro.

Note that there are some differences from the original behavior.
See |narrow-bugs| for the detail.




==============================================================================
INTERFACES *narrow-interfaces*

*:Narrow*
:[range]Narrow
Narrow down to [range]. The default [range] is the current line.
If the buffer is already narrowed, nothing will happen.

*:Widen*
:Widen
Widen to make the entire buffer visible again.
If the buffer is not narrowed, nothing will happen.

*narrow#Narrow()*
narrow#Narrow({line1}, {line2})
Function version of |:Narrow|.
Narrow down to between {line1} and {line2}.
Return true on success or false on failure.

*narrow#Widen()*
narrow#Widen()
Function version of |:Widen|.
Return true on success or false on failure.

*b:narrow_original_state*
b:narrow_original_state
The variable to save the state of the buffer before narrowing.
This variable doesn't exist when the buffer is not narrowed.




==============================================================================
BUGS *narrow-bugs*

- Narrowing is based on Vim's |folding| feature. So you can
* See hidden portions as folds.
* Show a hidden portion by |zR| or other commands.
* Move the cursor into a hidden portion.
* Modify the content of a hidden portion by |:s| or other commands.
In Emacs, these actions aren't allowed.

- This plugin uses |:mkview| to save the current state of a buffer for
|:Widen|, and and |:mkview| writes a file. So this plugin may be confused
when there are two or more Vim processes and they narrow/widen the same
buffers.

- Temporary files written by |:mkview| will be created in the "narrow"
subdirectory in 'viewdir'. For example, "~/.vim/view/narrow".




==============================================================================
CHANGELOG *narrow-changelog*

0.0 2007-12-15T22:34:52+09:00
- Initial version.




==============================================================================
vim:tw=78:ts=8:ft=help:norl:
23 changes: 23 additions & 0 deletions plugin/narrow.vim
@@ -0,0 +1,23 @@
" narrow - Emulate Emacs' narrowing feature
" Version: 0.0
" Copyright (C) 2007 kana <http://nicht.s8.xrea.com/>
" License: MIT license (see <http://www.opensource.org/licenses/mit-license>)
" $Id$

if exists('g:loaded_narrow')
finish
endif




command -bar -range Narrow call narrow#Narrow(<line1>, <line2>)
command -bar Widen call narrow#Widen()




let g:loaded_narrow = 1

" __END__
" vim: foldmethod=marker

0 comments on commit d499281

Please sign in to comment.